sdmmd_return_t SDMMD_DirectServiceReceive(SocketConnection handle, CFMutableDataRef *data) { uint32_t size = (data && *data ? (uint32_t)CFDataGetLength(*data) : 0); if (size) { if (handle.isSSL == true || CheckIfExpectingResponse(handle, 1 * kMilliseconds)) { unsigned char *buffer = calloc(1, size); uint32_t remainder = size; size_t received; while (remainder) { if (handle.isSSL) { received = SSL_read(handle.socket.ssl, &buffer[size - remainder], remainder); } else { received = recv(handle.socket.conn, &buffer[size - remainder], remainder, 0); } if (!received) { break; } remainder -= received; } if (*data) { CFDataReplaceBytes(*data, CFRangeMake(0, size), buffer, size); } else { CFDataRef receivedData = CFDataCreate(kCFAllocatorDefault, buffer, size); *data = CFDataCreateMutableCopy(kCFAllocatorDefault, size, receivedData); CFSafeRelease(receivedData); } free(buffer); } return kAMDSuccess; } return kAMDSuccess; }
void CFDataAppendBytes (CFMutableDataRef d, const UInt8 *bytes, CFIndex length) { CF_OBJC_FUNCDISPATCH2(_kCFDataTypeID, void, d, "appendBytes:length:", bytes, length); CFDataReplaceBytes (d, CFRangeMake(d->_length, 0), bytes, length); }
/* * Update a security buffer's offset to be the current end of data in a CFData. */ void secBufOffset( CFMutableDataRef buf, CFIndex offsetIndex) /* obtained from appendSecBuf() */ { CFIndex currPos = CFDataGetLength(buf); unsigned char cb[4]; OSWriteLittleInt32(cb, 0, (uint32_t)currPos); CFRange range = {offsetIndex, 4}; CFDataReplaceBytes(buf, range, cb, 4); }
static inline CFDataRef TSICTStringCreateDataFromIntermediateRepresentation(TStringIRep* rep) { CFIndex len = CFDataGetLength(rep->data); CFMutableDataRef buffer = CFDataCreateMutableCopy(kCFAllocatorDefault, (len + 12), rep->data); UInt8* bufferBytes = CFDataGetMutableBytePtr(buffer); size_t prefixLength = strlen(rep->length) + 1; CFDataReplaceBytes(buffer, BeginningRange, (const UInt8*)rep->length, (CFIndex)prefixLength); if (rep->format == kTSITStringFormatTNetstring) { const UInt8 ftag = (UInt8)TNetstringTypes[rep->type]; CFDataAppendBytes(buffer, &ftag, 1); bufferBytes[(prefixLength - 1)] = TNetstringSeparator; } else if (rep->format == kTSITStringFormatOTNetstring) { const UInt8 ftag = (UInt8)OTNetstringTypes[rep->type]; bufferBytes[(prefixLength - 1)] = ftag; } CFDataRef dataRep = CFDataCreateCopy(kCFAllocatorDefault, buffer); CFRelease(buffer); return dataRep; }
void CFDataDeleteBytes(CFMutableDataRef data, CFRange range) { CF_OBJC_FUNCDISPATCHV(__kCFDataTypeID, void, (NSMutableData *)data, replaceBytesInRange:NSMakeRange(range.location, range.length) withBytes:NULL length:0); CFAssert1(__CFDataIsMutable(data), __kCFLogAssertion, "%s(): data is immutable", __PRETTY_FUNCTION__); CFDataReplaceBytes(data, range, NULL, 0); }
void CFDataAppendBytes(CFMutableDataRef data, const uint8_t *bytes, CFIndex length) { CF_OBJC_FUNCDISPATCHV(__kCFDataTypeID, void, (NSMutableData *)data, appendBytes:(const void *)bytes length:(NSUInteger)length); CFAssert1(__CFDataIsMutable(data), __kCFLogAssertion, "%s(): data is immutable", __PRETTY_FUNCTION__); CFDataReplaceBytes(data, CFRangeMake(__CFDataLength(data), 0), bytes, length); }
// NULL bytesDeallocator to this function does not mean the default allocator, it means // that there should be no deallocator, and the bytes should be copied. static CFMutableDataRef __CFDataInit(CFAllocatorRef allocator, CFOptionFlags flags, CFIndex capacity, const uint8_t *bytes, CFIndex length, CFAllocatorRef bytesDeallocator) { CFMutableDataRef memory; __CFGenericValidateMutabilityFlags(flags); CFAssert2(0 <= capacity, __kCFLogAssertion, "%s(): capacity (%d) cannot be less than zero", __PRETTY_FUNCTION__, capacity); CFAssert3(kCFFixedMutable != __CFMutableVarietyFromFlags(flags) || length <= capacity, __kCFLogAssertion, "%s(): for kCFFixedMutable type, capacity (%d) must be greater than or equal to number of initial elements (%d)", __PRETTY_FUNCTION__, capacity, length); CFAssert2(0 <= length, __kCFLogAssertion, "%s(): length (%d) cannot be less than zero", __PRETTY_FUNCTION__, length); Boolean collectableMemory = CF_IS_COLLECTABLE_ALLOCATOR(allocator); Boolean noCopy = bytesDeallocator != NULL; Boolean isMutable = ((flags & __kCFMutable) != 0); Boolean isGrowable = ((flags & __kCFGrowable) != 0); Boolean allocateInline = !isGrowable && !noCopy && capacity < INLINE_BYTES_THRESHOLD; allocator = (allocator == NULL) ? __CFGetDefaultAllocator() : allocator; Boolean useAllocator = (allocator != kCFAllocatorSystemDefault && allocator != kCFAllocatorMalloc && allocator != kCFAllocatorMallocZone); CFIndex size = sizeof(struct __CFData) - sizeof(CFRuntimeBase); if (allocateInline) { size += sizeof(uint8_t) * __CFDataNumBytesForCapacity(capacity) + sizeof(uint8_t) * 15; // for 16-byte alignment fixup } memory = (CFMutableDataRef)_CFRuntimeCreateInstance(allocator, __kCFDataTypeID, size, NULL); if (NULL == memory) { return NULL; } __CFDataSetNumBytesUsed(memory, 0); __CFDataSetLength(memory, 0); __CFDataSetInfoBits(memory, (allocateInline ? __kCFBytesInline : 0) | (useAllocator ? __kCFUseAllocator : 0) | (collectableMemory ? __kCFAllocatesCollectable : 0)); BOOL finalize = YES; BOOL scan = YES; if (collectableMemory) { if (allocateInline) { // We have no pointer to anything that needs to be reclaimed, so don't scan or finalize. scan = NO; finalize = NO; } else if (noCopy) { if (CF_IS_COLLECTABLE_ALLOCATOR(bytesDeallocator)) { // We're taking responsibility for externally GC-allocated memory, so scan us, but we don't need to finalize. finalize = NO; } else if (bytesDeallocator == kCFAllocatorNull) { // We don't have responsibility for these bytes, so there's no need to be scanned and we don't need to finalize. scan = NO; finalize = NO; } else { // We have a pointer to non-GC-allocated memory, so don't scan, but do finalize. scan = NO; } } if (!scan) auto_zone_set_unscanned(objc_collectableZone(), memory); if (!finalize) auto_zone_set_nofinalize(objc_collectableZone(), memory); } if (isMutable && isGrowable) { __CFDataSetCapacity(memory, __CFDataRoundUpCapacity(1)); __CFDataSetNumBytes(memory, __CFDataNumBytesForCapacity(__CFDataRoundUpCapacity(1))); __CFSetMutableVariety(memory, kCFMutable); } else { /* Don't round up capacity */ __CFDataSetCapacity(memory, capacity); __CFDataSetNumBytes(memory, __CFDataNumBytesForCapacity(capacity)); __CFSetMutableVariety(memory, kCFFixedMutable); } if (noCopy) { __CFAssignWithWriteBarrier((void **)&memory->_bytes, (uint8_t *)bytes); if (finalize) { if (_CFAllocatorIsGCRefZero(bytesDeallocator)) { memory->_bytesDeallocator = bytesDeallocator; } else { memory->_bytesDeallocator = (CFAllocatorRef)CFRetain(_CFConvertAllocatorToNonGCRefZeroEquivalent(bytesDeallocator)); } } if (CF_IS_COLLECTABLE_ALLOCATOR(bytesDeallocator) && !_CFAllocatorIsGCRefZero(bytesDeallocator)) { // When given a GC allocator which is not one of the GCRefZero ones as the deallocator, we assume that the no-copy memory is GC-allocated with a retain count of (at least) 1 and we should release it now instead of waiting until __CFDataDeallocate. auto_zone_release(objc_collectableZone(), memory->_bytes); } __CFDataSetNumBytesUsed(memory, length); __CFDataSetLength(memory, length); // Mutable no-copy datas are not allowed, so don't bother setting needsToZero flag. } else { Boolean cleared = (isMutable && !isGrowable && !_CFExecutableLinkedOnOrAfter(CFSystemVersionSnowLeopard)); if (!allocateInline) { // assume that allocators give 16-byte aligned memory back -- it is their responsibility __CFAssignWithWriteBarrier((void **)&memory->_bytes, __CFDataAllocate(memory, __CFDataNumBytes(memory) * sizeof(uint8_t), cleared)); if (__CFOASafe) __CFSetLastAllocationEventName(memory->_bytes, "CFData (store)"); if (NULL == memory->_bytes) { CFRelease(memory); return NULL; } } else { if (length == 0 && !isMutable) { // NSData sets its bytes pointer to NULL when its length is zero. Starting in 10.7 we do the same for CFData. memory->_bytes = NULL; // It is important to set this data as not inlined, so we do not recalculate a bytes pointer from null. __CFDataSetInline(memory, false); } cleared = true; } __CFDataSetNeedsToZero(memory, !cleared); memory->_bytesDeallocator = NULL; CFDataReplaceBytes(memory, CFRangeMake(0, 0), bytes, length); } __CFSetMutableVariety(memory, __CFMutableVarietyFromFlags(flags)); return memory; }
void CFDataDeleteBytes(CFMutableDataRef data, CFRange range) { CF_OBJC_FUNCDISPATCH3(__kCFDataTypeID, void, data, "replaceBytesInRange:withBytes:length:", range, NULL, 0); CFAssert1(__CFDataIsMutable(data), __kCFLogAssertion, "%s(): data is immutable", __PRETTY_FUNCTION__); CFDataReplaceBytes(data, range, NULL, 0); }
void CFDataDeleteBytes (CFMutableDataRef d, CFRange range) { CFDataReplaceBytes(d, range, NULL, 0); }
void Blob::replaceRange(Range range, const UInt8 *buffer, Index length) { gfx_assert(range.location < this->length() && range.max() < this->length(), str("out of bounds range")); CFDataReplaceBytes(getStorage(), range, buffer, length); }