unicode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. BUG_ON(size < 2);
  45. usesize = min_t(size_t, ptr[size - 1], sizeof(dest->u_name));
  46. usesize = min(usesize, size - 2);
  47. dest->u_cmpID = ptr[0];
  48. dest->u_len = usesize;
  49. memcpy(dest->u_name, ptr + 1, usesize);
  50. memset(dest->u_name + usesize, 0, sizeof(dest->u_name) - usesize);
  51. return 0;
  52. }
  53. /*
  54. * udf_build_ustr_exact
  55. */
  56. static int udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
  57. {
  58. if ((!dest) || (!ptr) || (!exactsize))
  59. return -1;
  60. memset(dest, 0, sizeof(struct ustr));
  61. dest->u_cmpID = ptr[0];
  62. dest->u_len = exactsize - 1;
  63. memcpy(dest->u_name, ptr + 1, exactsize - 1);
  64. return 0;
  65. }
  66. /*
  67. * udf_ocu_to_utf8
  68. *
  69. * PURPOSE
  70. * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
  71. *
  72. * PRE-CONDITIONS
  73. * utf Pointer to UTF-8 output buffer.
  74. * ocu Pointer to OSTA Compressed Unicode input buffer
  75. * of size UDF_NAME_LEN bytes.
  76. * both of type "struct ustr *"
  77. *
  78. * POST-CONDITIONS
  79. * <return> Zero on success.
  80. *
  81. * HISTORY
  82. * November 12, 1997 - Andrew E. Mileski
  83. * Written, tested, and released.
  84. */
  85. int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
  86. {
  87. const uint8_t *ocu;
  88. uint8_t cmp_id, ocu_len;
  89. int i;
  90. ocu_len = ocu_i->u_len;
  91. if (ocu_len == 0) {
  92. memset(utf_o, 0, sizeof(struct ustr));
  93. return 0;
  94. }
  95. cmp_id = ocu_i->u_cmpID;
  96. if (cmp_id != 8 && cmp_id != 16) {
  97. memset(utf_o, 0, sizeof(struct ustr));
  98. printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
  99. cmp_id, ocu_i->u_name);
  100. return 0;
  101. }
  102. ocu = ocu_i->u_name;
  103. utf_o->u_len = 0;
  104. for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
  105. /* Expand OSTA compressed Unicode to Unicode */
  106. uint32_t c = ocu[i++];
  107. if (cmp_id == 16)
  108. c = (c << 8) | ocu[i++];
  109. /* Compress Unicode to UTF-8 */
  110. if (c < 0x80U)
  111. utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
  112. else if (c < 0x800U) {
  113. utf_o->u_name[utf_o->u_len++] =
  114. (uint8_t)(0xc0 | (c >> 6));
  115. utf_o->u_name[utf_o->u_len++] =
  116. (uint8_t)(0x80 | (c & 0x3f));
  117. } else {
  118. utf_o->u_name[utf_o->u_len++] =
  119. (uint8_t)(0xe0 | (c >> 12));
  120. utf_o->u_name[utf_o->u_len++] =
  121. (uint8_t)(0x80 |
  122. ((c >> 6) & 0x3f));
  123. utf_o->u_name[utf_o->u_len++] =
  124. (uint8_t)(0x80 | (c & 0x3f));
  125. }
  126. }
  127. utf_o->u_cmpID = 8;
  128. return utf_o->u_len;
  129. }
  130. /*
  131. *
  132. * udf_utf8_to_ocu
  133. *
  134. * PURPOSE
  135. * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
  136. *
  137. * DESCRIPTION
  138. * This routine is only called by udf_lookup().
  139. *
  140. * PRE-CONDITIONS
  141. * ocu Pointer to OSTA Compressed Unicode output
  142. * buffer of size UDF_NAME_LEN bytes.
  143. * utf Pointer to UTF-8 input buffer.
  144. * utf_len Length of UTF-8 input buffer in bytes.
  145. *
  146. * POST-CONDITIONS
  147. * <return> Zero on success.
  148. *
  149. * HISTORY
  150. * November 12, 1997 - Andrew E. Mileski
  151. * Written, tested, and released.
  152. */
  153. static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
  154. {
  155. unsigned c, i, max_val, utf_char;
  156. int utf_cnt, u_len;
  157. memset(ocu, 0, sizeof(dstring) * length);
  158. ocu[0] = 8;
  159. max_val = 0xffU;
  160. try_again:
  161. u_len = 0U;
  162. utf_char = 0U;
  163. utf_cnt = 0U;
  164. for (i = 0U; i < utf->u_len; i++) {
  165. c = (uint8_t)utf->u_name[i];
  166. /* Complete a multi-byte UTF-8 character */
  167. if (utf_cnt) {
  168. utf_char = (utf_char << 6) | (c & 0x3fU);
  169. if (--utf_cnt)
  170. continue;
  171. } else {
  172. /* Check for a multi-byte UTF-8 character */
  173. if (c & 0x80U) {
  174. /* Start a multi-byte UTF-8 character */
  175. if ((c & 0xe0U) == 0xc0U) {
  176. utf_char = c & 0x1fU;
  177. utf_cnt = 1;
  178. } else if ((c & 0xf0U) == 0xe0U) {
  179. utf_char = c & 0x0fU;
  180. utf_cnt = 2;
  181. } else if ((c & 0xf8U) == 0xf0U) {
  182. utf_char = c & 0x07U;
  183. utf_cnt = 3;
  184. } else if ((c & 0xfcU) == 0xf8U) {
  185. utf_char = c & 0x03U;
  186. utf_cnt = 4;
  187. } else if ((c & 0xfeU) == 0xfcU) {
  188. utf_char = c & 0x01U;
  189. utf_cnt = 5;
  190. } else {
  191. goto error_out;
  192. }
  193. continue;
  194. } else {
  195. /* Single byte UTF-8 character (most common) */
  196. utf_char = c;
  197. }
  198. }
  199. /* Choose no compression if necessary */
  200. if (utf_char > max_val) {
  201. if (max_val == 0xffU) {
  202. max_val = 0xffffU;
  203. ocu[0] = (uint8_t)0x10U;
  204. goto try_again;
  205. }
  206. goto error_out;
  207. }
  208. if (max_val == 0xffffU)
  209. ocu[++u_len] = (uint8_t)(utf_char >> 8);
  210. ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
  211. }
  212. if (utf_cnt) {
  213. error_out:
  214. ocu[++u_len] = '?';
  215. printk(KERN_DEBUG "udf: bad UTF-8 character\n");
  216. }
  217. ocu[length - 1] = (uint8_t)u_len + 1;
  218. return u_len + 1;
  219. }
  220. static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
  221. const struct ustr *ocu_i)
  222. {
  223. const uint8_t *ocu;
  224. uint8_t cmp_id, ocu_len;
  225. int i;
  226. ocu_len = ocu_i->u_len;
  227. if (ocu_len == 0) {
  228. memset(utf_o, 0, sizeof(struct ustr));
  229. return 0;
  230. }
  231. cmp_id = ocu_i->u_cmpID;
  232. if (cmp_id != 8 && cmp_id != 16) {
  233. memset(utf_o, 0, sizeof(struct ustr));
  234. printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
  235. cmp_id, ocu_i->u_name);
  236. return 0;
  237. }
  238. ocu = ocu_i->u_name;
  239. utf_o->u_len = 0;
  240. for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
  241. /* Expand OSTA compressed Unicode to Unicode */
  242. uint32_t 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. }