]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/udf/unicode.c
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 utf_o
->u_name
[utf_o
->u_len
++] =
132 (uint8_t)(0xc0 | (c
>> 6));
133 utf_o
->u_name
[utf_o
->u_len
++] =
134 (uint8_t)(0x80 | (c
& 0x3f));
136 utf_o
->u_name
[utf_o
->u_len
++] =
137 (uint8_t)(0xe0 | (c
>> 12));
138 utf_o
->u_name
[utf_o
->u_len
++] =
141 utf_o
->u_name
[utf_o
->u_len
++] =
142 (uint8_t)(0x80 | (c
& 0x3f));
155 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
158 * This routine is only called by udf_lookup().
161 * ocu Pointer to OSTA Compressed Unicode output
162 * buffer of size UDF_NAME_LEN bytes.
163 * utf Pointer to UTF-8 input buffer.
164 * utf_len Length of UTF-8 input buffer in bytes.
167 * <return> Zero on success.
170 * November 12, 1997 - Andrew E. Mileski
171 * Written, tested, and released.
173 static int udf_UTF8toCS0(dstring
*ocu
, struct ustr
*utf
, int length
)
175 unsigned c
, i
, max_val
, utf_char
;
178 memset(ocu
, 0, sizeof(dstring
) * length
);
186 for (i
= 0U; i
< utf
->u_len
; i
++) {
187 c
= (uint8_t)utf
->u_name
[i
];
189 /* Complete a multi-byte UTF-8 character */
191 utf_char
= (utf_char
<< 6) | (c
& 0x3fU
);
195 /* Check for a multi-byte UTF-8 character */
197 /* Start a multi-byte UTF-8 character */
198 if ((c
& 0xe0U
) == 0xc0U
) {
199 utf_char
= c
& 0x1fU
;
201 } else if ((c
& 0xf0U
) == 0xe0U
) {
202 utf_char
= c
& 0x0fU
;
204 } else if ((c
& 0xf8U
) == 0xf0U
) {
205 utf_char
= c
& 0x07U
;
207 } else if ((c
& 0xfcU
) == 0xf8U
) {
208 utf_char
= c
& 0x03U
;
210 } else if ((c
& 0xfeU
) == 0xfcU
) {
211 utf_char
= c
& 0x01U
;
218 /* Single byte UTF-8 character (most common) */
223 /* Choose no compression if necessary */
224 if (utf_char
> max_val
) {
225 if (max_val
== 0xffU
) {
227 ocu
[0] = (uint8_t)0x10U
;
233 if (max_val
== 0xffffU
)
234 ocu
[++u_len
] = (uint8_t)(utf_char
>> 8);
235 ocu
[++u_len
] = (uint8_t)(utf_char
& 0xffU
);
241 printk(KERN_DEBUG
pr_fmt("bad UTF-8 character\n"));
244 ocu
[length
- 1] = (uint8_t)u_len
+ 1;
249 static int udf_CS0toNLS(struct nls_table
*nls
, struct ustr
*utf_o
,
250 const struct ustr
*ocu_i
)
253 uint8_t cmp_id
, ocu_len
;
257 ocu_len
= ocu_i
->u_len
;
259 memset(utf_o
, 0, sizeof(struct ustr
));
263 cmp_id
= ocu_i
->u_cmpID
;
264 if (cmp_id
!= 8 && cmp_id
!= 16) {
265 memset(utf_o
, 0, sizeof(struct ustr
));
266 pr_err("unknown compression code (%d) stri=%s\n",
267 cmp_id
, ocu_i
->u_name
);
273 for (i
= 0; (i
< ocu_len
) && (utf_o
->u_len
<= (UDF_NAME_LEN
- 3));) {
274 /* Expand OSTA compressed Unicode to Unicode */
275 uint32_t c
= ocu
[i
++];
277 c
= (c
<< 8) | ocu
[i
++];
279 len
= nls
->uni2char(c
, &utf_o
->u_name
[utf_o
->u_len
],
280 UDF_NAME_LEN
- utf_o
->u_len
);
281 /* Valid character? */
285 utf_o
->u_name
[utf_o
->u_len
++] = '?';
292 static int udf_NLStoCS0(struct nls_table
*nls
, dstring
*ocu
, struct ustr
*uni
,
300 memset(ocu
, 0, sizeof(dstring
) * length
);
306 for (i
= 0U; i
< uni
->u_len
; i
++) {
307 len
= nls
->char2uni(&uni
->u_name
[i
], uni
->u_len
- i
, &uni_char
);
310 /* Invalid character, deal with it */
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
, int slen
,
333 uint8_t *dname
, int dlen
)
335 struct ustr
*filename
, *unifilename
;
341 filename
= kmalloc(sizeof(struct ustr
), GFP_NOFS
);
345 unifilename
= kmalloc(sizeof(struct ustr
), GFP_NOFS
);
351 udf_build_ustr_exact(unifilename
, sname
, slen
);
352 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UTF8
)) {
353 ret
= 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 ret
= udf_CS0toNLS(UDF_SB(sb
)->s_nls_map
, filename
,
363 udf_debug("Failed in udf_get_filename: sname = %s\n",
370 ret
= udf_translate_to_linux(dname
, dlen
,
371 filename
->u_name
, filename
->u_len
,
372 unifilename
->u_name
, unifilename
->u_len
);
373 /* Zero length filename isn't valid... */
383 int udf_put_filename(struct super_block
*sb
, const uint8_t *sname
,
384 uint8_t *dname
, int flen
)
386 struct ustr unifilename
;
389 if (!udf_char_to_ustr(&unifilename
, sname
, flen
))
392 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UTF8
)) {
393 namelen
= udf_UTF8toCS0(dname
, &unifilename
, UDF_NAME_LEN
);
396 } else if (UDF_QUERY_FLAG(sb
, UDF_FLAG_NLS_MAP
)) {
397 namelen
= udf_NLStoCS0(UDF_SB(sb
)->s_nls_map
, dname
,
398 &unifilename
, UDF_NAME_LEN
);
407 #define ILLEGAL_CHAR_MARK '_'
411 /* Number of chars we need to store generated CRC to make filename unique */
414 static int udf_translate_to_linux(uint8_t *newName
, int newLen
,
415 uint8_t *udfName
, int udfLen
,
416 uint8_t *fidName
, int fidNameLen
)
418 int index
, newIndex
= 0, needsCRC
= 0;
419 int extIndex
= 0, newExtIndex
= 0, hasExt
= 0;
420 unsigned short valueCRC
;
423 if (udfName
[0] == '.' &&
424 (udfLen
== 1 || (udfLen
== 2 && udfName
[1] == '.'))) {
427 memcpy(newName
, udfName
, udfLen
);
429 for (index
= 0; index
< udfLen
; index
++) {
430 curr
= udfName
[index
];
431 if (curr
== '/' || curr
== 0) {
433 curr
= ILLEGAL_CHAR_MARK
;
434 while (index
+ 1 < udfLen
&&
435 (udfName
[index
+ 1] == '/' ||
436 udfName
[index
+ 1] == 0))
439 if (curr
== EXT_MARK
&&
440 (udfLen
- index
- 1) <= EXT_SIZE
) {
441 if (udfLen
== index
+ 1)
446 newExtIndex
= newIndex
;
449 if (newIndex
< newLen
)
450 newName
[newIndex
++] = curr
;
456 uint8_t ext
[EXT_SIZE
];
457 int localExtIndex
= 0;
462 index
< EXT_SIZE
&& extIndex
+ index
+ 1 < udfLen
;
464 curr
= udfName
[extIndex
+ index
+ 1];
466 if (curr
== '/' || curr
== 0) {
468 curr
= ILLEGAL_CHAR_MARK
;
469 while (extIndex
+ index
+ 2 < udfLen
&&
470 (index
+ 1 < EXT_SIZE
&&
471 (udfName
[extIndex
+ index
+ 2] == '/' ||
472 udfName
[extIndex
+ index
+ 2] == 0)))
475 ext
[localExtIndex
++] = curr
;
477 maxFilenameLen
= newLen
- CRC_LEN
- localExtIndex
;
478 if (newIndex
> maxFilenameLen
)
479 newIndex
= maxFilenameLen
;
481 newIndex
= newExtIndex
;
482 } else if (newIndex
> newLen
- CRC_LEN
)
483 newIndex
= newLen
- CRC_LEN
;
484 newName
[newIndex
++] = CRC_MARK
;
485 valueCRC
= crc_itu_t(0, fidName
, fidNameLen
);
486 newName
[newIndex
++] = hex_asc_upper_hi(valueCRC
>> 8);
487 newName
[newIndex
++] = hex_asc_upper_lo(valueCRC
>> 8);
488 newName
[newIndex
++] = hex_asc_upper_hi(valueCRC
);
489 newName
[newIndex
++] = hex_asc_upper_lo(valueCRC
);
492 newName
[newIndex
++] = EXT_MARK
;
493 for (index
= 0; index
< localExtIndex
; index
++)
494 newName
[newIndex
++] = ext
[index
];