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 * E-mail regarding any portion of the Linux UDF file system should be
16 * directed to the development team's mailing list (run by majordomo):
17 * linux_udf@hpesjro.fc.hp.com
20 * This file is distributed under the terms of the GNU General Public
21 * License (GPL). Copies of the GPL can be obtained from:
22 * ftp://prep.ai.mit.edu/pub/gnu/GPL
23 * Each contributing author retains all rights to their own work.
28 #include <linux/kernel.h>
29 #include <linux/string.h> /* for memset */
30 #include <linux/nls.h>
31 #include <linux/udf_fs.h>
35 static int udf_translate_to_linux(uint8_t *, uint8_t *, int, uint8_t *, int);
37 static int udf_char_to_ustr(struct ustr *dest, const uint8_t *src, int strlen)
39 if ( (!dest) || (!src) || (!strlen) || (strlen > UDF_NAME_LEN-2) )
41 memset(dest, 0, sizeof(struct ustr));
42 memcpy(dest->u_name, src, strlen);
51 int udf_build_ustr(struct ustr *dest, dstring *ptr, int size)
55 if ( (!dest) || (!ptr) || (!size) )
58 memset(dest, 0, sizeof(struct ustr));
59 usesize= (size > UDF_NAME_LEN) ? UDF_NAME_LEN : size;
61 dest->u_len=ptr[size-1];
62 memcpy(dest->u_name, ptr+1, usesize-1);
67 * udf_build_ustr_exact
69 static int udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
71 if ( (!dest) || (!ptr) || (!exactsize) )
74 memset(dest, 0, sizeof(struct ustr));
76 dest->u_len=exactsize-1;
77 memcpy(dest->u_name, ptr+1, exactsize-1);
85 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
88 * This routine is only called by udf_filldir().
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, struct ustr *ocu_i)
107 uint8_t cmp_id, ocu_len;
112 ocu_len = ocu_i->u_len;
113 cmp_id = ocu_i->u_cmpID;
118 memset(utf_o, 0, sizeof(struct ustr));
124 if ((cmp_id != 8) && (cmp_id != 16))
126 printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n", cmp_id, ocu_i->u_name);
130 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN-3)) ;)
133 /* Expand OSTA compressed Unicode to Unicode */
136 c = (c << 8) | ocu[i++];
138 /* Compress Unicode to UTF-8 */
140 utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
143 utf_o->u_name[utf_o->u_len++] = (uint8_t)(0xc0 | (c >> 6));
144 utf_o->u_name[utf_o->u_len++] = (uint8_t)(0x80 | (c & 0x3f));
148 utf_o->u_name[utf_o->u_len++] = (uint8_t)(0xe0 | (c >> 12));
149 utf_o->u_name[utf_o->u_len++] = (uint8_t)(0x80 | ((c >> 6) & 0x3f));
150 utf_o->u_name[utf_o->u_len++] = (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++)
196 c = (uint8_t)utf->u_name[i];
198 /* Complete a multi-byte UTF-8 character */
201 utf_char = (utf_char << 6) | (c & 0x3fU);
207 /* Check for a multi-byte UTF-8 character */
210 /* Start a multi-byte UTF-8 character */
211 if ((c & 0xe0U) == 0xc0U)
213 utf_char = c & 0x1fU;
216 else if ((c & 0xf0U) == 0xe0U)
218 utf_char = c & 0x0fU;
221 else if ((c & 0xf8U) == 0xf0U)
223 utf_char = c & 0x07U;
226 else if ((c & 0xfcU) == 0xf8U)
228 utf_char = c & 0x03U;
231 else if ((c & 0xfeU) == 0xfcU)
233 utf_char = c & 0x01U;
240 /* Single byte UTF-8 character (most common) */
244 /* Choose no compression if necessary */
245 if (utf_char > max_val)
247 if ( 0xffU == max_val )
250 ocu[0] = (uint8_t)0x10U;
256 if (max_val == 0xffffU)
258 ocu[++u_len] = (uint8_t)(utf_char >> 8);
260 ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
268 printk(KERN_DEBUG "udf: bad UTF-8 character\n");
271 ocu[length - 1] = (uint8_t)u_len + 1;
275 static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o, struct ustr *ocu_i)
279 uint8_t cmp_id, ocu_len;
284 ocu_len = ocu_i->u_len;
285 cmp_id = ocu_i->u_cmpID;
290 memset(utf_o, 0, sizeof(struct ustr));
296 if ((cmp_id != 8) && (cmp_id != 16))
298 printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n", cmp_id, ocu_i->u_name);
302 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN-3)) ;)
304 /* Expand OSTA compressed Unicode to Unicode */
307 c = (c << 8) | ocu[i++];
309 utf_o->u_len += nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
310 UDF_NAME_LEN - utf_o->u_len);
317 static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni, int length)
319 unsigned len, i, max_val;
323 memset(ocu, 0, sizeof(dstring) * length);
329 for (i = 0U; i < uni->u_len; i++)
331 len = nls->char2uni(&uni->u_name[i], uni->u_len-i, &uni_char);
335 if (uni_char > max_val)
338 ocu[0] = (uint8_t)0x10U;
342 if (max_val == 0xffffU)
343 ocu[++u_len] = (uint8_t)(uni_char >> 8);
344 ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
348 ocu[length - 1] = (uint8_t)u_len + 1;
352 int udf_get_filename(struct super_block *sb, uint8_t *sname, uint8_t *dname, int flen)
354 struct ustr filename, unifilename;
357 if (udf_build_ustr_exact(&unifilename, sname, flen))
362 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
364 if (!udf_CS0toUTF8(&filename, &unifilename) )
366 udf_debug("Failed in udf_get_filename: sname = %s\n", sname);
370 else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
372 if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, &filename, &unifilename) )
374 udf_debug("Failed in udf_get_filename: sname = %s\n", sname);
381 if ((len = udf_translate_to_linux(dname, filename.u_name, filename.u_len,
382 unifilename.u_name, unifilename.u_len)))
389 int udf_put_filename(struct super_block *sb, const uint8_t *sname, uint8_t *dname, int flen)
391 struct ustr unifilename;
394 if ( !(udf_char_to_ustr(&unifilename, sname, flen)) )
399 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
401 if ( !(namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN)) )
406 else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
408 if ( !(namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname, &unifilename, UDF_NAME_LEN)) )
419 #define ILLEGAL_CHAR_MARK '_'
424 static int udf_translate_to_linux(uint8_t *newName, uint8_t *udfName, int udfLen, uint8_t *fidName, int fidNameLen)
426 int index, newIndex = 0, needsCRC = 0;
427 int extIndex = 0, newExtIndex = 0, hasExt = 0;
428 unsigned short valueCRC;
430 const uint8_t hexChar[] = "0123456789ABCDEF";
432 if (udfName[0] == '.' && (udfLen == 1 ||
433 (udfLen == 2 && udfName[1] == '.')))
437 memcpy(newName, udfName, udfLen);
441 for (index = 0; index < udfLen; index++)
443 curr = udfName[index];
444 if (curr == '/' || curr == 0)
447 curr = ILLEGAL_CHAR_MARK;
448 while (index+1 < udfLen && (udfName[index+1] == '/' ||
449 udfName[index+1] == 0))
452 if (curr == EXT_MARK && (udfLen - index - 1) <= EXT_SIZE)
454 if (udfLen == index + 1)
460 newExtIndex = newIndex;
464 newName[newIndex++] = curr;
471 uint8_t ext[EXT_SIZE];
472 int localExtIndex = 0;
477 for(index = 0; index<EXT_SIZE && extIndex + index +1 < udfLen;
480 curr = udfName[extIndex + index + 1];
482 if (curr == '/' || curr == 0)
485 curr = ILLEGAL_CHAR_MARK;
486 while(extIndex + index + 2 < udfLen && (index + 1 < EXT_SIZE
487 && (udfName[extIndex + index + 2] == '/' ||
488 udfName[extIndex + index + 2] == 0)))
491 ext[localExtIndex++] = curr;
493 maxFilenameLen = 250 - localExtIndex;
494 if (newIndex > maxFilenameLen)
495 newIndex = maxFilenameLen;
497 newIndex = newExtIndex;
499 else if (newIndex > 250)
501 newName[newIndex++] = CRC_MARK;
502 valueCRC = udf_crc(fidName, fidNameLen, 0);
503 newName[newIndex++] = hexChar[(valueCRC & 0xf000) >> 12];
504 newName[newIndex++] = hexChar[(valueCRC & 0x0f00) >> 8];
505 newName[newIndex++] = hexChar[(valueCRC & 0x00f0) >> 4];
506 newName[newIndex++] = hexChar[(valueCRC & 0x000f)];
510 newName[newIndex++] = EXT_MARK;
511 for (index = 0;index < localExtIndex ;index++ )
512 newName[newIndex++] = ext[index];