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) )
36 memset(dest
, 0, sizeof(struct ustr
));
37 memcpy(dest
->u_name
, src
, strlen
);
46 int udf_build_ustr(struct ustr
*dest
, dstring
*ptr
, int size
)
50 if ( (!dest
) || (!ptr
) || (!size
) )
53 memset(dest
, 0, sizeof(struct ustr
));
54 usesize
= (size
> UDF_NAME_LEN
) ? UDF_NAME_LEN
: size
;
56 dest
->u_len
=ptr
[size
-1];
57 memcpy(dest
->u_name
, ptr
+1, usesize
-1);
62 * udf_build_ustr_exact
64 static int udf_build_ustr_exact(struct ustr
*dest
, dstring
*ptr
, int exactsize
)
66 if ( (!dest
) || (!ptr
) || (!exactsize
) )
69 memset(dest
, 0, sizeof(struct ustr
));
71 dest
->u_len
=exactsize
-1;
72 memcpy(dest
->u_name
, ptr
+1, exactsize
-1);
80 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
83 * This routine is only called by udf_filldir().
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> Zero on success.
95 * November 12, 1997 - Andrew E. Mileski
96 * Written, tested, and released.
98 int udf_CS0toUTF8(struct ustr
*utf_o
, struct ustr
*ocu_i
)
102 uint8_t cmp_id
, ocu_len
;
107 ocu_len
= ocu_i
->u_len
;
108 cmp_id
= ocu_i
->u_cmpID
;
113 memset(utf_o
, 0, sizeof(struct ustr
));
119 if ((cmp_id
!= 8) && (cmp_id
!= 16))
121 printk(KERN_ERR
"udf: unknown compression code (%d) stri=%s\n", cmp_id
, ocu_i
->u_name
);
125 for (i
= 0; (i
< ocu_len
) && (utf_o
->u_len
<= (UDF_NAME_LEN
-3)) ;)
128 /* Expand OSTA compressed Unicode to Unicode */
131 c
= (c
<< 8) | ocu
[i
++];
133 /* Compress Unicode to UTF-8 */
135 utf_o
->u_name
[utf_o
->u_len
++] = (uint8_t)c
;
138 utf_o
->u_name
[utf_o
->u_len
++] = (uint8_t)(0xc0 | (c
>> 6));
139 utf_o
->u_name
[utf_o
->u_len
++] = (uint8_t)(0x80 | (c
& 0x3f));
143 utf_o
->u_name
[utf_o
->u_len
++] = (uint8_t)(0xe0 | (c
>> 12));
144 utf_o
->u_name
[utf_o
->u_len
++] = (uint8_t)(0x80 | ((c
>> 6) & 0x3f));
145 utf_o
->u_name
[utf_o
->u_len
++] = (uint8_t)(0x80 | (c
& 0x3f));
158 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
161 * This routine is only called by udf_lookup().
164 * ocu Pointer to OSTA Compressed Unicode output
165 * buffer of size UDF_NAME_LEN bytes.
166 * utf Pointer to UTF-8 input buffer.
167 * utf_len Length of UTF-8 input buffer in bytes.
170 * <return> Zero on success.
173 * November 12, 1997 - Andrew E. Mileski
174 * Written, tested, and released.
176 static int udf_UTF8toCS0(dstring
*ocu
, struct ustr
*utf
, int length
)
178 unsigned c
, i
, max_val
, utf_char
;
181 memset(ocu
, 0, sizeof(dstring
) * length
);
189 for (i
= 0U; i
< utf
->u_len
; i
++)
191 c
= (uint8_t)utf
->u_name
[i
];
193 /* Complete a multi-byte UTF-8 character */
196 utf_char
= (utf_char
<< 6) | (c
& 0x3fU
);
202 /* Check for a multi-byte UTF-8 character */
205 /* Start a multi-byte UTF-8 character */
206 if ((c
& 0xe0U
) == 0xc0U
)
208 utf_char
= c
& 0x1fU
;
211 else if ((c
& 0xf0U
) == 0xe0U
)
213 utf_char
= c
& 0x0fU
;
216 else if ((c
& 0xf8U
) == 0xf0U
)
218 utf_char
= c
& 0x07U
;
221 else if ((c
& 0xfcU
) == 0xf8U
)
223 utf_char
= c
& 0x03U
;
226 else if ((c
& 0xfeU
) == 0xfcU
)
228 utf_char
= c
& 0x01U
;
235 /* Single byte UTF-8 character (most common) */
239 /* Choose no compression if necessary */
240 if (utf_char
> max_val
)
242 if ( 0xffU
== max_val
)
245 ocu
[0] = (uint8_t)0x10U
;
251 if (max_val
== 0xffffU
)
253 ocu
[++u_len
] = (uint8_t)(utf_char
>> 8);
255 ocu
[++u_len
] = (uint8_t)(utf_char
& 0xffU
);
263 printk(KERN_DEBUG
"udf: bad UTF-8 character\n");
266 ocu
[length
- 1] = (uint8_t)u_len
+ 1;
270 static int udf_CS0toNLS(struct nls_table
*nls
, struct ustr
*utf_o
, struct ustr
*ocu_i
)
274 uint8_t cmp_id
, ocu_len
;
279 ocu_len
= ocu_i
->u_len
;
280 cmp_id
= ocu_i
->u_cmpID
;
285 memset(utf_o
, 0, sizeof(struct ustr
));
291 if ((cmp_id
!= 8) && (cmp_id
!= 16))
293 printk(KERN_ERR
"udf: unknown compression code (%d) stri=%s\n", cmp_id
, ocu_i
->u_name
);
297 for (i
= 0; (i
< ocu_len
) && (utf_o
->u_len
<= (UDF_NAME_LEN
-3)) ;)
299 /* Expand OSTA compressed Unicode to Unicode */
302 c
= (c
<< 8) | ocu
[i
++];
304 utf_o
->u_len
+= nls
->uni2char(c
, &utf_o
->u_name
[utf_o
->u_len
],
305 UDF_NAME_LEN
- utf_o
->u_len
);
312 static int udf_NLStoCS0(struct nls_table
*nls
, dstring
*ocu
, struct ustr
*uni
, int length
)
314 unsigned len
, i
, max_val
;
318 memset(ocu
, 0, sizeof(dstring
) * length
);
324 for (i
= 0U; i
< uni
->u_len
; i
++)
326 len
= nls
->char2uni(&uni
->u_name
[i
], uni
->u_len
-i
, &uni_char
);
330 if (uni_char
> max_val
)
333 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
, uint8_t *dname
, int flen
)
349 struct ustr filename
, unifilename
;
352 if (udf_build_ustr_exact(&unifilename
, sname
, flen
))
357 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UTF8
))
359 if (!udf_CS0toUTF8(&filename
, &unifilename
) )
361 udf_debug("Failed in udf_get_filename: sname = %s\n", sname
);
365 else if (UDF_QUERY_FLAG(sb
, UDF_FLAG_NLS_MAP
))
367 if (!udf_CS0toNLS(UDF_SB(sb
)->s_nls_map
, &filename
, &unifilename
) )
369 udf_debug("Failed in udf_get_filename: sname = %s\n", sname
);
376 if ((len
= udf_translate_to_linux(dname
, filename
.u_name
, filename
.u_len
,
377 unifilename
.u_name
, unifilename
.u_len
)))
384 int udf_put_filename(struct super_block
*sb
, const uint8_t *sname
, uint8_t *dname
, int flen
)
386 struct ustr unifilename
;
389 if ( !(udf_char_to_ustr(&unifilename
, sname
, flen
)) )
394 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UTF8
))
396 if ( !(namelen
= udf_UTF8toCS0(dname
, &unifilename
, UDF_NAME_LEN
)) )
401 else if (UDF_QUERY_FLAG(sb
, UDF_FLAG_NLS_MAP
))
403 if ( !(namelen
= udf_NLStoCS0(UDF_SB(sb
)->s_nls_map
, dname
, &unifilename
, UDF_NAME_LEN
)) )
414 #define ILLEGAL_CHAR_MARK '_'
419 static int udf_translate_to_linux(uint8_t *newName
, uint8_t *udfName
, int udfLen
, uint8_t *fidName
, int fidNameLen
)
421 int index
, newIndex
= 0, needsCRC
= 0;
422 int extIndex
= 0, newExtIndex
= 0, hasExt
= 0;
423 unsigned short valueCRC
;
425 const uint8_t hexChar
[] = "0123456789ABCDEF";
427 if (udfName
[0] == '.' && (udfLen
== 1 ||
428 (udfLen
== 2 && udfName
[1] == '.')))
432 memcpy(newName
, udfName
, udfLen
);
436 for (index
= 0; index
< udfLen
; index
++)
438 curr
= udfName
[index
];
439 if (curr
== '/' || curr
== 0)
442 curr
= ILLEGAL_CHAR_MARK
;
443 while (index
+1 < udfLen
&& (udfName
[index
+1] == '/' ||
444 udfName
[index
+1] == 0))
447 if (curr
== EXT_MARK
&& (udfLen
- index
- 1) <= EXT_SIZE
)
449 if (udfLen
== index
+ 1)
455 newExtIndex
= newIndex
;
459 newName
[newIndex
++] = curr
;
466 uint8_t ext
[EXT_SIZE
];
467 int localExtIndex
= 0;
472 for(index
= 0; index
<EXT_SIZE
&& extIndex
+ index
+1 < udfLen
;
475 curr
= udfName
[extIndex
+ index
+ 1];
477 if (curr
== '/' || curr
== 0)
480 curr
= ILLEGAL_CHAR_MARK
;
481 while(extIndex
+ index
+ 2 < udfLen
&& (index
+ 1 < EXT_SIZE
482 && (udfName
[extIndex
+ index
+ 2] == '/' ||
483 udfName
[extIndex
+ index
+ 2] == 0)))
486 ext
[localExtIndex
++] = curr
;
488 maxFilenameLen
= 250 - localExtIndex
;
489 if (newIndex
> maxFilenameLen
)
490 newIndex
= maxFilenameLen
;
492 newIndex
= newExtIndex
;
494 else if (newIndex
> 250)
496 newName
[newIndex
++] = CRC_MARK
;
497 valueCRC
= udf_crc(fidName
, fidNameLen
, 0);
498 newName
[newIndex
++] = hexChar
[(valueCRC
& 0xf000) >> 12];
499 newName
[newIndex
++] = hexChar
[(valueCRC
& 0x0f00) >> 8];
500 newName
[newIndex
++] = hexChar
[(valueCRC
& 0x00f0) >> 4];
501 newName
[newIndex
++] = hexChar
[(valueCRC
& 0x000f)];
505 newName
[newIndex
++] = EXT_MARK
;
506 for (index
= 0;index
< localExtIndex
;index
++ )
507 newName
[newIndex
++] = ext
[index
];