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