Linux Audio

Check our new training course

Loading...
v5.14.15
  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 *
 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>
 26#include <linux/crc-itu-t.h>
 27#include <linux/slab.h>
 28
 29#include "udf_sb.h"
 30
 31#define PLANE_SIZE 0x10000
 32#define UNICODE_MAX 0x10ffff
 33#define SURROGATE_MASK 0xfffff800
 34#define SURROGATE_PAIR 0x0000d800
 35#define SURROGATE_LOW  0x00000400
 36#define SURROGATE_CHAR_BITS 10
 37#define SURROGATE_CHAR_MASK ((1 << SURROGATE_CHAR_BITS) - 1)
 38
 39#define ILLEGAL_CHAR_MARK	'_'
 40#define EXT_MARK		'.'
 41#define CRC_MARK		'#'
 42#define EXT_SIZE		5
 43/* Number of chars we need to store generated CRC to make filename unique */
 44#define CRC_LEN			5
 45
 46static unicode_t get_utf16_char(const uint8_t *str_i, int str_i_max_len,
 47				int str_i_idx, int u_ch, unicode_t *ret)
 48{
 49	unicode_t c;
 50	int start_idx = str_i_idx;
 51
 52	/* Expand OSTA compressed Unicode to Unicode */
 53	c = str_i[str_i_idx++];
 54	if (u_ch > 1)
 55		c = (c << 8) | str_i[str_i_idx++];
 56	if ((c & SURROGATE_MASK) == SURROGATE_PAIR) {
 57		unicode_t next;
 58
 59		/* Trailing surrogate char */
 60		if (str_i_idx >= str_i_max_len) {
 61			c = UNICODE_MAX + 1;
 62			goto out;
 63		}
 64
 65		/* Low surrogate must follow the high one... */
 66		if (c & SURROGATE_LOW) {
 67			c = UNICODE_MAX + 1;
 68			goto out;
 69		}
 70
 71		WARN_ON_ONCE(u_ch != 2);
 72		next = str_i[str_i_idx++] << 8;
 73		next |= str_i[str_i_idx++];
 74		if ((next & SURROGATE_MASK) != SURROGATE_PAIR ||
 75		    !(next & SURROGATE_LOW)) {
 76			c = UNICODE_MAX + 1;
 77			goto out;
 78		}
 79
 80		c = PLANE_SIZE +
 81		    ((c & SURROGATE_CHAR_MASK) << SURROGATE_CHAR_BITS) +
 82		    (next & SURROGATE_CHAR_MASK);
 83	}
 84out:
 85	*ret = c;
 86	return str_i_idx - start_idx;
 87}
 
 
 88
 
 
 89
 90static int udf_name_conv_char(uint8_t *str_o, int str_o_max_len,
 91			      int *str_o_idx,
 92			      const uint8_t *str_i, int str_i_max_len,
 93			      int *str_i_idx,
 94			      int u_ch, int *needsCRC,
 95			      int (*conv_f)(wchar_t, unsigned char *, int),
 96			      int translate)
 97{
 98	unicode_t c;
 99	int illChar = 0;
100	int len, gotch = 0;
101
102	while (!gotch && *str_i_idx < str_i_max_len) {
103		if (*str_o_idx >= str_o_max_len) {
104			*needsCRC = 1;
105			return gotch;
106		}
107
108		len = get_utf16_char(str_i, str_i_max_len, *str_i_idx, u_ch,
109				     &c);
110		/* These chars cannot be converted. Replace them. */
111		if (c == 0 || c > UNICODE_MAX || (conv_f && c > MAX_WCHAR_T) ||
112		    (translate && c == '/')) {
113			illChar = 1;
114			if (!translate)
115				gotch = 1;
116		} else if (illChar)
117			break;
118		else
119			gotch = 1;
120		*str_i_idx += len;
121	}
122	if (illChar) {
123		*needsCRC = 1;
124		c = ILLEGAL_CHAR_MARK;
125		gotch = 1;
126	}
127	if (gotch) {
128		if (conv_f) {
129			len = conv_f(c, &str_o[*str_o_idx],
130				     str_o_max_len - *str_o_idx);
131		} else {
132			len = utf32_to_utf8(c, &str_o[*str_o_idx],
133					    str_o_max_len - *str_o_idx);
134			if (len < 0)
135				len = -ENAMETOOLONG;
136		}
137		/* Valid character? */
138		if (len >= 0)
139			*str_o_idx += len;
140		else if (len == -ENAMETOOLONG) {
141			*needsCRC = 1;
142			gotch = 0;
143		} else {
144			str_o[(*str_o_idx)++] = ILLEGAL_CHAR_MARK;
145			*needsCRC = 1;
146		}
147	}
148	return gotch;
149}
150
151static int udf_name_from_CS0(struct super_block *sb,
152			     uint8_t *str_o, int str_max_len,
153			     const uint8_t *ocu, int ocu_len,
154			     int translate)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155{
156	uint32_t c;
157	uint8_t cmp_id;
158	int idx, len;
159	int u_ch;
160	int needsCRC = 0;
161	int ext_i_len, ext_max_len;
162	int str_o_len = 0;	/* Length of resulting output */
163	int ext_o_len = 0;	/* Extension output length */
164	int ext_crc_len = 0;	/* Extension output length if used with CRC */
165	int i_ext = -1;		/* Extension position in input buffer */
166	int o_crc = 0;		/* Rightmost possible output pos for CRC+ext */
167	unsigned short valueCRC;
168	uint8_t ext[EXT_SIZE * NLS_MAX_CHARSET_SIZE + 1];
169	uint8_t crc[CRC_LEN];
170	int (*conv_f)(wchar_t, unsigned char *, int);
171
172	if (str_max_len <= 0)
 
 
173		return 0;
 
174
175	if (ocu_len == 0) {
176		memset(str_o, 0, str_max_len);
 
 
 
177		return 0;
178	}
179
180	if (UDF_SB(sb)->s_nls_map)
181		conv_f = UDF_SB(sb)->s_nls_map->uni2char;
182	else
183		conv_f = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
185	cmp_id = ocu[0];
186	if (cmp_id != 8 && cmp_id != 16) {
187		memset(str_o, 0, str_max_len);
188		pr_err("unknown compression code (%u)\n", cmp_id);
189		return -EINVAL;
190	}
191	u_ch = cmp_id >> 3;
192
193	ocu++;
194	ocu_len--;
195
196	if (ocu_len % u_ch) {
197		pr_err("incorrect filename length (%d)\n", ocu_len + 1);
198		return -EINVAL;
199	}
200
201	if (translate) {
202		/* Look for extension */
203		for (idx = ocu_len - u_ch, ext_i_len = 0;
204		     (idx >= 0) && (ext_i_len < EXT_SIZE);
205		     idx -= u_ch, ext_i_len++) {
206			c = ocu[idx];
207			if (u_ch > 1)
208				c = (c << 8) | ocu[idx + 1];
209
210			if (c == EXT_MARK) {
211				if (ext_i_len)
212					i_ext = idx;
213				break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214			}
215		}
216		if (i_ext >= 0) {
217			/* Convert extension */
218			ext_max_len = min_t(int, sizeof(ext), str_max_len);
219			ext[ext_o_len++] = EXT_MARK;
220			idx = i_ext + u_ch;
221			while (udf_name_conv_char(ext, ext_max_len, &ext_o_len,
222						  ocu, ocu_len, &idx,
223						  u_ch, &needsCRC,
224						  conv_f, translate)) {
225				if ((ext_o_len + CRC_LEN) < str_max_len)
226					ext_crc_len = ext_o_len;
227			}
 
228		}
229	}
230
231	idx = 0;
232	while (1) {
233		if (translate && (idx == i_ext)) {
234			if (str_o_len > (str_max_len - ext_o_len))
235				needsCRC = 1;
236			break;
237		}
238
239		if (!udf_name_conv_char(str_o, str_max_len, &str_o_len,
240					ocu, ocu_len, &idx,
241					u_ch, &needsCRC, conv_f, translate))
242			break;
243
244		if (translate &&
245		    (str_o_len <= (str_max_len - ext_o_len - CRC_LEN)))
246			o_crc = str_o_len;
247	}
248
249	if (translate) {
250		if (str_o_len <= 2 && str_o[0] == '.' &&
251		    (str_o_len == 1 || str_o[1] == '.'))
252			needsCRC = 1;
253		if (needsCRC) {
254			str_o_len = o_crc;
255			valueCRC = crc_itu_t(0, ocu, ocu_len);
256			crc[0] = CRC_MARK;
257			crc[1] = hex_asc_upper_hi(valueCRC >> 8);
258			crc[2] = hex_asc_upper_lo(valueCRC >> 8);
259			crc[3] = hex_asc_upper_hi(valueCRC);
260			crc[4] = hex_asc_upper_lo(valueCRC);
261			len = min_t(int, CRC_LEN, str_max_len - str_o_len);
262			memcpy(&str_o[str_o_len], crc, len);
263			str_o_len += len;
264			ext_o_len = ext_crc_len;
265		}
266		if (ext_o_len > 0) {
267			memcpy(&str_o[str_o_len], ext, ext_o_len);
268			str_o_len += ext_o_len;
269		}
270	}
271
272	return str_o_len;
 
 
273}
274
275static int udf_name_to_CS0(struct super_block *sb,
276			   uint8_t *ocu, int ocu_max_len,
277			   const uint8_t *str_i, int str_len)
278{
 
 
279	int i, len;
280	unsigned int max_val;
281	int u_len, u_ch;
282	unicode_t uni_char;
283	int (*conv_f)(const unsigned char *, int, wchar_t *);
284
285	if (ocu_max_len <= 0)
 
 
 
286		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
288	if (UDF_SB(sb)->s_nls_map)
289		conv_f = UDF_SB(sb)->s_nls_map->char2uni;
290	else
291		conv_f = NULL;
 
 
 
 
 
 
292
293	memset(ocu, 0, ocu_max_len);
294	ocu[0] = 8;
295	max_val = 0xff;
296	u_ch = 1;
297
298try_again:
299	u_len = 1;
300	for (i = 0; i < str_len; i += len) {
301		/* Name didn't fit? */
302		if (u_len + u_ch > ocu_max_len)
303			return 0;
304		if (conv_f) {
305			wchar_t wchar;
306
307			len = conv_f(&str_i[i], str_len - i, &wchar);
308			if (len > 0)
309				uni_char = wchar;
310		} else {
311			len = utf8_to_utf32(&str_i[i], str_len - i,
312					    &uni_char);
313		}
314		/* Invalid character, deal with it */
315		if (len <= 0 || uni_char > UNICODE_MAX) {
316			len = 1;
317			uni_char = '?';
318		}
319
320		if (uni_char > max_val) {
321			unicode_t c;
322
323			if (max_val == 0xff) {
324				max_val = 0xffff;
325				ocu[0] = 0x10;
326				u_ch = 2;
327				goto try_again;
328			}
329			/*
330			 * Use UTF-16 encoding for chars outside we
331			 * cannot encode directly.
332			 */
333			if (u_len + 2 * u_ch > ocu_max_len)
334				return 0;
335
336			uni_char -= PLANE_SIZE;
337			c = SURROGATE_PAIR |
338			    ((uni_char >> SURROGATE_CHAR_BITS) &
339			     SURROGATE_CHAR_MASK);
340			ocu[u_len++] = (uint8_t)(c >> 8);
341			ocu[u_len++] = (uint8_t)(c & 0xff);
342			uni_char = SURROGATE_PAIR | SURROGATE_LOW |
343					(uni_char & SURROGATE_CHAR_MASK);
344		}
345
346		if (max_val == 0xffff)
347			ocu[u_len++] = (uint8_t)(uni_char >> 8);
348		ocu[u_len++] = (uint8_t)(uni_char & 0xff);
 
349	}
350
351	return u_len;
 
352}
353
354/*
355 * Convert CS0 dstring to output charset. Warning: This function may truncate
356 * input string if it is too long as it is used for informational strings only
357 * and it is better to truncate the string than to refuse mounting a media.
358 */
359int udf_dstrCS0toChar(struct super_block *sb, uint8_t *utf_o, int o_len,
360		      const uint8_t *ocu_i, int i_len)
361{
362	int s_len = 0;
 
363
364	if (i_len > 0) {
365		s_len = ocu_i[i_len - 1];
366		if (s_len >= i_len) {
367			pr_warn("incorrect dstring lengths (%d/%d),"
368				" truncating\n", s_len, i_len);
369			s_len = i_len - 1;
370			/* 2-byte encoding? Need to round properly... */
371			if (ocu_i[0] == 16)
372				s_len -= (s_len - 1) & 2;
373		}
374	}
375
376	return udf_name_from_CS0(sb, utf_o, o_len, ocu_i, s_len, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377}
378
379int udf_get_filename(struct super_block *sb, const uint8_t *sname, int slen,
380		     uint8_t *dname, int dlen)
381{
382	int ret;
 
383
384	if (!slen)
385		return -EIO;
386
387	if (dlen <= 0)
 
 
 
 
 
 
 
 
 
388		return 0;
389
390	ret = udf_name_from_CS0(sb, dname, dlen, sname, slen, 1);
391	/* Zero length filename isn't valid... */
392	if (ret == 0)
393		ret = -EINVAL;
394	return ret;
395}
396
397int udf_put_filename(struct super_block *sb, const uint8_t *sname, int slen,
398		     uint8_t *dname, int dlen)
 
 
 
 
 
 
399{
400	return udf_name_to_CS0(sb, dname, dlen, sname, slen);
401}
 
 
 
402
v3.15
  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 *
 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>
 26#include <linux/crc-itu-t.h>
 27#include <linux/slab.h>
 28
 29#include "udf_sb.h"
 30
 31static int udf_translate_to_linux(uint8_t *, uint8_t *, int, uint8_t *, int);
 
 
 
 
 
 
 32
 33static int udf_char_to_ustr(struct ustr *dest, const uint8_t *src, int strlen)
 
 
 
 
 
 
 
 
 34{
 35	if ((!dest) || (!src) || (!strlen) || (strlen > UDF_NAME_LEN - 2))
 36		return 0;
 37
 38	memset(dest, 0, sizeof(struct ustr));
 39	memcpy(dest->u_name, src, strlen);
 40	dest->u_cmpID = 0x08;
 41	dest->u_len = strlen;
 
 
 
 
 
 
 
 
 42
 43	return strlen;
 44}
 
 
 
 45
 46/*
 47 * udf_build_ustr
 48 */
 49int udf_build_ustr(struct ustr *dest, dstring *ptr, int size)
 50{
 51	int usesize;
 
 
 52
 53	if (!dest || !ptr || !size)
 54		return -1;
 55	BUG_ON(size < 2);
 56
 57	usesize = min_t(size_t, ptr[size - 1], sizeof(dest->u_name));
 58	usesize = min(usesize, size - 2);
 59	dest->u_cmpID = ptr[0];
 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);
 63
 64	return 0;
 65}
 66
 67/*
 68 * udf_build_ustr_exact
 69 */
 70static int udf_build_ustr_exact(struct ustr *dest, dstring *ptr, int exactsize)
 
 
 
 71{
 72	if ((!dest) || (!ptr) || (!exactsize))
 73		return -1;
 
 
 
 
 
 
 
 74
 75	memset(dest, 0, sizeof(struct ustr));
 76	dest->u_cmpID = ptr[0];
 77	dest->u_len = exactsize - 1;
 78	memcpy(dest->u_name, ptr + 1, exactsize - 1);
 79
 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 *
 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 */
102int udf_CS0toUTF8(struct ustr *utf_o, const struct ustr *ocu_i)
103{
104	const uint8_t *ocu;
105	uint8_t cmp_id, ocu_len;
106	int i;
 
 
 
 
 
 
 
 
 
 
 
 
107
108	ocu_len = ocu_i->u_len;
109	if (ocu_len == 0) {
110		memset(utf_o, 0, sizeof(struct ustr));
111		return 0;
112	}
113
114	cmp_id = ocu_i->u_cmpID;
115	if (cmp_id != 8 && cmp_id != 16) {
116		memset(utf_o, 0, sizeof(struct ustr));
117		pr_err("unknown compression code (%d) stri=%s\n",
118		       cmp_id, ocu_i->u_name);
119		return 0;
120	}
121
122	ocu = ocu_i->u_name;
123	utf_o->u_len = 0;
124	for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
125
126		/* Expand OSTA compressed Unicode to Unicode */
127		uint32_t c = ocu[i++];
128		if (cmp_id == 16)
129			c = (c << 8) | ocu[i++];
130
131		/* Compress Unicode to UTF-8 */
132		if (c < 0x80U)
133			utf_o->u_name[utf_o->u_len++] = (uint8_t)c;
134		else if (c < 0x800U) {
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));
139		} else {
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));
147		}
148	}
149	utf_o->u_cmpID = 8;
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 */
177static int udf_UTF8toCS0(dstring *ocu, struct ustr *utf, int length)
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
186try_again:
187	u_len = 0U;
188	utf_char = 0U;
189	utf_cnt = 0U;
190	for (i = 0U; i < utf->u_len; i++) {
191		c = (uint8_t)utf->u_name[i];
192
193		/* Complete a multi-byte UTF-8 character */
194		if (utf_cnt) {
195			utf_char = (utf_char << 6) | (c & 0x3fU);
196			if (--utf_cnt)
197				continue;
198		} else {
199			/* Check for a multi-byte UTF-8 character */
200			if (c & 0x80U) {
201				/* Start a multi-byte UTF-8 character */
202				if ((c & 0xe0U) == 0xc0U) {
203					utf_char = c & 0x1fU;
204					utf_cnt = 1;
205				} else if ((c & 0xf0U) == 0xe0U) {
206					utf_char = c & 0x0fU;
207					utf_cnt = 2;
208				} else if ((c & 0xf8U) == 0xf0U) {
209					utf_char = c & 0x07U;
210					utf_cnt = 3;
211				} else if ((c & 0xfcU) == 0xf8U) {
212					utf_char = c & 0x03U;
213					utf_cnt = 4;
214				} else if ((c & 0xfeU) == 0xfcU) {
215					utf_char = c & 0x01U;
216					utf_cnt = 5;
217				} else {
218					goto error_out;
219				}
220				continue;
221			} else {
222				/* Single byte UTF-8 character (most common) */
223				utf_char = c;
224			}
225		}
226
227		/* Choose no compression if necessary */
228		if (utf_char > max_val) {
229			if (max_val == 0xffU) {
230				max_val = 0xffffU;
231				ocu[0] = (uint8_t)0x10U;
232				goto try_again;
 
 
 
 
233			}
234			goto error_out;
235		}
 
236
237		if (max_val == 0xffffU)
238			ocu[++u_len] = (uint8_t)(utf_char >> 8);
239		ocu[++u_len] = (uint8_t)(utf_char & 0xffU);
240	}
 
 
 
241
242	if (utf_cnt) {
243error_out:
244		ocu[++u_len] = '?';
245		printk(KERN_DEBUG pr_fmt("bad UTF-8 character\n"));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246	}
247
248	ocu[length - 1] = (uint8_t)u_len + 1;
249
250	return u_len + 1;
251}
252
253static int udf_CS0toNLS(struct nls_table *nls, struct ustr *utf_o,
254			const struct ustr *ocu_i)
 
255{
256	const uint8_t *ocu;
257	uint8_t cmp_id, ocu_len;
258	int i, len;
 
 
 
 
259
260
261	ocu_len = ocu_i->u_len;
262	if (ocu_len == 0) {
263		memset(utf_o, 0, sizeof(struct ustr));
264		return 0;
265	}
266
267	cmp_id = ocu_i->u_cmpID;
268	if (cmp_id != 8 && cmp_id != 16) {
269		memset(utf_o, 0, sizeof(struct ustr));
270		pr_err("unknown compression code (%d) stri=%s\n",
271		       cmp_id, ocu_i->u_name);
272		return 0;
273	}
274
275	ocu = ocu_i->u_name;
276	utf_o->u_len = 0;
277	for (i = 0; (i < ocu_len) && (utf_o->u_len <= (UDF_NAME_LEN - 3));) {
278		/* Expand OSTA compressed Unicode to Unicode */
279		uint32_t c = ocu[i++];
280		if (cmp_id == 16)
281			c = (c << 8) | ocu[i++];
282
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++] = '?';
290	}
291	utf_o->u_cmpID = 8;
292
293	return utf_o->u_len;
294}
295
296static int udf_NLStoCS0(struct nls_table *nls, dstring *ocu, struct ustr *uni,
297			int length)
298{
299	int len;
300	unsigned i, max_val;
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
308try_again:
309	u_len = 0U;
310	for (i = 0U; i < uni->u_len; i++) {
311		len = nls->char2uni(&uni->u_name[i], uni->u_len - i, &uni_char);
312		if (!len)
313			continue;
 
 
 
 
 
 
 
 
 
 
314		/* Invalid character, deal with it */
315		if (len < 0) {
316			len = 1;
317			uni_char = '?';
318		}
319
320		if (uni_char > max_val) {
321			max_val = 0xffffU;
322			ocu[0] = (uint8_t)0x10U;
323			goto try_again;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
324		}
325
326		if (max_val == 0xffffU)
327			ocu[++u_len] = (uint8_t)(uni_char >> 8);
328		ocu[++u_len] = (uint8_t)(uni_char & 0xffU);
329		i += len - 1;
330	}
331
332	ocu[length - 1] = (uint8_t)u_len + 1;
333	return u_len + 1;
334}
335
336int udf_get_filename(struct super_block *sb, uint8_t *sname, uint8_t *dname,
337		     int flen)
 
 
 
 
 
338{
339	struct ustr *filename, *unifilename;
340	int len = 0;
341
342	filename = kmalloc(sizeof(struct ustr), GFP_NOFS);
343	if (!filename)
344		return 0;
 
 
 
 
 
 
 
 
345
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
353	if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
354		if (!udf_CS0toUTF8(filename, unifilename)) {
355			udf_debug("Failed in udf_get_filename: sname = %s\n",
356				  sname);
357			goto out2;
358		}
359	} else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
360		if (!udf_CS0toNLS(UDF_SB(sb)->s_nls_map, filename,
361				  unifilename)) {
362			udf_debug("Failed in udf_get_filename: sname = %s\n",
363				  sname);
364			goto out2;
365		}
366	} else
367		goto out2;
368
369	len = udf_translate_to_linux(dname, filename->u_name, filename->u_len,
370				     unifilename->u_name, unifilename->u_len);
371out2:
372	kfree(unifilename);
373out1:
374	kfree(filename);
375	return len;
376}
377
378int udf_put_filename(struct super_block *sb, const uint8_t *sname,
379		     uint8_t *dname, int flen)
380{
381	struct ustr unifilename;
382	int namelen;
383
384	if (!udf_char_to_ustr(&unifilename, sname, flen))
385		return 0;
386
387	if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
388		namelen = udf_UTF8toCS0(dname, &unifilename, UDF_NAME_LEN);
389		if (!namelen)
390			return 0;
391	} else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
392		namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, dname,
393					&unifilename, UDF_NAME_LEN);
394		if (!namelen)
395			return 0;
396	} else
397		return 0;
398
399	return namelen;
 
 
 
 
400}
401
402#define ILLEGAL_CHAR_MARK	'_'
403#define EXT_MARK		'.'
404#define CRC_MARK		'#'
405#define EXT_SIZE 		5
406
407static int udf_translate_to_linux(uint8_t *newName, uint8_t *udfName,
408				  int udfLen, uint8_t *fidName,
409				  int fidNameLen)
410{
411	int index, newIndex = 0, needsCRC = 0;
412	int extIndex = 0, newExtIndex = 0, hasExt = 0;
413	unsigned short valueCRC;
414	uint8_t curr;
415	const uint8_t hexChar[] = "0123456789ABCDEF";
416
417	if (udfName[0] == '.' &&
418	    (udfLen == 1 || (udfLen == 2 && udfName[1] == '.'))) {
419		needsCRC = 1;
420		newIndex = udfLen;
421		memcpy(newName, udfName, udfLen);
422	} else {
423		for (index = 0; index < udfLen; index++) {
424			curr = udfName[index];
425			if (curr == '/' || curr == 0) {
426				needsCRC = 1;
427				curr = ILLEGAL_CHAR_MARK;
428				while (index + 1 < udfLen &&
429						(udfName[index + 1] == '/' ||
430						 udfName[index + 1] == 0))
431					index++;
432			}
433			if (curr == EXT_MARK &&
434					(udfLen - index - 1) <= EXT_SIZE) {
435				if (udfLen == index + 1)
436					hasExt = 0;
437				else {
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	}
449	if (needsCRC) {
450		uint8_t ext[EXT_SIZE];
451		int localExtIndex = 0;
452
453		if (hasExt) {
454			int maxFilenameLen;
455			for (index = 0;
456			     index < EXT_SIZE && extIndex + index + 1 < udfLen;
457			     index++) {
458				curr = udfName[extIndex + index + 1];
459
460				if (curr == '/' || curr == 0) {
461					needsCRC = 1;
462					curr = ILLEGAL_CHAR_MARK;
463					while (extIndex + index + 2 < udfLen &&
464					      (index + 1 < EXT_SIZE &&
465						(udfName[extIndex + index + 2] == '/' ||
466						 udfName[extIndex + index + 2] == 0)))
467						index++;
468				}
469				ext[localExtIndex++] = curr;
470			}
471			maxFilenameLen = 250 - localExtIndex;
472			if (newIndex > maxFilenameLen)
473				newIndex = maxFilenameLen;
474			else
475				newIndex = newExtIndex;
476		} else if (newIndex > 250)
477			newIndex = 250;
478		newName[newIndex++] = CRC_MARK;
479		valueCRC = crc_itu_t(0, fidName, fidNameLen);
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
485		if (hasExt) {
486			newName[newIndex++] = EXT_MARK;
487			for (index = 0; index < localExtIndex; index++)
488				newName[newIndex++] = ext[index];
489		}
490	}
491
492	return newIndex;
493}