forked from opensource-apple/CF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CFCharacterSet.c
2923 lines (2491 loc) · 137 KB
/
CFCharacterSet.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
/*
* Copyright (c) 2015 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* CFCharacterSet.c
Copyright (c) 1999-2014, Apple Inc. All rights reserved.
Responsibility: Aki Inoue
*/
#include <CoreFoundation/CFCharacterSet.h>
#include <CoreFoundation/CFByteOrder.h>
#include "CFCharacterSetPriv.h"
#include <CoreFoundation/CFData.h>
#include <CoreFoundation/CFString.h>
#include "CFInternal.h"
#include <CoreFoundation/CFUniChar.h>
#include "CFUniCharPriv.h"
#include <stdlib.h>
#include <string.h>
#define BITSPERBYTE 8 /* (CHAR_BIT * sizeof(unsigned char)) */
#define LOG_BPB 3
#define LOG_BPLW 5
#define NUMCHARACTERS 65536
#define MAX_ANNEX_PLANE (16)
/* Number of things in the array keeping the bits.
*/
#define __kCFBitmapSize (NUMCHARACTERS / BITSPERBYTE)
/* How many elements max can be in an __kCFCharSetClassString CFCharacterSet
*/
#define __kCFStringCharSetMax 64
/* The last builtin set ID number
*/
#define __kCFLastBuiltinSetID kCFCharacterSetNewline
/* How many elements in the "singles" array before we use binary search.
*/
#define __kCFSetBreakeven 10
/* This tells us, within 1k or so, whether a thing is POTENTIALLY in the set (in the bitmap blob of the private structure) before we bother to do specific checking.
*/
#define __CFCSetBitsInRange(n, i) (i[n>>15] & (1L << ((n>>10) % 32)))
/* Compact bitmap params
*/
#define __kCFCompactBitmapNumPages (256)
#define __kCFCompactBitmapMaxPages (128) // the max pages allocated
#define __kCFCompactBitmapPageSize (__kCFBitmapSize / __kCFCompactBitmapNumPages)
typedef struct {
CFCharacterSetRef *_nonBMPPlanes;
unsigned int _validEntriesBitmap;
unsigned char _numOfAllocEntries;
unsigned char _isAnnexInverted;
uint16_t _padding;
} CFCharSetAnnexStruct;
struct __CFCharacterSet {
CFRuntimeBase _base;
CFHashCode _hashValue;
union {
struct {
CFIndex _type;
} _builtin;
struct {
UInt32 _firstChar;
CFIndex _length;
} _range;
struct {
UniChar *_buffer;
CFIndex _length;
} _string;
struct {
uint8_t *_bits;
} _bitmap;
struct {
uint8_t *_cBits;
} _compactBitmap;
} _variants;
CFCharSetAnnexStruct *_annex;
};
/* _base._info values interesting for CFCharacterSet
*/
enum {
__kCFCharSetClassTypeMask = 0x0070,
__kCFCharSetClassBuiltin = 0x0000,
__kCFCharSetClassRange = 0x0010,
__kCFCharSetClassString = 0x0020,
__kCFCharSetClassBitmap = 0x0030,
__kCFCharSetClassSet = 0x0040,
__kCFCharSetClassCompactBitmap = 0x0040,
__kCFCharSetIsInvertedMask = 0x0008,
__kCFCharSetIsInverted = 0x0008,
__kCFCharSetHasHashValueMask = 0x00004,
__kCFCharSetHasHashValue = 0x0004,
/* Generic CFBase values */
__kCFCharSetIsMutableMask = 0x0001,
__kCFCharSetIsMutable = 0x0001,
};
/* Inline accessor macros for _base._info
*/
CF_INLINE Boolean __CFCSetIsMutable(CFCharacterSetRef cset) {return (cset->_base._cfinfo[CF_INFO_BITS] & __kCFCharSetIsMutableMask) == __kCFCharSetIsMutable;}
CF_INLINE Boolean __CFCSetIsBuiltin(CFCharacterSetRef cset) {return (cset->_base._cfinfo[CF_INFO_BITS] & __kCFCharSetClassTypeMask) == __kCFCharSetClassBuiltin;}
CF_INLINE Boolean __CFCSetIsRange(CFCharacterSetRef cset) {return (cset->_base._cfinfo[CF_INFO_BITS] & __kCFCharSetClassTypeMask) == __kCFCharSetClassRange;}
CF_INLINE Boolean __CFCSetIsString(CFCharacterSetRef cset) {return (cset->_base._cfinfo[CF_INFO_BITS] & __kCFCharSetClassTypeMask) == __kCFCharSetClassString;}
CF_INLINE Boolean __CFCSetIsBitmap(CFCharacterSetRef cset) {return (cset->_base._cfinfo[CF_INFO_BITS] & __kCFCharSetClassTypeMask) == __kCFCharSetClassBitmap;}
CF_INLINE Boolean __CFCSetIsCompactBitmap(CFCharacterSetRef cset) {return (cset->_base._cfinfo[CF_INFO_BITS] & __kCFCharSetClassTypeMask) == __kCFCharSetClassCompactBitmap;}
CF_INLINE Boolean __CFCSetIsInverted(CFCharacterSetRef cset) {return (cset->_base._cfinfo[CF_INFO_BITS] & __kCFCharSetIsInvertedMask) == __kCFCharSetIsInverted;}
CF_INLINE Boolean __CFCSetHasHashValue(CFCharacterSetRef cset) {return (cset->_base._cfinfo[CF_INFO_BITS] & __kCFCharSetHasHashValueMask) == __kCFCharSetHasHashValue;}
CF_INLINE UInt32 __CFCSetClassType(CFCharacterSetRef cset) {return (cset->_base._cfinfo[CF_INFO_BITS] & __kCFCharSetClassTypeMask);}
CF_INLINE void __CFCSetPutIsMutable(CFMutableCharacterSetRef cset, Boolean isMutable) {(isMutable ? (cset->_base._cfinfo[CF_INFO_BITS] |= __kCFCharSetIsMutable) : (cset->_base._cfinfo[CF_INFO_BITS] &= ~ __kCFCharSetIsMutable));}
CF_INLINE void __CFCSetPutIsInverted(CFMutableCharacterSetRef cset, Boolean isInverted) {(isInverted ? (cset->_base._cfinfo[CF_INFO_BITS] |= __kCFCharSetIsInverted) : (cset->_base._cfinfo[CF_INFO_BITS] &= ~__kCFCharSetIsInverted));}
CF_INLINE void __CFCSetPutHasHashValue(CFMutableCharacterSetRef cset, Boolean hasHash) {(hasHash ? (cset->_base._cfinfo[CF_INFO_BITS] |= __kCFCharSetHasHashValue) : (cset->_base._cfinfo[CF_INFO_BITS] &= ~__kCFCharSetHasHashValue));}
CF_INLINE void __CFCSetPutClassType(CFMutableCharacterSetRef cset, UInt32 classType) {cset->_base._cfinfo[CF_INFO_BITS] &= ~__kCFCharSetClassTypeMask; cset->_base._cfinfo[CF_INFO_BITS] |= classType;}
CF_PRIVATE Boolean __CFCharacterSetIsMutable(CFCharacterSetRef cset) {return __CFCSetIsMutable(cset);}
/* Inline contents accessor macros
*/
CF_INLINE CFCharacterSetPredefinedSet __CFCSetBuiltinType(CFCharacterSetRef cset) {return cset->_variants._builtin._type;}
CF_INLINE UInt32 __CFCSetRangeFirstChar(CFCharacterSetRef cset) {return cset->_variants._range._firstChar;}
CF_INLINE CFIndex __CFCSetRangeLength(CFCharacterSetRef cset) {return cset->_variants._range._length;}
CF_INLINE UniChar *__CFCSetStringBuffer(CFCharacterSetRef cset) {return (UniChar*)(cset->_variants._string._buffer);}
CF_INLINE CFIndex __CFCSetStringLength(CFCharacterSetRef cset) {return cset->_variants._string._length;}
CF_INLINE uint8_t *__CFCSetBitmapBits(CFCharacterSetRef cset) {return cset->_variants._bitmap._bits;}
CF_INLINE uint8_t *__CFCSetCompactBitmapBits(CFCharacterSetRef cset) {return cset->_variants._compactBitmap._cBits;}
CF_INLINE void __CFCSetPutBuiltinType(CFMutableCharacterSetRef cset, CFCharacterSetPredefinedSet type) {cset->_variants._builtin._type = type;}
CF_INLINE void __CFCSetPutRangeFirstChar(CFMutableCharacterSetRef cset, UInt32 first) {cset->_variants._range._firstChar = first;}
CF_INLINE void __CFCSetPutRangeLength(CFMutableCharacterSetRef cset, CFIndex length) {cset->_variants._range._length = length;}
CF_INLINE void __CFCSetPutStringBuffer(CFMutableCharacterSetRef cset, UniChar *theBuffer) {cset->_variants._string._buffer = theBuffer;}
CF_INLINE void __CFCSetPutStringLength(CFMutableCharacterSetRef cset, CFIndex length) {cset->_variants._string._length = length;}
CF_INLINE void __CFCSetPutBitmapBits(CFMutableCharacterSetRef cset, uint8_t *bits) {cset->_variants._bitmap._bits = bits;}
CF_INLINE void __CFCSetPutCompactBitmapBits(CFMutableCharacterSetRef cset, uint8_t *bits) {cset->_variants._compactBitmap._cBits = bits;}
/* Validation funcs
*/
#if defined(CF_ENABLE_ASSERTIONS)
CF_INLINE void __CFCSetValidateBuiltinType(CFCharacterSetPredefinedSet type, const char *func) {
CFAssert2(type > 0 && type <= __kCFLastBuiltinSetID, __kCFLogAssertion, "%s: Unknowen builtin type %d", func, type);
}
CF_INLINE void __CFCSetValidateRange(CFRange theRange, const char *func) {
CFAssert3(theRange.location >= 0 && theRange.location + theRange.length <= 0x1FFFFF, __kCFLogAssertion, "%s: Range out of Unicode range (location -> %d length -> %d)", func, theRange.location, theRange.length);
}
CF_INLINE void __CFCSetValidateTypeAndMutability(CFCharacterSetRef cset, const char *func) {
__CFGenericValidateType(cset, __kCFCharacterSetTypeID);
CFAssert1(__CFCSetIsMutable(cset), __kCFLogAssertion, "%s: Immutable character set passed to mutable function", func);
}
#else
#define __CFCSetValidateBuiltinType(t,f)
#define __CFCSetValidateRange(r,f)
#define __CFCSetValidateTypeAndMutability(r,f)
#endif
/* Inline utility funcs
*/
static Boolean __CFCSetIsEqualBitmap(const UInt32 *bits1, const UInt32 *bits2) {
CFIndex length = __kCFBitmapSize / sizeof(UInt32);
if (bits1 == bits2) {
return true;
} else if (bits1 && bits2) {
if (bits1 == (const UInt32 *)-1) {
while (length--) if ((UInt32)-1 != *bits2++) return false;
} else if (bits2 == (const UInt32 *)-1) {
while (length--) if ((UInt32)-1 != *bits1++) return false;
} else {
while (length--) if (*bits1++ != *bits2++) return false;
}
return true;
} else if (!bits1 && !bits2) { // empty set
return true;
} else {
if (bits2) bits1 = bits2;
if (bits1 == (const UInt32 *)-1) return false;
while (length--) if (*bits1++) return false;
return true;
}
}
CF_INLINE Boolean __CFCSetIsEqualBitmapInverted(const UInt32 *bits1, const UInt32 *bits2) {
CFIndex length = __kCFBitmapSize / sizeof(UInt32);
while (length--) if (*bits1++ != ~(*(bits2++))) return false;
return true;
}
static Boolean __CFCSetIsBitmapEqualToRange(const UInt32 *bits, UniChar firstChar, UniChar lastChar, Boolean isInverted) {
CFIndex firstCharIndex = firstChar >> LOG_BPB;
CFIndex lastCharIndex = lastChar >> LOG_BPB;
CFIndex length;
UInt32 value;
if (firstCharIndex == lastCharIndex) {
value = ((((UInt32)0xFF) << (firstChar & (BITSPERBYTE - 1))) & (((UInt32)0xFF) >> ((BITSPERBYTE - 1) - (lastChar & (BITSPERBYTE - 1))))) << (((sizeof(UInt32) - 1) - (firstCharIndex % sizeof(UInt32))) * BITSPERBYTE);
value = CFSwapInt32HostToBig(value);
firstCharIndex = lastCharIndex = firstChar >> LOG_BPLW;
if (*(bits + firstCharIndex) != (isInverted ? ~value : value)) return FALSE;
} else {
UInt32 firstCharMask;
UInt32 lastCharMask;
length = firstCharIndex % sizeof(UInt32);
firstCharMask = (((((UInt32)0xFF) << (firstChar & (BITSPERBYTE - 1))) & 0xFF) << (((sizeof(UInt32) - 1) - length) * BITSPERBYTE)) | (((UInt32)0xFFFFFFFF) >> ((length + 1) * BITSPERBYTE));
length = lastCharIndex % sizeof(UInt32);
lastCharMask = ((((UInt32)0xFF) >> ((BITSPERBYTE - 1) - (lastChar & (BITSPERBYTE - 1)))) << (((sizeof(UInt32) - 1) - length) * BITSPERBYTE)) | (((UInt32)0xFFFFFFFF) << ((sizeof(UInt32) - length) * BITSPERBYTE));
firstCharIndex = firstChar >> LOG_BPLW;
lastCharIndex = lastChar >> LOG_BPLW;
if (firstCharIndex == lastCharIndex) {
firstCharMask &= lastCharMask;
value = CFSwapInt32HostToBig(firstCharMask & lastCharMask);
if (*(bits + firstCharIndex) != (isInverted ? ~value : value)) return FALSE;
} else {
value = CFSwapInt32HostToBig(firstCharMask);
if (*(bits + firstCharIndex) != (isInverted ? ~value : value)) return FALSE;
value = CFSwapInt32HostToBig(lastCharMask);
if (*(bits + lastCharIndex) != (isInverted ? ~value : value)) return FALSE;
}
}
length = firstCharIndex;
value = (isInverted ? ((UInt32)0xFFFFFFFF) : 0);
while (length--) {
if (*(bits++) != value) return FALSE;
}
++bits; // Skip firstCharIndex
length = (lastCharIndex - (firstCharIndex + 1));
value = (isInverted ? 0 : ((UInt32)0xFFFFFFFF));
while (length-- > 0) {
if (*(bits++) != value) return FALSE;
}
if (firstCharIndex != lastCharIndex) ++bits;
length = (0xFFFF >> LOG_BPLW) - lastCharIndex;
value = (isInverted ? ((UInt32)0xFFFFFFFF) : 0);
while (length--) {
if (*(bits++) != value) return FALSE;
}
return TRUE;
}
CF_INLINE Boolean __CFCSetIsBitmapSupersetOfBitmap(const UInt32 *bits1, const UInt32 *bits2, Boolean isInverted1, Boolean isInverted2) {
CFIndex length = __kCFBitmapSize / sizeof(UInt32);
UInt32 val1, val2;
while (length--) {
val2 = (isInverted2 ? ~(*(bits2++)) : *(bits2++));
val1 = (isInverted1 ? ~(*(bits1++)) : *(bits1++)) & val2;
if (val1 != val2) return false;
}
return true;
}
CF_INLINE Boolean __CFCSetHasNonBMPPlane(CFCharacterSetRef cset) { return ((cset)->_annex && (cset)->_annex->_validEntriesBitmap ? true : false); }
CF_INLINE Boolean __CFCSetAnnexIsInverted (CFCharacterSetRef cset) { return ((cset)->_annex && (cset)->_annex->_isAnnexInverted ? true : false); }
CF_INLINE UInt32 __CFCSetAnnexValidEntriesBitmap(CFCharacterSetRef cset) { return ((cset)->_annex ? (cset)->_annex->_validEntriesBitmap : 0); }
CF_INLINE Boolean __CFCSetIsEmpty(CFCharacterSetRef cset) {
if (__CFCSetHasNonBMPPlane(cset) || __CFCSetAnnexIsInverted(cset)) return false;
switch (__CFCSetClassType(cset)) {
case __kCFCharSetClassRange: if (!__CFCSetRangeLength(cset)) return true; break;
case __kCFCharSetClassString: if (!__CFCSetStringLength(cset)) return true; break;
case __kCFCharSetClassBitmap: if (!__CFCSetBitmapBits(cset)) return true; break;
case __kCFCharSetClassCompactBitmap: if (!__CFCSetCompactBitmapBits(cset)) return true; break;
}
return false;
}
CF_INLINE void __CFCSetBitmapAddCharacter(uint8_t *bitmap, UniChar theChar) {
bitmap[(theChar) >> LOG_BPB] |= (((unsigned)1) << (theChar & (BITSPERBYTE - 1)));
}
CF_INLINE void __CFCSetBitmapRemoveCharacter(uint8_t *bitmap, UniChar theChar) {
bitmap[(theChar) >> LOG_BPB] &= ~(((unsigned)1) << (theChar & (BITSPERBYTE - 1)));
}
CF_INLINE Boolean __CFCSetIsMemberBitmap(const uint8_t *bitmap, UniChar theChar) {
return ((bitmap[(theChar) >> LOG_BPB] & (((unsigned)1) << (theChar & (BITSPERBYTE - 1)))) ? true : false);
}
#define NUM_32BIT_SLOTS (NUMCHARACTERS / 32)
CF_INLINE void __CFCSetBitmapFastFillWithValue(UInt32 *bitmap, uint8_t value) {
UInt32 mask = (value << 24) | (value << 16) | (value << 8) | value;
UInt32 numSlots = NUMCHARACTERS / 32;
while (numSlots--) *(bitmap++) = mask;
}
CF_INLINE void __CFCSetBitmapAddCharactersInRange(uint8_t *bitmap, UniChar firstChar, UniChar lastChar) {
if (firstChar == lastChar) {
bitmap[firstChar >> LOG_BPB] |= (((unsigned)1) << (firstChar & (BITSPERBYTE - 1)));
} else {
UInt32 idx = firstChar >> LOG_BPB;
UInt32 max = lastChar >> LOG_BPB;
if (idx == max) {
bitmap[idx] |= (((unsigned)0xFF) << (firstChar & (BITSPERBYTE - 1))) & (((unsigned)0xFF) >> ((BITSPERBYTE - 1) - (lastChar & (BITSPERBYTE - 1))));
} else {
bitmap[idx] |= (((unsigned)0xFF) << (firstChar & (BITSPERBYTE - 1)));
bitmap[max] |= (((unsigned)0xFF) >> ((BITSPERBYTE - 1) - (lastChar & (BITSPERBYTE - 1))));
++idx;
while (idx < max) bitmap[idx++] = 0xFF;
}
}
}
CF_INLINE void __CFCSetBitmapRemoveCharactersInRange(uint8_t *bitmap, UniChar firstChar, UniChar lastChar) {
UInt32 idx = firstChar >> LOG_BPB;
UInt32 max = lastChar >> LOG_BPB;
if (idx == max) {
bitmap[idx] &= ~((((unsigned)0xFF) << (firstChar & (BITSPERBYTE - 1))) & (((unsigned)0xFF) >> ((BITSPERBYTE - 1) - (lastChar & (BITSPERBYTE - 1)))));
} else {
bitmap[idx] &= ~(((unsigned)0xFF) << (firstChar & (BITSPERBYTE - 1)));
bitmap[max] &= ~(((unsigned)0xFF) >> ((BITSPERBYTE - 1) - (lastChar & (BITSPERBYTE - 1))));
++idx;
while (idx < max) bitmap[idx++] = 0;
}
}
#define __CFCSetAnnexBitmapSetPlane(bitmap,plane) ((bitmap) |= (1 << (plane)))
#define __CFCSetAnnexBitmapClearPlane(bitmap,plane) ((bitmap) &= (~(1 << (plane))))
#define __CFCSetAnnexBitmapGetPlane(bitmap,plane) ((bitmap) & (1 << (plane)))
CF_INLINE void __CFCSetAllocateAnnexForPlane(CFCharacterSetRef cset, int plane) {
if (cset->_annex == NULL) {
((CFMutableCharacterSetRef)cset)->_annex = (CFCharSetAnnexStruct *)CFAllocatorAllocate(CFGetAllocator(cset), sizeof(CFCharSetAnnexStruct), 0);
cset->_annex->_numOfAllocEntries = plane;
cset->_annex->_isAnnexInverted = false;
cset->_annex->_validEntriesBitmap = 0;
cset->_annex->_nonBMPPlanes = ((plane > 0) ? (CFCharacterSetRef*)CFAllocatorAllocate(CFGetAllocator(cset), sizeof(CFCharacterSetRef) * plane, 0) : NULL);
} else if (cset->_annex->_numOfAllocEntries < plane) {
cset->_annex->_numOfAllocEntries = plane;
if (NULL == cset->_annex->_nonBMPPlanes) {
cset->_annex->_nonBMPPlanes = (CFCharacterSetRef*)CFAllocatorAllocate(CFGetAllocator(cset), sizeof(CFCharacterSetRef) * plane, 0);
} else {
cset->_annex->_nonBMPPlanes = (CFCharacterSetRef*)CFAllocatorReallocate(CFGetAllocator(cset), (void *)cset->_annex->_nonBMPPlanes, sizeof(CFCharacterSetRef) * plane, 0);
}
}
}
CF_INLINE void __CFCSetAnnexSetIsInverted(CFCharacterSetRef cset, Boolean flag) {
if (flag) __CFCSetAllocateAnnexForPlane(cset, 0);
if (cset->_annex) ((CFMutableCharacterSetRef)cset)->_annex->_isAnnexInverted = flag;
}
CF_INLINE void __CFCSetPutCharacterSetToAnnexPlane(CFCharacterSetRef cset, CFCharacterSetRef annexCSet, int plane) {
__CFCSetAllocateAnnexForPlane(cset, plane);
if (__CFCSetAnnexBitmapGetPlane(cset->_annex->_validEntriesBitmap, plane)) CFRelease(cset->_annex->_nonBMPPlanes[plane - 1]);
if (annexCSet) {
cset->_annex->_nonBMPPlanes[plane - 1] = (CFCharacterSetRef)CFRetain(annexCSet);
__CFCSetAnnexBitmapSetPlane(cset->_annex->_validEntriesBitmap, plane);
} else {
__CFCSetAnnexBitmapClearPlane(cset->_annex->_validEntriesBitmap, plane);
}
}
CF_INLINE CFCharacterSetRef __CFCSetGetAnnexPlaneCharacterSet(CFCharacterSetRef cset, int plane) {
__CFCSetAllocateAnnexForPlane(cset, plane);
if (!__CFCSetAnnexBitmapGetPlane(cset->_annex->_validEntriesBitmap, plane)) {
cset->_annex->_nonBMPPlanes[plane - 1] = (CFCharacterSetRef)CFCharacterSetCreateMutable(CFGetAllocator(cset));
__CFCSetAnnexBitmapSetPlane(cset->_annex->_validEntriesBitmap, plane);
}
return cset->_annex->_nonBMPPlanes[plane - 1];
}
CF_INLINE CFCharacterSetRef __CFCSetGetAnnexPlaneCharacterSetNoAlloc(CFCharacterSetRef cset, int plane) {
return (cset->_annex && __CFCSetAnnexBitmapGetPlane(cset->_annex->_validEntriesBitmap, plane) ? cset->_annex->_nonBMPPlanes[plane - 1] : NULL);
}
CF_INLINE void __CFCSetDeallocateAnnexPlane(CFCharacterSetRef cset) {
if (cset->_annex) {
int idx;
for (idx = 0;idx < MAX_ANNEX_PLANE;idx++) {
if (__CFCSetAnnexBitmapGetPlane(cset->_annex->_validEntriesBitmap, idx + 1)) {
CFRelease(cset->_annex->_nonBMPPlanes[idx]);
}
}
CFAllocatorDeallocate(CFGetAllocator(cset), cset->_annex->_nonBMPPlanes);
CFAllocatorDeallocate(CFGetAllocator(cset), cset->_annex);
((CFMutableCharacterSetRef)cset)->_annex = NULL;
}
}
CF_INLINE uint8_t __CFCSetGetHeaderValue(const uint8_t *bitmap, int *numPages) {
uint8_t value = *bitmap;
if ((value == 0) || (value == UINT8_MAX)) {
int numBytes = __kCFCompactBitmapPageSize - 1;
while (numBytes > 0) {
if (*(++bitmap) != value) break;
--numBytes;
}
if (numBytes == 0) return value;
}
return (uint8_t)(++(*numPages));
}
CF_INLINE bool __CFCSetIsMemberInCompactBitmap(const uint8_t *compactBitmap, UTF16Char character) {
uint8_t value = compactBitmap[(character >> 8)]; // Assuming __kCFCompactBitmapNumPages == 256
if (value == 0) {
return false;
} else if (value == UINT8_MAX) {
return true;
} else {
compactBitmap += (__kCFCompactBitmapNumPages + (__kCFCompactBitmapPageSize * (value - 1)));
character &= 0xFF; // Assuming __kCFCompactBitmapNumPages == 256
return ((compactBitmap[(character / BITSPERBYTE)] & (1 << (character % BITSPERBYTE))) ? true : false);
}
}
CF_INLINE uint32_t __CFCSetGetCompactBitmapSize(const uint8_t *compactBitmap) {
uint32_t length = __kCFCompactBitmapNumPages;
uint32_t size = __kCFCompactBitmapNumPages;
uint8_t value;
while (length-- > 0) {
value = *(compactBitmap++);
if ((value != 0) && (value != UINT8_MAX)) size += __kCFCompactBitmapPageSize;
}
return size;
}
CF_INLINE void __CFExpandCompactBitmap(const uint8_t *src, uint8_t *dst) {
const uint8_t *srcBody = src + __kCFCompactBitmapNumPages;
int i;
uint8_t value;
for (i = 0;i < __kCFCompactBitmapNumPages;i++) {
value = *(src++);
if ((value == 0) || (value == UINT8_MAX)) {
memset(dst, value, __kCFCompactBitmapPageSize);
} else {
memmove(dst, srcBody, __kCFCompactBitmapPageSize);
srcBody += __kCFCompactBitmapPageSize;
}
dst += __kCFCompactBitmapPageSize;
}
}
static void __CFCheckForExpandedSet(CFCharacterSetRef cset) {
static int8_t __CFNumberOfPlanesForLogging = -1;
static bool warnedOnce = false;
if (0 > __CFNumberOfPlanesForLogging) {
const char *envVar = __CFgetenv("CFCharacterSetCheckForExpandedSet");
long value = (envVar ? strtol_l(envVar, NULL, 0, NULL) : 0);
__CFNumberOfPlanesForLogging = (int8_t)(((value > 0) && (value <= 16)) ? value : 0);
}
if (__CFNumberOfPlanesForLogging) {
uint32_t entries = __CFCSetAnnexValidEntriesBitmap(cset);
int count = 0;
while (entries) {
if ((entries & 1) && (++count >= __CFNumberOfPlanesForLogging)) {
if (!warnedOnce) {
CFLog(kCFLogLevelWarning, CFSTR("An expanded CFMutableCharacter has been detected. Recommend to compact with CFCharacterSetCreateCopy"));
warnedOnce = true;
}
break;
}
entries >>= 1;
}
}
}
static void __CFCSetGetBitmap(CFCharacterSetRef cset, uint8_t *bits) {
uint8_t *bitmap;
CFIndex length = __kCFBitmapSize;
if (__CFCSetIsBitmap(cset) && (bitmap = __CFCSetBitmapBits(cset))) {
memmove(bits, bitmap, __kCFBitmapSize);
} else {
Boolean isInverted = __CFCSetIsInverted(cset);
uint8_t value = (isInverted ? (uint8_t)-1 : 0);
bitmap = bits;
while (length--) *bitmap++ = value; // Initialize the buffer
if (!__CFCSetIsEmpty(cset)) {
switch (__CFCSetClassType(cset)) {
case __kCFCharSetClassBuiltin: {
UInt8 result = CFUniCharGetBitmapForPlane(__CFCSetBuiltinType(cset), 0, bits, (isInverted != 0));
if (result == kCFUniCharBitmapEmpty && isInverted) {
length = __kCFBitmapSize;
bitmap = bits;
while (length--) *bitmap++ = 0;
} else if (result == kCFUniCharBitmapAll && !isInverted) {
length = __kCFBitmapSize;
bitmap = bits;
while (length--) *bitmap++ = (UInt8)0xFF;
}
}
break;
case __kCFCharSetClassRange: {
UInt32 theChar = __CFCSetRangeFirstChar(cset);
if (theChar < NUMCHARACTERS) { // the range starts in BMP
length = __CFCSetRangeLength(cset);
if (theChar + length >= NUMCHARACTERS) length = NUMCHARACTERS - theChar;
if (isInverted) {
__CFCSetBitmapRemoveCharactersInRange(bits, theChar, (UniChar)(theChar + length) - 1);
} else {
__CFCSetBitmapAddCharactersInRange(bits, theChar, (UniChar)(theChar + length) - 1);
}
}
}
break;
case __kCFCharSetClassString: {
const UniChar *buffer = __CFCSetStringBuffer(cset);
length = __CFCSetStringLength(cset);
while (length--) (isInverted ? __CFCSetBitmapRemoveCharacter(bits, *buffer++) : __CFCSetBitmapAddCharacter(bits, *buffer++));
}
break;
case __kCFCharSetClassCompactBitmap:
__CFExpandCompactBitmap(__CFCSetCompactBitmapBits(cset), bits);
break;
}
}
}
}
static Boolean __CFCharacterSetEqual(CFTypeRef cf1, CFTypeRef cf2);
static Boolean __CFCSetIsEqualAnnex(CFCharacterSetRef cf1, CFCharacterSetRef cf2) {
CFCharacterSetRef subSet1;
CFCharacterSetRef subSet2;
Boolean isAnnexInvertStateIdentical = (__CFCSetAnnexIsInverted(cf1) == __CFCSetAnnexIsInverted(cf2) ? true: false);
int idx;
if (isAnnexInvertStateIdentical) {
if (__CFCSetAnnexValidEntriesBitmap(cf1) != __CFCSetAnnexValidEntriesBitmap(cf2)) return false;
for (idx = 1;idx <= MAX_ANNEX_PLANE;idx++) {
subSet1 = __CFCSetGetAnnexPlaneCharacterSetNoAlloc(cf1, idx);
subSet2 = __CFCSetGetAnnexPlaneCharacterSetNoAlloc(cf2, idx);
if (subSet1 && !__CFCharacterSetEqual(subSet1, subSet2)) return false;
}
} else {
uint8_t bitsBuf[__kCFBitmapSize];
uint8_t bitsBuf2[__kCFBitmapSize];
for (idx = 1;idx <= MAX_ANNEX_PLANE;idx++) {
subSet1 = __CFCSetGetAnnexPlaneCharacterSetNoAlloc(cf1, idx);
subSet2 = __CFCSetGetAnnexPlaneCharacterSetNoAlloc(cf2, idx);
if (subSet1 == NULL && subSet2 == NULL) {
return false;
} else if (subSet1 == NULL) {
if (__CFCSetIsBitmap(subSet2)) {
if (!__CFCSetIsEqualBitmap((const UInt32 *)__CFCSetBitmapBits(subSet2), (const UInt32 *)-1)) {
return false;
}
} else {
__CFCSetGetBitmap(subSet2, bitsBuf);
if (!__CFCSetIsEqualBitmap((const UInt32 *)bitsBuf, (const UInt32 *)-1)) {
return false;
}
}
} else if (subSet2 == NULL) {
if (__CFCSetIsBitmap(subSet1)) {
if (!__CFCSetIsEqualBitmap((const UInt32 *)__CFCSetBitmapBits(subSet1), (const UInt32 *)-1)) {
return false;
}
} else {
__CFCSetGetBitmap(subSet1, bitsBuf);
if (!__CFCSetIsEqualBitmap((const UInt32 *)bitsBuf, (const UInt32 *)-1)) {
return false;
}
}
} else {
Boolean isBitmap1 = __CFCSetIsBitmap(subSet1);
Boolean isBitmap2 = __CFCSetIsBitmap(subSet2);
if (isBitmap1 && isBitmap2) {
if (!__CFCSetIsEqualBitmapInverted((const UInt32 *)__CFCSetBitmapBits(subSet1), (const UInt32 *)__CFCSetBitmapBits(subSet2))) {
return false;
}
} else if (!isBitmap1 && !isBitmap2) {
__CFCSetGetBitmap(subSet1, bitsBuf);
__CFCSetGetBitmap(subSet2, bitsBuf2);
if (!__CFCSetIsEqualBitmapInverted((const UInt32 *)bitsBuf, (const UInt32 *)bitsBuf2)) {
return false;
}
} else {
if (isBitmap2) {
CFCharacterSetRef tmp = subSet2;
subSet2 = subSet1;
subSet1 = tmp;
}
__CFCSetGetBitmap(subSet2, bitsBuf);
if (!__CFCSetIsEqualBitmapInverted((const UInt32 *)__CFCSetBitmapBits(subSet1), (const UInt32 *)bitsBuf)) {
return false;
}
}
}
}
}
return true;
}
/* Compact bitmap
*/
static uint8_t *__CFCreateCompactBitmap(CFAllocatorRef allocator, const uint8_t *bitmap) {
const uint8_t *src;
uint8_t *dst;
int i;
int numPages = 0;
uint8_t header[__kCFCompactBitmapNumPages];
src = bitmap;
for (i = 0;i < __kCFCompactBitmapNumPages;i++) {
header[i] = __CFCSetGetHeaderValue(src, &numPages);
// Allocating more pages is probably not interesting enough to be compact
if (numPages > __kCFCompactBitmapMaxPages) return NULL;
src += __kCFCompactBitmapPageSize;
}
dst = (uint8_t *)CFAllocatorAllocate(allocator, __kCFCompactBitmapNumPages + (__kCFCompactBitmapPageSize * numPages), 0);
if (numPages > 0) {
uint8_t *dstBody = dst + __kCFCompactBitmapNumPages;
src = bitmap;
for (i = 0;i < __kCFCompactBitmapNumPages;i++) {
dst[i] = header[i];
if ((dst[i] != 0) && (dst[i] != UINT8_MAX)) {
memmove(dstBody, src, __kCFCompactBitmapPageSize);
dstBody += __kCFCompactBitmapPageSize;
}
src += __kCFCompactBitmapPageSize;
}
} else {
memmove(dst, header, __kCFCompactBitmapNumPages);
}
return dst;
}
static void __CFCSetMakeCompact(CFMutableCharacterSetRef cset) {
if (__CFCSetIsBitmap(cset) && __CFCSetBitmapBits(cset)) {
uint8_t *bitmap = __CFCSetBitmapBits(cset);
uint8_t *cBitmap = __CFCreateCompactBitmap(CFGetAllocator(cset), bitmap);
if (cBitmap) {
CFAllocatorDeallocate(CFGetAllocator(cset), bitmap);
__CFCSetPutClassType(cset, __kCFCharSetClassCompactBitmap);
__CFCSetPutCompactBitmapBits(cset, cBitmap);
}
}
}
static void __CFCSetAddNonBMPPlanesInRange(CFMutableCharacterSetRef cset, CFRange range) {
int firstChar = (range.location & 0xFFFF);
int maxChar = range.location + range.length;
int idx = range.location >> 16; // first plane
int maxPlane = (maxChar - 1) >> 16; // last plane
CFRange planeRange;
CFMutableCharacterSetRef annexPlane;
maxChar &= 0xFFFF;
for (idx = (idx ? idx : 1);idx <= maxPlane;idx++) {
planeRange.location = __CFMax(firstChar, 0);
planeRange.length = (idx == maxPlane && maxChar ? maxChar : 0x10000) - planeRange.location;
if (__CFCSetAnnexIsInverted(cset)) {
if ((annexPlane = (CFMutableCharacterSetRef)__CFCSetGetAnnexPlaneCharacterSetNoAlloc(cset, idx))) {
CFCharacterSetRemoveCharactersInRange(annexPlane, planeRange);
if (__CFCSetIsEmpty(annexPlane) && !__CFCSetIsInverted(annexPlane)) {
CFRelease(annexPlane);
__CFCSetAnnexBitmapClearPlane(cset->_annex->_validEntriesBitmap, idx);
}
}
} else {
CFCharacterSetAddCharactersInRange((CFMutableCharacterSetRef)__CFCSetGetAnnexPlaneCharacterSet(cset, idx), planeRange);
}
}
if (!__CFCSetHasNonBMPPlane(cset) && !__CFCSetAnnexIsInverted(cset)) __CFCSetDeallocateAnnexPlane(cset);
}
static void __CFCSetRemoveNonBMPPlanesInRange(CFMutableCharacterSetRef cset, CFRange range) {
int firstChar = (range.location & 0xFFFF);
int maxChar = range.location + range.length;
int idx = range.location >> 16; // first plane
int maxPlane = (maxChar - 1) >> 16; // last plane
CFRange planeRange;
CFMutableCharacterSetRef annexPlane;
maxChar &= 0xFFFF;
for (idx = (idx ? idx : 1);idx <= maxPlane;idx++) {
planeRange.location = __CFMax(firstChar, 0);
planeRange.length = (idx == maxPlane && maxChar ? maxChar : 0x10000) - planeRange.location;
if (__CFCSetAnnexIsInverted(cset)) {
CFCharacterSetAddCharactersInRange((CFMutableCharacterSetRef)__CFCSetGetAnnexPlaneCharacterSet(cset, idx), planeRange);
} else {
if ((annexPlane = (CFMutableCharacterSetRef)__CFCSetGetAnnexPlaneCharacterSetNoAlloc(cset, idx))) {
CFCharacterSetRemoveCharactersInRange(annexPlane, planeRange);
if(__CFCSetIsEmpty(annexPlane) && !__CFCSetIsInverted(annexPlane)) {
CFRelease(annexPlane);
__CFCSetAnnexBitmapClearPlane(cset->_annex->_validEntriesBitmap, idx);
}
}
}
}
if (!__CFCSetHasNonBMPPlane(cset) && !__CFCSetAnnexIsInverted(cset)) __CFCSetDeallocateAnnexPlane(cset);
}
static void __CFCSetMakeBitmap(CFMutableCharacterSetRef cset) {
if (!__CFCSetIsBitmap(cset) || !__CFCSetBitmapBits(cset)) {
CFAllocatorRef allocator = CFGetAllocator(cset);
uint8_t *bitmap = (uint8_t *)CFAllocatorAllocate(allocator, __kCFBitmapSize, 0);
__CFCSetGetBitmap(cset, bitmap);
if (__CFCSetIsBuiltin(cset)) {
CFIndex numPlanes = CFUniCharGetNumberOfPlanes(__CFCSetBuiltinType(cset));
if (numPlanes > 1) {
CFMutableCharacterSetRef annexSet;
uint8_t *annexBitmap = NULL;
int idx;
UInt8 result;
__CFCSetAllocateAnnexForPlane(cset, numPlanes - 1);
for (idx = 1;idx < numPlanes;idx++) {
if (NULL == annexBitmap) {
annexBitmap = (uint8_t *)CFAllocatorAllocate(allocator, __kCFBitmapSize, 0);
}
result = CFUniCharGetBitmapForPlane(__CFCSetBuiltinType(cset), idx, annexBitmap, false);
if (result == kCFUniCharBitmapEmpty) continue;
if (result == kCFUniCharBitmapAll) {
CFIndex bitmapLength = __kCFBitmapSize;
uint8_t *bytes = annexBitmap;
while (bitmapLength-- > 0) *(bytes++) = (uint8_t)0xFF;
}
annexSet = (CFMutableCharacterSetRef)__CFCSetGetAnnexPlaneCharacterSet(cset, idx);
__CFCSetPutClassType(annexSet, __kCFCharSetClassBitmap);
__CFCSetPutBitmapBits(annexSet, annexBitmap);
__CFCSetPutIsInverted(annexSet, false);
__CFCSetPutHasHashValue(annexSet, false);
annexBitmap = NULL;
}
if (annexBitmap) CFAllocatorDeallocate(allocator, annexBitmap);
}
} else if (__CFCSetIsCompactBitmap(cset) && __CFCSetCompactBitmapBits(cset)) {
CFAllocatorDeallocate(allocator, __CFCSetCompactBitmapBits(cset));
__CFCSetPutCompactBitmapBits(cset, NULL);
} else if (__CFCSetIsString(cset) && __CFCSetStringBuffer(cset)) {
CFAllocatorDeallocate(allocator, __CFCSetStringBuffer(cset));
__CFCSetPutStringBuffer(cset, NULL);
} else if (__CFCSetIsRange(cset)) { // We may have to allocate annex here
Boolean needsToInvert = (!__CFCSetHasNonBMPPlane(cset) && __CFCSetIsInverted(cset) ? true : false);
__CFCSetAddNonBMPPlanesInRange(cset, CFRangeMake(__CFCSetRangeFirstChar(cset), __CFCSetRangeLength(cset)));
if (needsToInvert) __CFCSetAnnexSetIsInverted(cset, true);
}
__CFCSetPutClassType(cset, __kCFCharSetClassBitmap);
__CFCSetPutBitmapBits(cset, bitmap);
__CFCSetPutIsInverted(cset, false);
}
}
CF_INLINE CFMutableCharacterSetRef __CFCSetGenericCreate(CFAllocatorRef allocator, UInt32 flags) {
CFMutableCharacterSetRef cset;
CFIndex size = sizeof(struct __CFCharacterSet) - sizeof(CFRuntimeBase);
cset = (CFMutableCharacterSetRef)_CFRuntimeCreateInstance(allocator, CFCharacterSetGetTypeID(), size, NULL);
if (NULL == cset) return NULL;
cset->_base._cfinfo[CF_INFO_BITS] |= flags;
cset->_hashValue = 0;
cset->_annex = NULL;
return cset;
}
static void __CFApplySurrogatesInString(CFMutableCharacterSetRef cset, CFStringRef string, void (*applyer)(CFMutableCharacterSetRef, CFRange)) {
CFStringInlineBuffer buffer;
CFIndex index, length = CFStringGetLength(string);
CFRange range = CFRangeMake(0, 0);
UTF32Char character;
CFStringInitInlineBuffer(string, &buffer, CFRangeMake(0, length));
for (index = 0;index < length;index++) {
character = __CFStringGetCharacterFromInlineBufferQuick(&buffer, index);
if (CFStringIsSurrogateHighCharacter(character) && ((index + 1) < length)) {
UTF16Char other = __CFStringGetCharacterFromInlineBufferQuick(&buffer, index + 1);
if (CFStringIsSurrogateLowCharacter(other)) {
character = CFStringGetLongCharacterForSurrogatePair(character, other);
if ((range.length + range.location) == character) {
++range.length;
} else {
if (range.length > 0) applyer(cset, range);
range.location = character;
range.length = 1;
}
}
++index; // skip the low surrogate
}
}
if (range.length > 0) applyer(cset, range);
}
/* Bsearch theChar for __kCFCharSetClassString
*/
CF_INLINE Boolean __CFCSetBsearchUniChar(const UniChar *theTable, CFIndex length, UniChar theChar) {
const UniChar *p, *q, *divider;
if ((theChar < theTable[0]) || (theChar > theTable[length - 1])) return false;
p = theTable;
q = p + (length - 1);
while (p <= q) {
divider = p + ((q - p) >> 1); /* divide by 2 */
if (theChar < *divider) q = divider - 1;
else if (theChar > *divider) p = divider + 1;
else return true;
}
return false;
}
/* Array of instantiated builtin set. Note builtin set ID starts with 1 so the array index is ID - 1
*/
static CFCharacterSetRef *__CFBuiltinSets = NULL;
/* Global lock for character set
*/
static OSSpinLock __CFCharacterSetLock = OS_SPINLOCK_INIT;
/* CFBase API functions
*/
static Boolean __CFCharacterSetEqual(CFTypeRef cf1, CFTypeRef cf2) {
Boolean isInvertStateIdentical = (__CFCSetIsInverted((CFCharacterSetRef)cf1) == __CFCSetIsInverted((CFCharacterSetRef)cf2) ? true: false);
Boolean isAnnexInvertStateIdentical = (__CFCSetAnnexIsInverted((CFCharacterSetRef)cf1) == __CFCSetAnnexIsInverted((CFCharacterSetRef)cf2) ? true: false);
CFIndex idx;
CFCharacterSetRef subSet1;
uint8_t bitsBuf[__kCFBitmapSize];
uint8_t *bits;
Boolean isBitmap1;
Boolean isBitmap2;
if (__CFCSetHasHashValue((CFCharacterSetRef)cf1) && __CFCSetHasHashValue((CFCharacterSetRef)cf2) && ((CFCharacterSetRef)cf1)->_hashValue != ((CFCharacterSetRef)cf2)->_hashValue) return false;
if (__CFCSetIsEmpty((CFCharacterSetRef)cf1) && __CFCSetIsEmpty((CFCharacterSetRef)cf2) && !isInvertStateIdentical) return false;
if ((__CFCSetClassType((CFCharacterSetRef)cf1) == __CFCSetClassType((CFCharacterSetRef)cf2)) && !__CFCSetIsCompactBitmap((CFCharacterSetRef)cf1)) { // Types are identical, we can do it fast
switch (__CFCSetClassType((CFCharacterSetRef)cf1)) {
case __kCFCharSetClassBuiltin:
return (__CFCSetBuiltinType((CFCharacterSetRef)cf1) == __CFCSetBuiltinType((CFCharacterSetRef)cf2) && isInvertStateIdentical ? true : false);
case __kCFCharSetClassRange:
return (__CFCSetRangeFirstChar((CFCharacterSetRef)cf1) == __CFCSetRangeFirstChar((CFCharacterSetRef)cf2) && __CFCSetRangeLength((CFCharacterSetRef)cf1) && __CFCSetRangeLength((CFCharacterSetRef)cf2) && isInvertStateIdentical ? true : false);
case __kCFCharSetClassString:
if (isInvertStateIdentical) {
const UniChar *buf1 = __CFCSetStringBuffer((CFCharacterSetRef)cf1);
const UniChar *buf1End = buf1 + __CFCSetStringLength((CFCharacterSetRef)cf1);
const UniChar *buf2 = __CFCSetStringBuffer((CFCharacterSetRef)cf2);
const UniChar *buf2End = buf2 + __CFCSetStringLength((CFCharacterSetRef)cf2);
while ((buf1 < buf1End) && (buf2 < buf2End)) {
UniChar char1 = *buf1;
UniChar char2 = *buf2;
if (char1 != char2) return false;
do { ++buf1; } while ((buf1 < buf1End) && (char1 == *buf1));
do { ++buf2; } while ((buf2 < buf2End) && (char2 == *buf2));
}
} else {
return false;
}
break;
case __kCFCharSetClassBitmap:
if (!__CFCSetIsEqualBitmap((const UInt32 *)__CFCSetBitmapBits((CFCharacterSetRef)cf1), (const UInt32 *)__CFCSetBitmapBits((CFCharacterSetRef)cf2))) return false;
break;
}
return __CFCSetIsEqualAnnex((CFCharacterSetRef)cf1, (CFCharacterSetRef)cf2);
}
// Check for easy empty cases
if (__CFCSetIsEmpty((CFCharacterSetRef)cf1) || __CFCSetIsEmpty((CFCharacterSetRef)cf2)) {
CFCharacterSetRef emptySet = (__CFCSetIsEmpty((CFCharacterSetRef)cf1) ? (CFCharacterSetRef)cf1 : (CFCharacterSetRef)cf2);
CFCharacterSetRef nonEmptySet = (emptySet == cf1 ? (CFCharacterSetRef)cf2 : (CFCharacterSetRef)cf1);
if (__CFCSetIsBuiltin(nonEmptySet)) {
return false;
} else if (__CFCSetIsRange(nonEmptySet)) {
if (isInvertStateIdentical) {
return (__CFCSetRangeLength(nonEmptySet) ? false : true);
} else {
return (__CFCSetRangeLength(nonEmptySet) == 0x110000 ? true : false);
}
} else {
if (__CFCSetAnnexIsInverted(nonEmptySet)) {
if (__CFCSetAnnexValidEntriesBitmap(nonEmptySet) != 0x1FFFE) return false;
} else {
if (__CFCSetAnnexValidEntriesBitmap(nonEmptySet)) return false;
}
if (__CFCSetIsBitmap(nonEmptySet)) {
bits = __CFCSetBitmapBits(nonEmptySet);
} else {
bits = bitsBuf;
__CFCSetGetBitmap(nonEmptySet, bitsBuf);
}
if (__CFCSetIsEqualBitmap(NULL, (const UInt32 *)bits)) {
if (!__CFCSetAnnexIsInverted(nonEmptySet)) return true;
} else {
return false;
}
// Annex set has to be CFRangeMake(0x10000, 0xfffff)
for (idx = 1;idx < MAX_ANNEX_PLANE;idx++) {
if (__CFCSetIsBitmap(nonEmptySet)) {
if (!__CFCSetIsEqualBitmap((__CFCSetAnnexIsInverted(nonEmptySet) ? NULL : (const UInt32 *)-1), (const UInt32 *)bitsBuf)) return false;
} else {
__CFCSetGetBitmap(__CFCSetGetAnnexPlaneCharacterSetNoAlloc(nonEmptySet, idx), bitsBuf);
if (!__CFCSetIsEqualBitmap((const UInt32 *)-1, (const UInt32 *)bitsBuf)) return false;
}
}
return true;
}
}
if (__CFCSetIsBuiltin((CFCharacterSetRef)cf1) || __CFCSetIsBuiltin((CFCharacterSetRef)cf2)) {
CFCharacterSetRef builtinSet = (__CFCSetIsBuiltin((CFCharacterSetRef)cf1) ? (CFCharacterSetRef)cf1 : (CFCharacterSetRef)cf2);
CFCharacterSetRef nonBuiltinSet = (builtinSet == cf1 ? (CFCharacterSetRef)cf2 : (CFCharacterSetRef)cf1);
if (__CFCSetIsRange(nonBuiltinSet)) {
UTF32Char firstChar = __CFCSetRangeFirstChar(nonBuiltinSet);
UTF32Char lastChar = (firstChar + __CFCSetRangeLength(nonBuiltinSet) - 1);
uint8_t firstPlane = (firstChar >> 16) & 0xFF;
uint8_t lastPlane = (lastChar >> 16) & 0xFF;
uint8_t result;
for (idx = 0;idx < MAX_ANNEX_PLANE;idx++) {
result = CFUniCharGetBitmapForPlane(__CFCSetBuiltinType(builtinSet), idx, bitsBuf, (isInvertStateIdentical != 0));
if (idx < firstPlane || idx > lastPlane) {