1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/fs.h>
22#include <linux/slab.h>
23#include "cifs_unicode.h"
24#include "cifs_uniupr.h"
25#include "cifspdu.h"
26#include "cifsglob.h"
27#include "cifs_debug.h"
28
29
30
31
32
33
34
35
36
37
38
39int
40cifs_ucs2_bytes(const __le16 *from, int maxbytes,
41 const struct nls_table *codepage)
42{
43 int i;
44 int charlen, outlen = 0;
45 int maxwords = maxbytes / 2;
46 char tmp[NLS_MAX_CHARSET_SIZE];
47 __u16 ftmp;
48
49 for (i = 0; i < maxwords; i++) {
50 ftmp = get_unaligned_le16(&from[i]);
51 if (ftmp == 0)
52 break;
53
54 charlen = codepage->uni2char(ftmp, tmp, NLS_MAX_CHARSET_SIZE);
55 if (charlen > 0)
56 outlen += charlen;
57 else
58 outlen++;
59 }
60
61 return outlen;
62}
63
64
65
66
67
68
69
70
71
72
73
74
75static int
76cifs_mapchar(char *target, const __u16 src_char, const struct nls_table *cp,
77 bool mapchar)
78{
79 int len = 1;
80
81 if (!mapchar)
82 goto cp_convert;
83
84
85
86
87
88
89 switch (src_char) {
90 case UNI_COLON:
91 *target = ':';
92 break;
93 case UNI_ASTERIK:
94 *target = '*';
95 break;
96 case UNI_QUESTION:
97 *target = '?';
98 break;
99 case UNI_PIPE:
100 *target = '|';
101 break;
102 case UNI_GRTRTHAN:
103 *target = '>';
104 break;
105 case UNI_LESSTHAN:
106 *target = '<';
107 break;
108 default:
109 goto cp_convert;
110 }
111
112out:
113 return len;
114
115cp_convert:
116 len = cp->uni2char(src_char, target, NLS_MAX_CHARSET_SIZE);
117 if (len <= 0) {
118 *target = '?';
119 len = 1;
120 }
121 goto out;
122}
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146int
147cifs_from_ucs2(char *to, const __le16 *from, int tolen, int fromlen,
148 const struct nls_table *codepage, bool mapchar)
149{
150 int i, charlen, safelen;
151 int outlen = 0;
152 int nullsize = nls_nullsize(codepage);
153 int fromwords = fromlen / 2;
154 char tmp[NLS_MAX_CHARSET_SIZE];
155 __u16 ftmp;
156
157
158
159
160
161
162
163 safelen = tolen - (NLS_MAX_CHARSET_SIZE + nullsize);
164
165 for (i = 0; i < fromwords; i++) {
166 ftmp = get_unaligned_le16(&from[i]);
167 if (ftmp == 0)
168 break;
169
170
171
172
173
174 if (outlen >= safelen) {
175 charlen = cifs_mapchar(tmp, ftmp, codepage, mapchar);
176 if ((outlen + charlen) > (tolen - nullsize))
177 break;
178 }
179
180
181 charlen = cifs_mapchar(&to[outlen], ftmp, codepage, mapchar);
182 outlen += charlen;
183 }
184
185
186 for (i = 0; i < nullsize; i++)
187 to[outlen++] = 0;
188
189 return outlen;
190}
191
192
193
194
195
196
197
198int
199cifs_strtoUCS(__le16 *to, const char *from, int len,
200 const struct nls_table *codepage)
201{
202 int charlen;
203 int i;
204 wchar_t wchar_to;
205
206 for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
207 charlen = codepage->char2uni(from, len, &wchar_to);
208 if (charlen < 1) {
209 cERROR(1, "strtoUCS: char2uni of 0x%x returned %d",
210 *from, charlen);
211
212 wchar_to = 0x003f;
213 charlen = 1;
214 }
215 put_unaligned_le16(wchar_to, &to[i]);
216 }
217
218 put_unaligned_le16(0, &to[i]);
219 return i;
220}
221
222
223
224
225
226
227
228
229
230
231
232
233char *
234cifs_strndup_from_ucs(const char *src, const int maxlen, const bool is_unicode,
235 const struct nls_table *codepage)
236{
237 int len;
238 char *dst;
239
240 if (is_unicode) {
241 len = cifs_ucs2_bytes((__le16 *) src, maxlen, codepage);
242 len += nls_nullsize(codepage);
243 dst = kmalloc(len, GFP_KERNEL);
244 if (!dst)
245 return NULL;
246 cifs_from_ucs2(dst, (__le16 *) src, len, maxlen, codepage,
247 false);
248 } else {
249 len = strnlen(src, maxlen);
250 len++;
251 dst = kmalloc(len, GFP_KERNEL);
252 if (!dst)
253 return NULL;
254 strlcpy(dst, src, len);
255 }
256
257 return dst;
258}
259
260
261
262
263
264
265
266int
267cifsConvertToUCS(__le16 *target, const char *source, int maxlen,
268 const struct nls_table *cp, int mapChars)
269{
270 int i, j, charlen;
271 int len_remaining = maxlen;
272 char src_char;
273 __u16 temp;
274
275 if (!mapChars)
276 return cifs_strtoUCS(target, source, PATH_MAX, cp);
277
278 for (i = 0, j = 0; i < maxlen; j++) {
279 src_char = source[i];
280 switch (src_char) {
281 case 0:
282 put_unaligned_le16(0, &target[j]);
283 goto ctoUCS_out;
284 case ':':
285 temp = UNI_COLON;
286 break;
287 case '*':
288 temp = UNI_ASTERIK;
289 break;
290 case '?':
291 temp = UNI_QUESTION;
292 break;
293 case '<':
294 temp = UNI_LESSTHAN;
295 break;
296 case '>':
297 temp = UNI_GRTRTHAN;
298 break;
299 case '|':
300 temp = UNI_PIPE;
301 break;
302
303
304
305
306
307 default:
308 charlen = cp->char2uni(source+i, len_remaining,
309 &temp);
310
311
312
313
314 if (charlen < 1) {
315 temp = 0x003f;
316 charlen = 1;
317 }
318 len_remaining -= charlen;
319
320
321
322
323
324 i += charlen;
325 continue;
326 }
327 put_unaligned_le16(temp, &target[j]);
328 i++;
329 len_remaining--;
330 }
331
332ctoUCS_out:
333 return i;
334}
335
336