blob: dc5990f4c9522d2f244e090283369d5e64ce0dce [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
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -060031static int udf_translate_to_linux(uint8_t *, int, const uint8_t *, int,
32 const uint8_t *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060034static int udf_uni2char_utf8(wchar_t uni,
35 unsigned char *out,
36 int boundlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060038 int u_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060040 if (boundlen <= 0)
41 return -ENAMETOOLONG;
42
43 if (uni < 0x80) {
44 out[u_len++] = (unsigned char)uni;
45 } else if (uni < 0x800) {
46 if (boundlen < 2)
47 return -ENAMETOOLONG;
48 out[u_len++] = (unsigned char)(0xc0 | (uni >> 6));
49 out[u_len++] = (unsigned char)(0x80 | (uni & 0x3f));
50 } else {
51 if (boundlen < 3)
52 return -ENAMETOOLONG;
53 out[u_len++] = (unsigned char)(0xe0 | (uni >> 12));
54 out[u_len++] = (unsigned char)(0x80 | ((uni >> 6) & 0x3f));
55 out[u_len++] = (unsigned char)(0x80 | (uni & 0x3f));
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060057 return u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060060static int udf_char2uni_utf8(const unsigned char *in,
61 int boundlen,
62 wchar_t *uni)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060064 unsigned int utf_char;
65 unsigned char c;
66 int utf_cnt, u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060068 utf_char = 0;
69 utf_cnt = 0;
70 for (u_len = 0; u_len < boundlen;) {
71 c = in[u_len++];
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 /* Complete a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070074 if (utf_cnt) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060075 utf_char = (utf_char << 6) | (c & 0x3f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (--utf_cnt)
77 continue;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070078 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 /* Check for a multi-byte UTF-8 character */
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060080 if (c & 0x80) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 /* Start a multi-byte UTF-8 character */
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060082 if ((c & 0xe0) == 0xc0) {
83 utf_char = c & 0x1f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 utf_cnt = 1;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060085 } else if ((c & 0xf0) == 0xe0) {
86 utf_char = c & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 utf_cnt = 2;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060088 } else if ((c & 0xf8) == 0xf0) {
89 utf_char = c & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 utf_cnt = 3;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060091 } else if ((c & 0xfc) == 0xf8) {
92 utf_char = c & 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 utf_cnt = 4;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060094 } else if ((c & 0xfe) == 0xfc) {
95 utf_char = c & 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 utf_cnt = 5;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070097 } else {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -060098 utf_cnt = -1;
99 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 continue;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700102 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /* Single byte UTF-8 character (most common) */
104 utf_char = c;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600107 *uni = utf_char;
108 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700110 if (utf_cnt) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600111 *uni = '?';
112 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600114 return u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600117static int udf_name_from_CS0(uint8_t *str_o, int str_max_len,
118 const uint8_t *ocu, int ocu_len,
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600119 int (*conv_f)(wchar_t, unsigned char *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600121 uint8_t cmp_id;
Jan Kara59285c22009-02-04 19:46:11 +0100122 int i, len;
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600123 int str_o_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600125 if (str_max_len <= 0)
126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700128 if (ocu_len == 0) {
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600129 memset(str_o, 0, str_max_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return 0;
131 }
132
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600133 cmp_id = ocu[0];
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100134 if (cmp_id != 8 && cmp_id != 16) {
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600135 memset(str_o, 0, str_max_len);
136 pr_err("unknown compression code (%d) stri=%s\n", cmp_id, ocu);
Fabian Frederick78fc2e62015-04-08 21:23:55 +0200137 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600140 for (i = 1; (i < ocu_len) && (str_o_len < str_max_len);) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /* Expand OSTA compressed Unicode to Unicode */
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100142 uint32_t c = ocu[i++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (cmp_id == 16)
144 c = (c << 8) | ocu[i++];
145
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600146 len = conv_f(c, &str_o[str_o_len], str_max_len - str_o_len);
Jan Kara59285c22009-02-04 19:46:11 +0100147 /* Valid character? */
148 if (len >= 0)
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600149 str_o_len += len;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600150 else if (len == -ENAMETOOLONG)
151 break;
Jan Kara59285c22009-02-04 19:46:11 +0100152 else
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600153 str_o[str_o_len++] = '?';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600156 return str_o_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600159static int udf_name_to_CS0(uint8_t *ocu, int ocu_max_len,
160 const uint8_t *str_i, int str_len,
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600161 int (*conv_f)(const unsigned char *, int, wchar_t *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600163 int i, len;
164 unsigned int max_val;
165 wchar_t uni_char;
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600166 int u_len, u_ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600168 if (ocu_max_len <= 0)
169 return 0;
170
171 memset(ocu, 0, ocu_max_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ocu[0] = 8;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600173 max_val = 0xff;
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600174 u_ch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700176try_again:
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600177 u_len = 1;
178 for (i = 0; i < str_len; i++) {
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600179 /* Name didn't fit? */
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600180 if (u_len + u_ch > ocu_max_len)
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600181 return 0;
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600182 len = conv_f(&str_i[i], str_len - i, &uni_char);
Jan Kara59285c22009-02-04 19:46:11 +0100183 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 continue;
Jan Kara59285c22009-02-04 19:46:11 +0100185 /* Invalid character, deal with it */
186 if (len < 0) {
187 len = 1;
188 uni_char = '?';
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700191 if (uni_char > max_val) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600192 max_val = 0xffff;
193 ocu[0] = 0x10;
Andrew Gabbasovbb00c892015-12-24 10:25:33 -0600194 u_ch = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto try_again;
196 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700197
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600198 if (max_val == 0xffff)
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600199 ocu[u_len++] = (uint8_t)(uni_char >> 8);
200 ocu[u_len++] = (uint8_t)(uni_char & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 i += len - 1;
202 }
203
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600204 return u_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600207int udf_CS0toUTF8(uint8_t *utf_o, int o_len, const uint8_t *ocu_i, int i_len)
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600208{
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600209 return udf_name_from_CS0(utf_o, o_len, ocu_i, i_len,
210 udf_uni2char_utf8);
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600211}
212
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600213int udf_get_filename(struct super_block *sb, const uint8_t *sname, int slen,
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100214 uint8_t *dname, int dlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600216 uint8_t *filename;
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600217 int (*conv_f)(wchar_t, unsigned char *, int);
Fabian Frederick6ce63832015-04-08 21:23:57 +0200218 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Fabian Frederick31f25662015-04-08 21:23:52 +0200220 if (!slen)
221 return -EIO;
222
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600223 if (dlen <= 0)
224 return 0;
225
226 filename = kmalloc(dlen, GFP_NOFS);
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100227 if (!filename)
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200228 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700230 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600231 conv_f = udf_uni2char_utf8;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700232 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600233 conv_f = UDF_SB(sb)->s_nls_map->uni2char;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800234 } else
Fabian Frederick5dce54b2015-04-08 21:23:56 +0200235 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600237 ret = udf_name_from_CS0(filename, dlen, sname, slen, conv_f);
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600238 if (ret < 0) {
239 udf_debug("Failed in udf_get_filename: sname = %s\n", sname);
240 goto out2;
241 }
242
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600243 ret = udf_translate_to_linux(dname, dlen, filename, dlen,
244 sname + 1, slen - 1);
Fabian Frederick6ce63832015-04-08 21:23:57 +0200245 /* Zero length filename isn't valid... */
246 if (ret == 0)
247 ret = -EINVAL;
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100248out2:
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100249 kfree(filename);
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200250 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Andrew Gabbasov525e2c52016-01-15 02:44:19 -0600253int udf_put_filename(struct super_block *sb, const uint8_t *sname, int slen,
254 uint8_t *dname, int dlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600256 int (*conv_f)(const unsigned char *, int, wchar_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700258 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600259 conv_f = udf_char2uni_utf8;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700260 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600261 conv_f = UDF_SB(sb)->s_nls_map->char2uni;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800262 } else
Andrew Gabbasov3e7fc202016-01-15 02:44:20 -0600263 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600265 return udf_name_to_CS0(dname, dlen, sname, slen, conv_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268#define ILLEGAL_CHAR_MARK '_'
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700269#define EXT_MARK '.'
270#define CRC_MARK '#'
271#define EXT_SIZE 5
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100272/* Number of chars we need to store generated CRC to make filename unique */
273#define CRC_LEN 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100275static int udf_translate_to_linux(uint8_t *newName, int newLen,
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600276 const uint8_t *udfName, int udfLen,
277 const uint8_t *fidName, int fidNameLen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700279 int index, newIndex = 0, needsCRC = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 int extIndex = 0, newExtIndex = 0, hasExt = 0;
281 unsigned short valueCRC;
282 uint8_t curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700284 if (udfName[0] == '.' &&
285 (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 needsCRC = 1;
287 newIndex = udfLen;
288 memcpy(newName, udfName, udfLen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700289 } else {
290 for (index = 0; index < udfLen; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 curr = udfName[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700292 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 needsCRC = 1;
294 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800295 while (index + 1 < udfLen &&
296 (udfName[index + 1] == '/' ||
297 udfName[index + 1] == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 index++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800299 }
300 if (curr == EXT_MARK &&
301 (udfLen - index - 1) <= EXT_SIZE) {
302 if (udfLen == index + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 hasExt = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800304 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 hasExt = 1;
306 extIndex = index;
307 newExtIndex = newIndex;
308 }
309 }
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100310 if (newIndex < newLen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 newName[newIndex++] = curr;
312 else
313 needsCRC = 1;
314 }
315 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700316 if (needsCRC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 uint8_t ext[EXT_SIZE];
318 int localExtIndex = 0;
319
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700320 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 int maxFilenameLen;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800322 for (index = 0;
323 index < EXT_SIZE && extIndex + index + 1 < udfLen;
324 index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 curr = udfName[extIndex + index + 1];
326
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700327 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 needsCRC = 1;
329 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800330 while (extIndex + index + 2 < udfLen &&
331 (index + 1 < EXT_SIZE &&
332 (udfName[extIndex + index + 2] == '/' ||
333 udfName[extIndex + index + 2] == 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 index++;
335 }
336 ext[localExtIndex++] = curr;
337 }
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100338 maxFilenameLen = newLen - CRC_LEN - localExtIndex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (newIndex > maxFilenameLen)
340 newIndex = maxFilenameLen;
341 else
342 newIndex = newExtIndex;
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100343 } else if (newIndex > newLen - CRC_LEN)
344 newIndex = newLen - CRC_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 newName[newIndex++] = CRC_MARK;
Bob Copelandf845fce2008-04-17 09:47:48 +0200346 valueCRC = crc_itu_t(0, fidName, fidNameLen);
Andy Shevchenkoc7ff4822014-07-09 15:35:30 +0300347 newName[newIndex++] = hex_asc_upper_hi(valueCRC >> 8);
348 newName[newIndex++] = hex_asc_upper_lo(valueCRC >> 8);
349 newName[newIndex++] = hex_asc_upper_hi(valueCRC);
350 newName[newIndex++] = hex_asc_upper_lo(valueCRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700352 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 newName[newIndex++] = EXT_MARK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700354 for (index = 0; index < localExtIndex; index++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 newName[newIndex++] = ext[index];
356 }
357 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return newIndex;
360}