[MTD] Support for protection register support on Intel FLASH chips
[linux-2.6] / fs / jfs / jfs_unicode.c
1 /*
2  *   Copyright (C) International Business Machines Corp., 2000-2004
3  *
4  *   This program is free software;  you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License as published by
6  *   the Free Software Foundation; either version 2 of the License, or 
7  *   (at your option) any later version.
8  * 
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12  *   the GNU General Public License for more details.
13  *
14  *   You should have received a copy of the GNU General Public License
15  *   along with this program;  if not, write to the Free Software 
16  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18
19 #include <linux/fs.h>
20 #include <linux/slab.h>
21 #include "jfs_incore.h"
22 #include "jfs_filsys.h"
23 #include "jfs_unicode.h"
24 #include "jfs_debug.h"
25
26 /*
27  * NAME:        jfs_strfromUCS()
28  *
29  * FUNCTION:    Convert little-endian unicode string to character string
30  *
31  */
32 int jfs_strfromUCS_le(char *to, const __le16 * from,
33                       int len, struct nls_table *codepage)
34 {
35         int i;
36         int outlen = 0;
37         static int warn_again = 5;      /* Only warn up to 5 times total */
38         int warn = !!warn_again;        /* once per string */
39
40         if (codepage) {
41                 for (i = 0; (i < len) && from[i]; i++) {
42                         int charlen;
43                         charlen =
44                             codepage->uni2char(le16_to_cpu(from[i]),
45                                                &to[outlen],
46                                                NLS_MAX_CHARSET_SIZE);
47                         if (charlen > 0)
48                                 outlen += charlen;
49                         else
50                                 to[outlen++] = '?';
51                 }
52         } else {
53                 for (i = 0; (i < len) && from[i]; i++) {
54                         if (le16_to_cpu(from[i]) & 0xff00) {
55                                 if (warn) {
56                                         warn--;
57                                         warn_again--;
58                                         printk(KERN_ERR
59                         "non-latin1 character 0x%x found in JFS file name\n", 
60                                                le16_to_cpu(from[i]));
61                                         printk(KERN_ERR
62                                 "mount with iocharset=utf8 to access\n");
63                                 }
64                                 to[i] = '?';
65                         }
66                         else
67                                 to[i] = (char) (le16_to_cpu(from[i]));
68                 }
69                 outlen = i;
70         }
71         to[outlen] = 0;
72         return outlen;
73 }
74
75 /*
76  * NAME:        jfs_strtoUCS()
77  *
78  * FUNCTION:    Convert character string to unicode string
79  *
80  */
81 static int jfs_strtoUCS(wchar_t * to, const unsigned char *from, int len,
82                 struct nls_table *codepage)
83 {
84         int charlen;
85         int i;
86
87         if (codepage) {
88                 for (i = 0; len && *from; i++, from += charlen, len -= charlen)
89                 {
90                         charlen = codepage->char2uni(from, len, &to[i]);
91                         if (charlen < 1) {
92                                 jfs_err("jfs_strtoUCS: char2uni returned %d.",
93                                         charlen);
94                                 jfs_err("charset = %s, char = 0x%x",
95                                         codepage->charset, *from);
96                                 return charlen;
97                         }
98                 }
99         } else {
100                 for (i = 0; (i < len) && from[i]; i++)
101                         to[i] = (wchar_t) from[i];
102         }
103
104         to[i] = 0;
105         return i;
106 }
107
108 /*
109  * NAME:        get_UCSname()
110  *
111  * FUNCTION:    Allocate and translate to unicode string
112  *
113  */
114 int get_UCSname(struct component_name * uniName, struct dentry *dentry)
115 {
116         struct nls_table *nls_tab = JFS_SBI(dentry->d_sb)->nls_tab;
117         int length = dentry->d_name.len;
118
119         if (length > JFS_NAME_MAX)
120                 return -ENAMETOOLONG;
121
122         uniName->name =
123             kmalloc((length + 1) * sizeof(wchar_t), GFP_NOFS);
124
125         if (uniName->name == NULL)
126                 return -ENOSPC;
127
128         uniName->namlen = jfs_strtoUCS(uniName->name, dentry->d_name.name,
129                                        length, nls_tab);
130
131         if (uniName->namlen < 0) {
132                 kfree(uniName->name);
133                 return uniName->namlen;
134         }
135
136         return 0;
137 }