unicode.c 9.0 KB

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