blob: 9e6099c26c27a7434de44af03b05f79cc97bd0ca [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>
26#include <linux/udf_fs.h>
27
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
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070052 if ((!dest) || (!ptr) || (!size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return -1;
54
55 memset(dest, 0, sizeof(struct ustr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070056 usesize = (size > UDF_NAME_LEN) ? UDF_NAME_LEN : size;
57 dest->u_cmpID = ptr[0];
58 dest->u_len = ptr[size - 1];
59 memcpy(dest->u_name, ptr + 1, usesize - 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return 0;
62}
63
64/*
65 * udf_build_ustr_exact
66 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070067static int udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070069 if ((!dest) || (!ptr) || (!exactsize))
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return -1;
71
72 memset(dest, 0, sizeof(struct ustr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070073 dest->u_cmpID = ptr[0];
74 dest->u_len = exactsize - 1;
75 memcpy(dest->u_name, ptr + 1, exactsize - 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return 0;
78}
79
80/*
81 * udf_ocu_to_utf8
82 *
83 * PURPOSE
84 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
85 *
86 * DESCRIPTION
87 * This routine is only called by udf_filldir().
88 *
89 * PRE-CONDITIONS
90 * utf Pointer to UTF-8 output buffer.
91 * ocu Pointer to OSTA Compressed Unicode input buffer
92 * of size UDF_NAME_LEN bytes.
93 * both of type "struct ustr *"
94 *
95 * POST-CONDITIONS
96 * <return> Zero on success.
97 *
98 * HISTORY
99 * November 12, 1997 - Andrew E. Mileski
100 * Written, tested, and released.
101 */
102int udf_CS0toUTF8(struct ustr *utf_o, struct ustr *ocu_i)
103{
104 uint8_t *ocu;
105 uint32_t c;
106 uint8_t cmp_id, ocu_len;
107 int i;
108
109 ocu = ocu_i->u_name;
110
111 ocu_len = ocu_i->u_len;
112 cmp_id = ocu_i->u_cmpID;
113 utf_o->u_len = 0;
114
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700115 if (ocu_len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 memset(utf_o, 0, sizeof(struct ustr));
117 utf_o->u_cmpID = 0;
118 utf_o->u_len = 0;
119 return 0;
120 }
121
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700122 if ((cmp_id != 8) && (cmp_id != 16)) {
123 printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
124 cmp_id, ocu_i->u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 0;
126 }
127
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700128 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 /* Expand OSTA compressed Unicode to Unicode */
131 c = ocu[i++];
132 if (cmp_id == 16)
133 c = (c << 8) | ocu[i++];
134
135 /* Compress Unicode to UTF-8 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700136 if (c < 0x80U) {
137 utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
138 } else if (c < 0x800U) {
139 utf_o->u_name[utf_o->u_len++] = (uint8_t)(0xc0 | (c >> 6));
140 utf_o->u_name[utf_o->u_len++] = (uint8_t)(0x80 | (c & 0x3f));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700141 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700142 utf_o->u_name[utf_o->u_len++] = (uint8_t)(0xe0 | (c >> 12));
143 utf_o->u_name[utf_o->u_len++] = (uint8_t)(0x80 | ((c >> 6) & 0x3f));
144 utf_o->u_name[utf_o->u_len++] = (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
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700235 if (max_val == 0xffffU) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700236 ocu[++u_len] = (uint8_t)(utf_char >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
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,
253 struct ustr *ocu_i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 uint8_t *ocu;
256 uint32_t c;
257 uint8_t cmp_id, ocu_len;
258 int i;
259
260 ocu = ocu_i->u_name;
261
262 ocu_len = ocu_i->u_len;
263 cmp_id = ocu_i->u_cmpID;
264 utf_o->u_len = 0;
265
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700266 if (ocu_len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 memset(utf_o, 0, sizeof(struct ustr));
268 utf_o->u_cmpID = 0;
269 utf_o->u_len = 0;
270 return 0;
271 }
272
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700273 if ((cmp_id != 8) && (cmp_id != 16)) {
274 printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n",
275 cmp_id, ocu_i->u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return 0;
277 }
278
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700279 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /* Expand OSTA compressed Unicode to Unicode */
281 c = ocu[i++];
282 if (cmp_id == 16)
283 c = (c << 8) | ocu[i++];
284
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700285 utf_o->u_len += nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
286 UDF_NAME_LEN - utf_o->u_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700288 utf_o->u_cmpID = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 return utf_o->u_len;
291}
292
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700293static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700294 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 unsigned len, i, max_val;
297 uint16_t uni_char;
298 int u_len;
299
300 memset(ocu, 0, sizeof(dstring) * length);
301 ocu[0] = 8;
302 max_val = 0xffU;
303
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700304try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 u_len = 0U;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700306 for (i = 0U; i < uni->u_len; i++) {
307 len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (len <= 0)
309 continue;
310
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700311 if (uni_char > max_val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 max_val = 0xffffU;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700313 ocu[0] = (uint8_t)0x10U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 goto try_again;
315 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (max_val == 0xffffU)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700318 ocu[++u_len] = (uint8_t)(uni_char >> 8);
319 ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 i += len - 1;
321 }
322
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700323 ocu[length - 1] = (uint8_t)u_len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return u_len + 1;
325}
326
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700327int udf_get_filename(struct super_block *sb, uint8_t *sname, uint8_t *dname,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700328 int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 struct ustr filename, unifilename;
331 int len;
332
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700333 if (udf_build_ustr_exact(&unifilename, sname, flen)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
335 }
336
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700337 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
338 if (!udf_CS0toUTF8(&filename, &unifilename)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700339 udf_debug("Failed in udf_get_filename: sname = %s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
341 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700342 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700343 if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, &filename, &unifilename)) {
344 udf_debug("Failed in udf_get_filename: sname = %s\n", sname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700347 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700351 len = udf_translate_to_linux(dname, filename.u_name, filename.u_len,
352 unifilename.u_name, unifilename.u_len);
353 if (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return len;
355 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358}
359
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700360int udf_put_filename(struct super_block *sb, const uint8_t *sname,
361 uint8_t *dname, int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct ustr unifilename;
364 int namelen;
365
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700366 if (!(udf_char_to_ustr(&unifilename, sname, flen))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368 }
369
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700370 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700371 namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
372 if (!namelen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700375 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700376 namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname, &unifilename, UDF_NAME_LEN);
377 if (!namelen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
379 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700380 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 return namelen;
385}
386
387#define ILLEGAL_CHAR_MARK '_'
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700388#define EXT_MARK '.'
389#define CRC_MARK '#'
390#define EXT_SIZE 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700392static int udf_translate_to_linux(uint8_t *newName, uint8_t *udfName, int udfLen,
393 uint8_t *fidName, int fidNameLen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700395 int index, newIndex = 0, needsCRC = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int extIndex = 0, newExtIndex = 0, hasExt = 0;
397 unsigned short valueCRC;
398 uint8_t curr;
399 const uint8_t hexChar[] = "0123456789ABCDEF";
400
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700401 if (udfName[0] == '.' &&
402 (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 needsCRC = 1;
404 newIndex = udfLen;
405 memcpy(newName, udfName, udfLen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700406 } else {
407 for (index = 0; index < udfLen; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 curr = udfName[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700409 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 needsCRC = 1;
411 curr = ILLEGAL_CHAR_MARK;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700412 while (index + 1 < udfLen && (udfName[index + 1] == '/' ||
413 udfName[index + 1] == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 index++;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700415 } if (curr == EXT_MARK && (udfLen - index - 1) <= EXT_SIZE) {
416 if (udfLen == index + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 hasExt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700418 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 hasExt = 1;
420 extIndex = index;
421 newExtIndex = newIndex;
422 }
423 }
424 if (newIndex < 256)
425 newName[newIndex++] = curr;
426 else
427 needsCRC = 1;
428 }
429 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700430 if (needsCRC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 uint8_t ext[EXT_SIZE];
432 int localExtIndex = 0;
433
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700434 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int maxFilenameLen;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700436 for(index = 0; index < EXT_SIZE && extIndex + index + 1 < udfLen; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 curr = udfName[extIndex + index + 1];
438
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700439 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 needsCRC = 1;
441 curr = ILLEGAL_CHAR_MARK;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700442 while(extIndex + index + 2 < udfLen &&
443 (index + 1 < EXT_SIZE
444 && (udfName[extIndex + index + 2] == '/' ||
445 udfName[extIndex + index + 2] == 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 index++;
447 }
448 ext[localExtIndex++] = curr;
449 }
450 maxFilenameLen = 250 - localExtIndex;
451 if (newIndex > maxFilenameLen)
452 newIndex = maxFilenameLen;
453 else
454 newIndex = newExtIndex;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700455 } else if (newIndex > 250) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 newIndex = 250;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 newName[newIndex++] = CRC_MARK;
459 valueCRC = udf_crc(fidName, fidNameLen, 0);
460 newName[newIndex++] = hexChar[(valueCRC & 0xf000) >> 12];
461 newName[newIndex++] = hexChar[(valueCRC & 0x0f00) >> 8];
462 newName[newIndex++] = hexChar[(valueCRC & 0x00f0) >> 4];
463 newName[newIndex++] = hexChar[(valueCRC & 0x000f)];
464
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700465 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 newName[newIndex++] = EXT_MARK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700467 for (index = 0; index < localExtIndex; index++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 newName[newIndex++] = ext[index];
469 }
470 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return newIndex;
473}