unicode.c 9.2 KB

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