forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFUniChar.c
1421 lines (1211 loc) · 55.8 KB
/
CFUniChar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* CFUniChar.c
Copyright (c) 2001-2018, Apple Inc. and the Swift project authors
Portions Copyright (c) 2014-2018, Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
Responsibility: Foundation Team
*/
#include <CoreFoundation/CFBase.h>
#include <CoreFoundation/CFByteOrder.h>
#include "CFInternal.h"
#include "CFUniChar.h"
#include "CFStringEncodingConverterExt.h"
#include "CFUnicodeDecomposition.h"
#include "CFUniCharPriv.h"
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#endif
#if TARGET_OS_MAC
#include <mach/mach.h>
#endif
#if TARGET_OS_WIN32
extern void _CFGetFrameworkPath(wchar_t *path, int maxLength);
#endif
#if TARGET_OS_MAC
#define __kCFCharacterSetDir "/System/Library/CoreServices"
#elif TARGET_OS_LINUX || TARGET_OS_BSD
#define __kCFCharacterSetDir "/usr/local/share/CoreFoundation"
#elif TARGET_OS_WIN32
#define __kCFCharacterSetDir "\\Windows\\CoreFoundation"
#endif
#if TARGET_OS_MAC
#define USE_MACHO_SEGMENT 1
#elif DEPLOYMENT_RUNTIME_SWIFT && (TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WIN32)
#define USE_RAW_SYMBOL 1
#endif
enum {
kCFUniCharLastExternalSet = kCFUniCharNewlineCharacterSet,
kCFUniCharFirstInternalSet = kCFUniCharCompatibilityDecomposableCharacterSet,
kCFUniCharLastInternalSet = kCFUniCharGraphemeExtendCharacterSet,
kCFUniCharFirstBitmapSet = kCFUniCharDecimalDigitCharacterSet
};
CF_INLINE uint32_t __CFUniCharMapExternalSetToInternalIndex(uint32_t cset) { return ((kCFUniCharFirstInternalSet <= cset) ? ((cset - kCFUniCharFirstInternalSet) + kCFUniCharLastExternalSet) : cset) - kCFUniCharFirstBitmapSet; }
CF_INLINE uint32_t __CFUniCharMapCompatibilitySetID(uint32_t cset) { return ((cset == kCFUniCharControlCharacterSet) ? kCFUniCharControlAndFormatterCharacterSet : (((cset > kCFUniCharLastExternalSet) && (cset < kCFUniCharFirstInternalSet)) ? ((cset - kCFUniCharLastExternalSet) + kCFUniCharFirstInternalSet) : cset)); }
#if TARGET_OS_MAC && USE_MACHO_SEGMENT
#include <mach-o/getsect.h>
#include <mach-o/dyld.h>
#include <mach-o/ldsyms.h>
extern const void* unicode_csbitmaps_section_start __asm("section$start$__UNICODE$__csbitmaps");
extern const void* unicode_csbitmaps_section_end __asm("section$end$__UNICODE$__csbitmaps");
extern const void* unicode_properties_section_start __asm("section$start$__UNICODE$__properties");
extern const void* unicode_properties_section_end __asm("section$end$__UNICODE$__properties");
extern const void* unicode_data_section_start __asm("section$start$__UNICODE$__data");
extern const void* unicode_data_section_end __asm("section$end$__UNICODE$__data");
static const void *__CFGetSectDataPtr(const char *segname, const char *sectname, uint64_t *sizep) {
// special case three common sections to have fast access
if ( strcmp(segname, "__UNICODE") == 0 ) {
if ( strcmp(sectname, "__csbitmaps") == 0) {
if (sizep) *sizep = &unicode_csbitmaps_section_end - &unicode_csbitmaps_section_start;
return &unicode_csbitmaps_section_start;
}
else if ( strcmp(sectname, "__properties") == 0 ) {
if (sizep) *sizep = &unicode_properties_section_end - &unicode_properties_section_start;
return &unicode_properties_section_start;
}
else if ( strcmp(sectname, "__data") == 0 ) {
if (sizep) *sizep = &unicode_data_section_end - &unicode_data_section_start;
return &unicode_data_section_start;
}
}
uint32_t idx, cnt = _dyld_image_count();
for (idx = 0; idx < cnt; idx++) {
void *mh = (void *)_dyld_get_image_header(idx);
if (mh != &_mh_dylib_header) continue;
#if TARGET_RT_64_BIT
const struct section_64 *sect = getsectbynamefromheader_64((struct mach_header_64 *)mh, segname, sectname);
#else
const struct section *sect = getsectbynamefromheader((struct mach_header *)mh, segname, sectname);
#endif
if (!sect) break;
if (sizep) *sizep = (uint64_t)sect->size;
return (char *)sect->addr + _dyld_get_image_vmaddr_slide(idx);
}
if (sizep) *sizep = 0ULL;
return NULL;
}
#endif
#if !USE_MACHO_SEGMENT
// Memory map the file
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
CF_INLINE void __CFUniCharCharacterSetPath(char *cpath) {
#elif TARGET_OS_WIN32
CF_INLINE void __CFUniCharCharacterSetPath(wchar_t *wpath) {
#else
#error Unknown or unspecified DEPLOYMENT_TARGET
#endif
#if TARGET_OS_MAC
strlcpy(cpath, __kCFCharacterSetDir, MAXPATHLEN);
#elif TARGET_OS_LINUX || TARGET_OS_BSD
strlcpy(cpath, __kCFCharacterSetDir, MAXPATHLEN);
#elif TARGET_OS_WIN32
wchar_t frameworkPath[MAXPATHLEN];
_CFGetFrameworkPath(frameworkPath, MAXPATHLEN);
wcsncpy(wpath, frameworkPath, MAXPATHLEN);
wcsncat(wpath, L"\\CoreFoundation.resources\\", MAXPATHLEN - wcslen(wpath));
#else
strlcpy(cpath, __kCFCharacterSetDir, MAXPATHLEN);
strlcat(cpath, "/CharacterSets/", MAXPATHLEN);
#endif
}
#if TARGET_OS_WIN32
#define MAX_BITMAP_STATE 512
//
// If a string is placed into this array, then it has been previously
// determined that the bitmap-file cannot be found. Thus, we make
// the assumption it won't be there in future calls and we avoid
// hitting the disk un-necessarily. This assumption isn't 100%
// correct, as bitmap-files can be added. We would have to re-start
// the application in order to pick-up the new bitmap info.
//
// We should probably re-visit this.
//
static wchar_t *mappedBitmapState[MAX_BITMAP_STATE];
static int __nNumStateEntries = -1;
CRITICAL_SECTION __bitmapStateLock = {0};
bool __GetBitmapStateForName(const wchar_t *bitmapName) {
if (NULL == __bitmapStateLock.DebugInfo)
InitializeCriticalSection(&__bitmapStateLock);
EnterCriticalSection(&__bitmapStateLock);
if (__nNumStateEntries >= 0) {
for (int i = 0; i < __nNumStateEntries; i++) {
if (wcscmp(mappedBitmapState[i], bitmapName) == 0) {
LeaveCriticalSection(&__bitmapStateLock);
return true;
}
}
}
LeaveCriticalSection(&__bitmapStateLock);
return false;
}
void __AddBitmapStateForName(const wchar_t *bitmapName) {
if (NULL == __bitmapStateLock.DebugInfo)
InitializeCriticalSection(&__bitmapStateLock);
EnterCriticalSection(&__bitmapStateLock);
__nNumStateEntries++;
mappedBitmapState[__nNumStateEntries] = (wchar_t *)malloc((lstrlenW(bitmapName)+1) * sizeof(wchar_t));
lstrcpyW(mappedBitmapState[__nNumStateEntries], bitmapName);
LeaveCriticalSection(&__bitmapStateLock);
}
#endif
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
static bool __CFUniCharLoadBytesFromFile(const char *fileName, const void **bytes, int64_t *fileSize) {
#elif TARGET_OS_WIN32
static bool __CFUniCharLoadBytesFromFile(const wchar_t *fileName, const void **bytes, int64_t *fileSize) {
#else
#error Unknown or unspecified DEPLOYMENT_TARGET
#endif
#if TARGET_OS_WIN32
HANDLE bitmapFileHandle = NULL;
HANDLE mappingHandle = NULL;
if (__GetBitmapStateForName(fileName)) {
// The fileName has been tried in the past, so just return false
// and move on.
*bytes = NULL;
return false;
}
mappingHandle = OpenFileMappingW(FILE_MAP_READ, TRUE, fileName);
if (NULL == mappingHandle) {
if ((bitmapFileHandle = CreateFileW(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) {
// We tried to get the bitmap file for mapping, but it's not there. Add to list of non-existent bitmap-files so
// we don't have to try this again in the future.
__AddBitmapStateForName(fileName);
return false;
}
mappingHandle = CreateFileMapping(bitmapFileHandle, NULL, PAGE_READONLY, 0, 0, NULL);
CloseHandle(bitmapFileHandle);
if (!mappingHandle) return false;
}
*bytes = MapViewOfFileEx(mappingHandle, FILE_MAP_READ, 0, 0, 0, 0);
if (NULL != fileSize) {
MEMORY_BASIC_INFORMATION memoryInfo;
if (0 == VirtualQueryEx(mappingHandle, *bytes, &memoryInfo, sizeof(memoryInfo))) {
*fileSize = 0; // This indicates no checking. Is it right ?
} else {
*fileSize = memoryInfo.RegionSize;
}
}
CloseHandle(mappingHandle);
return (*bytes ? true : false);
#else
struct stat statBuf;
int fd = -1;
if ((fd = open(fileName, O_RDONLY, 0)) < 0) {
return false;
}
if (fstat(fd, &statBuf) < 0 || (*bytes = mmap(0, statBuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *)-1) {
close(fd);
return false;
}
close(fd);
if (NULL != fileSize) *fileSize = statBuf.st_size;
return true;
#endif
}
#endif // USE_MACHO_SEGMENT
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
#if !defined(CF_UNICHAR_BITMAP_FILE)
#if USE_MACHO_SEGMENT
#define CF_UNICHAR_BITMAP_FILE "__csbitmaps"
#else
#define CF_UNICHAR_BITMAP_FILE "/CFCharacterSetBitmaps.bitmap"
#endif
#endif
#elif TARGET_OS_WIN32
#if !defined(CF_UNICHAR_BITMAP_FILE)
#define CF_UNICHAR_BITMAP_FILE L"CFCharacterSetBitmaps.bitmap"
#endif
#else
#error Unknown or unspecified DEPLOYMENT_TARGET
#endif
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
#if __CF_BIG_ENDIAN__
#if USE_MACHO_SEGMENT
#define MAPPING_TABLE_FILE "__data"
#else
#define MAPPING_TABLE_FILE "/CFUnicodeData-B.mapping"
#endif
#else
#if USE_MACHO_SEGMENT
#define MAPPING_TABLE_FILE "__data"
#else
#define MAPPING_TABLE_FILE "/CFUnicodeData-L.mapping"
#endif
#endif
#elif TARGET_OS_WIN32
#if __CF_BIG_ENDIAN__
#if USE_MACHO_SEGMENT
#define MAPPING_TABLE_FILE "__data"
#else
#define MAPPING_TABLE_FILE L"CFUnicodeData-B.mapping"
#endif
#else
#if USE_MACHO_SEGMENT
#define MAPPING_TABLE_FILE "__data"
#else
#define MAPPING_TABLE_FILE L"CFUnicodeData-L.mapping"
#endif
#endif
#else
#error Unknown or unspecified DEPLOYMENT_TARGET
#endif
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
#if USE_MACHO_SEGMENT
#define PROP_DB_FILE "__properties"
#else
#define PROP_DB_FILE "/CFUniCharPropertyDatabase.data"
#endif
#elif TARGET_OS_WIN32
#if USE_MACHO_SEGMENT
#define PROP_DB_FILE "__properties"
#else
#define PROP_DB_FILE L"CFUniCharPropertyDatabase.data"
#endif
#else
#error Unknown or unspecified DEPLOYMENT_TARGET
#endif
#if __CF_BIG_ENDIAN__
#define CF_UNICODE_DATA_SYM __CFUnicodeDataB
#else
#define CF_UNICODE_DATA_SYM __CFUnicodeDataL
#endif
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
static bool __CFUniCharLoadFile(const char *bitmapName, const void **bytes, int64_t *fileSize) {
#if USE_MACHO_SEGMENT
*bytes = __CFGetSectDataPtr("__UNICODE", bitmapName, NULL);
if (NULL != fileSize) *fileSize = 0;
return *bytes ? true : false;
#elif USE_RAW_SYMBOL
extern void *__CFCharacterSetBitmapData;
extern void *CF_UNICODE_DATA_SYM;
extern void *__CFUniCharPropertyDatabase;
if (strcmp(bitmapName, CF_UNICHAR_BITMAP_FILE) == 0) {
*bytes = &__CFCharacterSetBitmapData;
} else if (strcmp(bitmapName, MAPPING_TABLE_FILE) == 0) {
*bytes = &CF_UNICODE_DATA_SYM;
} else if (strcmp(bitmapName, PROP_DB_FILE) == 0) {
*bytes = &__CFUniCharPropertyDatabase;
}
if (NULL != fileSize) *fileSize = 0;
return *bytes ? true : false;
#else
char cpath[MAXPATHLEN];
__CFUniCharCharacterSetPath(cpath);
strlcat(cpath, bitmapName, MAXPATHLEN);
Boolean needToFree = false;
const char *possiblyFrameworkRootedCPath = CFPathRelativeToAppleFrameworksRoot(cpath, &needToFree);
bool result = __CFUniCharLoadBytesFromFile(possiblyFrameworkRootedCPath, bytes, fileSize);
if (needToFree) free((void *)possiblyFrameworkRootedCPath);
return result;
#endif
}
#elif TARGET_OS_WIN32
static bool __CFUniCharLoadFile(const wchar_t *bitmapName, const void **bytes, int64_t *fileSize) {
#if USE_RAW_SYMBOL
extern void *__CFCharacterSetBitmapData;
extern void *CF_UNICODE_DATA_SYM;
extern void *__CFUniCharPropertyDatabase;
if (wcscmp(bitmapName, CF_UNICHAR_BITMAP_FILE) == 0) {
*bytes = &__CFCharacterSetBitmapData;
} else if (wcscmp(bitmapName, MAPPING_TABLE_FILE) == 0) {
*bytes = &CF_UNICODE_DATA_SYM;
} else if (wcscmp(bitmapName, PROP_DB_FILE) == 0) {
*bytes = &__CFUniCharPropertyDatabase;
}
if (NULL != fileSize) *fileSize = 0;
return *bytes ? true : false;
#else
wchar_t wpath[MAXPATHLEN];
__CFUniCharCharacterSetPath(wpath);
wcsncat(wpath, bitmapName, MAXPATHLEN);
return __CFUniCharLoadBytesFromFile(wpath, bytes, fileSize);
#endif
}
#else
#error Unknown or unspecified DEPLOYMENT_TARGET
#endif
// Bitmap functions
/*
Currently unused but left in for symmetry/informative purposes
CF_INLINE bool isControl(UTF32Char theChar, uint16_t charset, const void *data) { // ISO Control
return (((theChar <= 0x001F) || (theChar >= 0x007F && theChar <= 0x009F)) ? true : false);
}*/
CF_INLINE bool isWhitespace(UTF32Char theChar, uint16_t charset, const void *data) { // Space
return (((theChar == 0x0020) || (theChar == 0x0009) || (theChar == 0x00A0) || (theChar == 0x1680) || (theChar >= 0x2000 && theChar <= 0x200B) || (theChar == 0x202F) || (theChar == 0x205F) || (theChar == 0x3000)) ? true : false);
}
CF_INLINE bool isNewline(UTF32Char theChar, uint16_t charset, const void *data) { // White space
return (((theChar >= 0x000A && theChar <= 0x000D) || (theChar == 0x0085) || (theChar == 0x2028) || (theChar == 0x2029)) ? true : false);
}
CF_INLINE bool isWhitespaceAndNewline(UTF32Char theChar, uint16_t charset, const void *data) { // White space
return ((isWhitespace(theChar, charset, data) || isNewline(theChar, charset, data)) ? true : false);
}
#if USE_MACHO_SEGMENT
CF_INLINE bool __CFSimpleFileSizeVerification(const void *bytes, int64_t fileSize) { return true; }
#elif 1
// <rdar://problem/8961744> __CFSimpleFileSizeVerification is broken
static bool __CFSimpleFileSizeVerification(const void *bytes, int64_t fileSize) { return true; }
#else
static bool __CFSimpleFileSizeVerification(const void *bytes, int64_t fileSize) {
bool result = true;
if (fileSize > 0) {
if ((sizeof(uint32_t) * 2) > fileSize) {
result = false;
} else {
uint32_t headerSize = CFSwapInt32BigToHost(*((uint32_t *)((char *)bytes + 4)));
if ((headerSize < (sizeof(uint32_t) * 4)) || (headerSize > fileSize)) {
result = false;
} else {
const uint32_t *lastElement = (uint32_t *)(((uint8_t *)bytes) + headerSize) - 2;
if ((headerSize + CFSwapInt32BigToHost(lastElement[0]) + CFSwapInt32BigToHost(lastElement[1])) > headerSize) result = false;
}
}
}
if (!result) CFLog(kCFLogLevelCritical, CFSTR("File size verification for Unicode database file failed."));
return result;
}
#endif // USE_MACHO_SEGMENT
typedef struct {
uint32_t _numPlanes;
const uint8_t **_planes;
} __CFUniCharBitmapData;
static char __CFUniCharUnicodeVersionString[8] = {0, 0, 0, 0, 0, 0, 0, 0};
static uint32_t __CFUniCharNumberOfBitmaps = 0;
static __CFUniCharBitmapData *__CFUniCharBitmapDataArray = NULL;
static CFLock_t __CFUniCharBitmapLock = CFLockInit;
static bool __CFUniCharLoadBitmapData(void) {
__CFUniCharBitmapData *array;
uint32_t headerSize;
uint32_t bitmapSize;
int numPlanes;
uint8_t currentPlane;
const void *bytes;
const void *bitmapBase;
const void *bitmap;
int idx, bitmapIndex;
int64_t fileSize;
__CFLock(&__CFUniCharBitmapLock);
if (__CFUniCharBitmapDataArray || !__CFUniCharLoadFile(CF_UNICHAR_BITMAP_FILE, &bytes, &fileSize) || !__CFSimpleFileSizeVerification(bytes, fileSize)) {
__CFUnlock(&__CFUniCharBitmapLock);
return false;
}
for (idx = 0;idx < 4 && ((const uint8_t *)bytes)[idx];idx++) {
__CFUniCharUnicodeVersionString[idx * 2] = ((const uint8_t *)bytes)[idx];
__CFUniCharUnicodeVersionString[idx * 2 + 1] = '.';
}
__CFUniCharUnicodeVersionString[(idx < 4 ? idx * 2 - 1 : 7)] = '\0';
headerSize = CFSwapInt32BigToHost(*((uint32_t *)((char *)bytes + 4)));
bitmapBase = (uint8_t *)bytes + headerSize;
bytes = (uint8_t *)bytes + (sizeof(uint32_t) * 2);
headerSize -= (sizeof(uint32_t) * 2);
__CFUniCharNumberOfBitmaps = headerSize / (sizeof(uint32_t) * 2);
array = (__CFUniCharBitmapData *)CFAllocatorAllocate(kCFAllocatorSystemDefault, sizeof(__CFUniCharBitmapData) * __CFUniCharNumberOfBitmaps, 0);
for (idx = 0;idx < (int)__CFUniCharNumberOfBitmaps;idx++) {
bitmap = (uint8_t *)bitmapBase + CFSwapInt32BigToHost(*((uint32_t *)bytes)); bytes = (uint8_t *)bytes + sizeof(uint32_t);
bitmapSize = CFSwapInt32BigToHost(*((uint32_t *)bytes)); bytes = (uint8_t *)bytes + sizeof(uint32_t);
numPlanes = bitmapSize / (8 * 1024);
numPlanes = *(const uint8_t *)((char *)bitmap + (((numPlanes - 1) * ((8 * 1024) + 1)) - 1)) + 1;
array[idx]._planes = (const uint8_t **)CFAllocatorAllocate(kCFAllocatorSystemDefault, sizeof(const void *) * numPlanes, 0);
array[idx]._numPlanes = numPlanes;
currentPlane = 0;
for (bitmapIndex = 0;bitmapIndex < numPlanes;bitmapIndex++) {
if (bitmapIndex == currentPlane) {
array[idx]._planes[bitmapIndex] = (const uint8_t *)bitmap;
bitmap = (uint8_t *)bitmap + (8 * 1024);
#if defined (__cplusplus)
currentPlane = *(((const uint8_t*&)bitmap)++);
#else
currentPlane = *((const uint8_t *)bitmap++);
#endif
} else {
array[idx]._planes[bitmapIndex] = NULL;
}
}
}
__CFUniCharBitmapDataArray = array;
__CFUnlock(&__CFUniCharBitmapLock);
return true;
}
CF_PRIVATE const char *__CFUniCharGetUnicodeVersionString(void) {
if (NULL == __CFUniCharBitmapDataArray) __CFUniCharLoadBitmapData();
return __CFUniCharUnicodeVersionString;
}
bool CFUniCharIsMemberOf(UTF32Char theChar, uint32_t charset) {
charset = __CFUniCharMapCompatibilitySetID(charset);
switch (charset) {
case kCFUniCharWhitespaceCharacterSet:
return isWhitespace(theChar, charset, NULL);
case kCFUniCharWhitespaceAndNewlineCharacterSet:
return isWhitespaceAndNewline(theChar, charset, NULL);
case kCFUniCharNewlineCharacterSet:
return isNewline(theChar, charset, NULL);
default: {
uint32_t tableIndex = __CFUniCharMapExternalSetToInternalIndex(charset);
if (NULL == __CFUniCharBitmapDataArray) __CFUniCharLoadBitmapData();
if (tableIndex < __CFUniCharNumberOfBitmaps) {
__CFUniCharBitmapData *data = __CFUniCharBitmapDataArray + tableIndex;
uint8_t planeNo = (theChar >> 16) & 0xFF;
// The bitmap data for kCFUniCharIllegalCharacterSet is actually LEGAL set less Plane 14 ~ 16
if (charset == kCFUniCharIllegalCharacterSet) {
if (planeNo == 0x0E) { // Plane 14
theChar &= 0xFF;
return (((theChar == 0x01) || ((theChar > 0x1F) && (theChar < 0x80))) ? false : true);
} else if (planeNo == 0x0F || planeNo == 0x10) { // Plane 15 & 16
return ((theChar & 0xFF) > 0xFFFD ? true : false);
} else {
return (planeNo < data->_numPlanes && data->_planes[planeNo] ? !CFUniCharIsMemberOfBitmap(theChar, data->_planes[planeNo]) : true);
}
} else if (charset == kCFUniCharControlAndFormatterCharacterSet) {
if (planeNo == 0x0E) { // Plane 14
theChar &= 0xFF;
return (((theChar == 0x01) || ((theChar > 0x1F) && (theChar < 0x80))) ? true : false);
} else {
return (planeNo < data->_numPlanes && data->_planes[planeNo] ? CFUniCharIsMemberOfBitmap(theChar, data->_planes[planeNo]) : false);
}
} else {
return (planeNo < data->_numPlanes && data->_planes[planeNo] ? CFUniCharIsMemberOfBitmap(theChar, data->_planes[planeNo]) : false);
}
}
return false;
}
}
}
const uint8_t *CFUniCharGetBitmapPtrForPlane(uint32_t charset, uint32_t plane) {
if (NULL == __CFUniCharBitmapDataArray) __CFUniCharLoadBitmapData();
charset = __CFUniCharMapCompatibilitySetID(charset);
if ((charset > kCFUniCharWhitespaceAndNewlineCharacterSet) && (charset != kCFUniCharIllegalCharacterSet) && (charset != kCFUniCharNewlineCharacterSet)) {
uint32_t tableIndex = __CFUniCharMapExternalSetToInternalIndex(charset);
if (tableIndex < __CFUniCharNumberOfBitmaps) {
__CFUniCharBitmapData *data = __CFUniCharBitmapDataArray + tableIndex;
return (plane < data->_numPlanes ? data->_planes[plane] : NULL);
}
}
return NULL;
}
CF_PRIVATE uint8_t CFUniCharGetBitmapForPlane(uint32_t charset, uint32_t plane, void *bitmap, bool isInverted) {
const uint8_t *src = CFUniCharGetBitmapPtrForPlane(charset, plane);
int numBytes = (8 * 1024);
if (src) {
if (isInverted) {
#if defined (__cplusplus)
while (numBytes-- > 0) *(((uint8_t *&)bitmap)++) = ~(*(src++));
#else
while (numBytes-- > 0) *((uint8_t *)bitmap++) = ~(*(src++));
#endif
} else {
#if defined (__cplusplus)
while (numBytes-- > 0) *(((uint8_t *&)bitmap)++) = *(src++);
#else
while (numBytes-- > 0) *((uint8_t *)bitmap++) = *(src++);
#endif
}
return kCFUniCharBitmapFilled;
} else if (charset == kCFUniCharIllegalCharacterSet) {
__CFUniCharBitmapData *data = __CFUniCharBitmapDataArray + __CFUniCharMapExternalSetToInternalIndex(__CFUniCharMapCompatibilitySetID(charset));
if (plane < data->_numPlanes && (src = data->_planes[plane])) {
if (isInverted) {
#if defined (__cplusplus)
while (numBytes-- > 0) *(((uint8_t *&)bitmap)++) = *(src++);
#else
while (numBytes-- > 0) *((uint8_t *)bitmap++) = *(src++);
#endif
} else {
#if defined (__cplusplus)
while (numBytes-- > 0) *(((uint8_t *&)bitmap)++) = ~(*(src++));
#else
while (numBytes-- > 0) *((uint8_t *)bitmap++) = ~(*(src++));
#endif
}
return kCFUniCharBitmapFilled;
} else if (plane == 0x0E) { // Plane 14
int idx;
uint8_t asciiRange = (isInverted ? (uint8_t)0xFF : (uint8_t)0);
uint8_t otherRange = (isInverted ? (uint8_t)0 : (uint8_t)0xFF);
#if defined (__cplusplus)
*(((uint8_t *&)bitmap)++) = 0x02; // UE0001 LANGUAGE TAG
#else
*((uint8_t *)bitmap++) = 0x02; // UE0001 LANGUAGE TAG
#endif
for (idx = 1;idx < numBytes;idx++) {
#if defined (__cplusplus)
*(((uint8_t *&)bitmap)++) = ((idx >= (0x20 / 8) && (idx < (0x80 / 8))) ? asciiRange : otherRange);
#else
*((uint8_t *)bitmap++) = ((idx >= (0x20 / 8) && (idx < (0x80 / 8))) ? asciiRange : otherRange);
#endif
}
return kCFUniCharBitmapFilled;
} else if (plane == 0x0F || plane == 0x10) { // Plane 15 & 16
uint32_t value = (isInverted ? ~0 : 0);
numBytes /= 4; // for 32bit
while (numBytes-- > 0) {
*((uint32_t *)bitmap) = value;
#if defined (__cplusplus)
bitmap = (uint8_t *)bitmap + sizeof(uint32_t);
#else
bitmap += sizeof(uint32_t);
#endif
}
*(((uint8_t *)bitmap) - 5) = (isInverted ? 0x3F : 0xC0); // 0xFFFE & 0xFFFF
return kCFUniCharBitmapFilled;
}
return (isInverted ? kCFUniCharBitmapEmpty : kCFUniCharBitmapAll);
} else if ((charset < kCFUniCharDecimalDigitCharacterSet) || (charset == kCFUniCharNewlineCharacterSet)) {
if (plane) return (isInverted ? kCFUniCharBitmapAll : kCFUniCharBitmapEmpty);
uint8_t *bitmapBase = (uint8_t *)bitmap;
CFIndex idx;
uint8_t nonFillValue = (isInverted ? (uint8_t)0xFF : (uint8_t)0);
#if defined (__cplusplus)
while (numBytes-- > 0) *(((uint8_t *&)bitmap)++) = nonFillValue;
#else
while (numBytes-- > 0) *((uint8_t *)bitmap++) = nonFillValue;
#endif
if ((charset == kCFUniCharWhitespaceAndNewlineCharacterSet) || (charset == kCFUniCharNewlineCharacterSet)) {
const UniChar newlines[] = {0x000A, 0x000B, 0x000C, 0x000D, 0x0085, 0x2028, 0x2029};
for (idx = 0;idx < (int)(sizeof(newlines) / sizeof(*newlines)); idx++) {
if (isInverted) {
CFUniCharRemoveCharacterFromBitmap(newlines[idx], bitmapBase);
} else {
CFUniCharAddCharacterToBitmap(newlines[idx], bitmapBase);
}
}
if (charset == kCFUniCharNewlineCharacterSet) return kCFUniCharBitmapFilled;
}
if (isInverted) {
CFUniCharRemoveCharacterFromBitmap(0x0009, bitmapBase);
CFUniCharRemoveCharacterFromBitmap(0x0020, bitmapBase);
CFUniCharRemoveCharacterFromBitmap(0x00A0, bitmapBase);
CFUniCharRemoveCharacterFromBitmap(0x1680, bitmapBase);
CFUniCharRemoveCharacterFromBitmap(0x202F, bitmapBase);
CFUniCharRemoveCharacterFromBitmap(0x205F, bitmapBase);
CFUniCharRemoveCharacterFromBitmap(0x3000, bitmapBase);
} else {
CFUniCharAddCharacterToBitmap(0x0009, bitmapBase);
CFUniCharAddCharacterToBitmap(0x0020, bitmapBase);
CFUniCharAddCharacterToBitmap(0x00A0, bitmapBase);
CFUniCharAddCharacterToBitmap(0x1680, bitmapBase);
CFUniCharAddCharacterToBitmap(0x202F, bitmapBase);
CFUniCharAddCharacterToBitmap(0x205F, bitmapBase);
CFUniCharAddCharacterToBitmap(0x3000, bitmapBase);
}
for (idx = 0x2000;idx <= 0x200B;idx++) {
if (isInverted) {
CFUniCharRemoveCharacterFromBitmap(idx, bitmapBase);
} else {
CFUniCharAddCharacterToBitmap(idx, bitmapBase);
}
}
return kCFUniCharBitmapFilled;
}
return (isInverted ? kCFUniCharBitmapAll : kCFUniCharBitmapEmpty);
}
CF_PRIVATE uint32_t CFUniCharGetNumberOfPlanes(uint32_t charset) {
if ((charset == kCFUniCharControlCharacterSet) || (charset == kCFUniCharControlAndFormatterCharacterSet)) {
return 15; // 0 to 14
} else if (charset < kCFUniCharDecimalDigitCharacterSet) {
return 1;
} else if (charset == kCFUniCharIllegalCharacterSet) {
return 17;
} else {
uint32_t numPlanes;
if (NULL == __CFUniCharBitmapDataArray) __CFUniCharLoadBitmapData();
numPlanes = __CFUniCharBitmapDataArray[__CFUniCharMapExternalSetToInternalIndex(__CFUniCharMapCompatibilitySetID(charset))]._numPlanes;
return numPlanes;
}
}
// Mapping data loading
static const void **__CFUniCharMappingTables = NULL;
static CFLock_t __CFUniCharMappingTableLock = CFLockInit;
CF_PRIVATE const void *CFUniCharGetMappingData(uint32_t type) {
__CFLock(&__CFUniCharMappingTableLock);
if (NULL == __CFUniCharMappingTables) {
const void *bytes;
const void *bodyBase;
int headerSize;
int idx, count;
int64_t fileSize;
if (!__CFUniCharLoadFile(MAPPING_TABLE_FILE, &bytes, &fileSize) || !__CFSimpleFileSizeVerification(bytes, fileSize)) {
__CFUnlock(&__CFUniCharMappingTableLock);
return NULL;
}
#if defined (__cplusplus)
bytes = (uint8_t *)bytes + 4; // Skip Unicode version
headerSize = *((uint8_t *)bytes); bytes = (uint8_t *)bytes + sizeof(uint32_t);
#else
bytes += 4; // Skip Unicode version
headerSize = *((uint32_t *)bytes); bytes += sizeof(uint32_t);
#endif
headerSize -= (sizeof(uint32_t) * 2);
bodyBase = (char *)bytes + headerSize;
count = headerSize / sizeof(uint32_t);
__CFUniCharMappingTables = (const void **)CFAllocatorAllocate(kCFAllocatorSystemDefault, sizeof(const void *) * count, 0);
for (idx = 0;idx < count;idx++) {
#if defined (__cplusplus)
__CFUniCharMappingTables[idx] = (char *)bodyBase + *((uint32_t *)bytes); bytes = (uint8_t *)bytes + sizeof(uint32_t);
#else
__CFUniCharMappingTables[idx] = (char *)bodyBase + *((uint32_t *)bytes); bytes += sizeof(uint32_t);
#endif
}
}
__CFUnlock(&__CFUniCharMappingTableLock);
return __CFUniCharMappingTables[type];
}
// Case mapping functions
#define DO_SPECIAL_CASE_MAPPING 1
static uint32_t *__CFUniCharCaseMappingTableCounts = NULL;
static uint32_t **__CFUniCharCaseMappingTable = NULL;
static const uint32_t **__CFUniCharCaseMappingExtraTable = NULL;
typedef struct {
uint32_t _key;
uint32_t _value;
} __CFUniCharCaseMappings;
/* Binary searches CFStringEncodingUnicodeTo8BitCharMap */
static uint32_t __CFUniCharGetMappedCase(const __CFUniCharCaseMappings *theTable, uint32_t numElem, UTF32Char character) {
const __CFUniCharCaseMappings *p, *q, *divider;
if ((character < theTable[0]._key) || (character > theTable[numElem-1]._key)) {
return 0;
}
p = theTable;
q = p + (numElem-1);
while (p <= q) {
divider = p + ((q - p) >> 1); /* divide by 2 */
if (character < divider->_key) { q = divider - 1; }
else if (character > divider->_key) { p = divider + 1; }
else { return divider->_value; }
}
return 0;
}
#define NUM_CASE_MAP_DATA (kCFUniCharCaseFold + 1)
static bool __CFUniCharLoadCaseMappingTable(void) {
uint32_t *countArray;
int idx;
if (NULL == __CFUniCharMappingTables) (void)CFUniCharGetMappingData(kCFUniCharToLowercase);
if (NULL == __CFUniCharMappingTables) return false;
__CFLock(&__CFUniCharMappingTableLock);
if (__CFUniCharCaseMappingTableCounts) {
__CFUnlock(&__CFUniCharMappingTableLock);
return true;
}
countArray = (uint32_t *)CFAllocatorAllocate(kCFAllocatorSystemDefault, sizeof(uint32_t) * NUM_CASE_MAP_DATA + sizeof(uint32_t *) * NUM_CASE_MAP_DATA * 2, 0);
__CFUniCharCaseMappingTable = (uint32_t **)((char *)countArray + sizeof(uint32_t) * NUM_CASE_MAP_DATA);
__CFUniCharCaseMappingExtraTable = (const uint32_t **)__CFUniCharCaseMappingTable + NUM_CASE_MAP_DATA;
for (idx = 0;idx < NUM_CASE_MAP_DATA;idx++) {
countArray[idx] = *((uint32_t *)__CFUniCharMappingTables[idx]) / (sizeof(uint32_t) * 2);
__CFUniCharCaseMappingTable[idx] = ((uint32_t *)__CFUniCharMappingTables[idx]) + 1;
__CFUniCharCaseMappingExtraTable[idx] = (const uint32_t *)((char *)__CFUniCharCaseMappingTable[idx] + *((uint32_t *)__CFUniCharMappingTables[idx]));
}
__CFUniCharCaseMappingTableCounts = countArray;
__CFUnlock(&__CFUniCharMappingTableLock);
return true;
}
#if __CF_BIG_ENDIAN__
#define TURKISH_LANG_CODE (0x7472) // tr
#define LITHUANIAN_LANG_CODE (0x6C74) // lt
#define AZERI_LANG_CODE (0x617A) // az
#define DUTCH_LANG_CODE (0x6E6C) // nl
#define GREEK_LANG_CODE (0x656C) // el
#else
#define TURKISH_LANG_CODE (0x7274) // tr
#define LITHUANIAN_LANG_CODE (0x746C) // lt
#define AZERI_LANG_CODE (0x7A61) // az
#define DUTCH_LANG_CODE (0x6C6E) // nl
#define GREEK_LANG_CODE (0x6C65) // el
#endif
CFIndex CFUniCharMapCaseTo(UTF32Char theChar, UTF16Char *convertedChar, CFIndex maxLength, uint32_t ctype, uint32_t flags, const uint8_t *langCode) {
__CFUniCharBitmapData *data;
uint8_t planeNo = (theChar >> 16) & 0xFF;
caseFoldRetry:
#if DO_SPECIAL_CASE_MAPPING
if (flags & kCFUniCharCaseMapFinalSigma) {
if (theChar == 0x03A3) { // Final sigma
*convertedChar = (ctype == kCFUniCharToLowercase ? 0x03C2 : 0x03A3);
return 1;
}
}
if (langCode) {
if (flags & kCFUniCharCaseMapGreekTonos) { // localized Greek uppercasing
if (theChar == 0x0301) { // GREEK TONOS
return 0;
} else if (theChar == 0x0344) {// COMBINING GREEK DIALYTIKA TONOS
*convertedChar = 0x0308; // COMBINING GREEK DIALYTIKA
return 1;
} else if (CFUniCharIsMemberOf(theChar, kCFUniCharDecomposableCharacterSet)) {
UTF32Char buffer[MAX_DECOMPOSED_LENGTH];
CFIndex length = CFUniCharDecomposeCharacter(theChar, buffer, MAX_DECOMPOSED_LENGTH);
if (length > 1) {
UTF32Char *characters = buffer + 1;
UTF32Char *tail = buffer + length;
while (characters < tail) {
if (*characters == 0x0301) break;
++characters;
}
if (characters < tail) { // found a tonos
CFIndex convertedLength = CFUniCharMapCaseTo(*buffer, convertedChar, maxLength, ctype, 0, langCode);
if (convertedLength == 0) {
*convertedChar = (UTF16Char)*buffer;
convertedLength = 1;
}
characters = buffer + 1;
while (characters < tail) {
if (*characters != 0x0301) { // not tonos
if (*characters < 0x10000) { // BMP
convertedChar[convertedLength] = (UTF16Char)*characters;
++convertedLength;
} else {
UTF32Char character = *characters - 0x10000;
convertedChar[convertedLength++] = (UTF16Char)((character >> 10) + 0xD800UL);
convertedChar[convertedLength++] = (UTF16Char)((character & 0x3FF) + 0xDC00UL);
}
}
++characters;
}
return convertedLength;
}
}
}
}
switch (*(uint16_t *)langCode) {
case LITHUANIAN_LANG_CODE:
if (theChar == 0x0307 && (flags & kCFUniCharCaseMapAfter_i)) {
return 0;
} else if (ctype == kCFUniCharToLowercase) {
if (flags & kCFUniCharCaseMapMoreAbove) {
switch (theChar) {
case 0x0049: // LATIN CAPITAL LETTER I
*(convertedChar++) = 0x0069;
*(convertedChar++) = 0x0307;
return 2;
case 0x004A: // LATIN CAPITAL LETTER J
*(convertedChar++) = 0x006A;
*(convertedChar++) = 0x0307;
return 2;
case 0x012E: // LATIN CAPITAL LETTER I WITH OGONEK
*(convertedChar++) = 0x012F;
*(convertedChar++) = 0x0307;
return 2;
default: break;
}
}
switch (theChar) {
case 0x00CC: // LATIN CAPITAL LETTER I WITH GRAVE
*(convertedChar++) = 0x0069;
*(convertedChar++) = 0x0307;
*(convertedChar++) = 0x0300;
return 3;
case 0x00CD: // LATIN CAPITAL LETTER I WITH ACUTE
*(convertedChar++) = 0x0069;
*(convertedChar++) = 0x0307;
*(convertedChar++) = 0x0301;
return 3;
case 0x0128: // LATIN CAPITAL LETTER I WITH TILDE
*(convertedChar++) = 0x0069;
*(convertedChar++) = 0x0307;
*(convertedChar++) = 0x0303;
return 3;
default: break;
}
}
break;
case TURKISH_LANG_CODE:
case AZERI_LANG_CODE:
if ((theChar == 0x0049) || (theChar == 0x0131)) { // LATIN CAPITAL LETTER I & LATIN SMALL LETTER DOTLESS I
*convertedChar = (((ctype == kCFUniCharToLowercase) || (ctype == kCFUniCharCaseFold)) ? ((kCFUniCharCaseMapMoreAbove & flags) ? 0x0069 : 0x0131) : 0x0049);
return 1;
} else if ((theChar == 0x0069) || (theChar == 0x0130)) { // LATIN SMALL LETTER I & LATIN CAPITAL LETTER I WITH DOT ABOVE
*convertedChar = (((ctype == kCFUniCharToLowercase) || (ctype == kCFUniCharCaseFold)) ? 0x0069 : 0x0130);
return 1;
} else if (theChar == 0x0307 && (kCFUniCharCaseMapAfter_i & flags)) { // COMBINING DOT ABOVE AFTER_i
if (ctype == kCFUniCharToLowercase) {
return 0;
} else {
*convertedChar = 0x0307;
return 1;
}
}
break;
case DUTCH_LANG_CODE:
if ((theChar == 0x004A) || (theChar == 0x006A)) {
*convertedChar = (((ctype == kCFUniCharToUppercase) || (ctype == kCFUniCharToTitlecase) || (kCFUniCharCaseMapDutchDigraph & flags)) ? 0x004A : 0x006A);
return 1;
}
break;
default: break;
}
}
#endif // DO_SPECIAL_CASE_MAPPING
if (NULL == __CFUniCharBitmapDataArray) __CFUniCharLoadBitmapData();
data = __CFUniCharBitmapDataArray + __CFUniCharMapExternalSetToInternalIndex(__CFUniCharMapCompatibilitySetID(ctype + kCFUniCharHasNonSelfLowercaseCharacterSet));
if (planeNo < data->_numPlanes && data->_planes[planeNo] && CFUniCharIsMemberOfBitmap(theChar, data->_planes[planeNo]) && (__CFUniCharCaseMappingTableCounts || __CFUniCharLoadCaseMappingTable())) {
uint32_t value = __CFUniCharGetMappedCase((const __CFUniCharCaseMappings *)__CFUniCharCaseMappingTable[ctype], __CFUniCharCaseMappingTableCounts[ctype], theChar);
if (!value && ctype == kCFUniCharToTitlecase) {
value = __CFUniCharGetMappedCase((const __CFUniCharCaseMappings *)__CFUniCharCaseMappingTable[kCFUniCharToUppercase], __CFUniCharCaseMappingTableCounts[kCFUniCharToUppercase], theChar);
if (value) ctype = kCFUniCharToUppercase;
}
if (value) {