| 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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Marcin Slusarz | 6305a0a | 2008-02-04 22:27:39 +0100 | [diff] [blame] | 52 | if (!dest || !ptr || !size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | return -1; | 
| Marcin Slusarz | 6305a0a | 2008-02-04 22:27:39 +0100 | [diff] [blame] | 54 | BUG_ON(size < 2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 |  | 
| Marcin Slusarz | 6305a0a | 2008-02-04 22:27:39 +0100 | [diff] [blame] | 56 | usesize = min_t(size_t, ptr[size - 1], sizeof(dest->u_name)); | 
|  | 57 | usesize = min(usesize, size - 2); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 58 | dest->u_cmpID = ptr[0]; | 
| Marcin Slusarz | 6305a0a | 2008-02-04 22:27:39 +0100 | [diff] [blame] | 59 | dest->u_len = usesize; | 
|  | 60 | memcpy(dest->u_name, ptr + 1, usesize); | 
|  | 61 | memset(dest->u_name + usesize, 0, sizeof(dest->u_name) - usesize); | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 62 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | return 0; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | /* | 
|  | 67 | * udf_build_ustr_exact | 
|  | 68 | */ | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 69 | 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] | 70 | { | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 71 | if ((!dest) || (!ptr) || (!exactsize)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return -1; | 
|  | 73 |  | 
|  | 74 | memset(dest, 0, sizeof(struct ustr)); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 75 | dest->u_cmpID = ptr[0]; | 
|  | 76 | dest->u_len = exactsize - 1; | 
|  | 77 | memcpy(dest->u_name, ptr + 1, exactsize - 1); | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 78 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return 0; | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | /* | 
|  | 83 | * udf_ocu_to_utf8 | 
|  | 84 | * | 
|  | 85 | * PURPOSE | 
|  | 86 | *	Convert OSTA Compressed Unicode to the UTF-8 equivalent. | 
|  | 87 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * PRE-CONDITIONS | 
|  | 89 | *	utf			Pointer to UTF-8 output buffer. | 
|  | 90 | *	ocu			Pointer to OSTA Compressed Unicode input buffer | 
|  | 91 | *				of size UDF_NAME_LEN bytes. | 
|  | 92 | * 				both of type "struct ustr *" | 
|  | 93 | * | 
|  | 94 | * POST-CONDITIONS | 
|  | 95 | *	<return>		Zero on success. | 
|  | 96 | * | 
|  | 97 | * HISTORY | 
|  | 98 | *	November 12, 1997 - Andrew E. Mileski | 
|  | 99 | *	Written, tested, and released. | 
|  | 100 | */ | 
| marcin.slusarz@gmail.com | 79cfe0f | 2008-01-30 22:03:51 +0100 | [diff] [blame] | 101 | int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { | 
| marcin.slusarz@gmail.com | 79cfe0f | 2008-01-30 22:03:51 +0100 | [diff] [blame] | 103 | const uint8_t *ocu; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | uint8_t cmp_id, ocu_len; | 
|  | 105 | int i; | 
|  | 106 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | ocu_len = ocu_i->u_len; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 108 | if (ocu_len == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | memset(utf_o, 0, sizeof(struct ustr)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return 0; | 
|  | 111 | } | 
|  | 112 |  | 
| marcin.slusarz@gmail.com | 79cfe0f | 2008-01-30 22:03:51 +0100 | [diff] [blame] | 113 | cmp_id = ocu_i->u_cmpID; | 
|  | 114 | if (cmp_id != 8 && cmp_id != 16) { | 
|  | 115 | memset(utf_o, 0, sizeof(struct ustr)); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 116 | printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n", | 
|  | 117 | cmp_id, ocu_i->u_name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return 0; | 
|  | 119 | } | 
|  | 120 |  | 
| marcin.slusarz@gmail.com | 79cfe0f | 2008-01-30 22:03:51 +0100 | [diff] [blame] | 121 | ocu = ocu_i->u_name; | 
|  | 122 | utf_o->u_len = 0; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 123 | 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] | 124 |  | 
|  | 125 | /* Expand OSTA compressed Unicode to Unicode */ | 
| marcin.slusarz@gmail.com | 79cfe0f | 2008-01-30 22:03:51 +0100 | [diff] [blame] | 126 | uint32_t c = ocu[i++]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | if (cmp_id == 16) | 
|  | 128 | c = (c << 8) | ocu[i++]; | 
|  | 129 |  | 
|  | 130 | /* Compress Unicode to UTF-8 */ | 
| marcin.slusarz@gmail.com | 79cfe0f | 2008-01-30 22:03:51 +0100 | [diff] [blame] | 131 | if (c < 0x80U) | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 132 | 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] | 133 | else if (c < 0x800U) { | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 134 | utf_o->u_name[utf_o->u_len++] = | 
|  | 135 | (uint8_t)(0xc0 | (c >> 6)); | 
|  | 136 | utf_o->u_name[utf_o->u_len++] = | 
|  | 137 | (uint8_t)(0x80 | (c & 0x3f)); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 138 | } else { | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 139 | utf_o->u_name[utf_o->u_len++] = | 
|  | 140 | (uint8_t)(0xe0 | (c >> 12)); | 
|  | 141 | utf_o->u_name[utf_o->u_len++] = | 
|  | 142 | (uint8_t)(0x80 | | 
|  | 143 | ((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 | 28de794 | 2007-07-21 04:37:18 -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 | 28de794 | 2007-07-21 04:37:18 -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++) { | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 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 | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 216 | } else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | goto error_out; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 218 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | continue; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 220 | } else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | /* Single byte UTF-8 character (most common) */ | 
|  | 222 | utf_char = c; | 
| 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 | } | 
|  | 225 |  | 
|  | 226 | /* Choose no compression if necessary */ | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 227 | if (utf_char > max_val) { | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 228 | if (max_val == 0xffU) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | max_val = 0xffffU; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 230 | ocu[0] = (uint8_t)0x10U; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | goto try_again; | 
|  | 232 | } | 
|  | 233 | goto error_out; | 
|  | 234 | } | 
|  | 235 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 236 | if (max_val == 0xffffU) | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 237 | ocu[++u_len] = (uint8_t)(utf_char >> 8); | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 238 | ocu[++u_len] = (uint8_t)(utf_char & 0xffU); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } | 
|  | 240 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 241 | if (utf_cnt) { | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 242 | error_out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | ocu[++u_len] = '?'; | 
|  | 244 | printk(KERN_DEBUG "udf: bad UTF-8 character\n"); | 
|  | 245 | } | 
|  | 246 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 247 | ocu[length - 1] = (uint8_t)u_len + 1; | 
|  | 248 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return u_len + 1; | 
|  | 250 | } | 
|  | 251 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 252 | 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] | 253 | const struct ustr *ocu_i) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { | 
| marcin.slusarz@gmail.com | 34f953d | 2008-02-27 22:38:36 +0100 | [diff] [blame] | 255 | const uint8_t *ocu; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | uint8_t cmp_id, ocu_len; | 
|  | 257 | int i; | 
|  | 258 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 |  | 
|  | 260 | ocu_len = ocu_i->u_len; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 261 | if (ocu_len == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | memset(utf_o, 0, sizeof(struct ustr)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | return 0; | 
|  | 264 | } | 
|  | 265 |  | 
| marcin.slusarz@gmail.com | 34f953d | 2008-02-27 22:38:36 +0100 | [diff] [blame] | 266 | cmp_id = ocu_i->u_cmpID; | 
|  | 267 | if (cmp_id != 8 && cmp_id != 16) { | 
|  | 268 | memset(utf_o, 0, sizeof(struct ustr)); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 269 | printk(KERN_ERR "udf: unknown compression code (%d) stri=%s\n", | 
|  | 270 | cmp_id, ocu_i->u_name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | return 0; | 
|  | 272 | } | 
|  | 273 |  | 
| marcin.slusarz@gmail.com | 34f953d | 2008-02-27 22:38:36 +0100 | [diff] [blame] | 274 | ocu = ocu_i->u_name; | 
|  | 275 | utf_o->u_len = 0; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 276 | 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] | 277 | /* Expand OSTA compressed Unicode to Unicode */ | 
| marcin.slusarz@gmail.com | 34f953d | 2008-02-27 22:38:36 +0100 | [diff] [blame] | 278 | uint32_t c = ocu[i++]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | if (cmp_id == 16) | 
|  | 280 | c = (c << 8) | ocu[i++]; | 
|  | 281 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 282 | utf_o->u_len += nls->uni2char(c, &utf_o->u_name[utf_o->u_len], | 
|  | 283 | UDF_NAME_LEN - utf_o->u_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 285 | utf_o->u_cmpID = 8; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 |  | 
|  | 287 | return utf_o->u_len; | 
|  | 288 | } | 
|  | 289 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 290 | 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] | 291 | int length) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { | 
|  | 293 | unsigned len, i, max_val; | 
|  | 294 | uint16_t uni_char; | 
|  | 295 | int u_len; | 
|  | 296 |  | 
|  | 297 | memset(ocu, 0, sizeof(dstring) * length); | 
|  | 298 | ocu[0] = 8; | 
|  | 299 | max_val = 0xffU; | 
|  | 300 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 301 | try_again: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | u_len = 0U; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 303 | for (i = 0U; i < uni->u_len; i++) { | 
|  | 304 | 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] | 305 | if (len <= 0) | 
|  | 306 | continue; | 
|  | 307 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 308 | if (uni_char > max_val) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | max_val = 0xffffU; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 310 | ocu[0] = (uint8_t)0x10U; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | goto try_again; | 
|  | 312 | } | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 313 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | if (max_val == 0xffffU) | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 315 | ocu[++u_len] = (uint8_t)(uni_char >> 8); | 
|  | 316 | ocu[++u_len] = (uint8_t)(uni_char & 0xffU); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | i += len - 1; | 
|  | 318 | } | 
|  | 319 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 320 | ocu[length - 1] = (uint8_t)u_len + 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | return u_len + 1; | 
|  | 322 | } | 
|  | 323 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 324 | 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] | 325 | int flen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { | 
|  | 327 | struct ustr filename, unifilename; | 
|  | 328 | int len; | 
|  | 329 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 330 | if (udf_build_ustr_exact(&unifilename, sname, flen)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 333 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) { | 
|  | 334 | if (!udf_CS0toUTF8(&filename, &unifilename)) { | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 335 | udf_debug("Failed in udf_get_filename: sname = %s\n", | 
|  | 336 | sname); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | return 0; | 
|  | 338 | } | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 339 | } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) { | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 340 | if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, &filename, | 
|  | 341 | &unifilename)) { | 
|  | 342 | udf_debug("Failed in udf_get_filename: sname = %s\n", | 
|  | 343 | sname); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | return 0; | 
|  | 345 | } | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 346 | } else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | return 0; | 
|  | 348 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 349 | len = udf_translate_to_linux(dname, filename.u_name, filename.u_len, | 
|  | 350 | unifilename.u_name, unifilename.u_len); | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 351 | if (len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | return len; | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 353 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return 0; | 
|  | 355 | } | 
|  | 356 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 357 | int udf_put_filename(struct super_block *sb, const uint8_t *sname, | 
|  | 358 | uint8_t *dname, int flen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { | 
|  | 360 | struct ustr unifilename; | 
|  | 361 | int namelen; | 
|  | 362 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 363 | if (!udf_char_to_ustr(&unifilename, sname, flen)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 366 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) { | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 367 | namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN); | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 368 | if (!namelen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | return 0; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 370 | } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) { | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 371 | namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname, | 
|  | 372 | &unifilename, UDF_NAME_LEN); | 
|  | 373 | if (!namelen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | return 0; | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 375 | } else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | return 0; | 
|  | 377 |  | 
|  | 378 | return namelen; | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | #define ILLEGAL_CHAR_MARK	'_' | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 382 | #define EXT_MARK		'.' | 
|  | 383 | #define CRC_MARK		'#' | 
|  | 384 | #define EXT_SIZE 		5 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 386 | static int udf_translate_to_linux(uint8_t *newName, uint8_t *udfName, | 
|  | 387 | int udfLen, uint8_t *fidName, | 
|  | 388 | int fidNameLen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 390 | int index, newIndex = 0, needsCRC = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | int extIndex = 0, newExtIndex = 0, hasExt = 0; | 
|  | 392 | unsigned short valueCRC; | 
|  | 393 | uint8_t curr; | 
|  | 394 | const uint8_t hexChar[] = "0123456789ABCDEF"; | 
|  | 395 |  | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 396 | if (udfName[0] == '.' && | 
|  | 397 | (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | needsCRC = 1; | 
|  | 399 | newIndex = udfLen; | 
|  | 400 | memcpy(newName, udfName, udfLen); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 401 | } else { | 
|  | 402 | for (index = 0; index < udfLen; index++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | curr = udfName[index]; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 404 | if (curr == '/' || curr == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | needsCRC = 1; | 
|  | 406 | curr = ILLEGAL_CHAR_MARK; | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 407 | while (index + 1 < udfLen && | 
|  | 408 | (udfName[index + 1] == '/' || | 
|  | 409 | udfName[index + 1] == 0)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | index++; | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 411 | } | 
|  | 412 | if (curr == EXT_MARK && | 
|  | 413 | (udfLen - index - 1) <= EXT_SIZE) { | 
|  | 414 | if (udfLen == index + 1) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | hasExt = 0; | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 416 | else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | hasExt = 1; | 
|  | 418 | extIndex = index; | 
|  | 419 | newExtIndex = newIndex; | 
|  | 420 | } | 
|  | 421 | } | 
|  | 422 | if (newIndex < 256) | 
|  | 423 | newName[newIndex++] = curr; | 
|  | 424 | else | 
|  | 425 | needsCRC = 1; | 
|  | 426 | } | 
|  | 427 | } | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 428 | if (needsCRC) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | uint8_t ext[EXT_SIZE]; | 
|  | 430 | int localExtIndex = 0; | 
|  | 431 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 432 | if (hasExt) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | int maxFilenameLen; | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 434 | for (index = 0; | 
|  | 435 | index < EXT_SIZE && extIndex + index + 1 < udfLen; | 
|  | 436 | index++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | curr = udfName[extIndex + index + 1]; | 
|  | 438 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 439 | if (curr == '/' || curr == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | needsCRC = 1; | 
|  | 441 | curr = ILLEGAL_CHAR_MARK; | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 442 | while (extIndex + index + 2 < udfLen && | 
|  | 443 | (index + 1 < EXT_SIZE && | 
|  | 444 | (udfName[extIndex + index + 2] == '/' || | 
|  | 445 | udfName[extIndex + index + 2] == 0))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | index++; | 
|  | 447 | } | 
|  | 448 | ext[localExtIndex++] = curr; | 
|  | 449 | } | 
|  | 450 | maxFilenameLen = 250 - localExtIndex; | 
|  | 451 | if (newIndex > maxFilenameLen) | 
|  | 452 | newIndex = maxFilenameLen; | 
|  | 453 | else | 
|  | 454 | newIndex = newExtIndex; | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 455 | } else if (newIndex > 250) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | newIndex = 250; | 
|  | 457 | newName[newIndex++] = CRC_MARK; | 
| Bob Copeland | f845fce | 2008-04-17 09:47:48 +0200 | [diff] [blame] | 458 | valueCRC = crc_itu_t(0, fidName, fidNameLen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | newName[newIndex++] = hexChar[(valueCRC & 0xf000) >> 12]; | 
|  | 460 | newName[newIndex++] = hexChar[(valueCRC & 0x0f00) >> 8]; | 
|  | 461 | newName[newIndex++] = hexChar[(valueCRC & 0x00f0) >> 4]; | 
|  | 462 | newName[newIndex++] = hexChar[(valueCRC & 0x000f)]; | 
|  | 463 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 464 | if (hasExt) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | newName[newIndex++] = EXT_MARK; | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 466 | for (index = 0; index < localExtIndex; index++) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | newName[newIndex++] = ext[index]; | 
|  | 468 | } | 
|  | 469 | } | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 470 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | return newIndex; | 
|  | 472 | } |