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>
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)
56 usesize = min_t(size_t, ptr[size - 1], sizeof(dest->u_name));
57 usesize = min(usesize, size - 2);
58 dest->u_cmpID = ptr[0];
59 dest->u_len = usesize;
60 memcpy(dest->u_name, ptr + 1, usesize);
61 memset(dest->u_name + usesize, 0, sizeof(dest->u_name) - usesize);
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));
75 dest->u_cmpID = ptr[0];
76 dest->u_len = exactsize - 1;
77 memcpy(dest->u_name, ptr + 1, exactsize - 1);
86 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
89 * utf Pointer to UTF-8 output buffer.
90 * ocu Pointer to OSTA Compressed Unicode input buffer
91 * of size UDF_NAME_LEN bytes.
92 * both of type "struct ustr *"
95 * <return> Zero on success.
98 * November 12, 1997 - Andrew E. Mileski
99 * Written, tested, and released.
101 int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
104 uint8_t cmp_id, ocu_len;
107 ocu_len = ocu_i->u_len;
109 memset(utf_o, 0, sizeof(struct ustr));
113 cmp_id = ocu_i->u_cmpID;
114 if (cmp_id != 8 && cmp_id != 16) {
115 memset(utf_o, 0, sizeof(struct ustr));
116 printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
117 cmp_id, ocu_i->u_name);
123 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
125 /* Expand OSTA compressed Unicode to Unicode */
126 uint32_t c = ocu[i++];
128 c = (c << 8) | ocu[i++];
130 /* Compress Unicode to UTF-8 */
132 utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
133 else if (c < 0x800U) {
134 utf_o->u_name[utf_o->u_len++] =
135 (uint8_t)(0xc0 | (c >> 6));
136 utf_o->u_name[utf_o->u_len++] =
137 (uint8_t)(0x80 | (c & 0x3f));
139 utf_o->u_name[utf_o->u_len++] =
140 (uint8_t)(0xe0 | (c >> 12));
141 utf_o->u_name[utf_o->u_len++] =
144 utf_o->u_name[utf_o->u_len++] =
145 (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++) {
190 c = (uint8_t)utf->u_name[i];
192 /* Complete a multi-byte UTF-8 character */
194 utf_char = (utf_char << 6) | (c & 0x3fU);
198 /* Check for a multi-byte UTF-8 character */
200 /* Start a multi-byte UTF-8 character */
201 if ((c & 0xe0U) == 0xc0U) {
202 utf_char = c & 0x1fU;
204 } else if ((c & 0xf0U) == 0xe0U) {
205 utf_char = c & 0x0fU;
207 } else if ((c & 0xf8U) == 0xf0U) {
208 utf_char = c & 0x07U;
210 } else if ((c & 0xfcU) == 0xf8U) {
211 utf_char = c & 0x03U;
213 } else if ((c & 0xfeU) == 0xfcU) {
214 utf_char = c & 0x01U;
221 /* Single byte UTF-8 character (most common) */
226 /* Choose no compression if necessary */
227 if (utf_char > max_val) {
228 if (max_val == 0xffU) {
230 ocu[0] = (uint8_t)0x10U;
236 if (max_val == 0xffffU)
237 ocu[++u_len] = (uint8_t)(utf_char >> 8);
238 ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
244 printk(KERN_DEBUG "udf: bad UTF-8 character\n");
247 ocu[length - 1] = (uint8_t)u_len + 1;
252 static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
253 const struct ustr *ocu_i)
256 uint8_t cmp_id, ocu_len;
260 ocu_len = ocu_i->u_len;
262 memset(utf_o, 0, sizeof(struct ustr));
266 cmp_id = ocu_i->u_cmpID;
267 if (cmp_id != 8 && cmp_id != 16) {
268 memset(utf_o, 0, sizeof(struct ustr));
269 printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
270 cmp_id, ocu_i->u_name);
276 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
277 /* Expand OSTA compressed Unicode to Unicode */
278 uint32_t c = ocu[i++];
280 c = (c << 8) | ocu[i++];
282 len = nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
283 UDF_NAME_LEN - utf_o->u_len);
284 /* Valid character? */
288 utf_o->u_name[utf_o->u_len++] = '?';
295 static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
303 memset(ocu, 0, sizeof(dstring) * length);
309 for (i = 0U; i < uni->u_len; i++) {
310 len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
313 /* Invalid character, deal with it */
319 if (uni_char > max_val) {
321 ocu[0] = (uint8_t)0x10U;
325 if (max_val == 0xffffU)
326 ocu[++u_len] = (uint8_t)(uni_char >> 8);
327 ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
331 ocu[length - 1] = (uint8_t)u_len + 1;
335 int udf_get_filename(struct super_block *sb, uint8_t *sname, uint8_t *dname,
338 struct ustr *filename, *unifilename;
341 filename = kmalloc(sizeof(struct ustr), GFP_NOFS);
345 unifilename = kmalloc(sizeof(struct ustr), GFP_NOFS);
349 if (udf_build_ustr_exact(unifilename, sname, flen))
352 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
353 if (!udf_CS0toUTF8(filename, unifilename)) {
354 udf_debug("Failed in udf_get_filename: sname = %s\n",
358 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
359 if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, filename,
361 udf_debug("Failed in udf_get_filename: sname = %s\n",
368 len = udf_translate_to_linux(dname, filename->u_name, filename->u_len,
369 unifilename->u_name, unifilename->u_len);
377 int udf_put_filename(struct super_block *sb, const uint8_t *sname,
378 uint8_t *dname, int flen)
380 struct ustr unifilename;
383 if (!udf_char_to_ustr(&unifilename, sname, flen))
386 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
387 namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
390 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
391 namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
392 &unifilename, UDF_NAME_LEN);
401 #define ILLEGAL_CHAR_MARK '_'
406 static int udf_translate_to_linux(uint8_t *newName, uint8_t *udfName,
407 int udfLen, uint8_t *fidName,
410 int index, newIndex = 0, needsCRC = 0;
411 int extIndex = 0, newExtIndex = 0, hasExt = 0;
412 unsigned short valueCRC;
414 const uint8_t hexChar[] = "0123456789ABCDEF";
416 if (udfName[0] == '.' &&
417 (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
420 memcpy(newName, udfName, udfLen);
422 for (index = 0; index < udfLen; index++) {
423 curr = udfName[index];
424 if (curr == '/' || curr == 0) {
426 curr = ILLEGAL_CHAR_MARK;
427 while (index + 1 < udfLen &&
428 (udfName[index + 1] == '/' ||
429 udfName[index + 1] == 0))
432 if (curr == EXT_MARK &&
433 (udfLen - index - 1) <= EXT_SIZE) {
434 if (udfLen == index + 1)
439 newExtIndex = newIndex;
443 newName[newIndex++] = curr;
449 uint8_t ext[EXT_SIZE];
450 int localExtIndex = 0;
455 index < EXT_SIZE && extIndex + index + 1 < udfLen;
457 curr = udfName[extIndex + index + 1];
459 if (curr == '/' || curr == 0) {
461 curr = ILLEGAL_CHAR_MARK;
462 while (extIndex + index + 2 < udfLen &&
463 (index + 1 < EXT_SIZE &&
464 (udfName[extIndex + index + 2] == '/' ||
465 udfName[extIndex + index + 2] == 0)))
468 ext[localExtIndex++] = curr;
470 maxFilenameLen = 250 - localExtIndex;
471 if (newIndex > maxFilenameLen)
472 newIndex = maxFilenameLen;
474 newIndex = newExtIndex;
475 } else if (newIndex > 250)
477 newName[newIndex++] = CRC_MARK;
478 valueCRC = crc_itu_t(0, fidName, fidNameLen);
479 newName[newIndex++] = hexChar[(valueCRC & 0xf000) >> 12];
480 newName[newIndex++] = hexChar[(valueCRC & 0x0f00) >> 8];
481 newName[newIndex++] = hexChar[(valueCRC & 0x00f0) >> 4];
482 newName[newIndex++] = hexChar[(valueCRC & 0x000f)];
485 newName[newIndex++] = EXT_MARK;
486 for (index = 0; index < localExtIndex; index++)
487 newName[newIndex++] = ext[index];