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