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