unicode.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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_unistrcmp(const struct hfsplus_unistr *s1, const struct hfsplus_unistr *s2)
  28. {
  29. u16 len1, len2, c1, c2;
  30. const hfsplus_unichr *p1, *p2;
  31. len1 = be16_to_cpu(s1->length);
  32. len2 = be16_to_cpu(s2->length);
  33. p1 = s1->unicode;
  34. p2 = s2->unicode;
  35. while (1) {
  36. c1 = c2 = 0;
  37. while (len1 && !c1) {
  38. c1 = case_fold(be16_to_cpu(*p1));
  39. p1++;
  40. len1--;
  41. }
  42. while (len2 && !c2) {
  43. c2 = case_fold(be16_to_cpu(*p2));
  44. p2++;
  45. len2--;
  46. }
  47. if (c1 != c2)
  48. return (c1 < c2) ? -1 : 1;
  49. if (!c1 && !c2)
  50. return 0;
  51. }
  52. }
  53. #define Hangul_SBase 0xac00
  54. #define Hangul_LBase 0x1100
  55. #define Hangul_VBase 0x1161
  56. #define Hangul_TBase 0x11a7
  57. #define Hangul_SCount 11172
  58. #define Hangul_LCount 19
  59. #define Hangul_VCount 21
  60. #define Hangul_TCount 28
  61. #define Hangul_NCount (Hangul_VCount * Hangul_TCount)
  62. static u16 *hfsplus_compose_lookup(u16 *p, u16 cc)
  63. {
  64. int i, s, e;
  65. s = 1;
  66. e = p[1];
  67. if (!e || cc < p[s * 2] || cc > p[e * 2])
  68. return NULL;
  69. do {
  70. i = (s + e) / 2;
  71. if (cc > p[i * 2])
  72. s = i + 1;
  73. else if (cc < p[i * 2])
  74. e = i - 1;
  75. else
  76. return hfsplus_compose_table + p[i * 2 + 1];
  77. } while (s <= e);
  78. return NULL;
  79. }
  80. int hfsplus_uni2asc(struct super_block *sb, const struct hfsplus_unistr *ustr, char *astr, int *len_p)
  81. {
  82. const hfsplus_unichr *ip;
  83. struct nls_table *nls = HFSPLUS_SB(sb).nls;
  84. u8 *op;
  85. u16 cc, c0, c1;
  86. u16 *ce1, *ce2;
  87. int i, len, ustrlen, res, compose;
  88. op = astr;
  89. ip = ustr->unicode;
  90. ustrlen = be16_to_cpu(ustr->length);
  91. len = *len_p;
  92. ce1 = NULL;
  93. compose = !(HFSPLUS_SB(sb).flags & HFSPLUS_SB_NODECOMPOSE);
  94. while (ustrlen > 0) {
  95. c0 = be16_to_cpu(*ip++);
  96. ustrlen--;
  97. /* search for single decomposed char */
  98. if (likely(compose))
  99. ce1 = hfsplus_compose_lookup(hfsplus_compose_table, c0);
  100. if (ce1 && (cc = ce1[0])) {
  101. /* start of a possibly decomposed Hangul char */
  102. if (cc != 0xffff)
  103. goto done;
  104. if (!ustrlen)
  105. goto same;
  106. c1 = be16_to_cpu(*ip) - Hangul_VBase;
  107. if (c1 < Hangul_VCount) {
  108. /* compose the Hangul char */
  109. cc = (c0 - Hangul_LBase) * Hangul_VCount;
  110. cc = (cc + c1) * Hangul_TCount;
  111. cc += Hangul_SBase;
  112. ip++;
  113. ustrlen--;
  114. if (!ustrlen)
  115. goto done;
  116. c1 = be16_to_cpu(*ip) - Hangul_TBase;
  117. if (c1 > 0 && c1 < Hangul_TCount) {
  118. cc += c1;
  119. ip++;
  120. ustrlen--;
  121. }
  122. goto done;
  123. }
  124. }
  125. while (1) {
  126. /* main loop for common case of not composed chars */
  127. if (!ustrlen)
  128. goto same;
  129. c1 = be16_to_cpu(*ip);
  130. if (likely(compose))
  131. ce1 = hfsplus_compose_lookup(hfsplus_compose_table, c1);
  132. if (ce1)
  133. break;
  134. switch (c0) {
  135. case 0:
  136. c0 = 0x2400;
  137. break;
  138. case '/':
  139. c0 = ':';
  140. break;
  141. }
  142. res = nls->uni2char(c0, op, len);
  143. if (res < 0) {
  144. if (res == -ENAMETOOLONG)
  145. goto out;
  146. *op = '?';
  147. res = 1;
  148. }
  149. op += res;
  150. len -= res;
  151. c0 = c1;
  152. ip++;
  153. ustrlen--;
  154. }
  155. ce2 = hfsplus_compose_lookup(ce1, c0);
  156. if (ce2) {
  157. i = 1;
  158. while (i < ustrlen) {
  159. ce1 = hfsplus_compose_lookup(ce2, be16_to_cpu(ip[i]));
  160. if (!ce1)
  161. break;
  162. i++;
  163. ce2 = ce1;
  164. }
  165. if ((cc = ce2[0])) {
  166. ip += i;
  167. ustrlen -= i;
  168. goto done;
  169. }
  170. }
  171. same:
  172. switch (c0) {
  173. case 0:
  174. cc = 0x2400;
  175. break;
  176. case '/':
  177. cc = ':';
  178. break;
  179. default:
  180. cc = c0;
  181. }
  182. done:
  183. res = nls->uni2char(cc, op, len);
  184. if (res < 0) {
  185. if (res == -ENAMETOOLONG)
  186. goto out;
  187. *op = '?';
  188. res = 1;
  189. }
  190. op += res;
  191. len -= res;
  192. }
  193. res = 0;
  194. out:
  195. *len_p = (char *)op - astr;
  196. return res;
  197. }
  198. int hfsplus_asc2uni(struct super_block *sb, struct hfsplus_unistr *ustr, const char *astr, int len)
  199. {
  200. struct nls_table *nls = HFSPLUS_SB(sb).nls;
  201. int size, off, decompose;
  202. wchar_t c;
  203. u16 outlen = 0;
  204. decompose = !(HFSPLUS_SB(sb).flags & HFSPLUS_SB_NODECOMPOSE);
  205. while (outlen < HFSPLUS_MAX_STRLEN && len > 0) {
  206. size = nls->char2uni(astr, len, &c);
  207. if (size <= 0) {
  208. c = '?';
  209. size = 1;
  210. }
  211. astr += size;
  212. len -= size;
  213. switch (c) {
  214. case 0x2400:
  215. c = 0;
  216. break;
  217. case ':':
  218. c = '/';
  219. break;
  220. }
  221. if (c >= 0xc0 && decompose) {
  222. off = hfsplus_decompose_table[(c >> 12) & 0xf];
  223. if (!off)
  224. goto done;
  225. if (off == 0xffff) {
  226. goto done;
  227. }
  228. off = hfsplus_decompose_table[off + ((c >> 8) & 0xf)];
  229. if (!off)
  230. goto done;
  231. off = hfsplus_decompose_table[off + ((c >> 4) & 0xf)];
  232. if (!off)
  233. goto done;
  234. off = hfsplus_decompose_table[off + (c & 0xf)];
  235. size = off & 3;
  236. if (!size)
  237. goto done;
  238. off /= 4;
  239. if (outlen + size > HFSPLUS_MAX_STRLEN)
  240. break;
  241. do {
  242. ustr->unicode[outlen++] = cpu_to_be16(hfsplus_decompose_table[off++]);
  243. } while (--size > 0);
  244. continue;
  245. }
  246. done:
  247. ustr->unicode[outlen++] = cpu_to_be16(c);
  248. }
  249. ustr->length = cpu_to_be16(outlen);
  250. if (len > 0)
  251. return -ENAMETOOLONG;
  252. return 0;
  253. }