Import 2.2.8pre2
[davej-history.git] / fs / isofs / joliet.c
blob3da5bd04c1d4f28cc60246cdde52b6fd57c4c0de
1 /*
2 * linux/fs/isofs/joliet.c
4 * (C) 1996 Gordon Chaffee
6 * Joliet: Microsoft's Unicode extensions to iso9660
7 */
9 #include <linux/string.h>
10 #include <linux/nls.h>
11 #include <linux/malloc.h>
12 #include <linux/iso_fs.h>
15 * Convert Unicode 16 to UTF8 or ASCII.
17 static int
18 uni16_to_x8(unsigned char *ascii, unsigned char *uni, int len,
19 struct nls_table *nls)
21 unsigned char *ip, *op;
22 unsigned char ch, cl;
23 unsigned char *uni_page;
25 ip = uni;
26 op = ascii;
28 while ((*ip || ip[1]) && len) {
29 ch = *ip++;
30 cl = *ip++;
32 uni_page = nls->page_uni2charset[ch];
33 if (uni_page && uni_page[cl]) {
34 *op++ = uni_page[cl];
35 } else {
36 *op++ = '?';
38 len--;
40 *op = 0;
41 return (op - ascii);
44 /* Convert big endian wide character string to utf8 */
45 static int
46 wcsntombs_be(__u8 *s, const __u8 *pwcs, int inlen, int maxlen)
48 const __u8 *ip;
49 __u8 *op;
50 int size;
51 __u16 c;
53 op = s;
54 ip = pwcs;
55 while ((*ip || ip[1]) && (maxlen > 0) && (inlen > 0)) {
56 c = (*ip << 8) | ip[1];
57 if (c > 0x7f) {
58 size = utf8_wctomb(op, c, maxlen);
59 if (size == -1) {
60 /* Ignore character and move on */
61 maxlen--;
62 } else {
63 op += size;
64 maxlen -= size;
66 } else {
67 *op++ = (__u8) c;
69 ip += 2;
70 inlen--;
72 return (op - s);
75 int
76 get_joliet_filename(struct iso_directory_record * de, struct inode * inode,
77 unsigned char *outname)
79 unsigned char utf8;
80 struct nls_table *nls;
81 unsigned char len = 0;
83 utf8 = inode->i_sb->u.isofs_sb.s_utf8;
84 nls = inode->i_sb->u.isofs_sb.s_nls_iocharset;
86 if (utf8) {
87 len = wcsntombs_be(outname, de->name,
88 de->name_len[0] >> 1, PAGE_SIZE);
89 } else {
90 len = uni16_to_x8(outname, de->name,
91 de->name_len[0] >> 1, nls);
93 if ((len > 2) && (outname[len-2] == ';') && (outname[len-1] == '1')) {
94 len -= 2;
98 * Windows doesn't like periods at the end of a name,
99 * so neither do we
101 while (len >= 2 && (outname[len-1] == '.')) {
102 len--;
105 return len;