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