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