unicode.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 <linux/slab.h>
  26. #include "udf_sb.h"
  27. #define SURROGATE_MASK 0xfffff800
  28. #define SURROGATE_PAIR 0x0000d800
  29. static int udf_uni2char_utf8(wchar_t uni,
  30. unsigned char *out,
  31. int boundlen)
  32. {
  33. int u_len = 0;
  34. if (boundlen <= 0)
  35. return -ENAMETOOLONG;
  36. if ((uni & SURROGATE_MASK) == SURROGATE_PAIR)
  37. return -EINVAL;
  38. if (uni < 0x80) {
  39. out[u_len++] = (unsigned char)uni;
  40. } else if (uni < 0x800) {
  41. if (boundlen < 2)
  42. return -ENAMETOOLONG;
  43. out[u_len++] = (unsigned char)(0xc0 | (uni >> 6));
  44. out[u_len++] = (unsigned char)(0x80 | (uni & 0x3f));
  45. } else {
  46. if (boundlen < 3)
  47. return -ENAMETOOLONG;
  48. out[u_len++] = (unsigned char)(0xe0 | (uni >> 12));
  49. out[u_len++] = (unsigned char)(0x80 | ((uni >> 6) & 0x3f));
  50. out[u_len++] = (unsigned char)(0x80 | (uni & 0x3f));
  51. }
  52. return u_len;
  53. }
  54. static int udf_char2uni_utf8(const unsigned char *in,
  55. int boundlen,
  56. wchar_t *uni)
  57. {
  58. unsigned int utf_char;
  59. unsigned char c;
  60. int utf_cnt, u_len;
  61. utf_char = 0;
  62. utf_cnt = 0;
  63. for (u_len = 0; u_len < boundlen;) {
  64. c = in[u_len++];
  65. /* Complete a multi-byte UTF-8 character */
  66. if (utf_cnt) {
  67. utf_char = (utf_char << 6) | (c & 0x3f);
  68. if (--utf_cnt)
  69. continue;
  70. } else {
  71. /* Check for a multi-byte UTF-8 character */
  72. if (c & 0x80) {
  73. /* Start a multi-byte UTF-8 character */
  74. if ((c & 0xe0) == 0xc0) {
  75. utf_char = c & 0x1f;
  76. utf_cnt = 1;
  77. } else if ((c & 0xf0) == 0xe0) {
  78. utf_char = c & 0x0f;
  79. utf_cnt = 2;
  80. } else if ((c & 0xf8) == 0xf0) {
  81. utf_char = c & 0x07;
  82. utf_cnt = 3;
  83. } else if ((c & 0xfc) == 0xf8) {
  84. utf_char = c & 0x03;
  85. utf_cnt = 4;
  86. } else if ((c & 0xfe) == 0xfc) {
  87. utf_char = c & 0x01;
  88. utf_cnt = 5;
  89. } else {
  90. utf_cnt = -1;
  91. break;
  92. }
  93. continue;
  94. } else {
  95. /* Single byte UTF-8 character (most common) */
  96. utf_char = c;
  97. }
  98. }
  99. *uni = utf_char;
  100. break;
  101. }
  102. if (utf_cnt) {
  103. *uni = '?';
  104. return -EINVAL;
  105. }
  106. return u_len;
  107. }
  108. #define ILLEGAL_CHAR_MARK '_'
  109. #define EXT_MARK '.'
  110. #define CRC_MARK '#'
  111. #define EXT_SIZE 5
  112. /* Number of chars we need to store generated CRC to make filename unique */
  113. #define CRC_LEN 5
  114. static int udf_name_conv_char(uint8_t *str_o, int str_o_max_len,
  115. int *str_o_idx,
  116. const uint8_t *str_i, int str_i_max_len,
  117. int *str_i_idx,
  118. int u_ch, int *needsCRC,
  119. int (*conv_f)(wchar_t, unsigned char *, int),
  120. int translate)
  121. {
  122. uint32_t c;
  123. int illChar = 0;
  124. int len, gotch = 0;
  125. for (; (!gotch) && (*str_i_idx < str_i_max_len); *str_i_idx += u_ch) {
  126. if (*str_o_idx >= str_o_max_len) {
  127. *needsCRC = 1;
  128. return gotch;
  129. }
  130. /* Expand OSTA compressed Unicode to Unicode */
  131. c = str_i[*str_i_idx];
  132. if (u_ch > 1)
  133. c = (c << 8) | str_i[*str_i_idx + 1];
  134. if (translate && (c == '/' || c == 0))
  135. illChar = 1;
  136. else if (illChar)
  137. break;
  138. else
  139. gotch = 1;
  140. }
  141. if (illChar) {
  142. *needsCRC = 1;
  143. c = ILLEGAL_CHAR_MARK;
  144. gotch = 1;
  145. }
  146. if (gotch) {
  147. len = conv_f(c, &str_o[*str_o_idx], str_o_max_len - *str_o_idx);
  148. /* Valid character? */
  149. if (len >= 0)
  150. *str_o_idx += len;
  151. else if (len == -ENAMETOOLONG) {
  152. *needsCRC = 1;
  153. gotch = 0;
  154. } else {
  155. str_o[(*str_o_idx)++] = '?';
  156. *needsCRC = 1;
  157. }
  158. }
  159. return gotch;
  160. }
  161. static int udf_name_from_CS0(uint8_t *str_o, int str_max_len,
  162. const uint8_t *ocu, int ocu_len,
  163. int (*conv_f)(wchar_t, unsigned char *, int),
  164. int translate)
  165. {
  166. uint32_t c;
  167. uint8_t cmp_id;
  168. int idx, len;
  169. int u_ch;
  170. int needsCRC = 0;
  171. int ext_i_len, ext_max_len;
  172. int str_o_len = 0; /* Length of resulting output */
  173. int ext_o_len = 0; /* Extension output length */
  174. int ext_crc_len = 0; /* Extension output length if used with CRC */
  175. int i_ext = -1; /* Extension position in input buffer */
  176. int o_crc = 0; /* Rightmost possible output pos for CRC+ext */
  177. unsigned short valueCRC;
  178. uint8_t ext[EXT_SIZE * NLS_MAX_CHARSET_SIZE + 1];
  179. uint8_t crc[CRC_LEN];
  180. if (str_max_len <= 0)
  181. return 0;
  182. if (ocu_len == 0) {
  183. memset(str_o, 0, str_max_len);
  184. return 0;
  185. }
  186. cmp_id = ocu[0];
  187. if (cmp_id != 8 && cmp_id != 16) {
  188. memset(str_o, 0, str_max_len);
  189. pr_err("unknown compression code (%d)\n", cmp_id);
  190. return -EINVAL;
  191. }
  192. u_ch = cmp_id >> 3;
  193. ocu++;
  194. ocu_len--;
  195. if (ocu_len % u_ch) {
  196. pr_err("incorrect filename length (%d)\n", ocu_len + 1);
  197. return -EINVAL;
  198. }
  199. if (translate) {
  200. /* Look for extension */
  201. for (idx = ocu_len - u_ch, ext_i_len = 0;
  202. (idx >= 0) && (ext_i_len < EXT_SIZE);
  203. idx -= u_ch, ext_i_len++) {
  204. c = ocu[idx];
  205. if (u_ch > 1)
  206. c = (c << 8) | ocu[idx + 1];
  207. if (c == EXT_MARK) {
  208. if (ext_i_len)
  209. i_ext = idx;
  210. break;
  211. }
  212. }
  213. if (i_ext >= 0) {
  214. /* Convert extension */
  215. ext_max_len = min_t(int, sizeof(ext), str_max_len);
  216. ext[ext_o_len++] = EXT_MARK;
  217. idx = i_ext + u_ch;
  218. while (udf_name_conv_char(ext, ext_max_len, &ext_o_len,
  219. ocu, ocu_len, &idx,
  220. u_ch, &needsCRC,
  221. conv_f, translate)) {
  222. if ((ext_o_len + CRC_LEN) < str_max_len)
  223. ext_crc_len = ext_o_len;
  224. }
  225. }
  226. }
  227. idx = 0;
  228. while (1) {
  229. if (translate && (idx == i_ext)) {
  230. if (str_o_len > (str_max_len - ext_o_len))
  231. needsCRC = 1;
  232. break;
  233. }
  234. if (!udf_name_conv_char(str_o, str_max_len, &str_o_len,
  235. ocu, ocu_len, &idx,
  236. u_ch, &needsCRC, conv_f, translate))
  237. break;
  238. if (translate &&
  239. (str_o_len <= (str_max_len - ext_o_len - CRC_LEN)))
  240. o_crc = str_o_len;
  241. }
  242. if (translate) {
  243. if (str_o_len <= 2 && str_o[0] == '.' &&
  244. (str_o_len == 1 || str_o[1] == '.'))
  245. needsCRC = 1;
  246. if (needsCRC) {
  247. str_o_len = o_crc;
  248. valueCRC = crc_itu_t(0, ocu, ocu_len);
  249. crc[0] = CRC_MARK;
  250. crc[1] = hex_asc_upper_hi(valueCRC >> 8);
  251. crc[2] = hex_asc_upper_lo(valueCRC >> 8);
  252. crc[3] = hex_asc_upper_hi(valueCRC);
  253. crc[4] = hex_asc_upper_lo(valueCRC);
  254. len = min_t(int, CRC_LEN, str_max_len - str_o_len);
  255. memcpy(&str_o[str_o_len], crc, len);
  256. str_o_len += len;
  257. ext_o_len = ext_crc_len;
  258. }
  259. if (ext_o_len > 0) {
  260. memcpy(&str_o[str_o_len], ext, ext_o_len);
  261. str_o_len += ext_o_len;
  262. }
  263. }
  264. return str_o_len;
  265. }
  266. static int udf_name_to_CS0(uint8_t *ocu, int ocu_max_len,
  267. const uint8_t *str_i, int str_len,
  268. int (*conv_f)(const unsigned char *, int, wchar_t *))
  269. {
  270. int i, len;
  271. unsigned int max_val;
  272. wchar_t uni_char;
  273. int u_len, u_ch;
  274. if (ocu_max_len <= 0)
  275. return 0;
  276. memset(ocu, 0, ocu_max_len);
  277. ocu[0] = 8;
  278. max_val = 0xff;
  279. u_ch = 1;
  280. try_again:
  281. u_len = 1;
  282. for (i = 0; i < str_len; i++) {
  283. /* Name didn't fit? */
  284. if (u_len + u_ch > ocu_max_len)
  285. return 0;
  286. len = conv_f(&str_i[i], str_len - i, &uni_char);
  287. if (!len)
  288. continue;
  289. /* Invalid character, deal with it */
  290. if (len < 0) {
  291. len = 1;
  292. uni_char = '?';
  293. }
  294. if (uni_char > max_val) {
  295. max_val = 0xffff;
  296. ocu[0] = 0x10;
  297. u_ch = 2;
  298. goto try_again;
  299. }
  300. if (max_val == 0xffff)
  301. ocu[u_len++] = (uint8_t)(uni_char >> 8);
  302. ocu[u_len++] = (uint8_t)(uni_char & 0xff);
  303. i += len - 1;
  304. }
  305. return u_len;
  306. }
  307. /*
  308. * Convert CS0 dstring to output charset. Warning: This function may truncate
  309. * input string if it is too long as it is used for informational strings only
  310. * and it is better to truncate the string than to refuse mounting a media.
  311. */
  312. int udf_dstrCS0toUTF8(uint8_t *utf_o, int o_len,
  313. const uint8_t *ocu_i, int i_len)
  314. {
  315. int s_len = 0;
  316. if (i_len > 0) {
  317. s_len = ocu_i[i_len - 1];
  318. if (s_len >= i_len) {
  319. pr_warn("incorrect dstring lengths (%d/%d),"
  320. " truncating\n", s_len, i_len);
  321. s_len = i_len - 1;
  322. /* 2-byte encoding? Need to round properly... */
  323. if (ocu_i[0] == 16)
  324. s_len -= (s_len - 1) & 2;
  325. }
  326. }
  327. return udf_name_from_CS0(utf_o, o_len, ocu_i, s_len,
  328. udf_uni2char_utf8, 0);
  329. }
  330. int udf_get_filename(struct super_block *sb, const uint8_t *sname, int slen,
  331. uint8_t *dname, int dlen)
  332. {
  333. int (*conv_f)(wchar_t, unsigned char *, int);
  334. int ret;
  335. if (!slen)
  336. return -EIO;
  337. if (dlen <= 0)
  338. return 0;
  339. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
  340. conv_f = udf_uni2char_utf8;
  341. } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
  342. conv_f = UDF_SB(sb)->s_nls_map->uni2char;
  343. } else
  344. BUG();
  345. ret = udf_name_from_CS0(dname, dlen, sname, slen, conv_f, 1);
  346. /* Zero length filename isn't valid... */
  347. if (ret == 0)
  348. ret = -EINVAL;
  349. return ret;
  350. }
  351. int udf_put_filename(struct super_block *sb, const uint8_t *sname, int slen,
  352. uint8_t *dname, int dlen)
  353. {
  354. int (*conv_f)(const unsigned char *, int, wchar_t *);
  355. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
  356. conv_f = udf_char2uni_utf8;
  357. } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
  358. conv_f = UDF_SB(sb)->s_nls_map->char2uni;
  359. } else
  360. BUG();
  361. return udf_name_to_CS0(dname, dlen, sname, slen, conv_f);
  362. }