unicode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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/udf_fs.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. memset(dest, 0, sizeof(struct ustr));
  46. usesize = (size > UDF_NAME_LEN) ? UDF_NAME_LEN : size;
  47. dest->u_cmpID = ptr[0];
  48. dest->u_len = ptr[size - 1];
  49. memcpy(dest->u_name, ptr + 1, usesize - 1);
  50. return 0;
  51. }
  52. /*
  53. * udf_build_ustr_exact
  54. */
  55. static int udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
  56. {
  57. if ((!dest) || (!ptr) || (!exactsize))
  58. return -1;
  59. memset(dest, 0, sizeof(struct ustr));
  60. dest->u_cmpID = ptr[0];
  61. dest->u_len = exactsize - 1;
  62. memcpy(dest->u_name, ptr + 1, exactsize - 1);
  63. return 0;
  64. }
  65. /*
  66. * udf_ocu_to_utf8
  67. *
  68. * PURPOSE
  69. * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
  70. *
  71. * DESCRIPTION
  72. * This routine is only called by udf_filldir().
  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, struct ustr *ocu_i)
  88. {
  89. uint8_t *ocu;
  90. uint32_t c;
  91. uint8_t cmp_id, ocu_len;
  92. int i;
  93. ocu = ocu_i->u_name;
  94. ocu_len = ocu_i->u_len;
  95. cmp_id = ocu_i->u_cmpID;
  96. utf_o->u_len = 0;
  97. if (ocu_len == 0) {
  98. memset(utf_o, 0, sizeof(struct ustr));
  99. utf_o->u_cmpID = 0;
  100. utf_o->u_len = 0;
  101. return 0;
  102. }
  103. if ((cmp_id != 8) && (cmp_id != 16)) {
  104. printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
  105. cmp_id, ocu_i->u_name);
  106. return 0;
  107. }
  108. for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
  109. /* Expand OSTA compressed Unicode to Unicode */
  110. c = ocu[i++];
  111. if (cmp_id == 16)
  112. c = (c << 8) | ocu[i++];
  113. /* Compress Unicode to UTF-8 */
  114. if (c < 0x80U) {
  115. utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
  116. } else if (c < 0x800U) {
  117. utf_o->u_name[utf_o->u_len++] =
  118. (uint8_t)(0xc0 | (c >> 6));
  119. utf_o->u_name[utf_o->u_len++] =
  120. (uint8_t)(0x80 | (c & 0x3f));
  121. } else {
  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_utf8_to_ocu
  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;
  161. memset(ocu, 0, sizeof(dstring) * length);
  162. ocu[0] = 8;
  163. max_val = 0xffU;
  164. try_again:
  165. u_len = 0U;
  166. utf_char = 0U;
  167. utf_cnt = 0U;
  168. for (i = 0U; i < utf->u_len; i++) {
  169. c = (uint8_t)utf->u_name[i];
  170. /* Complete a multi-byte UTF-8 character */
  171. if (utf_cnt) {
  172. utf_char = (utf_char << 6) | (c & 0x3fU);
  173. if (--utf_cnt)
  174. continue;
  175. } else {
  176. /* Check for a multi-byte UTF-8 character */
  177. if (c & 0x80U) {
  178. /* Start a multi-byte UTF-8 character */
  179. if ((c & 0xe0U) == 0xc0U) {
  180. utf_char = c & 0x1fU;
  181. utf_cnt = 1;
  182. } else if ((c & 0xf0U) == 0xe0U) {
  183. utf_char = c & 0x0fU;
  184. utf_cnt = 2;
  185. } else if ((c & 0xf8U) == 0xf0U) {
  186. utf_char = c & 0x07U;
  187. utf_cnt = 3;
  188. } else if ((c & 0xfcU) == 0xf8U) {
  189. utf_char = c & 0x03U;
  190. utf_cnt = 4;
  191. } else if ((c & 0xfeU) == 0xfcU) {
  192. utf_char = c & 0x01U;
  193. utf_cnt = 5;
  194. } else {
  195. goto error_out;
  196. }
  197. continue;
  198. } else {
  199. /* Single byte UTF-8 character (most common) */
  200. utf_char = c;
  201. }
  202. }
  203. /* Choose no compression if necessary */
  204. if (utf_char > max_val) {
  205. if (max_val == 0xffU) {
  206. max_val = 0xffffU;
  207. ocu[0] = (uint8_t)0x10U;
  208. goto try_again;
  209. }
  210. goto error_out;
  211. }
  212. if (max_val == 0xffffU)
  213. ocu[++u_len] = (uint8_t)(utf_char >> 8);
  214. ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
  215. }
  216. if (utf_cnt) {
  217. error_out:
  218. ocu[++u_len] = '?';
  219. printk(KERN_DEBUG "udf: bad UTF-8 character\n");
  220. }
  221. ocu[length - 1] = (uint8_t)u_len + 1;
  222. return u_len + 1;
  223. }
  224. static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
  225. struct ustr *ocu_i)
  226. {
  227. uint8_t *ocu;
  228. uint32_t c;
  229. uint8_t cmp_id, ocu_len;
  230. int i;
  231. ocu = ocu_i->u_name;
  232. ocu_len = ocu_i->u_len;
  233. cmp_id = ocu_i->u_cmpID;
  234. utf_o->u_len = 0;
  235. if (ocu_len == 0) {
  236. memset(utf_o, 0, sizeof(struct ustr));
  237. utf_o->u_cmpID = 0;
  238. utf_o->u_len = 0;
  239. return 0;
  240. }
  241. if ((cmp_id != 8) && (cmp_id != 16)) {
  242. printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
  243. cmp_id, ocu_i->u_name);
  244. return 0;
  245. }
  246. for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
  247. /* Expand OSTA compressed Unicode to Unicode */
  248. c = ocu[i++];
  249. if (cmp_id == 16)
  250. c = (c << 8) | ocu[i++];
  251. utf_o->u_len += nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
  252. UDF_NAME_LEN - utf_o->u_len);
  253. }
  254. utf_o->u_cmpID = 8;
  255. return utf_o->u_len;
  256. }
  257. static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
  258. int length)
  259. {
  260. unsigned len, i, max_val;
  261. uint16_t uni_char;
  262. int u_len;
  263. memset(ocu, 0, sizeof(dstring) * length);
  264. ocu[0] = 8;
  265. max_val = 0xffU;
  266. try_again:
  267. u_len = 0U;
  268. for (i = 0U; i < uni->u_len; i++) {
  269. len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
  270. if (len <= 0)
  271. continue;
  272. if (uni_char > max_val) {
  273. max_val = 0xffffU;
  274. ocu[0] = (uint8_t)0x10U;
  275. goto try_again;
  276. }
  277. if (max_val == 0xffffU)
  278. ocu[++u_len] = (uint8_t)(uni_char >> 8);
  279. ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
  280. i += len - 1;
  281. }
  282. ocu[length - 1] = (uint8_t)u_len + 1;
  283. return u_len + 1;
  284. }
  285. int udf_get_filename(struct super_block *sb, uint8_t *sname, uint8_t *dname,
  286. int flen)
  287. {
  288. struct ustr filename, unifilename;
  289. int len;
  290. if (udf_build_ustr_exact(&unifilename, sname, flen))
  291. return 0;
  292. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
  293. if (!udf_CS0toUTF8(&filename, &unifilename)) {
  294. udf_debug("Failed in udf_get_filename: sname = %s\n",
  295. sname);
  296. return 0;
  297. }
  298. } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
  299. if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, &filename,
  300. &unifilename)) {
  301. udf_debug("Failed in udf_get_filename: sname = %s\n",
  302. sname);
  303. return 0;
  304. }
  305. } else
  306. return 0;
  307. len = udf_translate_to_linux(dname, filename.u_name, filename.u_len,
  308. unifilename.u_name, unifilename.u_len);
  309. if (len)
  310. return len;
  311. return 0;
  312. }
  313. int udf_put_filename(struct super_block *sb, const uint8_t *sname,
  314. uint8_t *dname, int flen)
  315. {
  316. struct ustr unifilename;
  317. int namelen;
  318. if (!udf_char_to_ustr(&unifilename, sname, flen))
  319. return 0;
  320. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
  321. namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
  322. if (!namelen)
  323. return 0;
  324. } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
  325. namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
  326. &unifilename, UDF_NAME_LEN);
  327. if (!namelen)
  328. return 0;
  329. } else
  330. return 0;
  331. return namelen;
  332. }
  333. #define ILLEGAL_CHAR_MARK '_'
  334. #define EXT_MARK '.'
  335. #define CRC_MARK '#'
  336. #define EXT_SIZE 5
  337. static int udf_translate_to_linux(uint8_t *newName, uint8_t *udfName,
  338. int udfLen, uint8_t *fidName,
  339. int fidNameLen)
  340. {
  341. int index, newIndex = 0, needsCRC = 0;
  342. int extIndex = 0, newExtIndex = 0, hasExt = 0;
  343. unsigned short valueCRC;
  344. uint8_t curr;
  345. const uint8_t hexChar[] = "0123456789ABCDEF";
  346. if (udfName[0] == '.' &&
  347. (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
  348. needsCRC = 1;
  349. newIndex = udfLen;
  350. memcpy(newName, udfName, udfLen);
  351. } else {
  352. for (index = 0; index < udfLen; index++) {
  353. curr = udfName[index];
  354. if (curr == '/' || curr == 0) {
  355. needsCRC = 1;
  356. curr = ILLEGAL_CHAR_MARK;
  357. while (index + 1 < udfLen &&
  358. (udfName[index + 1] == '/' ||
  359. udfName[index + 1] == 0))
  360. index++;
  361. }
  362. if (curr == EXT_MARK &&
  363. (udfLen - index - 1) <= EXT_SIZE) {
  364. if (udfLen == index + 1)
  365. hasExt = 0;
  366. else {
  367. hasExt = 1;
  368. extIndex = index;
  369. newExtIndex = newIndex;
  370. }
  371. }
  372. if (newIndex < 256)
  373. newName[newIndex++] = curr;
  374. else
  375. needsCRC = 1;
  376. }
  377. }
  378. if (needsCRC) {
  379. uint8_t ext[EXT_SIZE];
  380. int localExtIndex = 0;
  381. if (hasExt) {
  382. int maxFilenameLen;
  383. for (index = 0;
  384. index < EXT_SIZE && extIndex + index + 1 < udfLen;
  385. index++) {
  386. curr = udfName[extIndex + index + 1];
  387. if (curr == '/' || curr == 0) {
  388. needsCRC = 1;
  389. curr = ILLEGAL_CHAR_MARK;
  390. while (extIndex + index + 2 < udfLen &&
  391. (index + 1 < EXT_SIZE &&
  392. (udfName[extIndex + index + 2] == '/' ||
  393. udfName[extIndex + index + 2] == 0)))
  394. index++;
  395. }
  396. ext[localExtIndex++] = curr;
  397. }
  398. maxFilenameLen = 250 - localExtIndex;
  399. if (newIndex > maxFilenameLen)
  400. newIndex = maxFilenameLen;
  401. else
  402. newIndex = newExtIndex;
  403. } else if (newIndex > 250)
  404. newIndex = 250;
  405. newName[newIndex++] = CRC_MARK;
  406. valueCRC = udf_crc(fidName, fidNameLen, 0);
  407. newName[newIndex++] = hexChar[(valueCRC & 0xf000) >> 12];
  408. newName[newIndex++] = hexChar[(valueCRC & 0x0f00) >> 8];
  409. newName[newIndex++] = hexChar[(valueCRC & 0x00f0) >> 4];
  410. newName[newIndex++] = hexChar[(valueCRC & 0x000f)];
  411. if (hasExt) {
  412. newName[newIndex++] = EXT_MARK;
  413. for (index = 0; index < localExtIndex; index++)
  414. newName[newIndex++] = ext[index];
  415. }
  416. }
  417. return newIndex;
  418. }