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