5 * Routines for converting between UTF-8 and OSTA Compressed Unicode.
6 * Also handles filename mangling
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
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.
23 #include <linux/kernel.h>
24 #include <linux/string.h> /* for memset */
25 #include <linux/nls.h>
26 #include <linux/crc-itu-t.h>
27 #include <linux/slab.h>
31 static int udf_translate_to_linux(uint8_t *, int, uint8_t *, int, uint8_t *,
34 static int udf_char_to_ustr(struct ustr
*dest
, const uint8_t *src
, int strlen
)
36 if ((!dest
) || (!src
) || (!strlen
) || (strlen
> UDF_NAME_LEN
- 2))
39 memset(dest
, 0, sizeof(struct ustr
));
40 memcpy(dest
->u_name
, src
, strlen
);
50 int udf_build_ustr(struct ustr
*dest
, dstring
*ptr
, int size
)
54 if (!dest
|| !ptr
|| !size
)
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
);
69 * udf_build_ustr_exact
71 static int udf_build_ustr_exact(struct ustr
*dest
, dstring
*ptr
, int exactsize
)
73 if ((!dest
) || (!ptr
) || (!exactsize
))
76 memset(dest
, 0, sizeof(struct ustr
));
77 dest
->u_cmpID
= ptr
[0];
78 dest
->u_len
= exactsize
- 1;
79 memcpy(dest
->u_name
, ptr
+ 1, exactsize
- 1);
88 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
91 * utf Pointer to UTF-8 output buffer.
92 * ocu Pointer to OSTA Compressed Unicode input buffer
93 * of size UDF_NAME_LEN bytes.
94 * both of type "struct ustr *"
97 * <return> Zero on success.
100 * November 12, 1997 - Andrew E. Mileski
101 * Written, tested, and released.
103 int udf_CS0toUTF8(struct ustr
*utf_o
, const struct ustr
*ocu_i
)
106 uint8_t cmp_id
, ocu_len
;
109 ocu_len
= ocu_i
->u_len
;
111 memset(utf_o
, 0, sizeof(struct ustr
));
115 cmp_id
= ocu_i
->u_cmpID
;
116 if (cmp_id
!= 8 && cmp_id
!= 16) {
117 memset(utf_o
, 0, sizeof(struct ustr
));
118 pr_err("unknown compression code (%d) stri=%s\n",
119 cmp_id
, ocu_i
->u_name
);
125 for (i
= 0; (i
< ocu_len
) && (utf_o
->u_len
<= (UDF_NAME_LEN
- 3));) {
127 /* Expand OSTA compressed Unicode to Unicode */
128 uint32_t c
= ocu
[i
++];
130 c
= (c
<< 8) | ocu
[i
++];
132 /* Compress Unicode to UTF-8 */
134 utf_o
->u_name
[utf_o
->u_len
++] = (uint8_t)c
;
135 else if (c
< 0x800U
) {
136 if (utf_o
->u_len
> (UDF_NAME_LEN
- 4))
138 utf_o
->u_name
[utf_o
->u_len
++] =
139 (uint8_t)(0xc0 | (c
>> 6));
140 utf_o
->u_name
[utf_o
->u_len
++] =
141 (uint8_t)(0x80 | (c
& 0x3f));
143 if (utf_o
->u_len
> (UDF_NAME_LEN
- 5))
145 utf_o
->u_name
[utf_o
->u_len
++] =
146 (uint8_t)(0xe0 | (c
>> 12));
147 utf_o
->u_name
[utf_o
->u_len
++] =
150 utf_o
->u_name
[utf_o
->u_len
++] =
151 (uint8_t)(0x80 | (c
& 0x3f));
164 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
167 * This routine is only called by udf_lookup().
170 * ocu Pointer to OSTA Compressed Unicode output
171 * buffer of size UDF_NAME_LEN bytes.
172 * utf Pointer to UTF-8 input buffer.
173 * utf_len Length of UTF-8 input buffer in bytes.
176 * <return> Zero on success.
179 * November 12, 1997 - Andrew E. Mileski
180 * Written, tested, and released.
182 static int udf_UTF8toCS0(dstring
*ocu
, struct ustr
*utf
, int length
)
184 unsigned c
, i
, max_val
, utf_char
;
185 int utf_cnt
, u_len
, u_ch
;
187 memset(ocu
, 0, sizeof(dstring
) * length
);
196 for (i
= 0U; i
< utf
->u_len
; i
++) {
197 /* Name didn't fit? */
198 if (u_len
+ 1 + u_ch
>= length
)
201 c
= (uint8_t)utf
->u_name
[i
];
203 /* Complete a multi-byte UTF-8 character */
205 utf_char
= (utf_char
<< 6) | (c
& 0x3fU
);
209 /* Check for a multi-byte UTF-8 character */
211 /* Start a multi-byte UTF-8 character */
212 if ((c
& 0xe0U
) == 0xc0U
) {
213 utf_char
= c
& 0x1fU
;
215 } else if ((c
& 0xf0U
) == 0xe0U
) {
216 utf_char
= c
& 0x0fU
;
218 } else if ((c
& 0xf8U
) == 0xf0U
) {
219 utf_char
= c
& 0x07U
;
221 } else if ((c
& 0xfcU
) == 0xf8U
) {
222 utf_char
= c
& 0x03U
;
224 } else if ((c
& 0xfeU
) == 0xfcU
) {
225 utf_char
= c
& 0x01U
;
232 /* Single byte UTF-8 character (most common) */
237 /* Choose no compression if necessary */
238 if (utf_char
> max_val
) {
239 if (max_val
== 0xffU
) {
241 ocu
[0] = (uint8_t)0x10U
;
248 if (max_val
== 0xffffU
)
249 ocu
[++u_len
] = (uint8_t)(utf_char
>> 8);
250 ocu
[++u_len
] = (uint8_t)(utf_char
& 0xffU
);
256 printk(KERN_DEBUG
pr_fmt("bad UTF-8 character\n"));
259 ocu
[length
- 1] = (uint8_t)u_len
+ 1;
264 static int udf_CS0toNLS(struct nls_table
*nls
, struct ustr
*utf_o
,
265 const struct ustr
*ocu_i
)
268 uint8_t cmp_id
, ocu_len
;
272 ocu_len
= ocu_i
->u_len
;
274 memset(utf_o
, 0, sizeof(struct ustr
));
278 cmp_id
= ocu_i
->u_cmpID
;
279 if (cmp_id
!= 8 && cmp_id
!= 16) {
280 memset(utf_o
, 0, sizeof(struct ustr
));
281 pr_err("unknown compression code (%d) stri=%s\n",
282 cmp_id
, ocu_i
->u_name
);
288 for (i
= 0; (i
< ocu_len
) && (utf_o
->u_len
<= (UDF_NAME_LEN
- 3));) {
289 /* Expand OSTA compressed Unicode to Unicode */
290 uint32_t c
= ocu
[i
++];
292 c
= (c
<< 8) | ocu
[i
++];
294 len
= nls
->uni2char(c
, &utf_o
->u_name
[utf_o
->u_len
],
295 UDF_NAME_LEN
- 2 - utf_o
->u_len
);
296 /* Valid character? */
300 utf_o
->u_name
[utf_o
->u_len
++] = '?';
307 static int udf_NLStoCS0(struct nls_table
*nls
, dstring
*ocu
, struct ustr
*uni
,
315 memset(ocu
, 0, sizeof(dstring
) * length
);
322 for (i
= 0U; i
< uni
->u_len
; i
++) {
323 /* Name didn't fit? */
324 if (u_len
+ 1 + u_ch
>= length
)
326 len
= nls
->char2uni(&uni
->u_name
[i
], uni
->u_len
- i
, &uni_char
);
329 /* Invalid character, deal with it */
335 if (uni_char
> max_val
) {
337 ocu
[0] = (uint8_t)0x10U
;
342 if (max_val
== 0xffffU
)
343 ocu
[++u_len
] = (uint8_t)(uni_char
>> 8);
344 ocu
[++u_len
] = (uint8_t)(uni_char
& 0xffU
);
348 ocu
[length
- 1] = (uint8_t)u_len
+ 1;
352 int udf_get_filename(struct super_block
*sb
, uint8_t *sname
, int slen
,
353 uint8_t *dname
, int dlen
)
355 struct ustr
*filename
, *unifilename
;
358 filename
= kmalloc(sizeof(struct ustr
), GFP_NOFS
);
362 unifilename
= kmalloc(sizeof(struct ustr
), GFP_NOFS
);
366 if (udf_build_ustr_exact(unifilename
, sname
, slen
))
369 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UTF8
)) {
370 if (!udf_CS0toUTF8(filename
, unifilename
)) {
371 udf_debug("Failed in udf_get_filename: sname = %s\n",
375 } else if (UDF_QUERY_FLAG(sb
, UDF_FLAG_NLS_MAP
)) {
376 if (!udf_CS0toNLS(UDF_SB(sb
)->s_nls_map
, filename
,
378 udf_debug("Failed in udf_get_filename: sname = %s\n",
385 len
= udf_translate_to_linux(dname
, dlen
,
386 filename
->u_name
, filename
->u_len
,
387 unifilename
->u_name
, unifilename
->u_len
);
395 int udf_put_filename(struct super_block
*sb
, const uint8_t *sname
,
396 uint8_t *dname
, int flen
)
398 struct ustr unifilename
;
401 if (!udf_char_to_ustr(&unifilename
, sname
, flen
))
404 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UTF8
)) {
405 namelen
= udf_UTF8toCS0(dname
, &unifilename
, UDF_NAME_LEN
);
408 } else if (UDF_QUERY_FLAG(sb
, UDF_FLAG_NLS_MAP
)) {
409 namelen
= udf_NLStoCS0(UDF_SB(sb
)->s_nls_map
, dname
,
410 &unifilename
, UDF_NAME_LEN
);
419 #define ILLEGAL_CHAR_MARK '_'
423 /* Number of chars we need to store generated CRC to make filename unique */
426 static int udf_translate_to_linux(uint8_t *newName
, int newLen
,
427 uint8_t *udfName
, int udfLen
,
428 uint8_t *fidName
, int fidNameLen
)
430 int index
, newIndex
= 0, needsCRC
= 0;
431 int extIndex
= 0, newExtIndex
= 0, hasExt
= 0;
432 unsigned short valueCRC
;
435 if (udfName
[0] == '.' &&
436 (udfLen
== 1 || (udfLen
== 2 && udfName
[1] == '.'))) {
439 memcpy(newName
, udfName
, udfLen
);
441 for (index
= 0; index
< udfLen
; index
++) {
442 curr
= udfName
[index
];
443 if (curr
== '/' || curr
== 0) {
445 curr
= ILLEGAL_CHAR_MARK
;
446 while (index
+ 1 < udfLen
&&
447 (udfName
[index
+ 1] == '/' ||
448 udfName
[index
+ 1] == 0))
451 if (curr
== EXT_MARK
&&
452 (udfLen
- index
- 1) <= EXT_SIZE
) {
453 if (udfLen
== index
+ 1)
458 newExtIndex
= newIndex
;
461 if (newIndex
< newLen
)
462 newName
[newIndex
++] = curr
;
468 uint8_t ext
[EXT_SIZE
];
469 int localExtIndex
= 0;
474 index
< EXT_SIZE
&& extIndex
+ index
+ 1 < udfLen
;
476 curr
= udfName
[extIndex
+ index
+ 1];
478 if (curr
== '/' || curr
== 0) {
480 curr
= ILLEGAL_CHAR_MARK
;
481 while (extIndex
+ index
+ 2 < udfLen
&&
482 (index
+ 1 < EXT_SIZE
&&
483 (udfName
[extIndex
+ index
+ 2] == '/' ||
484 udfName
[extIndex
+ index
+ 2] == 0)))
487 ext
[localExtIndex
++] = curr
;
489 maxFilenameLen
= newLen
- CRC_LEN
- localExtIndex
;
490 if (newIndex
> maxFilenameLen
)
491 newIndex
= maxFilenameLen
;
493 newIndex
= newExtIndex
;
494 } else if (newIndex
> newLen
- CRC_LEN
)
495 newIndex
= newLen
- CRC_LEN
;
496 newName
[newIndex
++] = CRC_MARK
;
497 valueCRC
= crc_itu_t(0, fidName
, fidNameLen
);
498 newName
[newIndex
++] = hex_asc_upper_hi(valueCRC
>> 8);
499 newName
[newIndex
++] = hex_asc_upper_lo(valueCRC
>> 8);
500 newName
[newIndex
++] = hex_asc_upper_hi(valueCRC
);
501 newName
[newIndex
++] = hex_asc_upper_lo(valueCRC
);
504 newName
[newIndex
++] = EXT_MARK
;
505 for (index
= 0; index
< localExtIndex
; index
++)
506 newName
[newIndex
++] = ext
[index
];