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 void udf_build_ustr_exact(struct ustr
*dest
, dstring
*ptr
, int exactsize
)
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);
83 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
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 *"
92 * <return> >= 0 on success.
95 * November 12, 1997 - Andrew E. Mileski
96 * Written, tested, and released.
98 int udf_CS0toUTF8(struct ustr
*utf_o
, const struct ustr
*ocu_i
)
101 uint8_t cmp_id
, ocu_len
;
104 ocu_len
= ocu_i
->u_len
;
106 memset(utf_o
, 0, sizeof(struct ustr
));
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
);
120 for (i
= 0; (i
< ocu_len
) && (utf_o
->u_len
<= (UDF_NAME_LEN
- 3));) {
122 /* Expand OSTA compressed Unicode to Unicode */
123 uint32_t c
= ocu
[i
++];
125 c
= (c
<< 8) | ocu
[i
++];
127 /* Compress Unicode to UTF-8 */
129 utf_o
->u_name
[utf_o
->u_len
++] = (uint8_t)c
;
130 else if (c
< 0x800U
) {
131 if (utf_o
->u_len
> (UDF_NAME_LEN
- 4))
133 utf_o
->u_name
[utf_o
->u_len
++] =
134 (uint8_t)(0xc0 | (c
>> 6));
135 utf_o
->u_name
[utf_o
->u_len
++] =
136 (uint8_t)(0x80 | (c
& 0x3f));
138 if (utf_o
->u_len
> (UDF_NAME_LEN
- 5))
140 utf_o
->u_name
[utf_o
->u_len
++] =
141 (uint8_t)(0xe0 | (c
>> 12));
142 utf_o
->u_name
[utf_o
->u_len
++] =
145 utf_o
->u_name
[utf_o
->u_len
++] =
146 (uint8_t)(0x80 | (c
& 0x3f));
159 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
162 * This routine is only called by udf_lookup().
165 * ocu Pointer to OSTA Compressed Unicode output
166 * buffer of size UDF_NAME_LEN bytes.
167 * utf Pointer to UTF-8 input buffer.
168 * utf_len Length of UTF-8 input buffer in bytes.
171 * <return> Zero on success.
174 * November 12, 1997 - Andrew E. Mileski
175 * Written, tested, and released.
177 static int udf_UTF8toCS0(dstring
*ocu
, struct ustr
*utf
, int length
)
179 unsigned c
, i
, max_val
, utf_char
;
180 int utf_cnt
, u_len
, u_ch
;
182 memset(ocu
, 0, sizeof(dstring
) * length
);
191 for (i
= 0U; i
< utf
->u_len
; i
++) {
192 /* Name didn't fit? */
193 if (u_len
+ 1 + u_ch
>= length
)
196 c
= (uint8_t)utf
->u_name
[i
];
198 /* Complete a multi-byte UTF-8 character */
200 utf_char
= (utf_char
<< 6) | (c
& 0x3fU
);
204 /* Check for a multi-byte UTF-8 character */
206 /* Start a multi-byte UTF-8 character */
207 if ((c
& 0xe0U
) == 0xc0U
) {
208 utf_char
= c
& 0x1fU
;
210 } else if ((c
& 0xf0U
) == 0xe0U
) {
211 utf_char
= c
& 0x0fU
;
213 } else if ((c
& 0xf8U
) == 0xf0U
) {
214 utf_char
= c
& 0x07U
;
216 } else if ((c
& 0xfcU
) == 0xf8U
) {
217 utf_char
= c
& 0x03U
;
219 } else if ((c
& 0xfeU
) == 0xfcU
) {
220 utf_char
= c
& 0x01U
;
227 /* Single byte UTF-8 character (most common) */
232 /* Choose no compression if necessary */
233 if (utf_char
> max_val
) {
234 if (max_val
== 0xffU
) {
236 ocu
[0] = (uint8_t)0x10U
;
243 if (max_val
== 0xffffU
)
244 ocu
[++u_len
] = (uint8_t)(utf_char
>> 8);
245 ocu
[++u_len
] = (uint8_t)(utf_char
& 0xffU
);
251 printk(KERN_DEBUG
pr_fmt("bad UTF-8 character\n"));
254 ocu
[length
- 1] = (uint8_t)u_len
+ 1;
259 static int udf_CS0toNLS(struct nls_table
*nls
, struct ustr
*utf_o
,
260 const struct ustr
*ocu_i
)
263 uint8_t cmp_id
, ocu_len
;
267 ocu_len
= ocu_i
->u_len
;
269 memset(utf_o
, 0, sizeof(struct ustr
));
273 cmp_id
= ocu_i
->u_cmpID
;
274 if (cmp_id
!= 8 && cmp_id
!= 16) {
275 memset(utf_o
, 0, sizeof(struct ustr
));
276 pr_err("unknown compression code (%d) stri=%s\n",
277 cmp_id
, ocu_i
->u_name
);
283 for (i
= 0; (i
< ocu_len
) && (utf_o
->u_len
<= (UDF_NAME_LEN
- 3));) {
284 /* Expand OSTA compressed Unicode to Unicode */
285 uint32_t c
= ocu
[i
++];
287 c
= (c
<< 8) | ocu
[i
++];
289 len
= nls
->uni2char(c
, &utf_o
->u_name
[utf_o
->u_len
],
290 UDF_NAME_LEN
- 2 - utf_o
->u_len
);
291 /* Valid character? */
295 utf_o
->u_name
[utf_o
->u_len
++] = '?';
302 static int udf_NLStoCS0(struct nls_table
*nls
, dstring
*ocu
, struct ustr
*uni
,
310 memset(ocu
, 0, sizeof(dstring
) * length
);
317 for (i
= 0U; i
< uni
->u_len
; i
++) {
318 /* Name didn't fit? */
319 if (u_len
+ 1 + u_ch
>= length
)
321 len
= nls
->char2uni(&uni
->u_name
[i
], uni
->u_len
- i
, &uni_char
);
324 /* Invalid character, deal with it */
330 if (uni_char
> max_val
) {
332 ocu
[0] = (uint8_t)0x10U
;
337 if (max_val
== 0xffffU
)
338 ocu
[++u_len
] = (uint8_t)(uni_char
>> 8);
339 ocu
[++u_len
] = (uint8_t)(uni_char
& 0xffU
);
343 ocu
[length
- 1] = (uint8_t)u_len
+ 1;
347 int udf_get_filename(struct super_block
*sb
, uint8_t *sname
, int slen
,
348 uint8_t *dname
, int dlen
)
350 struct ustr
*filename
, *unifilename
;
356 filename
= kmalloc(sizeof(struct ustr
), GFP_NOFS
);
360 unifilename
= kmalloc(sizeof(struct ustr
), GFP_NOFS
);
366 udf_build_ustr_exact(unifilename
, sname
, slen
);
367 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UTF8
)) {
368 ret
= udf_CS0toUTF8(filename
, unifilename
);
370 udf_debug("Failed in udf_get_filename: sname = %s\n",
374 } else if (UDF_QUERY_FLAG(sb
, UDF_FLAG_NLS_MAP
)) {
375 ret
= udf_CS0toNLS(UDF_SB(sb
)->s_nls_map
, filename
,
378 udf_debug("Failed in udf_get_filename: sname = %s\n",
385 ret
= udf_translate_to_linux(dname
, dlen
,
386 filename
->u_name
, filename
->u_len
,
387 unifilename
->u_name
, unifilename
->u_len
);
388 /* Zero length filename isn't valid... */
398 int udf_put_filename(struct super_block
*sb
, const uint8_t *sname
,
399 uint8_t *dname
, int flen
)
401 struct ustr unifilename
;
404 if (!udf_char_to_ustr(&unifilename
, sname
, flen
))
407 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UTF8
)) {
408 namelen
= udf_UTF8toCS0(dname
, &unifilename
, UDF_NAME_LEN
);
411 } else if (UDF_QUERY_FLAG(sb
, UDF_FLAG_NLS_MAP
)) {
412 namelen
= udf_NLStoCS0(UDF_SB(sb
)->s_nls_map
, dname
,
413 &unifilename
, UDF_NAME_LEN
);
422 #define ILLEGAL_CHAR_MARK '_'
426 /* Number of chars we need to store generated CRC to make filename unique */
429 static int udf_translate_to_linux(uint8_t *newName
, int newLen
,
430 uint8_t *udfName
, int udfLen
,
431 uint8_t *fidName
, int fidNameLen
)
433 int index
, newIndex
= 0, needsCRC
= 0;
434 int extIndex
= 0, newExtIndex
= 0, hasExt
= 0;
435 unsigned short valueCRC
;
438 if (udfName
[0] == '.' &&
439 (udfLen
== 1 || (udfLen
== 2 && udfName
[1] == '.'))) {
442 memcpy(newName
, udfName
, udfLen
);
444 for (index
= 0; index
< udfLen
; index
++) {
445 curr
= udfName
[index
];
446 if (curr
== '/' || curr
== 0) {
448 curr
= ILLEGAL_CHAR_MARK
;
449 while (index
+ 1 < udfLen
&&
450 (udfName
[index
+ 1] == '/' ||
451 udfName
[index
+ 1] == 0))
454 if (curr
== EXT_MARK
&&
455 (udfLen
- index
- 1) <= EXT_SIZE
) {
456 if (udfLen
== index
+ 1)
461 newExtIndex
= newIndex
;
464 if (newIndex
< newLen
)
465 newName
[newIndex
++] = curr
;
471 uint8_t ext
[EXT_SIZE
];
472 int localExtIndex
= 0;
477 index
< EXT_SIZE
&& extIndex
+ index
+ 1 < udfLen
;
479 curr
= udfName
[extIndex
+ index
+ 1];
481 if (curr
== '/' || curr
== 0) {
483 curr
= ILLEGAL_CHAR_MARK
;
484 while (extIndex
+ index
+ 2 < udfLen
&&
485 (index
+ 1 < EXT_SIZE
&&
486 (udfName
[extIndex
+ index
+ 2] == '/' ||
487 udfName
[extIndex
+ index
+ 2] == 0)))
490 ext
[localExtIndex
++] = curr
;
492 maxFilenameLen
= newLen
- CRC_LEN
- localExtIndex
;
493 if (newIndex
> maxFilenameLen
)
494 newIndex
= maxFilenameLen
;
496 newIndex
= newExtIndex
;
497 } else if (newIndex
> newLen
- CRC_LEN
)
498 newIndex
= newLen
- CRC_LEN
;
499 newName
[newIndex
++] = CRC_MARK
;
500 valueCRC
= crc_itu_t(0, fidName
, fidNameLen
);
501 newName
[newIndex
++] = hex_asc_upper_hi(valueCRC
>> 8);
502 newName
[newIndex
++] = hex_asc_upper_lo(valueCRC
>> 8);
503 newName
[newIndex
++] = hex_asc_upper_hi(valueCRC
);
504 newName
[newIndex
++] = hex_asc_upper_lo(valueCRC
);
507 newName
[newIndex
++] = EXT_MARK
;
508 for (index
= 0; index
< localExtIndex
; index
++)
509 newName
[newIndex
++] = ext
[index
];