UPSDataRef GetPrivateData( CFDictionaryRef properties ) { UPSDataRef upsDataRef = NULL; CFMutableDataRef data = NULL; UInt32 i = 0; UInt32 count = 0; // Allocated the global array if necessary if (!gUPSDataArrayRef && !(gUPSDataArrayRef = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks))) { return NULL; } // Get the device and vendor ID for this UPS so that we can see if we already have // an entry for it in our global data // // Find an empty location in our array count = CFArrayGetCount(gUPSDataArrayRef); for ( i = 0; i < count; i++) { data = (CFMutableDataRef)CFArrayGetValueAtIndex(gUPSDataArrayRef, i); if ( !data ) continue; upsDataRef =(UPSDataRef)CFDataGetMutableBytePtr(data); if (upsDataRef && !(upsDataRef->isPresent)) break; upsDataRef = NULL; } // No valid upsDataRef was found, so let's go ahead and allocate one if ( (upsDataRef == NULL) && (data = CFDataCreateMutable(kCFAllocatorDefault, sizeof(UPSData))) ) { upsDataRef =(UPSDataRef)CFDataGetMutableBytePtr(data); bzero( upsDataRef, sizeof(UPSData) ); CFArrayAppendValue(gUPSDataArrayRef, data); CFRelease(data); } // If we have a pointer to our global, then fill in some of the field in that structure // if ( upsDataRef != NULL ) { upsDataRef->upsID = i; } return upsDataRef; }
static CFDataRef SecECPPrivateKeyExport(CFAllocatorRef allocator, ccec_full_ctx_t fullkey) { size_t prime_size = ccec_cp_prime_size(ccec_ctx_cp(fullkey)); size_t key_size = ccec_export_pub_size(fullkey) + prime_size; CFMutableDataRef blob = CFDataCreateMutable(allocator, key_size); if (blob) { CFDataSetLength(blob, key_size); ccec_export_pub(fullkey, CFDataGetMutableBytePtr(blob)); UInt8 *dest = CFDataGetMutableBytePtr(blob) + ccec_export_pub_size(fullkey); const cc_unit *k = ccec_ctx_k(fullkey); ccn_write_uint_padded(ccec_ctx_n(fullkey), k, prime_size, dest); } return blob; }
bool SOSAccountPublishCloudParameters(SOSAccountRef account, CFErrorRef* error){ bool success = false; CFIndex cloud_der_len = der_sizeof_cloud_parameters( account->user_public, account->user_key_parameters, error); CFMutableDataRef cloudParameters = CFDataCreateMutableWithScratch(kCFAllocatorDefault, cloud_der_len); if (der_encode_cloud_parameters(account->user_public, account->user_key_parameters, error, CFDataGetMutableBytePtr(cloudParameters), CFDataGetMutablePastEndPtr(cloudParameters)) != NULL) { CFErrorRef changeError = NULL; if (SOSTrasnportKeyParameterPublishCloudParameters(account->key_transport, cloudParameters, error)) { success = true; } else { SOSCreateErrorWithFormat(kSOSErrorSendFailure, changeError, error, NULL, CFSTR("update parameters key failed [%@]"), cloudParameters); } CFReleaseSafe(changeError); } else { SOSCreateError(kSOSErrorEncodeFailure, CFSTR("Encoding parameters failed"), NULL, error); } CFReleaseNull(cloudParameters); return success; }
CFDataRef SecDigestCreate(CFAllocatorRef allocator, const SecAsn1Oid *algorithm, const SecAsn1Item *params, const UInt8 *data, CFIndex length) { unsigned char *(*digestFcn)(const void *data, CC_LONG len, unsigned char *md); CFIndex digestLen; if (length > INT32_MAX) return NULL; if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA1)) { digestFcn = CC_SHA1; digestLen = CC_SHA1_DIGEST_LENGTH; } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA224)) { digestFcn = CC_SHA224; digestLen = CC_SHA224_DIGEST_LENGTH; } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA256)) { digestFcn = CC_SHA256; digestLen = CC_SHA256_DIGEST_LENGTH; } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA384)) { digestFcn = CC_SHA384; digestLen = CC_SHA384_DIGEST_LENGTH; } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA512)) { digestFcn = CC_SHA512; digestLen = CC_SHA512_DIGEST_LENGTH; } else { return NULL; } CFMutableDataRef digest = CFDataCreateMutable(allocator, digestLen); CFDataSetLength(digest, digestLen); digestFcn(data, (CC_LONG)length, CFDataGetMutableBytePtr(digest)); return digest; }
CFDataRef SecSHA256DigestCreateFromData(CFAllocatorRef allocator, CFDataRef data) { CFMutableDataRef digest = CFDataCreateMutable(allocator, CC_SHA256_DIGEST_LENGTH); CFDataSetLength(digest, CC_SHA256_DIGEST_LENGTH); CCDigest(kCCDigestSHA256, CFDataGetBytePtr(data), CFDataGetLength(data), CFDataGetMutableBytePtr(digest)); return digest; }
CFDataRef SOSUserKeyCreateGenerateParameters(CFErrorRef *error) { size_t saltlen = SALTMAX; uint8_t salt[saltlen]; size_t iterations = ITERATIONMIN; size_t keysize = 256; if(CCRandomCopyBytes(kCCRandomDefault, salt, sizeof(salt)) != kCCSuccess) { SOSCreateError(kSOSErrorProcessingFailure, CFSTR("CCRandomCopyBytes failed"), NULL, error); return NULL; } CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0); CFDataSetLength(result, der_sizeof_pbkdf2_params(saltlen, salt, iterations, keysize)); uint8_t * encode = der_encode_pbkdf2_params(saltlen, salt, iterations, keysize, CFDataGetBytePtr(result), CFDataGetMutableBytePtr(result) + CFDataGetLength(result)); if (!encode) CFReleaseNull(result); if (result) { secnotice("keygen", "Created new parameters: iterations %zd, keysize %zd: %@", iterations, keysize, result); } return result; }
//--------------------------------------------------------------------------- // _io_ups_send_command // // This routine allow remote processes to issue commands to the UPS. It is // expected that command will come in as a serialized CFDictionaryRef. //--------------------------------------------------------------------------- kern_return_t _io_ups_send_command( mach_port_t server, int upsID, void * commandBuffer, IOByteCount commandSize) { CFDictionaryRef command; CFMutableDataRef data; UPSDataRef upsDataRef; IOReturn res = kIOReturnError; command = (CFDictionaryRef)IOCFUnserialize(commandBuffer, kCFAllocatorDefault, kNilOptions, NULL); if (command) { if (!gUPSDataArrayRef || (upsID >= CFArrayGetCount(gUPSDataArrayRef))) { res = kIOReturnBadArgument; } else { data = (CFMutableDataRef)CFArrayGetValueAtIndex(gUPSDataArrayRef, upsID); upsDataRef =(UPSDataRef)CFDataGetMutableBytePtr(data); if (upsDataRef && upsDataRef->upsPlugInInterface) res = (*upsDataRef->upsPlugInInterface)->sendCommand(upsDataRef->upsPlugInInterface, command); } CFRelease(command); } return res; }
void CFDataSetLength(CFMutableDataRef data, CFIndex newLength) { CFIndex oldLength, capacity; Boolean isGrowable; CF_OBJC_FUNCDISPATCHV(__kCFDataTypeID, void, (NSMutableData *)data, setLength:(NSUInteger)newLength); CFAssert1(__CFDataIsMutable(data), __kCFLogAssertion, "%s(): data is immutable", __PRETTY_FUNCTION__); oldLength = __CFDataLength(data); capacity = __CFDataCapacity(data); isGrowable = __CFDataIsGrowable(data); if (__CFDataIsMutable(data)) { if (newLength < 0) { if (isGrowable) { __CFDataHandleOutOfMemory(data, newLength); } else { HALT; } } else if (capacity < newLength) { if (isGrowable) { __CFDataGrow(data, newLength - oldLength, true); } else { CFAssert1(newLength <= __CFDataCapacity(data), __kCFLogAssertion, "%s(): fixed-capacity data is full", __PRETTY_FUNCTION__); } } else if (oldLength < newLength && __CFDataNeedsToZero(data)) { memset(CFDataGetMutableBytePtr(data) + oldLength, 0, newLength - oldLength); } else if (newLength < oldLength) { __CFDataSetNeedsToZero(data, true); } } __CFDataSetLength(data, newLength); __CFDataSetNumBytesUsed(data, newLength); }
static CFDataRef SecRSAPublicKeyCreatePKCS1(CFAllocatorRef allocator, ccrsa_pub_ctx_t pubkey) { size_t m_size = ccn_write_int_size(ccrsa_ctx_n(pubkey), ccrsa_ctx_m(pubkey)); size_t e_size = ccn_write_int_size(ccrsa_ctx_n(pubkey), ccrsa_ctx_e(pubkey)); const size_t seq_size = DERLengthOfItem(ASN1_INTEGER, m_size) + DERLengthOfItem(ASN1_INTEGER, e_size); const size_t result_size = DERLengthOfItem(ASN1_SEQUENCE, seq_size); CFMutableDataRef pkcs1 = CFDataCreateMutable(allocator, result_size); if (pkcs1 == NULL) return NULL; CFDataSetLength(pkcs1, result_size); uint8_t *bytes = CFDataGetMutableBytePtr(pkcs1); *bytes++ = ASN1_CONSTR_SEQUENCE; DERSize itemLength = 4; DEREncodeLength(seq_size, bytes, &itemLength); bytes += itemLength; ccasn_encode_int(ccrsa_ctx_n(pubkey), ccrsa_ctx_m(pubkey), m_size, &bytes); ccasn_encode_int(ccrsa_ctx_n(pubkey), ccrsa_ctx_e(pubkey), e_size, &bytes); return pkcs1; }
CFDataRef SecSHA256DigestCreate(CFAllocatorRef allocator, const UInt8 *data, CFIndex length) { CFMutableDataRef digest = CFDataCreateMutable(allocator, CC_SHA256_DIGEST_LENGTH); CFDataSetLength(digest, CC_SHA256_DIGEST_LENGTH); CCDigest(kCCDigestSHA256, data, length, CFDataGetMutableBytePtr(digest)); return digest; }
void SecKeyFromPassphraseDataHMACSHA1(CFDataRef password, CFDataRef salt, uint32_t interationCount, CFMutableDataRef derivedKey) { pbkdf2_hmac_sha1(CFDataGetBytePtr(password), CFDataGetLength(password), CFDataGetBytePtr(salt), CFDataGetLength(salt), interationCount, CFDataGetMutableBytePtr(derivedKey), CFDataGetLength(derivedKey)); }
STATIC CFDataRef my_CFDataCreateWithRandomBytes(CFIndex size) { CFMutableDataRef data; data = CFDataCreateMutable(NULL, size); CFDataSetLength(data, size); fill_with_random(CFDataGetMutableBytePtr(data), (int)size); return (data); }
static uint8_t* AppendEncryptedSignature(SecOTRSessionRef session, const cc_unit* s, bool usePrime, CFMutableDataRef appendTo) { CFMutableDataRef signature = CFDataCreateMutable(kCFAllocatorDefault, 0); CFMutableDataRef mbData = CFDataCreateMutable(kCFAllocatorDefault, 0); CFMutableDataRef mb = CFDataCreateMutable(kCFAllocatorDefault, 0); SecFDHKAppendPublicSerialization(session->_myKey, mbData); SecPDHKAppendSerialization(session->_theirKey, mbData); CFIndex publicKeyOffset = CFDataGetLength(mbData); SecOTRPublicIdentityRef myPublic = SecOTRPublicIdentityCopyFromPrivate(kCFAllocatorDefault, session->_me, NULL); AppendPublicKey(mbData, myPublic); CFReleaseNull(myPublic); AppendLong(mbData, session->_keyID); DeriveAndAppendSHA256HMAC(mb, kExponentiationUnits, s, usePrime ? kM1Prime : kM1, (size_t)CFDataGetLength(mbData), CFDataGetBytePtr(mbData)); CFDataDeleteBytes(mbData, CFRangeMake(0, publicKeyOffset)); CFMutableDataRef xb = mbData; mbData = NULL; SecOTRFIAppendSignature(session->_me, mb, signature, NULL); CFReleaseNull(mb); AppendCFDataAsDATA(xb, signature); CFReleaseNull(signature); CFIndex dataLength = CFDataGetLength(xb); CFIndex signatureStartIndex = CFDataGetLength(appendTo); /* 64 bits cast: We are appending the signature we just generated, which is never bigger than 2^32 bytes. */ assert(((unsigned long)dataLength)<=UINT32_MAX); /* debug check, correct as long as CFIndex is a signed long */ AppendLong(appendTo, (uint32_t)dataLength); uint8_t *destination = CFDataIncreaseLengthAndGetMutableBytes(appendTo, dataLength); uint8_t c[kOTRAuthKeyBytes]; DeriveOTR128BitPairFromS(kCs, kExponentiationUnits, s, sizeof(c), usePrime ? NULL : c, sizeof(c), usePrime ? c : NULL); AES_CTR_IV0_Transform(sizeof(c), c, (size_t)dataLength, CFDataGetBytePtr(xb), destination); bzero(c, sizeof(c)); CFReleaseNull(xb); return CFDataGetMutableBytePtr(appendTo) + signatureStartIndex; }
/* Encode the public key and return it in a newly allocated CFDataRef. */ static CFDataRef SecECPublicKeyExport(CFAllocatorRef allocator, ccec_pub_ctx_t pubkey) { size_t pub_size = ccec_export_pub_size(pubkey); CFMutableDataRef blob = CFDataCreateMutable(allocator, pub_size); if (blob) { CFDataSetLength(blob, pub_size); ccec_export_pub(pubkey, CFDataGetMutableBytePtr(blob)); } return blob; }
void CFDataReplaceBytes(CFMutableDataRef data, CFRange range, const uint8_t *newBytes, CFIndex newLength) { CF_OBJC_FUNCDISPATCHV(__kCFDataTypeID, void, (NSMutableData *)data, replaceBytesInRange:NSMakeRange(range.location, range.length) withBytes:(const void *)newBytes length:(NSUInteger)newLength); __CFGenericValidateType(data, __kCFDataTypeID); __CFDataValidateRange(data, range, __PRETTY_FUNCTION__); CFAssert1(__CFDataIsMutable(data), __kCFLogAssertion, "%s(): data is immutable", __PRETTY_FUNCTION__); CFAssert2(0 <= newLength, __kCFLogAssertion, "%s(): newLength (%d) cannot be less than zero", __PRETTY_FUNCTION__, newLength); CFIndex len = __CFDataLength(data); if (len < 0 || range.length < 0 || newLength < 0) HALT; CFIndex newCount = len - range.length + newLength; if (newCount < 0) HALT; uint8_t *bytePtr = (uint8_t *)CFDataGetMutableBytePtr(data); uint8_t *srcBuf = (uint8_t *)newBytes; switch (__CFMutableVariety(data)) { case kCFMutable: if (__CFDataNumBytes(data) < newCount) { if (bytePtr && newBytes && newBytes < bytePtr + __CFDataCapacity(data) && bytePtr < newBytes + newLength) { srcBuf = (uint8_t *)malloc(newLength * sizeof(uint8_t)); memmove(srcBuf, newBytes, newLength * sizeof(uint8_t)); } __CFDataGrow(data, newLength - range.length, false); bytePtr = (uint8_t *)CFDataGetMutableBytePtr(data); } break; case kCFFixedMutable: CFAssert1(newCount <= __CFDataCapacity(data), __kCFLogAssertion, "%s(): fixed-capacity data is full", __PRETTY_FUNCTION__); // Continuing after this could cause buffer overruns. if (newCount > __CFDataCapacity(data)) HALT; break; } if (newLength != range.length && range.location + range.length < len) { memmove(bytePtr + range.location + newLength, bytePtr + range.location + range.length, (len - range.location - range.length) * sizeof(uint8_t)); } if (0 < newLength) { memmove(bytePtr + range.location, srcBuf, newLength * sizeof(uint8_t)); } if (srcBuf != newBytes) free(srcBuf); __CFDataSetNumBytesUsed(data, newCount); __CFDataSetLength(data, newCount); }
//--------------------------------------------------------------------------- // _io_ups_get_capabilities // // This routine allow remote processes to issue commands to the UPS. It will // return a CFSetRef that is serialized. //--------------------------------------------------------------------------- kern_return_t _io_ups_get_capabilities( mach_port_t server, int upsID, void ** capabilitiesBufferPtr, IOByteCount * capabilitiesBufferSizePtr) { CFSetRef capabilities; CFMutableDataRef data; CFDataRef serializedData; UPSDataRef upsDataRef; IOReturn res = kIOReturnError; if (!capabilitiesBufferPtr || !capabilitiesBufferSizePtr || !gUPSDataArrayRef || (upsID >= CFArrayGetCount(gUPSDataArrayRef))) { return kIOReturnBadArgument; } data = (CFMutableDataRef)CFArrayGetValueAtIndex(gUPSDataArrayRef, upsID); upsDataRef = (UPSDataRef)CFDataGetMutableBytePtr(data); if (!upsDataRef || !upsDataRef->upsPlugInInterface) return kIOReturnBadArgument; res = (*upsDataRef->upsPlugInInterface)->getCapabilities( upsDataRef->upsPlugInInterface, &capabilities); if ((res != kIOReturnSuccess) || !capabilities) return kIOReturnError; serializedData = (CFDataRef)IOCFSerialize( capabilities, kNilOptions ); if (!serializedData) return kIOReturnError; *capabilitiesBufferSizePtr = CFDataGetLength(serializedData); vm_allocate(mach_task_self(), (vm_address_t *)capabilitiesBufferPtr, *capabilitiesBufferSizePtr, TRUE); if( *capabilitiesBufferPtr ) memcpy(*capabilitiesBufferPtr, CFDataGetBytePtr(serializedData), *capabilitiesBufferSizePtr); CFRelease( serializedData ); return res; }
void DAUnitSetState( DADiskRef disk, DAUnitState state, Boolean value ) { CFNumberRef key; key = DADiskGetDescription( disk, kDADiskDescriptionMediaBSDUnitKey ); if ( key ) { CFMutableDataRef data; data = ( CFMutableDataRef ) CFDictionaryGetValue( gDAUnitList, key ); if ( data ) { __DAUnit * unit; unit = ( void * ) CFDataGetMutableBytePtr( data ); unit->state &= ~state; unit->state |= value ? state : 0; } else { data = CFDataCreateMutable( kCFAllocatorDefault, sizeof( __DAUnit ) ); if ( data ) { __DAUnit * unit; unit = ( void * ) CFDataGetMutableBytePtr( data ); unit->state = value ? state : 0; CFDictionarySetValue( gDAUnitList, key, data ); CFRelease( data ); } } } }
/* * Opens a configuration file and returns a CFArrayRef consisting * of a CFStringRef for each line. */ CFArrayRef configRead(const char *path) { int fd; struct stat statBuf; CFMutableDataRef data; CFStringRef str; CFArrayRef config = NULL; fd = open(path, O_RDONLY, 0644); if (fd < 0) { goto done; } if (fstat(fd, &statBuf) < 0) { goto done; } if ((statBuf.st_mode & S_IFMT) != S_IFREG) { goto done; } data = CFDataCreateMutable(NULL, statBuf.st_size); CFDataSetLength(data, statBuf.st_size); if(read(fd, (void *)CFDataGetMutableBytePtr(data), statBuf.st_size) != statBuf.st_size) { CFRelease(data); goto done; } str = CFStringCreateFromExternalRepresentation(NULL, data, kCFStringEncodingMacRoman); CFRelease(data); config = CFStringCreateArrayBySeparatingStrings(NULL, str, CFSTR("\n")); CFRelease(str); done: if (fd >= 0) { close(fd); } if (config == NULL) { CFMutableArrayRef emptyConfig; /* pretend that we found an empty file */ emptyConfig = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); CFArrayAppendValue(emptyConfig, CFSTR("")); config = (CFArrayRef)emptyConfig; } return config; }
CoreTextController::CoreTextRun::CoreTextRun(CTRunRef ctRun, const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength) : m_CTRun(ctRun) , m_fontData(fontData) , m_characters(characters) , m_stringLocation(stringLocation) , m_stringLength(stringLength) { m_glyphCount = CTRunGetGlyphCount(ctRun); m_indices = CTRunGetStringIndicesPtr(ctRun); if (!m_indices) { m_indicesData.adoptCF(CFDataCreateMutable(kCFAllocatorDefault, m_glyphCount * sizeof(CFIndex))); CFDataIncreaseLength(m_indicesData.get(), m_glyphCount * sizeof(CFIndex)); m_indices = reinterpret_cast<const CFIndex*>(CFDataGetMutableBytePtr(m_indicesData.get())); CTRunGetStringIndices(ctRun, CFRangeMake(0, 0), const_cast<CFIndex*>(m_indices)); } }
/* Create a URI suitable for use in an http GET request, will return NULL if the length would exceed 255 bytes. */ static CFURLRef createGetURL(CFURLRef responder, CFDataRef request) { CFURLRef getURL = NULL; CFMutableDataRef base64Request = NULL; CFStringRef base64RequestString = NULL; CFStringRef peRequest = NULL; CFIndex base64Len; base64Len = SecBase64Encode(NULL, CFDataGetLength(request), NULL, 0); /* Don't bother doing all the work below if we know the end result will exceed 255 bytes (minus one for the '/' separator makes 254). */ if (base64Len + CFURLGetBytes(responder, NULL, 0) > 254) return NULL; require(base64Request = CFDataCreateMutable(kCFAllocatorDefault, base64Len), errOut); CFDataSetLength(base64Request, base64Len); SecBase64Encode(CFDataGetBytePtr(request), CFDataGetLength(request), (char *)CFDataGetMutableBytePtr(base64Request), base64Len); require(base64RequestString = CFStringCreateWithBytes(kCFAllocatorDefault, CFDataGetBytePtr(base64Request), base64Len, kCFStringEncodingUTF8, false), errOut); require(peRequest = CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, base64RequestString, NULL, CFSTR("+/="), kCFStringEncodingUTF8), errOut); #if 1 CFStringRef urlString = CFURLGetString(responder); CFStringRef fullURL; if (CFStringHasSuffix(urlString, CFSTR("/"))) { fullURL = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@%@"), urlString, peRequest); } else { fullURL = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@/%@"), urlString, peRequest); } getURL = CFURLCreateWithString(kCFAllocatorDefault, fullURL, NULL); CFRelease(fullURL); #else getURL = CFURLCreateWithString(kCFAllocatorDefault, peRequest, responder); #endif errOut: CFReleaseSafe(base64Request); CFReleaseSafe(base64RequestString); CFReleaseSafe(peRequest); return getURL; }
STATIC CFDataRef make_DUID_LL_data(interface_t * if_p) { CFMutableDataRef data; int duid_len; DHCPDUID_LLRef ll_p; duid_len = offsetof(DHCPDUID_LL, linklayer_address) + if_link_length(if_p); data = CFDataCreateMutable(NULL, duid_len); CFDataSetLength(data, duid_len); ll_p = (DHCPDUID_LLRef)CFDataGetMutableBytePtr(data); DHCPDUIDSetType((DHCPDUIDRef)ll_p, kDHCPDUIDTypeLL); DHCPDUID_LLSetHardwareType(ll_p, if_link_arptype(if_p)); bcopy(if_link_address(if_p), ll_p->linklayer_address, if_link_length(if_p)); return (data); }
/* write a 32-bit word, little endian */ void appendUint32( CFMutableDataRef buf, uint32_t word) { #if 1 unsigned char cb[4]; OSWriteLittleInt32(cb, 0, word); CFDataAppendBytes(buf, cb, 4); #else /* This is an alternate implementation which may or may not be faster than the above. */ CFIndex offset = CFDataGetLength(buf); UInt8 *bytes = CFDataGetMutableBytePtr(buf); CFDataIncreaseLength(buf, 4); OSWriteLittleInt32(bytes, offset, word); #endif }
CFDataRef SOSCoderCopyDER(SOSCoderRef coder, CFErrorRef* error) { CFMutableDataRef encoded = NULL; size_t encoded_size = SOSCoderGetDEREncodedSize(coder, error); if (encoded_size > 0) { encoded = CFDataCreateMutable(NULL, encoded_size); if (encoded) { CFDataSetLength(encoded, encoded_size); uint8_t * der = CFDataGetMutableBytePtr(encoded); uint8_t * der_end = der + encoded_size; if (!SOSCoderEncodeToDER(coder, error, der, der_end)) { CFReleaseNull(encoded); encoded = NULL; } } } return encoded; }
CFDataRef SecKeyCopyExponent(SecKeyRef key) { ccrsa_pub_ctx_t pubkey; pubkey.pub = key->key; size_t e_size = ccn_write_uint_size(ccrsa_ctx_n(pubkey), ccrsa_ctx_e(pubkey)); CFAllocatorRef allocator = CFGetAllocator(key); CFMutableDataRef exponentData = CFDataCreateMutable(allocator, e_size); if (exponentData == NULL) return NULL; CFDataSetLength(exponentData, e_size); ccn_write_uint(ccrsa_ctx_n(pubkey), ccrsa_ctx_m(pubkey), e_size, CFDataGetMutableBytePtr(exponentData)); return exponentData; }
CFDataRef SecKeyCopyModulus(SecKeyRef key) { ccrsa_pub_ctx_t pubkey; pubkey.pub = key->key; size_t m_size = ccn_write_uint_size(ccrsa_ctx_n(pubkey), ccrsa_ctx_m(pubkey)); CFAllocatorRef allocator = CFGetAllocator(key); CFMutableDataRef modulusData = CFDataCreateMutable(allocator, m_size); if (modulusData == NULL) return NULL; CFDataSetLength(modulusData, m_size); ccn_write_uint(ccrsa_ctx_n(pubkey), ccrsa_ctx_m(pubkey), m_size, CFDataGetMutableBytePtr(modulusData)); return modulusData; }
CGImageRef Image::createAbstraction(float stylization, uint quantization) { pixel4b *rgbPixels = (pixel4b *) CFDataGetMutableBytePtr(_data); // Convert from RGB to Lab colorspace to perform operations on lightness channel. RGBtoLab(rgbPixels, _pixels); // Initial bilateral filter. bilateral(); // Extract edges. pixel3f *edges = createEdges(stylization); // Additional bilateral filtering. bilateral(); bilateral(); // Quantize lightness channel. quantize(quantization); // Overlay edges. overlayEdges(edges); // Convert back to RGB colorspace. LabtoRGB(_pixels, rgbPixels); // Create an image from the modified data. CGContextRef context = CGBitmapContextCreate( rgbPixels, _width, _height, _bitsPerComponent, _bytesPerRow, _colorSpaceRef, _bitmapInfo ); CGImageRef image = CGBitmapContextCreateImage(context); delete[] edges; return image; }
CFDataRef SOSRecoveryKeyBagCopyEncoded(SOSRecoveryKeyBagRef RecoveryKeyBag, CFErrorRef* error) { CFDataRef result = NULL; CFMutableDataRef encoded = NULL; size_t encodedSize = der_sizeof_RecoveryKeyBag(RecoveryKeyBag, error); require_quiet(encodedSize, fail); encoded = CFDataCreateMutableWithScratch(kCFAllocatorDefault, encodedSize); require_quiet(SecAllocationError(encoded, error, CFSTR("Failed to create scratch")), fail); uint8_t *encode_to = CFDataGetMutableBytePtr(encoded); uint8_t *encode_to_end = encode_to + CFDataGetLength(encoded); require_quiet(encode_to == der_encode_RecoveryKeyBag(RecoveryKeyBag, error, encode_to, encode_to_end), fail); CFTransferRetained(result, encoded); fail: CFReleaseSafe(encoded); return result; }
static void* TV2FP(CFMutableDictionaryRef dict, const char* name, void *tvp) { static uint32 glue[6] = { 0x3D800000, 0x618C0000, 0x800C0000, 0x804C0004, 0x7C0903A6, 0x4E800420 }; uint32* newGlue = NULL; if (tvp != NULL) { CFStringRef nameRef = CFStringCreateWithCString(NULL, name, kCFStringEncodingASCII); if (nameRef) { CFMutableDataRef glueData = (CFMutableDataRef) CFDictionaryGetValue(dict, nameRef); if (glueData == NULL) { glueData = CFDataCreateMutable(NULL, sizeof(glue)); if (glueData != NULL) { newGlue = (uint32*) CFDataGetMutableBytePtr(glueData); memcpy(newGlue, glue, sizeof(glue)); newGlue[0] |= ((UInt32)tvp >> 16); newGlue[1] |= ((UInt32)tvp & 0xFFFF); MakeDataExecutable(newGlue, sizeof(glue)); CFDictionaryAddValue(dict, nameRef, glueData); CFRelease(glueData); PR_LOG(_pr_linker_lm, PR_LOG_MIN, ("TV2FP: created wrapper for CFM function %s().", name)); } } else {
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; }
CGImageRef CGImageMaskCreateWithImageRef(CGImageRef imageRef) { size_t maskWidth = CGImageGetWidth(imageRef); size_t maskHeight = CGImageGetHeight(imageRef); size_t bytesPerRow = maskWidth; size_t bufferSize = maskWidth * maskHeight; CFMutableDataRef dataBuffer = CFDataCreateMutable(kCFAllocatorDefault, 0); CFDataSetLength(dataBuffer, bufferSize); CGColorSpaceRef greyColorSpaceRef = CGColorSpaceCreateDeviceGray(); CGContextRef ctx = CGBitmapContextCreate(CFDataGetMutableBytePtr(dataBuffer), maskWidth, maskHeight, 8, bytesPerRow, greyColorSpaceRef, kCGImageAlphaNone); CGContextDrawImage(ctx, CGRectMake(0, 0, maskWidth, maskHeight), imageRef); CGContextRelease(ctx); CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData(dataBuffer); CGImageRef maskImageRef = CGImageMaskCreate(maskWidth, maskHeight, 8, 8, bytesPerRow, dataProvider, NULL, FALSE); CGDataProviderRelease(dataProvider); CGColorSpaceRelease(greyColorSpaceRef); CFRelease(dataBuffer); return maskImageRef; }