2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 Includes Unicode 3.2 decomposition code derived from Core Foundation
27 #include <sys/param.h>
28 #include <sys/utfconv.h>
29 #include <sys/errno.h>
30 #include <architecture/byte_order.h>
33 * UTF-8 (Unicode Transformation Format)
35 * UTF-8 is the Unicode Transformation Format that serializes a Unicode
36 * character as a sequence of one to four bytes. Only the shortest form
37 * required to represent the significant Unicode bits is legal.
39 * UTF-8 Multibyte Codes
41 * Bytes Bits Unicode Min Unicode Max UTF-8 Byte Sequence (binary)
42 * -----------------------------------------------------------------------------
43 * 1 7 0x0000 0x007F 0xxxxxxx
44 * 2 11 0x0080 0x07FF 110xxxxx 10xxxxxx
45 * 3 16 0x0800 0xFFFF 1110xxxx 10xxxxxx 10xxxxxx
46 * 4 21 0x10000 0x10FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
47 * -----------------------------------------------------------------------------
51 #define UNICODE_TO_UTF8_LEN(c) \
52 ((c) < 0x0080 ? 1 : ((c) < 0x0800 ? 2 : (((c) & 0xf800) == 0xd800 ? 2 : 3)))
54 #define UCS_ALT_NULL 0x2400
56 /* Surrogate Pair Constants */
57 #define SP_HALF_SHIFT 10
58 #define SP_HALF_BASE 0x0010000UL
59 #define SP_HALF_MASK 0x3FFUL
61 #define SP_HIGH_FIRST 0xD800UL
62 #define SP_HIGH_LAST 0xDBFFUL
63 #define SP_LOW_FIRST 0xDC00UL
64 #define SP_LOW_LAST 0xDFFFUL
67 #include "vfs_utfconvdata.h"
71 * Test for a combining character.
73 * Similar to __CFUniCharIsNonBaseCharacter except that
74 * unicode_combinable also includes Hangul Jamo characters.
77 unicode_combinable(u_int16_t character
)
79 const u_int8_t
*bitmap
= __CFUniCharCombiningBitmap
;
82 if (character
< 0x0300)
85 value
= bitmap
[(character
>> 8) & 0xFF];
90 bitmap
= bitmap
+ ((value
- 1) * 32) + 256;
91 return (bitmap
[(character
& 0xFF) / 8] & (1 << (character
% 8)) ? 1 : 0);
97 * Test for a precomposed character.
99 * Similar to __CFUniCharIsDecomposableCharacter.
102 unicode_decomposeable(u_int16_t character
) {
103 const u_int8_t
*bitmap
= __CFUniCharDecomposableBitmap
;
106 if (character
< 0x00C0)
109 value
= bitmap
[(character
>> 8) & 0xFF];
114 bitmap
= bitmap
+ ((value
- 1) * 32) + 256;
115 return (bitmap
[(character
& 0xFF) / 8] & (1 << (character
% 8)) ? 1 : 0);
122 * Get the combing class.
124 * Similar to CFUniCharGetCombiningPropertyForCharacter.
126 static inline u_int8_t
127 get_combining_class(u_int16_t character
) {
128 const u_int8_t
*bitmap
= __CFUniCharCombiningPropertyBitmap
;
130 u_int8_t value
= bitmap
[(character
>> 8)];
133 bitmap
= bitmap
+ (value
* 256);
134 return bitmap
[character
% 256];
140 static int unicode_decompose(u_int16_t character
, u_int16_t
*convertedChars
);
142 static u_int16_t
unicode_combine(u_int16_t base
, u_int16_t combining
);
144 static void priortysort(u_int16_t
* characters
, int count
);
146 char utf_extrabytes
[32] = {
147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
148 -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 2, 2, 3, -1
153 * utf8_encodelen - Calculates the UTF-8 encoding length for a Unicode filename
156 * If '/' chars are allowed on disk then an alternate
157 * (replacement) char must be provided in altslash.
160 * UTF_REVERSE_ENDIAN: Unicode byteorder is opposite current runtime
163 utf8_encodelen(const u_int16_t
* ucsp
, size_t ucslen
, u_int16_t altslash
,
168 int swapbytes
= (flags
& UTF_REVERSE_ENDIAN
);
171 charcnt
= ucslen
/ 2;
174 while (charcnt
-- > 0) {
178 ucs_ch
= NXSwapShort(ucs_ch
);
180 ucs_ch
= altslash
? altslash
: '_';
181 else if (ucs_ch
== '\0')
182 ucs_ch
= UCS_ALT_NULL
;
184 len
+= UNICODE_TO_UTF8_LEN(ucs_ch
);
192 * utf8_encodestr - Encodes a Unicode string to UTF-8
195 * The resulting UTF-8 string is NULL terminated.
197 * If '/' chars are allowed on disk then an alternate
198 * (replacement) char must be provided in altslash.
201 * UTF_REVERSE_ENDIAN: Unicode byteorder is opposite current runtime
202 * UTF_NO_NULL_TERM: don't add NULL termination to UTF-8 output
205 * ENAMETOOLONG: Name didn't fit; only buflen bytes were encoded
206 * EINVAL: Illegal char found; char was replaced by an '_'.
209 utf8_encodestr(const u_int16_t
* ucsp
, size_t ucslen
, u_int8_t
* utf8p
,
210 size_t * utf8len
, size_t buflen
, u_int16_t altslash
, int flags
)
215 u_int16_t
* chp
= NULL
;
216 u_int16_t sequence
[8];
219 int swapbytes
= (flags
& UTF_REVERSE_ENDIAN
);
220 int nullterm
= ((flags
& UTF_NO_NULL_TERM
) == 0);
221 int decompose
= (flags
& UTF_DECOMPOSED
);
225 bufend
= bufstart
+ buflen
;
228 charcnt
= ucslen
/ 2;
230 while (charcnt
-- > 0) {
235 ucs_ch
= swapbytes
? NXSwapShort(*ucsp
++) : *ucsp
++;
237 if (decompose
&& unicode_decomposeable(ucs_ch
)) {
238 extra
= unicode_decompose(ucs_ch
, sequence
) - 1;
240 ucs_ch
= sequence
[0];
245 /* Slash and NULL are not permitted */
253 } else if (ucs_ch
== '\0') {
254 ucs_ch
= UCS_ALT_NULL
;
257 if (ucs_ch
< 0x0080) {
258 if (utf8p
>= bufend
) {
259 result
= ENAMETOOLONG
;
264 } else if (ucs_ch
< 0x800) {
265 if ((utf8p
+ 1) >= bufend
) {
266 result
= ENAMETOOLONG
;
269 *utf8p
++ = 0xc0 | (ucs_ch
>> 6);
270 *utf8p
++ = 0x80 | (0x3f & ucs_ch
);
273 /* Combine valid surrogate pairs */
274 if (ucs_ch
>= SP_HIGH_FIRST
&& ucs_ch
<= SP_HIGH_LAST
279 ch2
= swapbytes
? NXSwapShort(*ucsp
) : *ucsp
;
280 if (ch2
>= SP_LOW_FIRST
&& ch2
<= SP_LOW_LAST
) {
281 pair
= ((ucs_ch
- SP_HIGH_FIRST
) << SP_HALF_SHIFT
)
282 + (ch2
- SP_LOW_FIRST
) + SP_HALF_BASE
;
283 if ((utf8p
+ 3) >= bufend
) {
284 result
= ENAMETOOLONG
;
289 *utf8p
++ = 0xf0 | (pair
>> 18);
290 *utf8p
++ = 0x80 | (0x3f & (pair
>> 12));
291 *utf8p
++ = 0x80 | (0x3f & (pair
>> 6));
292 *utf8p
++ = 0x80 | (0x3f & pair
);
296 if ((utf8p
+ 2) >= bufend
) {
297 result
= ENAMETOOLONG
;
300 *utf8p
++ = 0xe0 | (ucs_ch
>> 12);
301 *utf8p
++ = 0x80 | (0x3f & (ucs_ch
>> 6));
302 *utf8p
++ = 0x80 | (0x3f & ucs_ch
);
306 *utf8len
= utf8p
- bufstart
;
315 * utf8_decodestr - Decodes a UTF-8 string back to Unicode
318 * The input UTF-8 string does not need to be null terminated
321 * If '/' chars are allowed on disk then an alternate
322 * (replacement) char must be provided in altslash.
325 * UTF_REV_ENDIAN: Unicode byteorder is oposite current runtime
326 * UTF_DECOMPOSED: Unicode output string must be fully decompsed
329 * ENAMETOOLONG: Name didn't fit; only ucslen chars were decoded.
330 * EINVAL: Illegal UTF-8 sequence found.
333 utf8_decodestr(const u_int8_t
* utf8p
, size_t utf8len
, u_int16_t
* ucsp
,
334 size_t *ucslen
, size_t buflen
, u_int16_t altslash
, int flags
)
342 int decompose
, precompose
, swapbytes
;
344 decompose
= (flags
& UTF_DECOMPOSED
);
345 precompose
= (flags
& UTF_PRECOMPOSED
);
346 swapbytes
= (flags
& UTF_REVERSE_ENDIAN
);
349 bufend
= (u_int16_t
*)((u_int8_t
*)ucsp
+ buflen
);
351 while (utf8len
-- > 0 && (byte
= *utf8p
++) != '\0') {
355 /* check for ascii */
357 ucs_ch
= byte
; /* 1st byte */
360 int extrabytes
= utf_extrabytes
[byte
>> 3];
362 if (utf8len
< extrabytes
)
364 utf8len
-= extrabytes
;
366 switch (extrabytes
) {
368 ch
= byte
; ch
<<= 6; /* 1st byte */
369 byte
= *utf8p
++; /* 2nd byte */
370 if ((byte
>> 6) != 2)
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)
394 if (ch
== 0xFFFE || ch
== 0xFFFF)
400 ch
= byte
; ch
<<= 6; /* 1st byte */
401 byte
= *utf8p
++; /* 2nd byte */
402 if ((byte
>> 6) != 2)
404 ch
+= byte
; ch
<<= 6;
405 byte
= *utf8p
++; /* 3rd byte */
406 if ((byte
>> 6) != 2)
408 ch
+= byte
; ch
<<= 6;
409 byte
= *utf8p
++; /* 4th byte */
410 if ((byte
>> 6) != 2)
413 ch
-= 0x03C82080UL
+ SP_HALF_BASE
;
414 ucs_ch
= (ch
>> SP_HALF_SHIFT
) + SP_HIGH_FIRST
;
415 if (ucs_ch
< SP_HIGH_FIRST
|| ucs_ch
> SP_HIGH_LAST
)
417 *ucsp
++ = swapbytes
? NXSwapShort(ucs_ch
) : ucs_ch
;
420 ucs_ch
= (ch
& SP_HALF_MASK
) + SP_LOW_FIRST
;
421 if (ucs_ch
< SP_LOW_FIRST
|| ucs_ch
> SP_LOW_LAST
)
423 *ucsp
++ = swapbytes
? NXSwapShort(ucs_ch
) : ucs_ch
;
429 if (unicode_decomposeable(ucs_ch
)) {
430 u_int16_t sequence
[8];
433 /* Before decomposing a new unicode character, sort
434 * previous combining characters, if any, and reset
437 if (combcharcnt
> 1){
438 priortysort(ucsp
- combcharcnt
, combcharcnt
);
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
);