2 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
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 the
12 * GNU General Public License for more details.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "exfat_config.h"
20 #include "exfat_global.h"
21 #include "exfat_data.h"
23 #include "exfat_nls.h"
24 #include "exfat_api.h"
25 #include "exfat_super.h"
28 #include <linux/nls.h>
30 static UINT16 bad_dos_chars[] = {
31 0x002B, 0x002C, 0x003B, 0x003D, 0x005B, 0x005D,
32 0xFF0B, 0xFF0C, 0xFF1B, 0xFF1D, 0xFF3B, 0xFF3D,
36 static UINT16 bad_uni_chars[] = {
37 0x0022, 0x002A, 0x002F, 0x003A,
38 0x003C, 0x003E, 0x003F, 0x005C, 0x007C,
42 static INT32 convert_uni_to_ch(struct nls_table *nls, UINT8 *ch, UINT16 uni, INT32 *lossy);
43 static INT32 convert_ch_to_uni(struct nls_table *nls, UINT16 *uni, UINT8 *ch, INT32 *lossy);
45 UINT16 nls_upper(struct super_block *sb, UINT16 a)
47 FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
49 if (EXFAT_SB(sb)->options.casesensitive)
51 if ((p_fs->vol_utbl)[get_col_index(a)] != NULL)
52 return (p_fs->vol_utbl)[get_col_index(a)][get_row_index(a)];
57 INT32 nls_dosname_cmp(struct super_block *sb, UINT8 *a, UINT8 *b)
59 return(STRNCMP((void *) a, (void *) b, DOS_NAME_LENGTH));
62 INT32 nls_uniname_cmp(struct super_block *sb, UINT16 *a, UINT16 *b)
66 for (i = 0; i < MAX_NAME_LENGTH; i++, a++, b++) {
67 if (nls_upper(sb, *a) != nls_upper(sb, *b)) return(1);
68 if (*a == 0x0) return(0);
73 void nls_uniname_to_dosname(struct super_block *sb, DOS_NAME_T *p_dosname, UNI_NAME_T *p_uniname, INT32 *p_lossy)
75 INT32 i, j, len, lossy = FALSE;
76 UINT8 buf[MAX_CHARSET_SIZE];
77 UINT8 lower = 0, upper = 0;
78 UINT8 *dosname = p_dosname->name;
79 UINT16 *uniname = p_uniname->name;
80 UINT16 *p, *last_period;
81 struct nls_table *nls = EXFAT_SB(sb)->nls_disk;
83 for (i = 0; i < DOS_NAME_LENGTH; i++) {
87 if (!nls_uniname_cmp(sb, uniname, (UINT16 *) UNI_CUR_DIR_NAME)) {
89 p_dosname->name_case = 0x0;
90 if (p_lossy != NULL) *p_lossy = FALSE;
94 if (!nls_uniname_cmp(sb, uniname, (UINT16 *) UNI_PAR_DIR_NAME)) {
97 p_dosname->name_case = 0x0;
98 if (p_lossy != NULL) *p_lossy = FALSE;
103 for (p = uniname; *p; p++) {
104 if (*p == (UINT16) '.') last_period = p;
108 while (i < DOS_NAME_LENGTH) {
110 if (last_period == NULL) break;
112 if (uniname <= last_period) {
113 if (uniname < last_period) lossy = TRUE;
114 uniname = last_period + 1;
118 if (*uniname == (UINT16) '\0') {
120 } else if (*uniname == (UINT16) ' ') {
122 } else if (*uniname == (UINT16) '.') {
123 if (uniname < last_period) lossy = TRUE;
125 } else if (WSTRCHR(bad_dos_chars, *uniname)) {
130 len = convert_uni_to_ch(nls, buf, *uniname, &lossy);
133 if ((i >= 8) && ((i+len) > DOS_NAME_LENGTH)) {
136 if ((i < 8) && ((i+len) > 8)) {
143 for (j = 0; j < len; j++, i++) {
144 *(dosname+i) = *(buf+j);
147 if ((*buf >= 'a') && (*buf <= 'z')) {
148 *(dosname+i) = *buf - ('a' - 'A');
150 if (i < 8) lower |= 0x08;
152 } else if ((*buf >= 'A') && (*buf <= 'Z')) {
155 if (i < 8) upper |= 0x08;
167 if (*dosname == 0xE5) *dosname = 0x05;
168 if (*uniname != 0x0) lossy = TRUE;
170 if (upper & lower) p_dosname->name_case = 0xFF;
171 else p_dosname->name_case = lower;
173 if (p_lossy != NULL) *p_lossy = lossy;
176 void nls_dosname_to_uniname(struct super_block *sb, UNI_NAME_T *p_uniname, DOS_NAME_T *p_dosname)
178 INT32 i = 0, j, n = 0;
179 UINT8 buf[DOS_NAME_LENGTH+2];
180 UINT8 *dosname = p_dosname->name;
181 UINT16 *uniname = p_uniname->name;
182 struct nls_table *nls = EXFAT_SB(sb)->nls_disk;
184 if (*dosname == 0x05) {
190 for ( ; i < 8; i++, n++) {
191 if (*(dosname+i) == ' ') break;
193 if ((*(dosname+i) >= 'A') && (*(dosname+i) <= 'Z') && (p_dosname->name_case & 0x08))
194 *(buf+n) = *(dosname+i) + ('a' - 'A');
196 *(buf+n) = *(dosname+i);
198 if (*(dosname+8) != ' ') {
203 for (i = 8; i < DOS_NAME_LENGTH; i++, n++) {
204 if (*(dosname+i) == ' ') break;
206 if ((*(dosname+i) >= 'A') && (*(dosname+i) <= 'Z') && (p_dosname->name_case & 0x10))
207 *(buf+n) = *(dosname+i) + ('a' - 'A');
209 *(buf+n) = *(dosname+i);
214 while (j < (MAX_NAME_LENGTH-1)) {
215 if (*(buf+i) == '\0') break;
217 i += convert_ch_to_uni(nls, uniname, (buf+i), NULL);
223 *uniname = (UINT16) '\0';
226 void nls_uniname_to_cstring(struct super_block *sb, UINT8 *p_cstring, UNI_NAME_T *p_uniname)
229 UINT8 buf[MAX_CHARSET_SIZE];
230 UINT16 *uniname = p_uniname->name;
231 struct nls_table *nls = EXFAT_SB(sb)->nls_io;
234 while (i < (MAX_NAME_LENGTH-1)) {
235 if (*uniname == (UINT16) '\0') break;
237 len = convert_uni_to_ch(nls, buf, *uniname, NULL);
240 for (j = 0; j < len; j++)
241 *p_cstring++ = (INT8) *(buf+j);
243 *p_cstring++ = (INT8) *buf;
253 void nls_cstring_to_uniname(struct super_block *sb, UNI_NAME_T *p_uniname, UINT8 *p_cstring, INT32 *p_lossy)
255 INT32 i, j, lossy = FALSE;
257 UINT16 upname[MAX_NAME_LENGTH];
258 UINT16 *uniname = p_uniname->name;
259 struct nls_table *nls = EXFAT_SB(sb)->nls_io;
261 end_of_name = p_cstring + STRLEN((INT8 *) p_cstring);
263 while (*(--end_of_name) == ' ') {
264 if (end_of_name < p_cstring) break;
266 *(++end_of_name) = '\0';
268 if (STRCMP((INT8 *) p_cstring, ".") && STRCMP((INT8 *) p_cstring, "..")) {
269 while (*(--end_of_name) == '.') {
270 if (end_of_name < p_cstring) break;
272 *(++end_of_name) = '\0';
275 if (*p_cstring == '\0')
279 while (j < (MAX_NAME_LENGTH-1)) {
280 if (*(p_cstring+i) == '\0') break;
282 i += convert_ch_to_uni(nls, uniname, (UINT8 *)(p_cstring+i), &lossy);
284 if ((*uniname < 0x0020) || WSTRCHR(bad_uni_chars, *uniname))
287 *(upname+j) = nls_upper(sb, *uniname);
293 if (*(p_cstring+i) != '\0')
295 *uniname = (UINT16) '\0';
297 p_uniname->name_len = j;
298 p_uniname->name_hash = calc_checksum_2byte((void *) upname, j<<1, 0, CS_DEFAULT);
304 static INT32 convert_ch_to_uni(struct nls_table *nls, UINT16 *uni, UINT8 *ch, INT32 *lossy)
311 *uni = (UINT16) ch[0];
315 if ((len = nls->char2uni(ch, NLS_MAX_CHARSET_SIZE, uni)) < 0) {
316 printk("%s: fail to use nls \n", __func__);
320 if (!strcmp(nls->charset, "utf8")) return(1);
327 static INT32 convert_uni_to_ch(struct nls_table *nls, UINT8 *ch, UINT16 uni, INT32 *lossy)
338 if ((len = nls->uni2char(uni, ch, NLS_MAX_CHARSET_SIZE)) < 0) {
339 printk("%s: fail to use nls \n", __func__);
340 if (lossy != NULL) *lossy = TRUE;