unicode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 int udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
  60. {
  61. if ((!dest) || (!ptr) || (!exactsize))
  62. return -1;
  63. memset(dest, 0, sizeof(struct ustr));
  64. dest->u_cmpID = ptr[0];
  65. dest->u_len = exactsize - 1;
  66. memcpy(dest->u_name, ptr + 1, exactsize - 1);
  67. return 0;
  68. }
  69. /*
  70. * udf_ocu_to_utf8
  71. *
  72. * PURPOSE
  73. * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
  74. *
  75. * PRE-CONDITIONS
  76. * utf Pointer to UTF-8 output buffer.
  77. * ocu Pointer to OSTA Compressed Unicode input buffer
  78. * of size UDF_NAME_LEN bytes.
  79. * both of type "struct ustr *"
  80. *
  81. * POST-CONDITIONS
  82. * <return> Zero on success.
  83. *
  84. * HISTORY
  85. * November 12, 1997 - Andrew E. Mileski
  86. * Written, tested, and released.
  87. */
  88. int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
  89. {
  90. const uint8_t *ocu;
  91. uint8_t cmp_id, ocu_len;
  92. int i;
  93. ocu_len = ocu_i->u_len;
  94. if (ocu_len == 0) {
  95. memset(utf_o, 0, sizeof(struct ustr));
  96. return 0;
  97. }
  98. cmp_id = ocu_i->u_cmpID;
  99. if (cmp_id != 8 && cmp_id != 16) {
  100. memset(utf_o, 0, sizeof(struct ustr));
  101. pr_err("unknown compression code (%d) stri=%s\n",
  102. cmp_id, ocu_i->u_name);
  103. return 0;
  104. }
  105. ocu = ocu_i->u_name;
  106. utf_o->u_len = 0;
  107. for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
  108. /* Expand OSTA compressed Unicode to Unicode */
  109. uint32_t c = ocu[i++];
  110. if (cmp_id == 16)
  111. c = (c << 8) | ocu[i++];
  112. /* Compress Unicode to UTF-8 */
  113. if (c < 0x80U)
  114. utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
  115. else if (c < 0x800U) {
  116. utf_o->u_name[utf_o->u_len++] =
  117. (uint8_t)(0xc0 | (c >> 6));
  118. utf_o->u_name[utf_o->u_len++] =
  119. (uint8_t)(0x80 | (c & 0x3f));
  120. } else {
  121. utf_o->u_name[utf_o->u_len++] =
  122. (uint8_t)(0xe0 | (c >> 12));
  123. utf_o->u_name[utf_o->u_len++] =
  124. (uint8_t)(0x80 |
  125. ((c >> 6) & 0x3f));
  126. utf_o->u_name[utf_o->u_len++] =
  127. (uint8_t)(0x80 | (c & 0x3f));
  128. }
  129. }
  130. utf_o->u_cmpID = 8;
  131. return utf_o->u_len;
  132. }
  133. /*
  134. *
  135. * udf_utf8_to_ocu
  136. *
  137. * PURPOSE
  138. * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
  139. *
  140. * DESCRIPTION
  141. * This routine is only called by udf_lookup().
  142. *
  143. * PRE-CONDITIONS
  144. * ocu Pointer to OSTA Compressed Unicode output
  145. * buffer of size UDF_NAME_LEN bytes.
  146. * utf Pointer to UTF-8 input buffer.
  147. * utf_len Length of UTF-8 input buffer in bytes.
  148. *
  149. * POST-CONDITIONS
  150. * <return> Zero on success.
  151. *
  152. * HISTORY
  153. * November 12, 1997 - Andrew E. Mileski
  154. * Written, tested, and released.
  155. */
  156. static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
  157. {
  158. unsigned c, i, max_val, utf_char;
  159. int utf_cnt, u_len;
  160. memset(ocu, 0, sizeof(dstring) * length);
  161. ocu[0] = 8;
  162. max_val = 0xffU;
  163. try_again:
  164. u_len = 0U;
  165. utf_char = 0U;
  166. utf_cnt = 0U;
  167. for (i = 0U; i < utf->u_len; i++) {
  168. c = (uint8_t)utf->u_name[i];
  169. /* Complete a multi-byte UTF-8 character */
  170. if (utf_cnt) {
  171. utf_char = (utf_char << 6) | (c & 0x3fU);
  172. if (--utf_cnt)
  173. continue;
  174. } else {
  175. /* Check for a multi-byte UTF-8 character */
  176. if (c & 0x80U) {
  177. /* Start a multi-byte UTF-8 character */
  178. if ((c & 0xe0U) == 0xc0U) {
  179. utf_char = c & 0x1fU;
  180. utf_cnt = 1;
  181. } else if ((c & 0xf0U) == 0xe0U) {
  182. utf_char = c & 0x0fU;
  183. utf_cnt = 2;
  184. } else if ((c & 0xf8U) == 0xf0U) {
  185. utf_char = c & 0x07U;
  186. utf_cnt = 3;
  187. } else if ((c & 0xfcU) == 0xf8U) {
  188. utf_char = c & 0x03U;
  189. utf_cnt = 4;
  190. } else if ((c & 0xfeU) == 0xfcU) {
  191. utf_char = c & 0x01U;
  192. utf_cnt = 5;
  193. } else {
  194. goto error_out;
  195. }
  196. continue;
  197. } else {
  198. /* Single byte UTF-8 character (most common) */
  199. utf_char = c;
  200. }
  201. }
  202. /* Choose no compression if necessary */
  203. if (utf_char > max_val) {
  204. if (max_val == 0xffU) {
  205. max_val = 0xffffU;
  206. ocu[0] = (uint8_t)0x10U;
  207. goto try_again;
  208. }
  209. goto error_out;
  210. }
  211. if (max_val == 0xffffU)
  212. ocu[++u_len] = (uint8_t)(utf_char >> 8);
  213. ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
  214. }
  215. if (utf_cnt) {
  216. error_out:
  217. ocu[++u_len] = '?';
  218. printk(KERN_DEBUG pr_fmt("bad UTF-8 character\n"));
  219. }
  220. ocu[length - 1] = (uint8_t)u_len + 1;
  221. return u_len + 1;
  222. }
  223. static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
  224. const struct ustr *ocu_i)
  225. {
  226. const uint8_t *ocu;
  227. uint8_t cmp_id, ocu_len;
  228. int i, len;
  229. ocu_len = ocu_i->u_len;
  230. if (ocu_len == 0) {
  231. memset(utf_o, 0, sizeof(struct ustr));
  232. return 0;
  233. }
  234. cmp_id = ocu_i->u_cmpID;
  235. if (cmp_id != 8 && cmp_id != 16) {
  236. memset(utf_o, 0, sizeof(struct ustr));
  237. pr_err("unknown compression code (%d) stri=%s\n",
  238. cmp_id, ocu_i->u_name);
  239. return 0;
  240. }
  241. ocu = ocu_i->u_name;
  242. utf_o->u_len = 0;
  243. for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
  244. /* Expand OSTA compressed Unicode to Unicode */
  245. uint32_t c = ocu[i++];
  246. if (cmp_id == 16)
  247. c = (c << 8) | ocu[i++];
  248. len = nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
  249. UDF_NAME_LEN - utf_o->u_len);
  250. /* Valid character? */
  251. if (len >= 0)
  252. utf_o->u_len += len;
  253. else
  254. utf_o->u_name[utf_o->u_len++] = '?';
  255. }
  256. utf_o->u_cmpID = 8;
  257. return utf_o->u_len;
  258. }
  259. static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
  260. int length)
  261. {
  262. int len;
  263. unsigned i, max_val;
  264. uint16_t uni_char;
  265. int u_len;
  266. memset(ocu, 0, sizeof(dstring) * length);
  267. ocu[0] = 8;
  268. max_val = 0xffU;
  269. try_again:
  270. u_len = 0U;
  271. for (i = 0U; i < uni->u_len; i++) {
  272. len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
  273. if (!len)
  274. continue;
  275. /* Invalid character, deal with it */
  276. if (len < 0) {
  277. len = 1;
  278. uni_char = '?';
  279. }
  280. if (uni_char > max_val) {
  281. max_val = 0xffffU;
  282. ocu[0] = (uint8_t)0x10U;
  283. goto try_again;
  284. }
  285. if (max_val == 0xffffU)
  286. ocu[++u_len] = (uint8_t)(uni_char >> 8);
  287. ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
  288. i += len - 1;
  289. }
  290. ocu[length - 1] = (uint8_t)u_len + 1;
  291. return u_len + 1;
  292. }
  293. int udf_get_filename(struct super_block *sb, uint8_t *sname, int slen,
  294. uint8_t *dname, int dlen)
  295. {
  296. struct ustr *filename, *unifilename;
  297. int len = 0;
  298. filename = kmalloc(sizeof(struct ustr), GFP_NOFS);
  299. if (!filename)
  300. return 0;
  301. unifilename = kmalloc(sizeof(struct ustr), GFP_NOFS);
  302. if (!unifilename)
  303. goto out1;
  304. if (udf_build_ustr_exact(unifilename, sname, slen))
  305. goto out2;
  306. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
  307. if (!udf_CS0toUTF8(filename, unifilename)) {
  308. udf_debug("Failed in udf_get_filename: sname = %s\n",
  309. sname);
  310. goto out2;
  311. }
  312. } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
  313. if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, filename,
  314. unifilename)) {
  315. udf_debug("Failed in udf_get_filename: sname = %s\n",
  316. sname);
  317. goto out2;
  318. }
  319. } else
  320. goto out2;
  321. len = udf_translate_to_linux(dname, dlen,
  322. filename->u_name, filename->u_len,
  323. unifilename->u_name, unifilename->u_len);
  324. out2:
  325. kfree(unifilename);
  326. out1:
  327. kfree(filename);
  328. return len;
  329. }
  330. int udf_put_filename(struct super_block *sb, const uint8_t *sname,
  331. uint8_t *dname, int flen)
  332. {
  333. struct ustr unifilename;
  334. int namelen;
  335. if (!udf_char_to_ustr(&unifilename, sname, flen))
  336. return 0;
  337. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
  338. namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
  339. if (!namelen)
  340. return 0;
  341. } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
  342. namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
  343. &unifilename, UDF_NAME_LEN);
  344. if (!namelen)
  345. return 0;
  346. } else
  347. return 0;
  348. return namelen;
  349. }
  350. #define ILLEGAL_CHAR_MARK '_'
  351. #define EXT_MARK '.'
  352. #define CRC_MARK '#'
  353. #define EXT_SIZE 5
  354. /* Number of chars we need to store generated CRC to make filename unique */
  355. #define CRC_LEN 5
  356. static int udf_translate_to_linux(uint8_t *newName, int newLen,
  357. uint8_t *udfName, int udfLen,
  358. uint8_t *fidName, int fidNameLen)
  359. {
  360. int index, newIndex = 0, needsCRC = 0;
  361. int extIndex = 0, newExtIndex = 0, hasExt = 0;
  362. unsigned short valueCRC;
  363. uint8_t curr;
  364. const uint8_t hexChar[] = "0123456789ABCDEF";
  365. if (udfName[0] == '.' &&
  366. (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
  367. needsCRC = 1;
  368. newIndex = udfLen;
  369. memcpy(newName, udfName, udfLen);
  370. } else {
  371. for (index = 0; index < udfLen; index++) {
  372. curr = udfName[index];
  373. if (curr == '/' || curr == 0) {
  374. needsCRC = 1;
  375. curr = ILLEGAL_CHAR_MARK;
  376. while (index + 1 < udfLen &&
  377. (udfName[index + 1] == '/' ||
  378. udfName[index + 1] == 0))
  379. index++;
  380. }
  381. if (curr == EXT_MARK &&
  382. (udfLen - index - 1) <= EXT_SIZE) {
  383. if (udfLen == index + 1)
  384. hasExt = 0;
  385. else {
  386. hasExt = 1;
  387. extIndex = index;
  388. newExtIndex = newIndex;
  389. }
  390. }
  391. if (newIndex < newLen)
  392. newName[newIndex++] = curr;
  393. else
  394. needsCRC = 1;
  395. }
  396. }
  397. if (needsCRC) {
  398. uint8_t ext[EXT_SIZE];
  399. int localExtIndex = 0;
  400. if (hasExt) {
  401. int maxFilenameLen;
  402. for (index = 0;
  403. index < EXT_SIZE && extIndex + index + 1 < udfLen;
  404. index++) {
  405. curr = udfName[extIndex + index + 1];
  406. if (curr == '/' || curr == 0) {
  407. needsCRC = 1;
  408. curr = ILLEGAL_CHAR_MARK;
  409. while (extIndex + index + 2 < udfLen &&
  410. (index + 1 < EXT_SIZE &&
  411. (udfName[extIndex + index + 2] == '/' ||
  412. udfName[extIndex + index + 2] == 0)))
  413. index++;
  414. }
  415. ext[localExtIndex++] = curr;
  416. }
  417. maxFilenameLen = newLen - CRC_LEN - localExtIndex;
  418. if (newIndex > maxFilenameLen)
  419. newIndex = maxFilenameLen;
  420. else
  421. newIndex = newExtIndex;
  422. } else if (newIndex > newLen - CRC_LEN)
  423. newIndex = newLen - CRC_LEN;
  424. newName[newIndex++] = CRC_MARK;
  425. valueCRC = crc_itu_t(0, fidName, fidNameLen);
  426. newName[newIndex++] = hexChar[(valueCRC & 0xf000) >> 12];
  427. newName[newIndex++] = hexChar[(valueCRC & 0x0f00) >> 8];
  428. newName[newIndex++] = hexChar[(valueCRC & 0x00f0) >> 4];
  429. newName[newIndex++] = hexChar[(valueCRC & 0x000f)];
  430. if (hasExt) {
  431. newName[newIndex++] = EXT_MARK;
  432. for (index = 0; index < localExtIndex; index++)
  433. newName[newIndex++] = ext[index];
  434. }
  435. }
  436. return newIndex;
  437. }