2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
32 Includes Unicode 3.2 decomposition code derived from Core Foundation
35 #include <sys/param.h>
36 #include <sys/utfconv.h>
37 #include <sys/errno.h>
38 #include <architecture/byte_order.h>
41 * UTF-8 (Unicode Transformation Format)
43 * UTF-8 is the Unicode Transformation Format that serializes a Unicode
44 * character as a sequence of one to four bytes. Only the shortest form
45 * required to represent the significant Unicode bits is legal.
47 * UTF-8 Multibyte Codes
49 * Bytes Bits Unicode Min Unicode Max UTF-8 Byte Sequence (binary)
50 * -----------------------------------------------------------------------------
51 * 1 7 0x0000 0x007F 0xxxxxxx
52 * 2 11 0x0080 0x07FF 110xxxxx 10xxxxxx
53 * 3 16 0x0800 0xFFFF 1110xxxx 10xxxxxx 10xxxxxx
54 * 4 21 0x10000 0x10FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
55 * -----------------------------------------------------------------------------
59 #define UNICODE_TO_UTF8_LEN(c) \
60 ((c) < 0x0080 ? 1 : ((c) < 0x0800 ? 2 : (((c) & 0xf800) == 0xd800 ? 2 : 3)))
62 #define UCS_ALT_NULL 0x2400
64 /* Surrogate Pair Constants */
65 #define SP_HALF_SHIFT 10
66 #define SP_HALF_BASE 0x0010000UL
67 #define SP_HALF_MASK 0x3FFUL
69 #define SP_HIGH_FIRST 0xD800UL
70 #define SP_HIGH_LAST 0xDBFFUL
71 #define SP_LOW_FIRST 0xDC00UL
72 #define SP_LOW_LAST 0xDFFFUL
75 #include "vfs_utfconvdata.h"
79 * Test for a combining character.
81 * Similar to __CFUniCharIsNonBaseCharacter except that
82 * unicode_combinable also includes Hangul Jamo characters.
85 unicode_combinable(u_int16_t character
)
87 const u_int8_t
*bitmap
= __CFUniCharCombiningBitmap
;
90 if (character
< 0x0300)
93 value
= bitmap
[(character
>> 8) & 0xFF];
98 bitmap
= bitmap
+ ((value
- 1) * 32) + 256;
99 return (bitmap
[(character
& 0xFF) / 8] & (1 << (character
% 8)) ? 1 : 0);
105 * Test for a precomposed character.
107 * Similar to __CFUniCharIsDecomposableCharacter.
110 unicode_decomposeable(u_int16_t character
) {
111 const u_int8_t
*bitmap
= __CFUniCharDecomposableBitmap
;
114 if (character
< 0x00C0)
117 value
= bitmap
[(character
>> 8) & 0xFF];
122 bitmap
= bitmap
+ ((value
- 1) * 32) + 256;
123 return (bitmap
[(character
& 0xFF) / 8] & (1 << (character
% 8)) ? 1 : 0);
130 * Get the combing class.
132 * Similar to CFUniCharGetCombiningPropertyForCharacter.
134 static inline u_int8_t
135 get_combining_class(u_int16_t character
) {
136 const u_int8_t
*bitmap
= __CFUniCharCombiningPropertyBitmap
;
138 u_int8_t value
= bitmap
[(character
>> 8)];
141 bitmap
= bitmap
+ (value
* 256);
142 return bitmap
[character
% 256];
148 static int unicode_decompose(u_int16_t character
, u_int16_t
*convertedChars
);
150 static u_int16_t
unicode_combine(u_int16_t base
, u_int16_t combining
);
152 static void priortysort(u_int16_t
* characters
, int count
);
154 char utf_extrabytes
[32] = {
155 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
156 -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 2, 2, 3, -1
161 * utf8_encodelen - Calculates the UTF-8 encoding length for a Unicode filename
164 * If '/' chars are allowed on disk then an alternate
165 * (replacement) char must be provided in altslash.
168 * UTF_REVERSE_ENDIAN: Unicode byteorder is opposite current runtime
171 utf8_encodelen(const u_int16_t
* ucsp
, size_t ucslen
, u_int16_t altslash
,
176 int swapbytes
= (flags
& UTF_REVERSE_ENDIAN
);
179 charcnt
= ucslen
/ 2;
182 while (charcnt
-- > 0) {
186 ucs_ch
= NXSwapShort(ucs_ch
);
188 ucs_ch
= altslash
? altslash
: '_';
189 else if (ucs_ch
== '\0')
190 ucs_ch
= UCS_ALT_NULL
;
192 len
+= UNICODE_TO_UTF8_LEN(ucs_ch
);
200 * utf8_encodestr - Encodes a Unicode string to UTF-8
203 * The resulting UTF-8 string is NULL terminated.
205 * If '/' chars are allowed on disk then an alternate
206 * (replacement) char must be provided in altslash.
209 * UTF_REVERSE_ENDIAN: Unicode byteorder is opposite current runtime
210 * UTF_NO_NULL_TERM: don't add NULL termination to UTF-8 output
213 * ENAMETOOLONG: Name didn't fit; only buflen bytes were encoded
214 * EINVAL: Illegal char found; char was replaced by an '_'.
217 utf8_encodestr(const u_int16_t
* ucsp
, size_t ucslen
, u_int8_t
* utf8p
,
218 size_t * utf8len
, size_t buflen
, u_int16_t altslash
, int flags
)
223 u_int16_t
* chp
= NULL
;
224 u_int16_t sequence
[8];
227 int swapbytes
= (flags
& UTF_REVERSE_ENDIAN
);
228 int nullterm
= ((flags
& UTF_NO_NULL_TERM
) == 0);
229 int decompose
= (flags
& UTF_DECOMPOSED
);
233 bufend
= bufstart
+ buflen
;
236 charcnt
= ucslen
/ 2;
238 while (charcnt
-- > 0) {
243 ucs_ch
= swapbytes
? NXSwapShort(*ucsp
++) : *ucsp
++;
245 if (decompose
&& unicode_decomposeable(ucs_ch
)) {
246 extra
= unicode_decompose(ucs_ch
, sequence
) - 1;
248 ucs_ch
= sequence
[0];
253 /* Slash and NULL are not permitted */
261 } else if (ucs_ch
== '\0') {
262 ucs_ch
= UCS_ALT_NULL
;
265 if (ucs_ch
< 0x0080) {
266 if (utf8p
>= bufend
) {
267 result
= ENAMETOOLONG
;
272 } else if (ucs_ch
< 0x800) {
273 if ((utf8p
+ 1) >= bufend
) {
274 result
= ENAMETOOLONG
;
277 *utf8p
++ = 0xc0 | (ucs_ch
>> 6);
278 *utf8p
++ = 0x80 | (0x3f & ucs_ch
);
281 /* Combine valid surrogate pairs */
282 if (ucs_ch
>= SP_HIGH_FIRST
&& ucs_ch
<= SP_HIGH_LAST
287 ch2
= swapbytes
? NXSwapShort(*ucsp
) : *ucsp
;
288 if (ch2
>= SP_LOW_FIRST
&& ch2
<= SP_LOW_LAST
) {
289 pair
= ((ucs_ch
- SP_HIGH_FIRST
) << SP_HALF_SHIFT
)
290 + (ch2
- SP_LOW_FIRST
) + SP_HALF_BASE
;
291 if ((utf8p
+ 3) >= bufend
) {
292 result
= ENAMETOOLONG
;
297 *utf8p
++ = 0xf0 | (pair
>> 18);
298 *utf8p
++ = 0x80 | (0x3f & (pair
>> 12));
299 *utf8p
++ = 0x80 | (0x3f & (pair
>> 6));
300 *utf8p
++ = 0x80 | (0x3f & pair
);
304 if ((utf8p
+ 2) >= bufend
) {
305 result
= ENAMETOOLONG
;
308 *utf8p
++ = 0xe0 | (ucs_ch
>> 12);
309 *utf8p
++ = 0x80 | (0x3f & (ucs_ch
>> 6));
310 *utf8p
++ = 0x80 | (0x3f & ucs_ch
);
314 *utf8len
= utf8p
- bufstart
;
323 * utf8_decodestr - Decodes a UTF-8 string back to Unicode
326 * The input UTF-8 string does not need to be null terminated
329 * If '/' chars are allowed on disk then an alternate
330 * (replacement) char must be provided in altslash.
333 * UTF_REV_ENDIAN: Unicode byteorder is oposite current runtime
334 * UTF_DECOMPOSED: Unicode output string must be fully decompsed
337 * ENAMETOOLONG: Name didn't fit; only ucslen chars were decoded.
338 * EINVAL: Illegal UTF-8 sequence found.
341 utf8_decodestr(const u_int8_t
* utf8p
, size_t utf8len
, u_int16_t
* ucsp
,
342 size_t *ucslen
, size_t buflen
, u_int16_t altslash
, int flags
)
350 int decompose
, precompose
, swapbytes
;
352 decompose
= (flags
& UTF_DECOMPOSED
);
353 precompose
= (flags
& UTF_PRECOMPOSED
);
354 swapbytes
= (flags
& UTF_REVERSE_ENDIAN
);
357 bufend
= (u_int16_t
*)((u_int8_t
*)ucsp
+ buflen
);
359 while (utf8len
-- > 0 && (byte
= *utf8p
++) != '\0') {
363 /* check for ascii */
365 ucs_ch
= byte
; /* 1st byte */
368 int extrabytes
= utf_extrabytes
[byte
>> 3];
370 if (utf8len
< extrabytes
)
372 utf8len
-= extrabytes
;
374 switch (extrabytes
) {
376 ch
= byte
; ch
<<= 6; /* 1st byte */
377 byte
= *utf8p
++; /* 2nd byte */
378 if ((byte
>> 6) != 2)
387 ch
= byte
; ch
<<= 6; /* 1st byte */
388 byte
= *utf8p
++; /* 2nd byte */
389 if ((byte
>> 6) != 2)
391 ch
+= byte
; ch
<<= 6;
392 byte
= *utf8p
++; /* 3rd byte */
393 if ((byte
>> 6) != 2)
402 if (ch
== 0xFFFE || ch
== 0xFFFF)
408 ch
= byte
; ch
<<= 6; /* 1st byte */
409 byte
= *utf8p
++; /* 2nd byte */
410 if ((byte
>> 6) != 2)
412 ch
+= byte
; ch
<<= 6;
413 byte
= *utf8p
++; /* 3rd byte */
414 if ((byte
>> 6) != 2)
416 ch
+= byte
; ch
<<= 6;
417 byte
= *utf8p
++; /* 4th byte */
418 if ((byte
>> 6) != 2)
421 ch
-= 0x03C82080UL
+ SP_HALF_BASE
;
422 ucs_ch
= (ch
>> SP_HALF_SHIFT
) + SP_HIGH_FIRST
;
423 if (ucs_ch
< SP_HIGH_FIRST
|| ucs_ch
> SP_HIGH_LAST
)
425 *ucsp
++ = swapbytes
? NXSwapShort(ucs_ch
) : ucs_ch
;
428 ucs_ch
= (ch
& SP_HALF_MASK
) + SP_LOW_FIRST
;
429 if (ucs_ch
< SP_LOW_FIRST
|| ucs_ch
> SP_LOW_LAST
)
431 *ucsp
++ = swapbytes
? NXSwapShort(ucs_ch
) : ucs_ch
;
437 if (unicode_decomposeable(ucs_ch
)) {
438 u_int16_t sequence
[8];
441 count
= unicode_decompose(ucs_ch
, sequence
);
443 for (i
= 0; i
< count
; ++i
) {
444 ucs_ch
= sequence
[i
];
445 *ucsp
++ = swapbytes
? NXSwapShort(ucs_ch
) : ucs_ch
;
449 combcharcnt
+= count
- 1;
452 } else if (precompose
&& (ucsp
!= bufstart
)) {
453 u_int16_t composite
, base
;
455 if (unicode_combinable(ucs_ch
)) {
456 base
= swapbytes
? NXSwapShort(*(ucsp
- 1)) : *(ucsp
- 1);
457 composite
= unicode_combine(base
, ucs_ch
);
464 if (ucs_ch
== UCS_ALT_NULL
)
467 if (ucs_ch
== altslash
)
471 * Make multiple combining character sequences canonical
473 if (unicode_combinable(ucs_ch
)) {
474 ++combcharcnt
; /* start tracking a run */
475 } else if (combcharcnt
) {
476 if (combcharcnt
> 1) {
477 priortysort(ucsp
- combcharcnt
, combcharcnt
);
479 combcharcnt
= 0; /* start over */
481 *ucsp
++ = swapbytes
? NXSwapShort(ucs_ch
) : ucs_ch
;
484 * Make a previous combining sequence canonical
486 if (combcharcnt
> 1) {
487 priortysort(ucsp
- combcharcnt
, combcharcnt
);
491 *ucslen
= (u_int8_t
*)ucsp
- (u_int8_t
*)bufstart
;
500 result
= ENAMETOOLONG
;
506 * utf8_validatestr - Check for a valid UTF-8 string.
509 utf8_validatestr(const u_int8_t
* utf8p
, size_t utf8len
)
516 while (utf8len
-- > 0 && (byte
= *utf8p
++) != '\0') {
518 continue; /* plain ascii */
520 extrabytes
= utf_extrabytes
[byte
>> 3];
522 if (utf8len
< extrabytes
)
524 utf8len
-= extrabytes
;
526 switch (extrabytes
) {
528 ch
= byte
; ch
<<= 6; /* 1st byte */
529 byte
= *utf8p
++; /* 2nd byte */
530 if ((byte
>> 6) != 2)
538 ch
= byte
; ch
<<= 6; /* 1st byte */
539 byte
= *utf8p
++; /* 2nd byte */
540 if ((byte
>> 6) != 2)
542 ch
+= byte
; ch
<<= 6;
543 byte
= *utf8p
++; /* 3rd byte */
544 if ((byte
>> 6) != 2)
553 if (ch
== 0xFFFE || ch
== 0xFFFF)
558 ch
= byte
; ch
<<= 6; /* 1st byte */
559 byte
= *utf8p
++; /* 2nd byte */
560 if ((byte
>> 6) != 2)
562 ch
+= byte
; ch
<<= 6;
563 byte
= *utf8p
++; /* 3rd byte */
564 if ((byte
>> 6) != 2)
566 ch
+= byte
; ch
<<= 6;
567 byte
= *utf8p
++; /* 4th byte */
568 if ((byte
>> 6) != 2)
571 ch
-= 0x03C82080UL
+ SP_HALF_BASE
;
572 ucs_ch
= (ch
>> SP_HALF_SHIFT
) + SP_HIGH_FIRST
;
573 if (ucs_ch
< SP_HIGH_FIRST
|| ucs_ch
> SP_HIGH_LAST
)
575 ucs_ch
= (ch
& SP_HALF_MASK
) + SP_LOW_FIRST
;
576 if (ucs_ch
< SP_LOW_FIRST
|| ucs_ch
> SP_LOW_LAST
)
591 * Unicode 3.2 decomposition code (derived from Core Foundation)
597 } unicode_mappings32
;
599 static inline u_int32_t
600 getmappedvalue32(const unicode_mappings32
*theTable
, u_int32_t numElem
,
603 const unicode_mappings32
*p
, *q
, *divider
;
605 if ((character
< theTable
[0]._key
) || (character
> theTable
[numElem
-1]._key
))
611 divider
= p
+ ((q
- p
) >> 1); /* divide by 2 */
612 if (character
< divider
->_key
) { q
= divider
- 1; }
613 else if (character
> divider
->_key
) { p
= divider
+ 1; }
614 else { return (divider
->_value
); }
619 #define RECURSIVE_DECOMPOSITION (1 << 15)
620 #define EXTRACT_COUNT(value) (((value) >> 12) & 0x0007)
625 } unicode_mappings16
;
627 static inline u_int16_t
628 getmappedvalue16(const unicode_mappings16
*theTable
, u_int32_t numElem
,
631 const unicode_mappings16
*p
, *q
, *divider
;
633 if ((character
< theTable
[0]._key
) || (character
> theTable
[numElem
-1]._key
))
639 divider
= p
+ ((q
- p
) >> 1); /* divide by 2 */
640 if (character
< divider
->_key
)
642 else if (character
> divider
->_key
)
645 return (divider
->_value
);
652 unicode_recursive_decompose(u_int16_t character
, u_int16_t
*convertedChars
)
658 const u_int16_t
*bmpMappings
;
659 u_int32_t usedLength
;
661 value
= getmappedvalue16(
662 (const unicode_mappings16
*)__CFUniCharDecompositionTable
,
663 __UniCharDecompositionTableLength
, character
);
664 length
= EXTRACT_COUNT(value
);
665 firstChar
= value
& 0x0FFF;
667 bmpMappings
= (length
== 1 ? &theChar
: __CFUniCharMultipleDecompositionTable
+ firstChar
);
670 if (value
& RECURSIVE_DECOMPOSITION
) {
671 usedLength
= unicode_recursive_decompose((u_int16_t
)*bmpMappings
, convertedChars
);
673 --length
; /* Decrement for the first char */
677 convertedChars
+= usedLength
;
680 usedLength
+= length
;
683 *(convertedChars
++) = *(bmpMappings
++);
688 #define HANGUL_SBASE 0xAC00
689 #define HANGUL_LBASE 0x1100
690 #define HANGUL_VBASE 0x1161
691 #define HANGUL_TBASE 0x11A7
693 #define HANGUL_SCOUNT 11172
694 #define HANGUL_LCOUNT 19
695 #define HANGUL_VCOUNT 21
696 #define HANGUL_TCOUNT 28
697 #define HANGUL_NCOUNT (HANGUL_VCOUNT * HANGUL_TCOUNT)
700 * unicode_decompose - decompose a composed Unicode char
702 * Composed Unicode characters are forbidden on
703 * HFS Plus volumes. ucs_decompose will convert a
704 * composed character into its correct decomposed
707 * Similar to CFUniCharDecomposeCharacter
710 unicode_decompose(u_int16_t character
, u_int16_t
*convertedChars
)
712 if ((character
>= HANGUL_SBASE
) &&
713 (character
<= (HANGUL_SBASE
+ HANGUL_SCOUNT
))) {
716 character
-= HANGUL_SBASE
;
717 length
= (character
% HANGUL_TCOUNT
? 3 : 2);
719 *(convertedChars
++) =
720 character
/ HANGUL_NCOUNT
+ HANGUL_LBASE
;
721 *(convertedChars
++) =
722 (character
% HANGUL_NCOUNT
) / HANGUL_TCOUNT
+ HANGUL_VBASE
;
724 *convertedChars
= (character
% HANGUL_TCOUNT
) + HANGUL_TBASE
;
727 return (unicode_recursive_decompose(character
, convertedChars
));
732 * unicode_combine - generate a precomposed Unicode char
734 * Precomposed Unicode characters are required for some volume
735 * formats and network protocols. unicode_combine will combine
736 * a decomposed character sequence into a single precomposed
737 * (composite) character.
739 * Similar toCFUniCharPrecomposeCharacter but unicode_combine
740 * also handles Hangul Jamo characters.
743 unicode_combine(u_int16_t base
, u_int16_t combining
)
748 if ((combining
>= HANGUL_VBASE
) && (combining
< (HANGUL_TBASE
+ HANGUL_TCOUNT
))) {
749 /* 2 char Hangul sequences */
750 if ((combining
< (HANGUL_VBASE
+ HANGUL_VCOUNT
)) &&
751 (base
>= HANGUL_LBASE
&& base
< (HANGUL_LBASE
+ HANGUL_LCOUNT
))) {
752 return (HANGUL_SBASE
+
753 ((base
- HANGUL_LBASE
)*(HANGUL_VCOUNT
*HANGUL_TCOUNT
)) +
754 ((combining
- HANGUL_VBASE
)*HANGUL_TCOUNT
));
757 /* 3 char Hangul sequences */
758 if ((combining
> HANGUL_TBASE
) &&
759 (base
>= HANGUL_SBASE
&& base
< (HANGUL_SBASE
+ HANGUL_SCOUNT
))) {
760 if ((base
- HANGUL_SBASE
) % HANGUL_TCOUNT
)
763 return (base
+ (combining
- HANGUL_TBASE
));
767 value
= getmappedvalue32(
768 (const unicode_mappings32
*)__CFUniCharPrecompSourceTable
,
769 __CFUniCharPrecompositionTableLength
, combining
);
772 value
= getmappedvalue16(
773 (const unicode_mappings16
*)
774 ((u_int32_t
*)__CFUniCharBMPPrecompDestinationTable
+ (value
& 0xFFFF)),
775 (value
>> 16), base
);
782 * priortysort - order combining chars into canonical order
784 * Similar to CFUniCharPrioritySort
787 priortysort(u_int16_t
* characters
, int count
)
790 u_int16_t
*ch1
, *ch2
;
794 end
= characters
+ count
;
798 ch2
= characters
+ 1;
799 p2
= get_combining_class(*ch1
);
802 p2
= get_combining_class(*ch2
);