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@
31 File: UnicodeWrappers.c
33 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) >= (UInt8) '0' && (c) <= (UInt8) '9') || \
57 ((c) >= (UInt8) 'A' && (c) <= (UInt8) 'F'))
60 static void GetFilenameExtension( ItemCount length
, ConstUniCharArrayPtr unicodeStr
, char* extStr
);
63 static UInt32
HexStringToInteger( UInt32 length
, const UInt8
*hexStr
);
67 * Get filename extension (if any) as a C string
70 GetFilenameExtension(ItemCount length
, ConstUniCharArrayPtr unicodeStr
, char * extStr
)
74 UInt16 extChars
; /* number of extension chars (excluding dot) */
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 UInt8
*extStrPtr
= extStr
;
111 const UniChar
*unicodeStrPtr
= &unicodeStr
[i
];
113 for ( i
= 0; i
<= extChars
; ++i
)
114 *(extStrPtr
++) = (UInt8
) *(unicodeStrPtr
++);
115 extStr
[extChars
+ 1] = '\0'; /* terminate extension + dot */
122 * Count filename extension characters (if any)
125 CountFilenameExtensionChars( const unsigned char * filename
, UInt32 length
)
129 UInt32 extChars
; /* number of extension chars (excluding dot) */
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
== (UInt8
) '.' ) {
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
, UInt32 length
, UInt32
*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(UInt32 length
, const UInt8
*hexStr
)
227 for ( i
= 0; i
< length
; ++i
) {
230 if (c
>= '0' && c
<= '9') {
232 value
+= (UInt32
) c
- (UInt32
) '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 SInt32
FastRelString( ConstStr255Param str1
, ConstStr255Param str2
)
255 UInt16
* compareTable
;
257 UInt8 length
, length2
;
264 if (length
== length2
)
266 else if (length
< length2
)
269 delta
= length2
- length
;
277 compareTable
= (UInt16
*) gCompareTable
;
286 if (aChar
!= bChar
) // If they don't match exacly, do case conversion
288 UInt16 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 SInt32
FastUnicodeCompare ( register ConstUniCharArrayPtr str1
, register ItemCount length1
,
368 register ConstUniCharArrayPtr str2
, register ItemCount length2
)
370 register UInt16 c1
,c2
;
371 register UInt16 temp
;
372 register UInt16
* lowerCaseTable
;
374 lowerCaseTable
= (UInt16
*) 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 sprintf(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 strcat(dstStr
, fileIDStr
);
443 strcat(dstStr
, extStr
);
444 *actualDstLen
= utf8len
+ (strlen(extStr
) + strlen(fileIDStr
));