19
19
#include "CFUniCharPriv.h"
20
20
#include <stdlib.h>
21
21
#include <string.h>
22
+ #include "CFPriv.h"
22
23
#include <unicode/uchar.h>
23
-
24
+ #include <unicode/uset.h>
24
25
25
26
#define BITSPERBYTE 8 /* (CHAR_BIT * sizeof(unsigned char)) */
26
27
#define LOG_BPB 3
@@ -104,57 +105,63 @@ enum {
104
105
__kCFCharSetClassCompactBitmap = 4 ,
105
106
};
106
107
108
+ #define __CFCSetInfoFlagVariantHighBit (6)
109
+ #define __CFCSetInfoFlagVariantLowBit (4)
110
+ #define __CFCSetInfoFlagInvertedBit (3)
111
+ #define __CFCSetInfoFlagHasHashBit (2)
112
+ #define __CFCSetInfoFlagIsMutableBit (0)
113
+
107
114
/* Inline accessor macros for _base._info
108
115
*/
109
116
CF_INLINE Boolean __CFCSetIsMutable (CFCharacterSetRef cset ) {
110
117
return __CFRuntimeGetFlag (cset , 0 );
111
118
}
112
119
113
120
CF_INLINE Boolean __CFCSetIsBuiltin (CFCharacterSetRef cset ) {
114
- return __CFRuntimeGetValue (cset , 6 , 4 ) == __kCFCharSetClassBuiltin ;
121
+ return __CFRuntimeGetValue (cset , __CFCSetInfoFlagVariantHighBit , __CFCSetInfoFlagVariantLowBit ) == __kCFCharSetClassBuiltin ;
115
122
}
116
123
117
124
CF_INLINE Boolean __CFCSetIsRange (CFCharacterSetRef cset ) {
118
- return __CFRuntimeGetValue (cset , 6 , 4 ) == __kCFCharSetClassRange ;
125
+ return __CFRuntimeGetValue (cset , __CFCSetInfoFlagVariantHighBit , __CFCSetInfoFlagVariantLowBit ) == __kCFCharSetClassRange ;
119
126
}
120
127
121
128
CF_INLINE Boolean __CFCSetIsString (CFCharacterSetRef cset ) {
122
- return __CFRuntimeGetValue (cset , 6 , 4 ) == __kCFCharSetClassString ;
129
+ return __CFRuntimeGetValue (cset , __CFCSetInfoFlagVariantHighBit , __CFCSetInfoFlagVariantLowBit ) == __kCFCharSetClassString ;
123
130
}
124
131
125
132
CF_INLINE Boolean __CFCSetIsBitmap (CFCharacterSetRef cset ) {
126
- return __CFRuntimeGetValue (cset , 6 , 4 ) == __kCFCharSetClassBitmap ;
133
+ return __CFRuntimeGetValue (cset , __CFCSetInfoFlagVariantHighBit , __CFCSetInfoFlagVariantLowBit ) == __kCFCharSetClassBitmap ;
127
134
}
128
135
CF_INLINE Boolean __CFCSetIsCompactBitmap (CFCharacterSetRef cset ) {
129
- return __CFRuntimeGetValue (cset , 6 , 4 ) == __kCFCharSetClassCompactBitmap ;
136
+ return __CFRuntimeGetValue (cset , __CFCSetInfoFlagVariantHighBit , __CFCSetInfoFlagVariantLowBit ) == __kCFCharSetClassCompactBitmap ;
130
137
}
131
138
132
139
CF_INLINE UInt32 __CFCSetClassType (CFCharacterSetRef cset ) {
133
- return __CFRuntimeGetValue (cset , 6 , 4 );
140
+ return __CFRuntimeGetValue (cset , __CFCSetInfoFlagVariantHighBit , __CFCSetInfoFlagVariantLowBit );
134
141
}
135
142
136
143
CF_INLINE Boolean __CFCSetIsInverted (CFCharacterSetRef cset ) {
137
- return __CFRuntimeGetFlag (cset , 3 );
144
+ return __CFRuntimeGetFlag (cset , __CFCSetInfoFlagInvertedBit );
138
145
}
139
146
140
147
CF_INLINE Boolean __CFCSetHasHashValue (CFCharacterSetRef cset ) {
141
- return __CFRuntimeGetFlag (cset , 2 );
148
+ return __CFRuntimeGetFlag (cset , __CFCSetInfoFlagHasHashBit );
142
149
}
143
150
144
151
CF_INLINE void __CFCSetPutIsMutable (CFMutableCharacterSetRef cset , Boolean isMutable ) {
145
- __CFRuntimeSetFlag (cset , 0 , isMutable );
152
+ __CFRuntimeSetFlag (cset , __CFCSetInfoFlagIsMutableBit , isMutable );
146
153
}
147
154
148
155
CF_INLINE void __CFCSetPutIsInverted (CFMutableCharacterSetRef cset , Boolean isInverted ) {
149
- __CFRuntimeSetFlag (cset , 3 , isInverted );
156
+ __CFRuntimeSetFlag (cset , __CFCSetInfoFlagInvertedBit , isInverted );
150
157
}
151
158
152
159
CF_INLINE void __CFCSetPutHasHashValue (CFMutableCharacterSetRef cset , Boolean hasHash ) {
153
- __CFRuntimeSetFlag (cset , 2 , hasHash );
160
+ __CFRuntimeSetFlag (cset , __CFCSetInfoFlagHasHashBit , hasHash );
154
161
}
155
162
156
163
CF_INLINE void __CFCSetPutClassType (CFMutableCharacterSetRef cset , UInt32 classType ) {
157
- __CFRuntimeSetValue (cset , 6 , 4 , classType );
164
+ __CFRuntimeSetValue (cset , __CFCSetInfoFlagVariantHighBit , __CFCSetInfoFlagVariantLowBit , classType );
158
165
}
159
166
160
167
CF_PRIVATE Boolean __CFCharacterSetIsMutable (CFCharacterSetRef cset ) {
@@ -181,13 +188,13 @@ CF_INLINE void __CFCSetPutCompactBitmapBits(CFMutableCharacterSetRef cset, uint8
181
188
182
189
/* Validation funcs
183
190
*/
184
-
185
191
CF_INLINE void __CFCSetValidateBuiltinType (CFCharacterSetPredefinedSet type , const char * func ) {
186
192
if (type < __kCFFirstBuiltinSetID || type > __kCFLastBuiltinSetID ) {
187
193
CFLog (__kCFLogAssertion , CFSTR ("%s: Unknown builtin type %ld" ), func , (long )type );
188
194
HALT_MSG ("Unknown builtin CFCharacterSet type" );
189
195
}
190
196
}
197
+
191
198
CF_INLINE void __CFCSetValidateTypeAndMutability (CFCharacterSetRef cset , const char * func ) {
192
199
__CFGenericValidateType (cset , _kCFRuntimeIDCFCharacterSet );
193
200
if (!__CFCSetIsMutable (cset )) {
@@ -196,11 +203,18 @@ CF_INLINE void __CFCSetValidateTypeAndMutability(CFCharacterSetRef cset, const c
196
203
}
197
204
}
198
205
199
- CF_INLINE void __CFCSetValidateRange (CFRange theRange , const char * func ) {
200
- if (theRange .location < UCHAR_MIN_VALUE || theRange .location > UCHAR_MAX_VALUE || theRange .length > UCHAR_MAX_VALUE + 1 || theRange .location + theRange .length < UCHAR_MIN_VALUE || theRange .location + theRange .length > UCHAR_MAX_VALUE + 1 ) {
206
+ bool _CFCharacterSetIsValidRange (CFRange theRange ) {
207
+ return theRange .location >= UCHAR_MIN_VALUE && theRange .location <= UCHAR_MAX_VALUE && theRange .length <= UCHAR_MAX_VALUE + 1 && theRange .location + theRange .length >= UCHAR_MIN_VALUE && theRange .location + theRange .length <= UCHAR_MAX_VALUE + 1 ;
208
+ }
209
+
210
+ CF_INLINE Boolean __CFCSetValidateRange (CFRange theRange , const char * func ) {
211
+ if (!_CFCharacterSetIsValidRange (theRange )) {
201
212
CFLog (__kCFLogAssertion , CFSTR ("%s: Range (location: %ld, length: %ld) outside of valid Unicode range (0x0 - 0x10FFFF)" ), func , (long )theRange .location , (long )theRange .length );
202
- HALT_MSG ("CFCharacterSet range is outside of valid Unicode range (0x0 - 0x10FFFF)" );
213
+ HALT_MSG ("CFCharacterSet range is outside of valid Unicode range (0x0 - 0x10FFFF)" );
214
+
215
+ return false;
203
216
}
217
+ return true;
204
218
}
205
219
206
220
/* Inline utility funcs
@@ -524,7 +538,7 @@ static void __CFCheckForExpandedSet(CFCharacterSetRef cset) {
524
538
static bool warnedOnce = false;
525
539
526
540
if (0 > __CFNumberOfPlanesForLogging ) {
527
- const char * envVar = __CFgetenv ("CFCharacterSetCheckForExpandedSet" );
541
+ const char * envVar = getenv ("CFCharacterSetCheckForExpandedSet" );
528
542
long value = (envVar ? strtol_l (envVar , NULL , 0 , NULL ) : 0 );
529
543
__CFNumberOfPlanesForLogging = (int8_t )(((value > 0 ) && (value <= 16 )) ? value : 0 );
530
544
}
@@ -1394,9 +1408,12 @@ CFCharacterSetRef CFCharacterSetGetPredefined(CFCharacterSetPredefinedSet theSet
1394
1408
return cset ;
1395
1409
}
1396
1410
1411
+
1397
1412
#if DEPLOYMENT_RUNTIME_SWIFT
1398
1413
Boolean _CFCharacterSetInitWithCharactersInRange (CFMutableCharacterSetRef cset , CFRange theRange ) {
1399
- __CFCSetValidateRange (theRange , __PRETTY_FUNCTION__ );
1414
+ if (!__CFCSetValidateRange (theRange , __PRETTY_FUNCTION__ )) {
1415
+ return false;
1416
+ }
1400
1417
1401
1418
if (theRange .length ) {
1402
1419
if (!__CFCSetGenericInit (cset , __kCFCharSetClassRange , false)) return false;
@@ -1412,7 +1429,9 @@ Boolean _CFCharacterSetInitWithCharactersInRange(CFMutableCharacterSetRef cset,
1412
1429
#endif
1413
1430
1414
1431
CFCharacterSetRef CFCharacterSetCreateWithCharactersInRange (CFAllocatorRef allocator , CFRange theRange ) {
1415
- __CFCSetValidateRange (theRange , __PRETTY_FUNCTION__ );
1432
+ if (!__CFCSetValidateRange (theRange , __PRETTY_FUNCTION__ )) {
1433
+ return NULL ;
1434
+ }
1416
1435
1417
1436
CFMutableCharacterSetRef cset ;
1418
1437
@@ -1634,7 +1653,6 @@ CFCharacterSetRef CFCharacterSetCreateWithBitmapRepresentation(CFAllocatorRef al
1634
1653
1635
1654
CFCharacterSetRef CFCharacterSetCreateInvertedSet (CFAllocatorRef alloc , CFCharacterSetRef theSet ) {
1636
1655
CFMutableCharacterSetRef cset ;
1637
-
1638
1656
CF_OBJC_RETAINED_FUNCDISPATCHV (_kCFRuntimeIDCFCharacterSet , CFCharacterSetRef , (NSCharacterSet * )theSet , invertedSet );
1639
1657
1640
1658
cset = CFCharacterSetCreateMutableCopy (alloc , theSet );
@@ -2002,7 +2020,7 @@ Boolean CFCharacterSetHasMemberInPlane(CFCharacterSetRef theSet, CFIndex thePlan
2002
2020
}
2003
2021
2004
2022
CF_OBJC_FUNCDISPATCHV (_kCFRuntimeIDCFCharacterSet , Boolean , (NSCharacterSet * )theSet , hasMemberInPlane :(uint8_t )thePlane );
2005
-
2023
+
2006
2024
if (__CFCSetIsEmpty (theSet )) {
2007
2025
return (isInverted ? TRUE : FALSE);
2008
2026
} else if (__CFCSetIsBuiltin (theSet )) {
@@ -2080,7 +2098,7 @@ Boolean CFCharacterSetHasMemberInPlane(CFCharacterSetRef theSet, CFIndex thePlan
2080
2098
2081
2099
CFDataRef CFCharacterSetCreateBitmapRepresentation (CFAllocatorRef alloc , CFCharacterSetRef theSet ) {
2082
2100
CF_OBJC_FUNCDISPATCHV (_kCFRuntimeIDCFCharacterSet , CFDataRef , (NSCharacterSet * )theSet , _retainedBitmapRepresentation );
2083
-
2101
+
2084
2102
__CFGenericValidateType (theSet , _kCFRuntimeIDCFCharacterSet );
2085
2103
2086
2104
bool isAnnexInverted = (__CFCSetAnnexIsInverted (theSet ) != 0 );
@@ -2280,9 +2298,11 @@ CFDataRef CFCharacterSetCreateBitmapRepresentation(CFAllocatorRef alloc, CFChara
2280
2298
/*** MutableCharacterSet functions ***/
2281
2299
void CFCharacterSetAddCharactersInRange (CFMutableCharacterSetRef theSet , CFRange theRange ) {
2282
2300
CF_OBJC_FUNCDISPATCHV (_kCFRuntimeIDCFCharacterSet , void , (NSMutableCharacterSet * )theSet , addCharactersInRange :NSMakeRange (theRange .location , theRange .length ));
2283
-
2301
+
2284
2302
__CFCSetValidateTypeAndMutability (theSet , __PRETTY_FUNCTION__ );
2285
- __CFCSetValidateRange (theRange , __PRETTY_FUNCTION__ );
2303
+ if (!__CFCSetValidateRange (theRange , __PRETTY_FUNCTION__ )) {
2304
+ return ;
2305
+ }
2286
2306
2287
2307
if (__CFCSetIsBuiltin ((CFCharacterSetRef )theSet ) && !__CFCSetIsMutable ((CFCharacterSetRef )theSet ) && !__CFCSetIsInverted ((CFCharacterSetRef )theSet )) {
2288
2308
CFCharacterSetRef sharedSet = CFCharacterSetGetPredefined (__CFCSetBuiltinType ((CFCharacterSetRef )theSet ));
@@ -2347,9 +2367,11 @@ void CFCharacterSetAddCharactersInRange(CFMutableCharacterSetRef theSet, CFRange
2347
2367
2348
2368
void CFCharacterSetRemoveCharactersInRange (CFMutableCharacterSetRef theSet , CFRange theRange ) {
2349
2369
CF_OBJC_FUNCDISPATCHV (_kCFRuntimeIDCFCharacterSet , void , (NSMutableCharacterSet * )theSet , removeCharactersInRange :NSMakeRange (theRange .location , theRange .length ));
2350
-
2370
+
2351
2371
__CFCSetValidateTypeAndMutability (theSet , __PRETTY_FUNCTION__ );
2352
- __CFCSetValidateRange (theRange , __PRETTY_FUNCTION__ );
2372
+ if (!__CFCSetValidateRange (theRange , __PRETTY_FUNCTION__ )) {
2373
+ return ;
2374
+ }
2353
2375
2354
2376
if (__CFCSetIsBuiltin ((CFCharacterSetRef )theSet ) && !__CFCSetIsMutable ((CFCharacterSetRef )theSet ) && !__CFCSetIsInverted ((CFCharacterSetRef )theSet )) {
2355
2377
CFCharacterSetRef sharedSet = CFCharacterSetGetPredefined (__CFCSetBuiltinType ((CFCharacterSetRef )theSet ));
@@ -2423,7 +2445,7 @@ void CFCharacterSetAddCharactersInString(CFMutableCharacterSetRef theSet, CFStr
2423
2445
BOOL hasSurrogate = NO ;
2424
2446
2425
2447
CF_OBJC_FUNCDISPATCHV (_kCFRuntimeIDCFCharacterSet , void , (NSMutableCharacterSet * )theSet , addCharactersInString :(NSString * )theString );
2426
-
2448
+
2427
2449
__CFCSetValidateTypeAndMutability (theSet , __PRETTY_FUNCTION__ );
2428
2450
2429
2451
if (__CFCSetIsBuiltin ((CFCharacterSetRef )theSet ) && !__CFCSetIsMutable ((CFCharacterSetRef )theSet ) && !__CFCSetIsInverted ((CFCharacterSetRef )theSet )) {
@@ -2516,7 +2538,7 @@ void CFCharacterSetRemoveCharactersInString(CFMutableCharacterSetRef theSet, CFS
2516
2538
BOOL hasSurrogate = NO ;
2517
2539
2518
2540
CF_OBJC_FUNCDISPATCHV (_kCFRuntimeIDCFCharacterSet , void , (NSMutableCharacterSet * )theSet , removeCharactersInString :(NSString * )theString );
2519
-
2541
+
2520
2542
__CFCSetValidateTypeAndMutability (theSet , __PRETTY_FUNCTION__ );
2521
2543
2522
2544
if (__CFCSetIsBuiltin ((CFCharacterSetRef )theSet ) && !__CFCSetIsMutable ((CFCharacterSetRef )theSet ) && !__CFCSetIsInverted ((CFCharacterSetRef )theSet )) {
@@ -2601,7 +2623,7 @@ void CFCharacterSetUnion(CFMutableCharacterSetRef theSet, CFCharacterSetRef theO
2601
2623
CFCharacterSetRef expandedSet = NULL ;
2602
2624
2603
2625
CF_OBJC_FUNCDISPATCHV (_kCFRuntimeIDCFCharacterSet , void , (NSMutableCharacterSet * )theSet , formUnionWithCharacterSet :(NSCharacterSet * )theOtherSet );
2604
-
2626
+
2605
2627
__CFCSetValidateTypeAndMutability (theSet , __PRETTY_FUNCTION__ );
2606
2628
2607
2629
if (__CFCSetIsBuiltin ((CFCharacterSetRef )theSet ) && !__CFCSetIsMutable ((CFCharacterSetRef )theSet ) && !__CFCSetIsInverted ((CFCharacterSetRef )theSet )) {
@@ -2738,7 +2760,7 @@ void CFCharacterSetIntersect(CFMutableCharacterSetRef theSet, CFCharacterSetRef
2738
2760
CFCharacterSetRef expandedSet = NULL ;
2739
2761
2740
2762
CF_OBJC_FUNCDISPATCHV (_kCFRuntimeIDCFCharacterSet , void , (NSMutableCharacterSet * )theSet , formIntersectionWithCharacterSet :(NSCharacterSet * )theOtherSet );
2741
-
2763
+
2742
2764
__CFCSetValidateTypeAndMutability (theSet , __PRETTY_FUNCTION__ );
2743
2765
2744
2766
if (__CFCSetIsBuiltin ((CFCharacterSetRef )theSet ) && !__CFCSetIsMutable ((CFCharacterSetRef )theSet ) && !__CFCSetIsInverted ((CFCharacterSetRef )theSet )) {
@@ -2981,7 +3003,7 @@ void CFCharacterSetIntersect(CFMutableCharacterSetRef theSet, CFCharacterSetRef
2981
3003
void CFCharacterSetInvert (CFMutableCharacterSetRef theSet ) {
2982
3004
2983
3005
CF_OBJC_FUNCDISPATCHV (_kCFRuntimeIDCFCharacterSet , void , (NSMutableCharacterSet * )theSet , invert );
2984
-
3006
+
2985
3007
__CFCSetValidateTypeAndMutability (theSet , __PRETTY_FUNCTION__ );
2986
3008
2987
3009
if (__CFCSetIsBuiltin ((CFCharacterSetRef )theSet ) && !__CFCSetIsMutable ((CFCharacterSetRef )theSet ) && !__CFCSetIsInverted ((CFCharacterSetRef )theSet )) {
@@ -3060,7 +3082,6 @@ void _CFCharacterSetFast(CFMutableCharacterSetRef theSet) {
3060
3082
}
3061
3083
}
3062
3084
}
3063
-
3064
3085
/* Keyed-coding support
3065
3086
*/
3066
3087
CFCharacterSetKeyedCodingType _CFCharacterSetGetKeyedCodingType (CFCharacterSetRef cset ) {
@@ -3072,7 +3093,7 @@ CFCharacterSetKeyedCodingType _CFCharacterSetGetKeyedCodingType(CFCharacterSetRe
3072
3093
3073
3094
case __kCFCharSetClassString : // We have to check if we have non-BMP here
3074
3095
if (!__CFCSetHasNonBMPPlane (cset ) && !__CFCSetAnnexIsInverted (cset )) return kCFCharacterSetKeyedCodingTypeString ; // BMP only. we can archive the string
3075
- /* fallthrough */
3096
+ CF_FALLTHROUGH ;
3076
3097
3077
3098
default :
3078
3099
return kCFCharacterSetKeyedCodingTypeBitmap ;
0 commit comments