2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 File: UnicodeWrappers.c
31 Contains: Wrapper routines for Unicode conversion and comparison.
36 #include <sys/param.h>
37 #include <sys/utfconv.h>
39 #include "../../hfs_macos_defs.h"
40 #include "UCStringCompareData.h"
42 #include "../headers/FileMgrInternal.h"
43 #include "../headers/HFSUnicodeWrappers.h"
46 kMinFileExtensionChars
= 1, /* does not include dot */
47 kMaxFileExtensionChars
= 5 /* does not include dot */
51 #define EXTENSIONCHAR(c) (((c) >= 0x61 && (c) <= 0x7A) || \
52 ((c) >= 0x41 && (c) <= 0x5A) || \
53 ((c) >= 0x30 && (c) <= 0x39))
56 #define IsHexDigit(c) (((c) >= (u_int8_t) '0' && (c) <= (u_int8_t) '9') || \
57 ((c) >= (u_int8_t) 'A' && (c) <= (u_int8_t) 'F'))
60 static void GetFilenameExtension( ItemCount length
, ConstUniCharArrayPtr unicodeStr
, char* extStr
);
63 static u_int32_t
HexStringToInteger( u_int32_t length
, const u_int8_t
*hexStr
);
67 * Get filename extension (if any) as a C string
70 GetFilenameExtension(ItemCount length
, ConstUniCharArrayPtr unicodeStr
, char * extStr
)
74 u_int16_t extChars
; /* number of extension chars (excluding dot) */
75 u_int16_t maxExtChars
;
76 Boolean foundExtension
;
78 extStr
[0] = '\0'; /* assume there's no extension */
81 return; /* "x.y" is smallest possible extension */
83 if ( length
< (kMaxFileExtensionChars
+ 2) )
84 maxExtChars
= length
- 2; /* save room for prefix + dot */
86 maxExtChars
= kMaxFileExtensionChars
;
90 foundExtension
= false;
92 while ( extChars
<= maxExtChars
) {
95 /* look for leading dot */
96 if ( c
== (UniChar
) '.' ) {
97 if ( extChars
> 0 ) /* cannot end with a dot */
98 foundExtension
= true;
102 if ( EXTENSIONCHAR(c
) )
108 /* if we found one then copy it */
109 if ( foundExtension
) {
110 u_int8_t
*extStrPtr
= (u_int8_t
*)extStr
;
111 const UniChar
*unicodeStrPtr
= &unicodeStr
[i
];
113 for ( i
= 0; i
<= extChars
; ++i
)
114 *(extStrPtr
++) = (u_int8_t
) *(unicodeStrPtr
++);
115 extStr
[extChars
+ 1] = '\0'; /* terminate extension + dot */
122 * Count filename extension characters (if any)
125 CountFilenameExtensionChars( const unsigned char * filename
, u_int32_t length
)
129 u_int32_t extChars
; /* number of extension chars (excluding dot) */
130 u_int16_t maxExtChars
;
131 Boolean foundExtension
;
134 return 0; /* "x.y" is smallest possible extension */
136 if ( length
< (kMaxFileExtensionChars
+ 2) )
137 maxExtChars
= length
- 2; /* save room for prefix + dot */
139 maxExtChars
= kMaxFileExtensionChars
;
141 extChars
= 0; /* assume there's no extension */
142 i
= length
- 1; /* index to last ascii character */
143 foundExtension
= false;
145 while ( extChars
<= maxExtChars
) {
148 /* look for leading dot */
149 if ( c
== (u_int8_t
) '.' ) {
150 if ( extChars
> 0 ) /* cannot end with a dot */
156 if ( EXTENSIONCHAR(c
) )
167 * extract the file id from a mangled name
170 GetEmbeddedFileID(const unsigned char * filename
, u_int32_t length
, u_int32_t
*prefixLength
)
178 if ( filename
== NULL
)
182 return 0; /* too small to have been mangled */
184 /* big enough for a file ID (#10) and an extension (.x) ? */
186 extChars
= CountFilenameExtensionChars(filename
, length
);
190 /* skip over dot plus extension characters */
192 length
-= (extChars
+ 1);
194 /* scan for file id digits */
195 for ( i
= length
- 1; i
>= 0; --i
) {
198 /* look for file ID marker */
200 if ( (length
- i
) < 3 )
201 break; /* too small to be a file ID */
204 return HexStringToInteger(length
- i
- 1, &filename
[i
+1]);
207 if ( !IsHexDigit(c
) )
208 break; /* file ID string must have hex digits */
217 HexStringToInteger(u_int32_t length
, const u_int8_t
*hexStr
)
227 for ( i
= 0; i
< length
; ++i
) {
230 if (c
>= '0' && c
<= '9') {
232 value
+= (u_int32_t
) c
- (u_int32_t
) '0';
233 } else if (c
>= 'A' && c
<= 'F') {
235 value
+= 10 + ((unsigned int) c
- (unsigned int) 'A');
237 return 0; /* bad character */
246 * Routine: FastRelString
248 * Output: returns -1 if str1 < str2
249 * returns 1 if str1 > str2
253 int32_t FastRelString( ConstStr255Param str1
, ConstStr255Param str2
)
255 u_int16_t
* compareTable
;
257 u_int8_t length
, length2
;
264 if (length
== length2
)
266 else if (length
< length2
)
269 delta
= length2
- length
;
277 compareTable
= (u_int16_t
*) gCompareTable
;
281 u_int8_t aChar
, bChar
;
286 if (aChar
!= bChar
) // If they don't match exacly, do case conversion
288 u_int16_t aSortWord
, bSortWord
;
290 aSortWord
= compareTable
[aChar
];
291 bSortWord
= compareTable
[bChar
];
293 if (aSortWord
> bSortWord
)
296 if (aSortWord
< bSortWord
)
300 // If characters match exactly, then go on to next character immediately without
301 // doing any extra work.
304 // if you got to here, then return bestGuess
311 // FastUnicodeCompare - Compare two Unicode strings; produce a relative ordering
314 // --------------------------
319 // The lower case table starts with 256 entries (one for each of the upper bytes
320 // of the original Unicode char). If that entry is zero, then all characters with
321 // that upper byte are already case folded. If the entry is non-zero, then it is
322 // the _index_ (not byte offset) of the start of the sub-table for the characters
323 // with that upper byte. All ignorable characters are folded to the value zero.
327 // Let c = source Unicode character
328 // Let table[] = lower case table
330 // lower = table[highbyte(c)]
334 // lower = table[lower+lowbyte(c)]
337 // ignore this character
339 // To handle ignorable characters, we now need a loop to find the next valid character.
340 // Also, we can't pre-compute the number of characters to compare; the string length might
341 // be larger than the number of non-ignorable characters. Further, we must be able to handle
342 // ignorable characters at any point in the string, including as the first or last characters.
343 // We use a zero value as a sentinel to detect both end-of-string and ignorable characters.
344 // Since the File Manager doesn't prevent the NUL character (value zero) as part of a filename,
345 // the case mapping table is assumed to map u+0000 to some non-zero value (like 0xFFFF, which is
346 // an invalid Unicode character).
351 // c1 = GetNextValidChar(str1) // returns zero if at end of string
352 // c2 = GetNextValidChar(str2)
354 // if (c1 != c2) break // found a difference
356 // if (c1 == 0) // reached end of string on both strings at once?
357 // return 0; // yes, so strings are equal
360 // // When we get here, c1 != c2. So, we just need to determine which one is less.
367 int32_t FastUnicodeCompare ( register ConstUniCharArrayPtr str1
, register ItemCount length1
,
368 register ConstUniCharArrayPtr str2
, register ItemCount length2
)
370 register u_int16_t c1
,c2
;
371 register u_int16_t temp
;
372 register u_int16_t
* lowerCaseTable
;
374 lowerCaseTable
= (u_int16_t
*) gLowerCaseTable
;
377 /* Set default values for c1, c2 in case there are no more valid chars */
381 /* Find next non-ignorable char from str1, or zero if no more */
382 while (length1
&& c1
== 0) {
385 /* check for basic latin first */
387 c1
= gLatinCaseFold
[c1
];
390 /* case fold if neccessary */
391 if ((temp
= lowerCaseTable
[c1
>>8]) != 0)
392 c1
= lowerCaseTable
[temp
+ (c1
& 0x00FF)];
396 /* Find next non-ignorable char from str2, or zero if no more */
397 while (length2
&& c2
== 0) {
400 /* check for basic latin first */
402 c2
= gLatinCaseFold
[c2
];
405 /* case fold if neccessary */
406 if ((temp
= lowerCaseTable
[c2
>>8]) != 0)
407 c2
= lowerCaseTable
[temp
+ (c2
& 0x00FF)];
410 if (c1
!= c2
) // found a difference, so stop looping
413 if (c1
== 0) // did we reach the end of both strings at the same time?
414 return 0; // yes, so strings are equal
425 ConvertUnicodeToUTF8Mangled(ByteCount srcLen
, ConstUniCharArrayPtr srcStr
, ByteCount maxDstLen
,
426 ByteCount
*actualDstLen
, unsigned char* dstStr
, HFSCatalogNodeID cnid
)
433 snprintf(fileIDStr
, sizeof(fileIDStr
), "#%X", cnid
);
434 GetFilenameExtension(srcLen
/sizeof(UniChar
), srcStr
, extStr
);
436 /* remove extension chars from source */
437 srcLen
-= strlen(extStr
) * sizeof(UniChar
);
438 subMaxLen
= maxDstLen
- (strlen(extStr
) + strlen(fileIDStr
));
440 (void) utf8_encodestr(srcStr
, srcLen
, dstStr
, &utf8len
, subMaxLen
, ':', 0);
442 strlcat((char *)dstStr
, fileIDStr
, maxDstLen
);
443 strlcat((char *)dstStr
, extStr
, maxDstLen
);
444 *actualDstLen
= utf8len
+ (strlen(extStr
) + strlen(fileIDStr
));
449 #else /* not HFS - temp workaround until 4277828 is fixed */
450 /* stubs for exported routines that aren't present when we build kernel without HFS */
452 #include <sys/types.h>
454 int32_t FastUnicodeCompare( void * str1
, u_int32_t length1
, void * str2
, u_int32_t length2
);
457 int32_t FastUnicodeCompare( __unused
void * str1
,
458 __unused u_int32_t length1
,
459 __unused
void * str2
,
460 __unused u_int32_t length2
)