blob: 35cc9477b066aed431b2352cd49ebb6f05025c79 [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{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070036 if ((!dest) || (!src) || (!strlen) || (strlen > UDF_NAME_LEN - 2))
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
79/*
Fabian Frederickd67e4a42015-04-08 21:23:53 +020080 * udf_CS0toUTF8
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
82 * PURPOSE
83 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
84 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * PRE-CONDITIONS
86 * utf Pointer to UTF-8 output buffer.
87 * ocu Pointer to OSTA Compressed Unicode input buffer
88 * of size UDF_NAME_LEN bytes.
89 * both of type "struct ustr *"
90 *
91 * POST-CONDITIONS
Fabian Fredericke9d4cf412015-04-08 21:23:54 +020092 * <return> >= 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *
94 * HISTORY
95 * November 12, 1997 - Andrew E. Mileski
96 * Written, tested, and released.
97 */
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +010098int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100100 const uint8_t *ocu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 uint8_t cmp_id, ocu_len;
102 int i;
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 ocu_len = ocu_i->u_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700105 if (ocu_len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 memset(utf_o, 0, sizeof(struct ustr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
108 }
109
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100110 cmp_id = ocu_i->u_cmpID;
111 if (cmp_id != 8 && cmp_id != 16) {
112 memset(utf_o, 0, sizeof(struct ustr));
Joe Perches78ace702011-10-10 01:08:05 -0700113 pr_err("unknown compression code (%d) stri=%s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700114 cmp_id, ocu_i->u_name);
Fabian Fredericke9d4cf412015-04-08 21:23:54 +0200115 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100118 ocu = ocu_i->u_name;
119 utf_o->u_len = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700120 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 /* Expand OSTA compressed Unicode to Unicode */
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100123 uint32_t c = ocu[i++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (cmp_id == 16)
125 c = (c << 8) | ocu[i++];
126
127 /* Compress Unicode to UTF-8 */
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100128 if (c < 0x80U)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700129 utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100130 else if (c < 0x800U) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800131 utf_o->u_name[utf_o->u_len++] =
132 (uint8_t)(0xc0 | (c >> 6));
133 utf_o->u_name[utf_o->u_len++] =
134 (uint8_t)(0x80 | (c & 0x3f));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700135 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800136 utf_o->u_name[utf_o->u_len++] =
137 (uint8_t)(0xe0 | (c >> 12));
138 utf_o->u_name[utf_o->u_len++] =
139 (uint8_t)(0x80 |
140 ((c >> 6) & 0x3f));
141 utf_o->u_name[utf_o->u_len++] =
142 (uint8_t)(0x80 | (c & 0x3f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700145 utf_o->u_cmpID = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 return utf_o->u_len;
148}
149
150/*
151 *
Fabian Frederickd67e4a42015-04-08 21:23:53 +0200152 * udf_UTF8toCS0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 *
154 * PURPOSE
155 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
156 *
157 * DESCRIPTION
158 * This routine is only called by udf_lookup().
159 *
160 * PRE-CONDITIONS
161 * ocu Pointer to OSTA Compressed Unicode output
162 * buffer of size UDF_NAME_LEN bytes.
163 * utf Pointer to UTF-8 input buffer.
164 * utf_len Length of UTF-8 input buffer in bytes.
165 *
166 * POST-CONDITIONS
167 * <return> Zero on success.
168 *
169 * HISTORY
170 * November 12, 1997 - Andrew E. Mileski
171 * Written, tested, and released.
172 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700173static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 unsigned c, i, max_val, utf_char;
176 int utf_cnt, u_len;
177
178 memset(ocu, 0, sizeof(dstring) * length);
179 ocu[0] = 8;
180 max_val = 0xffU;
181
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700182try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 u_len = 0U;
184 utf_char = 0U;
185 utf_cnt = 0U;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700186 for (i = 0U; i < utf->u_len; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700187 c = (uint8_t)utf->u_name[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 /* Complete a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700190 if (utf_cnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 utf_char = (utf_char << 6) | (c & 0x3fU);
192 if (--utf_cnt)
193 continue;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700194 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* Check for a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700196 if (c & 0x80U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* Start a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700198 if ((c & 0xe0U) == 0xc0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 utf_char = c & 0x1fU;
200 utf_cnt = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700201 } else if ((c & 0xf0U) == 0xe0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 utf_char = c & 0x0fU;
203 utf_cnt = 2;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700204 } else if ((c & 0xf8U) == 0xf0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 utf_char = c & 0x07U;
206 utf_cnt = 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700207 } else if ((c & 0xfcU) == 0xf8U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 utf_char = c & 0x03U;
209 utf_cnt = 4;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700210 } else if ((c & 0xfeU) == 0xfcU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 utf_char = c & 0x01U;
212 utf_cnt = 5;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700213 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 goto error_out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 continue;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700217 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /* Single byte UTF-8 character (most common) */
219 utf_char = c;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222
223 /* Choose no compression if necessary */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700224 if (utf_char > max_val) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700225 if (max_val == 0xffU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 max_val = 0xffffU;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700227 ocu[0] = (uint8_t)0x10U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto try_again;
229 }
230 goto error_out;
231 }
232
Marcin Slusarz4b111112008-02-08 04:20:36 -0800233 if (max_val == 0xffffU)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700234 ocu[++u_len] = (uint8_t)(utf_char >> 8);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700235 ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700238 if (utf_cnt) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700239error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ocu[++u_len] = '?';
Joe Perches78ace702011-10-10 01:08:05 -0700241 printk(KERN_DEBUG pr_fmt("bad UTF-8 character\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700244 ocu[length - 1] = (uint8_t)u_len + 1;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return u_len + 1;
247}
248
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700249static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100250 const struct ustr *ocu_i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100252 const uint8_t *ocu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 uint8_t cmp_id, ocu_len;
Jan Kara59285c22009-02-04 19:46:11 +0100254 int i, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 ocu_len = ocu_i->u_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700258 if (ocu_len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 memset(utf_o, 0, sizeof(struct ustr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
261 }
262
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100263 cmp_id = ocu_i->u_cmpID;
264 if (cmp_id != 8 && cmp_id != 16) {
265 memset(utf_o, 0, sizeof(struct ustr));
Joe Perches78ace702011-10-10 01:08:05 -0700266 pr_err("unknown compression code (%d) stri=%s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700267 cmp_id, ocu_i->u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 0;
269 }
270
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100271 ocu = ocu_i->u_name;
272 utf_o->u_len = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700273 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* Expand OSTA compressed Unicode to Unicode */
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100275 uint32_t c = ocu[i++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (cmp_id == 16)
277 c = (c << 8) | ocu[i++];
278
Jan Kara59285c22009-02-04 19:46:11 +0100279 len = nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
280 UDF_NAME_LEN - utf_o->u_len);
281 /* Valid character? */
282 if (len >= 0)
283 utf_o->u_len += len;
284 else
285 utf_o->u_name[utf_o->u_len++] = '?';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700287 utf_o->u_cmpID = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 return utf_o->u_len;
290}
291
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700292static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700293 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Jan Kara59285c22009-02-04 19:46:11 +0100295 int len;
296 unsigned i, max_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 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);
Jan Kara59285c22009-02-04 19:46:11 +0100308 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 continue;
Jan Kara59285c22009-02-04 19:46:11 +0100310 /* Invalid character, deal with it */
311 if (len < 0) {
312 len = 1;
313 uni_char = '?';
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700316 if (uni_char > max_val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 max_val = 0xffffU;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700318 ocu[0] = (uint8_t)0x10U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 goto try_again;
320 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (max_val == 0xffffU)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700323 ocu[++u_len] = (uint8_t)(uni_char >> 8);
324 ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 i += len - 1;
326 }
327
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700328 ocu[length - 1] = (uint8_t)u_len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return u_len + 1;
330}
331
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100332int udf_get_filename(struct super_block *sb, uint8_t *sname, int slen,
333 uint8_t *dname, int dlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100335 struct ustr *filename, *unifilename;
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200336 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Fabian Frederick31f25662015-04-08 21:23:52 +0200338 if (!slen)
339 return -EIO;
340
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100341 filename = kmalloc(sizeof(struct ustr), GFP_NOFS);
342 if (!filename)
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200343 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100345 unifilename = kmalloc(sizeof(struct ustr), GFP_NOFS);
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200346 if (!unifilename) {
347 ret = -ENOMEM;
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100348 goto out1;
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200349 }
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100350
Fabian Frederick31f25662015-04-08 21:23:52 +0200351 udf_build_ustr_exact(unifilename, sname, slen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700352 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Fabian Fredericke9d4cf412015-04-08 21:23:54 +0200353 ret = udf_CS0toUTF8(filename, unifilename);
354 if (ret < 0) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800355 udf_debug("Failed in udf_get_filename: sname = %s\n",
356 sname);
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100357 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700359 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100360 if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, filename,
361 unifilename)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800362 udf_debug("Failed in udf_get_filename: sname = %s\n",
363 sname);
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100364 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800366 } else
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100367 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200369 ret = udf_translate_to_linux(dname, dlen,
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100370 filename->u_name, filename->u_len,
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100371 unifilename->u_name, unifilename->u_len);
372out2:
373 kfree(unifilename);
374out1:
375 kfree(filename);
Fabian Frederick5ceb8b52015-04-08 21:23:51 +0200376 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700379int udf_put_filename(struct super_block *sb, const uint8_t *sname,
380 uint8_t *dname, int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct ustr unifilename;
383 int namelen;
384
Marcin Slusarz4b111112008-02-08 04:20:36 -0800385 if (!udf_char_to_ustr(&unifilename, sname, flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700388 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700389 namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800390 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700392 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800393 namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
394 &unifilename, UDF_NAME_LEN);
395 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800397 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
399
400 return namelen;
401}
402
403#define ILLEGAL_CHAR_MARK '_'
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700404#define EXT_MARK '.'
405#define CRC_MARK '#'
406#define EXT_SIZE 5
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100407/* Number of chars we need to store generated CRC to make filename unique */
408#define CRC_LEN 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100410static int udf_translate_to_linux(uint8_t *newName, int newLen,
411 uint8_t *udfName, int udfLen,
412 uint8_t *fidName, int fidNameLen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700414 int index, newIndex = 0, needsCRC = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int extIndex = 0, newExtIndex = 0, hasExt = 0;
416 unsigned short valueCRC;
417 uint8_t curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700419 if (udfName[0] == '.' &&
420 (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 needsCRC = 1;
422 newIndex = udfLen;
423 memcpy(newName, udfName, udfLen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700424 } else {
425 for (index = 0; index < udfLen; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 curr = udfName[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700427 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 needsCRC = 1;
429 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800430 while (index + 1 < udfLen &&
431 (udfName[index + 1] == '/' ||
432 udfName[index + 1] == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 index++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800434 }
435 if (curr == EXT_MARK &&
436 (udfLen - index - 1) <= EXT_SIZE) {
437 if (udfLen == index + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 hasExt = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800439 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 hasExt = 1;
441 extIndex = index;
442 newExtIndex = newIndex;
443 }
444 }
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100445 if (newIndex < newLen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 newName[newIndex++] = curr;
447 else
448 needsCRC = 1;
449 }
450 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700451 if (needsCRC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 uint8_t ext[EXT_SIZE];
453 int localExtIndex = 0;
454
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700455 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int maxFilenameLen;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800457 for (index = 0;
458 index < EXT_SIZE && extIndex + index + 1 < udfLen;
459 index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 curr = udfName[extIndex + index + 1];
461
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700462 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 needsCRC = 1;
464 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800465 while (extIndex + index + 2 < udfLen &&
466 (index + 1 < EXT_SIZE &&
467 (udfName[extIndex + index + 2] == '/' ||
468 udfName[extIndex + index + 2] == 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 index++;
470 }
471 ext[localExtIndex++] = curr;
472 }
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100473 maxFilenameLen = newLen - CRC_LEN - localExtIndex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (newIndex > maxFilenameLen)
475 newIndex = maxFilenameLen;
476 else
477 newIndex = newExtIndex;
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100478 } else if (newIndex > newLen - CRC_LEN)
479 newIndex = newLen - CRC_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 newName[newIndex++] = CRC_MARK;
Bob Copelandf845fce2008-04-17 09:47:48 +0200481 valueCRC = crc_itu_t(0, fidName, fidNameLen);
Andy Shevchenkoc7ff4822014-07-09 15:35:30 +0300482 newName[newIndex++] = hex_asc_upper_hi(valueCRC >> 8);
483 newName[newIndex++] = hex_asc_upper_lo(valueCRC >> 8);
484 newName[newIndex++] = hex_asc_upper_hi(valueCRC);
485 newName[newIndex++] = hex_asc_upper_lo(valueCRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700487 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 newName[newIndex++] = EXT_MARK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700489 for (index = 0; index < localExtIndex; index++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 newName[newIndex++] = ext[index];
491 }
492 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return newIndex;
495}