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
= case_fold_table
[(c
>>8)];
24 tmp
= case_fold_table
[tmp
+ (c
& 0xFF)];
30 /* Compare unicode strings, return values like normal strcmp */
31 int hfsplus_unistrcmp(const struct hfsplus_unistr
*s1
, const struct hfsplus_unistr
*s2
)
33 u16 len1
, len2
, c1
, c2
;
34 const hfsplus_unichr
*p1
, *p2
;
36 len1
= be16_to_cpu(s1
->length
);
37 len2
= be16_to_cpu(s2
->length
);
45 c1
= case_fold(be16_to_cpu(*p1
));
50 c2
= case_fold(be16_to_cpu(*p2
));
56 return (c1
< c2
) ? -1 : 1;
62 int hfsplus_uni2asc(const struct hfsplus_unistr
*ustr
, char *astr
, int *len
)
64 const hfsplus_unichr
*ip
;
71 ustrlen
= be16_to_cpu(ustr
->length
);
73 while (ustrlen
> 0 && tmp
> 0) {
74 cc
= be16_to_cpu(*ip
);
84 size
= utf8_wctomb(op
, cc
, tmp
);
98 *len
= (char *)op
- astr
;
100 return -ENAMETOOLONG
;
104 int hfsplus_asc2uni(struct hfsplus_unistr
*ustr
, const char *astr
, int len
)
110 while (outlen
<= HFSPLUS_MAX_STRLEN
&& len
> 0) {
112 tmp
= utf8_mbtowc(&c
, astr
, len
);
133 ustr
->unicode
[outlen
] = cpu_to_be16(c
);
136 ustr
->length
= cpu_to_be16(outlen
);
138 return -ENAMETOOLONG
;