unicode.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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,
  104. const struct hfsplus_unistr *ustr,
  105. char *astr, int *len_p)
  106. {
  107. const hfsplus_unichr *ip;
  108. struct nls_table *nls = HFSPLUS_SB(sb)->nls;
  109. u8 *op;
  110. u16 cc, c0, c1;
  111. u16 *ce1, *ce2;
  112. int i, len, ustrlen, res, compose;
  113. op = astr;
  114. ip = ustr->unicode;
  115. ustrlen = be16_to_cpu(ustr->length);
  116. len = *len_p;
  117. ce1 = NULL;
  118. compose = !test_bit(HFSPLUS_SB_NODECOMPOSE, &HFSPLUS_SB(sb)->flags);
  119. while (ustrlen > 0) {
  120. c0 = be16_to_cpu(*ip++);
  121. ustrlen--;
  122. /* search for single decomposed char */
  123. if (likely(compose))
  124. ce1 = hfsplus_compose_lookup(hfsplus_compose_table, c0);
  125. if (ce1 && (cc = ce1[0])) {
  126. /* start of a possibly decomposed Hangul char */
  127. if (cc != 0xffff)
  128. goto done;
  129. if (!ustrlen)
  130. goto same;
  131. c1 = be16_to_cpu(*ip) - Hangul_VBase;
  132. if (c1 < Hangul_VCount) {
  133. /* compose the Hangul char */
  134. cc = (c0 - Hangul_LBase) * Hangul_VCount;
  135. cc = (cc + c1) * Hangul_TCount;
  136. cc += Hangul_SBase;
  137. ip++;
  138. ustrlen--;
  139. if (!ustrlen)
  140. goto done;
  141. c1 = be16_to_cpu(*ip) - Hangul_TBase;
  142. if (c1 > 0 && c1 < Hangul_TCount) {
  143. cc += c1;
  144. ip++;
  145. ustrlen--;
  146. }
  147. goto done;
  148. }
  149. }
  150. while (1) {
  151. /* main loop for common case of not composed chars */
  152. if (!ustrlen)
  153. goto same;
  154. c1 = be16_to_cpu(*ip);
  155. if (likely(compose))
  156. ce1 = hfsplus_compose_lookup(
  157. hfsplus_compose_table, c1);
  158. if (ce1)
  159. break;
  160. switch (c0) {
  161. case 0:
  162. c0 = 0x2400;
  163. break;
  164. case '/':
  165. c0 = ':';
  166. break;
  167. }
  168. res = nls->uni2char(c0, op, len);
  169. if (res < 0) {
  170. if (res == -ENAMETOOLONG)
  171. goto out;
  172. *op = '?';
  173. res = 1;
  174. }
  175. op += res;
  176. len -= res;
  177. c0 = c1;
  178. ip++;
  179. ustrlen--;
  180. }
  181. ce2 = hfsplus_compose_lookup(ce1, c0);
  182. if (ce2) {
  183. i = 1;
  184. while (i < ustrlen) {
  185. ce1 = hfsplus_compose_lookup(ce2,
  186. be16_to_cpu(ip[i]));
  187. if (!ce1)
  188. break;
  189. i++;
  190. ce2 = ce1;
  191. }
  192. if ((cc = ce2[0])) {
  193. ip += i;
  194. ustrlen -= i;
  195. goto done;
  196. }
  197. }
  198. same:
  199. switch (c0) {
  200. case 0:
  201. cc = 0x2400;
  202. break;
  203. case '/':
  204. cc = ':';
  205. break;
  206. default:
  207. cc = c0;
  208. }
  209. done:
  210. res = nls->uni2char(cc, op, len);
  211. if (res < 0) {
  212. if (res == -ENAMETOOLONG)
  213. goto out;
  214. *op = '?';
  215. res = 1;
  216. }
  217. op += res;
  218. len -= res;
  219. }
  220. res = 0;
  221. out:
  222. *len_p = (char *)op - astr;
  223. return res;
  224. }
  225. /*
  226. * Convert one or more ASCII characters into a single unicode character.
  227. * Returns the number of ASCII characters corresponding to the unicode char.
  228. */
  229. static inline int asc2unichar(struct super_block *sb, const char *astr, int len,
  230. wchar_t *uc)
  231. {
  232. int size = HFSPLUS_SB(sb)->nls->char2uni(astr, len, uc);
  233. if (size <= 0) {
  234. *uc = '?';
  235. size = 1;
  236. }
  237. switch (*uc) {
  238. case 0x2400:
  239. *uc = 0;
  240. break;
  241. case ':':
  242. *uc = '/';
  243. break;
  244. }
  245. return size;
  246. }
  247. /* Decomposes a single unicode character. */
  248. static inline u16 *decompose_unichar(wchar_t uc, int *size)
  249. {
  250. int off;
  251. off = hfsplus_decompose_table[(uc >> 12) & 0xf];
  252. if (off == 0 || off == 0xffff)
  253. return NULL;
  254. off = hfsplus_decompose_table[off + ((uc >> 8) & 0xf)];
  255. if (!off)
  256. return NULL;
  257. off = hfsplus_decompose_table[off + ((uc >> 4) & 0xf)];
  258. if (!off)
  259. return NULL;
  260. off = hfsplus_decompose_table[off + (uc & 0xf)];
  261. *size = off & 3;
  262. if (*size == 0)
  263. return NULL;
  264. return hfsplus_decompose_table + (off / 4);
  265. }
  266. int hfsplus_asc2uni(struct super_block *sb, struct hfsplus_unistr *ustr,
  267. const char *astr, int len)
  268. {
  269. int size, dsize, decompose;
  270. u16 *dstr, outlen = 0;
  271. wchar_t c;
  272. decompose = !test_bit(HFSPLUS_SB_NODECOMPOSE, &HFSPLUS_SB(sb)->flags);
  273. while (outlen < HFSPLUS_MAX_STRLEN && len > 0) {
  274. size = asc2unichar(sb, astr, len, &c);
  275. if (decompose && (dstr = decompose_unichar(c, &dsize))) {
  276. if (outlen + dsize > HFSPLUS_MAX_STRLEN)
  277. break;
  278. do {
  279. ustr->unicode[outlen++] = cpu_to_be16(*dstr++);
  280. } while (--dsize > 0);
  281. } else
  282. ustr->unicode[outlen++] = cpu_to_be16(c);
  283. astr += size;
  284. len -= size;
  285. }
  286. ustr->length = cpu_to_be16(outlen);
  287. if (len > 0)
  288. return -ENAMETOOLONG;
  289. return 0;
  290. }
  291. /*
  292. * Hash a string to an integer as appropriate for the HFS+ filesystem.
  293. * Composed unicode characters are decomposed and case-folding is performed
  294. * if the appropriate bits are (un)set on the superblock.
  295. */
  296. int hfsplus_hash_dentry(const struct dentry *dentry, const struct inode *inode,
  297. struct qstr *str)
  298. {
  299. struct super_block *sb = dentry->d_sb;
  300. const char *astr;
  301. const u16 *dstr;
  302. int casefold, decompose, size, len;
  303. unsigned long hash;
  304. wchar_t c;
  305. u16 c2;
  306. casefold = test_bit(HFSPLUS_SB_CASEFOLD, &HFSPLUS_SB(sb)->flags);
  307. decompose = !test_bit(HFSPLUS_SB_NODECOMPOSE, &HFSPLUS_SB(sb)->flags);
  308. hash = init_name_hash();
  309. astr = str->name;
  310. len = str->len;
  311. while (len > 0) {
  312. int uninitialized_var(dsize);
  313. size = asc2unichar(sb, astr, len, &c);
  314. astr += size;
  315. len -= size;
  316. if (decompose && (dstr = decompose_unichar(c, &dsize))) {
  317. do {
  318. c2 = *dstr++;
  319. if (!casefold || (c2 = case_fold(c2)))
  320. hash = partial_name_hash(c2, hash);
  321. } while (--dsize > 0);
  322. } else {
  323. c2 = c;
  324. if (!casefold || (c2 = case_fold(c2)))
  325. hash = partial_name_hash(c2, hash);
  326. }
  327. }
  328. str->hash = end_name_hash(hash);
  329. return 0;
  330. }
  331. /*
  332. * Compare strings with HFS+ filename ordering.
  333. * Composed unicode characters are decomposed and case-folding is performed
  334. * if the appropriate bits are (un)set on the superblock.
  335. */
  336. int hfsplus_compare_dentry(const struct dentry *parent,
  337. const struct inode *pinode,
  338. const struct dentry *dentry, const struct inode *inode,
  339. unsigned int len, const char *str, const struct qstr *name)
  340. {
  341. struct super_block *sb = parent->d_sb;
  342. int casefold, decompose, size;
  343. int dsize1, dsize2, len1, len2;
  344. const u16 *dstr1, *dstr2;
  345. const char *astr1, *astr2;
  346. u16 c1, c2;
  347. wchar_t c;
  348. casefold = test_bit(HFSPLUS_SB_CASEFOLD, &HFSPLUS_SB(sb)->flags);
  349. decompose = !test_bit(HFSPLUS_SB_NODECOMPOSE, &HFSPLUS_SB(sb)->flags);
  350. astr1 = str;
  351. len1 = len;
  352. astr2 = name->name;
  353. len2 = name->len;
  354. dsize1 = dsize2 = 0;
  355. dstr1 = dstr2 = NULL;
  356. while (len1 > 0 && len2 > 0) {
  357. if (!dsize1) {
  358. size = asc2unichar(sb, astr1, len1, &c);
  359. astr1 += size;
  360. len1 -= size;
  361. if (decompose)
  362. dstr1 = decompose_unichar(c, &dsize1);
  363. if (!decompose || !dstr1) {
  364. c1 = c;
  365. dstr1 = &c1;
  366. dsize1 = 1;
  367. }
  368. }
  369. if (!dsize2) {
  370. size = asc2unichar(sb, astr2, len2, &c);
  371. astr2 += size;
  372. len2 -= size;
  373. if (decompose)
  374. dstr2 = decompose_unichar(c, &dsize2);
  375. if (!decompose || !dstr2) {
  376. c2 = c;
  377. dstr2 = &c2;
  378. dsize2 = 1;
  379. }
  380. }
  381. c1 = *dstr1;
  382. c2 = *dstr2;
  383. if (casefold) {
  384. if (!(c1 = case_fold(c1))) {
  385. dstr1++;
  386. dsize1--;
  387. continue;
  388. }
  389. if (!(c2 = case_fold(c2))) {
  390. dstr2++;
  391. dsize2--;
  392. continue;
  393. }
  394. }
  395. if (c1 < c2)
  396. return -1;
  397. else if (c1 > c2)
  398. return 1;
  399. dstr1++;
  400. dsize1--;
  401. dstr2++;
  402. dsize2--;
  403. }
  404. if (len1 < len2)
  405. return -1;
  406. if (len1 > len2)
  407. return 1;
  408. return 0;
  409. }