X-Git-Url: https://git.saurik.com/apple/cf.git/blobdiff_plain/b0e0750aaee7a1a2cd8e3aa918bd9fc756e9dfcf..cf7d2af96685aba74e84652f9ed1098e9253902e:/CFURLAccess.c diff --git a/CFURLAccess.c b/CFURLAccess.c index e4fe101..2e0cae4 100644 --- a/CFURLAccess.c +++ b/CFURLAccess.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008 Apple Inc. All rights reserved. + * Copyright (c) 2009 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * @@ -21,8 +21,8 @@ * @APPLE_LICENSE_HEADER_END@ */ /* CFURLAccess.c - Copyright 1999-2002, Apple, Inc. All rights reserved. - Responsibility: Becky Willrich + Copyright (c) 1999-2009, Apple Inc. All rights reserved. + Responsibility: Chris Linn */ /*------ @@ -37,7 +37,8 @@ CFData read/write routines #include #include #include -#if DEPLOYMENT_TARGET_MACOSX +#include +#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED #include #include #include @@ -46,17 +47,37 @@ CFData read/write routines #include #include #include +#elif DEPLOYMENT_TARGET_WINDOWS +//#include +#include +#include +#include +#include +#include +#include +#else +#error Unknown or unspecified DEPLOYMENT_TARGET #endif -#if DEPLOYMENT_TARGET_MACOSX +#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED + -DEFINE_WEAK_CFNETWORK_FUNC(Boolean, _CFURLCreateDataAndPropertiesFromResource, (CFAllocatorRef A, CFURLRef B, CFDataRef *C, CFDictionaryRef *D, CFArrayRef E, SInt32 *F), (A, B, C, D, E, F), false) -DEFINE_WEAK_CFNETWORK_FUNC(Boolean, _CFURLWriteDataAndPropertiesToResource, (CFURLRef A, CFDataRef B, CFDictionaryRef C, SInt32 *D), (A, B, C, D), false) -DEFINE_WEAK_CFNETWORK_FUNC(Boolean, _CFURLDestroyResource, (CFURLRef A, SInt32 *B), (A, B), false) +DEFINE_WEAK_CFNETWORK_FUNC_FAIL(Boolean, _CFURLCreateDataAndPropertiesFromResource, (CFAllocatorRef A, CFURLRef B, CFDataRef *C, CFDictionaryRef *D, CFArrayRef E, SInt32 *F), (A, B, C, D, E, F), { if(C) *C=NULL; if (D) *D=NULL; if(F) *F=kCFURLImproperArgumentsError; }, false) +DEFINE_WEAK_CFNETWORK_FUNC_FAIL(Boolean, _CFURLWriteDataAndPropertiesToResource, (CFURLRef A, CFDataRef B, CFDictionaryRef C, SInt32 *D), (A, B, C, D), if (D) *D = kCFURLImproperArgumentsError, false) +DEFINE_WEAK_CFNETWORK_FUNC_FAIL(Boolean, _CFURLDestroyResource, (CFURLRef A, SInt32 *B), (A, B), if(B) *B = kCFURLImproperArgumentsError, false) + +#elif DEPLOYMENT_TARGET_WINDOWS +#else +#error Unknown or unspecified DEPLOYMENT_TARGET #endif +typedef struct __NSString__ *NSString; + +/* + Pre-10.6 property keys +*/ CONST_STRING_DECL(kCFURLFileExists, "kCFURLFileExists") CONST_STRING_DECL(kCFURLFilePOSIXMode, "kCFURLFilePOSIXMode") @@ -131,7 +152,13 @@ static CFDictionaryRef _CFFileURLCreatePropertiesFromResource(CFAllocatorRef all } if (_CFGetFileProperties(alloc, url, &exists, &posixMode, &size, modTimePtr, &ownerID, contentsPtr) != 0) { - if (errorCode) { + + // If all they've asked for is whether this file exists, then any error will just make + // this return kCFURLFileExists = kCFBooleanFalse, which handles the case where the filename is invalid or too long or something. + if ( arrayRange.length == 1 && CFArrayContainsValue( desiredProperties, arrayRange, kCFURLFileExists ) ) { + CFDictionarySetValue(propertyDict, kCFURLFileExists, kCFBooleanFalse); + } + else if (errorCode) { *errorCode = kCFURLUnknownError; } return propertyDict; @@ -224,8 +251,12 @@ static Boolean _CFFileURLWritePropertiesToResource(CFURLRef url, CFDictionaryRef CFNumberRef modeNum = (CFNumberRef)value; CFNumberGetValue(modeNum, kCFNumberSInt32Type, &mode); } else { -#if DEPLOYMENT_TARGET_MACOSX +#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED #define MODE_TYPE mode_t +#elif DEPLOYMENT_TARGET_WINDOWS +#define MODE_TYPE uint16_t +#else +#error Unknown or unspecified DEPLOYMENT_TARGET #endif const MODE_TYPE *modePtr = (const MODE_TYPE *)CFDataGetBytePtr((CFDataRef)value); mode = *modePtr; @@ -273,9 +304,15 @@ static Boolean _CFFileURLCreateDataAndPropertiesFromResource(CFAllocatorRef allo if (fetchedProperties) { *fetchedProperties = _CFFileURLCreatePropertiesFromResource(alloc, url, desiredProperties, errorCode); - if (!*fetchedProperties) success = false; + if (!*fetchedProperties) + success = false; } + if ( ! success && fetchedData && *fetchedData ) { + CFRelease( *fetchedData ); + *fetchedData = NULL; + } + return success; } @@ -518,15 +555,20 @@ done: static inline CFStringRef percentExpandAndTrimContentType(CFAllocatorRef alloc, CFStringRef str, CFRange range) { - CFStringRef contentTypeUnexpanded = CFStringCreateWithSubstring(alloc, str, range); - CFStringRef contentTypeExpanded = CFURLCreateStringByReplacingPercentEscapes(alloc, contentTypeUnexpanded, CFSTR("")); - CFRelease(contentTypeUnexpanded); - - CFMutableStringRef contentTypeHeader = CFStringCreateMutableCopy(alloc, 0, contentTypeExpanded); + CFMutableStringRef contentTypeHeader = NULL; + CFStringRef contentTypeUnexpanded = CFStringCreateWithSubstring(alloc, str, range); + CFStringRef contentTypeExpanded = CFURLCreateStringByReplacingPercentEscapes(alloc, contentTypeUnexpanded, CFSTR("")); + + if (NULL == contentTypeExpanded) { + contentTypeHeader = CFStringCreateMutableCopy(alloc, 0, contentTypeUnexpanded); + } else { + contentTypeHeader = CFStringCreateMutableCopy(alloc, 0, contentTypeExpanded); CFRelease(contentTypeExpanded); - CFStringTrimWhitespace(contentTypeHeader); - - return contentTypeHeader; + } + CFRelease(contentTypeUnexpanded); + CFStringTrimWhitespace(contentTypeHeader); + + return contentTypeHeader; } static Boolean parseDataRequestURL(CFURLRef url, CFDataRef* outData, CFStringRef* outMimeType, CFStringRef* outTextEncodingName) @@ -548,10 +590,6 @@ static Boolean parseDataRequestURL(CFURLRef url, CFDataRef* outData, CFStringRef mimeType = (CFStringRef) CFRetain(CFSTR("text/plain")); } - if (textEncodingName == NULL) { - textEncodingName = (CFStringRef) CFRetain(CFSTR("us-ascii")); - } - CFIndex bufferSize = CFURLGetBytes(url, NULL, 0); UInt8* srcBuffer = (UInt8*) malloc(bufferSize); CFURLGetBytes(url, srcBuffer, bufferSize); @@ -690,15 +728,17 @@ Boolean CFURLCreateDataAndPropertiesFromResource(CFAllocatorRef alloc, CFURLRef if (CFStringCompare(scheme, CFSTR("file"), kCFCompareCaseInsensitive) == kCFCompareEqualTo) { result = _CFFileURLCreateDataAndPropertiesFromResource(alloc, url, fetchedData, desiredProperties, fetchedProperties, errorCode); } else if (CFStringCompare(scheme, CFSTR("data"), kCFCompareCaseInsensitive) == kCFCompareEqualTo) { - result = _CFDataURLCreateDataAndPropertiesFromResource(alloc, url, fetchedData, desiredProperties, fetchedProperties, errorCode); - } else { -#if DEPLOYMENT_TARGET_MACOSX + result = _CFDataURLCreateDataAndPropertiesFromResource(alloc, url, fetchedData, desiredProperties, fetchedProperties, errorCode); + } else { +#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED result = __CFNetwork__CFURLCreateDataAndPropertiesFromResource(alloc, url, fetchedData, fetchedProperties, desiredProperties, errorCode); - if (!result) { - if (fetchedData) *fetchedData = NULL; - if (fetchedProperties) *fetchedProperties = NULL; - if (errorCode) *errorCode = kCFURLUnknownSchemeError; - } +#elif DEPLOYMENT_TARGET_WINDOWS + if (fetchedData) *fetchedData = NULL; + if (fetchedProperties) *fetchedProperties = NULL; + if (errorCode) *errorCode = kCFURLUnknownSchemeError; + result = false; +#else +#error Unknown or unspecified DEPLOYMENT_TARGET #endif } CFRelease(scheme); @@ -735,12 +775,22 @@ Boolean CFURLWriteDataAndPropertiesToResource(CFURLRef url, CFDataRef data, CFDi if (data) { if (CFURLHasDirectoryPath(url)) { // Create a directory +#if DEPLOYMENT_TARGET_WINDOWS + wchar_t cPath[CFMaxPathSize]; + if (!_CFURLGetWideFileSystemRepresentation(url, true, cPath, CFMaxPathSize)) +#else char cPath[CFMaxPathSize]; - if (!CFURLGetFileSystemRepresentation(url, true, (unsigned char *)cPath, CFMaxPathSize)) { + if (!CFURLGetFileSystemRepresentation(url, true, (unsigned char *)cPath, CFMaxPathSize)) +#endif + { if (errorCode) *errorCode = kCFURLImproperArgumentsError; success = false; } else { +#if DEPLOYMENT_TARGET_WINDOWS + success = _CFCreateDirectoryWide(cPath); +#else success = _CFCreateDirectory(cPath); +#endif if (!success && errorCode) *errorCode = kCFURLUnknownError; } } else { @@ -758,12 +808,17 @@ Boolean CFURLWriteDataAndPropertiesToResource(CFURLRef url, CFDataRef data, CFDi return success; } else { CFRelease(scheme); -#if DEPLOYMENT_TARGET_MACOSX +#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED Boolean result = __CFNetwork__CFURLWriteDataAndPropertiesToResource(url, data, propertyDict, errorCode); if (!result) { if (errorCode) *errorCode = kCFURLUnknownSchemeError; } return result; +#elif DEPLOYMENT_TARGET_WINDOWS + if (errorCode) *errorCode = kCFURLUnknownSchemeError; + return false; +#else +#error Unknown or unspecified DEPLOYMENT_TARGET #endif } } @@ -801,13 +856,19 @@ Boolean CFURLDestroyResource(CFURLRef url, SInt32 *errorCode) { } } else { CFRelease(scheme); -#if DEPLOYMENT_TARGET_MACOSX +#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED Boolean result = __CFNetwork__CFURLDestroyResource(url, errorCode); if (!result) { if (errorCode) *errorCode = kCFURLUnknownSchemeError; } return result; +#elif DEPLOYMENT_TARGET_WINDOWS + if (errorCode) *errorCode = kCFURLUnknownSchemeError; + return false; +#else +#error Unknown or unspecified DEPLOYMENT_TARGET #endif } } +