unicode.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * linux/fs/hfsplus/unicode.c
  3. *
  4. * Copyright (C) 2001
  5. * Brad Boyer (flar@allandria.com)
  6. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  7. *
  8. * Handler routines for unicode strings
  9. */
  10. #include <linux/types.h>
  11. #include <linux/nls.h>
  12. #include "hfsplus_fs.h"
  13. #include "hfsplus_raw.h"
  14. /* Fold the case of a unicode char, given the 16 bit value */
  15. /* Returns folded char, or 0 if ignorable */
  16. static inline u16 case_fold(u16 c)
  17. {
  18. u16 tmp;
  19. tmp = hfsplus_case_fold_table[c >> 8];
  20. if (tmp)
  21. tmp = hfsplus_case_fold_table[tmp + (c & 0xff)];
  22. else
  23. tmp = c;
  24. return tmp;
  25. }
  26. /* Compare unicode strings, return values like normal strcmp */
  27. int hfsplus_strcasecmp(const struct hfsplus_unistr *s1,
  28. const struct hfsplus_unistr *s2)
  29. {
  30. u16 len1, len2, c1, c2;
  31. const hfsplus_unichr *p1, *p2;
  32. len1 = be16_to_cpu(s1->length);
  33. len2 = be16_to_cpu(s2->length);
  34. p1 = s1->unicode;
  35. p2 = s2->unicode;
  36. while (1) {
  37. c1 = c2 = 0;
  38. while (len1 && !c1) {
  39. c1 = case_fold(be16_to_cpu(*p1));
  40. p1++;
  41. len1--;
  42. }
  43. while (len2 && !c2) {
  44. c2 = case_fold(be16_to_cpu(*p2));
  45. p2++;
  46. len2--;
  47. }
  48. if (c1 != c2)
  49. return (c1 < c2) ? -1 : 1;
  50. if (!c1 && !c2)
  51. return 0;
  52. }
  53. }
  54. /* Compare names as a sequence of 16-bit unsigned integers */
  55. int hfsplus_strcmp(const struct hfsplus_unistr *s1,
  56. const struct hfsplus_unistr *s2)
  57. {
  58. u16 len1, len2, c1, c2;
  59. const hfsplus_unichr *p1, *p2;
  60. int len;
  61. len1 = be16_to_cpu(s1->length);
  62. len2 = be16_to_cpu(s2->length);
  63. p1 = s1->unicode;
  64. p2 = s2->unicode;
  65. for (len = min(len1, len2); len > 0; len--) {
  66. c1 = be16_to_cpu(*p1);
  67. c2 = be16_to_cpu(*p2);
  68. if (c1 != c2)
  69. return c1 < c2 ? -1 : 1;
  70. p1++;
  71. p2++;
  72. }
  73. return len1 < len2 ? -1 :
  74. len1 > len2 ? 1 : 0;
  75. }
  76. #define Hangul_SBase 0xac00
  77. #define Hangul_LBase 0x1100
  78. #define Hangul_VBase 0x1161
  79. #define Hangul_TBase 0x11a7
  80. #define Hangul_SCount 11172
  81. #define Hangul_LCount 19
  82. #define Hangul_VCount 21
  83. #define Hangul_TCount 28
  84. #define Hangul_NCount (Hangul_VCount * Hangul_TCount)
  85. static u16 *hfsplus_compose_lookup(u16 *p, u16 cc)
  86. {
  87. int i, s, e;
  88. s = 1;
  89. e = p[1];
  90. if (!e || cc < p[s * 2] || cc > p[e * 2])
  91. return NULL;
  92. do {
  93. i = (s + e) / 2;
  94. if (cc > p[i * 2])
  95. s = i + 1;
  96. else if (cc < p[i * 2])
  97. e = i - 1;
  98. else
  99. return hfsplus_compose_table + p[i * 2 + 1];
  100. } while (s <= e);
  101. return NULL;
  102. }
  103. int hfsplus_uni2asc(struct super_block *sb, const struct hfsplus_unistr *ustr, char *astr, int *len_p)
  104. {
  105. const hfsplus_unichr *ip;
  106. struct nls_table *nls = HFSPLUS_SB(sb).nls;
  107. u8 *op;
  108. u16 cc, c0, c1;
  109. u16 *ce1, *ce2;
  110. int i, len, ustrlen, res, compose;
  111. op = astr;
  112. ip = ustr->unicode;
  113. ustrlen = be16_to_cpu(ustr->length);
  114. len = *len_p;
  115. ce1 = NULL;
  116. compose = !(HFSPLUS_SB(sb).flags & HFSPLUS_SB_NODECOMPOSE);
  117. while (ustrlen > 0) {
  118. c0 = be16_to_cpu(*ip++);
  119. ustrlen--;
  120. /* search for single decomposed char */
  121. if (likely(compose))
  122. ce1 = hfsplus_compose_lookup(hfsplus_compose_table, c0);
  123. if (ce1 && (cc = ce1[0])) {
  124. /* start of a possibly decomposed Hangul char */
  125. if (cc != 0xffff)
  126. goto done;
  127. if (!ustrlen)
  128. goto same;
  129. c1 = be16_to_cpu(*ip) - Hangul_VBase;
  130. if (c1 < Hangul_VCount) {
  131. /* compose the Hangul char */
  132. cc = (c0 - Hangul_LBase) * Hangul_VCount;
  133. cc = (cc + c1) * Hangul_TCount;
  134. cc += Hangul_SBase;
  135. ip++;
  136. ustrlen--;
  137. if (!ustrlen)
  138. goto done;
  139. c1 = be16_to_cpu(*ip) - Hangul_TBase;
  140. if (c1 > 0 && c1 < Hangul_TCount) {
  141. cc += c1;
  142. ip++;
  143. ustrlen--;
  144. }
  145. goto done;
  146. }
  147. }
  148. while (1) {
  149. /* main loop for common case of not composed chars */
  150. if (!ustrlen)
  151. goto same;
  152. c1 = be16_to_cpu(*ip);
  153. if (likely(compose))
  154. ce1 = hfsplus_compose_lookup(hfsplus_compose_table, c1);
  155. if (ce1)
  156. break;
  157. switch (c0) {
  158. case 0:
  159. c0 = 0x2400;
  160. break;
  161. case '/':
  162. c0 = ':';
  163. break;
  164. }
  165. res = nls->uni2char(c0, op, len);
  166. if (res < 0) {
  167. if (res == -ENAMETOOLONG)
  168. goto out;
  169. *op = '?';
  170. res = 1;
  171. }
  172. op += res;
  173. len -= res;
  174. c0 = c1;
  175. ip++;
  176. ustrlen--;
  177. }
  178. ce2 = hfsplus_compose_lookup(ce1, c0);
  179. if (ce2) {
  180. i = 1;
  181. while (i < ustrlen) {
  182. ce1 = hfsplus_compose_lookup(ce2, be16_to_cpu(ip[i]));
  183. if (!ce1)
  184. break;
  185. i++;
  186. ce2 = ce1;
  187. }
  188. if ((cc = ce2[0])) {
  189. ip += i;
  190. ustrlen -= i;
  191. goto done;
  192. }
  193. }
  194. same:
  195. switch (c0) {
  196. case 0:
  197. cc = 0x2400;
  198. break;
  199. case '/':
  200. cc = ':';
  201. break;
  202. default:
  203. cc = c0;
  204. }
  205. done:
  206. res = nls->uni2char(cc, op, len);
  207. if (res < 0) {
  208. if (res == -ENAMETOOLONG)
  209. goto out;
  210. *op = '?';
  211. res = 1;
  212. }
  213. op += res;
  214. len -= res;
  215. }
  216. res = 0;
  217. out:
  218. *len_p = (char *)op - astr;
  219. return res;
  220. }
  221. int hfsplus_asc2uni(struct super_block *sb, struct hfsplus_unistr *ustr, const char *astr, int len)
  222. {
  223. struct nls_table *nls = HFSPLUS_SB(sb).nls;
  224. int size, off, decompose;
  225. wchar_t c;
  226. u16 outlen = 0;
  227. decompose = !(HFSPLUS_SB(sb).flags & HFSPLUS_SB_NODECOMPOSE);
  228. while (outlen < HFSPLUS_MAX_STRLEN && len > 0) {
  229. size = nls->char2uni(astr, len, &c);
  230. if (size <= 0) {
  231. c = '?';
  232. size = 1;
  233. }
  234. astr += size;
  235. len -= size;
  236. switch (c) {
  237. case 0x2400:
  238. c = 0;
  239. break;
  240. case ':':
  241. c = '/';
  242. break;
  243. }
  244. if (c >= 0xc0 && decompose) {
  245. off = hfsplus_decompose_table[(c >> 12) & 0xf];
  246. if (!off)
  247. goto done;
  248. if (off == 0xffff) {
  249. goto done;
  250. }
  251. off = hfsplus_decompose_table[off + ((c >> 8) & 0xf)];
  252. if (!off)
  253. goto done;
  254. off = hfsplus_decompose_table[off + ((c >> 4) & 0xf)];
  255. if (!off)
  256. goto done;
  257. off = hfsplus_decompose_table[off + (c & 0xf)];
  258. size = off & 3;
  259. if (!size)
  260. goto done;
  261. off /= 4;
  262. if (outlen + size > HFSPLUS_MAX_STRLEN)
  263. break;
  264. do {
  265. ustr->unicode[outlen++] = cpu_to_be16(hfsplus_decompose_table[off++]);
  266. } while (--size > 0);
  267. continue;
  268. }
  269. done:
  270. ustr->unicode[outlen++] = cpu_to_be16(c);
  271. }
  272. ustr->length = cpu_to_be16(outlen);
  273. if (len > 0)
  274. return -ENAMETOOLONG;
  275. return 0;
  276. }