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