Commit d987f62c authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'udf_for_v4.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs

Pull udf updates from Jan Kara:
 "UDF support for UTF-16 characters in file names"

* tag 'udf_for_v4.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs:
  udf: Add support for decoding UTF-16 characters
  udf: Add support for encoding UTF-16 characters
  udf: Push sb argument to udf_name_[to|from]_CS0()
  udf: Convert ident strings to proper charset
  udf: Use UTF-32 <-> UTF-8 conversion functions from NLS
  udf: Always require NLS support
parents 091a0f27 8a0cdef1
config UDF_FS config UDF_FS
tristate "UDF file system support" tristate "UDF file system support"
select CRC_ITU_T select CRC_ITU_T
select NLS
help help
This is a file system used on some CD-ROMs and DVDs. Since the This is a file system used on some CD-ROMs and DVDs. Since the
file system is supported by multiple operating systems and is more file system is supported by multiple operating systems and is more
...@@ -13,8 +14,3 @@ config UDF_FS ...@@ -13,8 +14,3 @@ config UDF_FS
module will be called udf. module will be called udf.
If unsure, say N. If unsure, say N.
config UDF_NLS
bool
default y
depends on (UDF_FS=m && NLS) || (UDF_FS=y && NLS=y)
...@@ -572,7 +572,6 @@ static int udf_parse_options(char *options, struct udf_options *uopt, ...@@ -572,7 +572,6 @@ static int udf_parse_options(char *options, struct udf_options *uopt,
case Opt_utf8: case Opt_utf8:
uopt->flags |= (1 << UDF_FLAG_UTF8); uopt->flags |= (1 << UDF_FLAG_UTF8);
break; break;
#ifdef CONFIG_UDF_NLS
case Opt_iocharset: case Opt_iocharset:
if (!remount) { if (!remount) {
if (uopt->nls_map) if (uopt->nls_map)
...@@ -581,7 +580,6 @@ static int udf_parse_options(char *options, struct udf_options *uopt, ...@@ -581,7 +580,6 @@ static int udf_parse_options(char *options, struct udf_options *uopt,
uopt->flags |= (1 << UDF_FLAG_NLS_MAP); uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
} }
break; break;
#endif
case Opt_uforget: case Opt_uforget:
uopt->flags |= (1 << UDF_FLAG_UID_FORGET); uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
break; break;
...@@ -892,14 +890,14 @@ static int udf_load_pvoldesc(struct super_block *sb, sector_t block) ...@@ -892,14 +890,14 @@ static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
#endif #endif
} }
ret = udf_dstrCS0toUTF8(outstr, 31, pvoldesc->volIdent, 32); ret = udf_dstrCS0toChar(sb, outstr, 31, pvoldesc->volIdent, 32);
if (ret < 0) if (ret < 0)
goto out_bh; goto out_bh;
strncpy(UDF_SB(sb)->s_volume_ident, outstr, ret); strncpy(UDF_SB(sb)->s_volume_ident, outstr, ret);
udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident); udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident);
ret = udf_dstrCS0toUTF8(outstr, 127, pvoldesc->volSetIdent, 128); ret = udf_dstrCS0toChar(sb, outstr, 127, pvoldesc->volSetIdent, 128);
if (ret < 0) if (ret < 0)
goto out_bh; goto out_bh;
...@@ -2117,7 +2115,6 @@ static int udf_fill_super(struct super_block *sb, void *options, int silent) ...@@ -2117,7 +2115,6 @@ static int udf_fill_super(struct super_block *sb, void *options, int silent)
udf_err(sb, "utf8 cannot be combined with iocharset\n"); udf_err(sb, "utf8 cannot be combined with iocharset\n");
goto parse_options_failure; goto parse_options_failure;
} }
#ifdef CONFIG_UDF_NLS
if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) { if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
uopt.nls_map = load_nls_default(); uopt.nls_map = load_nls_default();
if (!uopt.nls_map) if (!uopt.nls_map)
...@@ -2125,7 +2122,6 @@ static int udf_fill_super(struct super_block *sb, void *options, int silent) ...@@ -2125,7 +2122,6 @@ static int udf_fill_super(struct super_block *sb, void *options, int silent)
else else
udf_debug("Using default NLS map\n"); udf_debug("Using default NLS map\n");
} }
#endif
if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP))) if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
uopt.flags |= (1 << UDF_FLAG_UTF8); uopt.flags |= (1 << UDF_FLAG_UTF8);
...@@ -2279,10 +2275,8 @@ static int udf_fill_super(struct super_block *sb, void *options, int silent) ...@@ -2279,10 +2275,8 @@ static int udf_fill_super(struct super_block *sb, void *options, int silent)
error_out: error_out:
iput(sbi->s_vat_inode); iput(sbi->s_vat_inode);
parse_options_failure: parse_options_failure:
#ifdef CONFIG_UDF_NLS
if (uopt.nls_map) if (uopt.nls_map)
unload_nls(uopt.nls_map); unload_nls(uopt.nls_map);
#endif
if (lvid_open) if (lvid_open)
udf_close_lvid(sb); udf_close_lvid(sb);
brelse(sbi->s_lvid_bh); brelse(sbi->s_lvid_bh);
...@@ -2332,10 +2326,8 @@ static void udf_put_super(struct super_block *sb) ...@@ -2332,10 +2326,8 @@ static void udf_put_super(struct super_block *sb)
sbi = UDF_SB(sb); sbi = UDF_SB(sb);
iput(sbi->s_vat_inode); iput(sbi->s_vat_inode);
#ifdef CONFIG_UDF_NLS
if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
unload_nls(sbi->s_nls_map); unload_nls(sbi->s_nls_map);
#endif
if (!sb_rdonly(sb)) if (!sb_rdonly(sb))
udf_close_lvid(sb); udf_close_lvid(sb);
brelse(sbi->s_lvid_bh); brelse(sbi->s_lvid_bh);
......
...@@ -220,7 +220,8 @@ extern int udf_get_filename(struct super_block *, const uint8_t *, int, ...@@ -220,7 +220,8 @@ extern int udf_get_filename(struct super_block *, const uint8_t *, int,
uint8_t *, int); uint8_t *, int);
extern int udf_put_filename(struct super_block *, const uint8_t *, int, extern int udf_put_filename(struct super_block *, const uint8_t *, int,
uint8_t *, int); uint8_t *, int);
extern int udf_dstrCS0toUTF8(uint8_t *, int, const uint8_t *, int); extern int udf_dstrCS0toChar(struct super_block *, uint8_t *, int,
const uint8_t *, int);
/* ialloc.c */ /* ialloc.c */
extern void udf_free_inode(struct inode *); extern void udf_free_inode(struct inode *);
......
...@@ -28,101 +28,64 @@ ...@@ -28,101 +28,64 @@
#include "udf_sb.h" #include "udf_sb.h"
#define PLANE_SIZE 0x10000
#define UNICODE_MAX 0x10ffff
#define SURROGATE_MASK 0xfffff800 #define SURROGATE_MASK 0xfffff800
#define SURROGATE_PAIR 0x0000d800 #define SURROGATE_PAIR 0x0000d800
#define SURROGATE_LOW 0x00000400
#define SURROGATE_CHAR_BITS 10
#define SURROGATE_CHAR_MASK ((1 << SURROGATE_CHAR_BITS) - 1)
static int udf_uni2char_utf8(wchar_t uni, #define ILLEGAL_CHAR_MARK '_'
unsigned char *out, #define EXT_MARK '.'
int boundlen) #define CRC_MARK '#'
{ #define EXT_SIZE 5
int u_len = 0; /* Number of chars we need to store generated CRC to make filename unique */
#define CRC_LEN 5
if (boundlen <= 0)
return -ENAMETOOLONG;
if ((uni & SURROGATE_MASK) == SURROGATE_PAIR) static unicode_t get_utf16_char(const uint8_t *str_i, int str_i_max_len,
return -EINVAL; int str_i_idx, int u_ch, unicode_t *ret)
{
unicode_t c;
int start_idx = str_i_idx;
if (uni < 0x80) { /* Expand OSTA compressed Unicode to Unicode */
out[u_len++] = (unsigned char)uni; c = str_i[str_i_idx++];
} else if (uni < 0x800) { if (u_ch > 1)
if (boundlen < 2) c = (c << 8) | str_i[str_i_idx++];
return -ENAMETOOLONG; if ((c & SURROGATE_MASK) == SURROGATE_PAIR) {
out[u_len++] = (unsigned char)(0xc0 | (uni >> 6)); unicode_t next;
out[u_len++] = (unsigned char)(0x80 | (uni & 0x3f));
} else { /* Trailing surrogate char */
if (boundlen < 3) if (str_i_idx >= str_i_max_len) {
return -ENAMETOOLONG; c = UNICODE_MAX + 1;
out[u_len++] = (unsigned char)(0xe0 | (uni >> 12)); goto out;
out[u_len++] = (unsigned char)(0x80 | ((uni >> 6) & 0x3f));
out[u_len++] = (unsigned char)(0x80 | (uni & 0x3f));
} }
return u_len;
}
static int udf_char2uni_utf8(const unsigned char *in, /* Low surrogate must follow the high one... */
int boundlen, if (c & SURROGATE_LOW) {
wchar_t *uni) c = UNICODE_MAX + 1;
{ goto out;
unsigned int utf_char;
unsigned char c;
int utf_cnt, u_len;
utf_char = 0;
utf_cnt = 0;
for (u_len = 0; u_len < boundlen;) {
c = in[u_len++];
/* Complete a multi-byte UTF-8 character */
if (utf_cnt) {
utf_char = (utf_char << 6) | (c & 0x3f);
if (--utf_cnt)
continue;
} else {
/* Check for a multi-byte UTF-8 character */
if (c & 0x80) {
/* Start a multi-byte UTF-8 character */
if ((c & 0xe0) == 0xc0) {
utf_char = c & 0x1f;
utf_cnt = 1;
} else if ((c & 0xf0) == 0xe0) {
utf_char = c & 0x0f;
utf_cnt = 2;
} else if ((c & 0xf8) == 0xf0) {
utf_char = c & 0x07;
utf_cnt = 3;
} else if ((c & 0xfc) == 0xf8) {
utf_char = c & 0x03;
utf_cnt = 4;
} else if ((c & 0xfe) == 0xfc) {
utf_char = c & 0x01;
utf_cnt = 5;
} else {
utf_cnt = -1;
break;
}
continue;
} else {
/* Single byte UTF-8 character (most common) */
utf_char = c;
}
} }
*uni = utf_char;
break; WARN_ON_ONCE(u_ch != 2);
next = str_i[str_i_idx++] << 8;
next |= str_i[str_i_idx++];
if ((next & SURROGATE_MASK) != SURROGATE_PAIR ||
!(next & SURROGATE_LOW)) {
c = UNICODE_MAX + 1;
goto out;
} }
if (utf_cnt) {
*uni = '?'; c = PLANE_SIZE +
return -EINVAL; ((c & SURROGATE_CHAR_MASK) << SURROGATE_CHAR_BITS) +
(next & SURROGATE_CHAR_MASK);
} }
return u_len; out:
*ret = c;
return str_i_idx - start_idx;
} }
#define ILLEGAL_CHAR_MARK '_'
#define EXT_MARK '.'
#define CRC_MARK '#'
#define EXT_SIZE 5
/* Number of chars we need to store generated CRC to make filename unique */
#define CRC_LEN 5
static int udf_name_conv_char(uint8_t *str_o, int str_o_max_len, static int udf_name_conv_char(uint8_t *str_o, int str_o_max_len,
int *str_o_idx, int *str_o_idx,
...@@ -132,27 +95,29 @@ static int udf_name_conv_char(uint8_t *str_o, int str_o_max_len, ...@@ -132,27 +95,29 @@ static int udf_name_conv_char(uint8_t *str_o, int str_o_max_len,
int (*conv_f)(wchar_t, unsigned char *, int), int (*conv_f)(wchar_t, unsigned char *, int),
int translate) int translate)
{ {
uint32_t c; unicode_t c;
int illChar = 0; int illChar = 0;
int len, gotch = 0; int len, gotch = 0;
for (; (!gotch) && (*str_i_idx < str_i_max_len); *str_i_idx += u_ch) { while (!gotch && *str_i_idx < str_i_max_len) {
if (*str_o_idx >= str_o_max_len) { if (*str_o_idx >= str_o_max_len) {
*needsCRC = 1; *needsCRC = 1;
return gotch; return gotch;
} }
/* Expand OSTA compressed Unicode to Unicode */ len = get_utf16_char(str_i, str_i_max_len, *str_i_idx, u_ch,
c = str_i[*str_i_idx]; &c);
if (u_ch > 1) /* These chars cannot be converted. Replace them. */
c = (c << 8) | str_i[*str_i_idx + 1]; if (c == 0 || c > UNICODE_MAX || (conv_f && c > MAX_WCHAR_T) ||
(translate && c == '/')) {
if (translate && (c == '/' || c == 0))
illChar = 1; illChar = 1;
else if (illChar) if (!translate)
gotch = 1;
} else if (illChar)
break; break;
else else
gotch = 1; gotch = 1;
*str_i_idx += len;
} }
if (illChar) { if (illChar) {
*needsCRC = 1; *needsCRC = 1;
...@@ -160,7 +125,15 @@ static int udf_name_conv_char(uint8_t *str_o, int str_o_max_len, ...@@ -160,7 +125,15 @@ static int udf_name_conv_char(uint8_t *str_o, int str_o_max_len,
gotch = 1; gotch = 1;
} }
if (gotch) { if (gotch) {
len = conv_f(c, &str_o[*str_o_idx], str_o_max_len - *str_o_idx); if (conv_f) {
len = conv_f(c, &str_o[*str_o_idx],
str_o_max_len - *str_o_idx);
} else {
len = utf32_to_utf8(c, &str_o[*str_o_idx],
str_o_max_len - *str_o_idx);
if (len < 0)
len = -ENAMETOOLONG;
}
/* Valid character? */ /* Valid character? */
if (len >= 0) if (len >= 0)
*str_o_idx += len; *str_o_idx += len;
...@@ -168,16 +141,16 @@ static int udf_name_conv_char(uint8_t *str_o, int str_o_max_len, ...@@ -168,16 +141,16 @@ static int udf_name_conv_char(uint8_t *str_o, int str_o_max_len,
*needsCRC = 1; *needsCRC = 1;
gotch = 0; gotch = 0;
} else { } else {
str_o[(*str_o_idx)++] = '?'; str_o[(*str_o_idx)++] = ILLEGAL_CHAR_MARK;
*needsCRC = 1; *needsCRC = 1;
} }
} }
return gotch; return gotch;
} }
static int udf_name_from_CS0(uint8_t *str_o, int str_max_len, static int udf_name_from_CS0(struct super_block *sb,
uint8_t *str_o, int str_max_len,
const uint8_t *ocu, int ocu_len, const uint8_t *ocu, int ocu_len,
int (*conv_f)(wchar_t, unsigned char *, int),
int translate) int translate)
{ {
uint32_t c; uint32_t c;
...@@ -194,6 +167,7 @@ static int udf_name_from_CS0(uint8_t *str_o, int str_max_len, ...@@ -194,6 +167,7 @@ static int udf_name_from_CS0(uint8_t *str_o, int str_max_len,
unsigned short valueCRC; unsigned short valueCRC;
uint8_t ext[EXT_SIZE * NLS_MAX_CHARSET_SIZE + 1]; uint8_t ext[EXT_SIZE * NLS_MAX_CHARSET_SIZE + 1];
uint8_t crc[CRC_LEN]; uint8_t crc[CRC_LEN];
int (*conv_f)(wchar_t, unsigned char *, int);
if (str_max_len <= 0) if (str_max_len <= 0)
return 0; return 0;
...@@ -203,6 +177,11 @@ static int udf_name_from_CS0(uint8_t *str_o, int str_max_len, ...@@ -203,6 +177,11 @@ static int udf_name_from_CS0(uint8_t *str_o, int str_max_len,
return 0; return 0;
} }
if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
conv_f = UDF_SB(sb)->s_nls_map->uni2char;
else
conv_f = NULL;
cmp_id = ocu[0]; cmp_id = ocu[0];
if (cmp_id != 8 && cmp_id != 16) { if (cmp_id != 8 && cmp_id != 16) {
memset(str_o, 0, str_max_len); memset(str_o, 0, str_max_len);
...@@ -293,18 +272,24 @@ static int udf_name_from_CS0(uint8_t *str_o, int str_max_len, ...@@ -293,18 +272,24 @@ static int udf_name_from_CS0(uint8_t *str_o, int str_max_len,
return str_o_len; return str_o_len;
} }
static int udf_name_to_CS0(uint8_t *ocu, int ocu_max_len, static int udf_name_to_CS0(struct super_block *sb,
const uint8_t *str_i, int str_len, uint8_t *ocu, int ocu_max_len,
int (*conv_f)(const unsigned char *, int, wchar_t *)) const uint8_t *str_i, int str_len)
{ {
int i, len; int i, len;
unsigned int max_val; unsigned int max_val;
wchar_t uni_char;
int u_len, u_ch; int u_len, u_ch;
unicode_t uni_char;
int (*conv_f)(const unsigned char *, int, wchar_t *);
if (ocu_max_len <= 0) if (ocu_max_len <= 0)
return 0; return 0;
if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
conv_f = UDF_SB(sb)->s_nls_map->char2uni;
else
conv_f = NULL;
memset(ocu, 0, ocu_max_len); memset(ocu, 0, ocu_max_len);
ocu[0] = 8; ocu[0] = 8;
max_val = 0xff; max_val = 0xff;
...@@ -312,36 +297,61 @@ static int udf_name_to_CS0(uint8_t *ocu, int ocu_max_len, ...@@ -312,36 +297,61 @@ static int udf_name_to_CS0(uint8_t *ocu, int ocu_max_len,
try_again: try_again:
u_len = 1; u_len = 1;
for (i = 0; i < str_len; i++) { for (i = 0; i < str_len; i += len) {
/* Name didn't fit? */ /* Name didn't fit? */
if (u_len + u_ch > ocu_max_len) if (u_len + u_ch > ocu_max_len)
return 0; return 0;
len = conv_f(&str_i[i], str_len - i, &uni_char); if (conv_f) {
if (!len) wchar_t wchar;
continue;
len = conv_f(&str_i[i], str_len - i, &wchar);
if (len > 0)
uni_char = wchar;
} else {
len = utf8_to_utf32(&str_i[i], str_len - i,
&uni_char);
}
/* Invalid character, deal with it */ /* Invalid character, deal with it */
if (len < 0) { if (len <= 0 || uni_char > UNICODE_MAX) {
len = 1; len = 1;
uni_char = '?'; uni_char = '?';
} }
if (uni_char > max_val) { if (uni_char > max_val) {
unicode_t c;
if (max_val == 0xff) {
max_val = 0xffff; max_val = 0xffff;
ocu[0] = 0x10; ocu[0] = 0x10;
u_ch = 2; u_ch = 2;
goto try_again; goto try_again;
} }
/*
* Use UTF-16 encoding for chars outside we
* cannot encode directly.
*/
if (u_len + 2 * u_ch > ocu_max_len)
return 0;
uni_char -= PLANE_SIZE;
c = SURROGATE_PAIR |
((uni_char >> SURROGATE_CHAR_BITS) &
SURROGATE_CHAR_MASK);
ocu[u_len++] = (uint8_t)(c >> 8);
ocu[u_len++] = (uint8_t)(c & 0xff);
uni_char = SURROGATE_PAIR | SURROGATE_LOW |
(uni_char & SURROGATE_CHAR_MASK);
}
if (max_val == 0xffff) if (max_val == 0xffff)
ocu[u_len++] = (uint8_t)(uni_char >> 8); ocu[u_len++] = (uint8_t)(uni_char >> 8);
ocu[u_len++] = (uint8_t)(uni_char & 0xff); ocu[u_len++] = (uint8_t)(uni_char & 0xff);
i += len - 1;
} }
return u_len; return u_len;
} }
int udf_dstrCS0toUTF8(uint8_t *utf_o, int o_len, int udf_dstrCS0toChar(struct super_block *sb, uint8_t *utf_o, int o_len,
const uint8_t *ocu_i, int i_len) const uint8_t *ocu_i, int i_len)
{ {
int s_len = 0; int s_len = 0;
...@@ -355,14 +365,12 @@ int udf_dstrCS0toUTF8(uint8_t *utf_o, int o_len, ...@@ -355,14 +365,12 @@ int udf_dstrCS0toUTF8(uint8_t *utf_o, int o_len,
} }
} }
return udf_name_from_CS0(utf_o, o_len, ocu_i, s_len, return udf_name_from_CS0(sb, utf_o, o_len, ocu_i, s_len, 0);
udf_uni2char_utf8, 0);
} }
int udf_get_filename(struct super_block *sb, const uint8_t *sname, int slen, int udf_get_filename(struct super_block *sb, const uint8_t *sname, int slen,
uint8_t *dname, int dlen) uint8_t *dname, int dlen)
{ {
int (*conv_f)(wchar_t, unsigned char *, int);
int ret; int ret;
if (!slen) if (!slen)
...@@ -371,14 +379,7 @@ int udf_get_filename(struct super_block *sb, const uint8_t *sname, int slen, ...@@ -371,14 +379,7 @@ int udf_get_filename(struct super_block *sb, const uint8_t *sname, int slen,
if (dlen <= 0) if (dlen <= 0)
return 0; return 0;
if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) { ret = udf_name_from_CS0(sb, dname, dlen, sname, slen, 1);
conv_f = udf_uni2char_utf8;
} else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
conv_f = UDF_SB(sb)->s_nls_map->uni2char;
} else
BUG();
ret = udf_name_from_CS0(dname, dlen, sname, slen, conv_f, 1);
/* Zero length filename isn't valid... */ /* Zero length filename isn't valid... */
if (ret == 0) if (ret == 0)
ret = -EINVAL; ret = -EINVAL;
...@@ -388,15 +389,6 @@ int udf_get_filename(struct super_block *sb, const uint8_t *sname, int slen, ...@@ -388,15 +389,6 @@ int udf_get_filename(struct super_block *sb, const uint8_t *sname, int slen,
int udf_put_filename(struct super_block *sb, const uint8_t *sname, int slen, int udf_put_filename(struct super_block *sb, const uint8_t *sname, int slen,
uint8_t *dname, int dlen) uint8_t *dname, int dlen)
{ {
int (*conv_f)(const unsigned char *, int, wchar_t *); return udf_name_to_CS0(sb, dname, dlen, sname, slen);
if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) {
conv_f = udf_char2uni_utf8;
} else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) {
conv_f = UDF_SB(sb)->s_nls_map->char2uni;
} else
BUG();
return udf_name_to_CS0(dname, dlen, sname, slen, conv_f);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment