unicode.c 11 KB

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