/** * Parses a ccnb-encoded name * @param d is the decoder * @param components may be NULL, otherwise is filled in with the * Component boundary offsets * @returns the number of Components in the Name, or -1 if there is an error. */ int ccn_parse_Name(struct ccn_buf_decoder *d, struct ccn_indexbuf *components) { int ncomp = 0; if (ccn_buf_match_dtag(d, CCN_DTAG_Name)) { if (components != NULL) components->n = 0; ccn_buf_advance(d); while (ccn_buf_match_dtag(d, CCN_DTAG_Component)) { if (components != NULL) ccn_indexbuf_append_element(components, d->decoder.token_index); ncomp += 1; ccn_buf_advance(d); if (ccn_buf_match_blob(d, NULL, NULL)) ccn_buf_advance(d); ccn_buf_check_close(d); } if (components != NULL) ccn_indexbuf_append_element(components, d->decoder.token_index); ccn_buf_check_close(d); } else d->decoder.state = -__LINE__; if (d->decoder.state < 0) return(-1); else return(ncomp); }
int ccn_parse_PublisherID(struct ccn_buf_decoder *d, struct ccn_parsed_interest *pi) { int res = -1; int iskey = 0; unsigned pubstart = d->decoder.token_index; unsigned keystart = pubstart; unsigned keyend = pubstart; unsigned pubend = pubstart; iskey = ccn_buf_match_dtag(d, CCN_DTAG_PublisherPublicKeyDigest); if (iskey || ccn_buf_match_dtag(d, CCN_DTAG_PublisherCertificateDigest) || ccn_buf_match_dtag(d, CCN_DTAG_PublisherIssuerKeyDigest) || ccn_buf_match_dtag(d, CCN_DTAG_PublisherIssuerCertificateDigest)) { res = d->decoder.element_index; ccn_buf_advance(d); keystart = d->decoder.token_index; if (!ccn_buf_match_some_blob(d)) return (d->decoder.state = -__LINE__); ccn_buf_advance(d); keyend = d->decoder.token_index; ccn_buf_check_close(d); pubend = d->decoder.token_index; } if (d->decoder.state < 0) return (d->decoder.state); if (pi != NULL) { pi->offset[CCN_PI_B_PublisherID] = pubstart; pi->offset[CCN_PI_B_PublisherIDKeyDigest] = keystart; pi->offset[CCN_PI_E_PublisherIDKeyDigest] = iskey ? keyend : keystart; pi->offset[CCN_PI_E_PublisherID] = pubend; } return(res); }
/* * utility, may need to be exported, to parse the buffer into a given slice * structure. */ static int slice_parse(struct ccns_slice *s, const unsigned char *p, size_t size) { int res = 0; struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d = ccn_buf_decoder_start(&decoder, p, size); uintmax_t version; int op; int start; struct ccn_charbuf *clause = NULL; if (!ccn_buf_match_dtag(d, CCN_DTAG_SyncConfigSlice)) return (-1); ccn_buf_advance(d); if (!ccn_buf_match_dtag(d, CCN_DTAG_SyncVersion)) return (-1); ccn_buf_advance(d); ccn_parse_uintmax(d, &version); ccn_buf_check_close(d); if (version != SLICE_VERSION) return (-1); start = d->decoder.token_index; if (ccn_parse_Name(d, NULL) < 0) return(-1); ccn_charbuf_reset(s->topo); res = ccn_charbuf_append(s->topo, p + start, d->decoder.token_index - start); if (res < 0) return(-1); start = d->decoder.token_index; if (ccn_parse_Name(d, NULL) < 0) return(-1); ccn_charbuf_reset(s->prefix); res = ccn_charbuf_append(s->prefix, p + start, d->decoder.token_index - start); if (res < 0) return(-1); if (!ccn_buf_match_dtag(d, CCN_DTAG_SyncConfigSliceList)) return(-1); ccn_buf_advance(d); clause = ccn_charbuf_create(); if (clause == NULL) return(-1); while (ccn_buf_match_dtag(d, CCN_DTAG_SyncConfigSliceOp)) { ccn_buf_advance(d); op = ccn_parse_nonNegativeInteger(d); // op is a small integer ccn_buf_check_close(d); if (op != 0) break; ccn_charbuf_reset(clause); start = d->decoder.token_index; if (ccn_parse_Name(d, NULL) < 0) break; res = ccn_charbuf_append(clause, p + start, d->decoder.token_index - start); ccns_slice_add_clause(s, clause); } ccn_charbuf_destroy(&clause); ccn_buf_check_close(d); /* </SyncConfigSliceList> */ ccn_buf_check_close(d); /* </SyncConfigSlice> */ if (d->decoder.index != size || !CCN_FINAL_DSTATE(d->decoder.state)) return(-1); return(0); }
int ccn_dhcp_content_parse(const unsigned char *p, size_t size, struct ccn_dhcp_entry *tail) { struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d = ccn_buf_decoder_start(&decoder, p, size); int i; int count; struct ccn_dhcp_entry *de = tail; if (ccn_buf_match_dtag(d, CCN_DTAG_Entry)) { ccn_buf_advance(d); count = ccn_parse_optional_tagged_nonNegativeInteger(d, CCN_DTAG_Count); for (i = 0; i < count; i ++) { struct ccn_charbuf *store = ccn_charbuf_create(); size_t start; size_t end; int host_off = -1; int port_off = -1; de->next = calloc(1, sizeof(*de)); de = de->next; memset(de, 0, sizeof(*de)); de->store = store; de->next = NULL; if (ccn_buf_match_dtag(d, CCN_DTAG_Name)) { de->name_prefix = ccn_charbuf_create(); start = d->decoder.token_index; ccn_parse_Name(d, NULL); end = d->decoder.token_index; ccn_charbuf_append(de->name_prefix, p + start, end - start); } else de->name_prefix = NULL; host_off = ccn_parse_tagged_string(d, CCN_DTAG_Host, store); port_off = ccn_parse_tagged_string(d, CCN_DTAG_Port, store); char *b = (char *)store->buf; char *h = b + host_off; char *p = b + port_off; if (host_off >= 0) memcpy((void *)de->address, h, strlen(h)); if (port_off >= 0) memcpy((void *)de->port, p, strlen(p)); } } else d->decoder.state = -__LINE__; if (d->decoder.index != size || !CCN_FINAL_DSTATE(d->decoder.state)) ccn_dhcp_content_destroy(tail->next); return count; }
static struct ccn_buf_decoder * ccn_buf_decoder_start_at_components(struct ccn_buf_decoder *d, const unsigned char *buf, size_t buflen) { ccn_buf_decoder_start(d, buf, buflen); while (ccn_buf_match_dtag(d, CCN_DTAG_Name) || ccn_buf_match_dtag(d, CCN_DTAG_Interest) || ccn_buf_match_dtag(d, CCN_DTAG_ContentObject) ) { ccn_buf_advance(d); ccn_parse_Signature(d, NULL); } return(d); }
int ccn_ref_tagged_string(enum ccn_dtag dtag, const unsigned char *buf, size_t start, size_t stop, const unsigned char **presult, size_t *psize) { struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d; const unsigned char *result = NULL; size_t size = 0; if (stop < start) return(-1); d = ccn_buf_decoder_start(&decoder, buf + start, stop - start); if (ccn_buf_match_dtag(d, dtag)) { ccn_buf_advance(d); if (d->decoder.state >= 0 && CCN_GET_TT_FROM_DSTATE(d->decoder.state) == CCN_UDATA) { result = d->buf + d->decoder.index; size = d->decoder.numval; ccn_buf_advance(d); } ccn_buf_check_close(d); } else return(-1); if (d->decoder.index != d->size || !CCN_FINAL_DSTATE(d->decoder.state)) return (CCN_DSTATE_ERR_CODING); if (presult) *presult = result; if (psize) *psize = size; return(0); }
int ccn_parse_optional_tagged_UDATA(struct ccn_buf_decoder *d, enum ccn_dtag dtag) { if (ccn_buf_match_dtag(d, dtag)) return(ccn_parse_required_tagged_UDATA(d, dtag)); return(-1); }
/** * Parses a ccnb-encoded element expected to contain a UDATA string. * @param d is the decoder * @param dtag is the expected dtag value * @param store - on success, the string value is appended to store, * with null termination. * @returns the offset into the store buffer of the copied value, or -1 for error. * If a parse error occurs, d->decoder.state is set to a negative value. * If the element is not present, -1 is returned but no parse error * is indicated. */ int ccn_parse_tagged_string(struct ccn_buf_decoder *d, enum ccn_dtag dtag, struct ccn_charbuf *store) { const unsigned char *p = NULL; size_t size = 0; int res; if (ccn_buf_match_dtag(d, dtag)) { ccn_buf_advance(d); if (d->decoder.state >= 0 && CCN_GET_TT_FROM_DSTATE(d->decoder.state) == CCN_UDATA) { res = store->length; p = d->buf + d->decoder.index; size = d->decoder.numval; ccn_buf_advance(d); } ccn_buf_check_close(d); if (d->decoder.state >= 0) { // XXX - should check for valid utf-8 data. res = store->length; if (size > 0) ccn_charbuf_append(store, p, size); ccn_charbuf_append_value(store, 0, 1); return(res); } } return(-1); }
static int ccn_parse_Signature(struct ccn_buf_decoder *d, struct ccn_parsed_ContentObject *x) { int res = -1; int i; struct ccn_parsed_ContentObject dummy; if (x == NULL) x = &dummy; for (i = CCN_PCO_B_Signature; i <= CCN_PCO_E_Signature; i++) { x->offset[i] = d->decoder.token_index; } if (ccn_buf_match_dtag(d, CCN_DTAG_Signature)) { res = d->decoder.element_index; ccn_buf_advance(d); x->offset[CCN_PCO_B_DigestAlgorithm] = d->decoder.token_index; ccn_parse_optional_tagged_UDATA(d, CCN_DTAG_DigestAlgorithm); x->offset[CCN_PCO_E_DigestAlgorithm] = d->decoder.token_index; x->offset[CCN_PCO_B_Witness] = d->decoder.token_index; ccn_parse_optional_tagged_BLOB(d, CCN_DTAG_Witness, 8, -1); x->offset[CCN_PCO_E_Witness] = d->decoder.token_index; x->offset[CCN_PCO_B_SignatureBits] = d->decoder.token_index; ccn_parse_required_tagged_BLOB(d, CCN_DTAG_SignatureBits, 16, -1); x->offset[CCN_PCO_E_SignatureBits] = d->decoder.token_index; ccn_buf_check_close(d); x->offset[CCN_PCO_E_Signature] = d->decoder.token_index; } if (d->decoder.state < 0) return (d->decoder.state); return(res); }
uintmax_t ccn_parse_required_tagged_binary_number(struct ccn_buf_decoder *d, enum ccn_dtag dtag, int minlen, int maxlen) { uintmax_t value = 0; const unsigned char *p = NULL; size_t len = 0; int i; if (0 <= minlen && minlen <= maxlen && maxlen <= sizeof(value) && ccn_buf_match_dtag(d, dtag)) { ccn_buf_advance(d); if (ccn_buf_match_blob(d, &p, &len)) ccn_buf_advance(d); ccn_buf_check_close(d); if (d->decoder.state < 0) return(value); if (minlen <= len && len <= maxlen) for (i = 0; i < len; i++) value = (value << 8) + p[i]; else d->decoder.state = -__LINE__; } else d->decoder.state = -__LINE__; return(value); }
int ccn_parse_ContentObject(const unsigned char *msg, size_t size, struct ccn_parsed_ContentObject *x, struct ccn_indexbuf *components) { struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d = ccn_buf_decoder_start(&decoder, msg, size); int res; x->magic = 20090415; x->digest_bytes = 0; if (ccn_buf_match_dtag(d, CCN_DTAG_ContentObject)) { ccn_buf_advance(d); res = ccn_parse_Signature(d, x); x->offset[CCN_PCO_B_Name] = d->decoder.token_index; x->offset[CCN_PCO_B_Component0] = d->decoder.index; res = ccn_parse_Name(d, components); if (res < 0) d->decoder.state = -__LINE__; x->name_ncomps = res; x->offset[CCN_PCO_E_ComponentLast] = d->decoder.token_index - 1; x->offset[CCN_PCO_E_Name] = d->decoder.token_index; ccn_parse_SignedInfo(d, x); x->offset[CCN_PCO_B_Content] = d->decoder.token_index; ccn_parse_required_tagged_BLOB(d, CCN_DTAG_Content, 0, -1); x->offset[CCN_PCO_E_Content] = d->decoder.token_index; ccn_buf_check_close(d); x->offset[CCN_PCO_E] = d->decoder.index; } else d->decoder.state = -__LINE__; if (d->decoder.index != size || !CCN_FINAL_DSTATE(d->decoder.state)) return (CCN_DSTATE_ERR_CODING); return(0); }
uintmax_t ccn_parse_optional_tagged_binary_number(struct ccn_buf_decoder *d, enum ccn_dtag dtag, int minlen, int maxlen, uintmax_t default_value) { if (ccn_buf_match_dtag(d, dtag)) return(ccn_parse_required_tagged_binary_number(d, dtag, minlen, maxlen)); return(default_value); }
int ccn_parse_optional_tagged_BLOB(struct ccn_buf_decoder *d, enum ccn_dtag dtag, int minlen, int maxlen) { if (ccn_buf_match_dtag(d, dtag)) return(ccn_parse_required_tagged_BLOB(d, dtag, minlen, maxlen)); return(-1); }
int ccn_parse_Exclude(struct ccn_buf_decoder *d) { int res = -1; if (ccn_buf_match_dtag(d, CCN_DTAG_Exclude)) { res = d->decoder.element_index; ccn_buf_advance(d); ccn_parse_optional_Any_or_Bloom(d); while (ccn_buf_match_dtag(d, CCN_DTAG_Component)) { ccn_parse_required_tagged_BLOB(d, CCN_DTAG_Component, 0, -1); ccn_parse_optional_Any_or_Bloom(d); } ccn_buf_check_close(d); } if (d->decoder.state < 0) return (d->decoder.state); return(res); }
int ccn_parse_Link(struct ccn_buf_decoder *d, struct ccn_parsed_Link *link, struct ccn_indexbuf *components) { int ncomp = 0; int res; if (ccn_buf_match_dtag(d, CCN_DTAG_Link)) { if (components == NULL) { /* We need to have the component offsets. */ components = ccn_indexbuf_create(); if (components == NULL) return(-1); res = ccn_parse_Link(d, link, components); ccn_indexbuf_destroy(&components); return(res); } ccn_buf_advance(d); link->offset[CCN_PL_B_Name] = d->decoder.element_index; link->offset[CCN_PL_B_Component0] = d->decoder.index; ncomp = ccn_parse_Name(d, components); if (d->decoder.state < 0) { memset(link->offset, 0, sizeof(link->offset)); return(d->decoder.state); } link->offset[CCN_PL_E_ComponentLast] = d->decoder.token_index - 1; link->offset[CCN_PL_E_Name] = d->decoder.token_index; link->name_ncomps = ncomp; /* parse optional Label string */ link->offset[CCN_PL_B_Label] = d->decoder.token_index; res = ccn_parse_optional_tagged_UDATA(d, CCN_DTAG_Label); link->offset[CCN_PL_E_Label] = d->decoder.token_index; /* parse optional LinkAuthenticator LinkAuthenticatorType */ if (ccn_buf_match_dtag(d, CCN_DTAG_LinkAuthenticator)) res = ccn_parse_LinkAuthenticator(d, link); ccn_buf_check_close(d); } else return (d->decoder.state = -__LINE__); if (d->decoder.state < 0) return (d->decoder.state); return(ncomp); }
int ccn_name_last_component_offset(const unsigned char *ccnb, size_t size) //CONET was static { struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d = ccn_buf_decoder_start(&decoder, ccnb, size); int res = -1; if (ccn_buf_match_dtag(d, CCN_DTAG_Name)) { ccn_buf_advance(d); res = d->decoder.token_index; /* in case of 0 components */ while (ccn_buf_match_dtag(d, CCN_DTAG_Component)) { res = d->decoder.token_index; ccn_buf_advance(d); if (ccn_buf_match_blob(d, NULL, NULL)) ccn_buf_advance(d); ccn_buf_check_close(d); } ccn_buf_check_close(d); } return ((d->decoder.state >= 0) ? res : -1); }
int ccn_uri_append(struct ccn_charbuf *c, const unsigned char *ccnb, size_t size, int includescheme) { int ncomp = 0; const unsigned char *comp = NULL; size_t compsize = 0; struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d = ccn_buf_decoder_start(&decoder, ccnb, size); if (ccn_buf_match_dtag(d, CCN_DTAG_Interest) || ccn_buf_match_dtag(d, CCN_DTAG_ContentObject)) { ccn_buf_advance(d); if (ccn_buf_match_dtag(d, CCN_DTAG_Signature)) ccn_buf_advance_past_element(d); } if (!ccn_buf_match_dtag(d, CCN_DTAG_Name)) return(-1); if (includescheme) ccn_charbuf_append_string(c, "ccnx:"); ccn_buf_advance(d); while (ccn_buf_match_dtag(d, CCN_DTAG_Component)) { ccn_buf_advance(d); compsize = 0; if (ccn_buf_match_blob(d, &comp, &compsize)) ccn_buf_advance(d); ccn_buf_check_close(d); if (d->decoder.state < 0) return(d->decoder.state); ncomp += 1; ccn_charbuf_append(c, "/", 1); ccn_uri_append_percentescaped(c, comp, compsize); } ccn_buf_check_close(d); if (d->decoder.state < 0) return (d->decoder.state); if (ncomp == 0) ccn_charbuf_append(c, "/", 1); return(ncomp); }
int ccn_parse_optional_tagged_nonNegativeInteger(struct ccn_buf_decoder *d, enum ccn_dtag dtag) { int res = -1; if (ccn_buf_match_dtag(d, dtag)) { ccn_buf_advance(d); res = ccn_parse_nonNegativeInteger(d); ccn_buf_check_close(d); } if (d->decoder.state < 0) return (d->decoder.state); return(res); }
int ccn_compare_names(const unsigned char *a, size_t asize, const unsigned char *b, size_t bsize) { struct ccn_buf_decoder a_decoder; struct ccn_buf_decoder b_decoder; struct ccn_buf_decoder *aa = ccn_buf_decoder_start_at_components(&a_decoder, a, asize); struct ccn_buf_decoder *bb = ccn_buf_decoder_start_at_components(&b_decoder, b, bsize); const unsigned char *acp = NULL; const unsigned char *bcp = NULL; size_t acsize; size_t bcsize; int cmp = 0; int more_a; for (;;) { more_a = ccn_buf_match_dtag(aa, CCN_DTAG_Component); cmp = more_a - ccn_buf_match_dtag(bb, CCN_DTAG_Component); if (more_a == 0 || cmp != 0) break; ccn_buf_advance(aa); ccn_buf_advance(bb); acsize = bcsize = 0; if (ccn_buf_match_blob(aa, &acp, &acsize)) ccn_buf_advance(aa); if (ccn_buf_match_blob(bb, &bcp, &bcsize)) ccn_buf_advance(bb); cmp = acsize - bcsize; if (cmp != 0) break; cmp = memcmp(acp, bcp, acsize); if (cmp != 0) break; ccn_buf_check_close(aa); ccn_buf_check_close(bb); } return (cmp); }
int ccn_parse_Collection_start(struct ccn_buf_decoder *d) { if (ccn_buf_match_dtag(d, CCN_DTAG_Collection)) { ccn_buf_advance(d); } else return (d->decoder.state = -__LINE__); if (d->decoder.state < 0) return (d->decoder.state); return(0); }
int ccn_parse_LinkAuthenticator(struct ccn_buf_decoder *d, struct ccn_parsed_Link *pl) { /* Implement with a single offset for the blob, CCN_PL_[BE]_PublisherDigest * and remember the DTAG value to indicate which type of digest it is */ if (ccn_buf_match_dtag(d, CCN_DTAG_LinkAuthenticator)) { ccn_buf_advance(d); // advance over DTAG token pl->offset[CCN_PL_B_LinkAuthenticator] = d->decoder.token_index; pl->offset[CCN_PL_B_PublisherID] = d->decoder.token_index; pl->offset[CCN_PL_B_PublisherDigest] = d->decoder.token_index; pl->offset[CCN_PL_E_PublisherDigest] = d->decoder.token_index; if (ccn_buf_match_dtag(d, CCN_DTAG_PublisherPublicKeyDigest) || ccn_buf_match_dtag(d, CCN_DTAG_PublisherCertificateDigest) || ccn_buf_match_dtag(d, CCN_DTAG_PublisherIssuerKeyDigest) || ccn_buf_match_dtag(d, CCN_DTAG_PublisherIssuerCertificateDigest)) { pl->publisher_digest_type = d->decoder.numval; // remember the DTAG ccn_buf_advance(d); // over the DTAG token if (!ccn_buf_match_some_blob(d)) return (d->decoder.state = -__LINE__); pl->offset[CCN_PL_B_PublisherDigest] = d->decoder.token_index; ccn_buf_advance(d); // over the digest pl->offset[CCN_PL_E_PublisherDigest] = d->decoder.token_index; ccn_buf_check_close(d); // over the DTAG closer } if (d->decoder.state < 0) return (d->decoder.state); pl->offset[CCN_PL_E_PublisherID] = d->decoder.token_index; /* parse optional NameComponentCount nonNegativeInteger */ pl->offset[CCN_PL_B_NameComponentCount] = d->decoder.token_index; pl->name_component_count = ccn_parse_optional_tagged_nonNegativeInteger(d, CCN_DTAG_NameComponentCount); pl->offset[CCN_PL_E_NameComponentCount] = d->decoder.token_index; /* parse optional Timestamp TimestampType */ pl->offset[CCN_PL_B_Timestamp] = d->decoder.token_index; if (ccn_buf_match_dtag(d, CCN_DTAG_Timestamp)) ccn_parse_required_tagged_timestamp(d, CCN_DTAG_Timestamp); pl->offset[CCN_PL_E_Timestamp] = d->decoder.token_index; /* parse optional Type ContentType */ pl->offset[CCN_PL_B_Type] = d->decoder.token_index; pl->type = ccn_parse_optional_tagged_binary_number(d, CCN_DTAG_Type, 3, 3, CCN_CONTENT_DATA); pl->offset[CCN_PL_E_Type] = d->decoder.token_index; /* parse optional ContentDigest Base64BinaryType */ pl->offset[CCN_PL_B_ContentDigest] = d->decoder.token_index; ccn_parse_optional_tagged_BLOB(d, CCN_DTAG_ContentDigest, 32, 32); pl->offset[CCN_PL_E_ContentDigest] = d->decoder.token_index; ccn_buf_check_close(d); pl->offset[CCN_PL_E_LinkAuthenticator] = d->decoder.token_index; } else d->decoder.state = -__LINE__; if (!CCN_FINAL_DSTATE(d->decoder.state)) return (CCN_DSTATE_ERR_CODING); return(0); }
int ccn_parse_Collection_next(struct ccn_buf_decoder *d, struct ccn_parsed_Link *link, struct ccn_indexbuf *components) { if (ccn_buf_match_dtag(d, CCN_DTAG_Link)) { return(ccn_parse_Link(d, link, components)); } else ccn_buf_check_close(d); if (d->decoder.state < 0) return(d->decoder.state); else return(0); }
static int ccn_parse_optional_Any_or_Bloom(struct ccn_buf_decoder *d) { int res; res = ccn_parse_optional_tagged_BLOB(d, CCN_DTAG_Bloom, 1, 1024+8); if (res >= 0) return(res); if (ccn_buf_match_dtag(d, CCN_DTAG_Any)) { ccn_buf_advance(d); ccn_buf_check_close(d); res = 0; } if (d->decoder.state < 0) return (d->decoder.state); return(res); }
int ccn_parse_required_tagged_timestamp(struct ccn_buf_decoder *d, enum ccn_dtag dtag) { int res = -1; if (ccn_buf_match_dtag(d, dtag)) { res = d->decoder.element_index; ccn_buf_advance(d); ccn_parse_timestamp(d); ccn_buf_check_close(d); } else d->decoder.state = -__LINE__; if (d->decoder.state < 0) return (-1); return(res); }
int ccn_ref_tagged_BLOB(enum ccn_dtag tt, const unsigned char *buf, size_t start, size_t stop,const unsigned char **presult, size_t *psize) { struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d; if (stop < start) return(-1); d = ccn_buf_decoder_start(&decoder, buf + start, stop - start); if (ccn_buf_match_dtag(d, tt)) { ccn_buf_advance(d); if (ccn_buf_match_blob(d, presult, psize)) ccn_buf_advance(d); ccn_buf_check_close(d); } else return(-1); if (d->decoder.index != d->size || !CCN_FINAL_DSTATE(d->decoder.state)) return (CCN_DSTATE_ERR_CODING); return(0); }
/** * Append Components from a ccnb-encoded Name to a flatname * * The ccnb encoded input may be a ContentObject, Interest, Prefix, * or Component instead of simply a Name. * @param dst is the destination, which should hold a ccnb-encoded Name * @param ccnb points to first byte of Name * @param size is the number of bytes in ccnb * @param skip is the number of components at the front of flatname to skip * @param count is the maximum number of componebts to append, or -1 for all * @returns number of appended components, or -1 if there is an error. */ int ccn_flatname_append_from_ccnb(struct ccn_charbuf *dst, const unsigned char *ccnb, size_t size, int skip, int count) { int ans = 0; int ncomp = 0; const unsigned char *comp = NULL; size_t compsize = 0; struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d = ccn_buf_decoder_start(&decoder, ccnb, size); int checkclose = 0; int res; if (ccn_buf_match_dtag(d, CCN_DTAG_Interest) || ccn_buf_match_dtag(d, CCN_DTAG_ContentObject)) { ccn_buf_advance(d); if (ccn_buf_match_dtag(d, CCN_DTAG_Signature)) ccn_buf_advance_past_element(d); } if ((ccn_buf_match_dtag(d, CCN_DTAG_Name) || ccn_buf_match_dtag(d, CCN_DTAG_Prefix))) { checkclose = 1; ccn_buf_advance(d); } else if (count != 0) count = 1; while (ccn_buf_match_dtag(d, CCN_DTAG_Component)) { if (ans == count) return(ans); ccn_buf_advance(d); compsize = 0; if (ccn_buf_match_blob(d, &comp, &compsize)) ccn_buf_advance(d); ccn_buf_check_close(d); if (d->decoder.state < 0) return(-1); ncomp += 1; if (ncomp > skip) { res = ccn_flatname_append_component(dst, comp, compsize); if (res < 0) return(-1); ans++; } } if (checkclose) ccn_buf_check_close(d); if (d->decoder.state < 0) return (-1); return(ans); }
int ccn_fetch_tagged_nonNegativeInteger(enum ccn_dtag tt, const unsigned char *buf, size_t start, size_t stop) { struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d; int result = -1; if (stop < start) return(-1); d = ccn_buf_decoder_start(&decoder, buf + start, stop - start); if (ccn_buf_match_dtag(d, tt)) { ccn_buf_advance(d); result = ccn_parse_nonNegativeInteger(d); ccn_buf_check_close(d); } if (result < 0) return(-1); return(result); }
int ccn_parse_required_tagged_UDATA(struct ccn_buf_decoder *d, enum ccn_dtag dtag) { int res = -1; if (ccn_buf_match_dtag(d, dtag)) { res = d->decoder.element_index; ccn_buf_advance(d); if (d->decoder.state >= 0 && CCN_GET_TT_FROM_DSTATE(d->decoder.state) == CCN_UDATA) ccn_buf_advance(d); else d->decoder.state = -__LINE__; ccn_buf_check_close(d); } else d->decoder.state = -__LINE__; if (d->decoder.state < 0) return (-1); return(res); }
static int ccn_parse_KeyName(struct ccn_buf_decoder *d, struct parsed_KeyName *x) { int res = -1; if (ccn_buf_match_dtag(d, CCN_DTAG_KeyName)) { res = d->decoder.element_index; ccn_buf_advance(d); x->Name = d->decoder.token_index; ccn_parse_Name(d, NULL); x->endName = d->decoder.token_index; x->PublisherID = ccn_parse_PublisherID(d, NULL); x->endPublisherID = d->decoder.token_index; ccn_buf_check_close(d); } else d->decoder.state = -__LINE__; if (d->decoder.state < 0) return (d->decoder.state); return(res); }
int ccn_parse_required_tagged_BLOB(struct ccn_buf_decoder *d, enum ccn_dtag dtag, int minlen, int maxlen) { int res = -1; size_t len = 0; if (ccn_buf_match_dtag(d, dtag)) { res = d->decoder.element_index; ccn_buf_advance(d); if (ccn_buf_match_some_blob(d)) { len = d->decoder.numval; ccn_buf_advance(d); } ccn_buf_check_close(d); if (len < minlen || (maxlen >= 0 && len > maxlen)) { d->decoder.state = -__LINE__; } } else d->decoder.state = -__LINE__; if (d->decoder.state < 0) return (d->decoder.state); return(res); }