2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 Includes Unicode 3.2 decomposition code derived from Core Foundation
28 #include <sys/param.h>
29 #include <sys/utfconv.h>
30 #include <sys/errno.h>
31 #include <architecture/byte_order.h>
34 * UTF-8 (Unicode Transformation Format)
36 * UTF-8 is the Unicode Transformation Format that serializes a Unicode
37 * character as a sequence of one to four bytes. Only the shortest form
38 * required to represent the significant Unicode bits is legal.
40 * UTF-8 Multibyte Codes
42 * Bytes Bits Unicode Min Unicode Max UTF-8 Byte Sequence (binary)
43 * -----------------------------------------------------------------------------
44 * 1 7 0x0000 0x007F 0xxxxxxx
45 * 2 11 0x0080 0x07FF 110xxxxx 10xxxxxx
46 * 3 16 0x0800 0xFFFF 1110xxxx 10xxxxxx 10xxxxxx
47 * 4 21 0x10000 0x10FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
48 * -----------------------------------------------------------------------------
52 #define UNICODE_TO_UTF8_LEN(c) \
53 ((c) < 0x0080 ? 1 : ((c) < 0x0800 ? 2 : (((c) & 0xf800) == 0xd800 ? 2 : 3)))
55 #define UCS_ALT_NULL 0x2400
57 /* Surrogate Pair Constants */
58 #define SP_HALF_SHIFT 10
59 #define SP_HALF_BASE 0x0010000UL
60 #define SP_HALF_MASK 0x3FFUL
62 #define SP_HIGH_FIRST 0xD800UL
63 #define SP_HIGH_LAST 0xDBFFUL
64 #define SP_LOW_FIRST 0xDC00UL
65 #define SP_LOW_LAST 0xDFFFUL
68 #include "vfs_utfconvdata.h"
72 * Test for a combining character.
74 * Similar to __CFUniCharIsNonBaseCharacter except that
75 * unicode_combinable also includes Hangul Jamo characters.
78 unicode_combinable(u_int16_t character
)
80 const u_int8_t
*bitmap
= __CFUniCharCombiningBitmap
;
83 if (character
< 0x0300)
86 value
= bitmap
[(character
>> 8) & 0xFF];
91 bitmap
= bitmap
+ ((value
- 1) * 32) + 256;
92 return (bitmap
[(character
& 0xFF) / 8] & (1 << (character
% 8)) ? 1 : 0);
98 * Test for a precomposed character.
100 * Similar to __CFUniCharIsDecomposableCharacter.
103 unicode_decomposeable(u_int16_t character
) {
104 const u_int8_t
*bitmap
= __CFUniCharDecomposableBitmap
;
107 if (character
< 0x00C0)
110 value
= bitmap
[(character
>> 8) & 0xFF];
115 bitmap
= bitmap
+ ((value
- 1) * 32) + 256;
116 return (bitmap
[(character
& 0xFF) / 8] & (1 << (character
% 8)) ? 1 : 0);
121 static int unicode_decompose(u_int16_t character
, u_int16_t
*convertedChars
);
123 static u_int16_t
unicode_combine(u_int16_t base
, u_int16_t combining
);
126 char utf_extrabytes
[32] = {
127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
128 -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 2, 2, 3, -1
133 * utf8_encodelen - Calculates the UTF-8 encoding length for a Unicode filename
136 * If '/' chars are allowed on disk then an alternate
137 * (replacement) char must be provided in altslash.
140 * UTF_REVERSE_ENDIAN: Unicode byteorder is opposite current runtime
143 utf8_encodelen(const u_int16_t
* ucsp
, size_t ucslen
, u_int16_t altslash
,
148 int swapbytes
= (flags
& UTF_REVERSE_ENDIAN
);
151 charcnt
= ucslen
/ 2;
154 while (charcnt
-- > 0) {
158 ucs_ch
= NXSwapShort(ucs_ch
);
160 ucs_ch
= altslash
? altslash
: '_';
161 else if (ucs_ch
== '\0')
162 ucs_ch
= UCS_ALT_NULL
;
164 len
+= UNICODE_TO_UTF8_LEN(ucs_ch
);
172 * utf8_encodestr - Encodes a Unicode string to UTF-8
175 * The resulting UTF-8 string is NULL terminated.
177 * If '/' chars are allowed on disk then an alternate
178 * (replacement) char must be provided in altslash.
181 * UTF_REVERSE_ENDIAN: Unicode byteorder is opposite current runtime
182 * UTF_NO_NULL_TERM: don't add NULL termination to UTF-8 output
185 * ENAMETOOLONG: Name didn't fit; only buflen bytes were encoded
186 * EINVAL: Illegal char found; char was replaced by an '_'.
189 utf8_encodestr(const u_int16_t
* ucsp
, size_t ucslen
, u_int8_t
* utf8p
,
190 size_t * utf8len
, size_t buflen
, u_int16_t altslash
, int flags
)
195 u_int16_t
* chp
= NULL
;
196 u_int16_t sequence
[8];
199 int swapbytes
= (flags
& UTF_REVERSE_ENDIAN
);
200 int nullterm
= ((flags
& UTF_NO_NULL_TERM
) == 0);
201 int decompose
= (flags
& UTF_DECOMPOSED
);
205 bufend
= bufstart
+ buflen
;
208 charcnt
= ucslen
/ 2;
210 while (charcnt
-- > 0) {
215 ucs_ch
= swapbytes
? NXSwapShort(*ucsp
++) : *ucsp
++;
217 if (decompose
&& unicode_decomposeable(ucs_ch
)) {
218 extra
= unicode_decompose(ucs_ch
, sequence
) - 1;
220 ucs_ch
= sequence
[0];
225 /* Slash and NULL are not permitted */
233 } else if (ucs_ch
== '\0') {
234 ucs_ch
= UCS_ALT_NULL
;
237 if (ucs_ch
< 0x0080) {
238 if (utf8p
>= bufend
) {
239 result
= ENAMETOOLONG
;
244 } else if (ucs_ch
< 0x800) {
245 if ((utf8p
+ 1) >= bufend
) {
246 result
= ENAMETOOLONG
;
249 *utf8p
++ = 0xc0 | (ucs_ch
>> 6);
250 *utf8p
++ = 0x80 | (0x3f & ucs_ch
);
253 /* Combine valid surrogate pairs */
254 if (ucs_ch
>= SP_HIGH_FIRST
&& ucs_ch
<= SP_HIGH_LAST
259 ch2
= swapbytes
? NXSwapShort(*ucsp
) : *ucsp
;
260 if (ch2
>= SP_LOW_FIRST
&& ch2
<= SP_LOW_LAST
) {
261 pair
= ((ucs_ch
- SP_HIGH_FIRST
) << SP_HALF_SHIFT
)
262 + (ch2
- SP_LOW_FIRST
) + SP_HALF_BASE
;
263 if ((utf8p
+ 3) >= bufend
) {
264 result
= ENAMETOOLONG
;
269 *utf8p
++ = 0xf0 | (pair
>> 18);
270 *utf8p
++ = 0x80 | (0x3f & (pair
>> 12));
271 *utf8p
++ = 0x80 | (0x3f & (pair
>> 6));
272 *utf8p
++ = 0x80 | (0x3f & pair
);
276 if ((utf8p
+ 2) >= bufend
) {
277 result
= ENAMETOOLONG
;
280 *utf8p
++ = 0xe0 | (ucs_ch
>> 12);
281 *utf8p
++ = 0x80 | (0x3f & (ucs_ch
>> 6));
282 *utf8p
++ = 0x80 | (0x3f & ucs_ch
);
286 *utf8len
= utf8p
- bufstart
;
295 * utf8_decodestr - Decodes a UTF-8 string back to Unicode
298 * The input UTF-8 string does not need to be null terminated
301 * If '/' chars are allowed on disk then an alternate
302 * (replacement) char must be provided in altslash.
305 * UTF_REV_ENDIAN: Unicode byteorder is oposite current runtime
306 * UTF_DECOMPOSED: Unicode output string must be fully decompsed
309 * ENAMETOOLONG: Name didn't fit; only ucslen chars were decoded.
310 * EINVAL: Illegal UTF-8 sequence found.
313 utf8_decodestr(const u_int8_t
* utf8p
, size_t utf8len
, u_int16_t
* ucsp
,
314 size_t *ucslen
, size_t buflen
, u_int16_t altslash
, int flags
)
321 int decompose
, precompose
, swapbytes
;
323 decompose
= (flags
& UTF_DECOMPOSED
);
324 precompose
= (flags
& UTF_PRECOMPOSED
);
325 swapbytes
= (flags
& UTF_REVERSE_ENDIAN
);
328 bufend
= (u_int16_t
*)((u_int8_t
*)ucsp
+ buflen
);
330 while (utf8len
-- > 0 && (byte
= *utf8p
++) != '\0') {
334 /* check for ascii */
336 ucs_ch
= byte
; /* 1st byte */
339 int extrabytes
= utf_extrabytes
[byte
>> 3];
341 if (utf8len
< extrabytes
)
343 utf8len
-= extrabytes
;
345 switch (extrabytes
) {
347 ch
= byte
; ch
<<= 6; /* 1st byte */
348 byte
= *utf8p
++; /* 2nd byte */
349 if ((byte
>> 6) != 2)
358 ch
= byte
; ch
<<= 6; /* 1st byte */
359 byte
= *utf8p
++; /* 2nd byte */
360 if ((byte
>> 6) != 2)
362 ch
+= byte
; ch
<<= 6;
363 byte
= *utf8p
++; /* 3rd byte */
364 if ((byte
>> 6) != 2)
373 if (ch
== 0xFFFE || ch
== 0xFFFF)
379 ch
= byte
; ch
<<= 6; /* 1st byte */
380 byte
= *utf8p
++; /* 2nd byte */
381 if ((byte
>> 6) != 2)
383 ch
+= byte
; ch
<<= 6;
384 byte
= *utf8p
++; /* 3rd byte */
385 if ((byte
>> 6) != 2)
387 ch
+= byte
; ch
<<= 6;
388 byte
= *utf8p
++; /* 4th byte */
389 if ((byte
>> 6) != 2)
392 ch
-= 0x03C82080UL
+ SP_HALF_BASE
;
393 ucs_ch
= (ch
>> SP_HALF_SHIFT
) + SP_HIGH_FIRST
;
394 if (ucs_ch
< SP_HIGH_FIRST
|| ucs_ch
> SP_HIGH_LAST
)
396 *ucsp
++ = swapbytes
? NXSwapShort(ucs_ch
) : ucs_ch
;
399 ucs_ch
= (ch
& SP_HALF_MASK
) + SP_LOW_FIRST
;
400 if (ucs_ch
< SP_LOW_FIRST
|| ucs_ch
> SP_LOW_LAST
)
402 *ucsp
++ = swapbytes
? NXSwapShort(ucs_ch
) : ucs_ch
;
408 if (unicode_decomposeable(ucs_ch
)) {
409 u_int16_t sequence
[8];
412 count
= unicode_decompose(ucs_ch
, sequence
);
414 for (i
= 0; i
< count
; ++i
) {
415 ucs_ch
= sequence
[i
];
416 *ucsp
++ = swapbytes
? NXSwapShort(ucs_ch
) : ucs_ch
;
422 } else if (precompose
&& (ucsp
!= bufstart
)) {
423 u_int16_t composite
, base
;
425 if (unicode_combinable(ucs_ch
)) {
426 base
= swapbytes
? NXSwapShort(*(ucsp
- 1)) : *(ucsp
- 1);
427 composite
= unicode_combine(base
, ucs_ch
);
434 if (ucs_ch
== UCS_ALT_NULL
)
437 if (ucs_ch
== altslash
)
440 *ucsp
++ = swapbytes
? NXSwapShort(ucs_ch
) : ucs_ch
;
444 *ucslen
= (u_int8_t
*)ucsp
- (u_int8_t
*)bufstart
;
453 result
= ENAMETOOLONG
;
459 * utf8_validatestr - Check for a valid UTF-8 string.
462 utf8_validatestr(const u_int8_t
* utf8p
, size_t utf8len
)
469 while (utf8len
-- > 0 && (byte
= *utf8p
++) != '\0') {
471 continue; /* plain ascii */
473 extrabytes
= utf_extrabytes
[byte
>> 3];
475 if (utf8len
< extrabytes
)
477 utf8len
-= extrabytes
;
479 switch (extrabytes
) {
481 ch
= byte
; ch
<<= 6; /* 1st byte */
482 byte
= *utf8p
++; /* 2nd byte */
483 if ((byte
>> 6) != 2)
491 ch
= byte
; ch
<<= 6; /* 1st byte */
492 byte
= *utf8p
++; /* 2nd byte */
493 if ((byte
>> 6) != 2)
495 ch
+= byte
; ch
<<= 6;
496 byte
= *utf8p
++; /* 3rd byte */
497 if ((byte
>> 6) != 2)
506 if (ch
== 0xFFFE || ch
== 0xFFFF)
511 ch
= byte
; ch
<<= 6; /* 1st byte */
512 byte
= *utf8p
++; /* 2nd byte */
513 if ((byte
>> 6) != 2)
515 ch
+= byte
; ch
<<= 6;
516 byte
= *utf8p
++; /* 3rd byte */
517 if ((byte
>> 6) != 2)
519 ch
+= byte
; ch
<<= 6;
520 byte
= *utf8p
++; /* 4th byte */
521 if ((byte
>> 6) != 2)
524 ch
-= 0x03C82080UL
+ SP_HALF_BASE
;
525 ucs_ch
= (ch
>> SP_HALF_SHIFT
) + SP_HIGH_FIRST
;
526 if (ucs_ch
< SP_HIGH_FIRST
|| ucs_ch
> SP_HIGH_LAST
)
528 ucs_ch
= (ch
& SP_HALF_MASK
) + SP_LOW_FIRST
;
529 if (ucs_ch
< SP_LOW_FIRST
|| ucs_ch
> SP_LOW_LAST
)
544 * Unicode 3.2 decomposition code (derived from Core Foundation)
550 } unicode_mappings32
;
552 static inline u_int32_t
553 getmappedvalue32(const unicode_mappings32
*theTable
, u_int32_t numElem
,
556 const unicode_mappings32
*p
, *q
, *divider
;
558 if ((character
< theTable
[0]._key
) || (character
> theTable
[numElem
-1]._key
))
564 divider
= p
+ ((q
- p
) >> 1); /* divide by 2 */
565 if (character
< divider
->_key
) { q
= divider
- 1; }
566 else if (character
> divider
->_key
) { p
= divider
+ 1; }
567 else { return (divider
->_value
); }
572 #define RECURSIVE_DECOMPOSITION (1 << 15)
573 #define EXTRACT_COUNT(value) (((value) >> 12) & 0x0007)
578 } unicode_mappings16
;
580 static inline u_int16_t
581 getmappedvalue16(const unicode_mappings16
*theTable
, u_int32_t numElem
,
584 const unicode_mappings16
*p
, *q
, *divider
;
586 if ((character
< theTable
[0]._key
) || (character
> theTable
[numElem
-1]._key
))
592 divider
= p
+ ((q
- p
) >> 1); /* divide by 2 */
593 if (character
< divider
->_key
)
595 else if (character
> divider
->_key
)
598 return (divider
->_value
);
605 unicode_recursive_decompose(u_int16_t character
, u_int16_t
*convertedChars
)
611 const u_int16_t
*bmpMappings
;
612 u_int32_t usedLength
;
614 value
= getmappedvalue16(
615 (const unicode_mappings16
*)__CFUniCharDecompositionTable
,
616 __UniCharDecompositionTableLength
, character
);
617 length
= EXTRACT_COUNT(value
);
618 firstChar
= value
& 0x0FFF;
620 bmpMappings
= (length
== 1 ? &theChar
: __CFUniCharMultipleDecompositionTable
+ firstChar
);
623 if (value
& RECURSIVE_DECOMPOSITION
) {
624 usedLength
= unicode_recursive_decompose((u_int16_t
)*bmpMappings
, convertedChars
);
626 --length
; /* Decrement for the first char */
630 convertedChars
+= usedLength
;
633 usedLength
+= length
;
636 *(convertedChars
++) = *(bmpMappings
++);
641 #define HANGUL_SBASE 0xAC00
642 #define HANGUL_LBASE 0x1100
643 #define HANGUL_VBASE 0x1161
644 #define HANGUL_TBASE 0x11A7
646 #define HANGUL_SCOUNT 11172
647 #define HANGUL_LCOUNT 19
648 #define HANGUL_VCOUNT 21
649 #define HANGUL_TCOUNT 28
650 #define HANGUL_NCOUNT (HANGUL_VCOUNT * HANGUL_TCOUNT)
653 * unicode_decompose - decompose a composed Unicode char
655 * Composed Unicode characters are forbidden on
656 * HFS Plus volumes. ucs_decompose will convert a
657 * composed character into its correct decomposed
660 * Similar to CFUniCharDecomposeCharacter
663 unicode_decompose(u_int16_t character
, u_int16_t
*convertedChars
)
665 if ((character
>= HANGUL_SBASE
) &&
666 (character
<= (HANGUL_SBASE
+ HANGUL_SCOUNT
))) {
669 character
-= HANGUL_SBASE
;
670 length
= (character
% HANGUL_TCOUNT
? 3 : 2);
672 *(convertedChars
++) =
673 character
/ HANGUL_NCOUNT
+ HANGUL_LBASE
;
674 *(convertedChars
++) =
675 (character
% HANGUL_NCOUNT
) / HANGUL_TCOUNT
+ HANGUL_VBASE
;
677 *convertedChars
= (character
% HANGUL_TCOUNT
) + HANGUL_TBASE
;
680 return (unicode_recursive_decompose(character
, convertedChars
));
685 * unicode_combine - generate a precomposed Unicode char
687 * Precomposed Unicode characters are required for some volume
688 * formats and network protocols. unicode_combine will combine
689 * a decomposed character sequence into a single precomposed
690 * (composite) character.
692 * Similar toCFUniCharPrecomposeCharacter but unicode_combine
693 * also handles Hangul Jamo characters.
696 unicode_combine(u_int16_t base
, u_int16_t combining
)
701 if ((combining
>= HANGUL_VBASE
) && (combining
< (HANGUL_TBASE
+ HANGUL_TCOUNT
))) {
702 /* 2 char Hangul sequences */
703 if ((combining
< (HANGUL_VBASE
+ HANGUL_VCOUNT
)) &&
704 (base
>= HANGUL_LBASE
&& base
< (HANGUL_LBASE
+ HANGUL_LCOUNT
))) {
705 return (HANGUL_SBASE
+
706 ((base
- HANGUL_LBASE
)*(HANGUL_VCOUNT
*HANGUL_TCOUNT
)) +
707 ((combining
- HANGUL_VBASE
)*HANGUL_TCOUNT
));
710 /* 3 char Hangul sequences */
711 if ((combining
> HANGUL_TBASE
) &&
712 (base
>= HANGUL_SBASE
&& base
< (HANGUL_SBASE
+ HANGUL_SCOUNT
))) {
713 if ((base
- HANGUL_SBASE
) % HANGUL_TCOUNT
)
716 return (base
+ (combining
- HANGUL_TBASE
));
720 value
= getmappedvalue32(
721 (const unicode_mappings32
*)__CFUniCharPrecompSourceTable
,
722 __CFUniCharPrecompositionTableLength
, combining
);
725 value
= getmappedvalue16(
726 (const unicode_mappings16
*)
727 ((u_int32_t
*)__CFUniCharBMPPrecompDestinationTable
+ (value
& 0xFFFF)),
728 (value
>> 16), base
);