unicode.c 11 KB

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