Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * 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 Copeland | f845fce | 2008-04-17 09:47:48 +0200 | [diff] [blame] | 26 | #include <linux/crc-itu-t.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include "udf_sb.h" |
| 30 | |
Jan Kara | b8a41c4 | 2018-04-12 17:06:18 +0200 | [diff] [blame^] | 31 | #define UNICODE_MAX 0x10ffff |
Jan Kara | 44f06ba | 2018-04-12 17:22:23 +0200 | [diff] [blame] | 32 | #define SURROGATE_MASK 0xfffff800 |
| 33 | #define SURROGATE_PAIR 0x0000d800 |
| 34 | |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 35 | static int udf_uni2char_utf8(wchar_t uni, |
| 36 | unsigned char *out, |
| 37 | int boundlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 39 | int u_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 41 | if (boundlen <= 0) |
| 42 | return -ENAMETOOLONG; |
| 43 | |
Jan Kara | b8a41c4 | 2018-04-12 17:06:18 +0200 | [diff] [blame^] | 44 | u_len = utf32_to_utf8(uni, out, boundlen); |
| 45 | if (u_len < 0) { |
| 46 | if (uni > UNICODE_MAX || |
| 47 | (uni & SURROGATE_MASK) == SURROGATE_PAIR) |
| 48 | return -EINVAL; |
| 49 | return -ENAMETOOLONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 51 | return u_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 54 | static int udf_char2uni_utf8(const unsigned char *in, |
| 55 | int boundlen, |
| 56 | wchar_t *uni) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
Jan Kara | b8a41c4 | 2018-04-12 17:06:18 +0200 | [diff] [blame^] | 58 | int u_len; |
| 59 | unicode_t c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Jan Kara | b8a41c4 | 2018-04-12 17:06:18 +0200 | [diff] [blame^] | 61 | u_len = utf8_to_utf32(in, boundlen, &c); |
| 62 | if (u_len < 0) { |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 63 | *uni = '?'; |
| 64 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
Jan Kara | b8a41c4 | 2018-04-12 17:06:18 +0200 | [diff] [blame^] | 66 | |
| 67 | if (c > MAX_WCHAR_T) |
| 68 | *uni = '?'; |
| 69 | else |
| 70 | *uni = c; |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 71 | return u_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Andrew Gabbasov | 484a10f | 2016-01-15 02:44:23 -0600 | [diff] [blame] | 74 | #define ILLEGAL_CHAR_MARK '_' |
| 75 | #define EXT_MARK '.' |
| 76 | #define CRC_MARK '#' |
| 77 | #define EXT_SIZE 5 |
| 78 | /* Number of chars we need to store generated CRC to make filename unique */ |
| 79 | #define CRC_LEN 5 |
| 80 | |
| 81 | static int udf_name_conv_char(uint8_t *str_o, int str_o_max_len, |
| 82 | int *str_o_idx, |
| 83 | const uint8_t *str_i, int str_i_max_len, |
| 84 | int *str_i_idx, |
| 85 | int u_ch, int *needsCRC, |
| 86 | int (*conv_f)(wchar_t, unsigned char *, int), |
| 87 | int translate) |
| 88 | { |
| 89 | uint32_t c; |
| 90 | int illChar = 0; |
| 91 | int len, gotch = 0; |
| 92 | |
| 93 | for (; (!gotch) && (*str_i_idx < str_i_max_len); *str_i_idx += u_ch) { |
| 94 | if (*str_o_idx >= str_o_max_len) { |
| 95 | *needsCRC = 1; |
| 96 | return gotch; |
| 97 | } |
| 98 | |
| 99 | /* Expand OSTA compressed Unicode to Unicode */ |
| 100 | c = str_i[*str_i_idx]; |
| 101 | if (u_ch > 1) |
| 102 | c = (c << 8) | str_i[*str_i_idx + 1]; |
| 103 | |
| 104 | if (translate && (c == '/' || c == 0)) |
| 105 | illChar = 1; |
| 106 | else if (illChar) |
| 107 | break; |
| 108 | else |
| 109 | gotch = 1; |
| 110 | } |
| 111 | if (illChar) { |
| 112 | *needsCRC = 1; |
| 113 | c = ILLEGAL_CHAR_MARK; |
| 114 | gotch = 1; |
| 115 | } |
| 116 | if (gotch) { |
| 117 | len = conv_f(c, &str_o[*str_o_idx], str_o_max_len - *str_o_idx); |
| 118 | /* Valid character? */ |
| 119 | if (len >= 0) |
| 120 | *str_o_idx += len; |
| 121 | else if (len == -ENAMETOOLONG) { |
| 122 | *needsCRC = 1; |
| 123 | gotch = 0; |
| 124 | } else { |
| 125 | str_o[(*str_o_idx)++] = '?'; |
| 126 | *needsCRC = 1; |
| 127 | } |
| 128 | } |
| 129 | return gotch; |
| 130 | } |
| 131 | |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 132 | static int udf_name_from_CS0(uint8_t *str_o, int str_max_len, |
| 133 | const uint8_t *ocu, int ocu_len, |
Andrew Gabbasov | 484a10f | 2016-01-15 02:44:23 -0600 | [diff] [blame] | 134 | int (*conv_f)(wchar_t, unsigned char *, int), |
| 135 | int translate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Andrew Gabbasov | 484a10f | 2016-01-15 02:44:23 -0600 | [diff] [blame] | 137 | uint32_t c; |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 138 | uint8_t cmp_id; |
Andrew Gabbasov | 484a10f | 2016-01-15 02:44:23 -0600 | [diff] [blame] | 139 | int idx, len; |
| 140 | int u_ch; |
| 141 | int needsCRC = 0; |
| 142 | int ext_i_len, ext_max_len; |
| 143 | int str_o_len = 0; /* Length of resulting output */ |
| 144 | int ext_o_len = 0; /* Extension output length */ |
| 145 | int ext_crc_len = 0; /* Extension output length if used with CRC */ |
| 146 | int i_ext = -1; /* Extension position in input buffer */ |
| 147 | int o_crc = 0; /* Rightmost possible output pos for CRC+ext */ |
| 148 | unsigned short valueCRC; |
| 149 | uint8_t ext[EXT_SIZE * NLS_MAX_CHARSET_SIZE + 1]; |
| 150 | uint8_t crc[CRC_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 152 | if (str_max_len <= 0) |
| 153 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 155 | if (ocu_len == 0) { |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 156 | memset(str_o, 0, str_max_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 160 | cmp_id = ocu[0]; |
marcin.slusarz@gmail.com | 34f953d | 2008-02-27 22:38:36 +0100 | [diff] [blame] | 161 | if (cmp_id != 8 && cmp_id != 16) { |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 162 | memset(str_o, 0, str_max_len); |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 163 | pr_err("unknown compression code (%u)\n", cmp_id); |
Andrew Gabbasov | 484a10f | 2016-01-15 02:44:23 -0600 | [diff] [blame] | 164 | return -EINVAL; |
| 165 | } |
| 166 | u_ch = cmp_id >> 3; |
| 167 | |
| 168 | ocu++; |
| 169 | ocu_len--; |
| 170 | |
| 171 | if (ocu_len % u_ch) { |
| 172 | pr_err("incorrect filename length (%d)\n", ocu_len + 1); |
Fabian Frederick | 78fc2e6 | 2015-04-08 21:23:55 +0200 | [diff] [blame] | 173 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Andrew Gabbasov | 484a10f | 2016-01-15 02:44:23 -0600 | [diff] [blame] | 176 | if (translate) { |
| 177 | /* Look for extension */ |
| 178 | for (idx = ocu_len - u_ch, ext_i_len = 0; |
| 179 | (idx >= 0) && (ext_i_len < EXT_SIZE); |
| 180 | idx -= u_ch, ext_i_len++) { |
| 181 | c = ocu[idx]; |
| 182 | if (u_ch > 1) |
| 183 | c = (c << 8) | ocu[idx + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Andrew Gabbasov | 484a10f | 2016-01-15 02:44:23 -0600 | [diff] [blame] | 185 | if (c == EXT_MARK) { |
| 186 | if (ext_i_len) |
| 187 | i_ext = idx; |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | if (i_ext >= 0) { |
| 192 | /* Convert extension */ |
| 193 | ext_max_len = min_t(int, sizeof(ext), str_max_len); |
| 194 | ext[ext_o_len++] = EXT_MARK; |
| 195 | idx = i_ext + u_ch; |
| 196 | while (udf_name_conv_char(ext, ext_max_len, &ext_o_len, |
| 197 | ocu, ocu_len, &idx, |
| 198 | u_ch, &needsCRC, |
| 199 | conv_f, translate)) { |
| 200 | if ((ext_o_len + CRC_LEN) < str_max_len) |
| 201 | ext_crc_len = ext_o_len; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | idx = 0; |
| 207 | while (1) { |
| 208 | if (translate && (idx == i_ext)) { |
| 209 | if (str_o_len > (str_max_len - ext_o_len)) |
| 210 | needsCRC = 1; |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 211 | break; |
Andrew Gabbasov | 484a10f | 2016-01-15 02:44:23 -0600 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | if (!udf_name_conv_char(str_o, str_max_len, &str_o_len, |
| 215 | ocu, ocu_len, &idx, |
| 216 | u_ch, &needsCRC, conv_f, translate)) |
| 217 | break; |
| 218 | |
| 219 | if (translate && |
| 220 | (str_o_len <= (str_max_len - ext_o_len - CRC_LEN))) |
| 221 | o_crc = str_o_len; |
| 222 | } |
| 223 | |
| 224 | if (translate) { |
| 225 | if (str_o_len <= 2 && str_o[0] == '.' && |
| 226 | (str_o_len == 1 || str_o[1] == '.')) |
| 227 | needsCRC = 1; |
| 228 | if (needsCRC) { |
| 229 | str_o_len = o_crc; |
| 230 | valueCRC = crc_itu_t(0, ocu, ocu_len); |
| 231 | crc[0] = CRC_MARK; |
| 232 | crc[1] = hex_asc_upper_hi(valueCRC >> 8); |
| 233 | crc[2] = hex_asc_upper_lo(valueCRC >> 8); |
| 234 | crc[3] = hex_asc_upper_hi(valueCRC); |
| 235 | crc[4] = hex_asc_upper_lo(valueCRC); |
| 236 | len = min_t(int, CRC_LEN, str_max_len - str_o_len); |
| 237 | memcpy(&str_o[str_o_len], crc, len); |
| 238 | str_o_len += len; |
| 239 | ext_o_len = ext_crc_len; |
| 240 | } |
| 241 | if (ext_o_len > 0) { |
| 242 | memcpy(&str_o[str_o_len], ext, ext_o_len); |
| 243 | str_o_len += ext_o_len; |
| 244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 247 | return str_o_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 250 | static int udf_name_to_CS0(uint8_t *ocu, int ocu_max_len, |
| 251 | const uint8_t *str_i, int str_len, |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 252 | int (*conv_f)(const unsigned char *, int, wchar_t *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 254 | int i, len; |
| 255 | unsigned int max_val; |
| 256 | wchar_t uni_char; |
Andrew Gabbasov | bb00c89 | 2015-12-24 10:25:33 -0600 | [diff] [blame] | 257 | int u_len, u_ch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 259 | if (ocu_max_len <= 0) |
| 260 | return 0; |
| 261 | |
| 262 | memset(ocu, 0, ocu_max_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | ocu[0] = 8; |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 264 | max_val = 0xff; |
Andrew Gabbasov | bb00c89 | 2015-12-24 10:25:33 -0600 | [diff] [blame] | 265 | u_ch = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 267 | try_again: |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 268 | u_len = 1; |
| 269 | for (i = 0; i < str_len; i++) { |
Andrew Gabbasov | bb00c89 | 2015-12-24 10:25:33 -0600 | [diff] [blame] | 270 | /* Name didn't fit? */ |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 271 | if (u_len + u_ch > ocu_max_len) |
Andrew Gabbasov | bb00c89 | 2015-12-24 10:25:33 -0600 | [diff] [blame] | 272 | return 0; |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 273 | len = conv_f(&str_i[i], str_len - i, &uni_char); |
Jan Kara | 59285c2 | 2009-02-04 19:46:11 +0100 | [diff] [blame] | 274 | if (!len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | continue; |
Jan Kara | 59285c2 | 2009-02-04 19:46:11 +0100 | [diff] [blame] | 276 | /* Invalid character, deal with it */ |
| 277 | if (len < 0) { |
| 278 | len = 1; |
| 279 | uni_char = '?'; |
| 280 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 282 | if (uni_char > max_val) { |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 283 | max_val = 0xffff; |
| 284 | ocu[0] = 0x10; |
Andrew Gabbasov | bb00c89 | 2015-12-24 10:25:33 -0600 | [diff] [blame] | 285 | u_ch = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | goto try_again; |
| 287 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 288 | |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 289 | if (max_val == 0xffff) |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 290 | ocu[u_len++] = (uint8_t)(uni_char >> 8); |
| 291 | ocu[u_len++] = (uint8_t)(uni_char & 0xff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | i += len - 1; |
| 293 | } |
| 294 | |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 295 | return u_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Andrew Gabbasov | c26f6c6 | 2016-04-25 06:19:38 -0500 | [diff] [blame] | 298 | int udf_dstrCS0toUTF8(uint8_t *utf_o, int o_len, |
| 299 | const uint8_t *ocu_i, int i_len) |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 300 | { |
Andrew Gabbasov | c26f6c6 | 2016-04-25 06:19:38 -0500 | [diff] [blame] | 301 | int s_len = 0; |
| 302 | |
| 303 | if (i_len > 0) { |
| 304 | s_len = ocu_i[i_len - 1]; |
| 305 | if (s_len >= i_len) { |
| 306 | pr_err("incorrect dstring lengths (%d/%d)\n", |
| 307 | s_len, i_len); |
| 308 | return -EINVAL; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | return udf_name_from_CS0(utf_o, o_len, ocu_i, s_len, |
Andrew Gabbasov | 484a10f | 2016-01-15 02:44:23 -0600 | [diff] [blame] | 313 | udf_uni2char_utf8, 0); |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 314 | } |
| 315 | |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 316 | int udf_get_filename(struct super_block *sb, const uint8_t *sname, int slen, |
Jan Kara | 0e5cc9a | 2014-12-18 22:37:50 +0100 | [diff] [blame] | 317 | uint8_t *dname, int dlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 319 | int (*conv_f)(wchar_t, unsigned char *, int); |
Fabian Frederick | 6ce6383 | 2015-04-08 21:23:57 +0200 | [diff] [blame] | 320 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Fabian Frederick | 31f2566 | 2015-04-08 21:23:52 +0200 | [diff] [blame] | 322 | if (!slen) |
| 323 | return -EIO; |
| 324 | |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 325 | if (dlen <= 0) |
| 326 | return 0; |
| 327 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 328 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) { |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 329 | conv_f = udf_uni2char_utf8; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 330 | } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) { |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 331 | conv_f = UDF_SB(sb)->s_nls_map->uni2char; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 332 | } else |
Fabian Frederick | 5dce54b | 2015-04-08 21:23:56 +0200 | [diff] [blame] | 333 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Andrew Gabbasov | 484a10f | 2016-01-15 02:44:23 -0600 | [diff] [blame] | 335 | ret = udf_name_from_CS0(dname, dlen, sname, slen, conv_f, 1); |
Fabian Frederick | 6ce6383 | 2015-04-08 21:23:57 +0200 | [diff] [blame] | 336 | /* Zero length filename isn't valid... */ |
| 337 | if (ret == 0) |
| 338 | ret = -EINVAL; |
Fabian Frederick | 5ceb8b5 | 2015-04-08 21:23:51 +0200 | [diff] [blame] | 339 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Andrew Gabbasov | 525e2c5 | 2016-01-15 02:44:19 -0600 | [diff] [blame] | 342 | int udf_put_filename(struct super_block *sb, const uint8_t *sname, int slen, |
| 343 | uint8_t *dname, int dlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 345 | int (*conv_f)(const unsigned char *, int, wchar_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 347 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) { |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 348 | conv_f = udf_char2uni_utf8; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 349 | } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) { |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 350 | conv_f = UDF_SB(sb)->s_nls_map->char2uni; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 351 | } else |
Andrew Gabbasov | 3e7fc20 | 2016-01-15 02:44:20 -0600 | [diff] [blame] | 352 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 354 | return udf_name_to_CS0(dname, dlen, sname, slen, conv_f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
| 356 | |