unicode.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /*
  222. * Convert one or more ASCII characters into a single unicode character.
  223. * Returns the number of ASCII characters corresponding to the unicode char.
  224. */
  225. static inline int asc2unichar(struct super_block *sb, const char *astr, int len,
  226. wchar_t *uc)
  227. {
  228. int size = HFSPLUS_SB(sb).nls->char2uni(astr, len, uc);
  229. if (size <= 0) {
  230. *uc = '?';
  231. size = 1;
  232. }
  233. switch (*uc) {
  234. case 0x2400:
  235. *uc = 0;
  236. break;
  237. case ':':
  238. *uc = '/';
  239. break;
  240. }
  241. return size;
  242. }
  243. /* Decomposes a single unicode character. */
  244. static inline u16 *decompose_unichar(wchar_t uc, int *size)
  245. {
  246. int off;
  247. off = hfsplus_decompose_table[(uc >> 12) & 0xf];
  248. if (off == 0 || off == 0xffff)
  249. return NULL;
  250. off = hfsplus_decompose_table[off + ((uc >> 8) & 0xf)];
  251. if (!off)
  252. return NULL;
  253. off = hfsplus_decompose_table[off + ((uc >> 4) & 0xf)];
  254. if (!off)
  255. return NULL;
  256. off = hfsplus_decompose_table[off + (uc & 0xf)];
  257. *size = off & 3;
  258. if (*size == 0)
  259. return NULL;
  260. return hfsplus_decompose_table + (off / 4);
  261. }
  262. int hfsplus_asc2uni(struct super_block *sb, struct hfsplus_unistr *ustr,
  263. const char *astr, int len)
  264. {
  265. int size, dsize, decompose;
  266. u16 *dstr, outlen = 0;
  267. wchar_t c;
  268. decompose = !(HFSPLUS_SB(sb).flags & HFSPLUS_SB_NODECOMPOSE);
  269. while (outlen < HFSPLUS_MAX_STRLEN && len > 0) {
  270. size = asc2unichar(sb, astr, len, &c);
  271. if (decompose && (dstr = decompose_unichar(c, &dsize))) {
  272. if (outlen + dsize > HFSPLUS_MAX_STRLEN)
  273. break;
  274. do {
  275. ustr->unicode[outlen++] = cpu_to_be16(*dstr++);
  276. } while (--dsize > 0);
  277. } else
  278. ustr->unicode[outlen++] = cpu_to_be16(c);
  279. astr += size;
  280. len -= size;
  281. }
  282. ustr->length = cpu_to_be16(outlen);
  283. if (len > 0)
  284. return -ENAMETOOLONG;
  285. return 0;
  286. }
  287. /*
  288. * Hash a string to an integer as appropriate for the HFS+ filesystem.
  289. * Composed unicode characters are decomposed and case-folding is performed
  290. * if the appropriate bits are (un)set on the superblock.
  291. */
  292. int hfsplus_hash_dentry(struct dentry *dentry, struct qstr *str)
  293. {
  294. struct super_block *sb = dentry->d_sb;
  295. const char *astr;
  296. const u16 *dstr;
  297. int casefold, decompose, size, len;
  298. unsigned long hash;
  299. wchar_t c;
  300. u16 c2;
  301. casefold = (HFSPLUS_SB(sb).flags & HFSPLUS_SB_CASEFOLD);
  302. decompose = !(HFSPLUS_SB(sb).flags & HFSPLUS_SB_NODECOMPOSE);
  303. hash = init_name_hash();
  304. astr = str->name;
  305. len = str->len;
  306. while (len > 0) {
  307. int uninitialized_var(dsize);
  308. size = asc2unichar(sb, astr, len, &c);
  309. astr += size;
  310. len -= size;
  311. if (decompose && (dstr = decompose_unichar(c, &dsize))) {
  312. do {
  313. c2 = *dstr++;
  314. if (!casefold || (c2 = case_fold(c2)))
  315. hash = partial_name_hash(c2, hash);
  316. } while (--dsize > 0);
  317. } else {
  318. c2 = c;
  319. if (!casefold || (c2 = case_fold(c2)))
  320. hash = partial_name_hash(c2, hash);
  321. }
  322. }
  323. str->hash = end_name_hash(hash);
  324. return 0;
  325. }
  326. /*
  327. * Compare strings with HFS+ filename ordering.
  328. * Composed unicode characters are decomposed and case-folding is performed
  329. * if the appropriate bits are (un)set on the superblock.
  330. */
  331. int hfsplus_compare_dentry(struct dentry *dentry, struct qstr *s1, struct qstr *s2)
  332. {
  333. struct super_block *sb = dentry->d_sb;
  334. int casefold, decompose, size;
  335. int dsize1, dsize2, len1, len2;
  336. const u16 *dstr1, *dstr2;
  337. const char *astr1, *astr2;
  338. u16 c1, c2;
  339. wchar_t c;
  340. casefold = (HFSPLUS_SB(sb).flags & HFSPLUS_SB_CASEFOLD);
  341. decompose = !(HFSPLUS_SB(sb).flags & HFSPLUS_SB_NODECOMPOSE);
  342. astr1 = s1->name;
  343. len1 = s1->len;
  344. astr2 = s2->name;
  345. len2 = s2->len;
  346. dsize1 = dsize2 = 0;
  347. dstr1 = dstr2 = NULL;
  348. while (len1 > 0 && len2 > 0) {
  349. if (!dsize1) {
  350. size = asc2unichar(sb, astr1, len1, &c);
  351. astr1 += size;
  352. len1 -= size;
  353. if (!decompose || !(dstr1 = decompose_unichar(c, &dsize1))) {
  354. c1 = c;
  355. dstr1 = &c1;
  356. dsize1 = 1;
  357. }
  358. }
  359. if (!dsize2) {
  360. size = asc2unichar(sb, astr2, len2, &c);
  361. astr2 += size;
  362. len2 -= size;
  363. if (!decompose || !(dstr2 = decompose_unichar(c, &dsize2))) {
  364. c2 = c;
  365. dstr2 = &c2;
  366. dsize2 = 1;
  367. }
  368. }
  369. c1 = *dstr1;
  370. c2 = *dstr2;
  371. if (casefold) {
  372. if (!(c1 = case_fold(c1))) {
  373. dstr1++;
  374. dsize1--;
  375. continue;
  376. }
  377. if (!(c2 = case_fold(c2))) {
  378. dstr2++;
  379. dsize2--;
  380. continue;
  381. }
  382. }
  383. if (c1 < c2)
  384. return -1;
  385. else if (c1 > c2)
  386. return 1;
  387. dstr1++;
  388. dsize1--;
  389. dstr2++;
  390. dsize2--;
  391. }
  392. if (len1 < len2)
  393. return -1;
  394. if (len1 > len2)
  395. return 1;
  396. return 0;
  397. }