2 * Copyright (c) 2000-2004 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.
34 #include <sys/param.h>
35 #include <sys/utfconv.h>
37 #include "../../hfs_macos_defs.h"
38 #include "UCStringCompareData.h"
40 #include "../headers/FileMgrInternal.h"
41 #include "../headers/HFSUnicodeWrappers.h"
44 kMinFileExtensionChars
= 1, /* does not include dot */
45 kMaxFileExtensionChars
= 5 /* does not include dot */
49 #define EXTENSIONCHAR(c) (((c) >= 0x61 && (c) <= 0x7A) || \
50 ((c) >= 0x41 && (c) <= 0x5A) || \
51 ((c) >= 0x30 && (c) <= 0x39))
54 #define IsHexDigit(c) (((c) >= (UInt8) '0' && (c) <= (UInt8) '9') || \
55 ((c) >= (UInt8) 'A' && (c) <= (UInt8) 'F'))
58 static void GetFilenameExtension( ItemCount length
, ConstUniCharArrayPtr unicodeStr
, char* extStr
);
61 static UInt32
HexStringToInteger( UInt32 length
, const UInt8
*hexStr
);
65 * Get filename extension (if any) as a C string
68 GetFilenameExtension(ItemCount length
, ConstUniCharArrayPtr unicodeStr
, char * extStr
)
72 UInt16 extChars
; /* number of extension chars (excluding dot) */
74 Boolean foundExtension
;
76 extStr
[0] = '\0'; /* assume there's no extension */
79 return; /* "x.y" is smallest possible extension */
81 if ( length
< (kMaxFileExtensionChars
+ 2) )
82 maxExtChars
= length
- 2; /* save room for prefix + dot */
84 maxExtChars
= kMaxFileExtensionChars
;
88 foundExtension
= false;
90 while ( extChars
<= maxExtChars
) {
93 /* look for leading dot */
94 if ( c
== (UniChar
) '.' ) {
95 if ( extChars
> 0 ) /* cannot end with a dot */
96 foundExtension
= true;
100 if ( EXTENSIONCHAR(c
) )
106 /* if we found one then copy it */
107 if ( foundExtension
) {
108 UInt8
*extStrPtr
= extStr
;
109 const UniChar
*unicodeStrPtr
= &unicodeStr
[i
];
111 for ( i
= 0; i
<= extChars
; ++i
)
112 *(extStrPtr
++) = (UInt8
) *(unicodeStrPtr
++);
113 extStr
[extChars
+ 1] = '\0'; /* terminate extension + dot */
120 * Count filename extension characters (if any)
123 CountFilenameExtensionChars( const unsigned char * filename
, UInt32 length
)
127 UInt32 extChars
; /* number of extension chars (excluding dot) */
129 Boolean foundExtension
;
132 return 0; /* "x.y" is smallest possible extension */
134 if ( length
< (kMaxFileExtensionChars
+ 2) )
135 maxExtChars
= length
- 2; /* save room for prefix + dot */
137 maxExtChars
= kMaxFileExtensionChars
;
139 extChars
= 0; /* assume there's no extension */
140 i
= length
- 1; /* index to last ascii character */
141 foundExtension
= false;
143 while ( extChars
<= maxExtChars
) {
146 /* look for leading dot */
147 if ( c
== (UInt8
) '.' ) {
148 if ( extChars
> 0 ) /* cannot end with a dot */
154 if ( EXTENSIONCHAR(c
) )
165 * extract the file id from a mangled name
168 GetEmbeddedFileID(const unsigned char * filename
, UInt32 length
, UInt32
*prefixLength
)
176 if ( filename
== NULL
)
180 return 0; /* too small to have been mangled */
182 /* big enough for a file ID (#10) and an extension (.x) ? */
184 extChars
= CountFilenameExtensionChars(filename
, length
);
188 /* skip over dot plus extension characters */
190 length
-= (extChars
+ 1);
192 /* scan for file id digits */
193 for ( i
= length
- 1; i
>= 0; --i
) {
196 /* look for file ID marker */
198 if ( (length
- i
) < 3 )
199 break; /* too small to be a file ID */
202 return HexStringToInteger(length
- i
- 1, &filename
[i
+1]);
205 if ( !IsHexDigit(c
) )
206 break; /* file ID string must have hex digits */
215 HexStringToInteger(UInt32 length
, const UInt8
*hexStr
)
225 for ( i
= 0; i
< length
; ++i
) {
228 if (c
>= '0' && c
<= '9') {
230 value
+= (UInt32
) c
- (UInt32
) '0';
231 } else if (c
>= 'A' && c
<= 'F') {
233 value
+= 10 + ((unsigned int) c
- (unsigned int) 'A');
235 return 0; /* bad character */
244 * Routine: FastRelString
246 * Output: returns -1 if str1 < str2
247 * returns 1 if str1 > str2
251 SInt32
FastRelString( ConstStr255Param str1
, ConstStr255Param str2
)
253 UInt16
* compareTable
;
255 UInt8 length
, length2
;
262 if (length
== length2
)
264 else if (length
< length2
)
267 delta
= length2
- length
;
275 compareTable
= (UInt16
*) gCompareTable
;
284 if (aChar
!= bChar
) // If they don't match exacly, do case conversion
286 UInt16 aSortWord
, bSortWord
;
288 aSortWord
= compareTable
[aChar
];
289 bSortWord
= compareTable
[bChar
];
291 if (aSortWord
> bSortWord
)
294 if (aSortWord
< bSortWord
)
298 // If characters match exactly, then go on to next character immediately without
299 // doing any extra work.
302 // if you got to here, then return bestGuess
309 // FastUnicodeCompare - Compare two Unicode strings; produce a relative ordering
312 // --------------------------
317 // The lower case table starts with 256 entries (one for each of the upper bytes
318 // of the original Unicode char). If that entry is zero, then all characters with
319 // that upper byte are already case folded. If the entry is non-zero, then it is
320 // the _index_ (not byte offset) of the start of the sub-table for the characters
321 // with that upper byte. All ignorable characters are folded to the value zero.
325 // Let c = source Unicode character
326 // Let table[] = lower case table
328 // lower = table[highbyte(c)]
332 // lower = table[lower+lowbyte(c)]
335 // ignore this character
337 // To handle ignorable characters, we now need a loop to find the next valid character.
338 // Also, we can't pre-compute the number of characters to compare; the string length might
339 // be larger than the number of non-ignorable characters. Further, we must be able to handle
340 // ignorable characters at any point in the string, including as the first or last characters.
341 // We use a zero value as a sentinel to detect both end-of-string and ignorable characters.
342 // Since the File Manager doesn't prevent the NUL character (value zero) as part of a filename,
343 // the case mapping table is assumed to map u+0000 to some non-zero value (like 0xFFFF, which is
344 // an invalid Unicode character).
349 // c1 = GetNextValidChar(str1) // returns zero if at end of string
350 // c2 = GetNextValidChar(str2)
352 // if (c1 != c2) break // found a difference
354 // if (c1 == 0) // reached end of string on both strings at once?
355 // return 0; // yes, so strings are equal
358 // // When we get here, c1 != c2. So, we just need to determine which one is less.
365 SInt32
FastUnicodeCompare ( register ConstUniCharArrayPtr str1
, register ItemCount length1
,
366 register ConstUniCharArrayPtr str2
, register ItemCount length2
)
368 register UInt16 c1
,c2
;
369 register UInt16 temp
;
370 register UInt16
* lowerCaseTable
;
372 lowerCaseTable
= (UInt16
*) gLowerCaseTable
;
375 /* Set default values for c1, c2 in case there are no more valid chars */
379 /* Find next non-ignorable char from str1, or zero if no more */
380 while (length1
&& c1
== 0) {
383 /* check for basic latin first */
385 c1
= gLatinCaseFold
[c1
];
388 /* case fold if neccessary */
389 if ((temp
= lowerCaseTable
[c1
>>8]) != 0)
390 c1
= lowerCaseTable
[temp
+ (c1
& 0x00FF)];
394 /* Find next non-ignorable char from str2, or zero if no more */
395 while (length2
&& c2
== 0) {
398 /* check for basic latin first */
400 c2
= gLatinCaseFold
[c2
];
403 /* case fold if neccessary */
404 if ((temp
= lowerCaseTable
[c2
>>8]) != 0)
405 c2
= lowerCaseTable
[temp
+ (c2
& 0x00FF)];
408 if (c1
!= c2
) // found a difference, so stop looping
411 if (c1
== 0) // did we reach the end of both strings at the same time?
412 return 0; // yes, so strings are equal
423 ConvertUnicodeToUTF8Mangled(ByteCount srcLen
, ConstUniCharArrayPtr srcStr
, ByteCount maxDstLen
,
424 ByteCount
*actualDstLen
, unsigned char* dstStr
, HFSCatalogNodeID cnid
)
431 sprintf(fileIDStr
, "#%X", cnid
);
432 GetFilenameExtension(srcLen
/sizeof(UniChar
), srcStr
, extStr
);
434 /* remove extension chars from source */
435 srcLen
-= strlen(extStr
) * sizeof(UniChar
);
436 subMaxLen
= maxDstLen
- (strlen(extStr
) + strlen(fileIDStr
));
438 (void) utf8_encodestr(srcStr
, srcLen
, dstStr
, &utf8len
, subMaxLen
, ':', 0);
440 strcat(dstStr
, fileIDStr
);
441 strcat(dstStr
, extStr
);
442 *actualDstLen
= utf8len
+ (strlen(extStr
) + strlen(fileIDStr
));