blob: 5599e753540106fda07e2d79790dccd7447c37bd [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include "udf_sb.h"
30
Jan Kara0e5cc9a2014-12-18 22:37:50 +010031static int udf_translate_to_linux(uint8_t *, int, uint8_t *, int, uint8_t *,
32 int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070034static int udf_char_to_ustr(struct ustr *dest, const uint8_t *src, int strlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Andrew Gabbasov9fba7052016-01-15 02:44:21 -060036 if ((!dest) || (!src) || (!strlen) || (strlen > UDF_NAME_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 memset(dest, 0, sizeof(struct ustr));
40 memcpy(dest->u_name, src, strlen);
41 dest->u_cmpID = 0x08;
42 dest->u_len = strlen;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 return strlen;
45}
46
47/*
48 * udf_build_ustr
49 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070050int udf_build_ustr(struct ustr *dest, dstring *ptr, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 int usesize;
53
Marcin Slusarz6305a0a2008-02-04 22:27:39 +010054 if (!dest || !ptr || !size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return -1;
Marcin Slusarz6305a0a2008-02-04 22:27:39 +010056 BUG_ON(size < 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Marcin Slusarz6305a0a2008-02-04 22:27:39 +010058 usesize = min_t(size_t, ptr[size - 1], sizeof(dest->u_name));
59 usesize = min(usesize, size - 2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070060 dest->u_cmpID = ptr[0];
Marcin Slusarz6305a0a2008-02-04 22:27:39 +010061 dest->u_len = usesize;
62 memcpy(dest->u_name, ptr + 1, usesize);
63 memset(dest->u_name + usesize, 0, sizeof(dest->u_name) - usesize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return 0;
66}
67
68/*
69 * udf_build_ustr_exact
70 */
Fabian Frederick31f25662015-04-08 21:23:52 +020071static void udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060079static int udf_uni2char_utf8(wchar_t uni,
80 unsigned char *out,
81 int boundlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060083 int u_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060085 if (boundlen <= 0)
86 return -ENAMETOOLONG;
87
88 if (uni < 0x80) {
89 out[u_len++] = (unsigned char)uni;
90 } else if (uni < 0x800) {
91 if (boundlen < 2)
92 return -ENAMETOOLONG;
93 out[u_len++] = (unsigned char)(0xc0 | (uni >> 6));
94 out[u_len++] = (unsigned char)(0x80 | (uni & 0x3f));
95 } else {
96 if (boundlen < 3)
97 return -ENAMETOOLONG;
98 out[u_len++] = (unsigned char)(0xe0 | (uni >> 12));
99 out[u_len++] = (unsigned char)(0x80 | ((uni >> 6) & 0x3f));
100 out[u_len++] = (unsigned char)(0x80 | (uni & 0x3f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600102 return u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600105static int udf_char2uni_utf8(const unsigned char *in,
106 int boundlen,
107 wchar_t *uni)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600109 unsigned int utf_char;
110 unsigned char c;
111 int utf_cnt, u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600113 utf_char = 0;
114 utf_cnt = 0;
115 for (u_len = 0; u_len < boundlen;) {
116 c = in[u_len++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 /* Complete a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700119 if (utf_cnt) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600120 utf_char = (utf_char << 6) | (c & 0x3f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (--utf_cnt)
122 continue;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700123 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* Check for a multi-byte UTF-8 character */
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600125 if (c & 0x80) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /* Start a multi-byte UTF-8 character */
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600127 if ((c & 0xe0) == 0xc0) {
128 utf_char = c & 0x1f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 utf_cnt = 1;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600130 } else if ((c & 0xf0) == 0xe0) {
131 utf_char = c & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 utf_cnt = 2;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600133 } else if ((c & 0xf8) == 0xf0) {
134 utf_char = c & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 utf_cnt = 3;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600136 } else if ((c & 0xfc) == 0xf8) {
137 utf_char = c & 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 utf_cnt = 4;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600139 } else if ((c & 0xfe) == 0xfc) {
140 utf_char = c & 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 utf_cnt = 5;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700142 } else {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600143 utf_cnt = -1;
144 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 continue;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700147 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* Single byte UTF-8 character (most common) */
149 utf_char = c;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600152 *uni = utf_char;
153 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700155 if (utf_cnt) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600156 *uni = '?';
157 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600159 return u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600162static int udf_name_from_CS0(struct ustr *utf_o,
163 const struct ustr *ocu_i,
164 int (*conv_f)(wchar_t, unsigned char *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100166 const uint8_t *ocu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 uint8_t cmp_id, ocu_len;
Jan Kara59285c22009-02-04 19:46:11 +0100168 int i, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 ocu_len = ocu_i->u_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700172 if (ocu_len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 memset(utf_o, 0, sizeof(struct ustr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 0;
175 }
176
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100177 cmp_id = ocu_i->u_cmpID;
178 if (cmp_id != 8 && cmp_id != 16) {
179 memset(utf_o, 0, sizeof(struct ustr));
Joe Perches78ace702011-10-10 01:08:05 -0700180 pr_err("unknown compression code (%d) stri=%s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700181 cmp_id, ocu_i->u_name);
Fabian Frederick78fc2e62015-04-08 21:23:55 +0200182 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100185 ocu = ocu_i->u_name;
186 utf_o->u_len = 0;
Andrew Gabbasov9fba7052016-01-15 02:44:21 -0600187 for (i = 0; (i < ocu_len) && (utf_o->u_len < UDF_NAME_LEN);) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /* Expand OSTA compressed Unicode to Unicode */
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100189 uint32_t c = ocu[i++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (cmp_id == 16)
191 c = (c << 8) | ocu[i++];
192
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600193 len = conv_f(c, &utf_o->u_name[utf_o->u_len],
Andrew Gabbasov9fba7052016-01-15 02:44:21 -0600194 UDF_NAME_LEN - utf_o->u_len);
Jan Kara59285c22009-02-04 19:46:11 +0100195 /* Valid character? */
196 if (len >= 0)
197 utf_o->u_len += len;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600198 else if (len == -ENAMETOOLONG)
199 break;
Jan Kara59285c22009-02-04 19:46:11 +0100200 else
201 utf_o->u_name[utf_o->u_len++] = '?';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700203 utf_o->u_cmpID = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 return utf_o->u_len;
206}
207
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600208static int udf_name_to_CS0(dstring *ocu, struct ustr *uni, int length,
209 int (*conv_f)(const unsigned char *, int, wchar_t *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600211 int i, len;
212 unsigned int max_val;
213 wchar_t uni_char;
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600214 int u_len, u_ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 memset(ocu, 0, sizeof(dstring) * length);
217 ocu[0] = 8;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600218 max_val = 0xff;
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600219 u_ch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700221try_again:
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600222 u_len = 0;
223 for (i = 0; i < uni->u_len; i++) {
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600224 /* Name didn't fit? */
225 if (u_len + 1 + u_ch >= length)
226 return 0;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600227 len = conv_f(&uni->u_name[i], uni->u_len - i, &uni_char);
Jan Kara59285c22009-02-04 19:46:11 +0100228 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 continue;
Jan Kara59285c22009-02-04 19:46:11 +0100230 /* Invalid character, deal with it */
231 if (len < 0) {
232 len = 1;
233 uni_char = '?';
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700236 if (uni_char > max_val) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600237 max_val = 0xffff;
238 ocu[0] = 0x10;
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600239 u_ch = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto try_again;
241 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700242
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600243 if (max_val == 0xffff)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700244 ocu[++u_len] = (uint8_t)(uni_char >> 8);
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600245 ocu[++u_len] = (uint8_t)(uni_char & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 i += len - 1;
247 }
248
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700249 ocu[length - 1] = (uint8_t)u_len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return u_len + 1;
251}
252
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600253int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
254{
255 return udf_name_from_CS0(utf_o, ocu_i, udf_uni2char_utf8);
256}
257
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100258int udf_get_filename(struct super_block *sb, uint8_t *sname, int slen,
259 uint8_t *dname, int dlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100261 struct ustr *filename, *unifilename;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600262 int (*conv_f)(wchar_t, unsigned char *, int);
Fabian Frederick6ce63832015-04-08 21:23:57 +0200263 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Fabian Frederick31f25662015-04-08 21:23:52 +0200265 if (!slen)
266 return -EIO;
267
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100268 filename = kmalloc(sizeof(struct ustr), GFP_NOFS);
269 if (!filename)
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200270 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100272 unifilename = kmalloc(sizeof(struct ustr), GFP_NOFS);
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200273 if (!unifilename) {
274 ret = -ENOMEM;
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100275 goto out1;
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200276 }
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100277
Fabian Frederick31f25662015-04-08 21:23:52 +0200278 udf_build_ustr_exact(unifilename, sname, slen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700279 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600280 conv_f = udf_uni2char_utf8;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700281 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600282 conv_f = UDF_SB(sb)->s_nls_map->uni2char;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800283 } else
Fabian Frederick5dce54b2015-04-08 21:23:56 +0200284 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600286 ret = udf_name_from_CS0(filename, unifilename, conv_f);
287 if (ret < 0) {
288 udf_debug("Failed in udf_get_filename: sname = %s\n", sname);
289 goto out2;
290 }
291
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200292 ret = udf_translate_to_linux(dname, dlen,
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100293 filename->u_name, filename->u_len,
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100294 unifilename->u_name, unifilename->u_len);
Fabian Frederick6ce63832015-04-08 21:23:57 +0200295 /* Zero length filename isn't valid... */
296 if (ret == 0)
297 ret = -EINVAL;
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100298out2:
299 kfree(unifilename);
300out1:
301 kfree(filename);
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200302 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Andrew Gabbasov525e2c52016-01-15 02:44:19 -0600305int udf_put_filename(struct super_block *sb, const uint8_t *sname, int slen,
306 uint8_t *dname, int dlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 struct ustr unifilename;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600309 int (*conv_f)(const unsigned char *, int, wchar_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Andrew Gabbasov525e2c52016-01-15 02:44:19 -0600311 if (!udf_char_to_ustr(&unifilename, sname, slen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700314 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600315 conv_f = udf_char2uni_utf8;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700316 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600317 conv_f = UDF_SB(sb)->s_nls_map->char2uni;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800318 } else
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600319 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600321 return udf_name_to_CS0(dname, &unifilename, dlen, conv_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324#define ILLEGAL_CHAR_MARK '_'
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700325#define EXT_MARK '.'
326#define CRC_MARK '#'
327#define EXT_SIZE 5
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100328/* Number of chars we need to store generated CRC to make filename unique */
329#define CRC_LEN 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100331static int udf_translate_to_linux(uint8_t *newName, int newLen,
332 uint8_t *udfName, int udfLen,
333 uint8_t *fidName, int fidNameLen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700335 int index, newIndex = 0, needsCRC = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int extIndex = 0, newExtIndex = 0, hasExt = 0;
337 unsigned short valueCRC;
338 uint8_t curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700340 if (udfName[0] == '.' &&
341 (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 needsCRC = 1;
343 newIndex = udfLen;
344 memcpy(newName, udfName, udfLen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700345 } else {
346 for (index = 0; index < udfLen; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 curr = udfName[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700348 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 needsCRC = 1;
350 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800351 while (index + 1 < udfLen &&
352 (udfName[index + 1] == '/' ||
353 udfName[index + 1] == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 index++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800355 }
356 if (curr == EXT_MARK &&
357 (udfLen - index - 1) <= EXT_SIZE) {
358 if (udfLen == index + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 hasExt = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800360 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 hasExt = 1;
362 extIndex = index;
363 newExtIndex = newIndex;
364 }
365 }
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100366 if (newIndex < newLen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 newName[newIndex++] = curr;
368 else
369 needsCRC = 1;
370 }
371 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700372 if (needsCRC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 uint8_t ext[EXT_SIZE];
374 int localExtIndex = 0;
375
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700376 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int maxFilenameLen;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800378 for (index = 0;
379 index < EXT_SIZE && extIndex + index + 1 < udfLen;
380 index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 curr = udfName[extIndex + index + 1];
382
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700383 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 needsCRC = 1;
385 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800386 while (extIndex + index + 2 < udfLen &&
387 (index + 1 < EXT_SIZE &&
388 (udfName[extIndex + index + 2] == '/' ||
389 udfName[extIndex + index + 2] == 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 index++;
391 }
392 ext[localExtIndex++] = curr;
393 }
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100394 maxFilenameLen = newLen - CRC_LEN - localExtIndex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (newIndex > maxFilenameLen)
396 newIndex = maxFilenameLen;
397 else
398 newIndex = newExtIndex;
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100399 } else if (newIndex > newLen - CRC_LEN)
400 newIndex = newLen - CRC_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 newName[newIndex++] = CRC_MARK;
Bob Copelandf845fce2008-04-17 09:47:48 +0200402 valueCRC = crc_itu_t(0, fidName, fidNameLen);
Andy Shevchenkoc7ff4822014-07-09 15:35:30 +0300403 newName[newIndex++] = hex_asc_upper_hi(valueCRC >> 8);
404 newName[newIndex++] = hex_asc_upper_lo(valueCRC >> 8);
405 newName[newIndex++] = hex_asc_upper_hi(valueCRC);
406 newName[newIndex++] = hex_asc_upper_lo(valueCRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700408 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 newName[newIndex++] = EXT_MARK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700410 for (index = 0; index < localExtIndex; index++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 newName[newIndex++] = ext[index];
412 }
413 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return newIndex;
416}