bool HTTP_Stream::open(const HTTP_Stream_Position& position) { bool success = false; CFStreamClientContext CTX = { 0, this, NULL, NULL, NULL }; /* Already opened a read stream, return */ if (m_readStream) { goto out; } /* Reset state */ m_position = position; m_readPending = false; m_httpHeadersParsed = false; m_contentType = ""; m_icyStream = false; m_icyHeaderCR = false; m_icyHeadersRead = false; m_icyHeadersParsed = false; m_icyHeaderLines.clear(); m_icyMetaDataInterval = 0; m_dataByteReadCount = 0; m_metaDataBytesRemaining = 0; if (!m_url) { goto out; } /* Failed to create a stream */ if (!(m_readStream = createReadStream(m_url))) { goto out; } if (!CFReadStreamSetClient(m_readStream, kCFStreamEventHasBytesAvailable | kCFStreamEventEndEncountered | kCFStreamEventErrorOccurred, readCallBack, &CTX)) { CFRelease(m_readStream), m_readStream = 0; goto out; } setScheduledInRunLoop(true); if (!CFReadStreamOpen(m_readStream)) { /* Open failed: clean */ CFReadStreamSetClient(m_readStream, 0, NULL, NULL); setScheduledInRunLoop(false); if (m_readStream) { CFRelease(m_readStream), m_readStream = 0; } goto out; } success = true; out: return success; }
bool HTTPInputSource::Open(CFErrorRef *error) { if(IsOpen()) { LOGGER_WARNING("org.sbooth.AudioEngine.InputSource.HTTP", "Open() called on an InputSource that is already open"); return true; } // Set up the HTTP request mRequest = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("GET"), mURL, kCFHTTPVersion1_1); if(NULL == mRequest) { if(error) *error = CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainPOSIX, ENOMEM, NULL); return false; } CFHTTPMessageSetHeaderFieldValue(mRequest, CFSTR("User-Agent"), CFSTR("SFBAudioEngine")); // Seek support if(0 < mDesiredOffset) { CFStringRef byteRange = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("bytes=%ld-"), mDesiredOffset); CFHTTPMessageSetHeaderFieldValue(mRequest, CFSTR("Range"), byteRange); CFRelease(byteRange), byteRange = NULL; } mReadStream = CFReadStreamCreateForStreamedHTTPRequest(kCFAllocatorDefault, mRequest, NULL); if(NULL == mReadStream) { CFRelease(mRequest), mRequest = NULL; if(error) *error = CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainPOSIX, ENOMEM, NULL); return false; } // Start the HTTP connection CFStreamClientContext myContext = { 0, this, NULL, NULL, NULL }; CFOptionFlags clientFlags = kCFStreamEventOpenCompleted | kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered; if(!CFReadStreamSetClient(mReadStream, clientFlags, myCFReadStreamClientCallBack, &myContext)) { CFRelease(mRequest), mRequest = NULL; CFRelease(mReadStream), mReadStream = NULL; if(error) *error = CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainPOSIX, ENOMEM, NULL); return false; } CFReadStreamScheduleWithRunLoop(mReadStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); if(!CFReadStreamOpen(mReadStream)) { CFRelease(mRequest), mRequest = NULL; CFRelease(mReadStream), mReadStream = NULL; if(error) *error = CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainPOSIX, ENOMEM, NULL); return false; } while(NULL == mResponseHeaders) CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true); mIsOpen = true; return true; }
static void nc_trigger(int argc, char **argv) { Boolean background = FALSE; int i; CFStringRef hostName = NULL; int port = 80; for (i = 0; i < 3 && i < argc; i++) { /* Parse host name. Must be first arg. */ if (i == 0) { hostName = CFStringCreateWithCString(NULL, argv[i], kCFStringEncodingUTF8); continue; } /* Check for optional background flag */ if (strcmp(argv[i], "background") == 0) { background = TRUE; continue; } /* Parse optional port number */ CFStringRef str = CFStringCreateWithCString(NULL, argv[i], kCFStringEncodingUTF8); if (str) { int num = CFStringGetIntValue(str); if (num) { port = num; } my_CFRelease(&str); } } if (hostName) { CFReadStreamRef readStream = NULL; CFWriteStreamRef writeStream = NULL; CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, hostName, port, &readStream, &writeStream); if (background) { CFReadStreamSetProperty(readStream, CFSTR("kCFStreamNetworkServiceType"), CFSTR("kCFStreamNetworkServiceTypeBackground")); CFWriteStreamSetProperty(writeStream, CFSTR("kCFStreamNetworkServiceType"), CFSTR("kCFStreamNetworkServiceTypeBackground")); } if (readStream && writeStream) { CFReadStreamOpen(readStream); CFWriteStreamOpen(writeStream); SCPrint(TRUE, stdout, CFSTR("Opened stream to %@, port %d%s\n"), hostName, port, background ? ", background traffic class" : ""); sleep(1); } my_CFRelease(&readStream); my_CFRelease(&writeStream); } else { SCPrint(TRUE, stderr, CFSTR("Invalid or missing host name\n")); } my_CFRelease(&hostName); exit(0); }
void updateKeymap() { CFReadStreamRef readStream; if (!fileExists(CFSTR(KEYMAP_PATH))) { stamp_file(CFSTR("Error: Keymap file is missing")); keymap = NULL; return; } readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault,CFURLCreateWithFileSystemPath(kCFAllocatorDefault,CFSTR(KEYMAP_PATH),kCFURLPOSIXPathStyle,false)); if (!readStream||!(CFReadStreamOpen(readStream))) { stamp_file(CFSTR("Error: Can't open keymap file")); keymap = NULL; return; } keymap = (CFDictionaryRef)CFPropertyListCreateFromStream(kCFAllocatorDefault,readStream,0,kCFPropertyListImmutable,NULL,NULL); CFReadStreamClose(readStream); if (!keymap) { stamp_file(CFSTR("Error: Can't read keymap file")); return; } Boolean validKey; showMods = (CFPreferencesGetAppBooleanValue(CFSTR("Mods"),PREF_DOMAIN,&validKey))?kCFBooleanTrue:kCFBooleanFalse; if (!validKey) { showMods = kCFBooleanTrue; CFPreferencesSetAppValue(CFSTR("Mods"),showMods,PREF_DOMAIN); } }
void SocketStreamHandle::scheduleStreams() { ASSERT(m_readStream); ASSERT(m_writeStream); CFStreamClientContext clientContext = { 0, this, 0, 0, copyCFStreamDescription }; // FIXME: Pass specific events we're interested in instead of -1. CFReadStreamSetClient(m_readStream.get(), static_cast<CFOptionFlags>(-1), readStreamCallback, &clientContext); CFWriteStreamSetClient(m_writeStream.get(), static_cast<CFOptionFlags>(-1), writeStreamCallback, &clientContext); #if PLATFORM(WIN) CFReadStreamScheduleWithRunLoop(m_readStream.get(), loaderRunLoop(), kCFRunLoopDefaultMode); CFWriteStreamScheduleWithRunLoop(m_writeStream.get(), loaderRunLoop(), kCFRunLoopDefaultMode); #else CFReadStreamScheduleWithRunLoop(m_readStream.get(), CFRunLoopGetCurrent(), kCFRunLoopCommonModes); CFWriteStreamScheduleWithRunLoop(m_writeStream.get(), CFRunLoopGetCurrent(), kCFRunLoopCommonModes); #endif CFReadStreamOpen(m_readStream.get()); CFWriteStreamOpen(m_writeStream.get()); #ifndef BUILDING_ON_TIGER if (m_pacRunLoopSource) removePACRunLoopSource(); #endif m_connectingSubstate = WaitingForConnect; }
CFDataRef WebApiClientMD5DigestCreateWithFilePath(CFStringRef filePath, size_t bufferSize) { // Declare needed variables CFDataRef result = NULL; CFReadStreamRef readStream = NULL; // Get the file URL CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)filePath, kCFURLPOSIXPathStyle, (Boolean)false); if ( fileURL ) { // Create and open the read stream readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault, fileURL); if ( readStream ) { Boolean didSucceed = CFReadStreamOpen(readStream); if ( didSucceed ) { // Initialize the hash object CC_MD5_CTX hashObject; CC_MD5_Init(&hashObject); // Make sure chunkSizeForReadingData is valid if ( bufferSize < 1 ) { bufferSize = FileHashDefaultChunkSizeForReadingData; } // Feed the data to the hash object bool hasMoreData = true; while ( hasMoreData ) { uint8_t buffer[bufferSize]; CFIndex readBytesCount = CFReadStreamRead(readStream, (UInt8 *)buffer, (CFIndex)sizeof(buffer)); if ( readBytesCount == -1 ) break; if ( readBytesCount == 0 ) { hasMoreData = false; continue; } CC_MD5_Update(&hashObject, (const void *)buffer, (CC_LONG)readBytesCount); } // Check if the read operation succeeded didSucceed = !hasMoreData; // Compute the hash digest unsigned char digest[CC_MD5_DIGEST_LENGTH]; CC_MD5_Final(digest, &hashObject); // Abort if the read operation failed if ( didSucceed ) { result = CFDataCreate(kCFAllocatorDefault, digest, CC_MD5_DIGEST_LENGTH); } } } } if ( readStream ) { CFReadStreamClose(readStream); CFRelease(readStream); } if ( fileURL ) { CFRelease(fileURL); } return result; }
extern CFDictionaryRef GPPropertyListCopyLocalizableStringsDictionary(CFURLRef fileURL) { CFReadStreamRef stream = CFReadStreamCreateWithFile(NULL, fileURL); if (stream == NULL) return NULL; if (!CFReadStreamOpen(stream)) { CFRelease(stream); return NULL; } CFDictionaryRef dict = CFPropertyListCreateFromStream(NULL, stream, 0, kCFPropertyListImmutable, NULL, NULL); CFReadStreamClose(stream); CFRelease(stream); if (dict == NULL) return NULL; if (CFGetTypeID(dict) != CFDictionaryGetTypeID()) { CFRelease(dict); return NULL; } CFDictionaryRef localizedStringsDict = CFDictionaryGetValue(dict, CFSTR("Localizations")); if (localizedStringsDict == NULL || CFGetTypeID(localizedStringsDict) != CFDictionaryGetTypeID()) { CFRelease(dict); return NULL; } CFIndex langCount = CFDictionaryGetCount(localizedStringsDict); CFStringRef keys[langCount]; // assume we don't have 4000 languages :p CFDictionaryGetKeysAndValues(localizedStringsDict, (const void**)keys, NULL); CFArrayCallBacks languagesCallbacks = {0, NULL, NULL, NULL, &CFEqual}; CFArrayRef languages = CFArrayCreate(NULL, (const void**)keys, langCount, &languagesCallbacks); CFArrayRef preferedLanguages = CFBundleCopyPreferredLocalizationsFromArray(languages); CFDictionaryRef retval = CFRetain(CFDictionaryGetValue(localizedStringsDict, CFArrayGetValueAtIndex(preferedLanguages, 0))); CFRelease(languages); CFRelease(preferedLanguages); CFRelease(dict); return retval; }
CURLcode SSLImpl::connect(int sockfd, int nonblocking, int *done, int ssl_verify_peer, void *storage, char* host_name) { ssl_data_t *data = (ssl_data_t*)storage; CFStreamCreatePairWithSocket(kCFAllocatorDefault, sockfd, &data->readStream, &data->writeStream); // Indicate that the connection needs to be done in secure manner CFReadStreamSetProperty(data->readStream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelSSLv3); CFWriteStreamSetProperty(data->writeStream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelSSLv3); CFMutableDictionaryRef dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); if (!dict) { RAWLOG_ERROR("Can not allocate CFMutableDictionaryRef"); return CURLE_SSL_CONNECT_ERROR; } CFDictionarySetValue(dict, kCFStreamSSLValidatesCertificateChain, ssl_verify_peer ? kCFBooleanTrue : kCFBooleanFalse); CFReadStreamSetProperty(data->readStream, kCFStreamPropertySSLSettings, dict); CFWriteStreamSetProperty(data->writeStream, kCFStreamPropertySSLSettings, dict); CFRelease(dict); if (!CFReadStreamOpen(data->readStream) || !CFWriteStreamOpen(data->writeStream)) { RAWLOG_ERROR("SSL connection error"); return CURLE_SSL_CONNECT_ERROR; } CFReadStreamScheduleWithRunLoop( data->readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); CFWriteStreamScheduleWithRunLoop( data->writeStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); *done = 1; return CURLE_OK; }
static void FetchIPAddress() { if(publicIPState == IPStateFetching) return; publicIPState = IPStateFetching; const UInt8 bodyBytes[] = {0}; CFDataRef body = CFDataCreate(kCFAllocatorDefault, bodyBytes, 0); CFURLRef url = CFURLCreateWithString(kCFAllocatorDefault, CFSTR("http://icanhazip.com"), NULL); CFHTTPMessageRef request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("GET"), url, kCFHTTPVersion1_1); CFHTTPMessageSetBody(request, body); #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" // Apple suggests the NSURLSession API instead of CFReadStreamCreateForHTTPRequest, // But obviously that doesn't really work here CFReadStreamRef stream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request); #pragma clang diagnostic pop CFRelease(body); CFRelease(url); CFRelease(request); CFMutableDataRef responseData = CFDataCreateMutable(kCFAllocatorDefault, 17); CFStreamClientContext context = { 0, responseData, NULL, NULL, NULL }; if(!CFReadStreamSetClient(stream, kCFStreamEventOpenCompleted | kCFStreamEventHasBytesAvailable | kCFStreamEventEndEncountered | kCFStreamEventErrorOccurred, &IPStreamCallback, &context)) { CFRelease(stream); publicIPState = IPStateInvalid; return; } // Add to the run loop and open the stream CFReadStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); if(!CFReadStreamOpen(stream)) { CFReadStreamSetClient(stream, 0, NULL, NULL); CFReadStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); CFRelease(stream); publicIPState = IPStateInvalid; return; } // Run the run loop do { CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0, TRUE); } while(publicIPState == IPStateFetching); }
static void openNextStream(FormStreamFields* form) { // Skip over any streams we can't open. // For some purposes we might want to return an error, but the current CFURLConnection // can't really do anything useful with an error at this point, so this is better. advanceCurrentStream(form); while (form->currentStream && !CFReadStreamOpen(form->currentStream)) advanceCurrentStream(form); }
static bool openNextStream(FormStreamFields* form) { // Skip over any streams we can't open. if (!advanceCurrentStream(form)) return false; while (form->currentStream && !CFReadStreamOpen(form->currentStream)) { if (!advanceCurrentStream(form)) return false; } return true; }
CFStringRef copy_disk_app_identifier(CFURLRef disk_app_url) { CFURLRef plist_url = CFURLCreateCopyAppendingPathComponent(NULL, disk_app_url, CFSTR("Info.plist"), false); CFReadStreamRef plist_stream = CFReadStreamCreateWithFile(NULL, plist_url); CFReadStreamOpen(plist_stream); CFPropertyListRef plist = CFPropertyListCreateWithStream(NULL, plist_stream, 0, kCFPropertyListImmutable, NULL, NULL); CFStringRef bundle_identifier = CFRetain(CFDictionaryGetValue(plist, CFSTR("CFBundleIdentifier"))); CFReadStreamClose(plist_stream); CFRelease(plist_url); CFRelease(plist_stream); CFRelease(plist); return bundle_identifier; }
/* MySimpleDirectoryListing implements the directory list command. It sets up a MyStreamInfo 'object' with the read stream being an FTP stream of the directory to list and with no write stream. It then returns, and the real work happens asynchronously in the runloop. The function returns true if the stream setup succeeded, and false if it failed. */ static Boolean MySimpleDirectoryListing(CFStringRef urlString, CFStringRef username, CFStringRef password) { CFReadStreamRef readStream; CFStreamClientContext context = { 0, NULL, NULL, NULL, NULL }; CFURLRef downloadURL; Boolean success = true; MyStreamInfo *streamInfo; assert(urlString != NULL); downloadURL = CFURLCreateWithString(kCFAllocatorDefault, urlString, NULL); assert(downloadURL != NULL); /* Create an FTP read stream for downloading operation from an FTP URL. */ readStream = CFReadStreamCreateWithFTPURL(kCFAllocatorDefault, downloadURL); assert(readStream != NULL); CFRelease(downloadURL); /* Initialize our MyStreamInfo structure, which we use to store some information about the stream. */ MyStreamInfoCreate(&streamInfo, readStream, NULL); context.info = (void *)streamInfo; /* CFReadStreamSetClient registers a callback to hear about interesting events that occur on a stream. */ success = CFReadStreamSetClient(readStream, kNetworkEvents, MyDirectoryListingCallBack, &context); if (success) { /* Schedule a run loop on which the client can be notified about stream events. The client callback will be triggered via the run loop. It's the caller's responsibility to ensure that the run loop is running. */ CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); MyCFStreamSetUsernamePassword(readStream, username, password); MyCFStreamSetFTPProxy(readStream, &streamInfo->proxyDict); /* CFReadStreamOpen will return success/failure. Opening a stream causes it to reserve all the system resources it requires. If the stream can open non-blocking, this will always return TRUE; listen to the run loop source to find out when the open completes and whether it was successful. */ success = CFReadStreamOpen(readStream); if (success == false) { fprintf(stderr, "CFReadStreamOpen failed\n"); MyStreamInfoDestroy(streamInfo); } } else { fprintf(stderr, "CFReadStreamSetClient failed\n"); MyStreamInfoDestroy(streamInfo); } return success; }
static bool openNextStream(FormStreamFields* form) { // CFReadStreamOpen() can cause this function to be re-entered from another thread before it returns. // One example when this can occur is when the stream being opened has no data. See <rdar://problem/23550269>. LockHolder locker(form->streamIsBeingOpenedOrClosedLock); // Skip over any streams we can't open. if (!advanceCurrentStream(form)) return false; while (form->currentStream && !CFReadStreamOpen(form->currentStream)) { if (!advanceCurrentStream(form)) return false; } return true; }
AppleBufferInputStream::AppleBufferInputStream(const StringBuffer& dataString) : AppleInputStream() { data = dataString.c_str(); totalSize = dataString.length(); if (data) { readStream = CFReadStreamCreateWithBytesNoCopy(kCFAllocatorDefault, (const UInt8 *)data, (CFIndex)totalSize, kCFAllocatorNull); if (CFReadStreamOpen(readStream) == TRUE) { streamOpened = true; } } }
sdmmd_return_t SDMMD_ServiceReceiveStream(SocketConnection handle, CFPropertyListRef *data) { CFDataRef dataBuffer = NULL; if (SDM_MD_CallSuccessful(SDMMD_ServiceReceive(handle, &dataBuffer))) { if (dataBuffer && CFDataGetLength(dataBuffer)) { CFReadStreamRef read = CFReadStreamCreateWithBytesNoCopy(kCFAllocatorDefault, CFDataGetBytePtr(dataBuffer), CFDataGetLength(dataBuffer), kCFAllocatorNull); CFReadStreamOpen(read); *data = CFPropertyListCreateWithStream(kCFAllocatorDefault, read, CFDataGetLength(dataBuffer), 0x2, 0, NULL); CFReadStreamClose(read); if (read) CFRelease(read); } return kAMDSuccess; } else { return kAMDNotConnectedError; } }
static bool OnLionOrLater() { if (sOnLionOrLater < 0) { SInt32 major = 0, minor = 0; CFURLRef url = CFURLCreateWithString(kCFAllocatorDefault, CFSTR("file:///System/Library/CoreServices/SystemVersion.plist"), NULL); CFReadStreamRef stream = CFReadStreamCreateWithFile(kCFAllocatorDefault, url); CFReadStreamOpen(stream); CFDictionaryRef sysVersionPlist = (CFDictionaryRef) CFPropertyListCreateWithStream(kCFAllocatorDefault, stream, 0, kCFPropertyListImmutable, NULL, NULL); CFReadStreamClose(stream); CFRelease(stream); CFRelease(url); CFStringRef versionString = (CFStringRef) CFDictionaryGetValue(sysVersionPlist, CFSTR("ProductVersion")); CFArrayRef versions = CFStringCreateArrayBySeparatingStrings(kCFAllocatorDefault, versionString, CFSTR(".")); CFIndex count = CFArrayGetCount(versions); if (count > 0) { CFStringRef component = (CFStringRef) CFArrayGetValueAtIndex(versions, 0); major = CFStringGetIntValue(component); if (count > 1) { component = (CFStringRef) CFArrayGetValueAtIndex(versions, 1); minor = CFStringGetIntValue(component); } } CFRelease(sysVersionPlist); CFRelease(versions); if (major < 10) { sOnLionOrLater = 0; } else { int version = 0x1000 + (minor << 4); sOnLionOrLater = version >= MAC_OS_X_VERSION_10_7_HEX ? 1 : 0; } } return sOnLionOrLater > 0 ? true : false; }
CFStringRef get_bundle_id(const char *app_path) { CFURLRef app_url = get_absolute_file_url(app_path); if (app_url == NULL) { return NULL; } CFURLRef url = CFURLCreateCopyAppendingPathComponent(NULL, app_url, CFSTR("Info.plist"), false); CFRelease(app_url); if (url == NULL) { return NULL; } CFReadStreamRef stream = CFReadStreamCreateWithFile(NULL, url); CFRelease(url); if (stream == NULL) { return NULL; } CFPropertyListRef plist = NULL; if (CFReadStreamOpen(stream) == TRUE) { plist = CFPropertyListCreateWithStream(NULL, stream, 0, kCFPropertyListImmutable, NULL, NULL); } CFReadStreamClose(stream); CFRelease(stream); if (plist == NULL) { return NULL; } const void *value = CFDictionaryGetValue(plist, CFSTR("CFBundleIdentifier")); CFStringRef bundle_id = NULL; if (value != NULL) { bundle_id = CFRetain(value); } CFRelease(plist); return bundle_id; }
BEGIN_FUNAMBOL_NAMESPACE AppleBufferInputStream::AppleBufferInputStream(const void* data_, const unsigned int dataSize) : AppleInputStream() { data = data_; totalSize = dataSize; if (data) { readStream = CFReadStreamCreateWithBytesNoCopy(kCFAllocatorDefault, (const UInt8 *)data, (CFIndex)dataSize, kCFAllocatorNull); if (CFReadStreamOpen(readStream) == TRUE) { streamOpened = true; } } }
static CFTypeRef CopyKeyFromFile(CFStringRef domain, CFStringRef key) { CFReadStreamRef s; CFDictionaryRef d; CFStringRef file; CFErrorRef e; CFURLRef url; CFTypeRef val; file = CFStringCreateWithFormat(NULL, 0, CFSTR("/Library/Preferences/%@.plist"), domain); if (file == NULL) return NULL; url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, domain, kCFURLPOSIXPathStyle, false); CFRelease(file); if (url == NULL) return NULL; s = CFReadStreamCreateWithFile(kCFAllocatorDefault, url); CFRelease(url); if (s == NULL) return NULL; if (!CFReadStreamOpen(s)) { CFRelease(s); return NULL; } d = (CFDictionaryRef)CFPropertyListCreateWithStream (kCFAllocatorDefault, s, 0, kCFPropertyListImmutable, NULL, &e); CFRelease(s); if (d == NULL) return NULL; if (CFGetTypeID(d) != CFDictionaryGetTypeID()) { CFRelease(d); return NULL; } val = CFDictionaryGetValue(d, key); if (val) CFRetain(val); CFRelease(d); return val; }
sdmmd_return_t SDMMD_ServiceReceiveStream(SocketConnection handle, CFPropertyListRef *data) { CFDataRef dataBuffer = NULL; sdmmd_return_t result = SDMMD_ServiceReceive(handle, &dataBuffer); CheckErrorAndReturn(result); if (dataBuffer && CFDataGetLength(dataBuffer)) { CFReadStreamRef read = CFReadStreamCreateWithBytesNoCopy(kCFAllocatorDefault, CFDataGetBytePtr(dataBuffer), CFDataGetLength(dataBuffer), kCFAllocatorNull); CFReadStreamOpen(read); *data = CFPropertyListCreateWithStream(kCFAllocatorDefault, read, CFDataGetLength(dataBuffer), kCFPropertyListMutableContainersAndLeaves, NULL, NULL); CFReadStreamClose(read); CFSafeRelease(read); } result = kAMDSuccess; CFSafeRelease(dataBuffer); ExitLabelAndReturn(result); }
static krb5_error_code parse_plist_config(krb5_context context, const char *path, krb5_config_section **parent) { CFReadStreamRef s; CFDictionaryRef d; CFURLRef url; url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (UInt8 *)path, strlen(path), FALSE); if (url == NULL) { krb5_clear_error_message(context); return ENOMEM; } s = CFReadStreamCreateWithFile(kCFAllocatorDefault, url); CFRelease(url); if (s == NULL) { krb5_clear_error_message(context); return ENOMEM; } if (!CFReadStreamOpen(s)) { CFRelease(s); krb5_clear_error_message(context); return ENOENT; } #ifdef HAVE_CFPROPERTYLISTCREATEWITHSTREAM d = (CFDictionaryRef)CFPropertyListCreateWithStream(NULL, s, 0, kCFPropertyListImmutable, NULL, NULL); #else d = (CFDictionaryRef)CFPropertyListCreateFromStream(NULL, s, 0, kCFPropertyListImmutable, NULL, NULL); #endif CFRelease(s); if (d == NULL) { krb5_clear_error_message(context); return ENOENT; } CFDictionaryApplyFunction(d, convert_content, parent); CFRelease(d); return 0; }
CFDictionaryRef download_file(int socket, CFDictionaryRef dict) { UInt8 buffer[8192]; CFIndex bytesRead; CFStringRef path = CFDictionaryGetValue(dict, CFSTR("Path")); if(path == NULL || CFGetTypeID(path) != CFStringGetTypeID()) return NULL; CFMutableDictionaryRef out = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path, kCFURLPOSIXPathStyle, FALSE); CFReadStreamRef stream = CFReadStreamCreateWithFile(kCFAllocatorDefault, fileURL); CFRelease(fileURL); if(!CFReadStreamOpen(stream)) { CFErrorRef error = CFReadStreamCopyError(stream); if (error != NULL) { CFStringRef errorDesc = CFErrorCopyDescription(error); CFDictionaryAddValue(out, CFSTR("Error"), errorDesc); CFRelease(errorDesc); CFRelease(error); } CFRelease(stream); return out; } CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0); while(CFReadStreamHasBytesAvailable(stream)) { if((bytesRead = CFReadStreamRead(stream, buffer, 8192)) <= 0) break; CFDataAppendBytes(data, buffer, bytesRead); } CFReadStreamClose(stream); CFRelease(stream); CFDictionaryAddValue(out, CFSTR("Data"), data); CFRelease(data); return out; }
CFPropertyListRef createPropertyListFromURL(CFURLRef file, u_int32_t mutability, CFPropertyListFormat *outFormat, CFStringRef *outErrorString) { CFPropertyListRef plist = NULL; if (!file) NSLog(CFSTR("in createPropertyListFromURL in CFGrowlAdditions: cannot read from a NULL URL")); else { CFReadStreamRef stream = CFReadStreamCreateWithFile(kCFAllocatorDefault, file); if (!stream) NSLog(CFSTR("in createPropertyListFromURL in CFGrowlAdditions: could not create stream for reading from URL %@"), file); else { if (!CFReadStreamOpen(stream)) NSLog(CFSTR("in createPropertyListFromURL in CFGrowlAdditions: could not open stream for reading from URL %@"), file); else { CFPropertyListFormat format; CFStringRef errorString = NULL; plist = CFPropertyListCreateFromStream(kCFAllocatorDefault, stream, /*streamLength*/ 0, mutability, &format, &errorString); if (!plist) NSLog(CFSTR("in createPropertyListFromURL in CFGrowlAdditions: could not read property list from URL %@ (error string: %@)"), file, errorString); if (outFormat) *outFormat = format; if (errorString) { if (outErrorString) *outErrorString = errorString; else CFRelease(errorString); } CFReadStreamClose(stream); } CFRelease(stream); } } return plist; }
CFDictionaryRef get_escrow_record(int socket, CFDictionaryRef dict) { CFStringRef hostid = CFDictionaryGetValue(dict, CFSTR("HostID")); if(hostid == NULL || CFGetTypeID(hostid) != CFStringGetTypeID()) return NULL; //TODO: check return values... CFStringRef path = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("/mnt2/root/Library/Lockdown/escrow_records/%@.plist"), hostid); //CFStringRef path = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("/private/var/root/Library/Lockdown/escrow_records/%@.plist"), hostid); CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path, kCFURLPOSIXPathStyle, FALSE); CFReadStreamRef stream = CFReadStreamCreateWithFile(kCFAllocatorDefault, fileURL); CFReadStreamOpen(stream); CFPropertyListRef plist = CFPropertyListCreateWithStream(kCFAllocatorDefault, stream, 0, kCFPropertyListImmutable, NULL, NULL); CFRelease(fileURL); CFRelease(stream); CFRelease(path); return plist; }
bool TCPStream_CFNetwork::connect() { state = Stream::State::Opening; CFStringRef host_cfstring = CFStringCreateWithCString(kCFAllocatorDefault, remoteHostName.c_str(), kCFStringEncodingUTF8); CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host_cfstring, remotePortNumber, &inputStream, &outputStream); CFRelease(host_cfstring); CFStreamClientContext inputStreamContext; memset(&inputStreamContext, 0, sizeof(inputStreamContext)); inputStreamContext.info = reinterpret_cast<void *>(this); CFReadStreamSetClient(inputStream, kCFStreamEventHasBytesAvailable | kCFStreamEventEndEncountered | kCFStreamEventErrorOccurred, inputStreamCallback, &inputStreamContext); CFReadStreamScheduleWithRunLoop(inputStream, CFRunLoopGetMain(), kCFRunLoopDefaultMode); if (CFReadStreamOpen(inputStream) && CFWriteStreamOpen(outputStream)) { state = Stream::State::Open; return true; } else { CFRelease(outputStream); CFRelease(inputStream); outputStream = nullptr; inputStream = nullptr; state = Stream::State::NotOpen; return false; } }
CFDataRef createDataFromURL( CFURLRef url ) { CFMutableDataRef fileContent = CFDataCreateMutable(kCFAllocatorDefault, 0); CFReadStreamRef stream = CFReadStreamCreateWithFile(kCFAllocatorDefault, url); if (stream) { if (CFReadStreamOpen(stream)) { UInt8 buffer[BUFFERSIZE]; CFIndex bytesRead; do { bytesRead = CFReadStreamRead(stream, buffer, sizeof(buffer)); if (bytesRead > 0) { CFDataAppendBytes(fileContent, buffer, bytesRead); } } while (bytesRead > 0); CFReadStreamClose(stream); } CFRelease(stream); } return fileContent; }
static RetainPtr<CFPropertyListRef> readFontPlist() { CString plistPath = fontsPlistPath().utf8(); RetainPtr<CFURLRef> url(AdoptCF, CFURLCreateFromFileSystemRepresentation(0, reinterpret_cast<const UInt8*>(plistPath.data()), plistPath.length(), false)); if (!url) return 0; RetainPtr<CFReadStreamRef> stream(AdoptCF, CFReadStreamCreateWithFile(0, url.get())); if (!stream) return 0; if (!CFReadStreamOpen(stream.get())) return 0; CFPropertyListFormat format = kCFPropertyListBinaryFormat_v1_0 | kCFPropertyListXMLFormat_v1_0; RetainPtr<CFPropertyListRef> plist(AdoptCF, CFPropertyListCreateFromStream(0, stream.get(), 0, kCFPropertyListMutableContainersAndLeaves, &format, 0)); CFReadStreamClose(stream.get()); return plist; }
static int low_open(mailstream_low * s) { struct mailstream_cfstream_data * cfstream_data; int r; cfstream_data = (struct mailstream_cfstream_data *) s->data; CFReadStreamOpen(cfstream_data->readStream); CFWriteStreamOpen(cfstream_data->writeStream); r = wait_runloop(s, STATE_WAIT_OPEN); if (r != WAIT_RUNLOOP_EXIT_NO_ERROR) { return -1; } if (cfstream_data->writeOpenResult < 0) return -1; if (cfstream_data->readOpenResult < 0) return -1; return 0; }
void HTTPLoader::load(CFURLRef url) { CFHTTPMessageRef request = CFHTTPMessageCreateRequest(NULL, CFSTR("GET"), url, kCFHTTPVersion1_1); mStream = CFReadStreamCreateForHTTPRequest(NULL, request); CFRelease(request); if (!CFReadStreamOpen(mStream)) { CFRelease(mStream); mStream = 0; } else { CFStreamClientContext context = {0, this, NULL, NULL, NULL}; CFReadStreamSetClient(mStream, kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered, readStreamCallback, &context); CFReadStreamScheduleWithRunLoop(mStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); } }