]> git.saurik.com Git - apple/cf.git/blob - CFBuiltinConverters.c
CF-476.17.tar.gz
[apple/cf.git] / CFBuiltinConverters.c
1 /*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* CFBuiltinConverters.c
24 Copyright 1999-2002, Apple, Inc. All rights reserved.
25 Responsibility: Aki Inoue
26 */
27
28 #include "CFStringEncodingConverterExt.h"
29 #include "CFUniChar.h"
30 #include "CFUnicodeDecomposition.h"
31 #include "CFUnicodePrecomposition.h"
32 #include "CFStringEncodingConverterPriv.h"
33 #include "CFInternal.h"
34
35 #define ParagraphSeparator 0x2029
36 #define ASCIINewLine 0x0a
37 static int8_t __CFMapsParagraphSeparator = -1;
38
39 CF_INLINE bool __CFIsParagraphSeparator(UTF16Char character) {
40 if (-1 == __CFMapsParagraphSeparator) __CFMapsParagraphSeparator = (_CFExecutableLinkedOnOrAfter(CFSystemVersionLeopard) ? false : true);
41
42 return ((__CFMapsParagraphSeparator && (ParagraphSeparator == character)) ? true : false);
43 }
44
45 /* Precomposition */
46 static const uint32_t __CFLatin1CombiningCharBitmap[] = { // 0x300 ~ 0x35FF
47 0xFBB94010, 0x01800000, 0x0000000,
48 };
49
50 bool CFStringEncodingIsValidCombiningCharacterForLatin1(UniChar character) {
51 return ((character >= 0x300) && (character < 0x360) && (__CFLatin1CombiningCharBitmap[(character - 0x300) / 32] & (1 << (31 - ((character - 0x300) % 32)))) ? true : false);
52 }
53
54 UniChar CFStringEncodingPrecomposeLatinCharacter(const UniChar *character, CFIndex numChars, CFIndex *usedChars) {
55 if (numChars > 0) {
56 UTF32Char ch = *(character++), nextCh, composedChar;
57 CFIndex usedCharLen = 1;
58
59 if (CFUniCharIsSurrogateHighCharacter(ch) || CFUniCharIsSurrogateLowCharacter(ch)) {
60 if (usedChars) (*usedChars) = usedCharLen;
61 return ch;
62 }
63
64 while (usedCharLen < numChars) {
65 nextCh = *(character++);
66
67 if (CFUniCharIsSurrogateHighCharacter(nextCh) || CFUniCharIsSurrogateLowCharacter(nextCh)) break;
68
69 if (CFUniCharIsMemberOf(nextCh, kCFUniCharNonBaseCharacterSet) && ((composedChar = CFUniCharPrecomposeCharacter(ch, nextCh)) != 0xFFFD)) {
70 if (composedChar > 0xFFFF) { // Non-base
71 break;
72 } else {
73 ch = composedChar;
74 }
75 } else {
76 break;
77 }
78 ++usedCharLen;
79 }
80 if (usedChars) (*usedChars) = usedCharLen;
81 return ch;
82 }
83 return 0xFFFD;
84 }
85
86 /* ASCII */
87 static bool __CFToASCII(uint32_t flags, UniChar character, uint8_t *byte) {
88 if (character < 0x80) {
89 *byte = (uint8_t)character;
90 } else if (__CFIsParagraphSeparator(character)) {
91 *byte = ASCIINewLine;
92 } else {
93 return false;
94 }
95 return true;
96 }
97
98 static bool __CFFromASCII(uint32_t flags, uint8_t byte, UniChar *character) {
99 if (byte < 0x80) {
100 *character = (UniChar)byte;
101 return true;
102 } else {
103 return false;
104 }
105 }
106
107
108 __private_extern__ const CFStringEncodingConverter __CFConverterASCII = {
109 __CFToASCII, __CFFromASCII, 1, 1, kCFStringEncodingConverterCheapEightBit,
110 NULL, NULL, NULL, NULL, NULL, NULL,
111 };
112
113 /* ISO Latin 1 (8859-1) */
114 static bool __CFToISOLatin1(uint32_t flags, UniChar character, uint8_t *byte) {
115 if (character <= 0xFF) {
116 *byte = (uint8_t)character;
117 } else if (__CFIsParagraphSeparator(character)) {
118 *byte = ASCIINewLine;
119 } else {
120 return false;
121 }
122
123 return true;
124 }
125
126 static bool __CFFromISOLatin1(uint32_t flags, uint8_t byte, UniChar *character) {
127 *character = (UniChar)byte;
128 return true;
129 }
130
131 static CFIndex __CFToISOLatin1Precompose(uint32_t flags, const UniChar *character, CFIndex numChars, uint8_t *bytes, CFIndex maxByteLen, CFIndex *usedByteLen) {
132 uint8_t byte;
133 CFIndex usedCharLen;
134
135 if (__CFToISOLatin1(flags, CFStringEncodingPrecomposeLatinCharacter(character, numChars, &usedCharLen), &byte) && byte && (usedCharLen > 1)) {
136 if (maxByteLen) *bytes = byte;
137 *usedByteLen = 1;
138 return usedCharLen;
139 } else {
140 return 0;
141 }
142 }
143
144 __private_extern__ const CFStringEncodingConverter __CFConverterISOLatin1 = {
145 __CFToISOLatin1, __CFFromISOLatin1, 1, 1, kCFStringEncodingConverterCheapEightBit,
146 NULL, NULL, NULL, NULL, __CFToISOLatin1Precompose, CFStringEncodingIsValidCombiningCharacterForLatin1,
147 };
148
149 /* Mac Roman */
150 #define NUM_MACROMAN_FROM_UNI 129
151 static const CFStringEncodingUnicodeTo8BitCharMap macRoman_from_uni[NUM_MACROMAN_FROM_UNI] = {
152 { 0x00A0, 0xCA }, /* NO-BREAK SPACE */
153 { 0x00A1, 0xC1 }, /* INVERTED EXCLAMATION MARK */
154 { 0x00A2, 0xA2 }, /* CENT SIGN */
155 { 0x00A3, 0xA3 }, /* POUND SIGN */
156 { 0x00A5, 0xB4 }, /* YEN SIGN */
157 { 0x00A7, 0xA4 }, /* SECTION SIGN */
158 { 0x00A8, 0xAC }, /* DIAERESIS */
159 { 0x00A9, 0xA9 }, /* COPYRIGHT SIGN */
160 { 0x00AA, 0xBB }, /* FEMININE ORDINAL INDICATOR */
161 { 0x00AB, 0xC7 }, /* LEFT-POINTING DOUBLE ANGLE QUOTATION MARK */
162 { 0x00AC, 0xC2 }, /* NOT SIGN */
163 { 0x00AE, 0xA8 }, /* REGISTERED SIGN */
164 { 0x00AF, 0xF8 }, /* MACRON */
165 { 0x00B0, 0xA1 }, /* DEGREE SIGN */
166 { 0x00B1, 0xB1 }, /* PLUS-MINUS SIGN */
167 { 0x00B4, 0xAB }, /* ACUTE ACCENT */
168 { 0x00B5, 0xB5 }, /* MICRO SIGN */
169 { 0x00B6, 0xA6 }, /* PILCROW SIGN */
170 { 0x00B7, 0xE1 }, /* MIDDLE DOT */
171 { 0x00B8, 0xFC }, /* CEDILLA */
172 { 0x00BA, 0xBC }, /* MASCULINE ORDINAL INDICATOR */
173 { 0x00BB, 0xC8 }, /* RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK */
174 { 0x00BF, 0xC0 }, /* INVERTED QUESTION MARK */
175 { 0x00C0, 0xCB }, /* LATIN CAPITAL LETTER A WITH GRAVE */
176 { 0x00C1, 0xE7 }, /* LATIN CAPITAL LETTER A WITH ACUTE */
177 { 0x00C2, 0xE5 }, /* LATIN CAPITAL LETTER A WITH CIRCUMFLEX */
178 { 0x00C3, 0xCC }, /* LATIN CAPITAL LETTER A WITH TILDE */
179 { 0x00C4, 0x80 }, /* LATIN CAPITAL LETTER A WITH DIAERESIS */
180 { 0x00C5, 0x81 }, /* LATIN CAPITAL LETTER A WITH RING ABOVE */
181 { 0x00C6, 0xAE }, /* LATIN CAPITAL LIGATURE AE */
182 { 0x00C7, 0x82 }, /* LATIN CAPITAL LETTER C WITH CEDILLA */
183 { 0x00C8, 0xE9 }, /* LATIN CAPITAL LETTER E WITH GRAVE */
184 { 0x00C9, 0x83 }, /* LATIN CAPITAL LETTER E WITH ACUTE */
185 { 0x00CA, 0xE6 }, /* LATIN CAPITAL LETTER E WITH CIRCUMFLEX */
186 { 0x00CB, 0xE8 }, /* LATIN CAPITAL LETTER E WITH DIAERESIS */
187 { 0x00CC, 0xED }, /* LATIN CAPITAL LETTER I WITH GRAVE */
188 { 0x00CD, 0xEA }, /* LATIN CAPITAL LETTER I WITH ACUTE */
189 { 0x00CE, 0xEB }, /* LATIN CAPITAL LETTER I WITH CIRCUMFLEX */
190 { 0x00CF, 0xEC }, /* LATIN CAPITAL LETTER I WITH DIAERESIS */
191 { 0x00D1, 0x84 }, /* LATIN CAPITAL LETTER N WITH TILDE */
192 { 0x00D2, 0xF1 }, /* LATIN CAPITAL LETTER O WITH GRAVE */
193 { 0x00D3, 0xEE }, /* LATIN CAPITAL LETTER O WITH ACUTE */
194 { 0x00D4, 0xEF }, /* LATIN CAPITAL LETTER O WITH CIRCUMFLEX */
195 { 0x00D5, 0xCD }, /* LATIN CAPITAL LETTER O WITH TILDE */
196 { 0x00D6, 0x85 }, /* LATIN CAPITAL LETTER O WITH DIAERESIS */
197 { 0x00D8, 0xAF }, /* LATIN CAPITAL LETTER O WITH STROKE */
198 { 0x00D9, 0xF4 }, /* LATIN CAPITAL LETTER U WITH GRAVE */
199 { 0x00DA, 0xF2 }, /* LATIN CAPITAL LETTER U WITH ACUTE */
200 { 0x00DB, 0xF3 }, /* LATIN CAPITAL LETTER U WITH CIRCUMFLEX */
201 { 0x00DC, 0x86 }, /* LATIN CAPITAL LETTER U WITH DIAERESIS */
202 { 0x00DF, 0xA7 }, /* LATIN SMALL LETTER SHARP S */
203 { 0x00E0, 0x88 }, /* LATIN SMALL LETTER A WITH GRAVE */
204 { 0x00E1, 0x87 }, /* LATIN SMALL LETTER A WITH ACUTE */
205 { 0x00E2, 0x89 }, /* LATIN SMALL LETTER A WITH CIRCUMFLEX */
206 { 0x00E3, 0x8B }, /* LATIN SMALL LETTER A WITH TILDE */
207 { 0x00E4, 0x8A }, /* LATIN SMALL LETTER A WITH DIAERESIS */
208 { 0x00E5, 0x8C }, /* LATIN SMALL LETTER A WITH RING ABOVE */
209 { 0x00E6, 0xBE }, /* LATIN SMALL LIGATURE AE */
210 { 0x00E7, 0x8D }, /* LATIN SMALL LETTER C WITH CEDILLA */
211 { 0x00E8, 0x8F }, /* LATIN SMALL LETTER E WITH GRAVE */
212 { 0x00E9, 0x8E }, /* LATIN SMALL LETTER E WITH ACUTE */
213 { 0x00EA, 0x90 }, /* LATIN SMALL LETTER E WITH CIRCUMFLEX */
214 { 0x00EB, 0x91 }, /* LATIN SMALL LETTER E WITH DIAERESIS */
215 { 0x00EC, 0x93 }, /* LATIN SMALL LETTER I WITH GRAVE */
216 { 0x00ED, 0x92 }, /* LATIN SMALL LETTER I WITH ACUTE */
217 { 0x00EE, 0x94 }, /* LATIN SMALL LETTER I WITH CIRCUMFLEX */
218 { 0x00EF, 0x95 }, /* LATIN SMALL LETTER I WITH DIAERESIS */
219 { 0x00F1, 0x96 }, /* LATIN SMALL LETTER N WITH TILDE */
220 { 0x00F2, 0x98 }, /* LATIN SMALL LETTER O WITH GRAVE */
221 { 0x00F3, 0x97 }, /* LATIN SMALL LETTER O WITH ACUTE */
222 { 0x00F4, 0x99 }, /* LATIN SMALL LETTER O WITH CIRCUMFLEX */
223 { 0x00F5, 0x9B }, /* LATIN SMALL LETTER O WITH TILDE */
224 { 0x00F6, 0x9A }, /* LATIN SMALL LETTER O WITH DIAERESIS */
225 { 0x00F7, 0xD6 }, /* DIVISION SIGN */
226 { 0x00F8, 0xBF }, /* LATIN SMALL LETTER O WITH STROKE */
227 { 0x00F9, 0x9D }, /* LATIN SMALL LETTER U WITH GRAVE */
228 { 0x00FA, 0x9C }, /* LATIN SMALL LETTER U WITH ACUTE */
229 { 0x00FB, 0x9E }, /* LATIN SMALL LETTER U WITH CIRCUMFLEX */
230 { 0x00FC, 0x9F }, /* LATIN SMALL LETTER U WITH DIAERESIS */
231 { 0x00FF, 0xD8 }, /* LATIN SMALL LETTER Y WITH DIAERESIS */
232 { 0x0131, 0xF5 }, /* LATIN SMALL LETTER DOTLESS I */
233 { 0x0152, 0xCE }, /* LATIN CAPITAL LIGATURE OE */
234 { 0x0153, 0xCF }, /* LATIN SMALL LIGATURE OE */
235 { 0x0178, 0xD9 }, /* LATIN CAPITAL LETTER Y WITH DIAERESIS */
236 { 0x0192, 0xC4 }, /* LATIN SMALL LETTER F WITH HOOK */
237 { 0x02C6, 0xF6 }, /* MODIFIER LETTER CIRCUMFLEX ACCENT */
238 { 0x02C7, 0xFF }, /* CARON */
239 { 0x02D8, 0xF9 }, /* BREVE */
240 { 0x02D9, 0xFA }, /* DOT ABOVE */
241 { 0x02DA, 0xFB }, /* RING ABOVE */
242 { 0x02DB, 0xFE }, /* OGONEK */
243 { 0x02DC, 0xF7 }, /* SMALL TILDE */
244 { 0x02DD, 0xFD }, /* DOUBLE ACUTE ACCENT */
245 { 0x03A9, 0xBD }, /* OHM SIGN (Canonical ?) */
246 { 0x03C0, 0xB9 }, /* GREEK SMALL LETTER PI */
247 { 0x2013, 0xD0 }, /* EN DASH */
248 { 0x2014, 0xD1 }, /* EM DASH */
249 { 0x2018, 0xD4 }, /* LEFT SINGLE QUOTATION MARK */
250 { 0x2019, 0xD5 }, /* RIGHT SINGLE QUOTATION MARK */
251 { 0x201A, 0xE2 }, /* SINGLE LOW-9 QUOTATION MARK */
252 { 0x201C, 0xD2 }, /* LEFT DOUBLE QUOTATION MARK */
253 { 0x201D, 0xD3 }, /* RIGHT DOUBLE QUOTATION MARK */
254 { 0x201E, 0xE3 }, /* DOUBLE LOW-9 QUOTATION MARK */
255 { 0x2020, 0xA0 }, /* DAGGER */
256 { 0x2021, 0xE0 }, /* DOUBLE DAGGER */
257 { 0x2022, 0xA5 }, /* BULLET */
258 { 0x2026, 0xC9 }, /* HORIZONTAL ELLIPSIS */
259 { 0x2030, 0xE4 }, /* PER MILLE SIGN */
260 { 0x2039, 0xDC }, /* SINGLE LEFT-POINTING ANGLE QUOTATION MARK */
261 { 0x203A, 0xDD }, /* SINGLE RIGHT-POINTING ANGLE QUOTATION MARK */
262 { 0x2044, 0xDA }, /* FRACTION SLASH */
263 { 0x20AC, 0xDB }, /* EURO SIGN */
264 { 0x2122, 0xAA }, /* TRADE MARK SIGN */
265 { 0x2126, 0xBD }, /* OHM SIGN */
266 { 0x2202, 0xB6 }, /* PARTIAL DIFFERENTIAL */
267 { 0x2206, 0xC6 }, /* INCREMENT */
268 { 0x220F, 0xB8 }, /* N-ARY PRODUCT */
269 { 0x2211, 0xB7 }, /* N-ARY SUMMATION */
270 { 0x221A, 0xC3 }, /* SQUARE ROOT */
271 { 0x221E, 0xB0 }, /* INFINITY */
272 { 0x222B, 0xBA }, /* INTEGRAL */
273 { 0x2248, 0xC5 }, /* ALMOST EQUAL TO */
274 { 0x2260, 0xAD }, /* NOT EQUAL TO */
275 { 0x2264, 0xB2 }, /* LESS-THAN OR EQUAL TO */
276 { 0x2265, 0xB3 }, /* GREATER-THAN OR EQUAL TO */
277 { 0x25CA, 0xD7 }, /* LOZENGE */
278 { 0xF8FF, 0xF0 }, /* Apple logo */
279 { 0xFB01, 0xDE }, /* LATIN SMALL LIGATURE FI */
280 { 0xFB02, 0xDF }, /* LATIN SMALL LIGATURE FL */
281 };
282
283 static bool __CFToMacRoman(uint32_t flags, UniChar character, uint8_t *byte) {
284 if (character < 0x80) {
285 *byte = (uint8_t)character;
286 return true;
287 } else {
288 return CFStringEncodingUnicodeTo8BitEncoding(macRoman_from_uni, NUM_MACROMAN_FROM_UNI, character, byte);
289 }
290 }
291
292 static const UniChar macRoman_to_uni[128] = {
293 0x00C4, /* LATIN CAPITAL LETTER A WITH DIAERESIS */
294 0x00C5, /* LATIN CAPITAL LETTER A WITH RING ABOVE */
295 0x00C7, /* LATIN CAPITAL LETTER C WITH CEDILLA */
296 0x00C9, /* LATIN CAPITAL LETTER E WITH ACUTE */
297 0x00D1, /* LATIN CAPITAL LETTER N WITH TILDE */
298 0x00D6, /* LATIN CAPITAL LETTER O WITH DIAERESIS */
299 0x00DC, /* LATIN CAPITAL LETTER U WITH DIAERESIS */
300 0x00E1, /* LATIN SMALL LETTER A WITH ACUTE */
301 0x00E0, /* LATIN SMALL LETTER A WITH GRAVE */
302 0x00E2, /* LATIN SMALL LETTER A WITH CIRCUMFLEX */
303 0x00E4, /* LATIN SMALL LETTER A WITH DIAERESIS */
304 0x00E3, /* LATIN SMALL LETTER A WITH TILDE */
305 0x00E5, /* LATIN SMALL LETTER A WITH RING ABOVE */
306 0x00E7, /* LATIN SMALL LETTER C WITH CEDILLA */
307 0x00E9, /* LATIN SMALL LETTER E WITH ACUTE */
308 0x00E8, /* LATIN SMALL LETTER E WITH GRAVE */
309 0x00EA, /* LATIN SMALL LETTER E WITH CIRCUMFLEX */
310 0x00EB, /* LATIN SMALL LETTER E WITH DIAERESIS */
311 0x00ED, /* LATIN SMALL LETTER I WITH ACUTE */
312 0x00EC, /* LATIN SMALL LETTER I WITH GRAVE */
313 0x00EE, /* LATIN SMALL LETTER I WITH CIRCUMFLEX */
314 0x00EF, /* LATIN SMALL LETTER I WITH DIAERESIS */
315 0x00F1, /* LATIN SMALL LETTER N WITH TILDE */
316 0x00F3, /* LATIN SMALL LETTER O WITH ACUTE */
317 0x00F2, /* LATIN SMALL LETTER O WITH GRAVE */
318 0x00F4, /* LATIN SMALL LETTER O WITH CIRCUMFLEX */
319 0x00F6, /* LATIN SMALL LETTER O WITH DIAERESIS */
320 0x00F5, /* LATIN SMALL LETTER O WITH TILDE */
321 0x00FA, /* LATIN SMALL LETTER U WITH ACUTE */
322 0x00F9, /* LATIN SMALL LETTER U WITH GRAVE */
323 0x00FB, /* LATIN SMALL LETTER U WITH CIRCUMFLEX */
324 0x00FC, /* LATIN SMALL LETTER U WITH DIAERESIS */
325 0x2020, /* DAGGER */
326 0x00B0, /* DEGREE SIGN */
327 0x00A2, /* CENT SIGN */
328 0x00A3, /* POUND SIGN */
329 0x00A7, /* SECTION SIGN */
330 0x2022, /* BULLET */
331 0x00B6, /* PILCROW SIGN */
332 0x00DF, /* LATIN SMALL LETTER SHARP S */
333 0x00AE, /* REGISTERED SIGN */
334 0x00A9, /* COPYRIGHT SIGN */
335 0x2122, /* TRADE MARK SIGN */
336 0x00B4, /* ACUTE ACCENT */
337 0x00A8, /* DIAERESIS */
338 0x2260, /* NOT EQUAL TO */
339 0x00C6, /* LATIN CAPITAL LIGATURE AE */
340 0x00D8, /* LATIN CAPITAL LETTER O WITH STROKE */
341 0x221E, /* INFINITY */
342 0x00B1, /* PLUS-MINUS SIGN */
343 0x2264, /* LESS-THAN OR EQUAL TO */
344 0x2265, /* GREATER-THAN OR EQUAL TO */
345 0x00A5, /* YEN SIGN */
346 0x00B5, /* MICRO SIGN */
347 0x2202, /* PARTIAL DIFFERENTIAL */
348 0x2211, /* N-ARY SUMMATION */
349 0x220F, /* N-ARY PRODUCT */
350 0x03C0, /* GREEK SMALL LETTER PI */
351 0x222B, /* INTEGRAL */
352 0x00AA, /* FEMININE ORDINAL INDICATOR */
353 0x00BA, /* MASCULINE ORDINAL INDICATOR */
354 0x03A9, /* OHM SIGN (Canonical mapping) */
355 0x00E6, /* LATIN SMALL LIGATURE AE */
356 0x00F8, /* LATIN SMALL LETTER O WITH STROKE */
357 0x00BF, /* INVERTED QUESTION MARK */
358 0x00A1, /* INVERTED EXCLAMATION MARK */
359 0x00AC, /* NOT SIGN */
360 0x221A, /* SQUARE ROOT */
361 0x0192, /* LATIN SMALL LETTER F WITH HOOK */
362 0x2248, /* ALMOST EQUAL TO */
363 0x2206, /* INCREMENT */
364 0x00AB, /* LEFT-POINTING DOUBLE ANGLE QUOTATION MARK */
365 0x00BB, /* RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK */
366 0x2026, /* HORIZONTAL ELLIPSIS */
367 0x00A0, /* NO-BREAK SPACE */
368 0x00C0, /* LATIN CAPITAL LETTER A WITH GRAVE */
369 0x00C3, /* LATIN CAPITAL LETTER A WITH TILDE */
370 0x00D5, /* LATIN CAPITAL LETTER O WITH TILDE */
371 0x0152, /* LATIN CAPITAL LIGATURE OE */
372 0x0153, /* LATIN SMALL LIGATURE OE */
373 0x2013, /* EN DASH */
374 0x2014, /* EM DASH */
375 0x201C, /* LEFT DOUBLE QUOTATION MARK */
376 0x201D, /* RIGHT DOUBLE QUOTATION MARK */
377 0x2018, /* LEFT SINGLE QUOTATION MARK */
378 0x2019, /* RIGHT SINGLE QUOTATION MARK */
379 0x00F7, /* DIVISION SIGN */
380 0x25CA, /* LOZENGE */
381 0x00FF, /* LATIN SMALL LETTER Y WITH DIAERESIS */
382 0x0178, /* LATIN CAPITAL LETTER Y WITH DIAERESIS */
383 0x2044, /* FRACTION SLASH */
384 0x20AC, /* EURO SIGN */
385 0x2039, /* SINGLE LEFT-POINTING ANGLE QUOTATION MARK */
386 0x203A, /* SINGLE RIGHT-POINTING ANGLE QUOTATION MARK */
387 0xFB01, /* LATIN SMALL LIGATURE FI */
388 0xFB02, /* LATIN SMALL LIGATURE FL */
389 0x2021, /* DOUBLE DAGGER */
390 0x00B7, /* MIDDLE DOT */
391 0x201A, /* SINGLE LOW-9 QUOTATION MARK */
392 0x201E, /* DOUBLE LOW-9 QUOTATION MARK */
393 0x2030, /* PER MILLE SIGN */
394 0x00C2, /* LATIN CAPITAL LETTER A WITH CIRCUMFLEX */
395 0x00CA, /* LATIN CAPITAL LETTER E WITH CIRCUMFLEX */
396 0x00C1, /* LATIN CAPITAL LETTER A WITH ACUTE */
397 0x00CB, /* LATIN CAPITAL LETTER E WITH DIAERESIS */
398 0x00C8, /* LATIN CAPITAL LETTER E WITH GRAVE */
399 0x00CD, /* LATIN CAPITAL LETTER I WITH ACUTE */
400 0x00CE, /* LATIN CAPITAL LETTER I WITH CIRCUMFLEX */
401 0x00CF, /* LATIN CAPITAL LETTER I WITH DIAERESIS */
402 0x00CC, /* LATIN CAPITAL LETTER I WITH GRAVE */
403 0x00D3, /* LATIN CAPITAL LETTER O WITH ACUTE */
404 0x00D4, /* LATIN CAPITAL LETTER O WITH CIRCUMFLEX */
405 0xF8FF, /* Apple logo */
406 0x00D2, /* LATIN CAPITAL LETTER O WITH GRAVE */
407 0x00DA, /* LATIN CAPITAL LETTER U WITH ACUTE */
408 0x00DB, /* LATIN CAPITAL LETTER U WITH CIRCUMFLEX */
409 0x00D9, /* LATIN CAPITAL LETTER U WITH GRAVE */
410 0x0131, /* LATIN SMALL LETTER DOTLESS I */
411 0x02C6, /* MODIFIER LETTER CIRCUMFLEX ACCENT */
412 0x02DC, /* SMALL TILDE */
413 0x00AF, /* MACRON */
414 0x02D8, /* BREVE */
415 0x02D9, /* DOT ABOVE */
416 0x02DA, /* RING ABOVE */
417 0x00B8, /* CEDILLA */
418 0x02DD, /* DOUBLE ACUTE ACCENT */
419 0x02DB, /* OGONEK */
420 0x02C7, /* CARON */
421 };
422
423 static bool __CFFromMacRoman(uint32_t flags, uint8_t byte, UniChar *character) {
424 *character = (byte < 0x80 ? (UniChar)byte : macRoman_to_uni[byte - 0x80]);
425 return true;
426 }
427
428 static CFIndex __CFToMacRomanPrecompose(uint32_t flags, const UniChar *character, CFIndex numChars, uint8_t *bytes, CFIndex maxByteLen, CFIndex *usedByteLen) {
429 uint8_t byte;
430 CFIndex usedCharLen;
431
432 if (__CFToMacRoman(flags, CFStringEncodingPrecomposeLatinCharacter(character, numChars, &usedCharLen), &byte) && byte && (usedCharLen > 1)) {
433 if (maxByteLen) *bytes = byte;
434 *usedByteLen = 1;
435 return usedCharLen;
436 } else {
437 return 0;
438 }
439 }
440
441 __private_extern__ const CFStringEncodingConverter __CFConverterMacRoman = {
442 __CFToMacRoman, __CFFromMacRoman, 1, 1, kCFStringEncodingConverterCheapEightBit,
443 NULL, NULL, NULL, NULL, __CFToMacRomanPrecompose, CFStringEncodingIsValidCombiningCharacterForLatin1,
444 };
445
446 /* Win Latin1 (ANSI CodePage 1252) */
447 #define NUM_1252_FROM_UNI 27
448 static const CFStringEncodingUnicodeTo8BitCharMap cp1252_from_uni[NUM_1252_FROM_UNI] = {
449 {0x0152, 0x8C}, // LATIN CAPITAL LIGATURE OE
450 {0x0153, 0x9C}, // LATIN SMALL LIGATURE OE
451 {0x0160, 0x8A}, // LATIN CAPITAL LETTER S WITH CARON
452 {0x0161, 0x9A}, // LATIN SMALL LETTER S WITH CARON
453 {0x0178, 0x9F}, // LATIN CAPITAL LETTER Y WITH DIAERESIS
454 {0x017D, 0x8E}, // LATIN CAPITAL LETTER Z WITH CARON
455 {0x017E, 0x9E}, // LATIN SMALL LETTER Z WITH CARON
456 {0x0192, 0x83}, // LATIN SMALL LETTER F WITH HOOK
457 {0x02C6, 0x88}, // MODIFIER LETTER CIRCUMFLEX ACCENT
458 {0x02DC, 0x98}, // SMALL TILDE
459 {0x2013, 0x96}, // EN DASH
460 {0x2014, 0x97}, // EM DASH
461 {0x2018, 0x91}, // LEFT SINGLE QUOTATION MARK
462 {0x2019, 0x92}, // RIGHT SINGLE QUOTATION MARK
463 {0x201A, 0x82}, // SINGLE LOW-9 QUOTATION MARK
464 {0x201C, 0x93}, // LEFT DOUBLE QUOTATION MARK
465 {0x201D, 0x94}, // RIGHT DOUBLE QUOTATION MARK
466 {0x201E, 0x84}, // DOUBLE LOW-9 QUOTATION MARK
467 {0x2020, 0x86}, // DAGGER
468 {0x2021, 0x87}, // DOUBLE DAGGER
469 {0x2022, 0x95}, // BULLET
470 {0x2026, 0x85}, // HORIZONTAL ELLIPSIS
471 {0x2030, 0x89}, // PER MILLE SIGN
472 {0x2039, 0x8B}, // SINGLE LEFT-POINTING ANGLE QUOTATION MARK
473 {0x203A, 0x9B}, // SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
474 {0x20AC, 0x80}, // EURO SIGN
475 {0x2122, 0x99}, // TRADE MARK SIGN
476 };
477
478 static bool __CFToWinLatin1(uint32_t flags, UniChar character, uint8_t *byte) {
479 if ((character < 0x80) || ((character > 0x9F) && (character <= 0x00FF))) {
480 *byte = (uint8_t)character;
481 return true;
482 }
483 return CFStringEncodingUnicodeTo8BitEncoding(cp1252_from_uni, NUM_1252_FROM_UNI, character, byte);
484 }
485
486 static const uint16_t cp1252_to_uni[32] = {
487 0x20AC, // EURO SIGN
488 0xFFFD, // NOT USED
489 0x201A, // SINGLE LOW-9 QUOTATION MARK
490 0x0192, // LATIN SMALL LETTER F WITH HOOK
491 0x201E, // DOUBLE LOW-9 QUOTATION MARK
492 0x2026, // HORIZONTAL ELLIPSIS
493 0x2020, // DAGGER
494 0x2021, // DOUBLE DAGGER
495 0x02C6, // MODIFIER LETTER CIRCUMFLEX ACCENT
496 0x2030, // PER MILLE SIGN
497 0x0160, // LATIN CAPITAL LETTER S WITH CARON
498 0x2039, // SINGLE LEFT-POINTING ANGLE QUOTATION MARK
499 0x0152, // LATIN CAPITAL LIGATURE OE
500 0xFFFD, // NOT USED
501 0x017D, // LATIN CAPITAL LETTER Z WITH CARON
502 0xFFFD, // NOT USED
503 0xFFFD, // NOT USED
504 0x2018, // LEFT SINGLE QUOTATION MARK
505 0x2019, // RIGHT SINGLE QUOTATION MARK
506 0x201C, // LEFT DOUBLE QUOTATION MARK
507 0x201D, // RIGHT DOUBLE QUOTATION MARK
508 0x2022, // BULLET
509 0x2013, // EN DASH
510 0x2014, // EM DASH
511 0x02DC, // SMALL TILDE
512 0x2122, // TRADE MARK SIGN
513 0x0161, // LATIN SMALL LETTER S WITH CARON
514 0x203A, // SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
515 0x0153, // LATIN SMALL LIGATURE OE
516 0xFFFD, // NOT USED
517 0x017E, // LATIN SMALL LETTER Z WITH CARON
518 0x0178, // LATIN CAPITAL LETTER Y WITH DIAERESIS
519 };
520
521 static bool __CFFromWinLatin1(uint32_t flags, uint8_t byte, UniChar *character) {
522 *character = (byte < 0x80 || byte > 0x9F ? (UniChar)byte : cp1252_to_uni[byte - 0x80]);
523 return (*character != 0xFFFD);
524 }
525
526 static CFIndex __CFToWinLatin1Precompose(uint32_t flags, const UniChar *character, CFIndex numChars, uint8_t *bytes, CFIndex maxByteLen, CFIndex *usedByteLen) {
527 uint8_t byte;
528 CFIndex usedCharLen;
529
530 if (__CFToWinLatin1(flags, CFStringEncodingPrecomposeLatinCharacter(character, numChars, &usedCharLen), &byte) && byte && (usedCharLen > 1)) {
531 if (maxByteLen) *bytes = byte;
532 *usedByteLen = 1;
533 return usedCharLen;
534 } else {
535 return 0;
536 }
537 }
538
539 __private_extern__ const CFStringEncodingConverter __CFConverterWinLatin1 = {
540 __CFToWinLatin1, __CFFromWinLatin1, 1, 1, kCFStringEncodingConverterCheapEightBit,
541 NULL, NULL, NULL, NULL, __CFToWinLatin1Precompose, CFStringEncodingIsValidCombiningCharacterForLatin1,
542 };
543
544 /* NEXTSTEP Encoding */
545 #define NUM_NEXTSTEP_FROM_UNI 127
546
547 static const CFStringEncodingUnicodeTo8BitCharMap nextstep_from_tab[NUM_NEXTSTEP_FROM_UNI] = {
548 { 0x00a0, 0x80 },
549 { 0x00a1, 0xa1 },
550 { 0x00a2, 0xa2 },
551 { 0x00a3, 0xa3 },
552 { 0x00a4, 0xa8 },
553 { 0x00a5, 0xa5 },
554 { 0x00a6, 0xb5 },
555 { 0x00a7, 0xa7 },
556 { 0x00a8, 0xc8 },
557 { 0x00a9, 0xa0 },
558 { 0x00aa, 0xe3 },
559 { 0x00ab, 0xab },
560 { 0x00ac, 0xbe },
561 /* { 0x00ad, 0x2d }, <= 96/10/25 rick removed; converts soft-hyphen to hyphen! */
562 { 0x00ae, 0xb0 },
563 { 0x00af, 0xc5 },
564 { 0x00b1, 0xd1 },
565 { 0x00b2, 0xc9 },
566 { 0x00b3, 0xcc },
567 { 0x00b4, 0xc2 },
568 { 0x00b5, 0x9d },
569 { 0x00b6, 0xb6 },
570 { 0x00b7, 0xb4 },
571 { 0x00b8, 0xcb },
572 { 0x00b9, 0xc0 },
573 { 0x00ba, 0xeb },
574 { 0x00bb, 0xbb },
575 { 0x00bc, 0xd2 },
576 { 0x00bd, 0xd3 },
577 { 0x00be, 0xd4 },
578 { 0x00bf, 0xbf },
579 { 0x00c0, 0x81 },
580 { 0x00c1, 0x82 },
581 { 0x00c2, 0x83 },
582 { 0x00c3, 0x84 },
583 { 0x00c4, 0x85 },
584 { 0x00c5, 0x86 },
585 { 0x00c6, 0xe1 },
586 { 0x00c7, 0x87 },
587 { 0x00c8, 0x88 },
588 { 0x00c9, 0x89 },
589 { 0x00ca, 0x8a },
590 { 0x00cb, 0x8b },
591 { 0x00cc, 0x8c },
592 { 0x00cd, 0x8d },
593 { 0x00ce, 0x8e },
594 { 0x00cf, 0x8f },
595 { 0x00d0, 0x90 },
596 { 0x00d1, 0x91 },
597 { 0x00d2, 0x92 },
598 { 0x00d3, 0x93 },
599 { 0x00d4, 0x94 },
600 { 0x00d5, 0x95 },
601 { 0x00d6, 0x96 },
602 { 0x00d7, 0x9e },
603 { 0x00d8, 0xe9 },
604 { 0x00d9, 0x97 },
605 { 0x00da, 0x98 },
606 { 0x00db, 0x99 },
607 { 0x00dc, 0x9a },
608 { 0x00dd, 0x9b },
609 { 0x00de, 0x9c },
610 { 0x00df, 0xfb },
611 { 0x00e0, 0xd5 },
612 { 0x00e1, 0xd6 },
613 { 0x00e2, 0xd7 },
614 { 0x00e3, 0xd8 },
615 { 0x00e4, 0xd9 },
616 { 0x00e5, 0xda },
617 { 0x00e6, 0xf1 },
618 { 0x00e7, 0xdb },
619 { 0x00e8, 0xdc },
620 { 0x00e9, 0xdd },
621 { 0x00ea, 0xde },
622 { 0x00eb, 0xdf },
623 { 0x00ec, 0xe0 },
624 { 0x00ed, 0xe2 },
625 { 0x00ee, 0xe4 },
626 { 0x00ef, 0xe5 },
627 { 0x00f0, 0xe6 },
628 { 0x00f1, 0xe7 },
629 { 0x00f2, 0xec },
630 { 0x00f3, 0xed },
631 { 0x00f4, 0xee },
632 { 0x00f5, 0xef },
633 { 0x00f6, 0xf0 },
634 { 0x00f7, 0x9f },
635 { 0x00f8, 0xf9 },
636 { 0x00f9, 0xf2 },
637 { 0x00fa, 0xf3 },
638 { 0x00fb, 0xf4 },
639 { 0x00fc, 0xf6 },
640 { 0x00fd, 0xf7 },
641 { 0x00fe, 0xfc },
642 { 0x00ff, 0xfd },
643 { 0x0131, 0xf5 },
644 { 0x0141, 0xe8 },
645 { 0x0142, 0xf8 },
646 { 0x0152, 0xea },
647 { 0x0153, 0xfa },
648 { 0x0192, 0xa6 },
649 { 0x02c6, 0xc3 },
650 { 0x02c7, 0xcf },
651 { 0x02cb, 0xc1 },
652 { 0x02d8, 0xc6 },
653 { 0x02d9, 0xc7 },
654 { 0x02da, 0xca },
655 { 0x02db, 0xce },
656 { 0x02dc, 0xc4 },
657 { 0x02dd, 0xcd },
658 { 0x2013, 0xb1 },
659 { 0x2014, 0xd0 },
660 { 0x2019, 0xa9 },
661 { 0x201a, 0xb8 },
662 { 0x201c, 0xaa },
663 { 0x201d, 0xba },
664 { 0x201e, 0xb9 },
665 { 0x2020, 0xb2 },
666 { 0x2021, 0xb3 },
667 { 0x2022, 0xb7 },
668 { 0x2026, 0xbc },
669 { 0x2030, 0xbd },
670 { 0x2039, 0xac },
671 { 0x203a, 0xad },
672 { 0x2044, 0xa4 },
673 { 0xfb01, 0xae },
674 { 0xfb02, 0xaf },
675 { 0xfffd, 0xff },
676 };
677
678 static bool __CFToNextStepLatin(uint32_t flags, UniChar character, uint8_t *byte) {
679 if (character < 0x80) {
680 *byte = (uint8_t)character;
681 return true;
682 } else if (__CFIsParagraphSeparator(character)) {
683 *byte = ASCIINewLine;
684 return true;
685 } else {
686 return CFStringEncodingUnicodeTo8BitEncoding(nextstep_from_tab, NUM_NEXTSTEP_FROM_UNI, character, byte);
687 }
688 };
689
690 static const UniChar NSToPrecompUnicodeTable[128] = {
691 /* NextStep Encoding Unicode */
692 /* 128 figspace */ 0x00a0, /* 0x2007 is fig space */
693 /* 129 Agrave */ 0x00c0,
694 /* 130 Aacute */ 0x00c1,
695 /* 131 Acircumflex */ 0x00c2,
696 /* 132 Atilde */ 0x00c3,
697 /* 133 Adieresis */ 0x00c4,
698 /* 134 Aring */ 0x00c5,
699 /* 135 Ccedilla */ 0x00c7,
700 /* 136 Egrave */ 0x00c8,
701 /* 137 Eacute */ 0x00c9,
702 /* 138 Ecircumflex */ 0x00ca,
703 /* 139 Edieresis */ 0x00cb,
704 /* 140 Igrave */ 0x00cc,
705 /* 141 Iacute */ 0x00cd,
706 /* 142 Icircumflex */ 0x00ce,
707 /* 143 Idieresis */ 0x00cf,
708 /* 144 Eth */ 0x00d0,
709 /* 145 Ntilde */ 0x00d1,
710 /* 146 Ograve */ 0x00d2,
711 /* 147 Oacute */ 0x00d3,
712 /* 148 Ocircumflex */ 0x00d4,
713 /* 149 Otilde */ 0x00d5,
714 /* 150 Odieresis */ 0x00d6,
715 /* 151 Ugrave */ 0x00d9,
716 /* 152 Uacute */ 0x00da,
717 /* 153 Ucircumflex */ 0x00db,
718 /* 154 Udieresis */ 0x00dc,
719 /* 155 Yacute */ 0x00dd,
720 /* 156 Thorn */ 0x00de,
721 /* 157 mu */ 0x00b5,
722 /* 158 multiply */ 0x00d7,
723 /* 159 divide */ 0x00f7,
724 /* 160 copyright */ 0x00a9,
725 /* 161 exclamdown */ 0x00a1,
726 /* 162 cent */ 0x00a2,
727 /* 163 sterling */ 0x00a3,
728 /* 164 fraction */ 0x2044,
729 /* 165 yen */ 0x00a5,
730 /* 166 florin */ 0x0192,
731 /* 167 section */ 0x00a7,
732 /* 168 currency */ 0x00a4,
733 /* 169 quotesingle */ 0x2019,
734 /* 170 quotedblleft */ 0x201c,
735 /* 171 guillemotleft */ 0x00ab,
736 /* 172 guilsinglleft */ 0x2039,
737 /* 173 guilsinglright */ 0x203a,
738 /* 174 fi */ 0xFB01,
739 /* 175 fl */ 0xFB02,
740 /* 176 registered */ 0x00ae,
741 /* 177 endash */ 0x2013,
742 /* 178 dagger */ 0x2020,
743 /* 179 daggerdbl */ 0x2021,
744 /* 180 periodcentered */ 0x00b7,
745 /* 181 brokenbar */ 0x00a6,
746 /* 182 paragraph */ 0x00b6,
747 /* 183 bullet */ 0x2022,
748 /* 184 quotesinglbase */ 0x201a,
749 /* 185 quotedblbase */ 0x201e,
750 /* 186 quotedblright */ 0x201d,
751 /* 187 guillemotright */ 0x00bb,
752 /* 188 ellipsis */ 0x2026,
753 /* 189 perthousand */ 0x2030,
754 /* 190 logicalnot */ 0x00ac,
755 /* 191 questiondown */ 0x00bf,
756 /* 192 onesuperior */ 0x00b9,
757 /* 193 grave */ 0x02cb,
758 /* 194 acute */ 0x00b4,
759 /* 195 circumflex */ 0x02c6,
760 /* 196 tilde */ 0x02dc,
761 /* 197 macron */ 0x00af,
762 /* 198 breve */ 0x02d8,
763 /* 199 dotaccent */ 0x02d9,
764 /* 200 dieresis */ 0x00a8,
765 /* 201 twosuperior */ 0x00b2,
766 /* 202 ring */ 0x02da,
767 /* 203 cedilla */ 0x00b8,
768 /* 204 threesuperior */ 0x00b3,
769 /* 205 hungarumlaut */ 0x02dd,
770 /* 206 ogonek */ 0x02db,
771 /* 207 caron */ 0x02c7,
772 /* 208 emdash */ 0x2014,
773 /* 209 plusminus */ 0x00b1,
774 /* 210 onequarter */ 0x00bc,
775 /* 211 onehalf */ 0x00bd,
776 /* 212 threequarters */ 0x00be,
777 /* 213 agrave */ 0x00e0,
778 /* 214 aacute */ 0x00e1,
779 /* 215 acircumflex */ 0x00e2,
780 /* 216 atilde */ 0x00e3,
781 /* 217 adieresis */ 0x00e4,
782 /* 218 aring */ 0x00e5,
783 /* 219 ccedilla */ 0x00e7,
784 /* 220 egrave */ 0x00e8,
785 /* 221 eacute */ 0x00e9,
786 /* 222 ecircumflex */ 0x00ea,
787 /* 223 edieresis */ 0x00eb,
788 /* 224 igrave */ 0x00ec,
789 /* 225 AE */ 0x00c6,
790 /* 226 iacute */ 0x00ed,
791 /* 227 ordfeminine */ 0x00aa,
792 /* 228 icircumflex */ 0x00ee,
793 /* 229 idieresis */ 0x00ef,
794 /* 230 eth */ 0x00f0,
795 /* 231 ntilde */ 0x00f1,
796 /* 232 Lslash */ 0x0141,
797 /* 233 Oslash */ 0x00d8,
798 /* 234 OE */ 0x0152,
799 /* 235 ordmasculine */ 0x00ba,
800 /* 236 ograve */ 0x00f2,
801 /* 237 oacute */ 0x00f3,
802 /* 238 ocircumflex */ 0x00f4,
803 /* 239 otilde */ 0x00f5,
804 /* 240 odieresis */ 0x00f6,
805 /* 241 ae */ 0x00e6,
806 /* 242 ugrave */ 0x00f9,
807 /* 243 uacute */ 0x00fa,
808 /* 244 ucircumflex */ 0x00fb,
809 /* 245 dotlessi */ 0x0131,
810 /* 246 udieresis */ 0x00fc,
811 /* 247 yacute */ 0x00fd,
812 /* 248 lslash */ 0x0142,
813 /* 249 oslash */ 0x00f8,
814 /* 250 oe */ 0x0153,
815 /* 251 germandbls */ 0x00df,
816 /* 252 thorn */ 0x00fe,
817 /* 253 ydieresis */ 0x00ff,
818 /* 254 .notdef */ 0xFFFD,
819 /* 255 .notdef */ 0xFFFD
820 };
821
822 static bool __CFFromNextStepLatin(uint32_t flags, uint8_t byte, UniChar *character) {
823 return ((*character = (byte < 0x80 ? (UniChar)byte : NSToPrecompUnicodeTable[byte - 0x80])) != 0xFFFD);
824 }
825
826 static CFIndex __CFToNextStepLatinPrecompose(uint32_t flags, const UniChar *character, CFIndex numChars, uint8_t *bytes, CFIndex maxByteLen, CFIndex *usedByteLen) {
827 uint8_t byte;
828 CFIndex usedCharLen;
829
830 if (__CFToNextStepLatin(flags, CFStringEncodingPrecomposeLatinCharacter(character, numChars, &usedCharLen), &byte) && byte && (usedCharLen > 1)) {
831 if (maxByteLen) *bytes = byte;
832 *usedByteLen = 1;
833 return usedCharLen;
834 } else {
835 return 0;
836 }
837 }
838
839 __private_extern__ const CFStringEncodingConverter __CFConverterNextStepLatin = {
840 __CFToNextStepLatin, __CFFromNextStepLatin, 1, 1, kCFStringEncodingConverterCheapEightBit,
841 NULL, NULL, NULL, NULL, __CFToNextStepLatinPrecompose, CFStringEncodingIsValidCombiningCharacterForLatin1,
842 };
843
844 /* UTF8 */
845 /*
846 * Copyright 2001 Unicode, Inc.
847 *
848 * Disclaimer
849 *
850 * This source code is provided as is by Unicode, Inc. No claims are
851 * made as to fitness for any particular purpose. No warranties of any
852 * kind are expressed or implied. The recipient agrees to determine
853 * applicability of information provided. If this file has been
854 * purchased on magnetic or optical media from Unicode, Inc., the
855 * sole remedy for any claim will be exchange of defective media
856 * within 90 days of receipt.
857 *
858 * Limitations on Rights to Redistribute This Code
859 *
860 * Unicode, Inc. hereby grants the right to freely use the information
861 * supplied in this file in the creation of products supporting the
862 * Unicode Standard, and to make copies of this file in any form
863 * for internal or external distribution as long as this notice
864 * remains attached.
865 */
866
867 static const uint32_t kReplacementCharacter = 0x0000FFFDUL;
868 static const uint32_t kMaximumUCS2 = 0x0000FFFFUL;
869 static const uint32_t kMaximumUTF16 = 0x0010FFFFUL;
870 static const uint32_t kMaximumUCS4 = 0x7FFFFFFFUL;
871
872 static const int halfShift = 10;
873 static const uint32_t halfBase = 0x0010000UL;
874 static const uint32_t halfMask = 0x3FFUL;
875 static const uint32_t kSurrogateHighStart = 0xD800UL;
876 static const uint32_t kSurrogateHighEnd = 0xDBFFUL;
877 static const uint32_t kSurrogateLowStart = 0xDC00UL;
878 static const uint32_t kSurrogateLowEnd = 0xDFFFUL;
879
880 /*
881 * Index into the table below with the first byte of a UTF-8 sequence to
882 * get the number of trailing bytes that are supposed to follow it.
883 */
884 static const char trailingBytesForUTF8[256] = {
885 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
886 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
887 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
888 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
889 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
890 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
891 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
892 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
893 };
894
895 /*
896 * Magic values subtracted from a buffer value during UTF8 conversion.
897 * This table contains as many values as there might be trailing bytes
898 * in a UTF-8 sequence.
899 */
900 static const UTF32Char offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
901 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
902
903 static const uint8_t firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
904
905 /* This code is similar in effect to making successive calls on the mbtowc and wctomb routines in FSS-UTF. However, it is considerably different in code:
906 * it is adapted to be consistent with UTF16,
907 * constants have been gathered.
908 * loops & conditionals have been removed as much as possible for
909 * efficiency, in favor of drop-through switch statements.
910 */
911
912 CF_INLINE uint16_t __CFUTF8BytesToWriteForCharacter(uint32_t ch) {
913 if (ch < 0x80) return 1;
914 else if (ch < 0x800) return 2;
915 else if (ch < 0x10000) return 3;
916 else if (ch < 0x200000) return 4;
917 else if (ch < 0x4000000) return 5;
918 else if (ch <= kMaximumUCS4) return 6;
919 else return 0;
920 }
921
922 CF_INLINE uint16_t __CFToUTF8Core(uint32_t ch, uint8_t *bytes, uint32_t maxByteLen) {
923 uint16_t bytesToWrite = __CFUTF8BytesToWriteForCharacter(ch);
924 const uint32_t byteMask = 0xBF;
925 const uint32_t byteMark = 0x80;
926
927 if (!bytesToWrite) {
928 bytesToWrite = 2;
929 ch = kReplacementCharacter;
930 }
931
932 if (maxByteLen < bytesToWrite) return 0;
933
934 switch (bytesToWrite) { /* note: code falls through cases! */
935 case 6: bytes[5] = (ch | byteMark) & byteMask; ch >>= 6;
936 case 5: bytes[4] = (ch | byteMark) & byteMask; ch >>= 6;
937 case 4: bytes[3] = (ch | byteMark) & byteMask; ch >>= 6;
938 case 3: bytes[2] = (ch | byteMark) & byteMask; ch >>= 6;
939 case 2: bytes[1] = (ch | byteMark) & byteMask; ch >>= 6;
940 case 1: bytes[0] = ch | firstByteMark[bytesToWrite];
941 }
942 return bytesToWrite;
943 }
944
945 static CFIndex __CFToUTF8(uint32_t flags, const UniChar *characters, CFIndex numChars, uint8_t *bytes, CFIndex maxByteLen, CFIndex *usedByteLen) {
946 uint16_t bytesWritten;
947 uint32_t ch;
948 const UniChar *beginCharacter = characters;
949 const UniChar *endCharacter = characters + numChars;
950 const uint8_t *beginBytes = bytes;
951 const uint8_t *endBytes = bytes + maxByteLen;
952 bool isStrict = (flags & kCFStringEncodingUseHFSPlusCanonical ? false : true);
953
954 while ((characters < endCharacter) && (!maxByteLen || (bytes < endBytes))) {
955 ch = *(characters++);
956
957 if (ch < 0x80) { // ASCII
958 if (maxByteLen) *bytes = ch;
959 ++bytes;
960 } else {
961 if (ch >= kSurrogateHighStart) {
962 if (ch <= kSurrogateHighEnd) {
963 if ((characters < endCharacter) && ((*characters >= kSurrogateLowStart) && (*characters <= kSurrogateLowEnd))) {
964 ch = ((ch - kSurrogateHighStart) << halfShift) + (*(characters++) - kSurrogateLowStart) + halfBase;
965 } else if (isStrict) {
966 --characters;
967 break;
968 }
969 } else if (isStrict && (ch <= kSurrogateLowEnd)) {
970 --characters;
971 break;
972 }
973 }
974
975 if (!(bytesWritten = (maxByteLen ? __CFToUTF8Core(ch, bytes, endBytes - bytes) : __CFUTF8BytesToWriteForCharacter(ch)))) {
976 characters -= (ch < 0x10000 ? 1 : 2);
977 break;
978 }
979 bytes += bytesWritten;
980 }
981 }
982
983 if (usedByteLen) *usedByteLen = bytes - beginBytes;
984 return characters - beginCharacter;
985 }
986
987 /*
988 * Utility routine to tell whether a sequence of bytes is legal UTF-8.
989 * This must be called with the length pre-determined by the first byte.
990 * If not calling this from ConvertUTF8to*, then the length can be set by:
991 * length = trailingBytesForUTF8[*source]+1;
992 * and the sequence is illegal right away if there aren't that many bytes
993 * available.
994 * If presented with a length > 4, this returns false. The Unicode
995 * definition of UTF-8 goes up to 4-byte sequences.
996 */
997
998 CF_INLINE bool __CFIsLegalUTF8(const uint8_t *source, CFIndex length) {
999 if (length > 4) return false;
1000
1001 const uint8_t *srcptr = source+length;
1002 uint8_t head = *source;
1003
1004 while (--srcptr > source) if ((*srcptr & 0xC0) != 0x80) return false;
1005
1006 if (((head >= 0x80) && (head < 0xC2)) || (head > 0xF4)) return false;
1007
1008 if (((head == 0xE0) && (*(source + 1) < 0xA0)) || ((head == 0xED) && (*(source + 1) > 0x9F)) || ((head == 0xF0) && (*(source + 1) < 0x90)) || ((head == 0xF4) && (*(source + 1) > 0x8F))) return false;
1009 return true;
1010 }
1011
1012 static CFIndex __CFFromUTF8(uint32_t flags, const uint8_t *bytes, CFIndex numBytes, UniChar *characters, CFIndex maxCharLen, CFIndex *usedCharLen) {
1013 const uint8_t *source = bytes;
1014 uint16_t extraBytesToRead;
1015 CFIndex theUsedCharLen = 0;
1016 uint32_t ch;
1017 bool isHFSPlus = (flags & kCFStringEncodingUseHFSPlusCanonical ? true : false);
1018 bool needsToDecompose = (flags & kCFStringEncodingUseCanonical || isHFSPlus ? true : false);
1019 bool strictUTF8 = (flags & kCFStringEncodingLenientUTF8Conversion ? false : true);
1020 UTF32Char decomposed[MAX_DECOMPOSED_LENGTH];
1021 CFIndex decompLength;
1022 bool isStrict = !isHFSPlus;
1023
1024 while (numBytes && (!maxCharLen || (theUsedCharLen < maxCharLen))) {
1025 extraBytesToRead = trailingBytesForUTF8[*source];
1026
1027 if (extraBytesToRead > --numBytes) break;
1028 numBytes -= extraBytesToRead;
1029
1030 /* Do this check whether lenient or strict */
1031 // We need to allow 0xA9 (copyright in MacRoman and Unicode) not to break existing apps
1032 // Will use a flag passed in from upper layers to switch restriction mode for this case in the next release
1033 if ((extraBytesToRead > 3) || (strictUTF8 && !__CFIsLegalUTF8(source, extraBytesToRead + 1))) {
1034 if ((*source == 0xA9) || (flags & kCFStringEncodingAllowLossyConversion)) {
1035 numBytes += extraBytesToRead;
1036 ++source;
1037 if (maxCharLen) *(characters++) = (UTF16Char)kReplacementCharacter;
1038 ++theUsedCharLen;
1039 continue;
1040 } else {
1041 break;
1042 }
1043 }
1044
1045 ch = 0;
1046 /*
1047 * The cases all fall through. See "Note A" below.
1048 */
1049 switch (extraBytesToRead) {
1050 case 3: ch += *source++; ch <<= 6;
1051 case 2: ch += *source++; ch <<= 6;
1052 case 1: ch += *source++; ch <<= 6;
1053 case 0: ch += *source++;
1054 }
1055 ch -= offsetsFromUTF8[extraBytesToRead];
1056
1057 if (ch <= kMaximumUCS2) {
1058 if (isStrict && (ch >= kSurrogateHighStart && ch <= kSurrogateLowEnd)) {
1059 source -= (extraBytesToRead + 1);
1060 break;
1061 }
1062 if (needsToDecompose && CFUniCharIsDecomposableCharacter(ch, isHFSPlus)) {
1063 decompLength = CFUniCharDecomposeCharacter(ch, decomposed, MAX_DECOMPOSED_LENGTH);
1064
1065 if (maxCharLen) {
1066 if (!CFUniCharFillDestinationBuffer(decomposed, decompLength, (void **)&characters, maxCharLen, &theUsedCharLen, kCFUniCharUTF16Format)) break;
1067 } else {
1068 theUsedCharLen += decompLength;
1069 }
1070 } else {
1071 if (maxCharLen) *(characters++) = (UTF16Char)ch;
1072 ++theUsedCharLen;
1073 }
1074 } else if (ch > kMaximumUTF16) {
1075 if (isStrict) {
1076 source -= (extraBytesToRead + 1);
1077 break;
1078 }
1079 if (maxCharLen) *(characters++) = (UTF16Char)kReplacementCharacter;
1080 ++theUsedCharLen;
1081 } else {
1082 if (needsToDecompose && CFUniCharIsDecomposableCharacter(ch, isHFSPlus)) {
1083 decompLength = CFUniCharDecomposeCharacter(ch, decomposed, MAX_DECOMPOSED_LENGTH);
1084
1085 if (maxCharLen) {
1086 if (!CFUniCharFillDestinationBuffer(decomposed, decompLength, (void **)&characters, maxCharLen, &theUsedCharLen, kCFUniCharUTF16Format)) break;
1087 } else {
1088 while (--decompLength >= 0) theUsedCharLen += (decomposed[decompLength] < 0x10000 ? 1 : 2);
1089 }
1090 } else {
1091 if (maxCharLen) {
1092 if ((theUsedCharLen + 2) > maxCharLen) break;
1093 ch -= halfBase;
1094 *(characters++) = (ch >> halfShift) + kSurrogateHighStart;
1095 *(characters++) = (ch & halfMask) + kSurrogateLowStart;
1096 }
1097 theUsedCharLen += 2;
1098 }
1099 }
1100 }
1101
1102 if (usedCharLen) *usedCharLen = theUsedCharLen;
1103
1104 return source - bytes;
1105 }
1106
1107 static CFIndex __CFToUTF8Len(uint32_t flags, const UniChar *characters, CFIndex numChars) {
1108 uint32_t bytesToWrite = 0;
1109 uint32_t ch;
1110
1111 while (numChars) {
1112 ch = *characters++;
1113 numChars--;
1114 if ((ch >= kSurrogateHighStart && ch <= kSurrogateHighEnd) && numChars && (*characters >= kSurrogateLowStart && *characters <= kSurrogateLowEnd)) {
1115 ch = ((ch - kSurrogateHighStart) << halfShift) + (*characters++ - kSurrogateLowStart) + halfBase;
1116 numChars--;
1117 }
1118 bytesToWrite += __CFUTF8BytesToWriteForCharacter(ch);
1119 }
1120
1121 return bytesToWrite;
1122 }
1123
1124 static CFIndex __CFFromUTF8Len(uint32_t flags, const uint8_t *source, CFIndex numBytes) {
1125 uint16_t extraBytesToRead;
1126 CFIndex theUsedCharLen = 0;
1127 uint32_t ch;
1128 bool isHFSPlus = (flags & kCFStringEncodingUseHFSPlusCanonical ? true : false);
1129 bool needsToDecompose = (flags & kCFStringEncodingUseCanonical || isHFSPlus ? true : false);
1130 bool strictUTF8 = (flags & kCFStringEncodingLenientUTF8Conversion ? false : true);
1131 UTF32Char decomposed[MAX_DECOMPOSED_LENGTH];
1132 CFIndex decompLength;
1133 bool isStrict = !isHFSPlus;
1134
1135 while (numBytes) {
1136 extraBytesToRead = trailingBytesForUTF8[*source];
1137
1138 if (extraBytesToRead > --numBytes) break;
1139 numBytes -= extraBytesToRead;
1140
1141 /* Do this check whether lenient or strict */
1142 // We need to allow 0xA9 (copyright in MacRoman and Unicode) not to break existing apps
1143 // Will use a flag passed in from upper layers to switch restriction mode for this case in the next release
1144 if ((extraBytesToRead > 3) || (strictUTF8 && !__CFIsLegalUTF8(source, extraBytesToRead + 1))) {
1145 if ((*source == 0xA9) || (flags & kCFStringEncodingAllowLossyConversion)) {
1146 numBytes += extraBytesToRead;
1147 ++source;
1148 ++theUsedCharLen;
1149 continue;
1150 } else {
1151 break;
1152 }
1153 }
1154
1155
1156 ch = 0;
1157 /*
1158 * The cases all fall through. See "Note A" below.
1159 */
1160 switch (extraBytesToRead) {
1161 case 3: ch += *source++; ch <<= 6;
1162 case 2: ch += *source++; ch <<= 6;
1163 case 1: ch += *source++; ch <<= 6;
1164 case 0: ch += *source++;
1165 }
1166 ch -= offsetsFromUTF8[extraBytesToRead];
1167
1168 if (ch <= kMaximumUCS2) {
1169 if (isStrict && (ch >= kSurrogateHighStart && ch <= kSurrogateLowEnd)) {
1170 break;
1171 }
1172 if (needsToDecompose && CFUniCharIsDecomposableCharacter(ch, isHFSPlus)) {
1173 decompLength = CFUniCharDecomposeCharacter(ch, decomposed, MAX_DECOMPOSED_LENGTH);
1174 theUsedCharLen += decompLength;
1175 } else {
1176 ++theUsedCharLen;
1177 }
1178 } else if (ch > kMaximumUTF16) {
1179 ++theUsedCharLen;
1180 } else {
1181 if (needsToDecompose && CFUniCharIsDecomposableCharacter(ch, isHFSPlus)) {
1182 decompLength = CFUniCharDecomposeCharacter(ch, decomposed, MAX_DECOMPOSED_LENGTH);
1183 while (--decompLength >= 0) theUsedCharLen += (decomposed[decompLength] < 0x10000 ? 1 : 2);
1184 } else {
1185 theUsedCharLen += 2;
1186 }
1187 }
1188 }
1189
1190 return theUsedCharLen;
1191 }
1192
1193 __private_extern__ const CFStringEncodingConverter __CFConverterUTF8 = {
1194 __CFToUTF8, __CFFromUTF8, 3, 2, kCFStringEncodingConverterStandard,
1195 __CFToUTF8Len, __CFFromUTF8Len, NULL, NULL, NULL, NULL,
1196 };