]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/cifs/cifs_unicode.h
CIFS: Add tree connect/disconnect capability for SMB2
[mirror_ubuntu-jammy-kernel.git] / fs / cifs / cifs_unicode.h
CommitLineData
1da177e4
LT
1/*
2 * cifs_unicode: Unicode kernel case support
3 *
4 * Function:
5 * Convert a unicode character to upper or lower case using
6 * compressed tables.
7 *
d185cda7 8 * Copyright (c) International Business Machines Corp., 2000,2009
1da177e4
LT
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
d38d8c74 12 * the Free Software Foundation; either version 2 of the License, or
1da177e4 13 * (at your option) any later version.
d38d8c74 14 *
1da177e4
LT
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
d38d8c74 21 * along with this program; if not, write to the Free Software
1da177e4
LT
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 *
25 * Notes:
26 * These APIs are based on the C library functions. The semantics
27 * should match the C functions but with expanded size operands.
28 *
29 * The upper/lower functions are based on a table created by mkupr.
30 * This is a compressed table of upper and lower case conversion.
31 *
32 */
bf4f1211
ID
33#ifndef _CIFS_UNICODE_H
34#define _CIFS_UNICODE_H
1da177e4
LT
35
36#include <asm/byteorder.h>
37#include <linux/types.h>
38#include <linux/nls.h>
39
40#define UNIUPR_NOLOWER /* Example to not expand lower case tables */
41
66345f50
JL
42/*
43 * Windows maps these to the user defined 16 bit Unicode range since they are
44 * reserved symbols (along with \ and /), otherwise illegal to store
45 * in filenames in NTFS
46 */
581ade4d 47#define UNI_ASTERISK (__u16) ('*' + 0xF000)
66345f50
JL
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
1da177e4
LT
55/* Just define what we want from uniupr.h. We don't want to define the tables
56 * in each source file.
57 */
58#ifndef UNICASERANGE_DEFINED
59struct UniCaseRange {
60 wchar_t start;
61 wchar_t end;
62 signed char *table;
63};
64#endif /* UNICASERANGE_DEFINED */
65
66#ifndef UNIUPR_NOUPPER
67extern signed char CifsUniUpperTable[512];
68extern const struct UniCaseRange CifsUniUpperRange[];
69#endif /* UNIUPR_NOUPPER */
70
71#ifndef UNIUPR_NOLOWER
bf4f1211
ID
72extern signed char CifsUniLowerTable[512];
73extern const struct UniCaseRange CifsUniLowerRange[];
1da177e4
LT
74#endif /* UNIUPR_NOLOWER */
75
76#ifdef __KERNEL__
acbbb76a
SF
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);
1da177e4
LT
87#endif
88
89/*
90 * UniStrcat: Concatenate the second string to the first
91 *
92 * Returns:
93 * Address of the first string
94 */
95static inline wchar_t *
50c2f753 96UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
1da177e4
LT
97{
98 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
99
100 while (*ucs1++) ; /* To end of first string */
101 ucs1--; /* Return to the null */
102 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
103 return anchor;
104}
105
106/*
107 * UniStrchr: Find a character in a string
108 *
109 * Returns:
110 * Address of first occurrence of character in string
111 * or NULL if the character is not in the string
112 */
113static inline wchar_t *
50c2f753 114UniStrchr(const wchar_t *ucs, wchar_t uc)
1da177e4
LT
115{
116 while ((*ucs != uc) && *ucs)
117 ucs++;
118
119 if (*ucs == uc)
120 return (wchar_t *) ucs;
121 return NULL;
122}
123
124/*
125 * UniStrcmp: Compare two strings
126 *
127 * Returns:
128 * < 0: First string is less than second
129 * = 0: Strings are equal
130 * > 0: First string is greater than second
131 */
132static inline int
50c2f753 133UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
1da177e4
LT
134{
135 while ((*ucs1 == *ucs2) && *ucs1) {
136 ucs1++;
137 ucs2++;
138 }
139 return (int) *ucs1 - (int) *ucs2;
140}
141
142/*
143 * UniStrcpy: Copy a string
144 */
145static inline wchar_t *
50c2f753 146UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
1da177e4
LT
147{
148 wchar_t *anchor = ucs1; /* save the start of result string */
149
150 while ((*ucs1++ = *ucs2++)) ;
151 return anchor;
152}
153
154/*
155 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
156 */
157static inline size_t
50c2f753 158UniStrlen(const wchar_t *ucs1)
1da177e4
LT
159{
160 int i = 0;
161
162 while (*ucs1++)
163 i++;
164 return i;
165}
166
167/*
d38d8c74
SF
168 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
169 * string (length limited)
1da177e4
LT
170 */
171static inline size_t
50c2f753 172UniStrnlen(const wchar_t *ucs1, int maxlen)
1da177e4
LT
173{
174 int i = 0;
175
176 while (*ucs1++) {
177 i++;
178 if (i >= maxlen)
179 break;
180 }
181 return i;
182}
183
184/*
185 * UniStrncat: Concatenate length limited string
186 */
187static inline wchar_t *
50c2f753 188UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
1da177e4
LT
189{
190 wchar_t *anchor = ucs1; /* save pointer to string 1 */
191
192 while (*ucs1++) ;
193 ucs1--; /* point to null terminator of s1 */
194 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
195 ucs1++;
196 ucs2++;
197 }
198 *ucs1 = 0; /* Null terminate the result */
199 return (anchor);
200}
201
202/*
203 * UniStrncmp: Compare length limited string
204 */
205static inline int
50c2f753 206UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
1da177e4
LT
207{
208 if (!n)
209 return 0; /* Null strings are equal */
210 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
211 ucs1++;
212 ucs2++;
213 }
214 return (int) *ucs1 - (int) *ucs2;
215}
216
217/*
218 * UniStrncmp_le: Compare length limited string - native to little-endian
219 */
220static inline int
50c2f753 221UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
1da177e4
LT
222{
223 if (!n)
224 return 0; /* Null strings are equal */
225 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
226 ucs1++;
227 ucs2++;
228 }
229 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
230}
231
232/*
233 * UniStrncpy: Copy length limited string with pad
234 */
235static inline wchar_t *
50c2f753 236UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
1da177e4
LT
237{
238 wchar_t *anchor = ucs1;
239
240 while (n-- && *ucs2) /* Copy the strings */
241 *ucs1++ = *ucs2++;
242
243 n++;
244 while (n--) /* Pad with nulls */
245 *ucs1++ = 0;
246 return anchor;
247}
248
249/*
250 * UniStrncpy_le: Copy length limited string with pad to little-endian
251 */
252static inline wchar_t *
50c2f753 253UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
1da177e4
LT
254{
255 wchar_t *anchor = ucs1;
256
257 while (n-- && *ucs2) /* Copy the strings */
258 *ucs1++ = __le16_to_cpu(*ucs2++);
259
260 n++;
261 while (n--) /* Pad with nulls */
262 *ucs1++ = 0;
263 return anchor;
264}
265
266/*
267 * UniStrstr: Find a string in a string
268 *
269 * Returns:
270 * Address of first match found
271 * NULL if no matching string is found
272 */
273static inline wchar_t *
50c2f753 274UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
1da177e4
LT
275{
276 const wchar_t *anchor1 = ucs1;
277 const wchar_t *anchor2 = ucs2;
278
279 while (*ucs1) {
ad7a2926
SF
280 if (*ucs1 == *ucs2) {
281 /* Partial match found */
1da177e4
LT
282 ucs1++;
283 ucs2++;
284 } else {
285 if (!*ucs2) /* Match found */
286 return (wchar_t *) anchor1;
287 ucs1 = ++anchor1; /* No match */
288 ucs2 = anchor2;
289 }
290 }
291
292 if (!*ucs2) /* Both end together */
293 return (wchar_t *) anchor1; /* Match found */
294 return NULL; /* No match */
295}
296
297#ifndef UNIUPR_NOUPPER
298/*
299 * UniToupper: Convert a unicode character to upper case
300 */
301static inline wchar_t
302UniToupper(register wchar_t uc)
303{
304 register const struct UniCaseRange *rp;
305
ad7a2926
SF
306 if (uc < sizeof(CifsUniUpperTable)) {
307 /* Latin characters */
1da177e4
LT
308 return uc + CifsUniUpperTable[uc]; /* Use base tables */
309 } else {
310 rp = CifsUniUpperRange; /* Use range tables */
311 while (rp->start) {
312 if (uc < rp->start) /* Before start of range */
313 return uc; /* Uppercase = input */
314 if (uc <= rp->end) /* In range */
315 return uc + rp->table[uc - rp->start];
316 rp++; /* Try next range */
317 }
318 }
319 return uc; /* Past last range */
320}
321
322/*
323 * UniStrupr: Upper case a unicode string
324 */
325static inline wchar_t *
50c2f753 326UniStrupr(register wchar_t *upin)
1da177e4
LT
327{
328 register wchar_t *up;
329
330 up = upin;
331 while (*up) { /* For all characters */
332 *up = UniToupper(*up);
333 up++;
334 }
335 return upin; /* Return input pointer */
336}
337#endif /* UNIUPR_NOUPPER */
338
339#ifndef UNIUPR_NOLOWER
340/*
341 * UniTolower: Convert a unicode character to lower case
342 */
343static inline wchar_t
bf4f1211 344UniTolower(register wchar_t uc)
1da177e4 345{
bf4f1211 346 register const struct UniCaseRange *rp;
1da177e4 347
bf4f1211 348 if (uc < sizeof(CifsUniLowerTable)) {
ad7a2926 349 /* Latin characters */
bf4f1211 350 return uc + CifsUniLowerTable[uc]; /* Use base tables */
1da177e4 351 } else {
bf4f1211 352 rp = CifsUniLowerRange; /* Use range tables */
1da177e4
LT
353 while (rp->start) {
354 if (uc < rp->start) /* Before start of range */
355 return uc; /* Uppercase = input */
356 if (uc <= rp->end) /* In range */
357 return uc + rp->table[uc - rp->start];
358 rp++; /* Try next range */
359 }
360 }
361 return uc; /* Past last range */
362}
363
364/*
365 * UniStrlwr: Lower case a unicode string
366 */
367static inline wchar_t *
50c2f753 368UniStrlwr(register wchar_t *upin)
1da177e4
LT
369{
370 register wchar_t *up;
371
372 up = upin;
373 while (*up) { /* For all characters */
374 *up = UniTolower(*up);
375 up++;
376 }
377 return upin; /* Return input pointer */
378}
379
380#endif
bf4f1211
ID
381
382#endif /* _CIFS_UNICODE_H */