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
, const struct hfsplus_unistr
*ustr
, char *astr
, int *len_p
)
123 const hfsplus_unichr
*ip
;
124 struct nls_table
*nls
= HFSPLUS_SB(sb
).nls
;
128 int i
, len
, ustrlen
, res
, compose
;
132 ustrlen
= be16_to_cpu(ustr
->length
);
135 compose
= !(HFSPLUS_SB(sb
).flags
& HFSPLUS_SB_NODECOMPOSE
);
137 while (ustrlen
> 0) {
138 c0
= be16_to_cpu(*ip
++);
140 /* search for single decomposed char */
142 ce1
= hfsplus_compose_lookup(hfsplus_compose_table
, c0
);
143 if (ce1
&& (cc
= ce1
[0])) {
144 /* start of a possibly decomposed Hangul char */
149 c1
= be16_to_cpu(*ip
) - Hangul_VBase
;
150 if (c1
< Hangul_VCount
) {
151 /* compose the Hangul char */
152 cc
= (c0
- Hangul_LBase
) * Hangul_VCount
;
153 cc
= (cc
+ c1
) * Hangul_TCount
;
159 c1
= be16_to_cpu(*ip
) - Hangul_TBase
;
160 if (c1
> 0 && c1
< Hangul_TCount
) {
169 /* main loop for common case of not composed chars */
172 c1
= be16_to_cpu(*ip
);
174 ce1
= hfsplus_compose_lookup(hfsplus_compose_table
, c1
);
185 res
= nls
->uni2char(c0
, op
, len
);
187 if (res
== -ENAMETOOLONG
)
198 ce2
= hfsplus_compose_lookup(ce1
, c0
);
201 while (i
< ustrlen
) {
202 ce1
= hfsplus_compose_lookup(ce2
, be16_to_cpu(ip
[i
]));
226 res
= nls
->uni2char(cc
, op
, len
);
228 if (res
== -ENAMETOOLONG
)
238 *len_p
= (char *)op
- astr
;
243 * Convert one or more ASCII characters into a single unicode character.
244 * Returns the number of ASCII characters corresponding to the unicode char.
246 static inline int asc2unichar(struct super_block
*sb
, const char *astr
, int len
,
249 int size
= HFSPLUS_SB(sb
).nls
->char2uni(astr
, len
, uc
);
265 /* Decomposes a single unicode character. */
266 static inline u16
*decompose_unichar(wchar_t uc
, int *size
)
270 off
= hfsplus_decompose_table
[(uc
>> 12) & 0xf];
271 if (off
== 0 || off
== 0xffff)
274 off
= hfsplus_decompose_table
[off
+ ((uc
>> 8) & 0xf)];
278 off
= hfsplus_decompose_table
[off
+ ((uc
>> 4) & 0xf)];
282 off
= hfsplus_decompose_table
[off
+ (uc
& 0xf)];
286 return hfsplus_decompose_table
+ (off
/ 4);
289 int hfsplus_asc2uni(struct super_block
*sb
, struct hfsplus_unistr
*ustr
,
290 const char *astr
, int len
)
292 int size
, dsize
, decompose
;
293 u16
*dstr
, outlen
= 0;
296 decompose
= !(HFSPLUS_SB(sb
).flags
& HFSPLUS_SB_NODECOMPOSE
);
297 while (outlen
< HFSPLUS_MAX_STRLEN
&& len
> 0) {
298 size
= asc2unichar(sb
, astr
, len
, &c
);
300 if (decompose
&& (dstr
= decompose_unichar(c
, &dsize
))) {
301 if (outlen
+ dsize
> HFSPLUS_MAX_STRLEN
)
304 ustr
->unicode
[outlen
++] = cpu_to_be16(*dstr
++);
305 } while (--dsize
> 0);
307 ustr
->unicode
[outlen
++] = cpu_to_be16(c
);
312 ustr
->length
= cpu_to_be16(outlen
);
314 return -ENAMETOOLONG
;
319 * Hash a string to an integer as appropriate for the HFS+ filesystem.
320 * Composed unicode characters are decomposed and case-folding is performed
321 * if the appropriate bits are (un)set on the superblock.
323 int hfsplus_hash_dentry(struct dentry
*dentry
, struct qstr
*str
)
325 struct super_block
*sb
= dentry
->d_sb
;
328 int casefold
, decompose
, size
, len
;
333 casefold
= (HFSPLUS_SB(sb
).flags
& HFSPLUS_SB_CASEFOLD
);
334 decompose
= !(HFSPLUS_SB(sb
).flags
& HFSPLUS_SB_NODECOMPOSE
);
335 hash
= init_name_hash();
339 int uninitialized_var(dsize
);
340 size
= asc2unichar(sb
, astr
, len
, &c
);
344 if (decompose
&& (dstr
= decompose_unichar(c
, &dsize
))) {
347 if (!casefold
|| (c2
= case_fold(c2
)))
348 hash
= partial_name_hash(c2
, hash
);
349 } while (--dsize
> 0);
352 if (!casefold
|| (c2
= case_fold(c2
)))
353 hash
= partial_name_hash(c2
, hash
);
356 str
->hash
= end_name_hash(hash
);
362 * Compare strings with HFS+ filename ordering.
363 * Composed unicode characters are decomposed and case-folding is performed
364 * if the appropriate bits are (un)set on the superblock.
366 int hfsplus_compare_dentry(struct dentry
*dentry
, struct qstr
*s1
, struct qstr
*s2
)
368 struct super_block
*sb
= dentry
->d_sb
;
369 int casefold
, decompose
, size
;
370 int dsize1
, dsize2
, len1
, len2
;
371 const u16
*dstr1
, *dstr2
;
372 const char *astr1
, *astr2
;
376 casefold
= (HFSPLUS_SB(sb
).flags
& HFSPLUS_SB_CASEFOLD
);
377 decompose
= !(HFSPLUS_SB(sb
).flags
& HFSPLUS_SB_NODECOMPOSE
);
383 dstr1
= dstr2
= NULL
;
385 while (len1
> 0 && len2
> 0) {
387 size
= asc2unichar(sb
, astr1
, len1
, &c
);
391 if (!decompose
|| !(dstr1
= decompose_unichar(c
, &dsize1
))) {
399 size
= asc2unichar(sb
, astr2
, len2
, &c
);
403 if (!decompose
|| !(dstr2
= decompose_unichar(c
, &dsize2
))) {
413 if (!(c1
= case_fold(c1
))) {
418 if (!(c2
= case_fold(c2
))) {
This page took 0.043713 seconds and 5 git commands to generate.