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 int udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
73 if ((!dest) || (!ptr) || (!exactsize))
76 memset(dest, 0, sizeof(struct ustr));
77 dest->u_cmpID = ptr[0];
78 dest->u_len = exactsize - 1;
79 memcpy(dest->u_name, ptr + 1, exactsize - 1);
88 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
91 * utf Pointer to UTF-8 output buffer.
92 * ocu Pointer to OSTA Compressed Unicode input buffer
93 * of size UDF_NAME_LEN bytes.
94 * both of type "struct ustr *"
97 * <return> Zero on success.
100 * November 12, 1997 - Andrew E. Mileski
101 * Written, tested, and released.
103 int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
106 uint8_t cmp_id, ocu_len;
109 ocu_len = ocu_i->u_len;
111 memset(utf_o, 0, sizeof(struct ustr));
115 cmp_id = ocu_i->u_cmpID;
116 if (cmp_id != 8 && cmp_id != 16) {
117 memset(utf_o, 0, sizeof(struct ustr));
118 pr_err("unknown compression code (%d) stri=%s\n",
119 cmp_id, ocu_i->u_name);
125 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
127 /* Expand OSTA compressed Unicode to Unicode */
128 uint32_t c = ocu[i++];
130 c = (c << 8) | ocu[i++];
132 /* Compress Unicode to UTF-8 */
134 utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
135 else if (c < 0x800U) {
136 if (utf_o->u_len > (UDF_NAME_LEN - 4))
138 utf_o->u_name[utf_o->u_len++] =
139 (uint8_t)(0xc0 | (c >> 6));
140 utf_o->u_name[utf_o->u_len++] =
141 (uint8_t)(0x80 | (c & 0x3f));
143 if (utf_o->u_len > (UDF_NAME_LEN - 5))
145 utf_o->u_name[utf_o->u_len++] =
146 (uint8_t)(0xe0 | (c >> 12));
147 utf_o->u_name[utf_o->u_len++] =
150 utf_o->u_name[utf_o->u_len++] =
151 (uint8_t)(0x80 | (c & 0x3f));
164 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
167 * This routine is only called by udf_lookup().
170 * ocu Pointer to OSTA Compressed Unicode output
171 * buffer of size UDF_NAME_LEN bytes.
172 * utf Pointer to UTF-8 input buffer.
173 * utf_len Length of UTF-8 input buffer in bytes.
176 * <return> Zero on success.
179 * November 12, 1997 - Andrew E. Mileski
180 * Written, tested, and released.
182 static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
184 unsigned c, i, max_val, utf_char;
187 memset(ocu, 0, sizeof(dstring) * length);
195 for (i = 0U; i < utf->u_len; i++) {
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;
242 if (max_val == 0xffffU)
243 ocu[++u_len] = (uint8_t)(utf_char >> 8);
244 ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
250 printk(KERN_DEBUG pr_fmt("bad UTF-8 character\n"));
253 ocu[length - 1] = (uint8_t)u_len + 1;
258 static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
259 const struct ustr *ocu_i)
262 uint8_t cmp_id, ocu_len;
266 ocu_len = ocu_i->u_len;
268 memset(utf_o, 0, sizeof(struct ustr));
272 cmp_id = ocu_i->u_cmpID;
273 if (cmp_id != 8 && cmp_id != 16) {
274 memset(utf_o, 0, sizeof(struct ustr));
275 pr_err("unknown compression code (%d) stri=%s\n",
276 cmp_id, ocu_i->u_name);
282 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
283 /* Expand OSTA compressed Unicode to Unicode */
284 uint32_t c = ocu[i++];
286 c = (c << 8) | ocu[i++];
288 len = nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
289 UDF_NAME_LEN - 2 - utf_o->u_len);
290 /* Valid character? */
294 utf_o->u_name[utf_o->u_len++] = '?';
301 static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
309 memset(ocu, 0, sizeof(dstring) * length);
315 for (i = 0U; i < uni->u_len; i++) {
316 len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
319 /* Invalid character, deal with it */
325 if (uni_char > max_val) {
327 ocu[0] = (uint8_t)0x10U;
331 if (max_val == 0xffffU)
332 ocu[++u_len] = (uint8_t)(uni_char >> 8);
333 ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
337 ocu[length - 1] = (uint8_t)u_len + 1;
341 int udf_get_filename(struct super_block *sb, uint8_t *sname, int slen,
342 uint8_t *dname, int dlen)
344 struct ustr *filename, *unifilename;
347 filename = kmalloc(sizeof(struct ustr), GFP_NOFS);
351 unifilename = kmalloc(sizeof(struct ustr), GFP_NOFS);
355 if (udf_build_ustr_exact(unifilename, sname, slen))
358 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
359 if (!udf_CS0toUTF8(filename, unifilename)) {
360 udf_debug("Failed in udf_get_filename: sname = %s\n",
364 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
365 if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, filename,
367 udf_debug("Failed in udf_get_filename: sname = %s\n",
374 len = udf_translate_to_linux(dname, dlen,
375 filename->u_name, filename->u_len,
376 unifilename->u_name, unifilename->u_len);
384 int udf_put_filename(struct super_block *sb, const uint8_t *sname,
385 uint8_t *dname, int flen)
387 struct ustr unifilename;
390 if (!udf_char_to_ustr(&unifilename, sname, flen))
393 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
394 namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
397 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
398 namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
399 &unifilename, UDF_NAME_LEN);
408 #define ILLEGAL_CHAR_MARK '_'
412 /* Number of chars we need to store generated CRC to make filename unique */
415 static int udf_translate_to_linux(uint8_t *newName, int newLen,
416 uint8_t *udfName, int udfLen,
417 uint8_t *fidName, int fidNameLen)
419 int index, newIndex = 0, needsCRC = 0;
420 int extIndex = 0, newExtIndex = 0, hasExt = 0;
421 unsigned short valueCRC;
423 const uint8_t hexChar[] = "0123456789ABCDEF";
425 if (udfName[0] == '.' &&
426 (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
429 memcpy(newName, udfName, udfLen);
431 for (index = 0; index < udfLen; index++) {
432 curr = udfName[index];
433 if (curr == '/' || curr == 0) {
435 curr = ILLEGAL_CHAR_MARK;
436 while (index + 1 < udfLen &&
437 (udfName[index + 1] == '/' ||
438 udfName[index + 1] == 0))
441 if (curr == EXT_MARK &&
442 (udfLen - index - 1) <= EXT_SIZE) {
443 if (udfLen == index + 1)
448 newExtIndex = newIndex;
451 if (newIndex < newLen)
452 newName[newIndex++] = curr;
458 uint8_t ext[EXT_SIZE];
459 int localExtIndex = 0;
464 index < EXT_SIZE && extIndex + index + 1 < udfLen;
466 curr = udfName[extIndex + index + 1];
468 if (curr == '/' || curr == 0) {
470 curr = ILLEGAL_CHAR_MARK;
471 while (extIndex + index + 2 < udfLen &&
472 (index + 1 < EXT_SIZE &&
473 (udfName[extIndex + index + 2] == '/' ||
474 udfName[extIndex + index + 2] == 0)))
477 ext[localExtIndex++] = curr;
479 maxFilenameLen = newLen - CRC_LEN - localExtIndex;
480 if (newIndex > maxFilenameLen)
481 newIndex = maxFilenameLen;
483 newIndex = newExtIndex;
484 } else if (newIndex > newLen - CRC_LEN)
485 newIndex = newLen - CRC_LEN;
486 newName[newIndex++] = CRC_MARK;
487 valueCRC = crc_itu_t(0, fidName, fidNameLen);
488 newName[newIndex++] = hexChar[(valueCRC & 0xf000) >> 12];
489 newName[newIndex++] = hexChar[(valueCRC & 0x0f00) >> 8];
490 newName[newIndex++] = hexChar[(valueCRC & 0x00f0) >> 4];
491 newName[newIndex++] = hexChar[(valueCRC & 0x000f)];
494 newName[newIndex++] = EXT_MARK;
495 for (index = 0; index < localExtIndex; index++)
496 newName[newIndex++] = ext[index];