blob: 07ade707fa60a43c9f64ad72512b1992c98c4ff1 [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
Steve Frenchb6938552014-09-25 13:20:05 -050055/*
56 * Macs use an older "SFM" mapping of the symbols above. Fortunately it does
57 * not conflict (although almost does) with the mapping above.
58 */
59
Björn Jacke1c5d8b32017-05-05 04:36:16 +020060#define SFM_DOUBLEQUOTE ((__u16) 0xF020)
Steve Frenchb6938552014-09-25 13:20:05 -050061#define SFM_ASTERISK ((__u16) 0xF021)
62#define SFM_QUESTION ((__u16) 0xF025)
63#define SFM_COLON ((__u16) 0xF022)
64#define SFM_GRTRTHAN ((__u16) 0xF024)
65#define SFM_LESSTHAN ((__u16) 0xF023)
66#define SFM_PIPE ((__u16) 0xF027)
67#define SFM_SLASH ((__u16) 0xF026)
Björn Jacke87c06042017-05-03 23:47:44 +020068#define SFM_SPACE ((__u16) 0xF028)
69#define SFM_PERIOD ((__u16) 0xF029)
Steve Frenchb6938552014-09-25 13:20:05 -050070
71/*
72 * Mapping mechanism to use when one of the seven reserved characters is
73 * encountered. We can only map using one of the mechanisms at a time
74 * since otherwise readdir could return directory entries which we would
75 * not be able to open
76 *
77 * NO_MAP_UNI_RSVD = do not perform any remapping of the character
78 * SFM_MAP_UNI_RSVD = map reserved characters using SFM scheme (MAC compatible)
79 * SFU_MAP_UNI_RSVD = map reserved characters ala SFU ("mapchars" option)
80 *
81 */
82#define NO_MAP_UNI_RSVD 0
83#define SFM_MAP_UNI_RSVD 1
84#define SFU_MAP_UNI_RSVD 2
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* Just define what we want from uniupr.h. We don't want to define the tables
87 * in each source file.
88 */
89#ifndef UNICASERANGE_DEFINED
90struct UniCaseRange {
91 wchar_t start;
92 wchar_t end;
93 signed char *table;
94};
95#endif /* UNICASERANGE_DEFINED */
96
97#ifndef UNIUPR_NOUPPER
98extern signed char CifsUniUpperTable[512];
99extern const struct UniCaseRange CifsUniUpperRange[];
100#endif /* UNIUPR_NOUPPER */
101
102#ifndef UNIUPR_NOLOWER
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400103extern signed char CifsUniLowerTable[512];
104extern const struct UniCaseRange CifsUniLowerRange[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#endif /* UNIUPR_NOLOWER */
106
107#ifdef __KERNEL__
Steve Frenchacbbb762012-01-18 22:32:33 -0600108int cifs_from_utf16(char *to, const __le16 *from, int tolen, int fromlen,
Steve Frenchb6938552014-09-25 13:20:05 -0500109 const struct nls_table *cp, int map_type);
Steve Frenchacbbb762012-01-18 22:32:33 -0600110int cifs_utf16_bytes(const __le16 *from, int maxbytes,
111 const struct nls_table *codepage);
112int cifs_strtoUTF16(__le16 *, const char *, int, const struct nls_table *);
113char *cifs_strndup_from_utf16(const char *src, const int maxlen,
114 const bool is_unicode,
115 const struct nls_table *codepage);
116extern int cifsConvertToUTF16(__le16 *target, const char *source, int maxlen,
117 const struct nls_table *cp, int mapChars);
Steve French2baa2682014-09-27 02:19:01 -0500118extern int cifs_remap(struct cifs_sb_info *cifs_sb);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400119#ifdef CONFIG_CIFS_SMB2
120extern __le16 *cifs_strndup_to_utf16(const char *src, const int maxlen,
121 int *utf16_len, const struct nls_table *cp,
122 int remap);
123#endif /* CONFIG_CIFS_SMB2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#endif
125
Jeff Laytonc2ccf532013-09-05 08:38:11 -0400126wchar_t cifs_toupper(wchar_t in);
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/*
129 * UniStrcat: Concatenate the second string to the first
130 *
131 * Returns:
132 * Address of the first string
133 */
134static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000135UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
138
139 while (*ucs1++) ; /* To end of first string */
140 ucs1--; /* Return to the null */
141 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
142 return anchor;
143}
144
145/*
146 * UniStrchr: Find a character in a string
147 *
148 * Returns:
149 * Address of first occurrence of character in string
150 * or NULL if the character is not in the string
151 */
152static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000153UniStrchr(const wchar_t *ucs, wchar_t uc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 while ((*ucs != uc) && *ucs)
156 ucs++;
157
158 if (*ucs == uc)
159 return (wchar_t *) ucs;
160 return NULL;
161}
162
163/*
164 * UniStrcmp: Compare two strings
165 *
166 * Returns:
167 * < 0: First string is less than second
168 * = 0: Strings are equal
169 * > 0: First string is greater than second
170 */
171static inline int
Steve French50c2f752007-07-13 00:33:32 +0000172UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 while ((*ucs1 == *ucs2) && *ucs1) {
175 ucs1++;
176 ucs2++;
177 }
178 return (int) *ucs1 - (int) *ucs2;
179}
180
181/*
182 * UniStrcpy: Copy a string
183 */
184static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000185UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 wchar_t *anchor = ucs1; /* save the start of result string */
188
189 while ((*ucs1++ = *ucs2++)) ;
190 return anchor;
191}
192
193/*
194 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
195 */
196static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000197UniStrlen(const wchar_t *ucs1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 int i = 0;
200
201 while (*ucs1++)
202 i++;
203 return i;
204}
205
206/*
Steve Frenchd38d8c72007-06-28 19:44:13 +0000207 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
208 * string (length limited)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
210static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000211UniStrnlen(const wchar_t *ucs1, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 int i = 0;
214
215 while (*ucs1++) {
216 i++;
217 if (i >= maxlen)
218 break;
219 }
220 return i;
221}
222
223/*
224 * UniStrncat: Concatenate length limited string
225 */
226static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000227UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 wchar_t *anchor = ucs1; /* save pointer to string 1 */
230
231 while (*ucs1++) ;
232 ucs1--; /* point to null terminator of s1 */
233 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
234 ucs1++;
235 ucs2++;
236 }
237 *ucs1 = 0; /* Null terminate the result */
238 return (anchor);
239}
240
241/*
242 * UniStrncmp: Compare length limited string
243 */
244static inline int
Steve French50c2f752007-07-13 00:33:32 +0000245UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 if (!n)
248 return 0; /* Null strings are equal */
249 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
250 ucs1++;
251 ucs2++;
252 }
253 return (int) *ucs1 - (int) *ucs2;
254}
255
256/*
257 * UniStrncmp_le: Compare length limited string - native to little-endian
258 */
259static inline int
Steve French50c2f752007-07-13 00:33:32 +0000260UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 if (!n)
263 return 0; /* Null strings are equal */
264 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
265 ucs1++;
266 ucs2++;
267 }
268 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
269}
270
271/*
272 * UniStrncpy: Copy length limited string with pad
273 */
274static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000275UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 wchar_t *anchor = ucs1;
278
279 while (n-- && *ucs2) /* Copy the strings */
280 *ucs1++ = *ucs2++;
281
282 n++;
283 while (n--) /* Pad with nulls */
284 *ucs1++ = 0;
285 return anchor;
286}
287
288/*
289 * UniStrncpy_le: Copy length limited string with pad to little-endian
290 */
291static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000292UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 wchar_t *anchor = ucs1;
295
296 while (n-- && *ucs2) /* Copy the strings */
297 *ucs1++ = __le16_to_cpu(*ucs2++);
298
299 n++;
300 while (n--) /* Pad with nulls */
301 *ucs1++ = 0;
302 return anchor;
303}
304
305/*
306 * UniStrstr: Find a string in a string
307 *
308 * Returns:
309 * Address of first match found
310 * NULL if no matching string is found
311 */
312static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000313UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 const wchar_t *anchor1 = ucs1;
316 const wchar_t *anchor2 = ucs2;
317
318 while (*ucs1) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000319 if (*ucs1 == *ucs2) {
320 /* Partial match found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 ucs1++;
322 ucs2++;
323 } else {
324 if (!*ucs2) /* Match found */
325 return (wchar_t *) anchor1;
326 ucs1 = ++anchor1; /* No match */
327 ucs2 = anchor2;
328 }
329 }
330
331 if (!*ucs2) /* Both end together */
332 return (wchar_t *) anchor1; /* Match found */
333 return NULL; /* No match */
334}
335
336#ifndef UNIUPR_NOUPPER
337/*
338 * UniToupper: Convert a unicode character to upper case
339 */
340static inline wchar_t
341UniToupper(register wchar_t uc)
342{
343 register const struct UniCaseRange *rp;
344
Steve Frenchad7a2922008-02-07 23:25:02 +0000345 if (uc < sizeof(CifsUniUpperTable)) {
346 /* Latin characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return uc + CifsUniUpperTable[uc]; /* Use base tables */
348 } else {
349 rp = CifsUniUpperRange; /* Use range tables */
350 while (rp->start) {
351 if (uc < rp->start) /* Before start of range */
352 return uc; /* Uppercase = input */
353 if (uc <= rp->end) /* In range */
354 return uc + rp->table[uc - rp->start];
355 rp++; /* Try next range */
356 }
357 }
358 return uc; /* Past last range */
359}
360
361/*
362 * UniStrupr: Upper case a unicode string
363 */
Steve Frenchfdf96a92013-06-25 14:03:16 -0500364static inline __le16 *
365UniStrupr(register __le16 *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Steve Frenchfdf96a92013-06-25 14:03:16 -0500367 register __le16 *up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 up = upin;
370 while (*up) { /* For all characters */
Steve Frenchfdf96a92013-06-25 14:03:16 -0500371 *up = cpu_to_le16(UniToupper(le16_to_cpu(*up)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 up++;
373 }
374 return upin; /* Return input pointer */
375}
376#endif /* UNIUPR_NOUPPER */
377
378#ifndef UNIUPR_NOLOWER
379/*
380 * UniTolower: Convert a unicode character to lower case
381 */
382static inline wchar_t
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400383UniTolower(register wchar_t uc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400385 register const struct UniCaseRange *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400387 if (uc < sizeof(CifsUniLowerTable)) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000388 /* Latin characters */
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400389 return uc + CifsUniLowerTable[uc]; /* Use base tables */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 } else {
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400391 rp = CifsUniLowerRange; /* Use range tables */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 while (rp->start) {
393 if (uc < rp->start) /* Before start of range */
394 return uc; /* Uppercase = input */
395 if (uc <= rp->end) /* In range */
396 return uc + rp->table[uc - rp->start];
397 rp++; /* Try next range */
398 }
399 }
400 return uc; /* Past last range */
401}
402
403/*
404 * UniStrlwr: Lower case a unicode string
405 */
406static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000407UniStrlwr(register wchar_t *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 register wchar_t *up;
410
411 up = upin;
412 while (*up) { /* For all characters */
413 *up = UniTolower(*up);
414 up++;
415 }
416 return upin; /* Return input pointer */
417}
418
419#endif
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400420
421#endif /* _CIFS_UNICODE_H */