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