]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/udf/unicode.c
udf: improve error management in udf_CS0toUTF8()
[mirror_ubuntu-bionic-kernel.git] / fs / udf / unicode.c
CommitLineData
1da177e4
LT
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 *
1da177e4
LT
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
21#include "udfdecl.h"
22
23#include <linux/kernel.h>
24#include <linux/string.h> /* for memset */
25#include <linux/nls.h>
f845fced 26#include <linux/crc-itu-t.h>
5a0e3ad6 27#include <linux/slab.h>
1da177e4
LT
28
29#include "udf_sb.h"
30
0e5cc9a4
JK
31static int udf_translate_to_linux(uint8_t *, int, uint8_t *, int, uint8_t *,
32 int);
1da177e4 33
28de7948 34static int udf_char_to_ustr(struct ustr *dest, const uint8_t *src, int strlen)
1da177e4 35{
cb00ea35 36 if ((!dest) || (!src) || (!strlen) || (strlen > UDF_NAME_LEN - 2))
1da177e4 37 return 0;
28de7948 38
1da177e4
LT
39 memset(dest, 0, sizeof(struct ustr));
40 memcpy(dest->u_name, src, strlen);
41 dest->u_cmpID = 0x08;
42 dest->u_len = strlen;
28de7948 43
1da177e4
LT
44 return strlen;
45}
46
47/*
48 * udf_build_ustr
49 */
28de7948 50int udf_build_ustr(struct ustr *dest, dstring *ptr, int size)
1da177e4
LT
51{
52 int usesize;
53
6305a0a9 54 if (!dest || !ptr || !size)
1da177e4 55 return -1;
6305a0a9 56 BUG_ON(size < 2);
1da177e4 57
6305a0a9
MS
58 usesize = min_t(size_t, ptr[size - 1], sizeof(dest->u_name));
59 usesize = min(usesize, size - 2);
cb00ea35 60 dest->u_cmpID = ptr[0];
6305a0a9
MS
61 dest->u_len = usesize;
62 memcpy(dest->u_name, ptr + 1, usesize);
63 memset(dest->u_name + usesize, 0, sizeof(dest->u_name) - usesize);
28de7948 64
1da177e4
LT
65 return 0;
66}
67
68/*
69 * udf_build_ustr_exact
70 */
31f2566f 71static void udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
1da177e4 72{
1da177e4 73 memset(dest, 0, sizeof(struct ustr));
cb00ea35
CG
74 dest->u_cmpID = ptr[0];
75 dest->u_len = exactsize - 1;
76 memcpy(dest->u_name, ptr + 1, exactsize - 1);
1da177e4
LT
77}
78
79/*
d67e4a48 80 * udf_CS0toUTF8
1da177e4
LT
81 *
82 * PURPOSE
83 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
84 *
1da177e4
LT
85 * PRE-CONDITIONS
86 * utf Pointer to UTF-8 output buffer.
87 * ocu Pointer to OSTA Compressed Unicode input buffer
88 * of size UDF_NAME_LEN bytes.
89 * both of type "struct ustr *"
90 *
91 * POST-CONDITIONS
e9d4cf41 92 * <return> >= 0 on success.
1da177e4
LT
93 *
94 * HISTORY
95 * November 12, 1997 - Andrew E. Mileski
96 * Written, tested, and released.
97 */
79cfe0ff 98int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
1da177e4 99{
79cfe0ff 100 const uint8_t *ocu;
1da177e4
LT
101 uint8_t cmp_id, ocu_len;
102 int i;
103
1da177e4 104 ocu_len = ocu_i->u_len;
cb00ea35 105 if (ocu_len == 0) {
1da177e4 106 memset(utf_o, 0, sizeof(struct ustr));
1da177e4
LT
107 return 0;
108 }
109
79cfe0ff 110 cmp_id = ocu_i->u_cmpID;
111 if (cmp_id != 8 && cmp_id != 16) {
112 memset(utf_o, 0, sizeof(struct ustr));
78ace70c 113 pr_err("unknown compression code (%d) stri=%s\n",
cb00ea35 114 cmp_id, ocu_i->u_name);
e9d4cf41 115 return -EINVAL;
1da177e4
LT
116 }
117
79cfe0ff 118 ocu = ocu_i->u_name;
119 utf_o->u_len = 0;
cb00ea35 120 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
1da177e4
LT
121
122 /* Expand OSTA compressed Unicode to Unicode */
79cfe0ff 123 uint32_t c = ocu[i++];
1da177e4
LT
124 if (cmp_id == 16)
125 c = (c << 8) | ocu[i++];
126
127 /* Compress Unicode to UTF-8 */
79cfe0ff 128 if (c < 0x80U)
28de7948 129 utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
79cfe0ff 130 else if (c < 0x800U) {
4b11111a
MS
131 utf_o->u_name[utf_o->u_len++] =
132 (uint8_t)(0xc0 | (c >> 6));
133 utf_o->u_name[utf_o->u_len++] =
134 (uint8_t)(0x80 | (c & 0x3f));
cb00ea35 135 } else {
4b11111a
MS
136 utf_o->u_name[utf_o->u_len++] =
137 (uint8_t)(0xe0 | (c >> 12));
138 utf_o->u_name[utf_o->u_len++] =
139 (uint8_t)(0x80 |
140 ((c >> 6) & 0x3f));
141 utf_o->u_name[utf_o->u_len++] =
142 (uint8_t)(0x80 | (c & 0x3f));
1da177e4
LT
143 }
144 }
cb00ea35 145 utf_o->u_cmpID = 8;
1da177e4
LT
146
147 return utf_o->u_len;
148}
149
150/*
151 *
d67e4a48 152 * udf_UTF8toCS0
1da177e4
LT
153 *
154 * PURPOSE
155 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
156 *
157 * DESCRIPTION
158 * This routine is only called by udf_lookup().
159 *
160 * PRE-CONDITIONS
161 * ocu Pointer to OSTA Compressed Unicode output
162 * buffer of size UDF_NAME_LEN bytes.
163 * utf Pointer to UTF-8 input buffer.
164 * utf_len Length of UTF-8 input buffer in bytes.
165 *
166 * POST-CONDITIONS
167 * <return> Zero on success.
168 *
169 * HISTORY
170 * November 12, 1997 - Andrew E. Mileski
171 * Written, tested, and released.
172 */
28de7948 173static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
1da177e4
LT
174{
175 unsigned c, i, max_val, utf_char;
176 int utf_cnt, u_len;
177
178 memset(ocu, 0, sizeof(dstring) * length);
179 ocu[0] = 8;
180 max_val = 0xffU;
181
28de7948 182try_again:
1da177e4
LT
183 u_len = 0U;
184 utf_char = 0U;
185 utf_cnt = 0U;
cb00ea35 186 for (i = 0U; i < utf->u_len; i++) {
28de7948 187 c = (uint8_t)utf->u_name[i];
1da177e4
LT
188
189 /* Complete a multi-byte UTF-8 character */
cb00ea35 190 if (utf_cnt) {
1da177e4
LT
191 utf_char = (utf_char << 6) | (c & 0x3fU);
192 if (--utf_cnt)
193 continue;
cb00ea35 194 } else {
1da177e4 195 /* Check for a multi-byte UTF-8 character */
cb00ea35 196 if (c & 0x80U) {
1da177e4 197 /* Start a multi-byte UTF-8 character */
cb00ea35 198 if ((c & 0xe0U) == 0xc0U) {
1da177e4
LT
199 utf_char = c & 0x1fU;
200 utf_cnt = 1;
cb00ea35 201 } else if ((c & 0xf0U) == 0xe0U) {
1da177e4
LT
202 utf_char = c & 0x0fU;
203 utf_cnt = 2;
cb00ea35 204 } else if ((c & 0xf8U) == 0xf0U) {
1da177e4
LT
205 utf_char = c & 0x07U;
206 utf_cnt = 3;
cb00ea35 207 } else if ((c & 0xfcU) == 0xf8U) {
1da177e4
LT
208 utf_char = c & 0x03U;
209 utf_cnt = 4;
cb00ea35 210 } else if ((c & 0xfeU) == 0xfcU) {
1da177e4
LT
211 utf_char = c & 0x01U;
212 utf_cnt = 5;
28de7948 213 } else {
1da177e4 214 goto error_out;
28de7948 215 }
1da177e4 216 continue;
28de7948 217 } else {
1da177e4
LT
218 /* Single byte UTF-8 character (most common) */
219 utf_char = c;
28de7948 220 }
1da177e4
LT
221 }
222
223 /* Choose no compression if necessary */
cb00ea35 224 if (utf_char > max_val) {
28de7948 225 if (max_val == 0xffU) {
1da177e4 226 max_val = 0xffffU;
28de7948 227 ocu[0] = (uint8_t)0x10U;
1da177e4
LT
228 goto try_again;
229 }
230 goto error_out;
231 }
232
4b11111a 233 if (max_val == 0xffffU)
28de7948 234 ocu[++u_len] = (uint8_t)(utf_char >> 8);
28de7948 235 ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
1da177e4
LT
236 }
237
cb00ea35 238 if (utf_cnt) {
28de7948 239error_out:
1da177e4 240 ocu[++u_len] = '?';
78ace70c 241 printk(KERN_DEBUG pr_fmt("bad UTF-8 character\n"));
1da177e4
LT
242 }
243
28de7948
CG
244 ocu[length - 1] = (uint8_t)u_len + 1;
245
1da177e4
LT
246 return u_len + 1;
247}
248
cb00ea35 249static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
34f953dd 250 const struct ustr *ocu_i)
1da177e4 251{
34f953dd 252 const uint8_t *ocu;
1da177e4 253 uint8_t cmp_id, ocu_len;
59285c28 254 int i, len;
1da177e4 255
1da177e4
LT
256
257 ocu_len = ocu_i->u_len;
cb00ea35 258 if (ocu_len == 0) {
1da177e4 259 memset(utf_o, 0, sizeof(struct ustr));
1da177e4
LT
260 return 0;
261 }
262
34f953dd 263 cmp_id = ocu_i->u_cmpID;
264 if (cmp_id != 8 && cmp_id != 16) {
265 memset(utf_o, 0, sizeof(struct ustr));
78ace70c 266 pr_err("unknown compression code (%d) stri=%s\n",
cb00ea35 267 cmp_id, ocu_i->u_name);
1da177e4
LT
268 return 0;
269 }
270
34f953dd 271 ocu = ocu_i->u_name;
272 utf_o->u_len = 0;
cb00ea35 273 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
1da177e4 274 /* Expand OSTA compressed Unicode to Unicode */
34f953dd 275 uint32_t c = ocu[i++];
1da177e4
LT
276 if (cmp_id == 16)
277 c = (c << 8) | ocu[i++];
278
59285c28
JK
279 len = nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
280 UDF_NAME_LEN - utf_o->u_len);
281 /* Valid character? */
282 if (len >= 0)
283 utf_o->u_len += len;
284 else
285 utf_o->u_name[utf_o->u_len++] = '?';
1da177e4 286 }
cb00ea35 287 utf_o->u_cmpID = 8;
1da177e4
LT
288
289 return utf_o->u_len;
290}
291
28de7948 292static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
cb00ea35 293 int length)
1da177e4 294{
59285c28
JK
295 int len;
296 unsigned i, max_val;
1da177e4
LT
297 uint16_t uni_char;
298 int u_len;
299
300 memset(ocu, 0, sizeof(dstring) * length);
301 ocu[0] = 8;
302 max_val = 0xffU;
303
28de7948 304try_again:
1da177e4 305 u_len = 0U;
cb00ea35
CG
306 for (i = 0U; i < uni->u_len; i++) {
307 len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
59285c28 308 if (!len)
1da177e4 309 continue;
59285c28
JK
310 /* Invalid character, deal with it */
311 if (len < 0) {
312 len = 1;
313 uni_char = '?';
314 }
1da177e4 315
cb00ea35 316 if (uni_char > max_val) {
1da177e4 317 max_val = 0xffffU;
28de7948 318 ocu[0] = (uint8_t)0x10U;
1da177e4
LT
319 goto try_again;
320 }
cb00ea35 321
1da177e4 322 if (max_val == 0xffffU)
28de7948
CG
323 ocu[++u_len] = (uint8_t)(uni_char >> 8);
324 ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
1da177e4
LT
325 i += len - 1;
326 }
327
28de7948 328 ocu[length - 1] = (uint8_t)u_len + 1;
1da177e4
LT
329 return u_len + 1;
330}
331
0e5cc9a4
JK
332int udf_get_filename(struct super_block *sb, uint8_t *sname, int slen,
333 uint8_t *dname, int dlen)
1da177e4 334{
530f1a5e 335 struct ustr *filename, *unifilename;
5ceb8b55 336 int ret = 0;
1da177e4 337
31f2566f
FF
338 if (!slen)
339 return -EIO;
340
530f1a5e
MS
341 filename = kmalloc(sizeof(struct ustr), GFP_NOFS);
342 if (!filename)
5ceb8b55 343 return -ENOMEM;
1da177e4 344
530f1a5e 345 unifilename = kmalloc(sizeof(struct ustr), GFP_NOFS);
5ceb8b55
FF
346 if (!unifilename) {
347 ret = -ENOMEM;
530f1a5e 348 goto out1;
5ceb8b55 349 }
530f1a5e 350
31f2566f 351 udf_build_ustr_exact(unifilename, sname, slen);
cb00ea35 352 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
e9d4cf41
FF
353 ret = udf_CS0toUTF8(filename, unifilename);
354 if (ret < 0) {
4b11111a
MS
355 udf_debug("Failed in udf_get_filename: sname = %s\n",
356 sname);
530f1a5e 357 goto out2;
1da177e4 358 }
cb00ea35 359 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
530f1a5e
MS
360 if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, filename,
361 unifilename)) {
4b11111a
MS
362 udf_debug("Failed in udf_get_filename: sname = %s\n",
363 sname);
530f1a5e 364 goto out2;
1da177e4 365 }
4b11111a 366 } else
530f1a5e
MS
367 goto out2;
368
5ceb8b55 369 ret = udf_translate_to_linux(dname, dlen,
0e5cc9a4 370 filename->u_name, filename->u_len,
530f1a5e
MS
371 unifilename->u_name, unifilename->u_len);
372out2:
373 kfree(unifilename);
374out1:
375 kfree(filename);
5ceb8b55 376 return ret;
1da177e4
LT
377}
378
28de7948
CG
379int udf_put_filename(struct super_block *sb, const uint8_t *sname,
380 uint8_t *dname, int flen)
1da177e4
LT
381{
382 struct ustr unifilename;
383 int namelen;
384
4b11111a 385 if (!udf_char_to_ustr(&unifilename, sname, flen))
1da177e4 386 return 0;
1da177e4 387
cb00ea35 388 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
28de7948 389 namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
4b11111a 390 if (!namelen)
1da177e4 391 return 0;
cb00ea35 392 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
4b11111a
MS
393 namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
394 &unifilename, UDF_NAME_LEN);
395 if (!namelen)
1da177e4 396 return 0;
4b11111a 397 } else
1da177e4
LT
398 return 0;
399
400 return namelen;
401}
402
403#define ILLEGAL_CHAR_MARK '_'
28de7948
CG
404#define EXT_MARK '.'
405#define CRC_MARK '#'
406#define EXT_SIZE 5
0e5cc9a4
JK
407/* Number of chars we need to store generated CRC to make filename unique */
408#define CRC_LEN 5
1da177e4 409
0e5cc9a4
JK
410static int udf_translate_to_linux(uint8_t *newName, int newLen,
411 uint8_t *udfName, int udfLen,
412 uint8_t *fidName, int fidNameLen)
1da177e4 413{
cb00ea35 414 int index, newIndex = 0, needsCRC = 0;
1da177e4
LT
415 int extIndex = 0, newExtIndex = 0, hasExt = 0;
416 unsigned short valueCRC;
417 uint8_t curr;
1da177e4 418
28de7948
CG
419 if (udfName[0] == '.' &&
420 (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
1da177e4
LT
421 needsCRC = 1;
422 newIndex = udfLen;
423 memcpy(newName, udfName, udfLen);
cb00ea35
CG
424 } else {
425 for (index = 0; index < udfLen; index++) {
1da177e4 426 curr = udfName[index];
cb00ea35 427 if (curr == '/' || curr == 0) {
1da177e4
LT
428 needsCRC = 1;
429 curr = ILLEGAL_CHAR_MARK;
4b11111a
MS
430 while (index + 1 < udfLen &&
431 (udfName[index + 1] == '/' ||
432 udfName[index + 1] == 0))
1da177e4 433 index++;
4b11111a
MS
434 }
435 if (curr == EXT_MARK &&
436 (udfLen - index - 1) <= EXT_SIZE) {
437 if (udfLen == index + 1)
1da177e4 438 hasExt = 0;
4b11111a 439 else {
1da177e4
LT
440 hasExt = 1;
441 extIndex = index;
442 newExtIndex = newIndex;
443 }
444 }
0e5cc9a4 445 if (newIndex < newLen)
1da177e4
LT
446 newName[newIndex++] = curr;
447 else
448 needsCRC = 1;
449 }
450 }
cb00ea35 451 if (needsCRC) {
1da177e4
LT
452 uint8_t ext[EXT_SIZE];
453 int localExtIndex = 0;
454
cb00ea35 455 if (hasExt) {
1da177e4 456 int maxFilenameLen;
4b11111a
MS
457 for (index = 0;
458 index < EXT_SIZE && extIndex + index + 1 < udfLen;
459 index++) {
1da177e4
LT
460 curr = udfName[extIndex + index + 1];
461
cb00ea35 462 if (curr == '/' || curr == 0) {
1da177e4
LT
463 needsCRC = 1;
464 curr = ILLEGAL_CHAR_MARK;
4b11111a
MS
465 while (extIndex + index + 2 < udfLen &&
466 (index + 1 < EXT_SIZE &&
467 (udfName[extIndex + index + 2] == '/' ||
468 udfName[extIndex + index + 2] == 0)))
1da177e4
LT
469 index++;
470 }
471 ext[localExtIndex++] = curr;
472 }
0e5cc9a4 473 maxFilenameLen = newLen - CRC_LEN - localExtIndex;
1da177e4
LT
474 if (newIndex > maxFilenameLen)
475 newIndex = maxFilenameLen;
476 else
477 newIndex = newExtIndex;
0e5cc9a4
JK
478 } else if (newIndex > newLen - CRC_LEN)
479 newIndex = newLen - CRC_LEN;
1da177e4 480 newName[newIndex++] = CRC_MARK;
f845fced 481 valueCRC = crc_itu_t(0, fidName, fidNameLen);
c7ff4821
AS
482 newName[newIndex++] = hex_asc_upper_hi(valueCRC >> 8);
483 newName[newIndex++] = hex_asc_upper_lo(valueCRC >> 8);
484 newName[newIndex++] = hex_asc_upper_hi(valueCRC);
485 newName[newIndex++] = hex_asc_upper_lo(valueCRC);
1da177e4 486
cb00ea35 487 if (hasExt) {
1da177e4 488 newName[newIndex++] = EXT_MARK;
cb00ea35 489 for (index = 0; index < localExtIndex; index++)
1da177e4
LT
490 newName[newIndex++] = ext[index];
491 }
492 }
28de7948 493
1da177e4
LT
494 return newIndex;
495}