unicode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * unicode.c
  3. *
  4. * PURPOSE
  5. * Routines for converting between UTF-8 and OSTA Compressed Unicode.
  6. * Also handles filename mangling
  7. *
  8. * DESCRIPTION
  9. * OSTA Compressed Unicode is explained in the OSTA UDF specification.
  10. * http://www.osta.org/
  11. * UTF-8 is explained in the IETF RFC XXXX.
  12. * ftp://ftp.internic.net/rfc/rfcxxxx.txt
  13. *
  14. * COPYRIGHT
  15. * This file is distributed under the terms of the GNU General Public
  16. * License (GPL). Copies of the GPL can be obtained from:
  17. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  18. * Each contributing author retains all rights to their own work.
  19. */
  20. #include "udfdecl.h"
  21. #include <linux/kernel.h>
  22. #include <linux/string.h> /* for memset */
  23. #include <linux/nls.h>
  24. #include <linux/crc-itu-t.h>
  25. #include <linux/slab.h>
  26. #include "udf_sb.h"
  27. static int udf_translate_to_linux(uint8_t *, int, uint8_t *, int, uint8_t *,
  28. int);
  29. static int udf_char_to_ustr(struct ustr *dest, const uint8_t *src, int strlen)
  30. {
  31. if ((!dest) || (!src) || (!strlen) || (strlen > UDF_NAME_LEN - 2))
  32. return 0;
  33. memset(dest, 0, sizeof(struct ustr));
  34. memcpy(dest->u_name, src, strlen);
  35. dest->u_cmpID = 0x08;
  36. dest->u_len = strlen;
  37. return strlen;
  38. }
  39. /*
  40. * udf_build_ustr
  41. */
  42. int udf_build_ustr(struct ustr *dest, dstring *ptr, int size)
  43. {
  44. int usesize;
  45. if (!dest || !ptr || !size)
  46. return -1;
  47. BUG_ON(size < 2);
  48. usesize = min_t(size_t, ptr[size - 1], sizeof(dest->u_name));
  49. usesize = min(usesize, size - 2);
  50. dest->u_cmpID = ptr[0];
  51. dest->u_len = usesize;
  52. memcpy(dest->u_name, ptr + 1, usesize);
  53. memset(dest->u_name + usesize, 0, sizeof(dest->u_name) - usesize);
  54. return 0;
  55. }
  56. /*
  57. * udf_build_ustr_exact
  58. */
  59. static void udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
  60. {
  61. memset(dest, 0, sizeof(struct ustr));
  62. dest->u_cmpID = ptr[0];
  63. dest->u_len = exactsize - 1;
  64. memcpy(dest->u_name, ptr + 1, exactsize - 1);
  65. }
  66. /*
  67. * udf_CS0toUTF8
  68. *
  69. * PURPOSE
  70. * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
  71. *
  72. * PRE-CONDITIONS
  73. * utf Pointer to UTF-8 output buffer.
  74. * ocu Pointer to OSTA Compressed Unicode input buffer
  75. * of size UDF_NAME_LEN bytes.
  76. * both of type "struct ustr *"
  77. *
  78. * POST-CONDITIONS
  79. * <return> >= 0 on success.
  80. *
  81. * HISTORY
  82. * November 12, 1997 - Andrew E. Mileski
  83. * Written, tested, and released.
  84. */
  85. int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
  86. {
  87. const uint8_t *ocu;
  88. uint8_t cmp_id, ocu_len;
  89. int i;
  90. ocu_len = ocu_i->u_len;
  91. if (ocu_len == 0) {
  92. memset(utf_o, 0, sizeof(struct ustr));
  93. return 0;
  94. }
  95. cmp_id = ocu_i->u_cmpID;
  96. if (cmp_id != 8 && cmp_id != 16) {
  97. memset(utf_o, 0, sizeof(struct ustr));
  98. pr_err("unknown compression code (%d) stri=%s\n",
  99. cmp_id, ocu_i->u_name);
  100. return -EINVAL;
  101. }
  102. ocu = ocu_i->u_name;
  103. utf_o->u_len = 0;
  104. for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
  105. /* Expand OSTA compressed Unicode to Unicode */
  106. uint32_t c = ocu[i++];
  107. if (cmp_id == 16)
  108. c = (c << 8) | ocu[i++];
  109. /* Compress Unicode to UTF-8 */
  110. if (c < 0x80U)
  111. utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
  112. else if (c < 0x800U) {
  113. if (utf_o->u_len > (UDF_NAME_LEN - 4))
  114. break;
  115. utf_o->u_name[utf_o->u_len++] =
  116. (uint8_t)(0xc0 | (c >> 6));
  117. utf_o->u_name[utf_o->u_len++] =
  118. (uint8_t)(0x80 | (c & 0x3f));
  119. } else {
  120. if (utf_o->u_len > (UDF_NAME_LEN - 5))
  121. break;
  122. utf_o->u_name[utf_o->u_len++] =
  123. (uint8_t)(0xe0 | (c >> 12));
  124. utf_o->u_name[utf_o->u_len++] =
  125. (uint8_t)(0x80 |
  126. ((c >> 6) & 0x3f));
  127. utf_o->u_name[utf_o->u_len++] =
  128. (uint8_t)(0x80 | (c & 0x3f));
  129. }
  130. }
  131. utf_o->u_cmpID = 8;
  132. return utf_o->u_len;
  133. }
  134. /*
  135. *
  136. * udf_UTF8toCS0
  137. *
  138. * PURPOSE
  139. * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
  140. *
  141. * DESCRIPTION
  142. * This routine is only called by udf_lookup().
  143. *
  144. * PRE-CONDITIONS
  145. * ocu Pointer to OSTA Compressed Unicode output
  146. * buffer of size UDF_NAME_LEN bytes.
  147. * utf Pointer to UTF-8 input buffer.
  148. * utf_len Length of UTF-8 input buffer in bytes.
  149. *
  150. * POST-CONDITIONS
  151. * <return> Zero on success.
  152. *
  153. * HISTORY
  154. * November 12, 1997 - Andrew E. Mileski
  155. * Written, tested, and released.
  156. */
  157. static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
  158. {
  159. unsigned c, i, max_val, utf_char;
  160. int utf_cnt, u_len, u_ch;
  161. memset(ocu, 0, sizeof(dstring) * length);
  162. ocu[0] = 8;
  163. max_val = 0xffU;
  164. u_ch = 1;
  165. try_again:
  166. u_len = 0U;
  167. utf_char = 0U;
  168. utf_cnt = 0U;
  169. for (i = 0U; i < utf->u_len; i++) {
  170. /* Name didn't fit? */
  171. if (u_len + 1 + u_ch >= length)
  172. return 0;
  173. c = (uint8_t)utf->u_name[i];
  174. /* Complete a multi-byte UTF-8 character */
  175. if (utf_cnt) {
  176. utf_char = (utf_char << 6) | (c & 0x3fU);
  177. if (--utf_cnt)
  178. continue;
  179. } else {
  180. /* Check for a multi-byte UTF-8 character */
  181. if (c & 0x80U) {
  182. /* Start a multi-byte UTF-8 character */
  183. if ((c & 0xe0U) == 0xc0U) {
  184. utf_char = c & 0x1fU;
  185. utf_cnt = 1;
  186. } else if ((c & 0xf0U) == 0xe0U) {
  187. utf_char = c & 0x0fU;
  188. utf_cnt = 2;
  189. } else if ((c & 0xf8U) == 0xf0U) {
  190. utf_char = c & 0x07U;
  191. utf_cnt = 3;
  192. } else if ((c & 0xfcU) == 0xf8U) {
  193. utf_char = c & 0x03U;
  194. utf_cnt = 4;
  195. } else if ((c & 0xfeU) == 0xfcU) {
  196. utf_char = c & 0x01U;
  197. utf_cnt = 5;
  198. } else {
  199. goto error_out;
  200. }
  201. continue;
  202. } else {
  203. /* Single byte UTF-8 character (most common) */
  204. utf_char = c;
  205. }
  206. }
  207. /* Choose no compression if necessary */
  208. if (utf_char > max_val) {
  209. if (max_val == 0xffU) {
  210. max_val = 0xffffU;
  211. ocu[0] = (uint8_t)0x10U;
  212. u_ch = 2;
  213. goto try_again;
  214. }
  215. goto error_out;
  216. }
  217. if (max_val == 0xffffU)
  218. ocu[++u_len] = (uint8_t)(utf_char >> 8);
  219. ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
  220. }
  221. if (utf_cnt) {
  222. error_out:
  223. ocu[++u_len] = '?';
  224. printk(KERN_DEBUG pr_fmt("bad UTF-8 character\n"));
  225. }
  226. ocu[length - 1] = (uint8_t)u_len + 1;
  227. return u_len + 1;
  228. }
  229. static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
  230. const struct ustr *ocu_i)
  231. {
  232. const uint8_t *ocu;
  233. uint8_t cmp_id, ocu_len;
  234. int i, len;
  235. ocu_len = ocu_i->u_len;
  236. if (ocu_len == 0) {
  237. memset(utf_o, 0, sizeof(struct ustr));
  238. return 0;
  239. }
  240. cmp_id = ocu_i->u_cmpID;
  241. if (cmp_id != 8 && cmp_id != 16) {
  242. memset(utf_o, 0, sizeof(struct ustr));
  243. pr_err("unknown compression code (%d) stri=%s\n",
  244. cmp_id, ocu_i->u_name);
  245. return -EINVAL;
  246. }
  247. ocu = ocu_i->u_name;
  248. utf_o->u_len = 0;
  249. for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
  250. /* Expand OSTA compressed Unicode to Unicode */
  251. uint32_t c = ocu[i++];
  252. if (cmp_id == 16)
  253. c = (c << 8) | ocu[i++];
  254. len = nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
  255. UDF_NAME_LEN - 2 - utf_o->u_len);
  256. /* Valid character? */
  257. if (len >= 0)
  258. utf_o->u_len += len;
  259. else
  260. utf_o->u_name[utf_o->u_len++] = '?';
  261. }
  262. utf_o->u_cmpID = 8;
  263. return utf_o->u_len;
  264. }
  265. static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
  266. int length)
  267. {
  268. int len;
  269. unsigned i, max_val;
  270. uint16_t uni_char;
  271. int u_len, u_ch;
  272. memset(ocu, 0, sizeof(dstring) * length);
  273. ocu[0] = 8;
  274. max_val = 0xffU;
  275. u_ch = 1;
  276. try_again:
  277. u_len = 0U;
  278. for (i = 0U; i < uni->u_len; i++) {
  279. /* Name didn't fit? */
  280. if (u_len + 1 + u_ch >= length)
  281. return 0;
  282. len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
  283. if (!len)
  284. continue;
  285. /* Invalid character, deal with it */
  286. if (len < 0) {
  287. len = 1;
  288. uni_char = '?';
  289. }
  290. if (uni_char > max_val) {
  291. max_val = 0xffffU;
  292. ocu[0] = (uint8_t)0x10U;
  293. u_ch = 2;
  294. goto try_again;
  295. }
  296. if (max_val == 0xffffU)
  297. ocu[++u_len] = (uint8_t)(uni_char >> 8);
  298. ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
  299. i += len - 1;
  300. }
  301. ocu[length - 1] = (uint8_t)u_len + 1;
  302. return u_len + 1;
  303. }
  304. int udf_get_filename(struct super_block *sb, uint8_t *sname, int slen,
  305. uint8_t *dname, int dlen)
  306. {
  307. struct ustr *filename, *unifilename;
  308. int ret;
  309. if (!slen)
  310. return -EIO;
  311. filename = kmalloc(sizeof(struct ustr), GFP_NOFS);
  312. if (!filename)
  313. return -ENOMEM;
  314. unifilename = kmalloc(sizeof(struct ustr), GFP_NOFS);
  315. if (!unifilename) {
  316. ret = -ENOMEM;
  317. goto out1;
  318. }
  319. udf_build_ustr_exact(unifilename, sname, slen);
  320. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
  321. ret = udf_CS0toUTF8(filename, unifilename);
  322. if (ret < 0) {
  323. udf_debug("Failed in udf_get_filename: sname = %s\n",
  324. sname);
  325. goto out2;
  326. }
  327. } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
  328. ret = udf_CS0toNLS(UDF_SB(sb)->s_nls_map, filename,
  329. unifilename);
  330. if (ret < 0) {
  331. udf_debug("Failed in udf_get_filename: sname = %s\n",
  332. sname);
  333. goto out2;
  334. }
  335. } else
  336. BUG();
  337. ret = udf_translate_to_linux(dname, dlen,
  338. filename->u_name, filename->u_len,
  339. unifilename->u_name, unifilename->u_len);
  340. /* Zero length filename isn't valid... */
  341. if (ret == 0)
  342. ret = -EINVAL;
  343. out2:
  344. kfree(unifilename);
  345. out1:
  346. kfree(filename);
  347. return ret;
  348. }
  349. int udf_put_filename(struct super_block *sb, const uint8_t *sname,
  350. uint8_t *dname, int flen)
  351. {
  352. struct ustr unifilename;
  353. int namelen;
  354. if (!udf_char_to_ustr(&unifilename, sname, flen))
  355. return 0;
  356. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
  357. namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
  358. if (!namelen)
  359. return 0;
  360. } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
  361. namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
  362. &unifilename, UDF_NAME_LEN);
  363. if (!namelen)
  364. return 0;
  365. } else
  366. return 0;
  367. return namelen;
  368. }
  369. #define ILLEGAL_CHAR_MARK '_'
  370. #define EXT_MARK '.'
  371. #define CRC_MARK '#'
  372. #define EXT_SIZE 5
  373. /* Number of chars we need to store generated CRC to make filename unique */
  374. #define CRC_LEN 5
  375. static int udf_translate_to_linux(uint8_t *newName, int newLen,
  376. uint8_t *udfName, int udfLen,
  377. uint8_t *fidName, int fidNameLen)
  378. {
  379. int index, newIndex = 0, needsCRC = 0;
  380. int extIndex = 0, newExtIndex = 0, hasExt = 0;
  381. unsigned short valueCRC;
  382. uint8_t curr;
  383. if (udfName[0] == '.' &&
  384. (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
  385. needsCRC = 1;
  386. newIndex = udfLen;
  387. memcpy(newName, udfName, udfLen);
  388. } else {
  389. for (index = 0; index < udfLen; index++) {
  390. curr = udfName[index];
  391. if (curr == '/' || curr == 0) {
  392. needsCRC = 1;
  393. curr = ILLEGAL_CHAR_MARK;
  394. while (index + 1 < udfLen &&
  395. (udfName[index + 1] == '/' ||
  396. udfName[index + 1] == 0))
  397. index++;
  398. }
  399. if (curr == EXT_MARK &&
  400. (udfLen - index - 1) <= EXT_SIZE) {
  401. if (udfLen == index + 1)
  402. hasExt = 0;
  403. else {
  404. hasExt = 1;
  405. extIndex = index;
  406. newExtIndex = newIndex;
  407. }
  408. }
  409. if (newIndex < newLen)
  410. newName[newIndex++] = curr;
  411. else
  412. needsCRC = 1;
  413. }
  414. }
  415. if (needsCRC) {
  416. uint8_t ext[EXT_SIZE];
  417. int localExtIndex = 0;
  418. if (hasExt) {
  419. int maxFilenameLen;
  420. for (index = 0;
  421. index < EXT_SIZE && extIndex + index + 1 < udfLen;
  422. index++) {
  423. curr = udfName[extIndex + index + 1];
  424. if (curr == '/' || curr == 0) {
  425. needsCRC = 1;
  426. curr = ILLEGAL_CHAR_MARK;
  427. while (extIndex + index + 2 < udfLen &&
  428. (index + 1 < EXT_SIZE &&
  429. (udfName[extIndex + index + 2] == '/' ||
  430. udfName[extIndex + index + 2] == 0)))
  431. index++;
  432. }
  433. ext[localExtIndex++] = curr;
  434. }
  435. maxFilenameLen = newLen - CRC_LEN - localExtIndex;
  436. if (newIndex > maxFilenameLen)
  437. newIndex = maxFilenameLen;
  438. else
  439. newIndex = newExtIndex;
  440. } else if (newIndex > newLen - CRC_LEN)
  441. newIndex = newLen - CRC_LEN;
  442. newName[newIndex++] = CRC_MARK;
  443. valueCRC = crc_itu_t(0, fidName, fidNameLen);
  444. newName[newIndex++] = hex_asc_upper_hi(valueCRC >> 8);
  445. newName[newIndex++] = hex_asc_upper_lo(valueCRC >> 8);
  446. newName[newIndex++] = hex_asc_upper_hi(valueCRC);
  447. newName[newIndex++] = hex_asc_upper_lo(valueCRC);
  448. if (hasExt) {
  449. newName[newIndex++] = EXT_MARK;
  450. for (index = 0; index < localExtIndex; index++)
  451. newName[newIndex++] = ext[index];
  452. }
  453. }
  454. return newIndex;
  455. }