unicode.c 8.8 KB

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