blob: 18df831afd3de0a66c08426a306f3a0965938847 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 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
21#include "udfdecl.h"
22
23#include <linux/kernel.h>
24#include <linux/string.h> /* for memset */
25#include <linux/nls.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020026#include <linux/crc-itu-t.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include "udf_sb.h"
30
Jan Karab8a41c42018-04-12 17:06:18 +020031#define UNICODE_MAX 0x10ffff
Jan Kara44f06ba2018-04-12 17:22:23 +020032#define SURROGATE_MASK 0xfffff800
33#define SURROGATE_PAIR 0x0000d800
34
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060035static int udf_uni2char_utf8(wchar_t uni,
36 unsigned char *out,
37 int boundlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060039 int u_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060041 if (boundlen <= 0)
42 return -ENAMETOOLONG;
43
Jan Karab8a41c42018-04-12 17:06:18 +020044 u_len = utf32_to_utf8(uni, out, boundlen);
45 if (u_len < 0) {
46 if (uni > UNICODE_MAX ||
47 (uni & SURROGATE_MASK) == SURROGATE_PAIR)
48 return -EINVAL;
49 return -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060051 return u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060054static int udf_char2uni_utf8(const unsigned char *in,
55 int boundlen,
56 wchar_t *uni)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Jan Karab8a41c42018-04-12 17:06:18 +020058 int u_len;
59 unicode_t c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Jan Karab8a41c42018-04-12 17:06:18 +020061 u_len = utf8_to_utf32(in, boundlen, &c);
62 if (u_len < 0) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060063 *uni = '?';
64 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
Jan Karab8a41c42018-04-12 17:06:18 +020066
67 if (c > MAX_WCHAR_T)
68 *uni = '?';
69 else
70 *uni = c;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060071 return u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Andrew Gabbasov484a10f2016-01-15 02:44:23 -060074#define ILLEGAL_CHAR_MARK '_'
75#define EXT_MARK '.'
76#define CRC_MARK '#'
77#define EXT_SIZE 5
78/* Number of chars we need to store generated CRC to make filename unique */
79#define CRC_LEN 5
80
81static int udf_name_conv_char(uint8_t *str_o, int str_o_max_len,
82 int *str_o_idx,
83 const uint8_t *str_i, int str_i_max_len,
84 int *str_i_idx,
85 int u_ch, int *needsCRC,
86 int (*conv_f)(wchar_t, unsigned char *, int),
87 int translate)
88{
89 uint32_t c;
90 int illChar = 0;
91 int len, gotch = 0;
92
93 for (; (!gotch) && (*str_i_idx < str_i_max_len); *str_i_idx += u_ch) {
94 if (*str_o_idx >= str_o_max_len) {
95 *needsCRC = 1;
96 return gotch;
97 }
98
99 /* Expand OSTA compressed Unicode to Unicode */
100 c = str_i[*str_i_idx];
101 if (u_ch > 1)
102 c = (c << 8) | str_i[*str_i_idx + 1];
103
104 if (translate && (c == '/' || c == 0))
105 illChar = 1;
106 else if (illChar)
107 break;
108 else
109 gotch = 1;
110 }
111 if (illChar) {
112 *needsCRC = 1;
113 c = ILLEGAL_CHAR_MARK;
114 gotch = 1;
115 }
116 if (gotch) {
117 len = conv_f(c, &str_o[*str_o_idx], str_o_max_len - *str_o_idx);
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)++] = '?';
126 *needsCRC = 1;
127 }
128 }
129 return gotch;
130}
131
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600132static int udf_name_from_CS0(uint8_t *str_o, int str_max_len,
133 const uint8_t *ocu, int ocu_len,
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600134 int (*conv_f)(wchar_t, unsigned char *, int),
135 int translate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600137 uint32_t c;
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600138 uint8_t cmp_id;
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600139 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];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600152 if (str_max_len <= 0)
153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700155 if (ocu_len == 0) {
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600156 memset(str_o, 0, str_max_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return 0;
158 }
159
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600160 cmp_id = ocu[0];
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100161 if (cmp_id != 8 && cmp_id != 16) {
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600162 memset(str_o, 0, str_max_len);
Steve Magnanifcbf7632017-10-12 08:48:41 -0500163 pr_err("unknown compression code (%u)\n", cmp_id);
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600164 return -EINVAL;
165 }
166 u_ch = cmp_id >> 3;
167
168 ocu++;
169 ocu_len--;
170
171 if (ocu_len % u_ch) {
172 pr_err("incorrect filename length (%d)\n", ocu_len + 1);
Fabian Frederick78fc2e62015-04-08 21:23:55 +0200173 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600176 if (translate) {
177 /* Look for extension */
178 for (idx = ocu_len - u_ch, ext_i_len = 0;
179 (idx >= 0) && (ext_i_len < EXT_SIZE);
180 idx -= u_ch, ext_i_len++) {
181 c = ocu[idx];
182 if (u_ch > 1)
183 c = (c << 8) | ocu[idx + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600185 if (c == EXT_MARK) {
186 if (ext_i_len)
187 i_ext = idx;
188 break;
189 }
190 }
191 if (i_ext >= 0) {
192 /* Convert extension */
193 ext_max_len = min_t(int, sizeof(ext), str_max_len);
194 ext[ext_o_len++] = EXT_MARK;
195 idx = i_ext + u_ch;
196 while (udf_name_conv_char(ext, ext_max_len, &ext_o_len,
197 ocu, ocu_len, &idx,
198 u_ch, &needsCRC,
199 conv_f, translate)) {
200 if ((ext_o_len + CRC_LEN) < str_max_len)
201 ext_crc_len = ext_o_len;
202 }
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;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600211 break;
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600212 }
213
214 if (!udf_name_conv_char(str_o, str_max_len, &str_o_len,
215 ocu, ocu_len, &idx,
216 u_ch, &needsCRC, conv_f, translate))
217 break;
218
219 if (translate &&
220 (str_o_len <= (str_max_len - ext_o_len - CRC_LEN)))
221 o_crc = str_o_len;
222 }
223
224 if (translate) {
225 if (str_o_len <= 2 && str_o[0] == '.' &&
226 (str_o_len == 1 || str_o[1] == '.'))
227 needsCRC = 1;
228 if (needsCRC) {
229 str_o_len = o_crc;
230 valueCRC = crc_itu_t(0, ocu, ocu_len);
231 crc[0] = CRC_MARK;
232 crc[1] = hex_asc_upper_hi(valueCRC >> 8);
233 crc[2] = hex_asc_upper_lo(valueCRC >> 8);
234 crc[3] = hex_asc_upper_hi(valueCRC);
235 crc[4] = hex_asc_upper_lo(valueCRC);
236 len = min_t(int, CRC_LEN, str_max_len - str_o_len);
237 memcpy(&str_o[str_o_len], crc, len);
238 str_o_len += len;
239 ext_o_len = ext_crc_len;
240 }
241 if (ext_o_len > 0) {
242 memcpy(&str_o[str_o_len], ext, ext_o_len);
243 str_o_len += ext_o_len;
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600247 return str_o_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600250static int udf_name_to_CS0(uint8_t *ocu, int ocu_max_len,
251 const uint8_t *str_i, int str_len,
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600252 int (*conv_f)(const unsigned char *, int, wchar_t *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600254 int i, len;
255 unsigned int max_val;
256 wchar_t uni_char;
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600257 int u_len, u_ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600259 if (ocu_max_len <= 0)
260 return 0;
261
262 memset(ocu, 0, ocu_max_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 ocu[0] = 8;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600264 max_val = 0xff;
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600265 u_ch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700267try_again:
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600268 u_len = 1;
269 for (i = 0; i < str_len; i++) {
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600270 /* Name didn't fit? */
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600271 if (u_len + u_ch > ocu_max_len)
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600272 return 0;
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600273 len = conv_f(&str_i[i], str_len - i, &uni_char);
Jan Kara59285c22009-02-04 19:46:11 +0100274 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 continue;
Jan Kara59285c22009-02-04 19:46:11 +0100276 /* Invalid character, deal with it */
277 if (len < 0) {
278 len = 1;
279 uni_char = '?';
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700282 if (uni_char > max_val) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600283 max_val = 0xffff;
284 ocu[0] = 0x10;
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600285 u_ch = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto try_again;
287 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700288
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600289 if (max_val == 0xffff)
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600290 ocu[u_len++] = (uint8_t)(uni_char >> 8);
291 ocu[u_len++] = (uint8_t)(uni_char & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 i += len - 1;
293 }
294
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600295 return u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Andrew Gabbasovc26f6c62016-04-25 06:19:38 -0500298int udf_dstrCS0toUTF8(uint8_t *utf_o, int o_len,
299 const uint8_t *ocu_i, int i_len)
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600300{
Andrew Gabbasovc26f6c62016-04-25 06:19:38 -0500301 int s_len = 0;
302
303 if (i_len > 0) {
304 s_len = ocu_i[i_len - 1];
305 if (s_len >= i_len) {
306 pr_err("incorrect dstring lengths (%d/%d)\n",
307 s_len, i_len);
308 return -EINVAL;
309 }
310 }
311
312 return udf_name_from_CS0(utf_o, o_len, ocu_i, s_len,
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600313 udf_uni2char_utf8, 0);
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600314}
315
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600316int udf_get_filename(struct super_block *sb, const uint8_t *sname, int slen,
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100317 uint8_t *dname, int dlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600319 int (*conv_f)(wchar_t, unsigned char *, int);
Fabian Frederick6ce63832015-04-08 21:23:57 +0200320 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Fabian Frederick31f25662015-04-08 21:23:52 +0200322 if (!slen)
323 return -EIO;
324
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600325 if (dlen <= 0)
326 return 0;
327
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700328 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600329 conv_f = udf_uni2char_utf8;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700330 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600331 conv_f = UDF_SB(sb)->s_nls_map->uni2char;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800332 } else
Fabian Frederick5dce54b2015-04-08 21:23:56 +0200333 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600335 ret = udf_name_from_CS0(dname, dlen, sname, slen, conv_f, 1);
Fabian Frederick6ce63832015-04-08 21:23:57 +0200336 /* Zero length filename isn't valid... */
337 if (ret == 0)
338 ret = -EINVAL;
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200339 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Andrew Gabbasov525e2c52016-01-15 02:44:19 -0600342int udf_put_filename(struct super_block *sb, const uint8_t *sname, int slen,
343 uint8_t *dname, int dlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600345 int (*conv_f)(const unsigned char *, int, wchar_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700347 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600348 conv_f = udf_char2uni_utf8;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700349 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600350 conv_f = UDF_SB(sb)->s_nls_map->char2uni;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800351 } else
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600352 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600354 return udf_name_to_CS0(dname, dlen, sname, slen, conv_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356