-
Notifications
You must be signed in to change notification settings - Fork 109
/
CFICUConverters.c
460 lines (356 loc) · 19.1 KB
/
CFICUConverters.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
/*
* 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@
*/
/* CFICUConverters.c
Copyright (c) 2004-2014, Apple Inc. All rights reserved.
Responsibility: Aki Inoue
*/
#include "CFStringEncodingDatabase.h"
#include "CFStringEncodingConverterPriv.h"
#include "CFICUConverters.h"
#include <CoreFoundation/CFStringEncodingExt.h>
#include <CoreFoundation/CFUniChar.h>
#include <unicode/ucnv.h>
#include <unicode/uversion.h>
#include "CFInternal.h"
#include <stdio.h>
// Thread data support
typedef struct {
uint8_t _numSlots;
uint8_t _nextSlot;
UConverter **_converters;
} __CFICUThreadData;
static void __CFICUThreadDataDestructor(void *context) {
__CFICUThreadData * data = (__CFICUThreadData *)context;
if (NULL != data->_converters) { // scan to make sure deallocation
UConverter **converter = data->_converters;
UConverter **limit = converter + data->_numSlots;
while (converter < limit) {
if (NULL != converter) ucnv_close(*converter);
++converter;
}
CFAllocatorDeallocate(NULL, data->_converters);
}
CFAllocatorDeallocate(NULL, data);
}
CF_INLINE __CFICUThreadData *__CFStringEncodingICUGetThreadData() {
__CFICUThreadData * data;
data = (__CFICUThreadData *)_CFGetTSD(__CFTSDKeyICUConverter);
if (NULL == data) {
data = (__CFICUThreadData *)CFAllocatorAllocate(NULL, sizeof(__CFICUThreadData), 0);
memset(data, 0, sizeof(__CFICUThreadData));
_CFSetTSD(__CFTSDKeyICUConverter, (void *)data, __CFICUThreadDataDestructor);
}
return data;
}
CF_PRIVATE const char *__CFStringEncodingGetICUName(CFStringEncoding encoding) {
#define STACK_BUFFER_SIZE (60)
char buffer[STACK_BUFFER_SIZE];
const char *result = NULL;
UErrorCode errorCode = U_ZERO_ERROR;
uint32_t codepage = 0;
if (kCFStringEncodingUTF7_IMAP == encoding) return "IMAP-mailbox-name";
if (kCFStringEncodingUnicode != (encoding & 0x0F00)) codepage = __CFStringEncodingGetWindowsCodePage(encoding); // we don't use codepage for UTF to avoid little endian weirdness of Windows
if ((0 != codepage) && (snprintf(buffer, STACK_BUFFER_SIZE, "windows-%d", codepage) < STACK_BUFFER_SIZE) && (NULL != (result = ucnv_getAlias(buffer, 0, &errorCode)))) return result;
if (__CFStringEncodingGetCanonicalName(encoding, buffer, STACK_BUFFER_SIZE)) result = ucnv_getAlias(buffer, 0, &errorCode);
return result;
#undef STACK_BUFFER_SIZE
}
CF_PRIVATE CFStringEncoding __CFStringEncodingGetFromICUName(const char *icuName) {
uint32_t codepage;
char *endPtr;
UErrorCode errorCode = U_ZERO_ERROR;
if ((0 == strncasecmp_l(icuName, "windows-", strlen("windows-"), NULL)) && (0 != (codepage = strtol(icuName + strlen("windows-"), &endPtr, 10))) && (*endPtr == '\0')) return __CFStringEncodingGetFromWindowsCodePage(codepage);
if (0 != ucnv_countAliases(icuName, &errorCode)) {
CFStringEncoding encoding;
const char *name;
// Try WINDOWS platform
name = ucnv_getStandardName(icuName, "WINDOWS", &errorCode);
if (NULL != name) {
if ((0 == strncasecmp_l(name, "windows-", strlen("windows-"), NULL)) && (0 != (codepage = strtol(name + strlen("windows-"), &endPtr, 10))) && (*endPtr == '\0')) return __CFStringEncodingGetFromWindowsCodePage(codepage);
if (strncasecmp_l(icuName, name, strlen(name), NULL) && (kCFStringEncodingInvalidId != (encoding = __CFStringEncodingGetFromCanonicalName(name)))) return encoding;
}
// Try JAVA platform
name = ucnv_getStandardName(icuName, "JAVA", &errorCode);
if ((NULL != name) && strncasecmp_l(icuName, name, strlen(name), NULL) && (kCFStringEncodingInvalidId != (encoding = __CFStringEncodingGetFromCanonicalName(name)))) return encoding;
// Try MIME platform
name = ucnv_getStandardName(icuName, "MIME", &errorCode);
if ((NULL != name) && strncasecmp_l(icuName, name, strlen(name), NULL) && (kCFStringEncodingInvalidId != (encoding = __CFStringEncodingGetFromCanonicalName(name)))) return encoding;
}
return kCFStringEncodingInvalidId;
}
CF_INLINE UConverter *__CFStringEncodingConverterCreateICUConverter(const char *icuName, uint32_t flags, bool toUnicode) {
UConverter *converter;
UErrorCode errorCode = U_ZERO_ERROR;
uint8_t streamID = CFStringEncodingStreamIDFromMask(flags);
if (0 != streamID) { // this is a part of streaming previously created
__CFICUThreadData *data = __CFStringEncodingICUGetThreadData();
--streamID; // map to array index
if ((streamID < data->_numSlots) && (NULL != data->_converters[streamID])) return data->_converters[streamID];
}
converter = ucnv_open(icuName, &errorCode);
if (NULL != converter) {
char lossyByte = CFStringEncodingMaskToLossyByte(flags);
if ((0 == lossyByte) && (0 != (flags & kCFStringEncodingAllowLossyConversion))) lossyByte = '?';
if (0 ==lossyByte) {
if (toUnicode) {
ucnv_setToUCallBack(converter, &UCNV_TO_U_CALLBACK_STOP, NULL, NULL, NULL, &errorCode);
} else {
ucnv_setFromUCallBack(converter, &UCNV_FROM_U_CALLBACK_STOP, NULL, NULL, NULL, &errorCode);
}
} else {
ucnv_setSubstChars(converter, &lossyByte, 1, &errorCode);
}
}
return converter;
}
#define ICU_CONVERTER_SLOT_INCREMENT (10)
#define ICU_CONVERTER_MAX_SLOT (255)
static CFIndex __CFStringEncodingConverterReleaseICUConverter(UConverter *converter, uint32_t flags, CFIndex status) {
uint8_t streamID = CFStringEncodingStreamIDFromMask(flags);
if ((kCFStringEncodingInvalidInputStream != status) && ((0 != (flags & kCFStringEncodingPartialInput)) || ((kCFStringEncodingInsufficientOutputBufferLength == status) && (0 != (flags & kCFStringEncodingPartialOutput))))) {
if (0 == streamID) {
__CFICUThreadData *data = __CFStringEncodingICUGetThreadData();
if (NULL == data->_converters) {
data->_converters = (UConverter **)CFAllocatorAllocate(NULL, sizeof(UConverter *) * ICU_CONVERTER_SLOT_INCREMENT, 0);
memset(data->_converters, 0, sizeof(UConverter *) * ICU_CONVERTER_SLOT_INCREMENT);
data->_numSlots = ICU_CONVERTER_SLOT_INCREMENT;
data->_nextSlot = 0;
} else if ((data->_nextSlot >= data->_numSlots) || (NULL != data->_converters[data->_nextSlot])) { // Need to find one
CFIndex index;
for (index = 0;index < data->_numSlots;index++) {
if (NULL == data->_converters[index]) {
data->_nextSlot = index;
break;
}
}
if (index >= data->_numSlots) { // we're full
UConverter **newConverters;
CFIndex newSize = data->_numSlots + ICU_CONVERTER_SLOT_INCREMENT;
if (newSize > ICU_CONVERTER_MAX_SLOT) { // something is terribly wrong
CFLog(kCFLogLevelError, CFSTR("Per-thread streaming ID for ICU converters exhausted. Ignoring..."));
ucnv_close(converter);
return 0;
}
newConverters = (UConverter **)CFAllocatorAllocate(NULL, sizeof(UConverter *) * newSize, 0);
memset(newConverters, 0, sizeof(UConverter *) * newSize);
memcpy(newConverters, data->_converters, sizeof(UConverter *) * data->_numSlots);
CFAllocatorDeallocate(NULL, data->_converters);
data->_converters = newConverters;
data->_nextSlot = data->_numSlots;
data->_numSlots = newSize;
}
}
data->_converters[data->_nextSlot] = converter;
streamID = data->_nextSlot + 1;
// now find next slot
++data->_nextSlot;
if ((data->_nextSlot >= data->_numSlots) || (NULL != data->_converters[data->_nextSlot])) {
data->_nextSlot = 0;
while ((data->_nextSlot < data->_numSlots) && (NULL != data->_converters[data->_nextSlot])) ++data->_nextSlot;
}
}
return CFStringEncodingStreamIDToMask(streamID);
}
if (0 != streamID) {
__CFICUThreadData *data = __CFStringEncodingICUGetThreadData();
--streamID; // map to array index
if ((streamID < data->_numSlots) && (converter == data->_converters[streamID])) {
data->_converters[streamID] = NULL;
if (data->_nextSlot > streamID) data->_nextSlot = streamID;
}
}
ucnv_close(converter);
return 0;
}
#define MAX_BUFFER_SIZE (1000)
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
#if 0
// we're no longer doing this check. Revive when the status in the bug changed.
#if (U_ICU_VERSION_MAJOR_NUM > 49)
#warning Unknown ICU version. Check binary compatibility issues for rdar://problem/6024743
#endif
#endif
#endif
#define HAS_ICU_BUG_6024743 (1)
#define HAS_ICU_BUG_6025527 (1)
CF_PRIVATE CFIndex __CFStringEncodingICUToBytes(const char *icuName, uint32_t flags, const UniChar *characters, CFIndex numChars, CFIndex *usedCharLen, uint8_t *bytes, CFIndex maxByteLen, CFIndex *usedByteLen) {
UConverter *converter;
UErrorCode errorCode = U_ZERO_ERROR;
const UTF16Char *source = characters;
const UTF16Char *sourceLimit = source + numChars;
char *destination = (char *)bytes;
const char *destinationLimit = destination + maxByteLen;
bool flush = ((0 == (flags & kCFStringEncodingPartialInput)) ? true : false);
CFIndex status;
if (NULL == (converter = __CFStringEncodingConverterCreateICUConverter(icuName, flags, false))) return kCFStringEncodingConverterUnavailable;
if (0 == maxByteLen) {
char buffer[MAX_BUFFER_SIZE];
CFIndex totalLength = 0;
while ((source < sourceLimit) && (U_ZERO_ERROR == errorCode)) {
destination = buffer;
destinationLimit = destination + MAX_BUFFER_SIZE;
ucnv_fromUnicode(converter, &destination, destinationLimit, (const UChar **)&source, (const UChar *)sourceLimit, NULL, flush, &errorCode);
totalLength += (destination - buffer);
if (U_BUFFER_OVERFLOW_ERROR == errorCode) errorCode = U_ZERO_ERROR;
}
if (NULL != usedByteLen) *usedByteLen = totalLength;
} else {
ucnv_fromUnicode(converter, &destination, destinationLimit, (const UChar **)&source, (const UChar *)sourceLimit, NULL, flush, &errorCode);
#if HAS_ICU_BUG_6024743
/* Another critical ICU design issue. Similar to conversion error, source pointer returned from U_BUFFER_OVERFLOW_ERROR is already beyond the last valid character position. It renders the returned value from source entirely unusable. We have to manually back up until succeeding <rdar://problem/7183045> Intrestingly, this issue doesn't apply to ucnv_toUnicode. The asynmmetric nature makes this more dangerous */
if (U_BUFFER_OVERFLOW_ERROR == errorCode) {
const uint8_t *bitmap = CFUniCharGetBitmapPtrForPlane(kCFUniCharNonBaseCharacterSet, 0);
const uint8_t *nonBase;
UTF32Char character;
do {
// Since the output buffer is filled, we can assume no invalid chars (including stray surrogates)
do {
sourceLimit = (source - 1);
character = *sourceLimit;
nonBase = bitmap;
if (CFUniCharIsSurrogateLowCharacter(character)) {
--sourceLimit;
character = CFUniCharGetLongCharacterForSurrogatePair(*sourceLimit, character);
nonBase = CFUniCharGetBitmapPtrForPlane(kCFUniCharNonBaseCharacterSet, (character >> 16) & 0x000F);
character &= 0xFFFF;
}
} while ((sourceLimit > characters) && CFUniCharIsMemberOfBitmap(character, nonBase));
if (sourceLimit > characters) {
source = characters;
destination = (char *)bytes;
errorCode = U_ZERO_ERROR;
ucnv_resetFromUnicode(converter);
ucnv_fromUnicode(converter, &destination, destinationLimit, (const UChar **)&source, (const UChar *)sourceLimit, NULL, flush, &errorCode);
}
} while (U_BUFFER_OVERFLOW_ERROR == errorCode);
errorCode = U_BUFFER_OVERFLOW_ERROR;
}
#endif
if (NULL != usedByteLen) *usedByteLen = destination - (const char *)bytes;
}
status = ((U_ZERO_ERROR == errorCode) ? kCFStringEncodingConversionSuccess : ((U_BUFFER_OVERFLOW_ERROR == errorCode) ? kCFStringEncodingInsufficientOutputBufferLength : kCFStringEncodingInvalidInputStream));
if (NULL != usedCharLen) {
#if HAS_ICU_BUG_6024743
/* ICU has a serious behavioral inconsistency issue that the source pointer returned from ucnv_fromUnicode() is after illegal input. We have to keep track of any changes in this area in order to prevent future binary compatiibility issues */
if (kCFStringEncodingInvalidInputStream == status) {
#define MAX_ERROR_BUFFER_LEN (32)
UTF16Char errorBuffer[MAX_ERROR_BUFFER_LEN];
int8_t errorLength = MAX_ERROR_BUFFER_LEN;
#undef MAX_ERROR_BUFFER_LEN
errorCode = U_ZERO_ERROR;
ucnv_getInvalidUChars(converter, (UChar *)errorBuffer, &errorLength, &errorCode);
if (U_ZERO_ERROR == errorCode) {
source -= errorLength;
} else {
// Gah, something is terribly wrong. Reset everything
source = characters; // 0 length
if (NULL != usedByteLen) *usedByteLen = 0;
}
}
#endif
*usedCharLen = source - characters;
}
status |= __CFStringEncodingConverterReleaseICUConverter(converter, flags, status);
return status;
}
CF_PRIVATE CFIndex __CFStringEncodingICUToUnicode(const char *icuName, uint32_t flags, const uint8_t *bytes, CFIndex numBytes, CFIndex *usedByteLen, UniChar *characters, CFIndex maxCharLen, CFIndex *usedCharLen) {
UConverter *converter;
UErrorCode errorCode = U_ZERO_ERROR;
const char *source = (const char *)bytes;
const char *sourceLimit = source + numBytes;
UTF16Char *destination = characters;
const UTF16Char *destinationLimit = destination + maxCharLen;
bool flush = ((0 == (flags & kCFStringEncodingPartialInput)) ? true : false);
CFIndex status;
if (NULL == (converter = __CFStringEncodingConverterCreateICUConverter(icuName, flags, true))) return kCFStringEncodingConverterUnavailable;
if (0 == maxCharLen) {
UTF16Char buffer[MAX_BUFFER_SIZE];
CFIndex totalLength = 0;
while ((source < sourceLimit) && (U_ZERO_ERROR == errorCode)) {
destination = buffer;
destinationLimit = destination + MAX_BUFFER_SIZE;
ucnv_toUnicode(converter, (UChar **)&destination, (const UChar *)destinationLimit, &source, sourceLimit, NULL, flush, &errorCode);
totalLength += (destination - buffer);
if (U_BUFFER_OVERFLOW_ERROR == errorCode) errorCode = U_ZERO_ERROR;
}
if (NULL != usedCharLen) *usedCharLen = totalLength;
} else {
ucnv_toUnicode(converter, (UChar **)&destination, (const UChar *)destinationLimit, &source, sourceLimit, NULL, flush, &errorCode);
if (NULL != usedCharLen) *usedCharLen = destination - characters;
}
status = ((U_ZERO_ERROR == errorCode) ? kCFStringEncodingConversionSuccess : ((U_BUFFER_OVERFLOW_ERROR == errorCode) ? kCFStringEncodingInsufficientOutputBufferLength : kCFStringEncodingInvalidInputStream));
if (NULL != usedByteLen) {
#if HAS_ICU_BUG_6024743
/* ICU has a serious behavioral inconsistency issue that the source pointer returned from ucnv_toUnicode() is after illegal input. We have to keep track of any changes in this area in order to prevent future binary compatiibility issues */
if (kCFStringEncodingInvalidInputStream == status) {
#define MAX_ERROR_BUFFER_LEN (32)
char errorBuffer[MAX_ERROR_BUFFER_LEN];
int8_t errorLength = MAX_ERROR_BUFFER_LEN;
#undef MAX_ERROR_BUFFER_LEN
errorCode = U_ZERO_ERROR;
ucnv_getInvalidChars(converter, errorBuffer, &errorLength, &errorCode);
if (U_ZERO_ERROR == errorCode) {
#if HAS_ICU_BUG_6025527
// Another ICU oddness here. ucnv_getInvalidUChars() writes the '\0' terminator, and errorLength includes the extra byte.
if ((errorLength > 0) && ('\0' == errorBuffer[errorLength - 1])) --errorLength;
#endif
source -= errorLength;
} else {
// Gah, something is terribly wrong. Reset everything
source = (const char *)bytes; // 0 length
if (NULL != usedCharLen) *usedCharLen = 0;
}
}
#endif
*usedByteLen = source - (const char *)bytes;
}
status |= __CFStringEncodingConverterReleaseICUConverter(converter, flags, status);
return status;
}
CF_PRIVATE CFIndex __CFStringEncodingICUCharLength(const char *icuName, uint32_t flags, const uint8_t *bytes, CFIndex numBytes) {
CFIndex usedCharLen;
return (__CFStringEncodingICUToUnicode(icuName, flags, bytes, numBytes, NULL, NULL, 0, &usedCharLen) == kCFStringEncodingConversionSuccess ? usedCharLen : 0);
}
CF_PRIVATE CFIndex __CFStringEncodingICUByteLength(const char *icuName, uint32_t flags, const UniChar *characters, CFIndex numChars) {
CFIndex usedByteLen;
return (__CFStringEncodingICUToBytes(icuName, flags, characters, numChars, NULL, NULL, 0, &usedByteLen) == kCFStringEncodingConversionSuccess ? usedByteLen : 0);
}
CF_PRIVATE CFStringEncoding *__CFStringEncodingCreateICUEncodings(CFAllocatorRef allocator, CFIndex *numberOfIndex) {
CFIndex count = ucnv_countAvailable();
CFIndex numEncodings = 0;
CFStringEncoding *encodings;
CFStringEncoding encoding;
CFIndex index;
if (0 == count) return NULL;
encodings = (CFStringEncoding *)CFAllocatorAllocate(NULL, sizeof(CFStringEncoding) * count, 0);
for (index = 0;index < count;index++) {
encoding = __CFStringEncodingGetFromICUName(ucnv_getAvailableName(index));
if (kCFStringEncodingInvalidId != encoding) encodings[numEncodings++] = encoding;
}
if (0 == numEncodings) {
CFAllocatorDeallocate(allocator, encodings);
encodings = NULL;
}
*numberOfIndex = numEncodings;
return encodings;
}