blob: 2eafe2c4d23979a2d70a0f886ad063525d4319c2 [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 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070071static int udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070073 if ((!dest) || (!ptr) || (!exactsize))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return -1;
75
76 memset(dest, 0, sizeof(struct ustr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070077 dest->u_cmpID = ptr[0];
78 dest->u_len = exactsize - 1;
79 memcpy(dest->u_name, ptr + 1, exactsize - 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return 0;
82}
83
84/*
85 * udf_ocu_to_utf8
86 *
87 * PURPOSE
88 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
89 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * PRE-CONDITIONS
91 * utf Pointer to UTF-8 output buffer.
92 * ocu Pointer to OSTA Compressed Unicode input buffer
93 * of size UDF_NAME_LEN bytes.
94 * both of type "struct ustr *"
95 *
96 * POST-CONDITIONS
97 * <return> Zero on success.
98 *
99 * HISTORY
100 * November 12, 1997 - Andrew E. Mileski
101 * Written, tested, and released.
102 */
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100103int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100105 const uint8_t *ocu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 uint8_t cmp_id, ocu_len;
107 int i;
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 ocu_len = ocu_i->u_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700110 if (ocu_len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 memset(utf_o, 0, sizeof(struct ustr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 0;
113 }
114
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100115 cmp_id = ocu_i->u_cmpID;
116 if (cmp_id != 8 && cmp_id != 16) {
117 memset(utf_o, 0, sizeof(struct ustr));
Joe Perches78ace702011-10-10 01:08:05 -0700118 pr_err("unknown compression code (%d) stri=%s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700119 cmp_id, ocu_i->u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 0;
121 }
122
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100123 ocu = ocu_i->u_name;
124 utf_o->u_len = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700125 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 /* Expand OSTA compressed Unicode to Unicode */
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100128 uint32_t c = ocu[i++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (cmp_id == 16)
130 c = (c << 8) | ocu[i++];
131
132 /* Compress Unicode to UTF-8 */
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100133 if (c < 0x80U)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700134 utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
marcin.slusarz@gmail.com79cfe0f2008-01-30 22:03:51 +0100135 else if (c < 0x800U) {
Andrew Gabbasovd6341752015-12-24 10:25:32 -0600136 if (utf_o->u_len > (UDF_NAME_LEN - 4))
137 break;
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 {
Andrew Gabbasovd6341752015-12-24 10:25:32 -0600143 if (utf_o->u_len > (UDF_NAME_LEN - 5))
144 break;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800145 utf_o->u_name[utf_o->u_len++] =
146 (uint8_t)(0xe0 | (c >> 12));
147 utf_o->u_name[utf_o->u_len++] =
148 (uint8_t)(0x80 |
149 ((c >> 6) & 0x3f));
150 utf_o->u_name[utf_o->u_len++] =
151 (uint8_t)(0x80 | (c & 0x3f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700154 utf_o->u_cmpID = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 return utf_o->u_len;
157}
158
159/*
160 *
161 * udf_utf8_to_ocu
162 *
163 * PURPOSE
164 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
165 *
166 * DESCRIPTION
167 * This routine is only called by udf_lookup().
168 *
169 * PRE-CONDITIONS
170 * ocu Pointer to OSTA Compressed Unicode output
171 * buffer of size UDF_NAME_LEN bytes.
172 * utf Pointer to UTF-8 input buffer.
173 * utf_len Length of UTF-8 input buffer in bytes.
174 *
175 * POST-CONDITIONS
176 * <return> Zero on success.
177 *
178 * HISTORY
179 * November 12, 1997 - Andrew E. Mileski
180 * Written, tested, and released.
181 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700182static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 unsigned c, i, max_val, utf_char;
Andrew Gabbasov516a1022015-12-24 10:25:33 -0600185 int utf_cnt, u_len, u_ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 memset(ocu, 0, sizeof(dstring) * length);
188 ocu[0] = 8;
189 max_val = 0xffU;
Andrew Gabbasov516a1022015-12-24 10:25:33 -0600190 u_ch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700192try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 u_len = 0U;
194 utf_char = 0U;
195 utf_cnt = 0U;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700196 for (i = 0U; i < utf->u_len; i++) {
Andrew Gabbasov516a1022015-12-24 10:25:33 -0600197 /* Name didn't fit? */
198 if (u_len + 1 + u_ch >= length)
199 return 0;
200
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700201 c = (uint8_t)utf->u_name[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /* Complete a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700204 if (utf_cnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 utf_char = (utf_char << 6) | (c & 0x3fU);
206 if (--utf_cnt)
207 continue;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700208 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* Check for a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700210 if (c & 0x80U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* Start a multi-byte UTF-8 character */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700212 if ((c & 0xe0U) == 0xc0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 utf_char = c & 0x1fU;
214 utf_cnt = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700215 } else if ((c & 0xf0U) == 0xe0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 utf_char = c & 0x0fU;
217 utf_cnt = 2;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700218 } else if ((c & 0xf8U) == 0xf0U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 utf_char = c & 0x07U;
220 utf_cnt = 3;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700221 } else if ((c & 0xfcU) == 0xf8U) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 utf_char = c & 0x03U;
223 utf_cnt = 4;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700224 } else if ((c & 0xfeU) == 0xfcU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 utf_char = c & 0x01U;
226 utf_cnt = 5;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700227 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto error_out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 continue;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700231 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* Single byte UTF-8 character (most common) */
233 utf_char = c;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
237 /* Choose no compression if necessary */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700238 if (utf_char > max_val) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700239 if (max_val == 0xffU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 max_val = 0xffffU;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700241 ocu[0] = (uint8_t)0x10U;
Andrew Gabbasov516a1022015-12-24 10:25:33 -0600242 u_ch = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto try_again;
244 }
245 goto error_out;
246 }
247
Marcin Slusarz4b111112008-02-08 04:20:36 -0800248 if (max_val == 0xffffU)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700249 ocu[++u_len] = (uint8_t)(utf_char >> 8);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700250 ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700253 if (utf_cnt) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700254error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 ocu[++u_len] = '?';
Joe Perches78ace702011-10-10 01:08:05 -0700256 printk(KERN_DEBUG pr_fmt("bad UTF-8 character\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700259 ocu[length - 1] = (uint8_t)u_len + 1;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return u_len + 1;
262}
263
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700264static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100265 const struct ustr *ocu_i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100267 const uint8_t *ocu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 uint8_t cmp_id, ocu_len;
Jan Kara59285c22009-02-04 19:46:11 +0100269 int i, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 ocu_len = ocu_i->u_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700273 if (ocu_len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 memset(utf_o, 0, sizeof(struct ustr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 0;
276 }
277
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100278 cmp_id = ocu_i->u_cmpID;
279 if (cmp_id != 8 && cmp_id != 16) {
280 memset(utf_o, 0, sizeof(struct ustr));
Joe Perches78ace702011-10-10 01:08:05 -0700281 pr_err("unknown compression code (%d) stri=%s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700282 cmp_id, ocu_i->u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284 }
285
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100286 ocu = ocu_i->u_name;
287 utf_o->u_len = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700288 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* Expand OSTA compressed Unicode to Unicode */
marcin.slusarz@gmail.com34f953d2008-02-27 22:38:36 +0100290 uint32_t c = ocu[i++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (cmp_id == 16)
292 c = (c << 8) | ocu[i++];
293
Jan Kara59285c22009-02-04 19:46:11 +0100294 len = nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
Andrew Gabbasovd6341752015-12-24 10:25:32 -0600295 UDF_NAME_LEN - 2 - utf_o->u_len);
Jan Kara59285c22009-02-04 19:46:11 +0100296 /* Valid character? */
297 if (len >= 0)
298 utf_o->u_len += len;
299 else
300 utf_o->u_name[utf_o->u_len++] = '?';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700302 utf_o->u_cmpID = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 return utf_o->u_len;
305}
306
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700307static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700308 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Jan Kara59285c22009-02-04 19:46:11 +0100310 int len;
311 unsigned i, max_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 uint16_t uni_char;
Andrew Gabbasov516a1022015-12-24 10:25:33 -0600313 int u_len, u_ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 memset(ocu, 0, sizeof(dstring) * length);
316 ocu[0] = 8;
317 max_val = 0xffU;
Andrew Gabbasov516a1022015-12-24 10:25:33 -0600318 u_ch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700320try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 u_len = 0U;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700322 for (i = 0U; i < uni->u_len; i++) {
Andrew Gabbasov516a1022015-12-24 10:25:33 -0600323 /* Name didn't fit? */
324 if (u_len + 1 + u_ch >= length)
325 return 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700326 len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
Jan Kara59285c22009-02-04 19:46:11 +0100327 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 continue;
Jan Kara59285c22009-02-04 19:46:11 +0100329 /* Invalid character, deal with it */
330 if (len < 0) {
331 len = 1;
332 uni_char = '?';
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700335 if (uni_char > max_val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 max_val = 0xffffU;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700337 ocu[0] = (uint8_t)0x10U;
Andrew Gabbasov516a1022015-12-24 10:25:33 -0600338 u_ch = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 goto try_again;
340 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (max_val == 0xffffU)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700343 ocu[++u_len] = (uint8_t)(uni_char >> 8);
344 ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 i += len - 1;
346 }
347
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700348 ocu[length - 1] = (uint8_t)u_len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return u_len + 1;
350}
351
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100352int udf_get_filename(struct super_block *sb, uint8_t *sname, int slen,
353 uint8_t *dname, int dlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100355 struct ustr *filename, *unifilename;
356 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100358 filename = kmalloc(sizeof(struct ustr), GFP_NOFS);
359 if (!filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100362 unifilename = kmalloc(sizeof(struct ustr), GFP_NOFS);
363 if (!unifilename)
364 goto out1;
365
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100366 if (udf_build_ustr_exact(unifilename, sname, slen))
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100367 goto out2;
368
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700369 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100370 if (!udf_CS0toUTF8(filename, unifilename)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800371 udf_debug("Failed in udf_get_filename: sname = %s\n",
372 sname);
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100373 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700375 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100376 if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, filename,
377 unifilename)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800378 udf_debug("Failed in udf_get_filename: sname = %s\n",
379 sname);
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100380 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800382 } else
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100383 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100385 len = udf_translate_to_linux(dname, dlen,
386 filename->u_name, filename->u_len,
Marcin Slusarz530f1a52008-11-16 19:02:45 +0100387 unifilename->u_name, unifilename->u_len);
388out2:
389 kfree(unifilename);
390out1:
391 kfree(filename);
392 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700395int udf_put_filename(struct super_block *sb, const uint8_t *sname,
396 uint8_t *dname, int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct ustr unifilename;
399 int namelen;
400
Marcin Slusarz4b111112008-02-08 04:20:36 -0800401 if (!udf_char_to_ustr(&unifilename, sname, flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700404 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700405 namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800406 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700408 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800409 namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
410 &unifilename, UDF_NAME_LEN);
411 if (!namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800413 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return 0;
415
416 return namelen;
417}
418
419#define ILLEGAL_CHAR_MARK '_'
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700420#define EXT_MARK '.'
421#define CRC_MARK '#'
422#define EXT_SIZE 5
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100423/* Number of chars we need to store generated CRC to make filename unique */
424#define CRC_LEN 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100426static int udf_translate_to_linux(uint8_t *newName, int newLen,
427 uint8_t *udfName, int udfLen,
428 uint8_t *fidName, int fidNameLen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700430 int index, newIndex = 0, needsCRC = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int extIndex = 0, newExtIndex = 0, hasExt = 0;
432 unsigned short valueCRC;
433 uint8_t curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700435 if (udfName[0] == '.' &&
436 (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 needsCRC = 1;
438 newIndex = udfLen;
439 memcpy(newName, udfName, udfLen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700440 } else {
441 for (index = 0; index < udfLen; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 curr = udfName[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700443 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 needsCRC = 1;
445 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800446 while (index + 1 < udfLen &&
447 (udfName[index + 1] == '/' ||
448 udfName[index + 1] == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 index++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800450 }
451 if (curr == EXT_MARK &&
452 (udfLen - index - 1) <= EXT_SIZE) {
453 if (udfLen == index + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 hasExt = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800455 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 hasExt = 1;
457 extIndex = index;
458 newExtIndex = newIndex;
459 }
460 }
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100461 if (newIndex < newLen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 newName[newIndex++] = curr;
463 else
464 needsCRC = 1;
465 }
466 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700467 if (needsCRC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 uint8_t ext[EXT_SIZE];
469 int localExtIndex = 0;
470
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700471 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 int maxFilenameLen;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800473 for (index = 0;
474 index < EXT_SIZE && extIndex + index + 1 < udfLen;
475 index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 curr = udfName[extIndex + index + 1];
477
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700478 if (curr == '/' || curr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 needsCRC = 1;
480 curr = ILLEGAL_CHAR_MARK;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800481 while (extIndex + index + 2 < udfLen &&
482 (index + 1 < EXT_SIZE &&
483 (udfName[extIndex + index + 2] == '/' ||
484 udfName[extIndex + index + 2] == 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 index++;
486 }
487 ext[localExtIndex++] = curr;
488 }
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100489 maxFilenameLen = newLen - CRC_LEN - localExtIndex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (newIndex > maxFilenameLen)
491 newIndex = maxFilenameLen;
492 else
493 newIndex = newExtIndex;
Jan Kara0e5cc9a2014-12-18 22:37:50 +0100494 } else if (newIndex > newLen - CRC_LEN)
495 newIndex = newLen - CRC_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 newName[newIndex++] = CRC_MARK;
Bob Copelandf845fce2008-04-17 09:47:48 +0200497 valueCRC = crc_itu_t(0, fidName, fidNameLen);
Andy Shevchenkoc7ff4822014-07-09 15:35:30 +0300498 newName[newIndex++] = hex_asc_upper_hi(valueCRC >> 8);
499 newName[newIndex++] = hex_asc_upper_lo(valueCRC >> 8);
500 newName[newIndex++] = hex_asc_upper_hi(valueCRC);
501 newName[newIndex++] = hex_asc_upper_lo(valueCRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700503 if (hasExt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 newName[newIndex++] = EXT_MARK;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700505 for (index = 0; index < localExtIndex; index++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 newName[newIndex++] = ext[index];
507 }
508 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return newIndex;
511}