blob: 695389a4fc239f245cfacfa5a1e2fde9eae5b4a7 [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
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060031static int udf_uni2char_utf8(wchar_t uni,
32 unsigned char *out,
33 int boundlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060035 int u_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060037 if (boundlen <= 0)
38 return -ENAMETOOLONG;
39
40 if (uni < 0x80) {
41 out[u_len++] = (unsigned char)uni;
42 } else if (uni < 0x800) {
43 if (boundlen < 2)
44 return -ENAMETOOLONG;
45 out[u_len++] = (unsigned char)(0xc0 | (uni >> 6));
46 out[u_len++] = (unsigned char)(0x80 | (uni & 0x3f));
47 } else {
48 if (boundlen < 3)
49 return -ENAMETOOLONG;
50 out[u_len++] = (unsigned char)(0xe0 | (uni >> 12));
51 out[u_len++] = (unsigned char)(0x80 | ((uni >> 6) & 0x3f));
52 out[u_len++] = (unsigned char)(0x80 | (uni & 0x3f));
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060054 return u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060057static int udf_char2uni_utf8(const unsigned char *in,
58 int boundlen,
59 wchar_t *uni)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060061 unsigned int utf_char;
62 unsigned char c;
63 int utf_cnt, u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060065 utf_char = 0;
66 utf_cnt = 0;
67 for (u_len = 0; u_len < boundlen;) {
68 c = in[u_len++];
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 /* Complete a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070071 if (utf_cnt) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060072 utf_char = (utf_char << 6) | (c & 0x3f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (--utf_cnt)
74 continue;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070075 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 /* Check for a multi-byte UTF-8 character */
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060077 if (c & 0x80) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 /* Start a multi-byte UTF-8 character */
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060079 if ((c & 0xe0) == 0xc0) {
80 utf_char = c & 0x1f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 utf_cnt = 1;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060082 } else if ((c & 0xf0) == 0xe0) {
83 utf_char = c & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 utf_cnt = 2;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060085 } else if ((c & 0xf8) == 0xf0) {
86 utf_char = c & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 utf_cnt = 3;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060088 } else if ((c & 0xfc) == 0xf8) {
89 utf_char = c & 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 utf_cnt = 4;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060091 } else if ((c & 0xfe) == 0xfc) {
92 utf_char = c & 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 utf_cnt = 5;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070094 } else {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060095 utf_cnt = -1;
96 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 continue;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070099 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /* Single byte UTF-8 character (most common) */
101 utf_char = c;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600104 *uni = utf_char;
105 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700107 if (utf_cnt) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600108 *uni = '?';
109 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600111 return u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600114#define ILLEGAL_CHAR_MARK '_'
115#define EXT_MARK '.'
116#define CRC_MARK '#'
117#define EXT_SIZE 5
118/* Number of chars we need to store generated CRC to make filename unique */
119#define CRC_LEN 5
120
121static int udf_name_conv_char(uint8_t *str_o, int str_o_max_len,
122 int *str_o_idx,
123 const uint8_t *str_i, int str_i_max_len,
124 int *str_i_idx,
125 int u_ch, int *needsCRC,
126 int (*conv_f)(wchar_t, unsigned char *, int),
127 int translate)
128{
129 uint32_t c;
130 int illChar = 0;
131 int len, gotch = 0;
132
133 for (; (!gotch) && (*str_i_idx < str_i_max_len); *str_i_idx += u_ch) {
134 if (*str_o_idx >= str_o_max_len) {
135 *needsCRC = 1;
136 return gotch;
137 }
138
139 /* Expand OSTA compressed Unicode to Unicode */
140 c = str_i[*str_i_idx];
141 if (u_ch > 1)
142 c = (c << 8) | str_i[*str_i_idx + 1];
143
144 if (translate && (c == '/' || c == 0))
145 illChar = 1;
146 else if (illChar)
147 break;
148 else
149 gotch = 1;
150 }
151 if (illChar) {
152 *needsCRC = 1;
153 c = ILLEGAL_CHAR_MARK;
154 gotch = 1;
155 }
156 if (gotch) {
157 len = conv_f(c, &str_o[*str_o_idx], str_o_max_len - *str_o_idx);
158 /* Valid character? */
159 if (len >= 0)
160 *str_o_idx += len;
161 else if (len == -ENAMETOOLONG) {
162 *needsCRC = 1;
163 gotch = 0;
164 } else {
165 str_o[(*str_o_idx)++] = '?';
166 *needsCRC = 1;
167 }
168 }
169 return gotch;
170}
171
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600172static int udf_name_from_CS0(uint8_t *str_o, int str_max_len,
173 const uint8_t *ocu, int ocu_len,
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600174 int (*conv_f)(wchar_t, unsigned char *, int),
175 int translate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600177 uint32_t c;
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600178 uint8_t cmp_id;
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600179 int idx, len;
180 int u_ch;
181 int needsCRC = 0;
182 int ext_i_len, ext_max_len;
183 int str_o_len = 0; /* Length of resulting output */
184 int ext_o_len = 0; /* Extension output length */
185 int ext_crc_len = 0; /* Extension output length if used with CRC */
186 int i_ext = -1; /* Extension position in input buffer */
187 int o_crc = 0; /* Rightmost possible output pos for CRC+ext */
188 unsigned short valueCRC;
189 uint8_t ext[EXT_SIZE * NLS_MAX_CHARSET_SIZE + 1];
190 uint8_t crc[CRC_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600192 if (str_max_len <= 0)
193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700195 if (ocu_len == 0) {
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600196 memset(str_o, 0, str_max_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 0;
198 }
199
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600200 cmp_id = ocu[0];
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100201 if (cmp_id != 8 && cmp_id != 16) {
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600202 memset(str_o, 0, str_max_len);
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600203 pr_err("unknown compression code (%d)\n", cmp_id);
204 return -EINVAL;
205 }
206 u_ch = cmp_id >> 3;
207
208 ocu++;
209 ocu_len--;
210
211 if (ocu_len % u_ch) {
212 pr_err("incorrect filename length (%d)\n", ocu_len + 1);
Fabian Frederick78fc2e62015-04-08 21:23:55 +0200213 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600216 if (translate) {
217 /* Look for extension */
218 for (idx = ocu_len - u_ch, ext_i_len = 0;
219 (idx >= 0) && (ext_i_len < EXT_SIZE);
220 idx -= u_ch, ext_i_len++) {
221 c = ocu[idx];
222 if (u_ch > 1)
223 c = (c << 8) | ocu[idx + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600225 if (c == EXT_MARK) {
226 if (ext_i_len)
227 i_ext = idx;
228 break;
229 }
230 }
231 if (i_ext >= 0) {
232 /* Convert extension */
233 ext_max_len = min_t(int, sizeof(ext), str_max_len);
234 ext[ext_o_len++] = EXT_MARK;
235 idx = i_ext + u_ch;
236 while (udf_name_conv_char(ext, ext_max_len, &ext_o_len,
237 ocu, ocu_len, &idx,
238 u_ch, &needsCRC,
239 conv_f, translate)) {
240 if ((ext_o_len + CRC_LEN) < str_max_len)
241 ext_crc_len = ext_o_len;
242 }
243 }
244 }
245
246 idx = 0;
247 while (1) {
248 if (translate && (idx == i_ext)) {
249 if (str_o_len > (str_max_len - ext_o_len))
250 needsCRC = 1;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600251 break;
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600252 }
253
254 if (!udf_name_conv_char(str_o, str_max_len, &str_o_len,
255 ocu, ocu_len, &idx,
256 u_ch, &needsCRC, conv_f, translate))
257 break;
258
259 if (translate &&
260 (str_o_len <= (str_max_len - ext_o_len - CRC_LEN)))
261 o_crc = str_o_len;
262 }
263
264 if (translate) {
265 if (str_o_len <= 2 && str_o[0] == '.' &&
266 (str_o_len == 1 || str_o[1] == '.'))
267 needsCRC = 1;
268 if (needsCRC) {
269 str_o_len = o_crc;
270 valueCRC = crc_itu_t(0, ocu, ocu_len);
271 crc[0] = CRC_MARK;
272 crc[1] = hex_asc_upper_hi(valueCRC >> 8);
273 crc[2] = hex_asc_upper_lo(valueCRC >> 8);
274 crc[3] = hex_asc_upper_hi(valueCRC);
275 crc[4] = hex_asc_upper_lo(valueCRC);
276 len = min_t(int, CRC_LEN, str_max_len - str_o_len);
277 memcpy(&str_o[str_o_len], crc, len);
278 str_o_len += len;
279 ext_o_len = ext_crc_len;
280 }
281 if (ext_o_len > 0) {
282 memcpy(&str_o[str_o_len], ext, ext_o_len);
283 str_o_len += ext_o_len;
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600287 return str_o_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600290static int udf_name_to_CS0(uint8_t *ocu, int ocu_max_len,
291 const uint8_t *str_i, int str_len,
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600292 int (*conv_f)(const unsigned char *, int, wchar_t *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600294 int i, len;
295 unsigned int max_val;
296 wchar_t uni_char;
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600297 int u_len, u_ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600299 if (ocu_max_len <= 0)
300 return 0;
301
302 memset(ocu, 0, ocu_max_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 ocu[0] = 8;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600304 max_val = 0xff;
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600305 u_ch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700307try_again:
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600308 u_len = 1;
309 for (i = 0; i < str_len; i++) {
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600310 /* Name didn't fit? */
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600311 if (u_len + u_ch > ocu_max_len)
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600312 return 0;
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600313 len = conv_f(&str_i[i], str_len - i, &uni_char);
Jan Kara59285c22009-02-04 19:46:11 +0100314 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 continue;
Jan Kara59285c22009-02-04 19:46:11 +0100316 /* Invalid character, deal with it */
317 if (len < 0) {
318 len = 1;
319 uni_char = '?';
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700322 if (uni_char > max_val) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600323 max_val = 0xffff;
324 ocu[0] = 0x10;
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600325 u_ch = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 goto try_again;
327 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700328
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600329 if (max_val == 0xffff)
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600330 ocu[u_len++] = (uint8_t)(uni_char >> 8);
331 ocu[u_len++] = (uint8_t)(uni_char & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 i += len - 1;
333 }
334
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600335 return u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Andrew Gabbasovc26f6c62016-04-25 06:19:38 -0500338int udf_dstrCS0toUTF8(uint8_t *utf_o, int o_len,
339 const uint8_t *ocu_i, int i_len)
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600340{
Andrew Gabbasovc26f6c62016-04-25 06:19:38 -0500341 int s_len = 0;
342
343 if (i_len > 0) {
344 s_len = ocu_i[i_len - 1];
345 if (s_len >= i_len) {
346 pr_err("incorrect dstring lengths (%d/%d)\n",
347 s_len, i_len);
348 return -EINVAL;
349 }
350 }
351
352 return udf_name_from_CS0(utf_o, o_len, ocu_i, s_len,
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600353 udf_uni2char_utf8, 0);
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600354}
355
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600356int udf_get_filename(struct super_block *sb, const uint8_t *sname, int slen,
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100357 uint8_t *dname, int dlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600359 int (*conv_f)(wchar_t, unsigned char *, int);
Fabian Frederick6ce63832015-04-08 21:23:57 +0200360 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Fabian Frederick31f25662015-04-08 21:23:52 +0200362 if (!slen)
363 return -EIO;
364
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600365 if (dlen <= 0)
366 return 0;
367
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700368 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600369 conv_f = udf_uni2char_utf8;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700370 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600371 conv_f = UDF_SB(sb)->s_nls_map->uni2char;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800372 } else
Fabian Frederick5dce54b2015-04-08 21:23:56 +0200373 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Andrew Gabbasov484a10f2016-01-15 02:44:23 -0600375 ret = udf_name_from_CS0(dname, dlen, sname, slen, conv_f, 1);
Fabian Frederick6ce63832015-04-08 21:23:57 +0200376 /* Zero length filename isn't valid... */
377 if (ret == 0)
378 ret = -EINVAL;
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200379 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Andrew Gabbasov525e2c52016-01-15 02:44:19 -0600382int udf_put_filename(struct super_block *sb, const uint8_t *sname, int slen,
383 uint8_t *dname, int dlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600385 int (*conv_f)(const unsigned char *, int, wchar_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700387 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600388 conv_f = udf_char2uni_utf8;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700389 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600390 conv_f = UDF_SB(sb)->s_nls_map->char2uni;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800391 } else
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600392 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600394 return udf_name_to_CS0(dname, dlen, sname, slen, conv_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396