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/udf_fs.h>
30 static int udf_translate_to_linux(uint8_t *, uint8_t *, int, uint8_t *, int);
32 static int udf_char_to_ustr(struct ustr
*dest
, const uint8_t *src
, int strlen
)
34 if ((!dest
) || (!src
) || (!strlen
) || (strlen
> UDF_NAME_LEN
- 2))
37 memset(dest
, 0, sizeof(struct ustr
));
38 memcpy(dest
->u_name
, src
, strlen
);
48 int udf_build_ustr(struct ustr
*dest
, dstring
*ptr
, int size
)
52 if ((!dest
) || (!ptr
) || (!size
))
55 memset(dest
, 0, sizeof(struct ustr
));
56 usesize
= (size
> UDF_NAME_LEN
) ? UDF_NAME_LEN
: size
;
57 dest
->u_cmpID
= ptr
[0];
58 dest
->u_len
= ptr
[size
- 1];
59 memcpy(dest
->u_name
, ptr
+ 1, usesize
- 1);
65 * udf_build_ustr_exact
67 static int udf_build_ustr_exact(struct ustr
*dest
, dstring
*ptr
, int exactsize
)
69 if ((!dest
) || (!ptr
) || (!exactsize
))
72 memset(dest
, 0, sizeof(struct ustr
));
73 dest
->u_cmpID
= ptr
[0];
74 dest
->u_len
= exactsize
- 1;
75 memcpy(dest
->u_name
, ptr
+ 1, exactsize
- 1);
84 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
87 * This routine is only called by udf_filldir().
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
, struct ustr
*ocu_i
)
106 uint8_t cmp_id
, ocu_len
;
111 ocu_len
= ocu_i
->u_len
;
112 cmp_id
= ocu_i
->u_cmpID
;
116 memset(utf_o
, 0, sizeof(struct ustr
));
122 if ((cmp_id
!= 8) && (cmp_id
!= 16)) {
123 printk(KERN_ERR
"udf: unknown compression code (%d) stri=%s\n",
124 cmp_id
, ocu_i
->u_name
);
128 for (i
= 0; (i
< ocu_len
) && (utf_o
->u_len
<= (UDF_NAME_LEN
- 3));) {
130 /* Expand OSTA compressed Unicode to Unicode */
133 c
= (c
<< 8) | ocu
[i
++];
135 /* Compress Unicode to UTF-8 */
137 utf_o
->u_name
[utf_o
->u_len
++] = (uint8_t)c
;
138 } else if (c
< 0x800U
) {
139 utf_o
->u_name
[utf_o
->u_len
++] =
140 (uint8_t)(0xc0 | (c
>> 6));
141 utf_o
->u_name
[utf_o
->u_len
++] =
142 (uint8_t)(0x80 | (c
& 0x3f));
144 utf_o
->u_name
[utf_o
->u_len
++] =
145 (uint8_t)(0xe0 | (c
>> 12));
146 utf_o
->u_name
[utf_o
->u_len
++] =
149 utf_o
->u_name
[utf_o
->u_len
++] =
150 (uint8_t)(0x80 | (c
& 0x3f));
163 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
166 * This routine is only called by udf_lookup().
169 * ocu Pointer to OSTA Compressed Unicode output
170 * buffer of size UDF_NAME_LEN bytes.
171 * utf Pointer to UTF-8 input buffer.
172 * utf_len Length of UTF-8 input buffer in bytes.
175 * <return> Zero on success.
178 * November 12, 1997 - Andrew E. Mileski
179 * Written, tested, and released.
181 static int udf_UTF8toCS0(dstring
*ocu
, struct ustr
*utf
, int length
)
183 unsigned c
, i
, max_val
, utf_char
;
186 memset(ocu
, 0, sizeof(dstring
) * length
);
194 for (i
= 0U; i
< utf
->u_len
; i
++) {
195 c
= (uint8_t)utf
->u_name
[i
];
197 /* Complete a multi-byte UTF-8 character */
199 utf_char
= (utf_char
<< 6) | (c
& 0x3fU
);
203 /* Check for a multi-byte UTF-8 character */
205 /* Start a multi-byte UTF-8 character */
206 if ((c
& 0xe0U
) == 0xc0U
) {
207 utf_char
= c
& 0x1fU
;
209 } else if ((c
& 0xf0U
) == 0xe0U
) {
210 utf_char
= c
& 0x0fU
;
212 } else if ((c
& 0xf8U
) == 0xf0U
) {
213 utf_char
= c
& 0x07U
;
215 } else if ((c
& 0xfcU
) == 0xf8U
) {
216 utf_char
= c
& 0x03U
;
218 } else if ((c
& 0xfeU
) == 0xfcU
) {
219 utf_char
= c
& 0x01U
;
226 /* Single byte UTF-8 character (most common) */
231 /* Choose no compression if necessary */
232 if (utf_char
> max_val
) {
233 if (max_val
== 0xffU
) {
235 ocu
[0] = (uint8_t)0x10U
;
241 if (max_val
== 0xffffU
)
242 ocu
[++u_len
] = (uint8_t)(utf_char
>> 8);
243 ocu
[++u_len
] = (uint8_t)(utf_char
& 0xffU
);
249 printk(KERN_DEBUG
"udf: bad UTF-8 character\n");
252 ocu
[length
- 1] = (uint8_t)u_len
+ 1;
257 static int udf_CS0toNLS(struct nls_table
*nls
, struct ustr
*utf_o
,
262 uint8_t cmp_id
, ocu_len
;
267 ocu_len
= ocu_i
->u_len
;
268 cmp_id
= ocu_i
->u_cmpID
;
272 memset(utf_o
, 0, sizeof(struct ustr
));
278 if ((cmp_id
!= 8) && (cmp_id
!= 16)) {
279 printk(KERN_ERR
"udf: unknown compression code (%d) stri=%s\n",
280 cmp_id
, ocu_i
->u_name
);
284 for (i
= 0; (i
< ocu_len
) && (utf_o
->u_len
<= (UDF_NAME_LEN
- 3));) {
285 /* Expand OSTA compressed Unicode to Unicode */
288 c
= (c
<< 8) | ocu
[i
++];
290 utf_o
->u_len
+= nls
->uni2char(c
, &utf_o
->u_name
[utf_o
->u_len
],
291 UDF_NAME_LEN
- utf_o
->u_len
);
298 static int udf_NLStoCS0(struct nls_table
*nls
, dstring
*ocu
, struct ustr
*uni
,
301 unsigned len
, i
, max_val
;
305 memset(ocu
, 0, sizeof(dstring
) * length
);
311 for (i
= 0U; i
< uni
->u_len
; i
++) {
312 len
= nls
->char2uni(&uni
->u_name
[i
], uni
->u_len
- i
, &uni_char
);
316 if (uni_char
> max_val
) {
318 ocu
[0] = (uint8_t)0x10U
;
322 if (max_val
== 0xffffU
)
323 ocu
[++u_len
] = (uint8_t)(uni_char
>> 8);
324 ocu
[++u_len
] = (uint8_t)(uni_char
& 0xffU
);
328 ocu
[length
- 1] = (uint8_t)u_len
+ 1;
332 int udf_get_filename(struct super_block
*sb
, uint8_t *sname
, uint8_t *dname
,
335 struct ustr filename
, unifilename
;
338 if (udf_build_ustr_exact(&unifilename
, sname
, flen
))
341 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UTF8
)) {
342 if (!udf_CS0toUTF8(&filename
, &unifilename
)) {
343 udf_debug("Failed in udf_get_filename: sname = %s\n",
347 } else if (UDF_QUERY_FLAG(sb
, UDF_FLAG_NLS_MAP
)) {
348 if (!udf_CS0toNLS(UDF_SB(sb
)->s_nls_map
, &filename
,
350 udf_debug("Failed in udf_get_filename: sname = %s\n",
357 len
= udf_translate_to_linux(dname
, filename
.u_name
, filename
.u_len
,
358 unifilename
.u_name
, unifilename
.u_len
);
365 int udf_put_filename(struct super_block
*sb
, const uint8_t *sname
,
366 uint8_t *dname
, int flen
)
368 struct ustr unifilename
;
371 if (!udf_char_to_ustr(&unifilename
, sname
, flen
))
374 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UTF8
)) {
375 namelen
= udf_UTF8toCS0(dname
, &unifilename
, UDF_NAME_LEN
);
378 } else if (UDF_QUERY_FLAG(sb
, UDF_FLAG_NLS_MAP
)) {
379 namelen
= udf_NLStoCS0(UDF_SB(sb
)->s_nls_map
, dname
,
380 &unifilename
, UDF_NAME_LEN
);
389 #define ILLEGAL_CHAR_MARK '_'
394 static int udf_translate_to_linux(uint8_t *newName
, uint8_t *udfName
,
395 int udfLen
, uint8_t *fidName
,
398 int index
, newIndex
= 0, needsCRC
= 0;
399 int extIndex
= 0, newExtIndex
= 0, hasExt
= 0;
400 unsigned short valueCRC
;
402 const uint8_t hexChar
[] = "0123456789ABCDEF";
404 if (udfName
[0] == '.' &&
405 (udfLen
== 1 || (udfLen
== 2 && udfName
[1] == '.'))) {
408 memcpy(newName
, udfName
, udfLen
);
410 for (index
= 0; index
< udfLen
; index
++) {
411 curr
= udfName
[index
];
412 if (curr
== '/' || curr
== 0) {
414 curr
= ILLEGAL_CHAR_MARK
;
415 while (index
+ 1 < udfLen
&&
416 (udfName
[index
+ 1] == '/' ||
417 udfName
[index
+ 1] == 0))
420 if (curr
== EXT_MARK
&&
421 (udfLen
- index
- 1) <= EXT_SIZE
) {
422 if (udfLen
== index
+ 1)
427 newExtIndex
= newIndex
;
431 newName
[newIndex
++] = curr
;
437 uint8_t ext
[EXT_SIZE
];
438 int localExtIndex
= 0;
443 index
< EXT_SIZE
&& extIndex
+ index
+ 1 < udfLen
;
445 curr
= udfName
[extIndex
+ index
+ 1];
447 if (curr
== '/' || curr
== 0) {
449 curr
= ILLEGAL_CHAR_MARK
;
450 while (extIndex
+ index
+ 2 < udfLen
&&
451 (index
+ 1 < EXT_SIZE
&&
452 (udfName
[extIndex
+ index
+ 2] == '/' ||
453 udfName
[extIndex
+ index
+ 2] == 0)))
456 ext
[localExtIndex
++] = curr
;
458 maxFilenameLen
= 250 - localExtIndex
;
459 if (newIndex
> maxFilenameLen
)
460 newIndex
= maxFilenameLen
;
462 newIndex
= newExtIndex
;
463 } else if (newIndex
> 250)
465 newName
[newIndex
++] = CRC_MARK
;
466 valueCRC
= udf_crc(fidName
, fidNameLen
, 0);
467 newName
[newIndex
++] = hexChar
[(valueCRC
& 0xf000) >> 12];
468 newName
[newIndex
++] = hexChar
[(valueCRC
& 0x0f00) >> 8];
469 newName
[newIndex
++] = hexChar
[(valueCRC
& 0x00f0) >> 4];
470 newName
[newIndex
++] = hexChar
[(valueCRC
& 0x000f)];
473 newName
[newIndex
++] = EXT_MARK
;
474 for (index
= 0; index
< localExtIndex
; index
++)
475 newName
[newIndex
++] = ext
[index
];