2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 File: UnicodeWrappers.c
28 Contains: Wrapper routines for Unicode conversion and comparison.
31 #include <sys/param.h>
32 #include <sys/utfconv.h>
34 #include "../../hfs_macos_defs.h"
35 #include "UCStringCompareData.h"
37 #include "../headers/FileMgrInternal.h"
38 #include "../headers/HFSUnicodeWrappers.h"
41 kMinFileExtensionChars
= 1, /* does not include dot */
42 kMaxFileExtensionChars
= 5 /* does not include dot */
46 #define EXTENSIONCHAR(c) (((c) >= 0x61 && (c) <= 0x7A) || \
47 ((c) >= 0x41 && (c) <= 0x5A) || \
48 ((c) >= 0x30 && (c) <= 0x39))
51 #define IsHexDigit(c) (((c) >= (UInt8) '0' && (c) <= (UInt8) '9') || \
52 ((c) >= (UInt8) 'A' && (c) <= (UInt8) 'F'))
55 static void GetFilenameExtension( ItemCount length
, ConstUniCharArrayPtr unicodeStr
, char* extStr
);
57 static void GetFileIDString( HFSCatalogNodeID fileID
, char* fileIDStr
);
59 static UInt32
HexStringToInteger( UInt32 length
, const UInt8
*hexStr
);
64 * Convert file ID into a hexidecimal string with no leading zeros
67 GetFileIDString( HFSCatalogNodeID fileID
, char * fileIDStr
)
70 UInt8
*translate
= (UInt8
*) "0123456789ABCDEF";
75 for ( i
= 0, b
= 28; b
>= 0; b
-= 4 ) {
76 c
= *(translate
+ ((fileID
>> b
) & 0x0000000F));
78 /* if its not a leading zero add it to our string */
79 if ( (c
!= (UInt8
) '0') || (i
> 1) || (b
== 0) )
83 fileIDStr
[++i
] = '\0';
88 * Get filename extension (if any) as a C string
91 GetFilenameExtension(ItemCount length
, ConstUniCharArrayPtr unicodeStr
, char * extStr
)
95 UInt16 extChars
; /* number of extension chars (excluding dot) */
97 Boolean foundExtension
;
99 extStr
[0] = '\0'; /* assume there's no extension */
102 return; /* "x.y" is smallest possible extension */
104 if ( length
< (kMaxFileExtensionChars
+ 2) )
105 maxExtChars
= length
- 2; /* save room for prefix + dot */
107 maxExtChars
= kMaxFileExtensionChars
;
111 foundExtension
= false;
113 while ( extChars
<= maxExtChars
) {
116 /* look for leading dot */
117 if ( c
== (UniChar
) '.' ) {
118 if ( extChars
> 0 ) /* cannot end with a dot */
119 foundExtension
= true;
123 if ( EXTENSIONCHAR(c
) )
129 /* if we found one then copy it */
130 if ( foundExtension
) {
131 UInt8
*extStrPtr
= extStr
;
132 const UniChar
*unicodeStrPtr
= &unicodeStr
[i
];
134 for ( i
= 0; i
<= extChars
; ++i
)
135 *(extStrPtr
++) = (UInt8
) *(unicodeStrPtr
++);
136 extStr
[extChars
+ 1] = '\0'; /* terminate extension + dot */
143 * Count filename extension characters (if any)
146 CountFilenameExtensionChars( const unsigned char * filename
, UInt32 length
)
150 UInt32 extChars
; /* number of extension chars (excluding dot) */
152 Boolean foundExtension
;
155 return 0; /* "x.y" is smallest possible extension */
157 if ( length
< (kMaxFileExtensionChars
+ 2) )
158 maxExtChars
= length
- 2; /* save room for prefix + dot */
160 maxExtChars
= kMaxFileExtensionChars
;
162 extChars
= 0; /* assume there's no extension */
163 i
= length
- 1; /* index to last ascii character */
164 foundExtension
= false;
166 while ( extChars
<= maxExtChars
) {
169 /* look for leading dot */
170 if ( c
== (UInt8
) '.' ) {
171 if ( extChars
> 0 ) /* cannot end with a dot */
177 if ( EXTENSIONCHAR(c
) )
188 * extract the file id from a mangled name
191 GetEmbeddedFileID(const unsigned char * filename
, UInt32 length
, UInt32
*prefixLength
)
199 if ( filename
== NULL
)
203 return 0; /* too small to have been mangled */
205 /* big enough for a file ID (#10) and an extension (.x) ? */
207 extChars
= CountFilenameExtensionChars(filename
, length
);
211 /* skip over dot plus extension characters */
213 length
-= (extChars
+ 1);
215 /* scan for file id digits */
216 for ( i
= length
- 1; i
>= 0; --i
) {
219 /* look for file ID marker */
221 if ( (length
- i
) < 3 )
222 break; /* too small to be a file ID */
225 return HexStringToInteger(length
- i
- 1, &filename
[i
+1]);
228 if ( !IsHexDigit(c
) )
229 break; /* file ID string must have hex digits */
238 HexStringToInteger(UInt32 length
, const UInt8
*hexStr
)
248 for ( i
= 0; i
< length
; ++i
) {
251 if (c
>= '0' && c
<= '9') {
253 value
+= (UInt32
) c
- (UInt32
) '0';
254 } else if (c
>= 'A' && c
<= 'F') {
256 value
+= 10 + ((unsigned int) c
- (unsigned int) 'A');
258 return 0; /* bad character */
267 * Routine: FastRelString
269 * Output: returns -1 if str1 < str2
270 * returns 1 if str1 > str2
274 SInt32
FastRelString( ConstStr255Param str1
, ConstStr255Param str2
)
276 UInt16
* compareTable
;
278 UInt8 length
, length2
;
285 if (length
== length2
)
287 else if (length
< length2
)
290 delta
= length2
- length
;
298 compareTable
= (UInt16
*) gCompareTable
;
307 if (aChar
!= bChar
) // If they don't match exacly, do case conversion
309 UInt16 aSortWord
, bSortWord
;
311 aSortWord
= compareTable
[aChar
];
312 bSortWord
= compareTable
[bChar
];
314 if (aSortWord
> bSortWord
)
317 if (aSortWord
< bSortWord
)
321 // If characters match exactly, then go on to next character immediately without
322 // doing any extra work.
325 // if you got to here, then return bestGuess
332 // FastUnicodeCompare - Compare two Unicode strings; produce a relative ordering
335 // --------------------------
340 // The lower case table starts with 256 entries (one for each of the upper bytes
341 // of the original Unicode char). If that entry is zero, then all characters with
342 // that upper byte are already case folded. If the entry is non-zero, then it is
343 // the _index_ (not byte offset) of the start of the sub-table for the characters
344 // with that upper byte. All ignorable characters are folded to the value zero.
348 // Let c = source Unicode character
349 // Let table[] = lower case table
351 // lower = table[highbyte(c)]
355 // lower = table[lower+lowbyte(c)]
358 // ignore this character
360 // To handle ignorable characters, we now need a loop to find the next valid character.
361 // Also, we can't pre-compute the number of characters to compare; the string length might
362 // be larger than the number of non-ignorable characters. Further, we must be able to handle
363 // ignorable characters at any point in the string, including as the first or last characters.
364 // We use a zero value as a sentinel to detect both end-of-string and ignorable characters.
365 // Since the File Manager doesn't prevent the NUL character (value zero) as part of a filename,
366 // the case mapping table is assumed to map u+0000 to some non-zero value (like 0xFFFF, which is
367 // an invalid Unicode character).
372 // c1 = GetNextValidChar(str1) // returns zero if at end of string
373 // c2 = GetNextValidChar(str2)
375 // if (c1 != c2) break // found a difference
377 // if (c1 == 0) // reached end of string on both strings at once?
378 // return 0; // yes, so strings are equal
381 // // When we get here, c1 != c2. So, we just need to determine which one is less.
388 SInt32
FastUnicodeCompare ( register ConstUniCharArrayPtr str1
, register ItemCount length1
,
389 register ConstUniCharArrayPtr str2
, register ItemCount length2
)
391 register UInt16 c1
,c2
;
392 register UInt16 temp
;
393 register UInt16
* lowerCaseTable
;
395 lowerCaseTable
= (UInt16
*) gLowerCaseTable
;
398 /* Set default values for c1, c2 in case there are no more valid chars */
402 /* Find next non-ignorable char from str1, or zero if no more */
403 while (length1
&& c1
== 0) {
406 /* check for basic latin first */
408 c1
= gLatinCaseFold
[c1
];
411 /* case fold if neccessary */
412 if ((temp
= lowerCaseTable
[c1
>>8]) != 0)
413 c1
= lowerCaseTable
[temp
+ (c1
& 0x00FF)];
417 /* Find next non-ignorable char from str2, or zero if no more */
418 while (length2
&& c2
== 0) {
421 /* check for basic latin first */
423 c2
= gLatinCaseFold
[c2
];
426 /* case fold if neccessary */
427 if ((temp
= lowerCaseTable
[c2
>>8]) != 0)
428 c2
= lowerCaseTable
[temp
+ (c2
& 0x00FF)];
431 if (c1
!= c2
) // found a difference, so stop looping
434 if (c1
== 0) // did we reach the end of both strings at the same time?
435 return 0; // yes, so strings are equal
446 ConvertUnicodeToUTF8Mangled(ByteCount srcLen
, ConstUniCharArrayPtr srcStr
, ByteCount maxDstLen
,
447 ByteCount
*actualDstLen
, unsigned char* dstStr
, HFSCatalogNodeID cnid
)
454 GetFileIDString(cnid
, fileIDStr
);
455 GetFilenameExtension(srcLen
/sizeof(UniChar
), srcStr
, extStr
);
457 /* remove extension chars from source */
458 srcLen
-= strlen(extStr
) * sizeof(UniChar
);
459 subMaxLen
= maxDstLen
- (strlen(extStr
) + strlen(fileIDStr
));
461 (void) utf8_encodestr(srcStr
, srcLen
, dstStr
, &utf8len
, subMaxLen
, ':', 0);
463 strcat(dstStr
, fileIDStr
);
464 strcat(dstStr
, extStr
);
465 *actualDstLen
= utf8len
+ (strlen(extStr
) + strlen(fileIDStr
));