blob: b9de050ad8302725c3d9a58b46ed494f759199f2 [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
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070051 if ((!dest) || (!ptr) || (!size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 return -1;
53
54 memset(dest, 0, sizeof(struct ustr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070055 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);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return 0;
61}
62
63/*
64 * udf_build_ustr_exact
65 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070066static int udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070068 if ((!dest) || (!ptr) || (!exactsize))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return -1;
70
71 memset(dest, 0, sizeof(struct ustr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070072 dest->u_cmpID = ptr[0];
73 dest->u_len = exactsize - 1;
74 memcpy(dest->u_name, ptr + 1, exactsize - 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return 0;
77}
78
79/*
80 * udf_ocu_to_utf8
81 *
82 * PURPOSE
83 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
84 *
85 * DESCRIPTION
86 * This routine is only called by udf_filldir().
87 *
88 * 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 */
101int udf_CS0toUTF8(struct ustr *utf_o, struct ustr *ocu_i)
102{
103 uint8_t *ocu;
104 uint32_t c;
105 uint8_t cmp_id, ocu_len;
106 int i;
107
108 ocu = ocu_i->u_name;
109
110 ocu_len = ocu_i->u_len;
111 cmp_id = ocu_i->u_cmpID;
112 utf_o->u_len = 0;
113
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700114 if (ocu_len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 memset(utf_o, 0, sizeof(struct ustr));
116 utf_o->u_cmpID = 0;
117 utf_o->u_len = 0;
118 return 0;
119 }
120
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700121 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return 0;
125 }
126
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700127 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /* Expand OSTA compressed Unicode to Unicode */
130 c = ocu[i++];
131 if (cmp_id == 16)
132 c = (c << 8) | ocu[i++];
133
134 /* Compress Unicode to UTF-8 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700135 if (c < 0x80U) {
136 utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
137 } else if (c < 0x800U) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800138 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));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700142 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800143 utf_o->u_name[utf_o->u_len++] =
144 (uint8_t)(0xe0 | (c >> 12));
145 utf_o->u_name[utf_o->u_len++] =
146 (uint8_t)(0x80 |
147 ((c >> 6) & 0x3f));
148 utf_o->u_name[utf_o->u_len++] =
149 (uint8_t)(0x80 | (c & 0x3f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700152 utf_o->u_cmpID = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 return utf_o->u_len;
155}
156
157/*
158 *
159 * udf_utf8_to_ocu
160 *
161 * PURPOSE
162 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
163 *
164 * DESCRIPTION
165 * This routine is only called by udf_lookup().
166 *
167 * PRE-CONDITIONS
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.
172 *
173 * POST-CONDITIONS
174 * <return> Zero on success.
175 *
176 * HISTORY
177 * November 12, 1997 - Andrew E. Mileski
178 * Written, tested, and released.
179 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700180static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 unsigned c, i, max_val, utf_char;
183 int utf_cnt, u_len;
184
185 memset(ocu, 0, sizeof(dstring) * length);
186 ocu[0] = 8;
187 max_val = 0xffU;
188
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700189try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 u_len = 0U;
191 utf_char = 0U;
192 utf_cnt = 0U;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700193 for (i = 0U; i < utf->u_len; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700194 c = (uint8_t)utf->u_name[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 /* Complete a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700197 if (utf_cnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 utf_char = (utf_char << 6) | (c & 0x3fU);
199 if (--utf_cnt)
200 continue;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700201 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* Check for a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700203 if (c & 0x80U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* Start a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700205 if ((c & 0xe0U) == 0xc0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 utf_char = c & 0x1fU;
207 utf_cnt = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700208 } else if ((c & 0xf0U) == 0xe0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 utf_char = c & 0x0fU;
210 utf_cnt = 2;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700211 } else if ((c & 0xf8U) == 0xf0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 utf_char = c & 0x07U;
213 utf_cnt = 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700214 } else if ((c & 0xfcU) == 0xf8U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 utf_char = c & 0x03U;
216 utf_cnt = 4;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700217 } else if ((c & 0xfeU) == 0xfcU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 utf_char = c & 0x01U;
219 utf_cnt = 5;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700220 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 goto error_out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 continue;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700224 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* Single byte UTF-8 character (most common) */
226 utf_char = c;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229
230 /* Choose no compression if necessary */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700231 if (utf_char > max_val) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700232 if (max_val == 0xffU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 max_val = 0xffffU;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700234 ocu[0] = (uint8_t)0x10U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 goto try_again;
236 }
237 goto error_out;
238 }
239
Marcin Slusarz4b111112008-02-08 04:20:36 -0800240 if (max_val == 0xffffU)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700241 ocu[++u_len] = (uint8_t)(utf_char >> 8);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700242 ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700245 if (utf_cnt) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700246error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 ocu[++u_len] = '?';
248 printk(KERN_DEBUG "udf: bad UTF-8 character\n");
249 }
250
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700251 ocu[length - 1] = (uint8_t)u_len + 1;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return u_len + 1;
254}
255
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700256static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
257 struct ustr *ocu_i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 uint8_t *ocu;
260 uint32_t c;
261 uint8_t cmp_id, ocu_len;
262 int i;
263
264 ocu = ocu_i->u_name;
265
266 ocu_len = ocu_i->u_len;
267 cmp_id = ocu_i->u_cmpID;
268 utf_o->u_len = 0;
269
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700270 if (ocu_len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 memset(utf_o, 0, sizeof(struct ustr));
272 utf_o->u_cmpID = 0;
273 utf_o->u_len = 0;
274 return 0;
275 }
276
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700277 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return 0;
281 }
282
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700283 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* Expand OSTA compressed Unicode to Unicode */
285 c = ocu[i++];
286 if (cmp_id == 16)
287 c = (c << 8) | ocu[i++];
288
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700289 utf_o->u_len += nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
290 UDF_NAME_LEN - utf_o->u_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700292 utf_o->u_cmpID = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 return utf_o->u_len;
295}
296
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700297static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700298 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 unsigned len, i, max_val;
301 uint16_t uni_char;
302 int u_len;
303
304 memset(ocu, 0, sizeof(dstring) * length);
305 ocu[0] = 8;
306 max_val = 0xffU;
307
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700308try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 u_len = 0U;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700310 for (i = 0U; i < uni->u_len; i++) {
311 len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (len <= 0)
313 continue;
314
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700315 if (uni_char > max_val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 max_val = 0xffffU;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700317 ocu[0] = (uint8_t)0x10U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto try_again;
319 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (max_val == 0xffffU)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700322 ocu[++u_len] = (uint8_t)(uni_char >> 8);
323 ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 i += len - 1;
325 }
326
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700327 ocu[length - 1] = (uint8_t)u_len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return u_len + 1;
329}
330
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700331int udf_get_filename(struct super_block *sb, uint8_t *sname, uint8_t *dname,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700332 int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct ustr filename, unifilename;
335 int len;
336
Marcin Slusarz4b111112008-02-08 04:20:36 -0800337 if (udf_build_ustr_exact(&unifilename, sname, flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700340 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
341 if (!udf_CS0toUTF8(&filename, &unifilename)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800342 udf_debug("Failed in udf_get_filename: sname = %s\n",
343 sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 0;
345 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700346 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800347 if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, &filename,
348 &unifilename)) {
349 udf_debug("Failed in udf_get_filename: sname = %s\n",
350 sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return 0;
352 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800353 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700356 len = udf_translate_to_linux(dname, filename.u_name, filename.u_len,
357 unifilename.u_name, unifilename.u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800358 if (len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return len;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 0;
362}
363
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700364int udf_put_filename(struct super_block *sb, const uint8_t *sname,
365 uint8_t *dname, int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct ustr unifilename;
368 int namelen;
369
Marcin Slusarz4b111112008-02-08 04:20:36 -0800370 if (!udf_char_to_ustr(&unifilename, sname, flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700373 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700374 namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800375 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700377 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800378 namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
379 &unifilename, UDF_NAME_LEN);
380 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800382 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return 0;
384
385 return namelen;
386}
387
388#define ILLEGAL_CHAR_MARK '_'
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700389#define EXT_MARK '.'
390#define CRC_MARK '#'
391#define EXT_SIZE 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Marcin Slusarz4b111112008-02-08 04:20:36 -0800393static int udf_translate_to_linux(uint8_t *newName, uint8_t *udfName,
394 int udfLen, uint8_t *fidName,
395 int fidNameLen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700397 int index, newIndex = 0, needsCRC = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 int extIndex = 0, newExtIndex = 0, hasExt = 0;
399 unsigned short valueCRC;
400 uint8_t curr;
401 const uint8_t hexChar[] = "0123456789ABCDEF";
402
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700403 if (udfName[0] == '.' &&
404 (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 needsCRC = 1;
406 newIndex = udfLen;
407 memcpy(newName, udfName, udfLen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700408 } else {
409 for (index = 0; index < udfLen; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 curr = udfName[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700411 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 needsCRC = 1;
413 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800414 while (index + 1 < udfLen &&
415 (udfName[index + 1] == '/' ||
416 udfName[index + 1] == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 index++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800418 }
419 if (curr == EXT_MARK &&
420 (udfLen - index - 1) <= EXT_SIZE) {
421 if (udfLen == index + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 hasExt = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800423 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 hasExt = 1;
425 extIndex = index;
426 newExtIndex = newIndex;
427 }
428 }
429 if (newIndex < 256)
430 newName[newIndex++] = curr;
431 else
432 needsCRC = 1;
433 }
434 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700435 if (needsCRC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 uint8_t ext[EXT_SIZE];
437 int localExtIndex = 0;
438
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700439 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int maxFilenameLen;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800441 for (index = 0;
442 index < EXT_SIZE && extIndex + index + 1 < udfLen;
443 index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 curr = udfName[extIndex + index + 1];
445
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700446 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 needsCRC = 1;
448 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800449 while (extIndex + index + 2 < udfLen &&
450 (index + 1 < EXT_SIZE &&
451 (udfName[extIndex + index + 2] == '/' ||
452 udfName[extIndex + index + 2] == 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 index++;
454 }
455 ext[localExtIndex++] = curr;
456 }
457 maxFilenameLen = 250 - localExtIndex;
458 if (newIndex > maxFilenameLen)
459 newIndex = maxFilenameLen;
460 else
461 newIndex = newExtIndex;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800462 } else if (newIndex > 250)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 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)];
470
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700471 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 newName[newIndex++] = EXT_MARK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700473 for (index = 0; index < localExtIndex; index++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 newName[newIndex++] = ext[index];
475 }
476 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return newIndex;
479}