2 * Copyright (c) 2012 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 Copyright (c) 2001-2012, Apple Inc. All rights reserved.
26 Responsibility: Aki Inoue
29 #include <CoreFoundation/CFByteOrder.h>
30 #include "CFInternal.h"
31 #include "CFUniChar.h"
32 #include "CFStringEncodingConverterExt.h"
33 #include "CFUnicodeDecomposition.h"
34 #include "CFUniCharPriv.h"
35 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
37 #include <sys/types.h>
39 #include <sys/param.h>
44 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
45 #include <mach/mach.h>
48 #if DEPLOYMENT_TARGET_WINDOWS
49 extern void _CFGetFrameworkPath(wchar_t *path
, int maxLength
);
52 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
53 #define __kCFCharacterSetDir "/System/Library/CoreServices"
54 #elif DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD || DEPLOYMENT_TARGET_EMBEDDED_MINI
55 #define __kCFCharacterSetDir "/usr/local/share/CoreFoundation"
56 #elif DEPLOYMENT_TARGET_WINDOWS
57 #define __kCFCharacterSetDir "\\Windows\\CoreFoundation"
60 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
61 #define USE_MACHO_SEGMENT 1
65 kCFUniCharLastExternalSet
= kCFUniCharNewlineCharacterSet
,
66 kCFUniCharFirstInternalSet
= kCFUniCharCompatibilityDecomposableCharacterSet
,
67 kCFUniCharLastInternalSet
= kCFUniCharGraphemeExtendCharacterSet
,
68 kCFUniCharFirstBitmapSet
= kCFUniCharDecimalDigitCharacterSet
71 CF_INLINE
uint32_t __CFUniCharMapExternalSetToInternalIndex(uint32_t cset
) { return ((kCFUniCharFirstInternalSet
<= cset
) ? ((cset
- kCFUniCharFirstInternalSet
) + kCFUniCharLastExternalSet
) : cset
) - kCFUniCharFirstBitmapSet
; }
72 CF_INLINE
uint32_t __CFUniCharMapCompatibilitySetID(uint32_t cset
) { return ((cset
== kCFUniCharControlCharacterSet
) ? kCFUniCharControlAndFormatterCharacterSet
: (((cset
> kCFUniCharLastExternalSet
) && (cset
< kCFUniCharFirstInternalSet
)) ? ((cset
- kCFUniCharLastExternalSet
) + kCFUniCharFirstInternalSet
) : cset
)); }
74 #if (DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED) && USE_MACHO_SEGMENT
75 #include <mach-o/getsect.h>
76 #include <mach-o/dyld.h>
77 #include <mach-o/ldsyms.h>
79 static const void *__CFGetSectDataPtr(const char *segname
, const char *sectname
, uint64_t *sizep
) {
80 uint32_t idx
, cnt
= _dyld_image_count();
81 for (idx
= 0; idx
< cnt
; idx
++) {
82 void *mh
= (void *)_dyld_get_image_header(idx
);
83 if (mh
!= &_mh_dylib_header
) continue;
85 const struct section_64
*sect
= getsectbynamefromheader_64((struct mach_header_64
*)mh
, segname
, sectname
);
87 const struct section
*sect
= getsectbynamefromheader((struct mach_header
*)mh
, segname
, sectname
);
90 if (sizep
) *sizep
= (uint64_t)sect
->size
;
91 return (char *)sect
->addr
+ _dyld_get_image_vmaddr_slide(idx
);
93 if (sizep
) *sizep
= 0ULL;
98 #if !USE_MACHO_SEGMENT
100 // Memory map the file
102 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX
103 CF_INLINE
void __CFUniCharCharacterSetPath(char *cpath
) {
104 #elif DEPLOYMENT_TARGET_WINDOWS
105 CF_INLINE
void __CFUniCharCharacterSetPath(wchar_t *wpath
) {
107 #error Unknown or unspecified DEPLOYMENT_TARGET
109 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
110 strlcpy(cpath
, __kCFCharacterSetDir
, MAXPATHLEN
);
111 #elif DEPLOYMENT_TARGET_LINUX
112 strlcpy(cpath
, __kCFCharacterSetDir
, MAXPATHLEN
);
113 #elif DEPLOYMENT_TARGET_WINDOWS
114 wchar_t frameworkPath
[MAXPATHLEN
];
115 _CFGetFrameworkPath(frameworkPath
, MAXPATHLEN
);
116 wcsncpy(wpath
, frameworkPath
, MAXPATHLEN
);
117 wcsncat(wpath
, L
"\\CoreFoundation.resources\\", MAXPATHLEN
- wcslen(wpath
));
119 strlcpy(cpath
, __kCFCharacterSetDir
, MAXPATHLEN
);
120 strlcat(cpath
, "/CharacterSets/", MAXPATHLEN
);
124 #if DEPLOYMENT_TARGET_WINDOWS
125 #define MAX_BITMAP_STATE 512
127 // If a string is placed into this array, then it has been previously
128 // determined that the bitmap-file cannot be found. Thus, we make
129 // the assumption it won't be there in future calls and we avoid
130 // hitting the disk un-necessarily. This assumption isn't 100%
131 // correct, as bitmap-files can be added. We would have to re-start
132 // the application in order to pick-up the new bitmap info.
134 // We should probably re-visit this.
136 static wchar_t *mappedBitmapState
[MAX_BITMAP_STATE
];
137 static int __nNumStateEntries
= -1;
138 CRITICAL_SECTION __bitmapStateLock
= {0};
140 bool __GetBitmapStateForName(const wchar_t *bitmapName
) {
141 if (NULL
== __bitmapStateLock
.DebugInfo
)
142 InitializeCriticalSection(&__bitmapStateLock
);
143 EnterCriticalSection(&__bitmapStateLock
);
144 if (__nNumStateEntries
>= 0) {
145 for (int i
= 0; i
< __nNumStateEntries
; i
++) {
146 if (wcscmp(mappedBitmapState
[i
], bitmapName
) == 0) {
147 LeaveCriticalSection(&__bitmapStateLock
);
152 LeaveCriticalSection(&__bitmapStateLock
);
155 void __AddBitmapStateForName(const wchar_t *bitmapName
) {
156 if (NULL
== __bitmapStateLock
.DebugInfo
)
157 InitializeCriticalSection(&__bitmapStateLock
);
158 EnterCriticalSection(&__bitmapStateLock
);
159 __nNumStateEntries
++;
160 mappedBitmapState
[__nNumStateEntries
] = (wchar_t *)malloc((lstrlenW(bitmapName
)+1) * sizeof(wchar_t));
161 lstrcpyW(mappedBitmapState
[__nNumStateEntries
], bitmapName
);
162 LeaveCriticalSection(&__bitmapStateLock
);
166 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX
167 static bool __CFUniCharLoadBytesFromFile(const char *fileName
, const void **bytes
, int64_t *fileSize
) {
168 #elif DEPLOYMENT_TARGET_WINDOWS
169 static bool __CFUniCharLoadBytesFromFile(const wchar_t *fileName
, const void **bytes
, int64_t *fileSize
) {
171 #error Unknown or unspecified DEPLOYMENT_TARGET
173 #if DEPLOYMENT_TARGET_WINDOWS
174 HANDLE bitmapFileHandle
= NULL
;
175 HANDLE mappingHandle
= NULL
;
177 if (__GetBitmapStateForName(fileName
)) {
178 // The fileName has been tried in the past, so just return false
183 mappingHandle
= OpenFileMappingW(FILE_MAP_READ
, TRUE
, fileName
);
184 if (NULL
== mappingHandle
) {
185 if ((bitmapFileHandle
= CreateFileW(fileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
)) == INVALID_HANDLE_VALUE
) {
186 // We tried to get the bitmap file for mapping, but it's not there. Add to list of non-existant bitmap-files so
187 // we don't have to try this again in the future.
188 __AddBitmapStateForName(fileName
);
191 mappingHandle
= CreateFileMapping(bitmapFileHandle
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
192 CloseHandle(bitmapFileHandle
);
193 if (!mappingHandle
) return false;
196 *bytes
= MapViewOfFileEx(mappingHandle
, FILE_MAP_READ
, 0, 0, 0, 0);
198 if (NULL
!= fileSize
) {
199 MEMORY_BASIC_INFORMATION memoryInfo
;
201 if (0 == VirtualQueryEx(mappingHandle
, *bytes
, &memoryInfo
, sizeof(memoryInfo
))) {
202 *fileSize
= 0; // This indicates no checking. Is it right ?
204 *fileSize
= memoryInfo
.RegionSize
;
208 CloseHandle(mappingHandle
);
210 return (*bytes
? true : false);
215 if ((fd
= open(fileName
, O_RDONLY
, 0)) < 0) {
218 if (fstat(fd
, &statBuf
) < 0 || (*bytes
= mmap(0, statBuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0)) == (void *)-1) {
224 if (NULL
!= fileSize
) *fileSize
= statBuf
.st_size
;
230 #endif // USE_MACHO_SEGMENT
232 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX
233 static bool __CFUniCharLoadFile(const char *bitmapName
, const void **bytes
, int64_t *fileSize
) {
234 #elif DEPLOYMENT_TARGET_WINDOWS
235 static bool __CFUniCharLoadFile(const wchar_t *bitmapName
, const void **bytes
, int64_t *fileSize
) {
237 #error Unknown or unspecified DEPLOYMENT_TARGET
239 #if USE_MACHO_SEGMENT
240 *bytes
= __CFGetSectDataPtr("__UNICODE", bitmapName
, NULL
);
242 if (NULL
!= fileSize
) *fileSize
= 0;
244 return *bytes
? true : false;
246 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX
247 char cpath
[MAXPATHLEN
];
248 __CFUniCharCharacterSetPath(cpath
);
249 strlcat(cpath
, bitmapName
, MAXPATHLEN
);
250 return __CFUniCharLoadBytesFromFile(cpath
, bytes
, fileSize
);
251 #elif DEPLOYMENT_TARGET_WINDOWS
252 wchar_t wpath
[MAXPATHLEN
];
253 __CFUniCharCharacterSetPath(wpath
);
254 wcsncat(wpath
, bitmapName
, MAXPATHLEN
);
255 return __CFUniCharLoadBytesFromFile(wpath
, bytes
, fileSize
);
257 #error Unknown or unspecified DEPLOYMENT_TARGET
263 CF_INLINE
bool isControl(UTF32Char theChar
, uint16_t charset
, const void *data
) { // ISO Control
264 return (((theChar
<= 0x001F) || (theChar
>= 0x007F && theChar
<= 0x009F)) ? true : false);
267 CF_INLINE
bool isWhitespace(UTF32Char theChar
, uint16_t charset
, const void *data
) { // Space
268 return (((theChar
== 0x0020) || (theChar
== 0x0009) || (theChar
== 0x00A0) || (theChar
== 0x1680) || (theChar
>= 0x2000 && theChar
<= 0x200B) || (theChar
== 0x202F) || (theChar
== 0x205F) || (theChar
== 0x3000)) ? true : false);
271 CF_INLINE
bool isNewline(UTF32Char theChar
, uint16_t charset
, const void *data
) { // White space
272 return (((theChar
>= 0x000A && theChar
<= 0x000D) || (theChar
== 0x0085) || (theChar
== 0x2028) || (theChar
== 0x2029)) ? true : false);
275 CF_INLINE
bool isWhitespaceAndNewline(UTF32Char theChar
, uint16_t charset
, const void *data
) { // White space
276 return ((isWhitespace(theChar
, charset
, data
) || isNewline(theChar
, charset
, data
)) ? true : false);
279 #if USE_MACHO_SEGMENT
280 CF_INLINE
bool __CFSimpleFileSizeVerification(const void *bytes
, int64_t fileSize
) { return true; }
282 // <rdar://problem/8961744> __CFSimpleFileSizeVerification is broken
283 static bool __CFSimpleFileSizeVerification(const void *bytes
, int64_t fileSize
) { return true; }
285 static bool __CFSimpleFileSizeVerification(const void *bytes
, int64_t fileSize
) {
289 if ((sizeof(uint32_t) * 2) > fileSize
) {
292 uint32_t headerSize
= CFSwapInt32BigToHost(*((uint32_t *)((char *)bytes
+ 4)));
294 if ((headerSize
< (sizeof(uint32_t) * 4)) || (headerSize
> fileSize
)) {
297 const uint32_t *lastElement
= (uint32_t *)(((uint8_t *)bytes
) + headerSize
) - 2;
299 if ((headerSize
+ CFSwapInt32BigToHost(lastElement
[0]) + CFSwapInt32BigToHost(lastElement
[1])) > headerSize
) result
= false;
304 if (!result
) CFLog(kCFLogLevelCritical
, CFSTR("File size verification for Unicode database file failed."));
308 #endif // USE_MACHO_SEGMENT
312 const uint8_t **_planes
;
313 } __CFUniCharBitmapData
;
315 static char __CFUniCharUnicodeVersionString
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
317 static uint32_t __CFUniCharNumberOfBitmaps
= 0;
318 static __CFUniCharBitmapData
*__CFUniCharBitmapDataArray
= NULL
;
320 static CFSpinLock_t __CFUniCharBitmapLock
= CFSpinLockInit
;
322 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX
323 #if !defined(CF_UNICHAR_BITMAP_FILE)
324 #if USE_MACHO_SEGMENT
325 #define CF_UNICHAR_BITMAP_FILE "__csbitmaps"
327 #define CF_UNICHAR_BITMAP_FILE "/CFCharacterSetBitmaps.bitmap"
330 #elif DEPLOYMENT_TARGET_WINDOWS
331 #if !defined(CF_UNICHAR_BITMAP_FILE)
332 #define CF_UNICHAR_BITMAP_FILE L"CFCharacterSetBitmaps.bitmap"
335 #error Unknown or unspecified DEPLOYMENT_TARGET
338 static bool __CFUniCharLoadBitmapData(void) {
339 __CFUniCharBitmapData
*array
;
343 uint8_t currentPlane
;
345 const void *bitmapBase
;
347 int idx
, bitmapIndex
;
350 __CFSpinLock(&__CFUniCharBitmapLock
);
352 if (__CFUniCharBitmapDataArray
|| !__CFUniCharLoadFile(CF_UNICHAR_BITMAP_FILE
, &bytes
, &fileSize
) || !__CFSimpleFileSizeVerification(bytes
, fileSize
)) {
353 __CFSpinUnlock(&__CFUniCharBitmapLock
);
357 for (idx
= 0;idx
< 4 && ((const uint8_t *)bytes
)[idx
];idx
++) {
358 __CFUniCharUnicodeVersionString
[idx
* 2] = ((const uint8_t *)bytes
)[idx
];
359 __CFUniCharUnicodeVersionString
[idx
* 2 + 1] = '.';
361 __CFUniCharUnicodeVersionString
[(idx
< 4 ? idx
* 2 - 1 : 7)] = '\0';
363 headerSize
= CFSwapInt32BigToHost(*((uint32_t *)((char *)bytes
+ 4)));
365 bitmapBase
= (uint8_t *)bytes
+ headerSize
;
366 bytes
= (uint8_t *)bytes
+ (sizeof(uint32_t) * 2);
367 headerSize
-= (sizeof(uint32_t) * 2);
369 __CFUniCharNumberOfBitmaps
= headerSize
/ (sizeof(uint32_t) * 2);
371 array
= (__CFUniCharBitmapData
*)CFAllocatorAllocate(kCFAllocatorSystemDefault
, sizeof(__CFUniCharBitmapData
) * __CFUniCharNumberOfBitmaps
, 0);
373 for (idx
= 0;idx
< (int)__CFUniCharNumberOfBitmaps
;idx
++) {
374 bitmap
= (uint8_t *)bitmapBase
+ CFSwapInt32BigToHost(*((uint32_t *)bytes
)); bytes
= (uint8_t *)bytes
+ sizeof(uint32_t);
375 bitmapSize
= CFSwapInt32BigToHost(*((uint32_t *)bytes
)); bytes
= (uint8_t *)bytes
+ sizeof(uint32_t);
377 numPlanes
= bitmapSize
/ (8 * 1024);
378 numPlanes
= *(const uint8_t *)((char *)bitmap
+ (((numPlanes
- 1) * ((8 * 1024) + 1)) - 1)) + 1;
379 array
[idx
]._planes
= (const uint8_t **)CFAllocatorAllocate(kCFAllocatorSystemDefault
, sizeof(const void *) * numPlanes
, 0);
380 array
[idx
]._numPlanes
= numPlanes
;
383 for (bitmapIndex
= 0;bitmapIndex
< numPlanes
;bitmapIndex
++) {
384 if (bitmapIndex
== currentPlane
) {
385 array
[idx
]._planes
[bitmapIndex
] = (const uint8_t *)bitmap
;
386 bitmap
= (uint8_t *)bitmap
+ (8 * 1024);
387 #if defined (__cplusplus)
388 currentPlane
= *(((const uint8_t*&)bitmap
)++);
390 currentPlane
= *((const uint8_t *)bitmap
++);
394 array
[idx
]._planes
[bitmapIndex
] = NULL
;
399 __CFUniCharBitmapDataArray
= array
;
401 __CFSpinUnlock(&__CFUniCharBitmapLock
);
406 __private_extern__
const char *__CFUniCharGetUnicodeVersionString(void) {
407 if (NULL
== __CFUniCharBitmapDataArray
) __CFUniCharLoadBitmapData();
408 return __CFUniCharUnicodeVersionString
;
411 bool CFUniCharIsMemberOf(UTF32Char theChar
, uint32_t charset
) {
412 charset
= __CFUniCharMapCompatibilitySetID(charset
);
415 case kCFUniCharWhitespaceCharacterSet
:
416 return isWhitespace(theChar
, charset
, NULL
);
418 case kCFUniCharWhitespaceAndNewlineCharacterSet
:
419 return isWhitespaceAndNewline(theChar
, charset
, NULL
);
421 case kCFUniCharNewlineCharacterSet
:
422 return isNewline(theChar
, charset
, NULL
);
425 uint32_t tableIndex
= __CFUniCharMapExternalSetToInternalIndex(charset
);
427 if (NULL
== __CFUniCharBitmapDataArray
) __CFUniCharLoadBitmapData();
429 if (tableIndex
< __CFUniCharNumberOfBitmaps
) {
430 __CFUniCharBitmapData
*data
= __CFUniCharBitmapDataArray
+ tableIndex
;
431 uint8_t planeNo
= (theChar
>> 16) & 0xFF;
433 // The bitmap data for kCFUniCharIllegalCharacterSet is actually LEGAL set less Plane 14 ~ 16
434 if (charset
== kCFUniCharIllegalCharacterSet
) {
435 if (planeNo
== 0x0E) { // Plane 14
437 return (((theChar
== 0x01) || ((theChar
> 0x1F) && (theChar
< 0x80))) ? false : true);
438 } else if (planeNo
== 0x0F || planeNo
== 0x10) { // Plane 15 & 16
439 return ((theChar
& 0xFF) > 0xFFFD ? true : false);
441 return (planeNo
< data
->_numPlanes
&& data
->_planes
[planeNo
] ? !CFUniCharIsMemberOfBitmap(theChar
, data
->_planes
[planeNo
]) : true);
443 } else if (charset
== kCFUniCharControlAndFormatterCharacterSet
) {
444 if (planeNo
== 0x0E) { // Plane 14
446 return (((theChar
== 0x01) || ((theChar
> 0x1F) && (theChar
< 0x80))) ? true : false);
448 return (planeNo
< data
->_numPlanes
&& data
->_planes
[planeNo
] ? CFUniCharIsMemberOfBitmap(theChar
, data
->_planes
[planeNo
]) : false);
451 return (planeNo
< data
->_numPlanes
&& data
->_planes
[planeNo
] ? CFUniCharIsMemberOfBitmap(theChar
, data
->_planes
[planeNo
]) : false);
459 const uint8_t *CFUniCharGetBitmapPtrForPlane(uint32_t charset
, uint32_t plane
) {
460 if (NULL
== __CFUniCharBitmapDataArray
) __CFUniCharLoadBitmapData();
462 charset
= __CFUniCharMapCompatibilitySetID(charset
);
464 if ((charset
> kCFUniCharWhitespaceAndNewlineCharacterSet
) && (charset
!= kCFUniCharIllegalCharacterSet
) && (charset
!= kCFUniCharNewlineCharacterSet
)) {
465 uint32_t tableIndex
= __CFUniCharMapExternalSetToInternalIndex(charset
);
467 if (tableIndex
< __CFUniCharNumberOfBitmaps
) {
468 __CFUniCharBitmapData
*data
= __CFUniCharBitmapDataArray
+ tableIndex
;
470 return (plane
< data
->_numPlanes
? data
->_planes
[plane
] : NULL
);
476 __private_extern__
uint8_t CFUniCharGetBitmapForPlane(uint32_t charset
, uint32_t plane
, void *bitmap
, bool isInverted
) {
477 const uint8_t *src
= CFUniCharGetBitmapPtrForPlane(charset
, plane
);
478 int numBytes
= (8 * 1024);
482 #if defined (__cplusplus)
483 while (numBytes
-- > 0) *(((uint8_t *&)bitmap
)++) = ~(*(src
++));
485 while (numBytes
-- > 0) *((uint8_t *)bitmap
++) = ~(*(src
++));
488 #if defined (__cplusplus)
489 while (numBytes
-- > 0) *(((uint8_t *&)bitmap
)++) = *(src
++);
491 while (numBytes
-- > 0) *((uint8_t *)bitmap
++) = *(src
++);
494 return kCFUniCharBitmapFilled
;
495 } else if (charset
== kCFUniCharIllegalCharacterSet
) {
496 __CFUniCharBitmapData
*data
= __CFUniCharBitmapDataArray
+ __CFUniCharMapExternalSetToInternalIndex(__CFUniCharMapCompatibilitySetID(charset
));
498 if (plane
< data
->_numPlanes
&& (src
= data
->_planes
[plane
])) {
500 #if defined (__cplusplus)
501 while (numBytes
-- > 0) *(((uint8_t *&)bitmap
)++) = *(src
++);
503 while (numBytes
-- > 0) *((uint8_t *)bitmap
++) = *(src
++);
506 #if defined (__cplusplus)
507 while (numBytes
-- > 0) *(((uint8_t *&)bitmap
)++) = ~(*(src
++));
509 while (numBytes
-- > 0) *((uint8_t *)bitmap
++) = ~(*(src
++));
512 return kCFUniCharBitmapFilled
;
513 } else if (plane
== 0x0E) { // Plane 14
515 uint8_t asciiRange
= (isInverted
? (uint8_t)0xFF : (uint8_t)0);
516 uint8_t otherRange
= (isInverted
? (uint8_t)0 : (uint8_t)0xFF);
518 #if defined (__cplusplus)
519 *(((uint8_t *&)bitmap
)++) = 0x02; // UE0001 LANGUAGE TAG
521 *((uint8_t *)bitmap
++) = 0x02; // UE0001 LANGUAGE TAG
523 for (idx
= 1;idx
< numBytes
;idx
++) {
524 #if defined (__cplusplus)
525 *(((uint8_t *&)bitmap
)++) = ((idx
>= (0x20 / 8) && (idx
< (0x80 / 8))) ? asciiRange
: otherRange
);
527 *((uint8_t *)bitmap
++) = ((idx
>= (0x20 / 8) && (idx
< (0x80 / 8))) ? asciiRange
: otherRange
);
530 return kCFUniCharBitmapFilled
;
531 } else if (plane
== 0x0F || plane
== 0x10) { // Plane 15 & 16
532 uint32_t value
= (isInverted
? ~0 : 0);
533 numBytes
/= 4; // for 32bit
535 while (numBytes
-- > 0) {
536 *((uint32_t *)bitmap
) = value
;
537 #if defined (__cplusplus)
538 bitmap
= (uint8_t *)bitmap
+ sizeof(uint32_t);
540 bitmap
+= sizeof(uint32_t);
543 *(((uint8_t *)bitmap
) - 5) = (isInverted
? 0x3F : 0xC0); // 0xFFFE & 0xFFFF
544 return kCFUniCharBitmapFilled
;
546 return (isInverted
? kCFUniCharBitmapEmpty
: kCFUniCharBitmapAll
);
547 } else if ((charset
< kCFUniCharDecimalDigitCharacterSet
) || (charset
== kCFUniCharNewlineCharacterSet
)) {
548 if (plane
) return (isInverted
? kCFUniCharBitmapAll
: kCFUniCharBitmapEmpty
);
550 uint8_t *bitmapBase
= (uint8_t *)bitmap
;
552 uint8_t nonFillValue
= (isInverted
? (uint8_t)0xFF : (uint8_t)0);
554 #if defined (__cplusplus)
555 while (numBytes
-- > 0) *(((uint8_t *&)bitmap
)++) = nonFillValue
;
557 while (numBytes
-- > 0) *((uint8_t *)bitmap
++) = nonFillValue
;
560 if ((charset
== kCFUniCharWhitespaceAndNewlineCharacterSet
) || (charset
== kCFUniCharNewlineCharacterSet
)) {
561 const UniChar newlines
[] = {0x000A, 0x000B, 0x000C, 0x000D, 0x0085, 0x2028, 0x2029};
563 for (idx
= 0;idx
< (int)(sizeof(newlines
) / sizeof(*newlines
)); idx
++) {
565 CFUniCharRemoveCharacterFromBitmap(newlines
[idx
], bitmapBase
);
567 CFUniCharAddCharacterToBitmap(newlines
[idx
], bitmapBase
);
571 if (charset
== kCFUniCharNewlineCharacterSet
) return kCFUniCharBitmapFilled
;
575 CFUniCharRemoveCharacterFromBitmap(0x0009, bitmapBase
);
576 CFUniCharRemoveCharacterFromBitmap(0x0020, bitmapBase
);
577 CFUniCharRemoveCharacterFromBitmap(0x00A0, bitmapBase
);
578 CFUniCharRemoveCharacterFromBitmap(0x1680, bitmapBase
);
579 CFUniCharRemoveCharacterFromBitmap(0x202F, bitmapBase
);
580 CFUniCharRemoveCharacterFromBitmap(0x205F, bitmapBase
);
581 CFUniCharRemoveCharacterFromBitmap(0x3000, bitmapBase
);
583 CFUniCharAddCharacterToBitmap(0x0009, bitmapBase
);
584 CFUniCharAddCharacterToBitmap(0x0020, bitmapBase
);
585 CFUniCharAddCharacterToBitmap(0x00A0, bitmapBase
);
586 CFUniCharAddCharacterToBitmap(0x1680, bitmapBase
);
587 CFUniCharAddCharacterToBitmap(0x202F, bitmapBase
);
588 CFUniCharAddCharacterToBitmap(0x205F, bitmapBase
);
589 CFUniCharAddCharacterToBitmap(0x3000, bitmapBase
);
592 for (idx
= 0x2000;idx
<= 0x200B;idx
++) {
594 CFUniCharRemoveCharacterFromBitmap(idx
, bitmapBase
);
596 CFUniCharAddCharacterToBitmap(idx
, bitmapBase
);
599 return kCFUniCharBitmapFilled
;
601 return (isInverted
? kCFUniCharBitmapAll
: kCFUniCharBitmapEmpty
);
604 __private_extern__
uint32_t CFUniCharGetNumberOfPlanes(uint32_t charset
) {
605 if ((charset
== kCFUniCharControlCharacterSet
) || (charset
== kCFUniCharControlAndFormatterCharacterSet
)) {
606 return 15; // 0 to 14
607 } else if (charset
< kCFUniCharDecimalDigitCharacterSet
) {
609 } else if (charset
== kCFUniCharIllegalCharacterSet
) {
614 if (NULL
== __CFUniCharBitmapDataArray
) __CFUniCharLoadBitmapData();
616 numPlanes
= __CFUniCharBitmapDataArray
[__CFUniCharMapExternalSetToInternalIndex(__CFUniCharMapCompatibilitySetID(charset
))]._numPlanes
;
622 // Mapping data loading
623 static const void **__CFUniCharMappingTables
= NULL
;
625 static CFSpinLock_t __CFUniCharMappingTableLock
= CFSpinLockInit
;
627 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX
628 #if __CF_BIG_ENDIAN__
629 #if USE_MACHO_SEGMENT
630 #define MAPPING_TABLE_FILE "__data"
632 #define MAPPING_TABLE_FILE "/CFUnicodeData-B.mapping"
635 #if USE_MACHO_SEGMENT
636 #define MAPPING_TABLE_FILE "__data"
638 #define MAPPING_TABLE_FILE "/CFUnicodeData-L.mapping"
641 #elif DEPLOYMENT_TARGET_WINDOWS
642 #if __CF_BIG_ENDIAN__
643 #if USE_MACHO_SEGMENT
644 #define MAPPING_TABLE_FILE "__data"
646 #define MAPPING_TABLE_FILE L"CFUnicodeData-B.mapping"
649 #if USE_MACHO_SEGMENT
650 #define MAPPING_TABLE_FILE "__data"
652 #define MAPPING_TABLE_FILE L"CFUnicodeData-L.mapping"
656 #error Unknown or unspecified DEPLOYMENT_TARGET
659 __private_extern__
const void *CFUniCharGetMappingData(uint32_t type
) {
661 __CFSpinLock(&__CFUniCharMappingTableLock
);
663 if (NULL
== __CFUniCharMappingTables
) {
665 const void *bodyBase
;
670 if (!__CFUniCharLoadFile(MAPPING_TABLE_FILE
, &bytes
, &fileSize
) || !__CFSimpleFileSizeVerification(bytes
, fileSize
)) {
671 __CFSpinUnlock(&__CFUniCharMappingTableLock
);
675 #if defined (__cplusplus)
676 bytes
= (uint8_t *)bytes
+ 4; // Skip Unicode version
677 headerSize
= *((uint8_t *)bytes
); bytes
= (uint8_t *)bytes
+ sizeof(uint32_t);
679 bytes
+= 4; // Skip Unicode version
680 headerSize
= *((uint32_t *)bytes
); bytes
+= sizeof(uint32_t);
682 headerSize
-= (sizeof(uint32_t) * 2);
683 bodyBase
= (char *)bytes
+ headerSize
;
685 count
= headerSize
/ sizeof(uint32_t);
687 __CFUniCharMappingTables
= (const void **)CFAllocatorAllocate(kCFAllocatorSystemDefault
, sizeof(const void *) * count
, 0);
689 for (idx
= 0;idx
< count
;idx
++) {
690 #if defined (__cplusplus)
691 __CFUniCharMappingTables
[idx
] = (char *)bodyBase
+ *((uint32_t *)bytes
); bytes
= (uint8_t *)bytes
+ sizeof(uint32_t);
693 __CFUniCharMappingTables
[idx
] = (char *)bodyBase
+ *((uint32_t *)bytes
); bytes
+= sizeof(uint32_t);
698 __CFSpinUnlock(&__CFUniCharMappingTableLock
);
700 return __CFUniCharMappingTables
[type
];
703 // Case mapping functions
704 #define DO_SPECIAL_CASE_MAPPING 1
706 static uint32_t *__CFUniCharCaseMappingTableCounts
= NULL
;
707 static uint32_t **__CFUniCharCaseMappingTable
= NULL
;
708 static const uint32_t **__CFUniCharCaseMappingExtraTable
= NULL
;
713 } __CFUniCharCaseMappings
;
715 /* Binary searches CFStringEncodingUnicodeTo8BitCharMap */
716 static uint32_t __CFUniCharGetMappedCase(const __CFUniCharCaseMappings
*theTable
, uint32_t numElem
, UTF32Char character
) {
717 const __CFUniCharCaseMappings
*p
, *q
, *divider
;
719 if ((character
< theTable
[0]._key
) || (character
> theTable
[numElem
-1]._key
)) {
725 divider
= p
+ ((q
- p
) >> 1); /* divide by 2 */
726 if (character
< divider
->_key
) { q
= divider
- 1; }
727 else if (character
> divider
->_key
) { p
= divider
+ 1; }
728 else { return divider
->_value
; }
733 #define NUM_CASE_MAP_DATA (kCFUniCharCaseFold + 1)
735 static bool __CFUniCharLoadCaseMappingTable(void) {
736 uint32_t *countArray
;
739 if (NULL
== __CFUniCharMappingTables
) (void)CFUniCharGetMappingData(kCFUniCharToLowercase
);
740 if (NULL
== __CFUniCharMappingTables
) return false;
742 __CFSpinLock(&__CFUniCharMappingTableLock
);
744 if (__CFUniCharCaseMappingTableCounts
) {
745 __CFSpinUnlock(&__CFUniCharMappingTableLock
);
749 countArray
= (uint32_t *)CFAllocatorAllocate(kCFAllocatorSystemDefault
, sizeof(uint32_t) * NUM_CASE_MAP_DATA
+ sizeof(uint32_t *) * NUM_CASE_MAP_DATA
* 2, 0);
750 __CFUniCharCaseMappingTable
= (uint32_t **)((char *)countArray
+ sizeof(uint32_t) * NUM_CASE_MAP_DATA
);
751 __CFUniCharCaseMappingExtraTable
= (const uint32_t **)__CFUniCharCaseMappingTable
+ NUM_CASE_MAP_DATA
;
753 for (idx
= 0;idx
< NUM_CASE_MAP_DATA
;idx
++) {
754 countArray
[idx
] = *((uint32_t *)__CFUniCharMappingTables
[idx
]) / (sizeof(uint32_t) * 2);
755 __CFUniCharCaseMappingTable
[idx
] = ((uint32_t *)__CFUniCharMappingTables
[idx
]) + 1;
756 __CFUniCharCaseMappingExtraTable
[idx
] = (const uint32_t *)((char *)__CFUniCharCaseMappingTable
[idx
] + *((uint32_t *)__CFUniCharMappingTables
[idx
]));
759 __CFUniCharCaseMappingTableCounts
= countArray
;
761 __CFSpinUnlock(&__CFUniCharMappingTableLock
);
765 #if __CF_BIG_ENDIAN__
766 #define TURKISH_LANG_CODE (0x7472) // tr
767 #define LITHUANIAN_LANG_CODE (0x6C74) // lt
768 #define AZERI_LANG_CODE (0x617A) // az
769 #define DUTCH_LANG_CODE (0x6E6C) // nl
770 #define GREEK_LANG_CODE (0x656C) // el
772 #define TURKISH_LANG_CODE (0x7274) // tr
773 #define LITHUANIAN_LANG_CODE (0x746C) // lt
774 #define AZERI_LANG_CODE (0x7A61) // az
775 #define DUTCH_LANG_CODE (0x6C6E) // nl
776 #define GREEK_LANG_CODE (0x6C65) // el
779 CFIndex
CFUniCharMapCaseTo(UTF32Char theChar
, UTF16Char
*convertedChar
, CFIndex maxLength
, uint32_t ctype
, uint32_t flags
, const uint8_t *langCode
) {
780 __CFUniCharBitmapData
*data
;
781 uint8_t planeNo
= (theChar
>> 16) & 0xFF;
785 #if DO_SPECIAL_CASE_MAPPING
786 if (flags
& kCFUniCharCaseMapFinalSigma
) {
787 if (theChar
== 0x03A3) { // Final sigma
788 *convertedChar
= (ctype
== kCFUniCharToLowercase
? 0x03C2 : 0x03A3);
794 if (flags
& kCFUniCharCaseMapGreekTonos
) { // localized Greek uppercasing
795 if (theChar
== 0x0301) { // GREEK TONOS
797 } else if (theChar
== 0x0344) {// COMBINING GREEK DIALYTIKA TONOS
798 *convertedChar
= 0x0308; // COMBINING GREEK DIALYTIKA
800 } else if (CFUniCharIsMemberOf(theChar
, kCFUniCharDecomposableCharacterSet
)) {
801 UTF32Char buffer
[MAX_DECOMPOSED_LENGTH
];
802 CFIndex length
= CFUniCharDecomposeCharacter(theChar
, buffer
, MAX_DECOMPOSED_LENGTH
);
805 UTF32Char
*characters
= buffer
+ 1;
806 UTF32Char
*tail
= buffer
+ length
;
808 while (characters
< tail
) {
809 if (*characters
== 0x0301) break;
813 if (characters
< tail
) { // found a tonos
814 CFIndex convertedLength
= CFUniCharMapCaseTo(*buffer
, convertedChar
, maxLength
, ctype
, 0, langCode
);
816 if (convertedLength
== 0) {
817 *convertedChar
= (UTF16Char
)*buffer
;
821 characters
= buffer
+ 1;
823 while (characters
< tail
) {
824 if (*characters
!= 0x0301) { // not tonos
825 if (*characters
< 0x10000) { // BMP
826 convertedChar
[convertedLength
] = (UTF16Char
)*characters
;
829 UTF32Char character
= *characters
- 0x10000;
830 convertedChar
[convertedLength
++] = (UTF16Char
)((character
>> 10) + 0xD800UL
);
831 convertedChar
[convertedLength
++] = (UTF16Char
)((character
& 0x3FF) + 0xDC00UL
);
837 return convertedLength
;
842 switch (*(uint16_t *)langCode
) {
843 case LITHUANIAN_LANG_CODE
:
844 if (theChar
== 0x0307 && (flags
& kCFUniCharCaseMapAfter_i
)) {
846 } else if (ctype
== kCFUniCharToLowercase
) {
847 if (flags
& kCFUniCharCaseMapMoreAbove
) {
849 case 0x0049: // LATIN CAPITAL LETTER I
850 *(convertedChar
++) = 0x0069;
851 *(convertedChar
++) = 0x0307;
854 case 0x004A: // LATIN CAPITAL LETTER J
855 *(convertedChar
++) = 0x006A;
856 *(convertedChar
++) = 0x0307;
859 case 0x012E: // LATIN CAPITAL LETTER I WITH OGONEK
860 *(convertedChar
++) = 0x012F;
861 *(convertedChar
++) = 0x0307;
868 case 0x00CC: // LATIN CAPITAL LETTER I WITH GRAVE
869 *(convertedChar
++) = 0x0069;
870 *(convertedChar
++) = 0x0307;
871 *(convertedChar
++) = 0x0300;
874 case 0x00CD: // LATIN CAPITAL LETTER I WITH ACUTE
875 *(convertedChar
++) = 0x0069;
876 *(convertedChar
++) = 0x0307;
877 *(convertedChar
++) = 0x0301;
880 case 0x0128: // LATIN CAPITAL LETTER I WITH TILDE
881 *(convertedChar
++) = 0x0069;
882 *(convertedChar
++) = 0x0307;
883 *(convertedChar
++) = 0x0303;
891 case TURKISH_LANG_CODE
:
892 case AZERI_LANG_CODE
:
893 if ((theChar
== 0x0049) || (theChar
== 0x0131)) { // LATIN CAPITAL LETTER I & LATIN SMALL LETTER DOTLESS I
894 *convertedChar
= (((ctype
== kCFUniCharToLowercase
) || (ctype
== kCFUniCharCaseFold
)) ? ((kCFUniCharCaseMapMoreAbove
& flags
) ? 0x0069 : 0x0131) : 0x0049);
896 } else if ((theChar
== 0x0069) || (theChar
== 0x0130)) { // LATIN SMALL LETTER I & LATIN CAPITAL LETTER I WITH DOT ABOVE
897 *convertedChar
= (((ctype
== kCFUniCharToLowercase
) || (ctype
== kCFUniCharCaseFold
)) ? 0x0069 : 0x0130);
899 } else if (theChar
== 0x0307 && (kCFUniCharCaseMapAfter_i
& flags
)) { // COMBINING DOT ABOVE AFTER_i
900 if (ctype
== kCFUniCharToLowercase
) {
903 *convertedChar
= 0x0307;
909 case DUTCH_LANG_CODE
:
910 if ((theChar
== 0x004A) || (theChar
== 0x006A)) {
911 *convertedChar
= (((ctype
== kCFUniCharToUppercase
) || (ctype
== kCFUniCharToTitlecase
) || (kCFUniCharCaseMapDutchDigraph
& flags
)) ? 0x004A : 0x006A);
919 #endif // DO_SPECIAL_CASE_MAPPING
921 if (NULL
== __CFUniCharBitmapDataArray
) __CFUniCharLoadBitmapData();
923 data
= __CFUniCharBitmapDataArray
+ __CFUniCharMapExternalSetToInternalIndex(__CFUniCharMapCompatibilitySetID(ctype
+ kCFUniCharHasNonSelfLowercaseCharacterSet
));
925 if (planeNo
< data
->_numPlanes
&& data
->_planes
[planeNo
] && CFUniCharIsMemberOfBitmap(theChar
, data
->_planes
[planeNo
]) && (__CFUniCharCaseMappingTableCounts
|| __CFUniCharLoadCaseMappingTable())) {
926 uint32_t value
= __CFUniCharGetMappedCase((const __CFUniCharCaseMappings
*)__CFUniCharCaseMappingTable
[ctype
], __CFUniCharCaseMappingTableCounts
[ctype
], theChar
);
928 if (!value
&& ctype
== kCFUniCharToTitlecase
) {
929 value
= __CFUniCharGetMappedCase((const __CFUniCharCaseMappings
*)__CFUniCharCaseMappingTable
[kCFUniCharToUppercase
], __CFUniCharCaseMappingTableCounts
[kCFUniCharToUppercase
], theChar
);
930 if (value
) ctype
= kCFUniCharToUppercase
;
934 CFIndex count
= CFUniCharConvertFlagToCount(value
);
937 if (value
& kCFUniCharNonBmpFlag
) {
939 value
= (value
& 0xFFFFFF) - 0x10000;
940 *(convertedChar
++) = (UTF16Char
)(value
>> 10) + 0xD800UL
;
941 *(convertedChar
++) = (UTF16Char
)(value
& 0x3FF) + 0xDC00UL
;
945 *convertedChar
= (UTF16Char
)value
;
948 } else if (count
< maxLength
) {
949 const uint32_t *extraMapping
= __CFUniCharCaseMappingExtraTable
[ctype
] + (value
& 0xFFFFFF);
951 if (value
& kCFUniCharNonBmpFlag
) {
952 CFIndex copiedLen
= 0;
954 while (count
-- > 0) {
955 value
= *(extraMapping
++);
956 if (value
> 0xFFFF) {
957 if (copiedLen
+ 2 >= maxLength
) break;
958 value
= (value
& 0xFFFFFF) - 0x10000;
959 convertedChar
[copiedLen
++] = (UTF16Char
)(value
>> 10) + 0xD800UL
;
960 convertedChar
[copiedLen
++] = (UTF16Char
)(value
& 0x3FF) + 0xDC00UL
;
962 if (copiedLen
+ 1 >= maxLength
) break;
963 convertedChar
[copiedLen
++] = value
;
966 if (!count
) return copiedLen
;
970 for (idx
= 0;idx
< count
;idx
++) *(convertedChar
++) = (UTF16Char
)*(extraMapping
++);
975 } else if (ctype
== kCFUniCharCaseFold
) {
976 ctype
= kCFUniCharToLowercase
;
980 if (theChar
> 0xFFFF) { // non-BMP
981 theChar
= (theChar
& 0xFFFFFF) - 0x10000;
982 *(convertedChar
++) = (UTF16Char
)(theChar
>> 10) + 0xD800UL
;
983 *(convertedChar
++) = (UTF16Char
)(theChar
& 0x3FF) + 0xDC00UL
;
986 *convertedChar
= theChar
;
991 CFIndex
CFUniCharMapTo(UniChar theChar
, UniChar
*convertedChar
, CFIndex maxLength
, uint16_t ctype
, uint32_t flags
) {
992 if (ctype
== kCFUniCharCaseFold
+ 1) { // kCFUniCharDecompose
993 if (CFUniCharIsDecomposableCharacter(theChar
, false)) {
994 UTF32Char buffer
[MAX_DECOMPOSED_LENGTH
];
995 CFIndex usedLength
= CFUniCharDecomposeCharacter(theChar
, buffer
, MAX_DECOMPOSED_LENGTH
);
998 for (idx
= 0;idx
< usedLength
;idx
++) *(convertedChar
++) = buffer
[idx
];
1001 *convertedChar
= theChar
;
1005 return CFUniCharMapCaseTo(theChar
, convertedChar
, maxLength
, ctype
, flags
, NULL
);
1009 CF_INLINE
bool __CFUniCharIsMoreAbove(UTF16Char
*buffer
, CFIndex length
) {
1010 UTF32Char currentChar
;
1013 while (length
-- > 0) {
1014 currentChar
= *(buffer
)++;
1015 if (CFUniCharIsSurrogateHighCharacter(currentChar
) && (length
> 0) && CFUniCharIsSurrogateLowCharacter(*(buffer
+ 1))) {
1016 currentChar
= CFUniCharGetLongCharacterForSurrogatePair(currentChar
, *(buffer
++));
1019 if (!CFUniCharIsMemberOf(currentChar
, kCFUniCharNonBaseCharacterSet
)) break;
1021 property
= CFUniCharGetCombiningPropertyForCharacter(currentChar
, (const uint8_t *)CFUniCharGetUnicodePropertyDataForPlane(kCFUniCharCombiningProperty
, (currentChar
>> 16) & 0xFF));
1023 if (property
== 230) return true; // Above priority
1028 CF_INLINE
bool __CFUniCharIsAfter_i(UTF16Char
*buffer
, CFIndex length
) {
1029 UTF32Char currentChar
= 0;
1031 UTF32Char decomposed
[MAX_DECOMPOSED_LENGTH
];
1032 CFIndex decompLength
;
1035 if (length
< 1) return 0;
1038 while (length
-- > 1) {
1039 currentChar
= *(--buffer
);
1040 if (CFUniCharIsSurrogateLowCharacter(currentChar
)) {
1041 if ((length
> 1) && CFUniCharIsSurrogateHighCharacter(*(buffer
- 1))) {
1042 currentChar
= CFUniCharGetLongCharacterForSurrogatePair(*(--buffer
), currentChar
);
1048 if (!CFUniCharIsMemberOf(currentChar
, kCFUniCharNonBaseCharacterSet
)) break;
1050 property
= CFUniCharGetCombiningPropertyForCharacter(currentChar
, (const uint8_t *)CFUniCharGetUnicodePropertyDataForPlane(kCFUniCharCombiningProperty
, (currentChar
>> 16) & 0xFF));
1052 if (property
== 230) return false; // Above priority
1055 currentChar
= *(--buffer
);
1056 } else if (CFUniCharIsSurrogateLowCharacter(currentChar
) && CFUniCharIsSurrogateHighCharacter(*(--buffer
))) {
1057 currentChar
= CFUniCharGetLongCharacterForSurrogatePair(*buffer
, currentChar
);
1060 decompLength
= CFUniCharDecomposeCharacter(currentChar
, decomposed
, MAX_DECOMPOSED_LENGTH
);
1061 currentChar
= *decomposed
;
1064 for (idx
= 1;idx
< decompLength
;idx
++) {
1065 currentChar
= decomposed
[idx
];
1066 property
= CFUniCharGetCombiningPropertyForCharacter(currentChar
, (const uint8_t *)CFUniCharGetUnicodePropertyDataForPlane(kCFUniCharCombiningProperty
, (currentChar
>> 16) & 0xFF));
1068 if (property
== 230) return false; // Above priority
1073 __private_extern__
uint32_t CFUniCharGetConditionalCaseMappingFlags(UTF32Char theChar
, UTF16Char
*buffer
, CFIndex currentIndex
, CFIndex length
, uint32_t type
, const uint8_t *langCode
, uint32_t lastFlags
) {
1074 if (theChar
== 0x03A3) { // GREEK CAPITAL LETTER SIGMA
1075 if ((type
== kCFUniCharToLowercase
) && (currentIndex
> 0)) {
1076 UTF16Char
*start
= buffer
;
1077 UTF16Char
*end
= buffer
+ length
;
1078 UTF32Char otherChar
;
1080 // First check if we're after a cased character
1081 buffer
+= (currentIndex
- 1);
1082 while (start
<= buffer
) {
1083 otherChar
= *(buffer
--);
1084 if (CFUniCharIsSurrogateLowCharacter(otherChar
) && (start
<= buffer
) && CFUniCharIsSurrogateHighCharacter(*buffer
)) {
1085 otherChar
= CFUniCharGetLongCharacterForSurrogatePair(*(buffer
--), otherChar
);
1087 if (!CFUniCharIsMemberOf(otherChar
, kCFUniCharCaseIgnorableCharacterSet
)) {
1088 if (!CFUniCharIsMemberOf(otherChar
, kCFUniCharUppercaseLetterCharacterSet
) && !CFUniCharIsMemberOf(otherChar
, kCFUniCharLowercaseLetterCharacterSet
)) return 0; // Uppercase set contains titlecase
1093 // Next check if we're before a cased character
1094 buffer
= start
+ currentIndex
+ 1;
1095 while (buffer
< end
) {
1096 otherChar
= *(buffer
++);
1097 if (CFUniCharIsSurrogateHighCharacter(otherChar
) && (buffer
< end
) && CFUniCharIsSurrogateLowCharacter(*buffer
)) {
1098 otherChar
= CFUniCharGetLongCharacterForSurrogatePair(otherChar
, *(buffer
++));
1100 if (!CFUniCharIsMemberOf(otherChar
, kCFUniCharCaseIgnorableCharacterSet
)) {
1101 if (CFUniCharIsMemberOf(otherChar
, kCFUniCharUppercaseLetterCharacterSet
) || CFUniCharIsMemberOf(otherChar
, kCFUniCharLowercaseLetterCharacterSet
)) return 0; // Uppercase set contains titlecase
1105 return kCFUniCharCaseMapFinalSigma
;
1107 } else if (langCode
) {
1108 if (*((const uint16_t *)langCode
) == LITHUANIAN_LANG_CODE
) {
1109 if ((theChar
== 0x0307) && ((kCFUniCharCaseMapAfter_i
|kCFUniCharCaseMapMoreAbove
) & lastFlags
) == (kCFUniCharCaseMapAfter_i
|kCFUniCharCaseMapMoreAbove
)) {
1110 return (__CFUniCharIsAfter_i(buffer
, currentIndex
) ? kCFUniCharCaseMapAfter_i
: 0);
1111 } else if (type
== kCFUniCharToLowercase
) {
1112 if ((theChar
== 0x0049) || (theChar
== 0x004A) || (theChar
== 0x012E)) {
1113 return (__CFUniCharIsMoreAbove(buffer
+ (++currentIndex
), length
- currentIndex
) ? kCFUniCharCaseMapMoreAbove
: 0);
1115 } else if ((theChar
== 'i') || (theChar
== 'j')) {
1116 return (__CFUniCharIsMoreAbove(buffer
+ (++currentIndex
), length
- currentIndex
) ? (kCFUniCharCaseMapAfter_i
|kCFUniCharCaseMapMoreAbove
) : 0);
1118 } else if ((*((const uint16_t *)langCode
) == TURKISH_LANG_CODE
) || (*((const uint16_t *)langCode
) == AZERI_LANG_CODE
)) {
1119 if (type
== kCFUniCharToLowercase
) {
1120 if (theChar
== 0x0307) {
1121 return (kCFUniCharCaseMapMoreAbove
& lastFlags
? kCFUniCharCaseMapAfter_i
: 0);
1122 } else if (theChar
== 0x0049) {
1123 return (((++currentIndex
< length
) && (buffer
[currentIndex
] == 0x0307)) ? kCFUniCharCaseMapMoreAbove
: 0);
1126 } else if (*((const uint16_t *)langCode
) == DUTCH_LANG_CODE
) {
1127 if (kCFUniCharCaseMapDutchDigraph
& lastFlags
) {
1128 return (((theChar
== 0x006A) || (theChar
== 0x004A)) ? kCFUniCharCaseMapDutchDigraph
: 0);
1130 if ((type
== kCFUniCharToTitlecase
) && ((theChar
== 0x0069) || (theChar
== 0x0049))) {
1131 return (((++currentIndex
< length
) && ((buffer
[currentIndex
] == 0x006A) || (buffer
[currentIndex
] == 0x004A))) ? kCFUniCharCaseMapDutchDigraph
: 0);
1136 if (kCFUniCharCaseMapGreekTonos
& lastFlags
) { // still searching for tonos
1137 if (CFUniCharIsMemberOf(theChar
, kCFUniCharNonBaseCharacterSet
)) {
1138 return kCFUniCharCaseMapGreekTonos
;
1141 if (((theChar
>= 0x0370) && (theChar
< 0x0400)) || ((theChar
>= 0x1F00) && (theChar
< 0x2000))) { // Greek/Coptic & Greek extended ranges
1142 if (((type
== kCFUniCharToUppercase
) || (type
== kCFUniCharToTitlecase
))&& (CFUniCharIsMemberOf(theChar
, kCFUniCharLetterCharacterSet
))) return kCFUniCharCaseMapGreekTonos
;
1148 // Unicode property database
1149 static __CFUniCharBitmapData
*__CFUniCharUnicodePropertyTable
= NULL
;
1150 static int __CFUniCharUnicodePropertyTableCount
= 0;
1152 static CFSpinLock_t __CFUniCharPropTableLock
= CFSpinLockInit
;
1154 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX
1155 #if USE_MACHO_SEGMENT
1156 #define PROP_DB_FILE "__properties"
1158 #define PROP_DB_FILE "/CFUniCharPropertyDatabase.data"
1160 #elif DEPLOYMENT_TARGET_WINDOWS
1161 #if USE_MACHO_SEGMENT
1162 #define PROP_DB_FILE "__properties"
1164 #define PROP_DB_FILE L"CFUniCharPropertyDatabase.data"
1167 #error Unknown or unspecified DEPLOYMENT_TARGET
1170 const void *CFUniCharGetUnicodePropertyDataForPlane(uint32_t propertyType
, uint32_t plane
) {
1172 __CFSpinLock(&__CFUniCharPropTableLock
);
1174 if (NULL
== __CFUniCharUnicodePropertyTable
) {
1175 __CFUniCharBitmapData
*table
;
1177 const void *bodyBase
;
1178 const void *planeBase
;
1181 int planeIndex
, planeCount
;
1185 if (!__CFUniCharLoadFile(PROP_DB_FILE
, &bytes
, &fileSize
) || !__CFSimpleFileSizeVerification(bytes
, fileSize
)) {
1186 __CFSpinUnlock(&__CFUniCharPropTableLock
);
1190 #if defined (__cplusplus)
1191 bytes
= (uint8_t*)bytes
+ 4; // Skip Unicode version
1192 headerSize
= CFSwapInt32BigToHost(*((uint32_t *)bytes
)); bytes
= (uint8_t *)bytes
+ sizeof(uint32_t);
1194 bytes
+= 4; // Skip Unicode version
1195 headerSize
= CFSwapInt32BigToHost(*((uint32_t *)bytes
)); bytes
+= sizeof(uint32_t);
1198 headerSize
-= (sizeof(uint32_t) * 2);
1199 bodyBase
= (char *)bytes
+ headerSize
;
1201 count
= headerSize
/ sizeof(uint32_t);
1202 __CFUniCharUnicodePropertyTableCount
= count
;
1204 table
= (__CFUniCharBitmapData
*)CFAllocatorAllocate(kCFAllocatorSystemDefault
, sizeof(__CFUniCharBitmapData
) * count
, 0);
1206 for (idx
= 0;idx
< count
;idx
++) {
1207 planeCount
= *((const uint8_t *)bodyBase
);
1208 planeBase
= (char *)bodyBase
+ planeCount
+ (planeCount
% 4 ? 4 - (planeCount
% 4) : 0);
1209 table
[idx
]._planes
= (const uint8_t **)CFAllocatorAllocate(kCFAllocatorSystemDefault
, sizeof(const void *) * planeCount
, 0);
1211 for (planeIndex
= 0;planeIndex
< planeCount
;planeIndex
++) {
1212 if ((planeSize
= ((const uint8_t *)bodyBase
)[planeIndex
+ 1])) {
1213 table
[idx
]._planes
[planeIndex
] = (const uint8_t *)planeBase
;
1214 #if defined (__cplusplus)
1215 planeBase
= (char*)planeBase
+ (planeSize
* 256);
1217 planeBase
+= (planeSize
* 256);
1220 table
[idx
]._planes
[planeIndex
] = NULL
;
1224 table
[idx
]._numPlanes
= planeCount
;
1225 #if defined (__cplusplus)
1226 bodyBase
= (const uint8_t *)bodyBase
+ (CFSwapInt32BigToHost(*(uint32_t *)bytes
));
1227 ((uint32_t *&)bytes
) ++;
1229 bodyBase
+= (CFSwapInt32BigToHost(*((uint32_t *)bytes
++)));
1233 __CFUniCharUnicodePropertyTable
= table
;
1236 __CFSpinUnlock(&__CFUniCharPropTableLock
);
1238 return (plane
< __CFUniCharUnicodePropertyTable
[propertyType
]._numPlanes
? __CFUniCharUnicodePropertyTable
[propertyType
]._planes
[plane
] : NULL
);
1241 __private_extern__
uint32_t CFUniCharGetNumberOfPlanesForUnicodePropertyData(uint32_t propertyType
) {
1242 (void)CFUniCharGetUnicodePropertyDataForPlane(propertyType
, 0);
1243 return __CFUniCharUnicodePropertyTable
[propertyType
]._numPlanes
;
1246 __private_extern__
uint32_t CFUniCharGetUnicodeProperty(UTF32Char character
, uint32_t propertyType
) {
1247 if (propertyType
== kCFUniCharCombiningProperty
) {
1248 return CFUniCharGetCombiningPropertyForCharacter(character
, (const uint8_t *)CFUniCharGetUnicodePropertyDataForPlane(propertyType
, (character
>> 16) & 0xFF));
1249 } else if (propertyType
== kCFUniCharBidiProperty
) {
1250 return CFUniCharGetBidiPropertyForCharacter(character
, (const uint8_t *)CFUniCharGetUnicodePropertyDataForPlane(propertyType
, (character
>> 16) & 0xFF));
1259 The UTF8 conversion in the following function is derived from ConvertUTF.c
1262 * Copyright 2001 Unicode, Inc.
1266 * This source code is provided as is by Unicode, Inc. No claims are
1267 * made as to fitness for any particular purpose. No warranties of any
1268 * kind are expressed or implied. The recipient agrees to determine
1269 * applicability of information provided. If this file has been
1270 * purchased on magnetic or optical media from Unicode, Inc., the
1271 * sole remedy for any claim will be exchange of defective media
1272 * within 90 days of receipt.
1274 * Limitations on Rights to Redistribute This Code
1276 * Unicode, Inc. hereby grants the right to freely use the information
1277 * supplied in this file in the creation of products supporting the
1278 * Unicode Standard, and to make copies of this file in any form
1279 * for internal or external distribution as long as this notice
1282 #define UNI_REPLACEMENT_CHAR (0x0000FFFDUL)
1284 bool CFUniCharFillDestinationBuffer(const UTF32Char
*src
, CFIndex srcLength
, void **dst
, CFIndex dstLength
, CFIndex
*filledLength
, uint32_t dstFormat
) {
1285 UTF32Char currentChar
;
1286 CFIndex usedLength
= *filledLength
;
1288 if (dstFormat
== kCFUniCharUTF16Format
) {
1289 UTF16Char
*dstBuffer
= (UTF16Char
*)*dst
;
1291 while (srcLength
-- > 0) {
1292 currentChar
= *(src
++);
1294 if (currentChar
> 0xFFFF) { // Non-BMP
1297 if (usedLength
> dstLength
) return false;
1298 currentChar
-= 0x10000;
1299 *(dstBuffer
++) = (UTF16Char
)((currentChar
>> 10) + 0xD800UL
);
1300 *(dstBuffer
++) = (UTF16Char
)((currentChar
& 0x3FF) + 0xDC00UL
);
1305 if (usedLength
> dstLength
) return false;
1306 *(dstBuffer
++) = (UTF16Char
)currentChar
;
1312 } else if (dstFormat
== kCFUniCharUTF8Format
) {
1313 uint8_t *dstBuffer
= (uint8_t *)*dst
;
1314 uint16_t bytesToWrite
= 0;
1315 const UTF32Char byteMask
= 0xBF;
1316 const UTF32Char byteMark
= 0x80;
1317 static const uint8_t firstByteMark
[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
1319 while (srcLength
-- > 0) {
1320 currentChar
= *(src
++);
1322 /* Figure out how many bytes the result will require */
1323 if (currentChar
< (UTF32Char
)0x80) {
1325 } else if (currentChar
< (UTF32Char
)0x800) {
1327 } else if (currentChar
< (UTF32Char
)0x10000) {
1329 } else if (currentChar
< (UTF32Char
)0x200000) {
1333 currentChar
= UNI_REPLACEMENT_CHAR
;
1336 usedLength
+= bytesToWrite
;
1339 if (usedLength
> dstLength
) return false;
1341 dstBuffer
+= bytesToWrite
;
1342 switch (bytesToWrite
) { /* note: everything falls through. */
1343 case 4: *--dstBuffer
= (currentChar
| byteMark
) & byteMask
; currentChar
>>= 6;
1344 case 3: *--dstBuffer
= (currentChar
| byteMark
) & byteMask
; currentChar
>>= 6;
1345 case 2: *--dstBuffer
= (currentChar
| byteMark
) & byteMask
; currentChar
>>= 6;
1346 case 1: *--dstBuffer
= currentChar
| firstByteMark
[bytesToWrite
];
1348 dstBuffer
+= bytesToWrite
;
1354 UTF32Char
*dstBuffer
= (UTF32Char
*)*dst
;
1356 while (srcLength
-- > 0) {
1357 currentChar
= *(src
++);
1361 if (usedLength
> dstLength
) return false;
1362 *(dstBuffer
++) = currentChar
;
1369 *filledLength
= usedLength
;
1374 #if DEPLOYMENT_TARGET_WINDOWS
1375 void __CFUniCharCleanup(void)
1379 // cleanup memory allocated by __CFUniCharLoadBitmapData()
1380 __CFSpinLock(&__CFUniCharBitmapLock
);
1382 if (__CFUniCharBitmapDataArray
!= NULL
) {
1383 for (idx
= 0; idx
< (int)__CFUniCharNumberOfBitmaps
; idx
++) {
1384 CFAllocatorDeallocate(kCFAllocatorSystemDefault
, __CFUniCharBitmapDataArray
[idx
]._planes
);
1385 __CFUniCharBitmapDataArray
[idx
]._planes
= NULL
;
1388 CFAllocatorDeallocate(kCFAllocatorSystemDefault
, __CFUniCharBitmapDataArray
);
1389 __CFUniCharBitmapDataArray
= NULL
;
1390 __CFUniCharNumberOfBitmaps
= 0;
1393 __CFSpinUnlock(&__CFUniCharBitmapLock
);
1395 // cleanup memory allocated by CFUniCharGetMappingData()
1396 __CFSpinLock(&__CFUniCharMappingTableLock
);
1398 if (__CFUniCharMappingTables
!= NULL
) {
1399 CFAllocatorDeallocate(kCFAllocatorSystemDefault
, __CFUniCharMappingTables
);
1400 __CFUniCharMappingTables
= NULL
;
1403 // cleanup memory allocated by __CFUniCharLoadCaseMappingTable()
1404 if (__CFUniCharCaseMappingTableCounts
!= NULL
) {
1405 CFAllocatorDeallocate(kCFAllocatorSystemDefault
, __CFUniCharCaseMappingTableCounts
);
1406 __CFUniCharCaseMappingTableCounts
= NULL
;
1408 __CFUniCharCaseMappingTable
= NULL
;
1409 __CFUniCharCaseMappingExtraTable
= NULL
;
1412 __CFSpinUnlock(&__CFUniCharMappingTableLock
);
1414 // cleanup memory allocated by CFUniCharGetUnicodePropertyDataForPlane()
1415 __CFSpinLock(&__CFUniCharPropTableLock
);
1417 if (__CFUniCharUnicodePropertyTable
!= NULL
) {
1418 for (idx
= 0; idx
< __CFUniCharUnicodePropertyTableCount
; idx
++) {
1419 CFAllocatorDeallocate(kCFAllocatorSystemDefault
, __CFUniCharUnicodePropertyTable
[idx
]._planes
);
1420 __CFUniCharUnicodePropertyTable
[idx
]._planes
= NULL
;
1423 CFAllocatorDeallocate(kCFAllocatorSystemDefault
, __CFUniCharUnicodePropertyTable
);
1424 __CFUniCharUnicodePropertyTable
= NULL
;
1425 __CFUniCharUnicodePropertyTableCount
= 0;
1428 __CFSpinUnlock(&__CFUniCharPropTableLock
);
1432 #undef USE_MACHO_SEGMENT