blob: a3bbdbde9f4b975528151607dfaa0d0bc90dfa69 [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>
Bob Copelandf845fce2008-04-17 09:47:48 +020026#include <linux/crc-itu-t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include "udf_sb.h"
29
30static int udf_translate_to_linux(uint8_t *, uint8_t *, int, uint8_t *, int);
31
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070032static int udf_char_to_ustr(struct ustr *dest, const uint8_t *src, int strlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070034 if ((!dest) || (!src) || (!strlen) || (strlen > UDF_NAME_LEN - 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 memset(dest, 0, sizeof(struct ustr));
38 memcpy(dest->u_name, src, strlen);
39 dest->u_cmpID = 0x08;
40 dest->u_len = strlen;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 return strlen;
43}
44
45/*
46 * udf_build_ustr
47 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070048int udf_build_ustr(struct ustr *dest, dstring *ptr, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 int usesize;
51
Marcin Slusarz6305a0a2008-02-04 22:27:39 +010052 if (!dest || !ptr || !size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return -1;
Marcin Slusarz6305a0a2008-02-04 22:27:39 +010054 BUG_ON(size < 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Marcin Slusarz6305a0a2008-02-04 22:27:39 +010056 usesize = min_t(size_t, ptr[size - 1], sizeof(dest->u_name));
57 usesize = min(usesize, size - 2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070058 dest->u_cmpID = ptr[0];
Marcin Slusarz6305a0a2008-02-04 22:27:39 +010059 dest->u_len = usesize;
60 memcpy(dest->u_name, ptr + 1, usesize);
61 memset(dest->u_name + usesize, 0, sizeof(dest->u_name) - usesize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return 0;
64}
65
66/*
67 * udf_build_ustr_exact
68 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070069static int udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070071 if ((!dest) || (!ptr) || (!exactsize))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return -1;
73
74 memset(dest, 0, sizeof(struct ustr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070075 dest->u_cmpID = ptr[0];
76 dest->u_len = exactsize - 1;
77 memcpy(dest->u_name, ptr + 1, exactsize - 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return 0;
80}
81
82/*
83 * udf_ocu_to_utf8
84 *
85 * PURPOSE
86 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
87 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * PRE-CONDITIONS
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 *"
93 *
94 * POST-CONDITIONS
95 * <return> Zero on success.
96 *
97 * HISTORY
98 * November 12, 1997 - Andrew E. Mileski
99 * Written, tested, and released.
100 */
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100101int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100103 const uint8_t *ocu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 uint8_t cmp_id, ocu_len;
105 int i;
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ocu_len = ocu_i->u_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700108 if (ocu_len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 memset(utf_o, 0, sizeof(struct ustr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return 0;
111 }
112
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100113 cmp_id = ocu_i->u_cmpID;
114 if (cmp_id != 8 && cmp_id != 16) {
115 memset(utf_o, 0, sizeof(struct ustr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700116 printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
117 cmp_id, ocu_i->u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 0;
119 }
120
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100121 ocu = ocu_i->u_name;
122 utf_o->u_len = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700123 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 /* Expand OSTA compressed Unicode to Unicode */
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100126 uint32_t c = ocu[i++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (cmp_id == 16)
128 c = (c << 8) | ocu[i++];
129
130 /* Compress Unicode to UTF-8 */
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100131 if (c < 0x80U)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700132 utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100133 else if (c < 0x800U) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800134 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));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700138 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800139 utf_o->u_name[utf_o->u_len++] =
140 (uint8_t)(0xe0 | (c >> 12));
141 utf_o->u_name[utf_o->u_len++] =
142 (uint8_t)(0x80 |
143 ((c >> 6) & 0x3f));
144 utf_o->u_name[utf_o->u_len++] =
145 (uint8_t)(0x80 | (c & 0x3f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700148 utf_o->u_cmpID = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 return utf_o->u_len;
151}
152
153/*
154 *
155 * udf_utf8_to_ocu
156 *
157 * PURPOSE
158 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
159 *
160 * DESCRIPTION
161 * This routine is only called by udf_lookup().
162 *
163 * PRE-CONDITIONS
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.
168 *
169 * POST-CONDITIONS
170 * <return> Zero on success.
171 *
172 * HISTORY
173 * November 12, 1997 - Andrew E. Mileski
174 * Written, tested, and released.
175 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700176static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 unsigned c, i, max_val, utf_char;
179 int utf_cnt, u_len;
180
181 memset(ocu, 0, sizeof(dstring) * length);
182 ocu[0] = 8;
183 max_val = 0xffU;
184
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700185try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 u_len = 0U;
187 utf_char = 0U;
188 utf_cnt = 0U;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700189 for (i = 0U; i < utf->u_len; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700190 c = (uint8_t)utf->u_name[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 /* Complete a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700193 if (utf_cnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 utf_char = (utf_char << 6) | (c & 0x3fU);
195 if (--utf_cnt)
196 continue;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700197 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* Check for a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700199 if (c & 0x80U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* Start a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700201 if ((c & 0xe0U) == 0xc0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 utf_char = c & 0x1fU;
203 utf_cnt = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700204 } else if ((c & 0xf0U) == 0xe0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 utf_char = c & 0x0fU;
206 utf_cnt = 2;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700207 } else if ((c & 0xf8U) == 0xf0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 utf_char = c & 0x07U;
209 utf_cnt = 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700210 } else if ((c & 0xfcU) == 0xf8U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 utf_char = c & 0x03U;
212 utf_cnt = 4;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700213 } else if ((c & 0xfeU) == 0xfcU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 utf_char = c & 0x01U;
215 utf_cnt = 5;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700216 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto error_out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 continue;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700220 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* Single byte UTF-8 character (most common) */
222 utf_char = c;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
226 /* Choose no compression if necessary */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700227 if (utf_char > max_val) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700228 if (max_val == 0xffU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 max_val = 0xffffU;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700230 ocu[0] = (uint8_t)0x10U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 goto try_again;
232 }
233 goto error_out;
234 }
235
Marcin Slusarz4b111112008-02-08 04:20:36 -0800236 if (max_val == 0xffffU)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700237 ocu[++u_len] = (uint8_t)(utf_char >> 8);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700238 ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700241 if (utf_cnt) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700242error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 ocu[++u_len] = '?';
244 printk(KERN_DEBUG "udf: bad UTF-8 character\n");
245 }
246
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700247 ocu[length - 1] = (uint8_t)u_len + 1;
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return u_len + 1;
250}
251
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700252static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100253 const struct ustr *ocu_i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100255 const uint8_t *ocu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 uint8_t cmp_id, ocu_len;
257 int i;
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 ocu_len = ocu_i->u_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700261 if (ocu_len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 memset(utf_o, 0, sizeof(struct ustr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 0;
264 }
265
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100266 cmp_id = ocu_i->u_cmpID;
267 if (cmp_id != 8 && cmp_id != 16) {
268 memset(utf_o, 0, sizeof(struct ustr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700269 printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
270 cmp_id, ocu_i->u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 0;
272 }
273
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100274 ocu = ocu_i->u_name;
275 utf_o->u_len = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700276 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* Expand OSTA compressed Unicode to Unicode */
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100278 uint32_t c = ocu[i++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (cmp_id == 16)
280 c = (c << 8) | ocu[i++];
281
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700282 utf_o->u_len += nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
283 UDF_NAME_LEN - utf_o->u_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700285 utf_o->u_cmpID = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 return utf_o->u_len;
288}
289
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700290static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700291 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 unsigned len, i, max_val;
294 uint16_t uni_char;
295 int u_len;
296
297 memset(ocu, 0, sizeof(dstring) * length);
298 ocu[0] = 8;
299 max_val = 0xffU;
300
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700301try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 u_len = 0U;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700303 for (i = 0U; i < uni->u_len; i++) {
304 len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (len <= 0)
306 continue;
307
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700308 if (uni_char > max_val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 max_val = 0xffffU;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700310 ocu[0] = (uint8_t)0x10U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 goto try_again;
312 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (max_val == 0xffffU)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700315 ocu[++u_len] = (uint8_t)(uni_char >> 8);
316 ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 i += len - 1;
318 }
319
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700320 ocu[length - 1] = (uint8_t)u_len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return u_len + 1;
322}
323
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700324int udf_get_filename(struct super_block *sb, uint8_t *sname, uint8_t *dname,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700325 int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100327 struct ustr *filename, *unifilename;
328 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100330 filename = kmalloc(sizeof(struct ustr), GFP_NOFS);
331 if (!filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100334 unifilename = kmalloc(sizeof(struct ustr), GFP_NOFS);
335 if (!unifilename)
336 goto out1;
337
338 if (udf_build_ustr_exact(unifilename, sname, flen))
339 goto out2;
340
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700341 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100342 if (!udf_CS0toUTF8(filename, unifilename)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800343 udf_debug("Failed in udf_get_filename: sname = %s\n",
344 sname);
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100345 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700347 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100348 if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, filename,
349 unifilename)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800350 udf_debug("Failed in udf_get_filename: sname = %s\n",
351 sname);
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100352 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800354 } else
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100355 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100357 len = udf_translate_to_linux(dname, filename->u_name, filename->u_len,
358 unifilename->u_name, unifilename->u_len);
359out2:
360 kfree(unifilename);
361out1:
362 kfree(filename);
363 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700366int udf_put_filename(struct super_block *sb, const uint8_t *sname,
367 uint8_t *dname, int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct ustr unifilename;
370 int namelen;
371
Marcin Slusarz4b111112008-02-08 04:20:36 -0800372 if (!udf_char_to_ustr(&unifilename, sname, flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700375 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700376 namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800377 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700379 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800380 namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
381 &unifilename, UDF_NAME_LEN);
382 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800384 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
386
387 return namelen;
388}
389
390#define ILLEGAL_CHAR_MARK '_'
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700391#define EXT_MARK '.'
392#define CRC_MARK '#'
393#define EXT_SIZE 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Marcin Slusarz4b111112008-02-08 04:20:36 -0800395static int udf_translate_to_linux(uint8_t *newName, uint8_t *udfName,
396 int udfLen, uint8_t *fidName,
397 int fidNameLen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700399 int index, newIndex = 0, needsCRC = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 int extIndex = 0, newExtIndex = 0, hasExt = 0;
401 unsigned short valueCRC;
402 uint8_t curr;
403 const uint8_t hexChar[] = "0123456789ABCDEF";
404
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700405 if (udfName[0] == '.' &&
406 (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 needsCRC = 1;
408 newIndex = udfLen;
409 memcpy(newName, udfName, udfLen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700410 } else {
411 for (index = 0; index < udfLen; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 curr = udfName[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700413 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 needsCRC = 1;
415 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800416 while (index + 1 < udfLen &&
417 (udfName[index + 1] == '/' ||
418 udfName[index + 1] == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 index++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800420 }
421 if (curr == EXT_MARK &&
422 (udfLen - index - 1) <= EXT_SIZE) {
423 if (udfLen == index + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 hasExt = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800425 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 hasExt = 1;
427 extIndex = index;
428 newExtIndex = newIndex;
429 }
430 }
431 if (newIndex < 256)
432 newName[newIndex++] = curr;
433 else
434 needsCRC = 1;
435 }
436 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700437 if (needsCRC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 uint8_t ext[EXT_SIZE];
439 int localExtIndex = 0;
440
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700441 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int maxFilenameLen;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800443 for (index = 0;
444 index < EXT_SIZE && extIndex + index + 1 < udfLen;
445 index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 curr = udfName[extIndex + index + 1];
447
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700448 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 needsCRC = 1;
450 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800451 while (extIndex + index + 2 < udfLen &&
452 (index + 1 < EXT_SIZE &&
453 (udfName[extIndex + index + 2] == '/' ||
454 udfName[extIndex + index + 2] == 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 index++;
456 }
457 ext[localExtIndex++] = curr;
458 }
459 maxFilenameLen = 250 - localExtIndex;
460 if (newIndex > maxFilenameLen)
461 newIndex = maxFilenameLen;
462 else
463 newIndex = newExtIndex;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800464 } else if (newIndex > 250)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 newIndex = 250;
466 newName[newIndex++] = CRC_MARK;
Bob Copelandf845fce2008-04-17 09:47:48 +0200467 valueCRC = crc_itu_t(0, fidName, fidNameLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 newName[newIndex++] = hexChar[(valueCRC & 0xf000) >> 12];
469 newName[newIndex++] = hexChar[(valueCRC & 0x0f00) >> 8];
470 newName[newIndex++] = hexChar[(valueCRC & 0x00f0) >> 4];
471 newName[newIndex++] = hexChar[(valueCRC & 0x000f)];
472
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700473 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 newName[newIndex++] = EXT_MARK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700475 for (index = 0; index < localExtIndex; index++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 newName[newIndex++] = ext[index];
477 }
478 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return newIndex;
481}