2 * Copyright (c) 2000-2004 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@
23 File: UnicodeWrappers.c
25 Contains: Wrapper routines for Unicode conversion and comparison.
28 #include <sys/param.h>
29 #include <sys/utfconv.h>
31 #include "../../hfs_macos_defs.h"
32 #include "UCStringCompareData.h"
34 #include "../headers/FileMgrInternal.h"
35 #include "../headers/HFSUnicodeWrappers.h"
38 kMinFileExtensionChars
= 1, /* does not include dot */
39 kMaxFileExtensionChars
= 5 /* does not include dot */
43 #define EXTENSIONCHAR(c) (((c) >= 0x61 && (c) <= 0x7A) || \
44 ((c) >= 0x41 && (c) <= 0x5A) || \
45 ((c) >= 0x30 && (c) <= 0x39))
48 #define IsHexDigit(c) (((c) >= (UInt8) '0' && (c) <= (UInt8) '9') || \
49 ((c) >= (UInt8) 'A' && (c) <= (UInt8) 'F'))
52 static void GetFilenameExtension( ItemCount length
, ConstUniCharArrayPtr unicodeStr
, char* extStr
);
55 static UInt32
HexStringToInteger( UInt32 length
, const UInt8
*hexStr
);
59 * Get filename extension (if any) as a C string
62 GetFilenameExtension(ItemCount length
, ConstUniCharArrayPtr unicodeStr
, char * extStr
)
66 UInt16 extChars
; /* number of extension chars (excluding dot) */
68 Boolean foundExtension
;
70 extStr
[0] = '\0'; /* assume there's no extension */
73 return; /* "x.y" is smallest possible extension */
75 if ( length
< (kMaxFileExtensionChars
+ 2) )
76 maxExtChars
= length
- 2; /* save room for prefix + dot */
78 maxExtChars
= kMaxFileExtensionChars
;
82 foundExtension
= false;
84 while ( extChars
<= maxExtChars
) {
87 /* look for leading dot */
88 if ( c
== (UniChar
) '.' ) {
89 if ( extChars
> 0 ) /* cannot end with a dot */
90 foundExtension
= true;
94 if ( EXTENSIONCHAR(c
) )
100 /* if we found one then copy it */
101 if ( foundExtension
) {
102 UInt8
*extStrPtr
= extStr
;
103 const UniChar
*unicodeStrPtr
= &unicodeStr
[i
];
105 for ( i
= 0; i
<= extChars
; ++i
)
106 *(extStrPtr
++) = (UInt8
) *(unicodeStrPtr
++);
107 extStr
[extChars
+ 1] = '\0'; /* terminate extension + dot */
114 * Count filename extension characters (if any)
117 CountFilenameExtensionChars( const unsigned char * filename
, UInt32 length
)
121 UInt32 extChars
; /* number of extension chars (excluding dot) */
123 Boolean foundExtension
;
126 return 0; /* "x.y" is smallest possible extension */
128 if ( length
< (kMaxFileExtensionChars
+ 2) )
129 maxExtChars
= length
- 2; /* save room for prefix + dot */
131 maxExtChars
= kMaxFileExtensionChars
;
133 extChars
= 0; /* assume there's no extension */
134 i
= length
- 1; /* index to last ascii character */
135 foundExtension
= false;
137 while ( extChars
<= maxExtChars
) {
140 /* look for leading dot */
141 if ( c
== (UInt8
) '.' ) {
142 if ( extChars
> 0 ) /* cannot end with a dot */
148 if ( EXTENSIONCHAR(c
) )
159 * extract the file id from a mangled name
162 GetEmbeddedFileID(const unsigned char * filename
, UInt32 length
, UInt32
*prefixLength
)
170 if ( filename
== NULL
)
174 return 0; /* too small to have been mangled */
176 /* big enough for a file ID (#10) and an extension (.x) ? */
178 extChars
= CountFilenameExtensionChars(filename
, length
);
182 /* skip over dot plus extension characters */
184 length
-= (extChars
+ 1);
186 /* scan for file id digits */
187 for ( i
= length
- 1; i
>= 0; --i
) {
190 /* look for file ID marker */
192 if ( (length
- i
) < 3 )
193 break; /* too small to be a file ID */
196 return HexStringToInteger(length
- i
- 1, &filename
[i
+1]);
199 if ( !IsHexDigit(c
) )
200 break; /* file ID string must have hex digits */
209 HexStringToInteger(UInt32 length
, const UInt8
*hexStr
)
219 for ( i
= 0; i
< length
; ++i
) {
222 if (c
>= '0' && c
<= '9') {
224 value
+= (UInt32
) c
- (UInt32
) '0';
225 } else if (c
>= 'A' && c
<= 'F') {
227 value
+= 10 + ((unsigned int) c
- (unsigned int) 'A');
229 return 0; /* bad character */
238 * Routine: FastRelString
240 * Output: returns -1 if str1 < str2
241 * returns 1 if str1 > str2
245 SInt32
FastRelString( ConstStr255Param str1
, ConstStr255Param str2
)
247 UInt16
* compareTable
;
249 UInt8 length
, length2
;
256 if (length
== length2
)
258 else if (length
< length2
)
261 delta
= length2
- length
;
269 compareTable
= (UInt16
*) gCompareTable
;
278 if (aChar
!= bChar
) // If they don't match exacly, do case conversion
280 UInt16 aSortWord
, bSortWord
;
282 aSortWord
= compareTable
[aChar
];
283 bSortWord
= compareTable
[bChar
];
285 if (aSortWord
> bSortWord
)
288 if (aSortWord
< bSortWord
)
292 // If characters match exactly, then go on to next character immediately without
293 // doing any extra work.
296 // if you got to here, then return bestGuess
303 // FastUnicodeCompare - Compare two Unicode strings; produce a relative ordering
306 // --------------------------
311 // The lower case table starts with 256 entries (one for each of the upper bytes
312 // of the original Unicode char). If that entry is zero, then all characters with
313 // that upper byte are already case folded. If the entry is non-zero, then it is
314 // the _index_ (not byte offset) of the start of the sub-table for the characters
315 // with that upper byte. All ignorable characters are folded to the value zero.
319 // Let c = source Unicode character
320 // Let table[] = lower case table
322 // lower = table[highbyte(c)]
326 // lower = table[lower+lowbyte(c)]
329 // ignore this character
331 // To handle ignorable characters, we now need a loop to find the next valid character.
332 // Also, we can't pre-compute the number of characters to compare; the string length might
333 // be larger than the number of non-ignorable characters. Further, we must be able to handle
334 // ignorable characters at any point in the string, including as the first or last characters.
335 // We use a zero value as a sentinel to detect both end-of-string and ignorable characters.
336 // Since the File Manager doesn't prevent the NUL character (value zero) as part of a filename,
337 // the case mapping table is assumed to map u+0000 to some non-zero value (like 0xFFFF, which is
338 // an invalid Unicode character).
343 // c1 = GetNextValidChar(str1) // returns zero if at end of string
344 // c2 = GetNextValidChar(str2)
346 // if (c1 != c2) break // found a difference
348 // if (c1 == 0) // reached end of string on both strings at once?
349 // return 0; // yes, so strings are equal
352 // // When we get here, c1 != c2. So, we just need to determine which one is less.
359 SInt32
FastUnicodeCompare ( register ConstUniCharArrayPtr str1
, register ItemCount length1
,
360 register ConstUniCharArrayPtr str2
, register ItemCount length2
)
362 register UInt16 c1
,c2
;
363 register UInt16 temp
;
364 register UInt16
* lowerCaseTable
;
366 lowerCaseTable
= (UInt16
*) gLowerCaseTable
;
369 /* Set default values for c1, c2 in case there are no more valid chars */
373 /* Find next non-ignorable char from str1, or zero if no more */
374 while (length1
&& c1
== 0) {
377 /* check for basic latin first */
379 c1
= gLatinCaseFold
[c1
];
382 /* case fold if neccessary */
383 if ((temp
= lowerCaseTable
[c1
>>8]) != 0)
384 c1
= lowerCaseTable
[temp
+ (c1
& 0x00FF)];
388 /* Find next non-ignorable char from str2, or zero if no more */
389 while (length2
&& c2
== 0) {
392 /* check for basic latin first */
394 c2
= gLatinCaseFold
[c2
];
397 /* case fold if neccessary */
398 if ((temp
= lowerCaseTable
[c2
>>8]) != 0)
399 c2
= lowerCaseTable
[temp
+ (c2
& 0x00FF)];
402 if (c1
!= c2
) // found a difference, so stop looping
405 if (c1
== 0) // did we reach the end of both strings at the same time?
406 return 0; // yes, so strings are equal
417 ConvertUnicodeToUTF8Mangled(ByteCount srcLen
, ConstUniCharArrayPtr srcStr
, ByteCount maxDstLen
,
418 ByteCount
*actualDstLen
, unsigned char* dstStr
, HFSCatalogNodeID cnid
)
425 sprintf(fileIDStr
, "#%X", cnid
);
426 GetFilenameExtension(srcLen
/sizeof(UniChar
), srcStr
, extStr
);
428 /* remove extension chars from source */
429 srcLen
-= strlen(extStr
) * sizeof(UniChar
);
430 subMaxLen
= maxDstLen
- (strlen(extStr
) + strlen(fileIDStr
));
432 (void) utf8_encodestr(srcStr
, srcLen
, dstStr
, &utf8len
, subMaxLen
, ':', 0);
434 strcat(dstStr
, fileIDStr
);
435 strcat(dstStr
, extStr
);
436 *actualDstLen
= utf8len
+ (strlen(extStr
) + strlen(fileIDStr
));