blob: d8eac3b6cefb35639000ff74367129fbc72f37dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Steve Frenchd185cda2009-04-30 17:45:10 +00008 * Copyright (c) International Business Machines Corp., 2000,2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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
Steve Frenchd38d8c72007-06-28 19:44:13 +000012 * the Free Software Foundation; either version 2 of the License, or
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * (at your option) any later version.
Steve Frenchd38d8c72007-06-28 19:44:13 +000014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * 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
Steve Frenchd38d8c72007-06-28 19:44:13 +000021 * along with this program; if not, write to the Free Software
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * 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 */
Igor Druzhininbf4f1212010-08-20 00:27:12 +040033#ifndef _CIFS_UNICODE_H
34#define _CIFS_UNICODE_H
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
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
Jeff Layton66345f52009-04-30 06:45:08 -040042/*
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 */
Jeff Layton581ade42011-04-05 15:02:37 -040047#define UNI_ASTERISK (__u16) ('*' + 0xF000)
Jeff Layton66345f52009-04-30 06:45:08 -040048#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/* 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
Igor Druzhininbf4f1212010-08-20 00:27:12 +040072extern signed char CifsUniLowerTable[512];
73extern const struct UniCaseRange CifsUniLowerRange[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif /* UNIUPR_NOLOWER */
75
76#ifdef __KERNEL__
Steve Frenchacbbb762012-01-18 22:32:33 -060077int 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);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +040087#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 /* CONFIG_CIFS_SMB2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#endif
93
Jeff Laytonc2ccf532013-09-05 08:38:11 -040094wchar_t cifs_toupper(wchar_t in);
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
97 * UniStrcat: Concatenate the second string to the first
98 *
99 * Returns:
100 * Address of the first string
101 */
102static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000103UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
106
107 while (*ucs1++) ; /* To end of first string */
108 ucs1--; /* Return to the null */
109 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
110 return anchor;
111}
112
113/*
114 * UniStrchr: Find a character in a string
115 *
116 * Returns:
117 * Address of first occurrence of character in string
118 * or NULL if the character is not in the string
119 */
120static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000121UniStrchr(const wchar_t *ucs, wchar_t uc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 while ((*ucs != uc) && *ucs)
124 ucs++;
125
126 if (*ucs == uc)
127 return (wchar_t *) ucs;
128 return NULL;
129}
130
131/*
132 * UniStrcmp: Compare two strings
133 *
134 * Returns:
135 * < 0: First string is less than second
136 * = 0: Strings are equal
137 * > 0: First string is greater than second
138 */
139static inline int
Steve French50c2f752007-07-13 00:33:32 +0000140UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 while ((*ucs1 == *ucs2) && *ucs1) {
143 ucs1++;
144 ucs2++;
145 }
146 return (int) *ucs1 - (int) *ucs2;
147}
148
149/*
150 * UniStrcpy: Copy a string
151 */
152static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000153UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 wchar_t *anchor = ucs1; /* save the start of result string */
156
157 while ((*ucs1++ = *ucs2++)) ;
158 return anchor;
159}
160
161/*
162 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
163 */
164static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000165UniStrlen(const wchar_t *ucs1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 int i = 0;
168
169 while (*ucs1++)
170 i++;
171 return i;
172}
173
174/*
Steve Frenchd38d8c72007-06-28 19:44:13 +0000175 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
176 * string (length limited)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
178static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000179UniStrnlen(const wchar_t *ucs1, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int i = 0;
182
183 while (*ucs1++) {
184 i++;
185 if (i >= maxlen)
186 break;
187 }
188 return i;
189}
190
191/*
192 * UniStrncat: Concatenate length limited string
193 */
194static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000195UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 wchar_t *anchor = ucs1; /* save pointer to string 1 */
198
199 while (*ucs1++) ;
200 ucs1--; /* point to null terminator of s1 */
201 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
202 ucs1++;
203 ucs2++;
204 }
205 *ucs1 = 0; /* Null terminate the result */
206 return (anchor);
207}
208
209/*
210 * UniStrncmp: Compare length limited string
211 */
212static inline int
Steve French50c2f752007-07-13 00:33:32 +0000213UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 if (!n)
216 return 0; /* Null strings are equal */
217 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
218 ucs1++;
219 ucs2++;
220 }
221 return (int) *ucs1 - (int) *ucs2;
222}
223
224/*
225 * UniStrncmp_le: Compare length limited string - native to little-endian
226 */
227static inline int
Steve French50c2f752007-07-13 00:33:32 +0000228UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 if (!n)
231 return 0; /* Null strings are equal */
232 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
233 ucs1++;
234 ucs2++;
235 }
236 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
237}
238
239/*
240 * UniStrncpy: Copy length limited string with pad
241 */
242static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000243UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 wchar_t *anchor = ucs1;
246
247 while (n-- && *ucs2) /* Copy the strings */
248 *ucs1++ = *ucs2++;
249
250 n++;
251 while (n--) /* Pad with nulls */
252 *ucs1++ = 0;
253 return anchor;
254}
255
256/*
257 * UniStrncpy_le: Copy length limited string with pad to little-endian
258 */
259static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000260UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 wchar_t *anchor = ucs1;
263
264 while (n-- && *ucs2) /* Copy the strings */
265 *ucs1++ = __le16_to_cpu(*ucs2++);
266
267 n++;
268 while (n--) /* Pad with nulls */
269 *ucs1++ = 0;
270 return anchor;
271}
272
273/*
274 * UniStrstr: Find a string in a string
275 *
276 * Returns:
277 * Address of first match found
278 * NULL if no matching string is found
279 */
280static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000281UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 const wchar_t *anchor1 = ucs1;
284 const wchar_t *anchor2 = ucs2;
285
286 while (*ucs1) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000287 if (*ucs1 == *ucs2) {
288 /* Partial match found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 ucs1++;
290 ucs2++;
291 } else {
292 if (!*ucs2) /* Match found */
293 return (wchar_t *) anchor1;
294 ucs1 = ++anchor1; /* No match */
295 ucs2 = anchor2;
296 }
297 }
298
299 if (!*ucs2) /* Both end together */
300 return (wchar_t *) anchor1; /* Match found */
301 return NULL; /* No match */
302}
303
304#ifndef UNIUPR_NOUPPER
305/*
306 * UniToupper: Convert a unicode character to upper case
307 */
308static inline wchar_t
309UniToupper(register wchar_t uc)
310{
311 register const struct UniCaseRange *rp;
312
Steve Frenchad7a2922008-02-07 23:25:02 +0000313 if (uc < sizeof(CifsUniUpperTable)) {
314 /* Latin characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return uc + CifsUniUpperTable[uc]; /* Use base tables */
316 } else {
317 rp = CifsUniUpperRange; /* Use range tables */
318 while (rp->start) {
319 if (uc < rp->start) /* Before start of range */
320 return uc; /* Uppercase = input */
321 if (uc <= rp->end) /* In range */
322 return uc + rp->table[uc - rp->start];
323 rp++; /* Try next range */
324 }
325 }
326 return uc; /* Past last range */
327}
328
329/*
330 * UniStrupr: Upper case a unicode string
331 */
Steve Frenchfdf96a92013-06-25 14:03:16 -0500332static inline __le16 *
333UniStrupr(register __le16 *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Steve Frenchfdf96a92013-06-25 14:03:16 -0500335 register __le16 *up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 up = upin;
338 while (*up) { /* For all characters */
Steve Frenchfdf96a92013-06-25 14:03:16 -0500339 *up = cpu_to_le16(UniToupper(le16_to_cpu(*up)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 up++;
341 }
342 return upin; /* Return input pointer */
343}
344#endif /* UNIUPR_NOUPPER */
345
346#ifndef UNIUPR_NOLOWER
347/*
348 * UniTolower: Convert a unicode character to lower case
349 */
350static inline wchar_t
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400351UniTolower(register wchar_t uc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400353 register const struct UniCaseRange *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400355 if (uc < sizeof(CifsUniLowerTable)) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000356 /* Latin characters */
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400357 return uc + CifsUniLowerTable[uc]; /* Use base tables */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 } else {
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400359 rp = CifsUniLowerRange; /* Use range tables */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 while (rp->start) {
361 if (uc < rp->start) /* Before start of range */
362 return uc; /* Uppercase = input */
363 if (uc <= rp->end) /* In range */
364 return uc + rp->table[uc - rp->start];
365 rp++; /* Try next range */
366 }
367 }
368 return uc; /* Past last range */
369}
370
371/*
372 * UniStrlwr: Lower case a unicode string
373 */
374static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000375UniStrlwr(register wchar_t *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 register wchar_t *up;
378
379 up = upin;
380 while (*up) { /* For all characters */
381 *up = UniTolower(*up);
382 up++;
383 }
384 return upin; /* Return input pointer */
385}
386
387#endif
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400388
389#endif /* _CIFS_UNICODE_H */