1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#ifndef _CIFS_UNICODE_H
34#define _CIFS_UNICODE_H
35
36#include <asm/byteorder.h>
37#include <linux/types.h>
38#include <linux/nls.h>
39
40#define UNIUPR_NOLOWER
41
42
43
44
45
46
47#define UNI_ASTERISK (__u16) ('*' + 0xF000)
48#define UNI_QUESTION (__u16) ('?' + 0xF000)
49#define UNI_COLON (__u16) (':' + 0xF000)
50#define UNI_GRTRTHAN (__u16) ('>' + 0xF000)
51#define UNI_LESSTHAN (__u16) ('<' + 0xF000)
52#define UNI_PIPE (__u16) ('|' + 0xF000)
53#define UNI_SLASH (__u16) ('\\' + 0xF000)
54
55
56
57
58#ifndef UNICASERANGE_DEFINED
59struct UniCaseRange {
60 wchar_t start;
61 wchar_t end;
62 signed char *table;
63};
64#endif
65
66#ifndef UNIUPR_NOUPPER
67extern signed char CifsUniUpperTable[512];
68extern const struct UniCaseRange CifsUniUpperRange[];
69#endif
70
71#ifndef UNIUPR_NOLOWER
72extern signed char CifsUniLowerTable[512];
73extern const struct UniCaseRange CifsUniLowerRange[];
74#endif
75
76#ifdef __KERNEL__
77int cifs_from_utf16(char *to, const __le16 *from, int tolen, int fromlen,
78 const struct nls_table *codepage, bool mapchar);
79int cifs_utf16_bytes(const __le16 *from, int maxbytes,
80 const struct nls_table *codepage);
81int cifs_strtoUTF16(__le16 *, const char *, int, const struct nls_table *);
82char *cifs_strndup_from_utf16(const char *src, const int maxlen,
83 const bool is_unicode,
84 const struct nls_table *codepage);
85extern int cifsConvertToUTF16(__le16 *target, const char *source, int maxlen,
86 const struct nls_table *cp, int mapChars);
87#ifdef CONFIG_CIFS_SMB2
88extern __le16 *cifs_strndup_to_utf16(const char *src, const int maxlen,
89 int *utf16_len, const struct nls_table *cp,
90 int remap);
91#endif
92#endif
93
94
95
96
97
98
99
100static inline wchar_t *
101UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
102{
103 wchar_t *anchor = ucs1;
104
105 while (*ucs1++) ;
106 ucs1--;
107 while ((*ucs1++ = *ucs2++)) ;
108 return anchor;
109}
110
111
112
113
114
115
116
117
118static inline wchar_t *
119UniStrchr(const wchar_t *ucs, wchar_t uc)
120{
121 while ((*ucs != uc) && *ucs)
122 ucs++;
123
124 if (*ucs == uc)
125 return (wchar_t *) ucs;
126 return NULL;
127}
128
129
130
131
132
133
134
135
136
137static inline int
138UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
139{
140 while ((*ucs1 == *ucs2) && *ucs1) {
141 ucs1++;
142 ucs2++;
143 }
144 return (int) *ucs1 - (int) *ucs2;
145}
146
147
148
149
150static inline wchar_t *
151UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
152{
153 wchar_t *anchor = ucs1;
154
155 while ((*ucs1++ = *ucs2++)) ;
156 return anchor;
157}
158
159
160
161
162static inline size_t
163UniStrlen(const wchar_t *ucs1)
164{
165 int i = 0;
166
167 while (*ucs1++)
168 i++;
169 return i;
170}
171
172
173
174
175
176static inline size_t
177UniStrnlen(const wchar_t *ucs1, int maxlen)
178{
179 int i = 0;
180
181 while (*ucs1++) {
182 i++;
183 if (i >= maxlen)
184 break;
185 }
186 return i;
187}
188
189
190
191
192static inline wchar_t *
193UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
194{
195 wchar_t *anchor = ucs1;
196
197 while (*ucs1++) ;
198 ucs1--;
199 while (n-- && (*ucs1 = *ucs2)) {
200 ucs1++;
201 ucs2++;
202 }
203 *ucs1 = 0;
204 return (anchor);
205}
206
207
208
209
210static inline int
211UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
212{
213 if (!n)
214 return 0;
215 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
216 ucs1++;
217 ucs2++;
218 }
219 return (int) *ucs1 - (int) *ucs2;
220}
221
222
223
224
225static inline int
226UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
227{
228 if (!n)
229 return 0;
230 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
231 ucs1++;
232 ucs2++;
233 }
234 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
235}
236
237
238
239
240static inline wchar_t *
241UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
242{
243 wchar_t *anchor = ucs1;
244
245 while (n-- && *ucs2)
246 *ucs1++ = *ucs2++;
247
248 n++;
249 while (n--)
250 *ucs1++ = 0;
251 return anchor;
252}
253
254
255
256
257static inline wchar_t *
258UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
259{
260 wchar_t *anchor = ucs1;
261
262 while (n-- && *ucs2)
263 *ucs1++ = __le16_to_cpu(*ucs2++);
264
265 n++;
266 while (n--)
267 *ucs1++ = 0;
268 return anchor;
269}
270
271
272
273
274
275
276
277
278static inline wchar_t *
279UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
280{
281 const wchar_t *anchor1 = ucs1;
282 const wchar_t *anchor2 = ucs2;
283
284 while (*ucs1) {
285 if (*ucs1 == *ucs2) {
286
287 ucs1++;
288 ucs2++;
289 } else {
290 if (!*ucs2)
291 return (wchar_t *) anchor1;
292 ucs1 = ++anchor1;
293 ucs2 = anchor2;
294 }
295 }
296
297 if (!*ucs2)
298 return (wchar_t *) anchor1;
299 return NULL;
300}
301
302#ifndef UNIUPR_NOUPPER
303
304
305
306static inline wchar_t
307UniToupper(register wchar_t uc)
308{
309 register const struct UniCaseRange *rp;
310
311 if (uc < sizeof(CifsUniUpperTable)) {
312
313 return uc + CifsUniUpperTable[uc];
314 } else {
315 rp = CifsUniUpperRange;
316 while (rp->start) {
317 if (uc < rp->start)
318 return uc;
319 if (uc <= rp->end)
320 return uc + rp->table[uc - rp->start];
321 rp++;
322 }
323 }
324 return uc;
325}
326
327
328
329
330static inline wchar_t *
331UniStrupr(register wchar_t *upin)
332{
333 register wchar_t *up;
334
335 up = upin;
336 while (*up) {
337 *up = UniToupper(*up);
338 up++;
339 }
340 return upin;
341}
342#endif
343
344#ifndef UNIUPR_NOLOWER
345
346
347
348static inline wchar_t
349UniTolower(register wchar_t uc)
350{
351 register const struct UniCaseRange *rp;
352
353 if (uc < sizeof(CifsUniLowerTable)) {
354
355 return uc + CifsUniLowerTable[uc];
356 } else {
357 rp = CifsUniLowerRange;
358 while (rp->start) {
359 if (uc < rp->start)
360 return uc;
361 if (uc <= rp->end)
362 return uc + rp->table[uc - rp->start];
363 rp++;
364 }
365 }
366 return uc;
367}
368
369
370
371
372static inline wchar_t *
373UniStrlwr(register wchar_t *upin)
374{
375 register wchar_t *up;
376
377 up = upin;
378 while (*up) {
379 *up = UniTolower(*up);
380 up++;
381 }
382 return upin;
383}
384
385#endif
386
387#endif
388