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 | |
| 79 | /* |
Fabian Frederick | d67e4a4 | 2015-04-08 21:23:53 +0200 | [diff] [blame] | 80 | * udf_CS0toUTF8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | * |
| 82 | * PURPOSE |
| 83 | * Convert OSTA Compressed Unicode to the UTF-8 equivalent. |
| 84 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | * PRE-CONDITIONS |
| 86 | * utf Pointer to UTF-8 output buffer. |
| 87 | * ocu Pointer to OSTA Compressed Unicode input buffer |
| 88 | * of size UDF_NAME_LEN bytes. |
| 89 | * both of type "struct ustr *" |
| 90 | * |
| 91 | * POST-CONDITIONS |
Fabian Frederick | e9d4cf41 | 2015-04-08 21:23:54 +0200 | [diff] [blame] | 92 | * <return> >= 0 on success. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * |
| 94 | * HISTORY |
| 95 | * November 12, 1997 - Andrew E. Mileski |
| 96 | * Written, tested, and released. |
| 97 | */ |
marcin.slusarz@gmail.com | 79cfe0f | 2008-01-30 22:03:51 +0100 | [diff] [blame] | 98 | int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
marcin.slusarz@gmail.com | 79cfe0f | 2008-01-30 22:03:51 +0100 | [diff] [blame] | 100 | const uint8_t *ocu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | uint8_t cmp_id, ocu_len; |
| 102 | int i; |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | ocu_len = ocu_i->u_len; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 105 | if (ocu_len == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | memset(utf_o, 0, sizeof(struct ustr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
marcin.slusarz@gmail.com | 79cfe0f | 2008-01-30 22:03:51 +0100 | [diff] [blame] | 110 | cmp_id = ocu_i->u_cmpID; |
| 111 | if (cmp_id != 8 && cmp_id != 16) { |
| 112 | memset(utf_o, 0, sizeof(struct ustr)); |
Joe Perches | 78ace70 | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 113 | pr_err("unknown compression code (%d) stri=%s\n", |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 114 | cmp_id, ocu_i->u_name); |
Fabian Frederick | e9d4cf41 | 2015-04-08 21:23:54 +0200 | [diff] [blame] | 115 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
marcin.slusarz@gmail.com | 79cfe0f | 2008-01-30 22:03:51 +0100 | [diff] [blame] | 118 | ocu = ocu_i->u_name; |
| 119 | utf_o->u_len = 0; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 120 | 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] | 121 | |
| 122 | /* Expand OSTA compressed Unicode to Unicode */ |
marcin.slusarz@gmail.com | 79cfe0f | 2008-01-30 22:03:51 +0100 | [diff] [blame] | 123 | uint32_t c = ocu[i++]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if (cmp_id == 16) |
| 125 | c = (c << 8) | ocu[i++]; |
| 126 | |
| 127 | /* Compress Unicode to UTF-8 */ |
marcin.slusarz@gmail.com | 79cfe0f | 2008-01-30 22:03:51 +0100 | [diff] [blame] | 128 | if (c < 0x80U) |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 129 | utf_o->u_name[utf_o->u_len++] = (uint8_t)c; |
marcin.slusarz@gmail.com | 79cfe0f | 2008-01-30 22:03:51 +0100 | [diff] [blame] | 130 | else if (c < 0x800U) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 131 | utf_o->u_name[utf_o->u_len++] = |
| 132 | (uint8_t)(0xc0 | (c >> 6)); |
| 133 | utf_o->u_name[utf_o->u_len++] = |
| 134 | (uint8_t)(0x80 | (c & 0x3f)); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 135 | } else { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 136 | utf_o->u_name[utf_o->u_len++] = |
| 137 | (uint8_t)(0xe0 | (c >> 12)); |
| 138 | utf_o->u_name[utf_o->u_len++] = |
| 139 | (uint8_t)(0x80 | |
| 140 | ((c >> 6) & 0x3f)); |
| 141 | utf_o->u_name[utf_o->u_len++] = |
| 142 | (uint8_t)(0x80 | (c & 0x3f)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 145 | utf_o->u_cmpID = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | return utf_o->u_len; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * |
Fabian Frederick | d67e4a4 | 2015-04-08 21:23:53 +0200 | [diff] [blame] | 152 | * udf_UTF8toCS0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | * |
| 154 | * PURPOSE |
| 155 | * Convert UTF-8 to the OSTA Compressed Unicode equivalent. |
| 156 | * |
| 157 | * DESCRIPTION |
| 158 | * This routine is only called by udf_lookup(). |
| 159 | * |
| 160 | * PRE-CONDITIONS |
| 161 | * ocu Pointer to OSTA Compressed Unicode output |
| 162 | * buffer of size UDF_NAME_LEN bytes. |
| 163 | * utf Pointer to UTF-8 input buffer. |
| 164 | * utf_len Length of UTF-8 input buffer in bytes. |
| 165 | * |
| 166 | * POST-CONDITIONS |
| 167 | * <return> Zero on success. |
| 168 | * |
| 169 | * HISTORY |
| 170 | * November 12, 1997 - Andrew E. Mileski |
| 171 | * Written, tested, and released. |
| 172 | */ |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 173 | static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
| 175 | unsigned c, i, max_val, utf_char; |
| 176 | int utf_cnt, u_len; |
| 177 | |
| 178 | memset(ocu, 0, sizeof(dstring) * length); |
| 179 | ocu[0] = 8; |
| 180 | max_val = 0xffU; |
| 181 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 182 | try_again: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | u_len = 0U; |
| 184 | utf_char = 0U; |
| 185 | utf_cnt = 0U; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 186 | for (i = 0U; i < utf->u_len; i++) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 187 | c = (uint8_t)utf->u_name[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | /* Complete a multi-byte UTF-8 character */ |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 190 | if (utf_cnt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | utf_char = (utf_char << 6) | (c & 0x3fU); |
| 192 | if (--utf_cnt) |
| 193 | continue; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 194 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | /* Check for a multi-byte UTF-8 character */ |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 196 | if (c & 0x80U) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | /* Start a multi-byte UTF-8 character */ |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 198 | if ((c & 0xe0U) == 0xc0U) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | utf_char = c & 0x1fU; |
| 200 | utf_cnt = 1; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 201 | } else if ((c & 0xf0U) == 0xe0U) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | utf_char = c & 0x0fU; |
| 203 | utf_cnt = 2; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 204 | } else if ((c & 0xf8U) == 0xf0U) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | utf_char = c & 0x07U; |
| 206 | utf_cnt = 3; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 207 | } else if ((c & 0xfcU) == 0xf8U) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | utf_char = c & 0x03U; |
| 209 | utf_cnt = 4; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 210 | } else if ((c & 0xfeU) == 0xfcU) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | utf_char = c & 0x01U; |
| 212 | utf_cnt = 5; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 213 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | goto error_out; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | continue; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 217 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | /* Single byte UTF-8 character (most common) */ |
| 219 | utf_char = c; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* Choose no compression if necessary */ |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 224 | if (utf_char > max_val) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 225 | if (max_val == 0xffU) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | max_val = 0xffffU; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 227 | ocu[0] = (uint8_t)0x10U; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | goto try_again; |
| 229 | } |
| 230 | goto error_out; |
| 231 | } |
| 232 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 233 | if (max_val == 0xffffU) |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 234 | ocu[++u_len] = (uint8_t)(utf_char >> 8); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 235 | ocu[++u_len] = (uint8_t)(utf_char & 0xffU); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 238 | if (utf_cnt) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 239 | error_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | ocu[++u_len] = '?'; |
Joe Perches | 78ace70 | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 241 | printk(KERN_DEBUG pr_fmt("bad UTF-8 character\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 244 | ocu[length - 1] = (uint8_t)u_len + 1; |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | return u_len + 1; |
| 247 | } |
| 248 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 249 | static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o, |
marcin.slusarz@gmail.com | 34f953d | 2008-02-27 22:38:36 +0100 | [diff] [blame] | 250 | const struct ustr *ocu_i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
marcin.slusarz@gmail.com | 34f953d | 2008-02-27 22:38:36 +0100 | [diff] [blame] | 252 | const uint8_t *ocu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | uint8_t cmp_id, ocu_len; |
Jan Kara | 59285c2 | 2009-02-04 19:46:11 +0100 | [diff] [blame] | 254 | int i, len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | ocu_len = ocu_i->u_len; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 258 | if (ocu_len == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | memset(utf_o, 0, sizeof(struct ustr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
marcin.slusarz@gmail.com | 34f953d | 2008-02-27 22:38:36 +0100 | [diff] [blame] | 263 | cmp_id = ocu_i->u_cmpID; |
| 264 | if (cmp_id != 8 && cmp_id != 16) { |
| 265 | memset(utf_o, 0, sizeof(struct ustr)); |
Joe Perches | 78ace70 | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 266 | pr_err("unknown compression code (%d) stri=%s\n", |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 267 | cmp_id, ocu_i->u_name); |
Fabian Frederick | 78fc2e6 | 2015-04-08 21:23:55 +0200 | [diff] [blame] | 268 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
marcin.slusarz@gmail.com | 34f953d | 2008-02-27 22:38:36 +0100 | [diff] [blame] | 271 | ocu = ocu_i->u_name; |
| 272 | utf_o->u_len = 0; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 273 | 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] | 274 | /* Expand OSTA compressed Unicode to Unicode */ |
marcin.slusarz@gmail.com | 34f953d | 2008-02-27 22:38:36 +0100 | [diff] [blame] | 275 | uint32_t c = ocu[i++]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | if (cmp_id == 16) |
| 277 | c = (c << 8) | ocu[i++]; |
| 278 | |
Jan Kara | 59285c2 | 2009-02-04 19:46:11 +0100 | [diff] [blame] | 279 | len = nls->uni2char(c, &utf_o->u_name[utf_o->u_len], |
| 280 | UDF_NAME_LEN - utf_o->u_len); |
| 281 | /* Valid character? */ |
| 282 | if (len >= 0) |
| 283 | utf_o->u_len += len; |
| 284 | else |
| 285 | utf_o->u_name[utf_o->u_len++] = '?'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 287 | utf_o->u_cmpID = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | return utf_o->u_len; |
| 290 | } |
| 291 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 292 | static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni, |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 293 | int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Jan Kara | 59285c2 | 2009-02-04 19:46:11 +0100 | [diff] [blame] | 295 | int len; |
| 296 | unsigned i, max_val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | uint16_t uni_char; |
| 298 | int u_len; |
| 299 | |
| 300 | memset(ocu, 0, sizeof(dstring) * length); |
| 301 | ocu[0] = 8; |
| 302 | max_val = 0xffU; |
| 303 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 304 | try_again: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | u_len = 0U; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 306 | for (i = 0U; i < uni->u_len; i++) { |
| 307 | len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char); |
Jan Kara | 59285c2 | 2009-02-04 19:46:11 +0100 | [diff] [blame] | 308 | if (!len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | continue; |
Jan Kara | 59285c2 | 2009-02-04 19:46:11 +0100 | [diff] [blame] | 310 | /* Invalid character, deal with it */ |
| 311 | if (len < 0) { |
| 312 | len = 1; |
| 313 | uni_char = '?'; |
| 314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 316 | if (uni_char > max_val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | max_val = 0xffffU; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 318 | ocu[0] = (uint8_t)0x10U; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | goto try_again; |
| 320 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | if (max_val == 0xffffU) |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 323 | ocu[++u_len] = (uint8_t)(uni_char >> 8); |
| 324 | ocu[++u_len] = (uint8_t)(uni_char & 0xffU); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | i += len - 1; |
| 326 | } |
| 327 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 328 | ocu[length - 1] = (uint8_t)u_len + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | return u_len + 1; |
| 330 | } |
| 331 | |
Jan Kara | 0e5cc9a | 2014-12-18 22:37:50 +0100 | [diff] [blame] | 332 | int udf_get_filename(struct super_block *sb, uint8_t *sname, int slen, |
| 333 | uint8_t *dname, int dlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
Marcin Slusarz | 530f1a5 | 2008-11-16 19:02:45 +0100 | [diff] [blame] | 335 | struct ustr *filename, *unifilename; |
Fabian Frederick | 6ce6383 | 2015-04-08 21:23:57 +0200 | [diff] [blame] | 336 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Fabian Frederick | 31f2566 | 2015-04-08 21:23:52 +0200 | [diff] [blame] | 338 | if (!slen) |
| 339 | return -EIO; |
| 340 | |
Marcin Slusarz | 530f1a5 | 2008-11-16 19:02:45 +0100 | [diff] [blame] | 341 | filename = kmalloc(sizeof(struct ustr), GFP_NOFS); |
| 342 | if (!filename) |
Fabian Frederick | 5ceb8b5 | 2015-04-08 21:23:51 +0200 | [diff] [blame] | 343 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Marcin Slusarz | 530f1a5 | 2008-11-16 19:02:45 +0100 | [diff] [blame] | 345 | unifilename = kmalloc(sizeof(struct ustr), GFP_NOFS); |
Fabian Frederick | 5ceb8b5 | 2015-04-08 21:23:51 +0200 | [diff] [blame] | 346 | if (!unifilename) { |
| 347 | ret = -ENOMEM; |
Marcin Slusarz | 530f1a5 | 2008-11-16 19:02:45 +0100 | [diff] [blame] | 348 | goto out1; |
Fabian Frederick | 5ceb8b5 | 2015-04-08 21:23:51 +0200 | [diff] [blame] | 349 | } |
Marcin Slusarz | 530f1a5 | 2008-11-16 19:02:45 +0100 | [diff] [blame] | 350 | |
Fabian Frederick | 31f2566 | 2015-04-08 21:23:52 +0200 | [diff] [blame] | 351 | udf_build_ustr_exact(unifilename, sname, slen); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 352 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) { |
Fabian Frederick | e9d4cf41 | 2015-04-08 21:23:54 +0200 | [diff] [blame] | 353 | ret = udf_CS0toUTF8(filename, unifilename); |
| 354 | if (ret < 0) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 355 | udf_debug("Failed in udf_get_filename: sname = %s\n", |
| 356 | sname); |
Marcin Slusarz | 530f1a5 | 2008-11-16 19:02:45 +0100 | [diff] [blame] | 357 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 359 | } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) { |
Fabian Frederick | 78fc2e6 | 2015-04-08 21:23:55 +0200 | [diff] [blame] | 360 | ret = udf_CS0toNLS(UDF_SB(sb)->s_nls_map, filename, |
| 361 | unifilename); |
| 362 | if (ret < 0) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 363 | udf_debug("Failed in udf_get_filename: sname = %s\n", |
| 364 | sname); |
Marcin Slusarz | 530f1a5 | 2008-11-16 19:02:45 +0100 | [diff] [blame] | 365 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 367 | } else |
Fabian Frederick | 5dce54b | 2015-04-08 21:23:56 +0200 | [diff] [blame] | 368 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Fabian Frederick | 5ceb8b5 | 2015-04-08 21:23:51 +0200 | [diff] [blame] | 370 | ret = udf_translate_to_linux(dname, dlen, |
Jan Kara | 0e5cc9a | 2014-12-18 22:37:50 +0100 | [diff] [blame] | 371 | filename->u_name, filename->u_len, |
Marcin Slusarz | 530f1a5 | 2008-11-16 19:02:45 +0100 | [diff] [blame] | 372 | unifilename->u_name, unifilename->u_len); |
Fabian Frederick | 6ce6383 | 2015-04-08 21:23:57 +0200 | [diff] [blame] | 373 | /* Zero length filename isn't valid... */ |
| 374 | if (ret == 0) |
| 375 | ret = -EINVAL; |
Marcin Slusarz | 530f1a5 | 2008-11-16 19:02:45 +0100 | [diff] [blame] | 376 | out2: |
| 377 | kfree(unifilename); |
| 378 | out1: |
| 379 | kfree(filename); |
Fabian Frederick | 5ceb8b5 | 2015-04-08 21:23:51 +0200 | [diff] [blame] | 380 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 383 | int udf_put_filename(struct super_block *sb, const uint8_t *sname, |
| 384 | uint8_t *dname, int flen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { |
| 386 | struct ustr unifilename; |
| 387 | int namelen; |
| 388 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 389 | if (!udf_char_to_ustr(&unifilename, sname, flen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 392 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 393 | namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN); |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 394 | if (!namelen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | return 0; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 396 | } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 397 | namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname, |
| 398 | &unifilename, UDF_NAME_LEN); |
| 399 | if (!namelen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | return 0; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 401 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | return 0; |
| 403 | |
| 404 | return namelen; |
| 405 | } |
| 406 | |
| 407 | #define ILLEGAL_CHAR_MARK '_' |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 408 | #define EXT_MARK '.' |
| 409 | #define CRC_MARK '#' |
| 410 | #define EXT_SIZE 5 |
Jan Kara | 0e5cc9a | 2014-12-18 22:37:50 +0100 | [diff] [blame] | 411 | /* Number of chars we need to store generated CRC to make filename unique */ |
| 412 | #define CRC_LEN 5 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Jan Kara | 0e5cc9a | 2014-12-18 22:37:50 +0100 | [diff] [blame] | 414 | static int udf_translate_to_linux(uint8_t *newName, int newLen, |
| 415 | uint8_t *udfName, int udfLen, |
| 416 | uint8_t *fidName, int fidNameLen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | { |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 418 | int index, newIndex = 0, needsCRC = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | int extIndex = 0, newExtIndex = 0, hasExt = 0; |
| 420 | unsigned short valueCRC; |
| 421 | uint8_t curr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 423 | if (udfName[0] == '.' && |
| 424 | (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | needsCRC = 1; |
| 426 | newIndex = udfLen; |
| 427 | memcpy(newName, udfName, udfLen); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 428 | } else { |
| 429 | for (index = 0; index < udfLen; index++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | curr = udfName[index]; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 431 | if (curr == '/' || curr == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | needsCRC = 1; |
| 433 | curr = ILLEGAL_CHAR_MARK; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 434 | while (index + 1 < udfLen && |
| 435 | (udfName[index + 1] == '/' || |
| 436 | udfName[index + 1] == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | index++; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 438 | } |
| 439 | if (curr == EXT_MARK && |
| 440 | (udfLen - index - 1) <= EXT_SIZE) { |
| 441 | if (udfLen == index + 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | hasExt = 0; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 443 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | hasExt = 1; |
| 445 | extIndex = index; |
| 446 | newExtIndex = newIndex; |
| 447 | } |
| 448 | } |
Jan Kara | 0e5cc9a | 2014-12-18 22:37:50 +0100 | [diff] [blame] | 449 | if (newIndex < newLen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | newName[newIndex++] = curr; |
| 451 | else |
| 452 | needsCRC = 1; |
| 453 | } |
| 454 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 455 | if (needsCRC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | uint8_t ext[EXT_SIZE]; |
| 457 | int localExtIndex = 0; |
| 458 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 459 | if (hasExt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | int maxFilenameLen; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 461 | for (index = 0; |
| 462 | index < EXT_SIZE && extIndex + index + 1 < udfLen; |
| 463 | index++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | curr = udfName[extIndex + index + 1]; |
| 465 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 466 | if (curr == '/' || curr == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | needsCRC = 1; |
| 468 | curr = ILLEGAL_CHAR_MARK; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 469 | while (extIndex + index + 2 < udfLen && |
| 470 | (index + 1 < EXT_SIZE && |
| 471 | (udfName[extIndex + index + 2] == '/' || |
| 472 | udfName[extIndex + index + 2] == 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | index++; |
| 474 | } |
| 475 | ext[localExtIndex++] = curr; |
| 476 | } |
Jan Kara | 0e5cc9a | 2014-12-18 22:37:50 +0100 | [diff] [blame] | 477 | maxFilenameLen = newLen - CRC_LEN - localExtIndex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | if (newIndex > maxFilenameLen) |
| 479 | newIndex = maxFilenameLen; |
| 480 | else |
| 481 | newIndex = newExtIndex; |
Jan Kara | 0e5cc9a | 2014-12-18 22:37:50 +0100 | [diff] [blame] | 482 | } else if (newIndex > newLen - CRC_LEN) |
| 483 | newIndex = newLen - CRC_LEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | newName[newIndex++] = CRC_MARK; |
Bob Copeland | f845fce | 2008-04-17 09:47:48 +0200 | [diff] [blame] | 485 | valueCRC = crc_itu_t(0, fidName, fidNameLen); |
Andy Shevchenko | c7ff482 | 2014-07-09 15:35:30 +0300 | [diff] [blame] | 486 | newName[newIndex++] = hex_asc_upper_hi(valueCRC >> 8); |
| 487 | newName[newIndex++] = hex_asc_upper_lo(valueCRC >> 8); |
| 488 | newName[newIndex++] = hex_asc_upper_hi(valueCRC); |
| 489 | newName[newIndex++] = hex_asc_upper_lo(valueCRC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 491 | if (hasExt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | newName[newIndex++] = EXT_MARK; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 493 | for (index = 0; index < localExtIndex; index++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | newName[newIndex++] = ext[index]; |
| 495 | } |
| 496 | } |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | return newIndex; |
| 499 | } |