]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/utilities.cpp
Security-58286.200.222.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / utilities.cpp
1 /*
2 * Copyright (c) 2000-2004,2011,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // Utilities
27 //
28 #include <security_utilities/utilities.h>
29 #include <utilities/SecCFRelease.h>
30 #include <CoreFoundation/CoreFoundation.h>
31 #include <dispatch/dispatch.h>
32
33 namespace Security
34 {
35
36 static CFMutableDictionaryRef gCacheDictionaryRef = NULL;
37 static dispatch_once_t gDictionaryCreated = 0;
38 static dispatch_queue_t gSerializeQueue;
39
40 char *cached_realpath(const char * file_name, char * resolved_name)
41 {
42 dispatch_once(&gDictionaryCreated,
43 ^{
44 gCacheDictionaryRef = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
45 gSerializeQueue = dispatch_queue_create("com.apple.SecurityLookupCacheQueue", DISPATCH_QUEUE_SERIAL);
46 });
47
48
49 __block char* valueToReturn = NULL;
50
51 dispatch_sync(gSerializeQueue,
52 ^{
53 // Put a maximum limit on the size of this cache.
54 CFIndex entryCount = CFDictionaryGetCount(gCacheDictionaryRef);
55
56 // make the incoming name a string
57 CFStringRef input = CFStringCreateWithCString(NULL, file_name, kCFStringEncodingUTF8);
58 if (entryCount < OPEN_MAX && input != NULL) // is it acceptable to use the cache?
59 {
60 // see if we can find that string in our dictionary
61 CFStringRef output = (CFStringRef) CFDictionaryGetValue(gCacheDictionaryRef, input);
62
63 if (output == NULL)
64 {
65 // the string is not in our cache, so use realpath
66 valueToReturn = realpath(file_name, resolved_name);
67 if (valueToReturn != NULL) // no error, so continue
68 {
69 // make a new entry in the dictionary for our string
70 output = CFStringCreateWithCString(NULL, valueToReturn, kCFStringEncodingUTF8);
71 CFDictionaryAddValue(gCacheDictionaryRef, input, output);
72 CFRelease(output);
73 }
74 }
75 else
76 {
77 char* valueToFree = NULL;
78
79 // we need to extract the value from the output
80
81 // figure out how big to make our buffer
82 CFIndex size = CFStringGetMaximumSizeForEncoding(CFStringGetLength(output), kCFStringEncodingUTF8) + 1; // account for NULL termination
83
84 // if the user has passed in a buffer, use it. If not, allocate our own
85
86 // technically, we don't know the size of the buffer that the
87 // user has passed in. It has to be large enough to hold the
88 // string, however, so we can use size as an estimator. The
89 // result will be the same, however: If the user didn't supply
90 // enough memory, he will crash. We behave exactly the same
91 // as realpath, which is the idea.
92 char *buffer = resolved_name;
93
94 // allocate a buffer if none was passed in
95 if (buffer == NULL) {
96 valueToFree = buffer = (char*) malloc(size);
97 }
98
99 if (buffer != NULL) // check to see if malloc failed earlier
100 {
101 if (!CFStringGetCString(output, buffer, size, kCFStringEncodingUTF8))
102 {
103 free((void*) valueToFree);
104 valueToReturn = NULL;
105 }
106 else
107 {
108 valueToReturn = buffer;
109 }
110 }
111 }
112 }
113 else
114 {
115 valueToReturn = realpath(file_name, resolved_name);
116 }
117
118 CFReleaseSafe(input);
119 });
120
121 return valueToReturn;
122 }
123
124 }
125
126