unicode.c 9.0 KB

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