2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 File: UnicodeWrappers.c
26 Contains: Wrapper routines for Unicode conversion and comparison.
29 #include <sys/param.h>
30 #include <sys/utfconv.h>
32 #include "../../hfs_macos_defs.h"
33 #include "UCStringCompareData.h"
35 #include "../headers/FileMgrInternal.h"
36 #include "../headers/HFSUnicodeWrappers.h"
39 kMinFileExtensionChars
= 1, /* does not include dot */
40 kMaxFileExtensionChars
= 5 /* does not include dot */
44 #define EXTENSIONCHAR(c) (((c) >= 0x61 && (c) <= 0x7A) || \
45 ((c) >= 0x41 && (c) <= 0x5A) || \
46 ((c) >= 0x30 && (c) <= 0x39))
49 #define IsHexDigit(c) (((c) >= (UInt8) '0' && (c) <= (UInt8) '9') || \
50 ((c) >= (UInt8) 'A' && (c) <= (UInt8) 'F'))
53 static void GetFilenameExtension( ItemCount length
, ConstUniCharArrayPtr unicodeStr
, char* extStr
);
56 static UInt32
HexStringToInteger( UInt32 length
, const UInt8
*hexStr
);
60 * Get filename extension (if any) as a C string
63 GetFilenameExtension(ItemCount length
, ConstUniCharArrayPtr unicodeStr
, char * extStr
)
67 UInt16 extChars
; /* number of extension chars (excluding dot) */
69 Boolean foundExtension
;
71 extStr
[0] = '\0'; /* assume there's no extension */
74 return; /* "x.y" is smallest possible extension */
76 if ( length
< (kMaxFileExtensionChars
+ 2) )
77 maxExtChars
= length
- 2; /* save room for prefix + dot */
79 maxExtChars
= kMaxFileExtensionChars
;
83 foundExtension
= false;
85 while ( extChars
<= maxExtChars
) {
88 /* look for leading dot */
89 if ( c
== (UniChar
) '.' ) {
90 if ( extChars
> 0 ) /* cannot end with a dot */
91 foundExtension
= true;
95 if ( EXTENSIONCHAR(c
) )
101 /* if we found one then copy it */
102 if ( foundExtension
) {
103 UInt8
*extStrPtr
= extStr
;
104 const UniChar
*unicodeStrPtr
= &unicodeStr
[i
];
106 for ( i
= 0; i
<= extChars
; ++i
)
107 *(extStrPtr
++) = (UInt8
) *(unicodeStrPtr
++);
108 extStr
[extChars
+ 1] = '\0'; /* terminate extension + dot */
115 * Count filename extension characters (if any)
118 CountFilenameExtensionChars( const unsigned char * filename
, UInt32 length
)
122 UInt32 extChars
; /* number of extension chars (excluding dot) */
124 Boolean foundExtension
;
127 return 0; /* "x.y" is smallest possible extension */
129 if ( length
< (kMaxFileExtensionChars
+ 2) )
130 maxExtChars
= length
- 2; /* save room for prefix + dot */
132 maxExtChars
= kMaxFileExtensionChars
;
134 extChars
= 0; /* assume there's no extension */
135 i
= length
- 1; /* index to last ascii character */
136 foundExtension
= false;
138 while ( extChars
<= maxExtChars
) {
141 /* look for leading dot */
142 if ( c
== (UInt8
) '.' ) {
143 if ( extChars
> 0 ) /* cannot end with a dot */
149 if ( EXTENSIONCHAR(c
) )
160 * extract the file id from a mangled name
163 GetEmbeddedFileID(const unsigned char * filename
, UInt32 length
, UInt32
*prefixLength
)
171 if ( filename
== NULL
)
175 return 0; /* too small to have been mangled */
177 /* big enough for a file ID (#10) and an extension (.x) ? */
179 extChars
= CountFilenameExtensionChars(filename
, length
);
183 /* skip over dot plus extension characters */
185 length
-= (extChars
+ 1);
187 /* scan for file id digits */
188 for ( i
= length
- 1; i
>= 0; --i
) {
191 /* look for file ID marker */
193 if ( (length
- i
) < 3 )
194 break; /* too small to be a file ID */
197 return HexStringToInteger(length
- i
- 1, &filename
[i
+1]);
200 if ( !IsHexDigit(c
) )
201 break; /* file ID string must have hex digits */
210 HexStringToInteger(UInt32 length
, const UInt8
*hexStr
)
220 for ( i
= 0; i
< length
; ++i
) {
223 if (c
>= '0' && c
<= '9') {
225 value
+= (UInt32
) c
- (UInt32
) '0';
226 } else if (c
>= 'A' && c
<= 'F') {
228 value
+= 10 + ((unsigned int) c
- (unsigned int) 'A');
230 return 0; /* bad character */
239 * Routine: FastRelString
241 * Output: returns -1 if str1 < str2
242 * returns 1 if str1 > str2
246 SInt32
FastRelString( ConstStr255Param str1
, ConstStr255Param str2
)
248 UInt16
* compareTable
;
250 UInt8 length
, length2
;
257 if (length
== length2
)
259 else if (length
< length2
)
262 delta
= length2
- length
;
270 compareTable
= (UInt16
*) gCompareTable
;
279 if (aChar
!= bChar
) // If they don't match exacly, do case conversion
281 UInt16 aSortWord
, bSortWord
;
283 aSortWord
= compareTable
[aChar
];
284 bSortWord
= compareTable
[bChar
];
286 if (aSortWord
> bSortWord
)
289 if (aSortWord
< bSortWord
)
293 // If characters match exactly, then go on to next character immediately without
294 // doing any extra work.
297 // if you got to here, then return bestGuess
304 // FastUnicodeCompare - Compare two Unicode strings; produce a relative ordering
307 // --------------------------
312 // The lower case table starts with 256 entries (one for each of the upper bytes
313 // of the original Unicode char). If that entry is zero, then all characters with
314 // that upper byte are already case folded. If the entry is non-zero, then it is
315 // the _index_ (not byte offset) of the start of the sub-table for the characters
316 // with that upper byte. All ignorable characters are folded to the value zero.
320 // Let c = source Unicode character
321 // Let table[] = lower case table
323 // lower = table[highbyte(c)]
327 // lower = table[lower+lowbyte(c)]
330 // ignore this character
332 // To handle ignorable characters, we now need a loop to find the next valid character.
333 // Also, we can't pre-compute the number of characters to compare; the string length might
334 // be larger than the number of non-ignorable characters. Further, we must be able to handle
335 // ignorable characters at any point in the string, including as the first or last characters.
336 // We use a zero value as a sentinel to detect both end-of-string and ignorable characters.
337 // Since the File Manager doesn't prevent the NUL character (value zero) as part of a filename,
338 // the case mapping table is assumed to map u+0000 to some non-zero value (like 0xFFFF, which is
339 // an invalid Unicode character).
344 // c1 = GetNextValidChar(str1) // returns zero if at end of string
345 // c2 = GetNextValidChar(str2)
347 // if (c1 != c2) break // found a difference
349 // if (c1 == 0) // reached end of string on both strings at once?
350 // return 0; // yes, so strings are equal
353 // // When we get here, c1 != c2. So, we just need to determine which one is less.
360 SInt32
FastUnicodeCompare ( register ConstUniCharArrayPtr str1
, register ItemCount length1
,
361 register ConstUniCharArrayPtr str2
, register ItemCount length2
)
363 register UInt16 c1
,c2
;
364 register UInt16 temp
;
365 register UInt16
* lowerCaseTable
;
367 lowerCaseTable
= (UInt16
*) gLowerCaseTable
;
370 /* Set default values for c1, c2 in case there are no more valid chars */
374 /* Find next non-ignorable char from str1, or zero if no more */
375 while (length1
&& c1
== 0) {
378 /* check for basic latin first */
380 c1
= gLatinCaseFold
[c1
];
383 /* case fold if neccessary */
384 if ((temp
= lowerCaseTable
[c1
>>8]) != 0)
385 c1
= lowerCaseTable
[temp
+ (c1
& 0x00FF)];
389 /* Find next non-ignorable char from str2, or zero if no more */
390 while (length2
&& c2
== 0) {
393 /* check for basic latin first */
395 c2
= gLatinCaseFold
[c2
];
398 /* case fold if neccessary */
399 if ((temp
= lowerCaseTable
[c2
>>8]) != 0)
400 c2
= lowerCaseTable
[temp
+ (c2
& 0x00FF)];
403 if (c1
!= c2
) // found a difference, so stop looping
406 if (c1
== 0) // did we reach the end of both strings at the same time?
407 return 0; // yes, so strings are equal
418 ConvertUnicodeToUTF8Mangled(ByteCount srcLen
, ConstUniCharArrayPtr srcStr
, ByteCount maxDstLen
,
419 ByteCount
*actualDstLen
, unsigned char* dstStr
, HFSCatalogNodeID cnid
)
426 sprintf(fileIDStr
, "#%X", cnid
);
427 GetFilenameExtension(srcLen
/sizeof(UniChar
), srcStr
, extStr
);
429 /* remove extension chars from source */
430 srcLen
-= strlen(extStr
) * sizeof(UniChar
);
431 subMaxLen
= maxDstLen
- (strlen(extStr
) + strlen(fileIDStr
));
433 (void) utf8_encodestr(srcStr
, srcLen
, dstStr
, &utf8len
, subMaxLen
, ':', 0);
435 strcat(dstStr
, fileIDStr
);
436 strcat(dstStr
, extStr
);
437 *actualDstLen
= utf8len
+ (strlen(extStr
) + strlen(fileIDStr
));