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