File: /usr/src/linux/fs/hpfs/name.c

1     /*
2      *  linux/fs/hpfs/name.c
3      *
4      *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
5      *
6      *  operations with filenames
7      */
8     
9     #include <linux/string.h>
10     #include "hpfs_fn.h"
11     
12     char *text_postfix[]={
13     ".ASM", ".BAS", ".BAT", ".C", ".CC", ".CFG", ".CMD", ".CON", ".CPP", ".DEF",
14     ".DOC", ".DPR", ".ERX", ".H", ".HPP", ".HTM", ".HTML", ".JAVA", ".LOG", ".PAS",
15     ".RC", ".TEX", ".TXT", ".Y", ""};
16     
17     char *text_prefix[]={
18     "AUTOEXEC.", "CHANGES", "COPYING", "CONFIG.", "CREDITS", "FAQ", "FILE_ID.DIZ",
19     "MAKEFILE", "READ.ME", "README", "TERMCAP", ""};
20     
21     void hpfs_decide_conv(struct inode *inode, unsigned char *name, unsigned len)
22     {
23     	int i;
24     	if (inode->i_hpfs_conv != CONV_AUTO) return;
25     	for (i = 0; *text_postfix[i]; i++) {
26     		int l = strlen(text_postfix[i]);
27     		if (l <= len)
28     			if (!hpfs_compare_names(inode->i_sb, text_postfix[i], l, name + len - l, l, 0))
29     				goto text;
30     	}
31     	for (i = 0; *text_prefix[i]; i++) {
32     		int l = strlen(text_prefix[i]);
33     		if (l <= len)
34     			if (!hpfs_compare_names(inode->i_sb, text_prefix[i], l, name, l, 0))
35     				goto text;
36     	}
37     	inode->i_hpfs_conv = CONV_BINARY;
38     	return;
39     	text:
40     	inode->i_hpfs_conv = CONV_TEXT;
41     	return;
42     }
43     
44     static inline int not_allowed_char(unsigned char c)
45     {
46     	return c<' ' || c=='"' || c=='*' || c=='/' || c==':' || c=='<' ||
47     	      c=='>' || c=='?' || c=='\\' || c=='|';
48     }
49     
50     static inline int no_dos_char(unsigned char c)
51     {	/* Characters that are allowed in HPFS but not in DOS */
52     	return c=='+' || c==',' || c==';' || c=='=' || c=='[' || c==']';
53     }
54     
55     static inline unsigned char upcase(unsigned char *dir, unsigned char a)
56     {
57     	if (a<128 || a==255) return a>='a' && a<='z' ? a - 0x20 : a;
58     	if (!dir) return a;
59     	return dir[a-128];
60     }
61     
62     unsigned char hpfs_upcase(unsigned char *dir, unsigned char a)
63     {
64     	return upcase(dir, a);
65     }
66     
67     static inline unsigned char locase(unsigned char *dir, unsigned char a)
68     {
69     	if (a<128 || a==255) return a>='A' && a<='Z' ? a + 0x20 : a;
70     	if (!dir) return a;
71     	return dir[a];
72     }
73     
74     int hpfs_chk_name(unsigned char *name, unsigned *len)
75     {
76     	int i;
77     	if (*len > 254) return -ENAMETOOLONG;
78     	hpfs_adjust_length(name, len);
79     	if (!*len) return -EINVAL;
80     	for (i = 0; i < *len; i++) if (not_allowed_char(name[i])) return -EINVAL;
81     	if (*len == 1) if (name[0] == '.') return -EINVAL;
82     	if (*len == 2) if (name[0] == '.' && name[1] == '.') return -EINVAL;
83     	return 0;
84     }
85     
86     char *hpfs_translate_name(struct super_block *s, unsigned char *from,
87     			  unsigned len, int lc, int lng)
88     {
89     	char *to;
90     	int i;
91     	if (s->s_hpfs_chk >= 2) if (hpfs_is_name_long(from, len) != lng) {
92     		printk("HPFS: Long name flag mismatch - name ");
93     		for (i=0; i<len; i++) printk("%c", from[i]);
94     		printk(" misidentified as %s.\n", lng ? "short" : "long");
95     		printk("HPFS: It's nothing serious. It could happen because of bug in OS/2.\nHPFS: Set checks=normal to disable this message.\n");
96     	}
97     	if (!lc) return from;
98     	if (!(to = kmalloc(len, GFP_KERNEL))) {
99     		printk("HPFS: can't allocate memory for name conversion buffer\n");
100     		return from;
101     	}
102     	for (i = 0; i < len; i++) to[i] = locase(s->s_hpfs_cp_table,from[i]);
103     	return to;
104     }
105     
106     int hpfs_compare_names(struct super_block *s, unsigned char *n1, unsigned l1,
107     		       unsigned char *n2, unsigned l2, int last)
108     {
109     	unsigned l = l1 < l2 ? l1 : l2;
110     	unsigned i;
111     	if (last) return -1;
112     	for (i = 0; i < l; i++) {
113     		unsigned char c1 = upcase(s->s_hpfs_cp_table,n1[i]);
114     		unsigned char c2 = upcase(s->s_hpfs_cp_table,n2[i]);
115     		if (c1 < c2) return -1;
116     		if (c1 > c2) return 1;
117     	}
118     	if (l1 < l2) return -1;
119     	if (l1 > l2) return 1;
120     	return 0;
121     }
122     
123     int hpfs_is_name_long(unsigned char *name, unsigned len)
124     {
125     	int i,j;
126     	for (i = 0; i < len && name[i] != '.'; i++)
127     		if (no_dos_char(name[i])) return 1;
128     	if (!i || i > 8) return 1;
129     	if (i == len) return 0;
130     	for (j = i + 1; j < len; j++)
131     		if (name[j] == '.' || no_dos_char(name[i])) return 1;
132     	return j - i > 4;
133     }
134     
135     /* OS/2 clears dots and spaces at the end of file name, so we have to */
136     
137     void hpfs_adjust_length(unsigned char *name, unsigned *len)
138     {
139     	if (!*len) return;
140     	if (*len == 1 && name[0] == '.') return;
141     	if (*len == 2 && name[0] == '.' && name[1] == '.') return;
142     	while (*len && (name[*len - 1] == '.' || name[*len - 1] == ' '))
143     		(*len)--;
144     }
145