5 * Routines for converting between UTF-8 and OSTA Compressed Unicode.
6 * Also handles filename mangling
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
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.
23 #include <linux/kernel.h>
24 #include <linux/string.h> /* for memset */
25 #include <linux/nls.h>
26 #include <linux/crc-itu-t.h>
27 #include <linux/slab.h>
31 static int udf_translate_to_linux(uint8_t *, int, uint8_t *, int, uint8_t *,
34 static int udf_char_to_ustr(struct ustr *dest, const uint8_t *src, int strlen)
36 if ((!dest) || (!src) || (!strlen) || (strlen > UDF_NAME_LEN - 2))
39 memset(dest, 0, sizeof(struct ustr));
40 memcpy(dest->u_name, src, strlen);
50 int udf_build_ustr(struct ustr *dest, dstring *ptr, int size)
54 if (!dest || !ptr || !size)
58 usesize = min_t(size_t, ptr[size - 1], sizeof(dest->u_name));
59 usesize = min(usesize, size - 2);
60 dest->u_cmpID = ptr[0];
61 dest->u_len = usesize;
62 memcpy(dest->u_name, ptr + 1, usesize);
63 memset(dest->u_name + usesize, 0, sizeof(dest->u_name) - usesize);
69 * udf_build_ustr_exact
71 static int udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
73 if ((!dest) || (!ptr) || (!exactsize))
76 memset(dest, 0, sizeof(struct ustr));
77 dest->u_cmpID = ptr[0];
78 dest->u_len = exactsize - 1;
79 memcpy(dest->u_name, ptr + 1, exactsize - 1);
88 * Convert OSTA Compressed Unicode to the UTF-8 equivalent.
91 * utf Pointer to UTF-8 output buffer.
92 * ocu Pointer to OSTA Compressed Unicode input buffer
93 * of size UDF_NAME_LEN bytes.
94 * both of type "struct ustr *"
97 * <return> Zero on success.
100 * November 12, 1997 - Andrew E. Mileski
101 * Written, tested, and released.
103 int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
106 uint8_t cmp_id, ocu_len;
109 ocu_len = ocu_i->u_len;
111 memset(utf_o, 0, sizeof(struct ustr));
115 cmp_id = ocu_i->u_cmpID;
116 if (cmp_id != 8 && cmp_id != 16) {
117 memset(utf_o, 0, sizeof(struct ustr));
118 pr_err("unknown compression code (%d) stri=%s\n",
119 cmp_id, ocu_i->u_name);
125 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
127 /* Expand OSTA compressed Unicode to Unicode */
128 uint32_t c = ocu[i++];
130 c = (c << 8) | ocu[i++];
132 /* Compress Unicode to UTF-8 */
134 utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
135 else if (c < 0x800U) {
136 utf_o->u_name[utf_o->u_len++] =
137 (uint8_t)(0xc0 | (c >> 6));
138 utf_o->u_name[utf_o->u_len++] =
139 (uint8_t)(0x80 | (c & 0x3f));
141 utf_o->u_name[utf_o->u_len++] =
142 (uint8_t)(0xe0 | (c >> 12));
143 utf_o->u_name[utf_o->u_len++] =
146 utf_o->u_name[utf_o->u_len++] =
147 (uint8_t)(0x80 | (c & 0x3f));
160 * Convert UTF-8 to the OSTA Compressed Unicode equivalent.
163 * This routine is only called by udf_lookup().
166 * ocu Pointer to OSTA Compressed Unicode output
167 * buffer of size UDF_NAME_LEN bytes.
168 * utf Pointer to UTF-8 input buffer.
169 * utf_len Length of UTF-8 input buffer in bytes.
172 * <return> Zero on success.
175 * November 12, 1997 - Andrew E. Mileski
176 * Written, tested, and released.
178 static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
180 unsigned c, i, max_val, utf_char;
183 memset(ocu, 0, sizeof(dstring) * length);
191 for (i = 0U; i < utf->u_len; i++) {
192 c = (uint8_t)utf->u_name[i];
194 /* Complete a multi-byte UTF-8 character */
196 utf_char = (utf_char << 6) | (c & 0x3fU);
200 /* Check for a multi-byte UTF-8 character */
202 /* Start a multi-byte UTF-8 character */
203 if ((c & 0xe0U) == 0xc0U) {
204 utf_char = c & 0x1fU;
206 } else if ((c & 0xf0U) == 0xe0U) {
207 utf_char = c & 0x0fU;
209 } else if ((c & 0xf8U) == 0xf0U) {
210 utf_char = c & 0x07U;
212 } else if ((c & 0xfcU) == 0xf8U) {
213 utf_char = c & 0x03U;
215 } else if ((c & 0xfeU) == 0xfcU) {
216 utf_char = c & 0x01U;
223 /* Single byte UTF-8 character (most common) */
228 /* Choose no compression if necessary */
229 if (utf_char > max_val) {
230 if (max_val == 0xffU) {
232 ocu[0] = (uint8_t)0x10U;
238 if (max_val == 0xffffU)
239 ocu[++u_len] = (uint8_t)(utf_char >> 8);
240 ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
246 printk(KERN_DEBUG pr_fmt("bad UTF-8 character\n"));
249 ocu[length - 1] = (uint8_t)u_len + 1;
254 static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
255 const struct ustr *ocu_i)
258 uint8_t cmp_id, ocu_len;
262 ocu_len = ocu_i->u_len;
264 memset(utf_o, 0, sizeof(struct ustr));
268 cmp_id = ocu_i->u_cmpID;
269 if (cmp_id != 8 && cmp_id != 16) {
270 memset(utf_o, 0, sizeof(struct ustr));
271 pr_err("unknown compression code (%d) stri=%s\n",
272 cmp_id, ocu_i->u_name);
278 for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
279 /* Expand OSTA compressed Unicode to Unicode */
280 uint32_t c = ocu[i++];
282 c = (c << 8) | ocu[i++];
284 len = nls->uni2char(c, &utf_o->u_name[utf_o->u_len],
285 UDF_NAME_LEN - utf_o->u_len);
286 /* Valid character? */
290 utf_o->u_name[utf_o->u_len++] = '?';
297 static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
305 memset(ocu, 0, sizeof(dstring) * length);
311 for (i = 0U; i < uni->u_len; i++) {
312 len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
315 /* Invalid character, deal with it */
321 if (uni_char > max_val) {
323 ocu[0] = (uint8_t)0x10U;
327 if (max_val == 0xffffU)
328 ocu[++u_len] = (uint8_t)(uni_char >> 8);
329 ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
333 ocu[length - 1] = (uint8_t)u_len + 1;
337 int udf_get_filename(struct super_block *sb, uint8_t *sname, int slen,
338 uint8_t *dname, int dlen)
340 struct ustr *filename, *unifilename;
343 filename = kmalloc(sizeof(struct ustr), GFP_NOFS);
347 unifilename = kmalloc(sizeof(struct ustr), GFP_NOFS);
351 if (udf_build_ustr_exact(unifilename, sname, slen))
354 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
355 if (!udf_CS0toUTF8(filename, unifilename)) {
356 udf_debug("Failed in udf_get_filename: sname = %s\n",
360 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
361 if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, filename,
363 udf_debug("Failed in udf_get_filename: sname = %s\n",
370 len = udf_translate_to_linux(dname, dlen,
371 filename->u_name, filename->u_len,
372 unifilename->u_name, unifilename->u_len);
380 int udf_put_filename(struct super_block *sb, const uint8_t *sname,
381 uint8_t *dname, int flen)
383 struct ustr unifilename;
386 if (!udf_char_to_ustr(&unifilename, sname, flen))
389 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
390 namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
393 } else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
394 namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
395 &unifilename, UDF_NAME_LEN);
404 #define ILLEGAL_CHAR_MARK '_'
408 /* Number of chars we need to store generated CRC to make filename unique */
411 static int udf_translate_to_linux(uint8_t *newName, int newLen,
412 uint8_t *udfName, int udfLen,
413 uint8_t *fidName, int fidNameLen)
415 int index, newIndex = 0, needsCRC = 0;
416 int extIndex = 0, newExtIndex = 0, hasExt = 0;
417 unsigned short valueCRC;
420 if (udfName[0] == '.' &&
421 (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
424 memcpy(newName, udfName, udfLen);
426 for (index = 0; index < udfLen; index++) {
427 curr = udfName[index];
428 if (curr == '/' || curr == 0) {
430 curr = ILLEGAL_CHAR_MARK;
431 while (index + 1 < udfLen &&
432 (udfName[index + 1] == '/' ||
433 udfName[index + 1] == 0))
436 if (curr == EXT_MARK &&
437 (udfLen - index - 1) <= EXT_SIZE) {
438 if (udfLen == index + 1)
443 newExtIndex = newIndex;
446 if (newIndex < newLen)
447 newName[newIndex++] = curr;
453 uint8_t ext[EXT_SIZE];
454 int localExtIndex = 0;
459 index < EXT_SIZE && extIndex + index + 1 < udfLen;
461 curr = udfName[extIndex + index + 1];
463 if (curr == '/' || curr == 0) {
465 curr = ILLEGAL_CHAR_MARK;
466 while (extIndex + index + 2 < udfLen &&
467 (index + 1 < EXT_SIZE &&
468 (udfName[extIndex + index + 2] == '/' ||
469 udfName[extIndex + index + 2] == 0)))
472 ext[localExtIndex++] = curr;
474 maxFilenameLen = newLen - CRC_LEN - localExtIndex;
475 if (newIndex > maxFilenameLen)
476 newIndex = maxFilenameLen;
478 newIndex = newExtIndex;
479 } else if (newIndex > newLen - CRC_LEN)
480 newIndex = newLen - CRC_LEN;
481 newName[newIndex++] = CRC_MARK;
482 valueCRC = crc_itu_t(0, fidName, fidNameLen);
483 newName[newIndex++] = hex_asc_upper_hi(valueCRC >> 8);
484 newName[newIndex++] = hex_asc_upper_lo(valueCRC >> 8);
485 newName[newIndex++] = hex_asc_upper_hi(valueCRC);
486 newName[newIndex++] = hex_asc_upper_lo(valueCRC);
489 newName[newIndex++] = EXT_MARK;
490 for (index = 0; index < localExtIndex; index++)
491 newName[newIndex++] = ext[index];