/** * 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_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); }
PyObject * SignedInfo_obj_from_ccn(PyObject *py_signed_info) { struct ccn_charbuf *signed_info; PyObject *py_obj_SignedInfo, *py_o; struct ccn_buf_decoder decoder, *d; size_t start, stop, size; const unsigned char *ptr; int r; signed_info = CCNObject_Get(SIGNED_INFO, py_signed_info); debug("SignedInfo_from_ccn start, size=%zd\n", signed_info->length); // 1) Create python object py_obj_SignedInfo = PyObject_CallObject(g_type_SignedInfo, NULL); if (!py_obj_SignedInfo) return NULL; // 2) Set ccn_data to a cobject pointing to the c struct // and ensure proper destructor is set up for the c object. r = PyObject_SetAttrString(py_obj_SignedInfo, "ccn_data", py_signed_info); JUMP_IF_NEG(r, error); // 3) Parse c structure and fill python attributes // using PyObject_SetAttrString // based on chk_signing_params // from ccn_client.c // //outputs: // Note, it is ok that non-filled optional elements // are initialized to None (through the .py file __init__) // d = ccn_buf_decoder_start(&decoder, signed_info->buf, signed_info->length); if (!ccn_buf_match_dtag(d, CCN_DTAG_SignedInfo)) { PyErr_Format(g_PyExc_CCNSignedInfoError, "Error finding" " CCN_DTAG_SignedInfo (decoder state: %d)", d->decoder.state); goto error; } ccn_buf_advance(d); /* PublisherPublic Key */ //XXX: should we check for case when PublishePublicKeyDigest is not present? -dk start = d->decoder.token_index; ccn_parse_required_tagged_BLOB(d, CCN_DTAG_PublisherPublicKeyDigest, 16, 64); stop = d->decoder.token_index; r = ccn_ref_tagged_BLOB(CCN_DTAG_PublisherPublicKeyDigest, d->buf, start, stop, &ptr, &size); if (r < 0) { PyErr_Format(g_PyExc_CCNSignedInfoError, "Error parsing" " CCN_DTAG_PublisherPublicKey (decoder state %d)", d->decoder.state); goto error; } // self.publisherPublicKeyDigest = None # SHA256 hash debug("PyObject_SetAttrString publisherPublicKeyDigest\n"); py_o = PyBytes_FromStringAndSize((const char*) ptr, size); JUMP_IF_NULL(py_o, error); r = PyObject_SetAttrString(py_obj_SignedInfo, "publisherPublicKeyDigest", py_o); Py_DECREF(py_o); JUMP_IF_NEG(r, error); /* Timestamp */ start = d->decoder.token_index; ccn_parse_required_tagged_BLOB(d, CCN_DTAG_Timestamp, 1, -1); stop = d->decoder.token_index; r = ccn_ref_tagged_BLOB(CCN_DTAG_Timestamp, d->buf, start, stop, &ptr, &size); if (r < 0) { PyErr_Format(g_PyExc_CCNSignedInfoError, "Error parsing" " CCN_DTAG_Timestamp (decoder state %d)", d->decoder.state); goto error; } // self.timeStamp = None # CCNx timestamp debug("PyObject_SetAttrString timeStamp\n"); py_o = PyBytes_FromStringAndSize((const char*) ptr, size); JUMP_IF_NULL(py_o, error); r = PyObject_SetAttrString(py_obj_SignedInfo, "timeStamp", py_o); Py_DECREF(py_o); JUMP_IF_NEG(r, error); /* Type */ assert(d->decoder.state >= 0); r = ccn_parse_optional_tagged_binary_number(d, CCN_DTAG_Type, 3, 3, CCN_CONTENT_DATA); if (d->decoder.state < 0) { PyErr_SetString(g_PyExc_CCNSignedInfoError, "Unable to parse type"); goto error; } py_o = _pyccn_Int_FromLong(r); JUMP_IF_NULL(py_o, error); r = PyObject_SetAttrString(py_obj_SignedInfo, "type", py_o); Py_DECREF(py_o); JUMP_IF_NEG(r, error); /* start = d->decoder.token_index; ccn_parse_optional_tagged_BLOB(d, CCN_DTAG_Type, 1, -1); stop = d->decoder.token_index; r = ccn_ref_tagged_BLOB(CCN_DTAG_Type, d->buf, start, stop, &ptr, &size); if (r == 0) { // type = None # CCNx type // TODO: Provide a string representation with the Base64 mnemonic? debug("PyObject_SetAttrString type\n"); py_o = PyByteArray_FromStringAndSize((const char*) ptr, size); JUMP_IF_NULL(py_o, error); r = PyObject_SetAttrString(py_obj_SignedInfo, "type", py_o); Py_DECREF(py_o); JUMP_IF_NEG(r, error); } */ /* FreshnessSeconds */ r = ccn_parse_optional_tagged_nonNegativeInteger(d, CCN_DTAG_FreshnessSeconds); if (r >= 0) { // self.freshnessSeconds = None debug("PyObject_SetAttrString freshnessSeconds\n"); py_o = _pyccn_Int_FromLong(r); JUMP_IF_NULL(py_o, error); r = PyObject_SetAttrString(py_obj_SignedInfo, "freshnessSeconds", py_o); Py_DECREF(py_o); JUMP_IF_NEG(r, error); } /* FinalBlockID */ #if 0 /* old code (left in case mine is wrong - dk) */ if (ccn_buf_match_dtag(d, CCN_DTAG_FinalBlockID)) { ccn_buf_advance(d); start = d->decoder.token_index; if (ccn_buf_match_some_blob(d)) ccn_buf_advance(d); stop = d->decoder.token_index; ccn_buf_check_close(d); if (d->decoder.state >= 0 && stop > start) { // self.finalBlockID = None fprintf(stderr, "PyObject_SetAttrString finalBlockID, len=%zd\n", stop - start); py_o = PyByteArray_FromStringAndSize((const char*) (d->buf + start), stop - start); PyObject_SetAttrString(py_obj_SignedInfo, "finalBlockID", py_o); Py_INCREF(py_o); } } #endif start = d->decoder.token_index; ccn_parse_optional_tagged_BLOB(d, CCN_DTAG_FinalBlockID, 1, -1); stop = d->decoder.token_index; r = ccn_ref_tagged_BLOB(CCN_DTAG_FinalBlockID, d->buf, start, stop, &ptr, &size); if (r == 0) { // self.finalBlockID = None debug("PyObject_SetAttrString finalBlockID, len=%zd\n", size); py_o = PyBytes_FromStringAndSize((const char*) ptr, size); JUMP_IF_NULL(py_o, error); r = PyObject_SetAttrString(py_obj_SignedInfo, "finalBlockID", py_o); Py_DECREF(py_o); JUMP_IF_NEG(r, error); } /* KeyLocator */ #if 0 /* Old code in case mine is wrong - dk */ start = d->decoder.token_index; if (ccn_buf_match_dtag(d, CCN_DTAG_KeyLocator)) ccn_buf_advance_past_element(d); stop = d->decoder.token_index; if (d->decoder.state >= 0 && stop > start) { fprintf(stderr, "PyObject_SetAttrString keyLocator, len=%zd\n", stop - start); struct ccn_charbuf* keyLocator = ccn_charbuf_create(); ccn_charbuf_append(keyLocator, d->buf + start, stop - start); // self.keyLocator = None py_o = KeyLocator_obj_from_ccn(keyLocator); // it will free PyObject_SetAttrString(py_obj_SignedInfo, "keyLocator", py_o); Py_INCREF(py_o); } #endif /* * KeyLocator is not a BLOB, but an another structure, this requires * us to parse it differently */ start = d->decoder.token_index; if (ccn_buf_match_dtag(d, CCN_DTAG_KeyLocator)) { struct ccn_charbuf *key_locator; PyObject *py_key_locator; r = ccn_buf_advance_past_element(d); if (r < 0) { PyErr_Format(g_PyExc_CCNSignedInfoError, "Error locating" " CCN_DTAG_KeyLocator (decoder state: %d, r: %d)", d->decoder.state, r); goto error; } stop = d->decoder.token_index; /* debug("element_index = %zd size = %zd nest = %d numval = %zd state = %d" " token_index %zd\n", d->decoder.element_index, d->decoder.index, d->decoder.nest, d->decoder.numval, d->decoder.state, d->decoder.token_index); assert(d->decoder.state >= 0); */ ptr = d->buf + start; size = stop - start; assert(size > 0); debug("PyObject_SetAttrString keyLocator, len=%zd\n", size); py_key_locator = CCNObject_New_charbuf(KEY_LOCATOR, &key_locator); JUMP_IF_NULL(py_key_locator, error); r = ccn_charbuf_append(key_locator, ptr, size); if (r < 0) { Py_DECREF(py_key_locator); PyErr_NoMemory(); goto error; } // self.keyLocator = None py_o = KeyLocator_obj_from_ccn(py_key_locator); Py_DECREF(py_key_locator); JUMP_IF_NULL(py_o, error); r = PyObject_SetAttrString(py_obj_SignedInfo, "keyLocator", py_o); Py_DECREF(py_o); JUMP_IF_NEG(r, error); } ccn_buf_check_close(d); if (d->decoder.state < 0) { PyErr_Format(g_PyExc_CCNSignedInfoError, "SignedInfo decoding error" " (decoder state: %d, numval: %zd)", d->decoder.state, d->decoder.numval); goto error; } // 4) Return the created object debug("SignedInfo_from_ccn ends\n"); return py_obj_SignedInfo; error: Py_DECREF(py_obj_SignedInfo); return NULL; }
PUBLIC struct content_entry * r_store_find_first_match_candidate(struct ccnr_handle *h, const unsigned char *interest_msg, const struct ccn_parsed_interest *pi) { int res; size_t start = pi->offset[CCN_PI_B_Name]; size_t end = pi->offset[CCN_PI_E_Name]; struct ccn_charbuf *namebuf = NULL; struct ccn_charbuf *flatname = NULL; struct content_entry *content = NULL; flatname = ccn_charbuf_create(); ccn_flatname_from_ccnb(flatname, interest_msg, pi->offset[CCN_PI_E]); if (pi->offset[CCN_PI_B_Exclude] < pi->offset[CCN_PI_E_Exclude]) { /* Check for <Exclude><Any/><Component>... fast case */ struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d; size_t ex1start; size_t ex1end; d = ccn_buf_decoder_start(&decoder, interest_msg + pi->offset[CCN_PI_B_Exclude], pi->offset[CCN_PI_E_Exclude] - pi->offset[CCN_PI_B_Exclude]); ccn_buf_advance(d); if (ccn_buf_match_dtag(d, CCN_DTAG_Any)) { ccn_buf_advance(d); ccn_buf_check_close(d); if (ccn_buf_match_dtag(d, CCN_DTAG_Component)) { ex1start = pi->offset[CCN_PI_B_Exclude] + d->decoder.token_index; ccn_buf_advance_past_element(d); ex1end = pi->offset[CCN_PI_B_Exclude] + d->decoder.token_index; if (d->decoder.state >= 0) { namebuf = ccn_charbuf_create(); ccn_charbuf_append(namebuf, interest_msg + start, end - start); namebuf->length--; ccn_charbuf_append(namebuf, interest_msg + ex1start, ex1end - ex1start); ccn_charbuf_append_closer(namebuf); res = ccn_flatname_append_from_ccnb(flatname, interest_msg + ex1start, ex1end - ex1start, 0, 1); if (res != 1) ccnr_debug_ccnb(h, __LINE__, "fastex_bug", NULL, namebuf->buf, namebuf->length); if (CCNSHOULDLOG(h, LM_8, CCNL_FINER)) ccnr_debug_ccnb(h, __LINE__, "fastex", NULL, namebuf->buf, namebuf->length); } } } } content = r_store_look(h, flatname->buf, flatname->length); ccn_charbuf_destroy(&namebuf); ccn_charbuf_destroy(&flatname); return(content); }