unicode.c 11 KB

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