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>
29 static int udf_translate_to_linux(uint8_t *, uint8_t *, int, uint8_t *, int);
31 static int udf_char_to_ustr(struct ustr *dest, const uint8_t *src, int strlen)
33 if ((!dest) || (!src) || (!strlen) || (strlen > UDF_NAME_LEN - 2))
36 memset(dest, 0, sizeof(struct ustr));
37 memcpy(dest->u_name, src, strlen);
47 int udf_build_ustr(struct ustr *dest, dstring *ptr, int size)
51 if ((!dest) || (!ptr) || (!size))
54 memset(dest, 0, sizeof(struct ustr));
55 usesize = (size > UDF_NAME_LEN) ? UDF_NAME_LEN : size;
56 dest->u_cmpID = ptr[0];
57 dest->u_len = ptr[size - 1];
58 memcpy(dest->u_name, ptr + 1, usesize - 1);
64 * udf_build_ustr_exact
66 static int udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
68 if ((!dest) || (!ptr) || (!exactsize))
71 memset(dest, 0, sizeof(struct ustr));
72 dest->u_cmpID = ptr[0];
73 dest->u_len = exactsize - 1;
74 memcpy(dest->u_name, ptr + 1, exactsize - 1);
83 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
86 * This routine is only called by udf_filldir().
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, struct ustr *ocu_i)
105 uint8_t cmp_id, ocu_len;
110 ocu_len = ocu_i->u_len;
111 cmp_id = ocu_i->u_cmpID;
115 memset(utf_o, 0, sizeof(struct ustr));
121 if ((cmp_id != 8) && (cmp_id != 16)) {
122 printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
123 cmp_id, ocu_i->u_name);
127 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
129 /* Expand OSTA compressed Unicode to Unicode */
132 c = (c << 8) | ocu[i++];
134 /* Compress Unicode to UTF-8 */
136 utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
137 } else if (c < 0x800U) {
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 utf_o->u_name[utf_o->u_len++] =
144 (uint8_t)(0xe0 | (c >> 12));
145 utf_o->u_name[utf_o->u_len++] =
148 utf_o->u_name[utf_o->u_len++] =
149 (uint8_t)(0x80 | (c & 0x3f));
162 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
165 * This routine is only called by udf_lookup().
168 * ocu Pointer to OSTA Compressed Unicode output
169 * buffer of size UDF_NAME_LEN bytes.
170 * utf Pointer to UTF-8 input buffer.
171 * utf_len Length of UTF-8 input buffer in bytes.
174 * <return> Zero on success.
177 * November 12, 1997 - Andrew E. Mileski
178 * Written, tested, and released.
180 static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
182 unsigned c, i, max_val, utf_char;
185 memset(ocu, 0, sizeof(dstring) * length);
193 for (i = 0U; i < utf->u_len; i++) {
194 c = (uint8_t)utf->u_name[i];
196 /* Complete a multi-byte UTF-8 character */
198 utf_char = (utf_char << 6) | (c & 0x3fU);
202 /* Check for a multi-byte UTF-8 character */
204 /* Start a multi-byte UTF-8 character */
205 if ((c & 0xe0U) == 0xc0U) {
206 utf_char = c & 0x1fU;
208 } else if ((c & 0xf0U) == 0xe0U) {
209 utf_char = c & 0x0fU;
211 } else if ((c & 0xf8U) == 0xf0U) {
212 utf_char = c & 0x07U;
214 } else if ((c & 0xfcU) == 0xf8U) {
215 utf_char = c & 0x03U;
217 } else if ((c & 0xfeU) == 0xfcU) {
218 utf_char = c & 0x01U;
225 /* Single byte UTF-8 character (most common) */
230 /* Choose no compression if necessary */
231 if (utf_char > max_val) {
232 if (max_val == 0xffU) {
234 ocu[0] = (uint8_t)0x10U;
240 if (max_val == 0xffffU)
241 ocu[++u_len] = (uint8_t)(utf_char >> 8);
242 ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
248 printk(KERN_DEBUG "udf: bad UTF-8 character\n");
251 ocu[length - 1] = (uint8_t)u_len + 1;
256 static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
261 uint8_t cmp_id, ocu_len;
266 ocu_len = ocu_i->u_len;
267 cmp_id = ocu_i->u_cmpID;
271 memset(utf_o, 0, sizeof(struct ustr));
277 if ((cmp_id != 8) && (cmp_id != 16)) {
278 printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
279 cmp_id, ocu_i->u_name);
283 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
284 /* Expand OSTA compressed Unicode to Unicode */
287 c = (c << 8) | ocu[i++];
289 utf_o->u_len += nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
290 UDF_NAME_LEN - utf_o->u_len);
297 static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
300 unsigned len, i, max_val;
304 memset(ocu, 0, sizeof(dstring) * length);
310 for (i = 0U; i < uni->u_len; i++) {
311 len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
315 if (uni_char > max_val) {
317 ocu[0] = (uint8_t)0x10U;
321 if (max_val == 0xffffU)
322 ocu[++u_len] = (uint8_t)(uni_char >> 8);
323 ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
327 ocu[length - 1] = (uint8_t)u_len + 1;
331 int udf_get_filename(struct super_block *sb, uint8_t *sname, uint8_t *dname,
334 struct ustr filename, unifilename;
337 if (udf_build_ustr_exact(&unifilename, sname, flen))
340 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
341 if (!udf_CS0toUTF8(&filename, &unifilename)) {
342 udf_debug("Failed in udf_get_filename: sname = %s\n",
346 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
347 if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, &filename,
349 udf_debug("Failed in udf_get_filename: sname = %s\n",
356 len = udf_translate_to_linux(dname, filename.u_name, filename.u_len,
357 unifilename.u_name, unifilename.u_len);
364 int udf_put_filename(struct super_block *sb, const uint8_t *sname,
365 uint8_t *dname, int flen)
367 struct ustr unifilename;
370 if (!udf_char_to_ustr(&unifilename, sname, flen))
373 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
374 namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
377 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
378 namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
379 &unifilename, UDF_NAME_LEN);
388 #define ILLEGAL_CHAR_MARK '_'
393 static int udf_translate_to_linux(uint8_t *newName, uint8_t *udfName,
394 int udfLen, uint8_t *fidName,
397 int index, newIndex = 0, needsCRC = 0;
398 int extIndex = 0, newExtIndex = 0, hasExt = 0;
399 unsigned short valueCRC;
401 const uint8_t hexChar[] = "0123456789ABCDEF";
403 if (udfName[0] == '.' &&
404 (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
407 memcpy(newName, udfName, udfLen);
409 for (index = 0; index < udfLen; index++) {
410 curr = udfName[index];
411 if (curr == '/' || curr == 0) {
413 curr = ILLEGAL_CHAR_MARK;
414 while (index + 1 < udfLen &&
415 (udfName[index + 1] == '/' ||
416 udfName[index + 1] == 0))
419 if (curr == EXT_MARK &&
420 (udfLen - index - 1) <= EXT_SIZE) {
421 if (udfLen == index + 1)
426 newExtIndex = newIndex;
430 newName[newIndex++] = curr;
436 uint8_t ext[EXT_SIZE];
437 int localExtIndex = 0;
442 index < EXT_SIZE && extIndex + index + 1 < udfLen;
444 curr = udfName[extIndex + index + 1];
446 if (curr == '/' || curr == 0) {
448 curr = ILLEGAL_CHAR_MARK;
449 while (extIndex + index + 2 < udfLen &&
450 (index + 1 < EXT_SIZE &&
451 (udfName[extIndex + index + 2] == '/' ||
452 udfName[extIndex + index + 2] == 0)))
455 ext[localExtIndex++] = curr;
457 maxFilenameLen = 250 - localExtIndex;
458 if (newIndex > maxFilenameLen)
459 newIndex = maxFilenameLen;
461 newIndex = newExtIndex;
462 } else if (newIndex > 250)
464 newName[newIndex++] = CRC_MARK;
465 valueCRC = udf_crc(fidName, fidNameLen, 0);
466 newName[newIndex++] = hexChar[(valueCRC & 0xf000) >> 12];
467 newName[newIndex++] = hexChar[(valueCRC & 0x0f00) >> 8];
468 newName[newIndex++] = hexChar[(valueCRC & 0x00f0) >> 4];
469 newName[newIndex++] = hexChar[(valueCRC & 0x000f)];
472 newName[newIndex++] = EXT_MARK;
473 for (index = 0; index < localExtIndex; index++)
474 newName[newIndex++] = ext[index];