unicode.c 11 KB

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