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
;
242 int hfsplus_asc2uni(struct super_block
*sb
, struct hfsplus_unistr
*ustr
, const char *astr
, int len
)
244 struct nls_table
*nls
= HFSPLUS_SB(sb
).nls
;
245 int size
, off
, decompose
;
249 decompose
= !(HFSPLUS_SB(sb
).flags
& HFSPLUS_SB_NODECOMPOSE
);
251 while (outlen
< HFSPLUS_MAX_STRLEN
&& len
> 0) {
252 size
= nls
->char2uni(astr
, len
, &c
);
267 if (c
>= 0xc0 && decompose
) {
268 off
= hfsplus_decompose_table
[(c
>> 12) & 0xf];
274 off
= hfsplus_decompose_table
[off
+ ((c
>> 8) & 0xf)];
277 off
= hfsplus_decompose_table
[off
+ ((c
>> 4) & 0xf)];
280 off
= hfsplus_decompose_table
[off
+ (c
& 0xf)];
285 if (outlen
+ size
> HFSPLUS_MAX_STRLEN
)
288 ustr
->unicode
[outlen
++] = cpu_to_be16(hfsplus_decompose_table
[off
++]);
289 } while (--size
> 0);
293 ustr
->unicode
[outlen
++] = cpu_to_be16(c
);
295 ustr
->length
= cpu_to_be16(outlen
);
297 return -ENAMETOOLONG
;