]>
git.saurik.com Git - apple/boot.git/blob - i386/libsaio/hfs_compare.c
2 * Copyright (c) 2000 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 * HFSCompare.c - Functions for working with and comparing HFS nams.
25 * Copyright (c) 1999-2000 Apple Computer, Inc.
31 #include "hfs_CaseTables.h"
33 //_______________________________________________________________________
35 // Routine: FastRelString
37 // Output: returns -1 if str1 < str2
38 // returns 1 if str1 > str2
41 //_______________________________________________________________________
43 int32_t FastRelString(char * str1
, char * str2
)
46 u_int8_t length
, length2
;
51 if (length
== length2
)
53 else if (length
< length2
)
63 u_int32_t aChar
, bChar
;
68 if (aChar
!= bChar
) /* If they don't match exacly, do case conversion */
70 u_int16_t aSortWord
, bSortWord
;
72 aSortWord
= gCompareTable
[aChar
];
73 bSortWord
= gCompareTable
[bChar
];
75 if (aSortWord
> bSortWord
)
78 if (aSortWord
< bSortWord
)
83 * If characters match exactly, then go on to next character
84 * immediately without doing any extra work.
88 /* if you got to here, then return bestGuess */
94 // FastUnicodeCompare - Compare two Unicode strings; produce a relative ordering
97 // --------------------------
102 // The lower case table starts with 256 entries (one for each of the upper bytes
103 // of the original Unicode char). If that entry is zero, then all characters with
104 // that upper byte are already case folded. If the entry is non-zero, then it is
105 // the _index_ (not byte offset) of the start of the sub-table for the characters
106 // with that upper byte. All ignorable characters are folded to the value zero.
110 // Let c = source Unicode character
111 // Let table[] = lower case table
113 // lower = table[highbyte(c)]
117 // lower = table[lower+lowbyte(c)]
120 // ignore this character
122 // To handle ignorable characters, we now need a loop to find the next valid character.
123 // Also, we can't pre-compute the number of characters to compare; the string length might
124 // be larger than the number of non-ignorable characters. Further, we must be able to handle
125 // ignorable characters at any point in the string, including as the first or last characters.
126 // We use a zero value as a sentinel to detect both end-of-string and ignorable characters.
127 // Since the File Manager doesn't prevent the NUL character (value zero) as part of a filename,
128 // the case mapping table is assumed to map u+0000 to some non-zero value (like 0xFFFF, which is
129 // an invalid Unicode character).
134 // c1 = GetNextValidChar(str1) // returns zero if at end of string
135 // c2 = GetNextValidChar(str2)
137 // if (c1 != c2) break // found a difference
139 // if (c1 == 0) // reached end of string on both strings at once?
140 // return 0; // yes, so strings are equal
143 // // When we get here, c1 != c2. So, we just need to determine which one is less.
150 int32_t FastUnicodeCompare( u_int16_t
* str1
, register u_int32_t length1
,
151 u_int16_t
* str2
, register u_int32_t length2
)
153 register u_int16_t c1
,c2
;
154 register u_int16_t temp
;
157 /* Set default values for c1, c2 in case there are no more valid chars */
161 /* Find next non-ignorable char from str1, or zero if no more */
162 while (length1
&& c1
== 0) {
163 c1
= SWAP_BE16(*(str1
++));
165 if ((temp
= gLowerCaseTable
[c1
>>8]) != 0) // is there a subtable for this upper byte?
166 c1
= gLowerCaseTable
[temp
+ (c1
& 0x00FF)]; // yes, so fold the char
169 /* Find next non-ignorable char from str2, or zero if no more */
170 while (length2
&& c2
== 0) {
171 c2
= SWAP_BE16(*(str2
++));
173 if ((temp
= gLowerCaseTable
[c2
>>8]) != 0) // is there a subtable for this upper byte?
174 c2
= gLowerCaseTable
[temp
+ (c2
& 0x00FF)]; // yes, so fold the char
177 if (c1
!= c2
) /* found a difference, so stop looping */
180 if (c1
== 0) /* did we reach the end of both strings at the same time? */
181 return 0; /* yes, so strings are equal */
192 * UTF-8 (UCS Transformation Format)
194 * The following subset of UTF-8 is used to encode UCS-2 filenames. It
195 * requires a maximum of three 3 bytes per UCS-2 character. Only the
196 * shortest encoding required to represent the significant UCS-2 bits
199 * UTF-8 Multibyte Codes
201 * Bytes Bits UCS-2 Min UCS-2 Max UTF-8 Byte Sequence (binary)
202 * -------------------------------------------------------------------
203 * 1 7 0x0000 0x007F 0xxxxxxx
204 * 2 11 0x0080 0x07FF 110xxxxx 10xxxxxx
205 * 3 16 0x0800 0xFFFF 1110xxxx 10xxxxxx 10xxxxxx
206 * -------------------------------------------------------------------
211 * utf_encodestr - Encodes the UCS-2 (Unicode) string at ucsp into a
212 * null terminated UTF-8 string at utf8p.
214 * ucslen is the number of UCS-2 input characters (not bytes)
215 * bufsize is the size of the output buffer in bytes
218 utf_encodestr( const u_int16_t
* ucsp
, int ucslen
,
219 u_int8_t
* utf8p
, u_int32_t bufsize
)
224 bufend
= utf8p
+ bufsize
;
226 while (ucslen
-- > 0) {
227 ucs_ch
= SWAP_BE16(*ucsp
++);
229 if (ucs_ch
< 0x0080) {
233 continue; /* skip over embedded NULLs */
236 } else if (ucs_ch
< 0x800) {
237 if ((utf8p
+ 1) >= bufend
)
239 *utf8p
++ = (ucs_ch
>> 6) | 0xc0;
240 *utf8p
++ = (ucs_ch
& 0x3f) | 0x80;
243 if ((utf8p
+ 2) >= bufend
)
245 *utf8p
++ = (ucs_ch
>> 12) | 0xe0;
246 *utf8p
++ = ((ucs_ch
>> 6) & 0x3f) | 0x80;
247 *utf8p
++ = ((ucs_ch
) & 0x3f) | 0x80;
256 * utf_decodestr - Decodes the null terminated UTF-8 string at
257 * utf8p into a UCS-2 (Unicode) string at ucsp.
259 * ucslen is the number of UCS-2 output characters (not bytes)
260 * bufsize is the size of the output buffer in bytes
262 void utf_decodestr(const u_int8_t
* utf8p
, u_int16_t
* ucsp
, u_int16_t
* ucslen
, u_int32_t bufsize
)
270 bufend
= (u_int16_t
*)((u_int8_t
*)ucsp
+ bufsize
);
272 while ((byte
= *utf8p
++) != '\0') {
276 /* check for ascii */
280 *ucsp
++ = SWAP_BE16(ucs_ch
);
284 switch (byte
& 0xf0) {
288 /* extract bits 6 - 10 from first byte */
289 ucs_ch
= (byte
& 0x1F) << 6;
293 /* extract bits 12 - 15 from first byte */
294 ucs_ch
= (byte
& 0x0F) << 6;
296 /* extract bits 6 - 11 from second byte */
297 if (((byte
= *utf8p
++) & 0xc0) != 0x80)
300 ucs_ch
+= (byte
& 0x3F);
307 /* extract bits 0 - 5 from final byte */
308 if (((byte
= *utf8p
++) & 0xc0) != 0x80)
310 ucs_ch
+= (byte
& 0x3F);
312 *ucsp
++ = SWAP_BE16(ucs_ch
);
315 *ucslen
= SWAP_BE16(ucsp
- bufstart
);