2 * linux/fs/hfsplus/unicode.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handler routines for unicode strings
11 #include <linux/types.h>
12 #include <linux/nls.h>
13 #include "hfsplus_fs.h"
14 #include "hfsplus_raw.h"
16 /* Fold the case of a unicode char, given the 16 bit value */
17 /* Returns folded char, or 0 if ignorable */
18 static inline u16
case_fold(u16 c
)
22 tmp
= hfsplus_case_fold_table
[c
>> 8];
24 tmp
= hfsplus_case_fold_table
[tmp
+ (c
& 0xff)];
30 /* Compare unicode strings, return values like normal strcmp */
31 int hfsplus_strcasecmp(const struct hfsplus_unistr
*s1
,
32 const struct hfsplus_unistr
*s2
)
34 u16 len1
, len2
, c1
, c2
;
35 const hfsplus_unichr
*p1
, *p2
;
37 len1
= be16_to_cpu(s1
->length
);
38 len2
= be16_to_cpu(s2
->length
);
46 c1
= case_fold(be16_to_cpu(*p1
));
51 c2
= case_fold(be16_to_cpu(*p2
));
57 return (c1
< c2
) ? -1 : 1;
63 /* Compare names as a sequence of 16-bit unsigned integers */
64 int hfsplus_strcmp(const struct hfsplus_unistr
*s1
,
65 const struct hfsplus_unistr
*s2
)
67 u16 len1
, len2
, c1
, c2
;
68 const hfsplus_unichr
*p1
, *p2
;
71 len1
= be16_to_cpu(s1
->length
);
72 len2
= be16_to_cpu(s2
->length
);
76 for (len
= min(len1
, len2
); len
> 0; len
--) {
77 c1
= be16_to_cpu(*p1
);
78 c2
= be16_to_cpu(*p2
);
80 return c1
< c2
? -1 : 1;
85 return len1
< len2
? -1 :
90 #define Hangul_SBase 0xac00
91 #define Hangul_LBase 0x1100
92 #define Hangul_VBase 0x1161
93 #define Hangul_TBase 0x11a7
94 #define Hangul_SCount 11172
95 #define Hangul_LCount 19
96 #define Hangul_VCount 21
97 #define Hangul_TCount 28
98 #define Hangul_NCount (Hangul_VCount * Hangul_TCount)
101 static u16
*hfsplus_compose_lookup(u16
*p
, u16 cc
)
107 if (!e
|| cc
< p
[s
* 2] || cc
> p
[e
* 2])
113 else if (cc
< p
[i
* 2])
116 return hfsplus_compose_table
+ p
[i
* 2 + 1];
121 int hfsplus_uni2asc(struct super_block
*sb
,
122 const struct hfsplus_unistr
*ustr
,
123 char *astr
, int *len_p
)
125 const hfsplus_unichr
*ip
;
126 struct nls_table
*nls
= HFSPLUS_SB(sb
)->nls
;
130 int i
, len
, ustrlen
, res
, compose
;
134 ustrlen
= be16_to_cpu(ustr
->length
);
137 compose
= !test_bit(HFSPLUS_SB_NODECOMPOSE
, &HFSPLUS_SB(sb
)->flags
);
139 while (ustrlen
> 0) {
140 c0
= be16_to_cpu(*ip
++);
142 /* search for single decomposed char */
144 ce1
= hfsplus_compose_lookup(hfsplus_compose_table
, c0
);
150 /* start of a possibly decomposed Hangul char */
155 c1
= be16_to_cpu(*ip
) - Hangul_VBase
;
156 if (c1
< Hangul_VCount
) {
157 /* compose the Hangul char */
158 cc
= (c0
- Hangul_LBase
) * Hangul_VCount
;
159 cc
= (cc
+ c1
) * Hangul_TCount
;
165 c1
= be16_to_cpu(*ip
) - Hangul_TBase
;
166 if (c1
> 0 && c1
< Hangul_TCount
) {
175 /* main loop for common case of not composed chars */
178 c1
= be16_to_cpu(*ip
);
180 ce1
= hfsplus_compose_lookup(
181 hfsplus_compose_table
, c1
);
192 res
= nls
->uni2char(c0
, op
, len
);
194 if (res
== -ENAMETOOLONG
)
205 ce2
= hfsplus_compose_lookup(ce1
, c0
);
208 while (i
< ustrlen
) {
209 ce1
= hfsplus_compose_lookup(ce2
,
235 res
= nls
->uni2char(cc
, op
, len
);
237 if (res
== -ENAMETOOLONG
)
247 *len_p
= (char *)op
- astr
;
252 * Convert one or more ASCII characters into a single unicode character.
253 * Returns the number of ASCII characters corresponding to the unicode char.
255 static inline int asc2unichar(struct super_block
*sb
, const char *astr
, int len
,
258 int size
= HFSPLUS_SB(sb
)->nls
->char2uni(astr
, len
, uc
);
274 /* Decomposes a single unicode character. */
275 static inline u16
*decompose_unichar(wchar_t uc
, int *size
)
279 off
= hfsplus_decompose_table
[(uc
>> 12) & 0xf];
280 if (off
== 0 || off
== 0xffff)
283 off
= hfsplus_decompose_table
[off
+ ((uc
>> 8) & 0xf)];
287 off
= hfsplus_decompose_table
[off
+ ((uc
>> 4) & 0xf)];
291 off
= hfsplus_decompose_table
[off
+ (uc
& 0xf)];
295 return hfsplus_decompose_table
+ (off
/ 4);
298 int hfsplus_asc2uni(struct super_block
*sb
,
299 struct hfsplus_unistr
*ustr
, int max_unistr_len
,
300 const char *astr
, int len
)
302 int size
, dsize
, decompose
;
303 u16
*dstr
, outlen
= 0;
306 decompose
= !test_bit(HFSPLUS_SB_NODECOMPOSE
, &HFSPLUS_SB(sb
)->flags
);
307 while (outlen
< max_unistr_len
&& len
> 0) {
308 size
= asc2unichar(sb
, astr
, len
, &c
);
311 dstr
= decompose_unichar(c
, &dsize
);
315 if (outlen
+ dsize
> max_unistr_len
)
318 ustr
->unicode
[outlen
++] = cpu_to_be16(*dstr
++);
319 } while (--dsize
> 0);
321 ustr
->unicode
[outlen
++] = cpu_to_be16(c
);
326 ustr
->length
= cpu_to_be16(outlen
);
328 return -ENAMETOOLONG
;
333 * Hash a string to an integer as appropriate for the HFS+ filesystem.
334 * Composed unicode characters are decomposed and case-folding is performed
335 * if the appropriate bits are (un)set on the superblock.
337 int hfsplus_hash_dentry(const struct dentry
*dentry
, const struct inode
*inode
,
340 struct super_block
*sb
= dentry
->d_sb
;
343 int casefold
, decompose
, size
, len
;
348 casefold
= test_bit(HFSPLUS_SB_CASEFOLD
, &HFSPLUS_SB(sb
)->flags
);
349 decompose
= !test_bit(HFSPLUS_SB_NODECOMPOSE
, &HFSPLUS_SB(sb
)->flags
);
350 hash
= init_name_hash();
354 int uninitialized_var(dsize
);
355 size
= asc2unichar(sb
, astr
, len
, &c
);
360 dstr
= decompose_unichar(c
, &dsize
);
369 hash
= partial_name_hash(c2
, hash
);
370 } while (--dsize
> 0);
376 hash
= partial_name_hash(c2
, hash
);
379 str
->hash
= end_name_hash(hash
);
385 * Compare strings with HFS+ filename ordering.
386 * Composed unicode characters are decomposed and case-folding is performed
387 * if the appropriate bits are (un)set on the superblock.
389 int hfsplus_compare_dentry(const struct dentry
*parent
,
390 const struct inode
*pinode
,
391 const struct dentry
*dentry
, const struct inode
*inode
,
392 unsigned int len
, const char *str
, const struct qstr
*name
)
394 struct super_block
*sb
= parent
->d_sb
;
395 int casefold
, decompose
, size
;
396 int dsize1
, dsize2
, len1
, len2
;
397 const u16
*dstr1
, *dstr2
;
398 const char *astr1
, *astr2
;
402 casefold
= test_bit(HFSPLUS_SB_CASEFOLD
, &HFSPLUS_SB(sb
)->flags
);
403 decompose
= !test_bit(HFSPLUS_SB_NODECOMPOSE
, &HFSPLUS_SB(sb
)->flags
);
409 dstr1
= dstr2
= NULL
;
411 while (len1
> 0 && len2
> 0) {
413 size
= asc2unichar(sb
, astr1
, len1
, &c
);
418 dstr1
= decompose_unichar(c
, &dsize1
);
419 if (!decompose
|| !dstr1
) {
427 size
= asc2unichar(sb
, astr2
, len2
, &c
);
432 dstr2
= decompose_unichar(c
, &dsize2
);
433 if (!decompose
|| !dstr2
) {