PARCBuffer * parcBuffer_Resize(PARCBuffer *buffer, size_t newCapacity) { parcBuffer_OptionalAssertValid(buffer); PARCByteArray *newArray = parcByteArray_Allocate(newCapacity); if (newArray == NULL) { return NULL; } size_t numberOfBytesToCopy = parcBuffer_Capacity(buffer); if (numberOfBytesToCopy > newCapacity) { numberOfBytesToCopy = newCapacity; } parcByteArray_PutBytes(newArray, 0, numberOfBytesToCopy, &parcByteArray_Array(buffer->array)[buffer->arrayOffset]); parcByteArray_Release(&buffer->array); buffer->array = newArray; buffer->arrayOffset = 0; buffer->limit = _computeNewLimit(buffer->capacity, buffer->limit, newCapacity); buffer->mark = _computeNewMark(buffer->mark, buffer->limit, newCapacity); buffer->capacity = newCapacity; buffer->position = (buffer->position < buffer->limit) ? buffer->position : buffer->limit; parcBuffer_OptionalAssertValid(buffer); return buffer; }
CCNxInterestPayloadId * ccnxInterestPayloadId_CreateAsSHA256Hash(const PARCBuffer *data) { CCNxInterestPayloadId *result = parcObject_CreateInstance(CCNxInterestPayloadId); PARCCryptoHasher *hasher = parcCryptoHasher_Create(PARCCryptoHashType_SHA256); parcCryptoHasher_Init(hasher); parcCryptoHasher_UpdateBuffer(hasher, data); PARCCryptoHash *hash = parcCryptoHasher_Finalize(hasher); parcCryptoHasher_Release(&hasher); PARCBuffer *hashData = parcCryptoHash_GetDigest(hash); PARCBuffer *codedHash = parcBuffer_Allocate(parcBuffer_Capacity(hashData) + 1); parcBuffer_PutUint8(codedHash, CCNxInterestPayloadId_TypeCode_RFC6920_SHA256); parcBuffer_PutBuffer(codedHash, hashData); parcBuffer_Flip(codedHash); result->nameSegment = ccnxNameSegment_CreateTypeValue(CCNxNameLabelType_PAYLOADID, codedHash); parcBuffer_Release(&codedHash); parcCryptoHash_Release(&hash); return result; }
PARCBuffer * parcBuffer_Clear(PARCBuffer *buffer) { parcBuffer_SetPosition(buffer, 0); parcBuffer_SetLimit(buffer, parcBuffer_Capacity(buffer)); _discardMark(buffer); return buffer; }
CCNxInterestPayloadId * ccnxInterestPayloadId_Create(const PARCBuffer *data, uint8_t type) { CCNxInterestPayloadId *result = parcObject_CreateInstance(CCNxInterestPayloadId); parcBuffer_AssertValid(data); PARCBuffer *buffer = parcBuffer_Allocate(parcBuffer_Capacity(data) + 1); assertTrue(type > CCNxInterestPayloadId_TypeCode_App, "App type must be greater than 0x80"); parcBuffer_PutUint8(buffer, type); parcBuffer_PutBuffer(buffer, data); parcBuffer_Flip(buffer); result->nameSegment = ccnxNameSegment_CreateTypeValue(CCNxNameLabelType_PAYLOADID, buffer); parcBuffer_Release(&buffer); return result; }
LONGBOW_TEST_CASE(Global, ccnxLink_GetContentObjectHash) { CCNxName *name = ccnxName_CreateFromCString("lci:/foo/bar/name"); PARCBuffer *keyId = parcBuffer_Allocate(10); PARCBuffer *contentObjectHash = parcBuffer_Allocate(20); CCNxLink *object = ccnxLink_Create(name, keyId, contentObjectHash); PARCBuffer *buffer = ccnxLink_GetContentObjectHash(object); assertNotNull(buffer, "Expected non-null return value."); assertTrue(parcBuffer_Capacity(buffer) == 20, "Expected the same buffer size back"); ccnxLink_Release(&object); ccnxName_Release(&name); parcBuffer_Release(&keyId); parcBuffer_Release(&contentObjectHash); }
PARCBuffer * parcBuffer_SetLimit(PARCBuffer *buffer, size_t newLimit) { parcBuffer_OptionalAssertValid(buffer); assertTrue(newLimit <= parcBuffer_Capacity(buffer), "new limit cannot be larger than the capacity"); if (_markIsDiscarded(buffer)) { buffer->limit = newLimit; _discardMark(buffer); } else { if (newLimit < buffer->position) { buffer->position = newLimit; } if (newLimit < buffer->mark) { _discardMark(buffer); } buffer->limit = newLimit; } _optionalAssertInvariants(buffer); return buffer; }
PARCBuffer * parcBuffer_Copy(const PARCBuffer *original) { parcBuffer_OptionalAssertValid(original); PARCBuffer *result = _parcBuffer_getInstance(); if (result != NULL) { PARCByteArray *array = parcByteArray_Copy(original->array); if (array != NULL) { _parcBuffer_Init(result, array, parcBuffer_ArrayOffset(original), parcBuffer_Position(original), parcBuffer_Limit(original), parcBuffer_Capacity(original)); } else { parcBuffer_Release(&result); } } return result; }