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 *, uint8_t *, int, uint8_t *, int);
33 static int udf_char_to_ustr(struct ustr
*dest
, const uint8_t *src
, int strlen
)
35 if ((!dest
) || (!src
) || (!strlen
) || (strlen
> UDF_NAME_LEN
- 2))
38 memset(dest
, 0, sizeof(struct ustr
));
39 memcpy(dest
->u_name
, src
, strlen
);
49 int udf_build_ustr(struct ustr
*dest
, dstring
*ptr
, int size
)
53 if (!dest
|| !ptr
|| !size
)
57 usesize
= min_t(size_t, ptr
[size
- 1], sizeof(dest
->u_name
));
58 usesize
= min(usesize
, size
- 2);
59 dest
->u_cmpID
= ptr
[0];
60 dest
->u_len
= usesize
;
61 memcpy(dest
->u_name
, ptr
+ 1, usesize
);
62 memset(dest
->u_name
+ usesize
, 0, sizeof(dest
->u_name
) - usesize
);
68 * udf_build_ustr_exact
70 static int udf_build_ustr_exact(struct ustr
*dest
, dstring
*ptr
, int exactsize
)
72 if ((!dest
) || (!ptr
) || (!exactsize
))
75 memset(dest
, 0, sizeof(struct ustr
));
76 dest
->u_cmpID
= ptr
[0];
77 dest
->u_len
= exactsize
- 1;
78 memcpy(dest
->u_name
, ptr
+ 1, exactsize
- 1);
87 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
90 * utf Pointer to UTF-8 output buffer.
91 * ocu Pointer to OSTA Compressed Unicode input buffer
92 * of size UDF_NAME_LEN bytes.
93 * both of type "struct ustr *"
96 * <return> Zero on success.
99 * November 12, 1997 - Andrew E. Mileski
100 * Written, tested, and released.
102 int udf_CS0toUTF8(struct ustr
*utf_o
, const struct ustr
*ocu_i
)
105 uint8_t cmp_id
, ocu_len
;
108 ocu_len
= ocu_i
->u_len
;
110 memset(utf_o
, 0, sizeof(struct ustr
));
114 cmp_id
= ocu_i
->u_cmpID
;
115 if (cmp_id
!= 8 && cmp_id
!= 16) {
116 memset(utf_o
, 0, sizeof(struct ustr
));
117 pr_err("unknown compression code (%d) stri=%s\n",
118 cmp_id
, ocu_i
->u_name
);
124 for (i
= 0; (i
< ocu_len
) && (utf_o
->u_len
<= (UDF_NAME_LEN
- 3));) {
126 /* Expand OSTA compressed Unicode to Unicode */
127 uint32_t c
= ocu
[i
++];
129 c
= (c
<< 8) | ocu
[i
++];
131 /* Compress Unicode to UTF-8 */
133 utf_o
->u_name
[utf_o
->u_len
++] = (uint8_t)c
;
134 else if (c
< 0x800U
) {
135 utf_o
->u_name
[utf_o
->u_len
++] =
136 (uint8_t)(0xc0 | (c
>> 6));
137 utf_o
->u_name
[utf_o
->u_len
++] =
138 (uint8_t)(0x80 | (c
& 0x3f));
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
;
182 memset(ocu
, 0, sizeof(dstring
) * length
);
190 for (i
= 0U; i
< utf
->u_len
; i
++) {
191 c
= (uint8_t)utf
->u_name
[i
];
193 /* Complete a multi-byte UTF-8 character */
195 utf_char
= (utf_char
<< 6) | (c
& 0x3fU
);
199 /* Check for a multi-byte UTF-8 character */
201 /* Start a multi-byte UTF-8 character */
202 if ((c
& 0xe0U
) == 0xc0U
) {
203 utf_char
= c
& 0x1fU
;
205 } else if ((c
& 0xf0U
) == 0xe0U
) {
206 utf_char
= c
& 0x0fU
;
208 } else if ((c
& 0xf8U
) == 0xf0U
) {
209 utf_char
= c
& 0x07U
;
211 } else if ((c
& 0xfcU
) == 0xf8U
) {
212 utf_char
= c
& 0x03U
;
214 } else if ((c
& 0xfeU
) == 0xfcU
) {
215 utf_char
= c
& 0x01U
;
222 /* Single byte UTF-8 character (most common) */
227 /* Choose no compression if necessary */
228 if (utf_char
> max_val
) {
229 if (max_val
== 0xffU
) {
231 ocu
[0] = (uint8_t)0x10U
;
237 if (max_val
== 0xffffU
)
238 ocu
[++u_len
] = (uint8_t)(utf_char
>> 8);
239 ocu
[++u_len
] = (uint8_t)(utf_char
& 0xffU
);
245 printk(KERN_DEBUG
pr_fmt("bad UTF-8 character\n"));
248 ocu
[length
- 1] = (uint8_t)u_len
+ 1;
253 static int udf_CS0toNLS(struct nls_table
*nls
, struct ustr
*utf_o
,
254 const struct ustr
*ocu_i
)
257 uint8_t cmp_id
, ocu_len
;
261 ocu_len
= ocu_i
->u_len
;
263 memset(utf_o
, 0, sizeof(struct ustr
));
267 cmp_id
= ocu_i
->u_cmpID
;
268 if (cmp_id
!= 8 && cmp_id
!= 16) {
269 memset(utf_o
, 0, sizeof(struct ustr
));
270 pr_err("unknown compression code (%d) stri=%s\n",
271 cmp_id
, ocu_i
->u_name
);
277 for (i
= 0; (i
< ocu_len
) && (utf_o
->u_len
<= (UDF_NAME_LEN
- 3));) {
278 /* Expand OSTA compressed Unicode to Unicode */
279 uint32_t c
= ocu
[i
++];
281 c
= (c
<< 8) | ocu
[i
++];
283 len
= nls
->uni2char(c
, &utf_o
->u_name
[utf_o
->u_len
],
284 UDF_NAME_LEN
- utf_o
->u_len
);
285 /* Valid character? */
289 utf_o
->u_name
[utf_o
->u_len
++] = '?';
296 static int udf_NLStoCS0(struct nls_table
*nls
, dstring
*ocu
, struct ustr
*uni
,
304 memset(ocu
, 0, sizeof(dstring
) * length
);
310 for (i
= 0U; i
< uni
->u_len
; i
++) {
311 len
= nls
->char2uni(&uni
->u_name
[i
], uni
->u_len
- i
, &uni_char
);
314 /* Invalid character, deal with it */
320 if (uni_char
> max_val
) {
322 ocu
[0] = (uint8_t)0x10U
;
326 if (max_val
== 0xffffU
)
327 ocu
[++u_len
] = (uint8_t)(uni_char
>> 8);
328 ocu
[++u_len
] = (uint8_t)(uni_char
& 0xffU
);
332 ocu
[length
- 1] = (uint8_t)u_len
+ 1;
336 int udf_get_filename(struct super_block
*sb
, uint8_t *sname
, uint8_t *dname
,
339 struct ustr
*filename
, *unifilename
;
342 filename
= kmalloc(sizeof(struct ustr
), GFP_NOFS
);
346 unifilename
= kmalloc(sizeof(struct ustr
), GFP_NOFS
);
350 if (udf_build_ustr_exact(unifilename
, sname
, flen
))
353 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UTF8
)) {
354 if (!udf_CS0toUTF8(filename
, unifilename
)) {
355 udf_debug("Failed in udf_get_filename: sname = %s\n",
359 } else if (UDF_QUERY_FLAG(sb
, UDF_FLAG_NLS_MAP
)) {
360 if (!udf_CS0toNLS(UDF_SB(sb
)->s_nls_map
, filename
,
362 udf_debug("Failed in udf_get_filename: sname = %s\n",
369 len
= udf_translate_to_linux(dname
, filename
->u_name
, filename
->u_len
,
370 unifilename
->u_name
, unifilename
->u_len
);
378 int udf_put_filename(struct super_block
*sb
, const uint8_t *sname
,
379 uint8_t *dname
, int flen
)
381 struct ustr unifilename
;
384 if (!udf_char_to_ustr(&unifilename
, sname
, flen
))
387 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UTF8
)) {
388 namelen
= udf_UTF8toCS0(dname
, &unifilename
, UDF_NAME_LEN
);
391 } else if (UDF_QUERY_FLAG(sb
, UDF_FLAG_NLS_MAP
)) {
392 namelen
= udf_NLStoCS0(UDF_SB(sb
)->s_nls_map
, dname
,
393 &unifilename
, UDF_NAME_LEN
);
402 #define ILLEGAL_CHAR_MARK '_'
407 static int udf_translate_to_linux(uint8_t *newName
, uint8_t *udfName
,
408 int udfLen
, uint8_t *fidName
,
411 int index
, newIndex
= 0, needsCRC
= 0;
412 int extIndex
= 0, newExtIndex
= 0, hasExt
= 0;
413 unsigned short valueCRC
;
415 const uint8_t hexChar
[] = "0123456789ABCDEF";
417 if (udfName
[0] == '.' &&
418 (udfLen
== 1 || (udfLen
== 2 && udfName
[1] == '.'))) {
421 memcpy(newName
, udfName
, udfLen
);
423 for (index
= 0; index
< udfLen
; index
++) {
424 curr
= udfName
[index
];
425 if (curr
== '/' || curr
== 0) {
427 curr
= ILLEGAL_CHAR_MARK
;
428 while (index
+ 1 < udfLen
&&
429 (udfName
[index
+ 1] == '/' ||
430 udfName
[index
+ 1] == 0))
433 if (curr
== EXT_MARK
&&
434 (udfLen
- index
- 1) <= EXT_SIZE
) {
435 if (udfLen
== index
+ 1)
440 newExtIndex
= newIndex
;
444 newName
[newIndex
++] = curr
;
450 uint8_t ext
[EXT_SIZE
];
451 int localExtIndex
= 0;
456 index
< EXT_SIZE
&& extIndex
+ index
+ 1 < udfLen
;
458 curr
= udfName
[extIndex
+ index
+ 1];
460 if (curr
== '/' || curr
== 0) {
462 curr
= ILLEGAL_CHAR_MARK
;
463 while (extIndex
+ index
+ 2 < udfLen
&&
464 (index
+ 1 < EXT_SIZE
&&
465 (udfName
[extIndex
+ index
+ 2] == '/' ||
466 udfName
[extIndex
+ index
+ 2] == 0)))
469 ext
[localExtIndex
++] = curr
;
471 maxFilenameLen
= 250 - localExtIndex
;
472 if (newIndex
> maxFilenameLen
)
473 newIndex
= maxFilenameLen
;
475 newIndex
= newExtIndex
;
476 } else if (newIndex
> 250)
478 newName
[newIndex
++] = CRC_MARK
;
479 valueCRC
= crc_itu_t(0, fidName
, fidNameLen
);
480 newName
[newIndex
++] = hexChar
[(valueCRC
& 0xf000) >> 12];
481 newName
[newIndex
++] = hexChar
[(valueCRC
& 0x0f00) >> 8];
482 newName
[newIndex
++] = hexChar
[(valueCRC
& 0x00f0) >> 4];
483 newName
[newIndex
++] = hexChar
[(valueCRC
& 0x000f)];
486 newName
[newIndex
++] = EXT_MARK
;
487 for (index
= 0; index
< localExtIndex
; index
++)
488 newName
[newIndex
++] = ext
[index
];