blob: f5872ae325a3a67308dd642b9a793ea1a2f9d540 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * unicode.c
3 *
4 * PURPOSE
5 * Routines for converting between UTF-8 and OSTA Compressed Unicode.
6 * Also handles filename mangling
7 *
8 * DESCRIPTION
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
13 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * COPYRIGHT
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.
19 */
20
21#include "udfdecl.h"
22
23#include <linux/kernel.h>
24#include <linux/string.h> /* for memset */
25#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "udf_sb.h"
28
29static int udf_translate_to_linux(uint8_t *, uint8_t *, int, uint8_t *, int);
30
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070031static int udf_char_to_ustr(struct ustr *dest, const uint8_t *src, int strlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070033 if ((!dest) || (!src) || (!strlen) || (strlen > UDF_NAME_LEN - 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 memset(dest, 0, sizeof(struct ustr));
37 memcpy(dest->u_name, src, strlen);
38 dest->u_cmpID = 0x08;
39 dest->u_len = strlen;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return strlen;
42}
43
44/*
45 * udf_build_ustr
46 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070047int udf_build_ustr(struct ustr *dest, dstring *ptr, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 int usesize;
50
Marcin Slusarz6305a0a2008-02-04 22:27:39 +010051 if (!dest || !ptr || !size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 return -1;
Marcin Slusarz6305a0a2008-02-04 22:27:39 +010053 BUG_ON(size < 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Marcin Slusarz6305a0a2008-02-04 22:27:39 +010055 usesize = min_t(size_t, ptr[size - 1], sizeof(dest->u_name));
56 usesize = min(usesize, size - 2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070057 dest->u_cmpID = ptr[0];
Marcin Slusarz6305a0a2008-02-04 22:27:39 +010058 dest->u_len = usesize;
59 memcpy(dest->u_name, ptr + 1, usesize);
60 memset(dest->u_name + usesize, 0, sizeof(dest->u_name) - usesize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return 0;
63}
64
65/*
66 * udf_build_ustr_exact
67 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070068static int udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070070 if ((!dest) || (!ptr) || (!exactsize))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return -1;
72
73 memset(dest, 0, sizeof(struct ustr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070074 dest->u_cmpID = ptr[0];
75 dest->u_len = exactsize - 1;
76 memcpy(dest->u_name, ptr + 1, exactsize - 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return 0;
79}
80
81/*
82 * udf_ocu_to_utf8
83 *
84 * PURPOSE
85 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
86 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * PRE-CONDITIONS
88 * utf Pointer to UTF-8 output buffer.
89 * ocu Pointer to OSTA Compressed Unicode input buffer
90 * of size UDF_NAME_LEN bytes.
91 * both of type "struct ustr *"
92 *
93 * POST-CONDITIONS
94 * <return> Zero on success.
95 *
96 * HISTORY
97 * November 12, 1997 - Andrew E. Mileski
98 * Written, tested, and released.
99 */
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100100int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100102 const uint8_t *ocu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 uint8_t cmp_id, ocu_len;
104 int i;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 ocu_len = ocu_i->u_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700107 if (ocu_len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 memset(utf_o, 0, sizeof(struct ustr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return 0;
110 }
111
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100112 cmp_id = ocu_i->u_cmpID;
113 if (cmp_id != 8 && cmp_id != 16) {
114 memset(utf_o, 0, sizeof(struct ustr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700115 printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
116 cmp_id, ocu_i->u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118 }
119
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100120 ocu = ocu_i->u_name;
121 utf_o->u_len = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700122 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /* Expand OSTA compressed Unicode to Unicode */
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100125 uint32_t c = ocu[i++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (cmp_id == 16)
127 c = (c << 8) | ocu[i++];
128
129 /* Compress Unicode to UTF-8 */
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100130 if (c < 0x80U)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700131 utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100132 else if (c < 0x800U) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800133 utf_o->u_name[utf_o->u_len++] =
134 (uint8_t)(0xc0 | (c >> 6));
135 utf_o->u_name[utf_o->u_len++] =
136 (uint8_t)(0x80 | (c & 0x3f));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700137 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800138 utf_o->u_name[utf_o->u_len++] =
139 (uint8_t)(0xe0 | (c >> 12));
140 utf_o->u_name[utf_o->u_len++] =
141 (uint8_t)(0x80 |
142 ((c >> 6) & 0x3f));
143 utf_o->u_name[utf_o->u_len++] =
144 (uint8_t)(0x80 | (c & 0x3f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700147 utf_o->u_cmpID = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 return utf_o->u_len;
150}
151
152/*
153 *
154 * udf_utf8_to_ocu
155 *
156 * PURPOSE
157 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
158 *
159 * DESCRIPTION
160 * This routine is only called by udf_lookup().
161 *
162 * PRE-CONDITIONS
163 * ocu Pointer to OSTA Compressed Unicode output
164 * buffer of size UDF_NAME_LEN bytes.
165 * utf Pointer to UTF-8 input buffer.
166 * utf_len Length of UTF-8 input buffer in bytes.
167 *
168 * POST-CONDITIONS
169 * <return> Zero on success.
170 *
171 * HISTORY
172 * November 12, 1997 - Andrew E. Mileski
173 * Written, tested, and released.
174 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700175static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 unsigned c, i, max_val, utf_char;
178 int utf_cnt, u_len;
179
180 memset(ocu, 0, sizeof(dstring) * length);
181 ocu[0] = 8;
182 max_val = 0xffU;
183
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700184try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 u_len = 0U;
186 utf_char = 0U;
187 utf_cnt = 0U;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700188 for (i = 0U; i < utf->u_len; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700189 c = (uint8_t)utf->u_name[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /* Complete a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700192 if (utf_cnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 utf_char = (utf_char << 6) | (c & 0x3fU);
194 if (--utf_cnt)
195 continue;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700196 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* Check for a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700198 if (c & 0x80U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Start a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700200 if ((c & 0xe0U) == 0xc0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 utf_char = c & 0x1fU;
202 utf_cnt = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700203 } else if ((c & 0xf0U) == 0xe0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 utf_char = c & 0x0fU;
205 utf_cnt = 2;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700206 } else if ((c & 0xf8U) == 0xf0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 utf_char = c & 0x07U;
208 utf_cnt = 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700209 } else if ((c & 0xfcU) == 0xf8U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 utf_char = c & 0x03U;
211 utf_cnt = 4;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700212 } else if ((c & 0xfeU) == 0xfcU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 utf_char = c & 0x01U;
214 utf_cnt = 5;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700215 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 goto error_out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 continue;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700219 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* Single byte UTF-8 character (most common) */
221 utf_char = c;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224
225 /* Choose no compression if necessary */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700226 if (utf_char > max_val) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700227 if (max_val == 0xffU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 max_val = 0xffffU;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700229 ocu[0] = (uint8_t)0x10U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 goto try_again;
231 }
232 goto error_out;
233 }
234
Marcin Slusarz4b111112008-02-08 04:20:36 -0800235 if (max_val == 0xffffU)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700236 ocu[++u_len] = (uint8_t)(utf_char >> 8);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700237 ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700240 if (utf_cnt) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700241error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 ocu[++u_len] = '?';
243 printk(KERN_DEBUG "udf: bad UTF-8 character\n");
244 }
245
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700246 ocu[length - 1] = (uint8_t)u_len + 1;
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return u_len + 1;
249}
250
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700251static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100252 const struct ustr *ocu_i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100254 const uint8_t *ocu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 uint8_t cmp_id, ocu_len;
256 int i;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 ocu_len = ocu_i->u_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700260 if (ocu_len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 memset(utf_o, 0, sizeof(struct ustr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 0;
263 }
264
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100265 cmp_id = ocu_i->u_cmpID;
266 if (cmp_id != 8 && cmp_id != 16) {
267 memset(utf_o, 0, sizeof(struct ustr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700268 printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
269 cmp_id, ocu_i->u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return 0;
271 }
272
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100273 ocu = ocu_i->u_name;
274 utf_o->u_len = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700275 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* Expand OSTA compressed Unicode to Unicode */
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100277 uint32_t c = ocu[i++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (cmp_id == 16)
279 c = (c << 8) | ocu[i++];
280
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700281 utf_o->u_len += nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
282 UDF_NAME_LEN - utf_o->u_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700284 utf_o->u_cmpID = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 return utf_o->u_len;
287}
288
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700289static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700290 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 unsigned len, i, max_val;
293 uint16_t uni_char;
294 int u_len;
295
296 memset(ocu, 0, sizeof(dstring) * length);
297 ocu[0] = 8;
298 max_val = 0xffU;
299
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700300try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 u_len = 0U;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700302 for (i = 0U; i < uni->u_len; i++) {
303 len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (len <= 0)
305 continue;
306
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700307 if (uni_char > max_val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 max_val = 0xffffU;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700309 ocu[0] = (uint8_t)0x10U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 goto try_again;
311 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (max_val == 0xffffU)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700314 ocu[++u_len] = (uint8_t)(uni_char >> 8);
315 ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 i += len - 1;
317 }
318
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700319 ocu[length - 1] = (uint8_t)u_len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return u_len + 1;
321}
322
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700323int udf_get_filename(struct super_block *sb, uint8_t *sname, uint8_t *dname,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700324 int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct ustr filename, unifilename;
327 int len;
328
Marcin Slusarz4b111112008-02-08 04:20:36 -0800329 if (udf_build_ustr_exact(&unifilename, sname, flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700332 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
333 if (!udf_CS0toUTF8(&filename, &unifilename)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800334 udf_debug("Failed in udf_get_filename: sname = %s\n",
335 sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700338 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800339 if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, &filename,
340 &unifilename)) {
341 udf_debug("Failed in udf_get_filename: sname = %s\n",
342 sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800345 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return 0;
347
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700348 len = udf_translate_to_linux(dname, filename.u_name, filename.u_len,
349 unifilename.u_name, unifilename.u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800350 if (len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return len;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354}
355
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700356int udf_put_filename(struct super_block *sb, const uint8_t *sname,
357 uint8_t *dname, int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct ustr unifilename;
360 int namelen;
361
Marcin Slusarz4b111112008-02-08 04:20:36 -0800362 if (!udf_char_to_ustr(&unifilename, sname, flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700365 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700366 namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800367 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700369 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800370 namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
371 &unifilename, UDF_NAME_LEN);
372 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800374 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376
377 return namelen;
378}
379
380#define ILLEGAL_CHAR_MARK '_'
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700381#define EXT_MARK '.'
382#define CRC_MARK '#'
383#define EXT_SIZE 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Marcin Slusarz4b111112008-02-08 04:20:36 -0800385static int udf_translate_to_linux(uint8_t *newName, uint8_t *udfName,
386 int udfLen, uint8_t *fidName,
387 int fidNameLen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700389 int index, newIndex = 0, needsCRC = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int extIndex = 0, newExtIndex = 0, hasExt = 0;
391 unsigned short valueCRC;
392 uint8_t curr;
393 const uint8_t hexChar[] = "0123456789ABCDEF";
394
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700395 if (udfName[0] == '.' &&
396 (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 needsCRC = 1;
398 newIndex = udfLen;
399 memcpy(newName, udfName, udfLen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700400 } else {
401 for (index = 0; index < udfLen; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 curr = udfName[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700403 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 needsCRC = 1;
405 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800406 while (index + 1 < udfLen &&
407 (udfName[index + 1] == '/' ||
408 udfName[index + 1] == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 index++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800410 }
411 if (curr == EXT_MARK &&
412 (udfLen - index - 1) <= EXT_SIZE) {
413 if (udfLen == index + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 hasExt = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800415 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 hasExt = 1;
417 extIndex = index;
418 newExtIndex = newIndex;
419 }
420 }
421 if (newIndex < 256)
422 newName[newIndex++] = curr;
423 else
424 needsCRC = 1;
425 }
426 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700427 if (needsCRC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 uint8_t ext[EXT_SIZE];
429 int localExtIndex = 0;
430
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700431 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int maxFilenameLen;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800433 for (index = 0;
434 index < EXT_SIZE && extIndex + index + 1 < udfLen;
435 index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 curr = udfName[extIndex + index + 1];
437
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700438 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 needsCRC = 1;
440 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800441 while (extIndex + index + 2 < udfLen &&
442 (index + 1 < EXT_SIZE &&
443 (udfName[extIndex + index + 2] == '/' ||
444 udfName[extIndex + index + 2] == 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 index++;
446 }
447 ext[localExtIndex++] = curr;
448 }
449 maxFilenameLen = 250 - localExtIndex;
450 if (newIndex > maxFilenameLen)
451 newIndex = maxFilenameLen;
452 else
453 newIndex = newExtIndex;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800454 } else if (newIndex > 250)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 newIndex = 250;
456 newName[newIndex++] = CRC_MARK;
457 valueCRC = udf_crc(fidName, fidNameLen, 0);
458 newName[newIndex++] = hexChar[(valueCRC & 0xf000) >> 12];
459 newName[newIndex++] = hexChar[(valueCRC & 0x0f00) >> 8];
460 newName[newIndex++] = hexChar[(valueCRC & 0x00f0) >> 4];
461 newName[newIndex++] = hexChar[(valueCRC & 0x000f)];
462
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700463 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 newName[newIndex++] = EXT_MARK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700465 for (index = 0; index < localExtIndex; index++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 newName[newIndex++] = ext[index];
467 }
468 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return newIndex;
471}