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