]> git.saurik.com Git - apple/cf.git/blob - Base.subproj/ForFoundationOnly.h
CF-299.tar.gz
[apple/cf.git] / Base.subproj / ForFoundationOnly.h
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* ForFoundationOnly.h
26 Copyright (c) 1998-2003, Apple, Inc. All rights reserved.
27 */
28
29 #if !CF_BUILDING_CF && !NSBUILDINGFOUNDATION
30 #error The header file ForFoundationOnly.h is for the exclusive use of the
31 #error CoreFoundation and Foundation projects. No other project should include it.
32 #endif
33
34 #if !defined(__COREFOUNDATION_FORFOUNDATIONONLY__)
35 #define __COREFOUNDATION_FORFOUNDATIONONLY__ 1
36
37 #include <CoreFoundation/CFBase.h>
38 #include <CoreFoundation/CFString.h>
39 #include <CoreFoundation/CFArray.h>
40 #include <CoreFoundation/CFDictionary.h>
41 #include <CoreFoundation/CFPriv.h>
42
43 // NOTE: miscellaneous declarations are at the end
44
45
46 // ---- CFBundle material ----------------------------------------
47
48 #include "CFBundlePriv.h"
49
50 #if defined(__cplusplus)
51 extern "C" {
52 #endif
53
54 CF_EXPORT const CFStringRef _kCFBundleExecutablePathKey;
55 CF_EXPORT const CFStringRef _kCFBundleInfoPlistURLKey;
56 CF_EXPORT const CFStringRef _kCFBundleNumericVersionKey;
57 CF_EXPORT const CFStringRef _kCFBundleResourcesFileMappedKey;
58 CF_EXPORT const CFStringRef _kCFBundleCFMLoadAsBundleKey;
59 CF_EXPORT const CFStringRef _kCFBundleAllowMixedLocalizationsKey;
60
61
62 CF_EXPORT CFArrayRef _CFBundleCopyLanguageSearchListInDirectory(CFAllocatorRef alloc, CFURLRef url, UInt8 *version);
63 CF_EXPORT CFArrayRef _CFBundleGetLanguageSearchList(CFBundleRef bundle);
64
65 #if defined(__cplusplus)
66 }
67 #endif
68
69
70 // ---- CFString material ----------------------------------------
71
72 #if defined(__cplusplus)
73 extern "C" {
74 #endif
75
76 /* Create a byte stream from a CFString backing. Can convert a string piece at a
77 time into a fixed size buffer. Returns number of characters converted.
78 Characters that cannot be converted to the specified encoding are represented
79 with the char specified by lossByte; if 0, then lossy conversion is not allowed
80 and conversion stops, returning partial results.
81 generatingExternalFile indicates that any extra stuff to allow this data to be
82 persistent (for instance, BOM) should be included.
83 Pass buffer==NULL if you don't care about the converted string (but just the
84 convertability, or number of bytes required, indicated by usedBufLen).
85 Does not zero-terminate. If you want to create Pascal or C string, allow one
86 extra byte at start or end.
87 */
88 CF_EXPORT CFIndex __CFStringEncodeByteStream(CFStringRef string, CFIndex rangeLoc, CFIndex rangeLen, Boolean generatingExternalFile, CFStringEncoding encoding, char lossByte, UInt8 *buffer, CFIndex max, CFIndex *usedBufLen);
89
90 CF_INLINE Boolean __CFStringEncodingIsSupersetOfASCII(CFStringEncoding encoding) {
91 switch (encoding & 0x0000FF00) {
92 case 0x0: // MacOS Script range
93 return true;
94
95 case 0x100: // Unicode range
96 if (encoding == kCFStringEncodingUnicode) return false;
97 return true;
98
99 case 0x200: // ISO range
100 return true;
101
102 case 0x600: // National standards range
103 if (encoding != kCFStringEncodingASCII) return false;
104 return true;
105
106 case 0x800: // ISO 2022 range
107 return false; // It's modal encoding
108
109 case 0xA00: // Misc standard range
110 return true;
111
112 case 0xB00:
113 if (encoding == kCFStringEncodingNonLossyASCII) return false;
114 return true;
115
116 case 0xC00: // EBCDIC
117 return false;
118
119 default:
120 return ((encoding & 0x0000FF00) > 0x0C00 ? false : true);
121 }
122 }
123
124 /* Desperately using extern here */
125 CF_EXPORT CFStringEncoding __CFDefaultEightBitStringEncoding;
126 CF_EXPORT CFStringEncoding __CFStringComputeEightBitStringEncoding(void);
127
128 CF_INLINE CFStringEncoding __CFStringGetEightBitStringEncoding(void) {
129 if (__CFDefaultEightBitStringEncoding == kCFStringEncodingInvalidId) __CFStringComputeEightBitStringEncoding();
130 return __CFDefaultEightBitStringEncoding;
131 }
132
133 enum {
134 __kCFVarWidthLocalBufferSize = 1008
135 };
136
137 typedef struct { /* A simple struct to maintain ASCII/Unicode versions of the same buffer. */
138 union {
139 UInt8 *ascii;
140 UniChar *unicode;
141 } chars;
142 Boolean isASCII; /* This really does mean 7-bit ASCII, not _NSDefaultCStringEncoding() */
143 Boolean shouldFreeChars; /* If the number of bytes exceeds __kCFVarWidthLocalBufferSize, bytes are allocated */
144 Boolean _unused1;
145 Boolean _unused2;
146 CFAllocatorRef allocator; /* Use this allocator to allocate, reallocate, and deallocate the bytes */
147 UInt32 numChars; /* This is in terms of ascii or unicode; that is, if isASCII, it is number of 7-bit chars; otherwise it is number of UniChars; note that the actual allocated space might be larger */
148 UInt8 localBuffer[__kCFVarWidthLocalBufferSize]; /* private; 168 ISO2022JP chars, 504 Unicode chars, 1008 ASCII chars */
149 } CFVarWidthCharBuffer;
150
151
152 /* Convert a byte stream to ASCII (7-bit!) or Unicode, with a CFVarWidthCharBuffer struct on the stack. false return indicates an error occured during the conversion. Depending on .isASCII, follow .chars.ascii or .chars.unicode. If .shouldFreeChars is returned as true, free the returned buffer when done with it. If useClientsMemoryPtr is provided as non-NULL, and the provided memory can be used as is, this is set to true, and the .ascii or .unicode buffer in CFVarWidthCharBuffer is set to bytes.
153 !!! If the stream is Unicode and has no BOM, the data is assumed to be big endian! Could be trouble on Intel if someone didn't follow that assumption.
154 !!! __CFStringDecodeByteStream2() needs to be deprecated and removed post-Jaguar.
155 */
156 CF_EXPORT Boolean __CFStringDecodeByteStream2(const UInt8 *bytes, UInt32 len, CFStringEncoding encoding, Boolean alwaysUnicode, CFVarWidthCharBuffer *buffer, Boolean *useClientsMemoryPtr);
157 CF_EXPORT Boolean __CFStringDecodeByteStream3(const UInt8 *bytes, UInt32 len, CFStringEncoding encoding, Boolean alwaysUnicode, CFVarWidthCharBuffer *buffer, Boolean *useClientsMemoryPtr, UInt32 converterFlags);
158
159
160 /* Convert single byte to Unicode; assumes one-to-one correspondence (that is, can only be used with 1-byte encodings). You can use the function if it's not NULL. The table is always safe to use; calling __CFSetCharToUniCharFunc() updates it.
161 */
162 CF_EXPORT Boolean (*__CFCharToUniCharFunc)(UInt32 flags, UInt8 ch, UniChar *unicodeChar);
163 CF_EXPORT void __CFSetCharToUniCharFunc(Boolean (*func)(UInt32 flags, UInt8 ch, UniChar *unicodeChar));
164 CF_EXPORT UniChar __CFCharToUniCharTable[256];
165
166 /* Character class functions UnicodeData-2_1_5.txt
167 */
168 CF_INLINE Boolean __CFIsWhitespace(UniChar theChar) {
169 return ((theChar < 0x21) || (theChar > 0x7E && theChar < 0xA1) || (theChar >= 0x2000 && theChar <= 0x200B) || (theChar == 0x3000)) ? true : false;
170 }
171
172 /* Same as CFStringGetCharacterFromInlineBuffer() but returns 0xFFFF on out of bounds access
173 */
174 CF_INLINE UniChar __CFStringGetCharacterFromInlineBufferAux(CFStringInlineBuffer *buf, CFIndex idx) {
175 if (buf->directBuffer) {
176 if (idx < 0 || idx >= buf->rangeToBuffer.length) return 0xFFFF;
177 return buf->directBuffer[idx + buf->rangeToBuffer.location];
178 }
179 if (idx >= buf->bufferedRangeEnd || idx < buf->bufferedRangeStart) {
180 if (idx < 0 || idx >= buf->rangeToBuffer.length) return 0xFFFF;
181 if ((buf->bufferedRangeStart = idx - 4) < 0) buf->bufferedRangeStart = 0;
182 buf->bufferedRangeEnd = buf->bufferedRangeStart + __kCFStringInlineBufferLength;
183 if (buf->bufferedRangeEnd > buf->rangeToBuffer.length) buf->bufferedRangeEnd = buf->rangeToBuffer.length;
184 CFStringGetCharacters(buf->theString, CFRangeMake(buf->rangeToBuffer.location + buf->bufferedRangeStart, buf->bufferedRangeEnd - buf->bufferedRangeStart), buf->buffer);
185 }
186 return buf->buffer[idx - buf->bufferedRangeStart];
187 }
188
189 /* Same as CFStringGetCharacterFromInlineBuffer(), but without the bounds checking (will return garbage or crash)
190 */
191 CF_INLINE UniChar __CFStringGetCharacterFromInlineBufferQuick(CFStringInlineBuffer *buf, CFIndex idx) {
192 if (buf->directBuffer) return buf->directBuffer[idx + buf->rangeToBuffer.location];
193 if (idx >= buf->bufferedRangeEnd || idx < buf->bufferedRangeStart) {
194 if ((buf->bufferedRangeStart = idx - 4) < 0) buf->bufferedRangeStart = 0;
195 buf->bufferedRangeEnd = buf->bufferedRangeStart + __kCFStringInlineBufferLength;
196 if (buf->bufferedRangeEnd > buf->rangeToBuffer.length) buf->bufferedRangeEnd = buf->rangeToBuffer.length;
197 CFStringGetCharacters(buf->theString, CFRangeMake(buf->rangeToBuffer.location + buf->bufferedRangeStart, buf->bufferedRangeEnd - buf->bufferedRangeStart), buf->buffer);
198 }
199 return buf->buffer[idx - buf->bufferedRangeStart];
200 }
201
202
203 /* These two allow specifying an alternate description function (instead of CFCopyDescription); used by NSString
204 */
205 CF_EXPORT void _CFStringAppendFormatAndArgumentsAux(CFMutableStringRef outputString, CFStringRef (*copyDescFunc)(void *, CFDictionaryRef), CFDictionaryRef formatOptions, CFStringRef formatString, va_list args);
206 CF_EXPORT CFStringRef _CFStringCreateWithFormatAndArgumentsAux(CFAllocatorRef alloc, CFStringRef (*copyDescFunc)(void *, CFDictionaryRef), CFDictionaryRef formatOptions, CFStringRef format, va_list arguments);
207
208 enum {_CFStringErrNone = 0, _CFStringErrNotMutable = 1, _CFStringErrNilArg = 2, _CFStringErrBounds = 3};
209
210 CF_EXPORT Boolean __CFStringNoteErrors(void); // Should string errors raise?
211
212 #if defined(__cplusplus)
213 }
214 #endif
215
216
217 // ---- Binary plist material ----------------------------------------
218
219 typedef const struct __CFKeyedArchiverUID * CFKeyedArchiverUIDRef;
220 extern CFTypeID _CFKeyedArchiverUIDGetTypeID(void);
221 extern CFKeyedArchiverUIDRef _CFKeyedArchiverUIDCreate(CFAllocatorRef allocator, uint32_t value);
222 extern uint32_t _CFKeyedArchiverUIDGetValue(CFKeyedArchiverUIDRef uid);
223
224
225 enum {
226 kCFBinaryPlistMarkerNull = 0x00,
227 kCFBinaryPlistMarkerFalse = 0x08,
228 kCFBinaryPlistMarkerTrue = 0x09,
229 kCFBinaryPlistMarkerFill = 0x0F,
230 kCFBinaryPlistMarkerInt = 0x10,
231 kCFBinaryPlistMarkerReal = 0x20,
232 kCFBinaryPlistMarkerDate = 0x33,
233 kCFBinaryPlistMarkerData = 0x40,
234 kCFBinaryPlistMarkerASCIIString = 0x50,
235 kCFBinaryPlistMarkerUnicode16String = 0x60,
236 kCFBinaryPlistMarkerUID = 0x80,
237 kCFBinaryPlistMarkerArray = 0xA0,
238 kCFBinaryPlistMarkerDict = 0xD0
239 };
240
241 typedef struct {
242 uint8_t _magic[6];
243 uint8_t _version[2];
244 } CFBinaryPlistHeader;
245
246 typedef struct {
247 uint8_t _unused[6];
248 uint8_t _offsetIntSize;
249 uint8_t _objectRefSize;
250 uint64_t _numObjects;
251 uint64_t _topObject;
252 uint64_t _offsetTableOffset;
253 } CFBinaryPlistTrailer;
254
255
256 // ---- Miscellaneous material ----------------------------------------
257
258 #include <CoreFoundation/CFBag.h>
259 #include <CoreFoundation/CFSet.h>
260 #include <math.h>
261
262 #if defined(__cplusplus)
263 extern "C" {
264 #endif
265
266 CF_EXPORT CFTypeID CFTypeGetTypeID(void);
267
268 CF_EXPORT void _CFArraySetCapacity(CFMutableArrayRef array, CFIndex cap);
269 CF_EXPORT void _CFBagSetCapacity(CFMutableBagRef bag, CFIndex cap);
270 CF_EXPORT void _CFDictionarySetCapacity(CFMutableDictionaryRef dict, CFIndex cap);
271 CF_EXPORT void _CFSetSetCapacity(CFMutableSetRef set, CFIndex cap);
272
273 CF_EXPORT void _CFArrayReplaceValues(CFMutableArrayRef array, CFRange range, const void **newValues, CFIndex newCount);
274
275
276 /* For use by NSNumber and CFNumber.
277 Hashing algorithm for CFNumber:
278 M = Max CFHashCode (assumed to be unsigned)
279 For positive integral values: N mod M
280 For negative integral values: (-N) mod M
281 For floating point numbers that are not integral: hash(integral part) + hash(float part * M)
282 */
283 CF_INLINE CFHashCode _CFHashInt(int i) {
284 return (i > 0) ? (CFHashCode)(i) : (CFHashCode)(-i);
285 }
286
287 CF_INLINE CFHashCode _CFHashDouble(double d) {
288 double dInt;
289 if (d < 0) d = -d;
290 dInt = rint(d);
291 return (CFHashCode)(fmod(dInt, (double)0xFFFFFFFF) + ((d - dInt) * 0xFFFFFFFF));
292 }
293
294
295 typedef void (*CFRunLoopPerformCallBack)(void *info);
296
297
298 #if defined(__MACH__)
299 #include <mach/mach_time.h>
300 CF_INLINE UInt64 __CFReadTSR(void) {
301 return mach_absolute_time();
302 }
303 #else
304 CF_INLINE UInt64 __CFReadTSR(void) {
305 union {
306 UInt64 time64;
307 UInt32 word[2];
308 } now;
309 #if defined(__i386__)
310 /* Read from Pentium and Pentium Pro 64-bit timestamp counter. */
311 /* The counter is set to 0 at processor reset and increments on */
312 /* every clock cycle. */
313 __asm__ volatile("rdtsc" : : : "eax", "edx");
314 __asm__ volatile("movl %%eax,%0" : "=m" (now.word[0]) : : "eax");
315 __asm__ volatile("movl %%edx,%0" : "=m" (now.word[1]) : : "edx");
316 #elif defined(__ppc__)
317 /* Read from PowerPC 64-bit time base register. The increment */
318 /* rate of the time base is implementation-dependent, but is */
319 /* 1/4th the bus clock cycle on 603/604/750 processors. */
320 UInt32 t3;
321 do {
322 __asm__ volatile("mftbu %0" : "=r" (now.word[0]));
323 __asm__ volatile("mftb %0" : "=r" (now.word[1]));
324 __asm__ volatile("mftbu %0" : "=r" (t3));
325 } while (now.word[0] != t3);
326 #else
327 // ??? Do not know how to read a time stamp register on this architecture
328 now.time64 = (uint64_t)0;
329 #endif
330 return now.time64;
331 }
332 #endif
333
334 #if defined(__cplusplus)
335 }
336 #endif
337
338 #endif /* ! __COREFOUNDATION_FORFOUNDATIONONLY__ */
339