blob: 80b3d845419faa708ff79da3c13e62e722da56cb [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * cifs_unicode: Unicode kernel case support
4 *
5 * Function:
6 * Convert a unicode character to upper or lower case using
7 * compressed tables.
8 *
Steve Frenchd185cda2009-04-30 17:45:10 +00009 * Copyright (c) International Business Machines Corp., 2000,2009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Notes:
12 * These APIs are based on the C library functions. The semantics
13 * should match the C functions but with expanded size operands.
14 *
15 * The upper/lower functions are based on a table created by mkupr.
16 * This is a compressed table of upper and lower case conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Igor Druzhininbf4f1212010-08-20 00:27:12 +040018#ifndef _CIFS_UNICODE_H
19#define _CIFS_UNICODE_H
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/byteorder.h>
22#include <linux/types.h>
23#include <linux/nls.h>
24
25#define UNIUPR_NOLOWER /* Example to not expand lower case tables */
26
Jeff Layton66345f52009-04-30 06:45:08 -040027/*
28 * Windows maps these to the user defined 16 bit Unicode range since they are
29 * reserved symbols (along with \ and /), otherwise illegal to store
30 * in filenames in NTFS
31 */
Jeff Layton581ade42011-04-05 15:02:37 -040032#define UNI_ASTERISK (__u16) ('*' + 0xF000)
Jeff Layton66345f52009-04-30 06:45:08 -040033#define UNI_QUESTION (__u16) ('?' + 0xF000)
34#define UNI_COLON (__u16) (':' + 0xF000)
35#define UNI_GRTRTHAN (__u16) ('>' + 0xF000)
36#define UNI_LESSTHAN (__u16) ('<' + 0xF000)
37#define UNI_PIPE (__u16) ('|' + 0xF000)
38#define UNI_SLASH (__u16) ('\\' + 0xF000)
39
Steve Frenchb6938552014-09-25 13:20:05 -050040/*
41 * Macs use an older "SFM" mapping of the symbols above. Fortunately it does
42 * not conflict (although almost does) with the mapping above.
43 */
44
Björn Jacke85435d72017-05-05 04:36:16 +020045#define SFM_DOUBLEQUOTE ((__u16) 0xF020)
Steve Frenchb6938552014-09-25 13:20:05 -050046#define SFM_ASTERISK ((__u16) 0xF021)
47#define SFM_QUESTION ((__u16) 0xF025)
48#define SFM_COLON ((__u16) 0xF022)
49#define SFM_GRTRTHAN ((__u16) 0xF024)
50#define SFM_LESSTHAN ((__u16) 0xF023)
51#define SFM_PIPE ((__u16) 0xF027)
52#define SFM_SLASH ((__u16) 0xF026)
Björn Jackeb704e702017-05-03 23:47:44 +020053#define SFM_SPACE ((__u16) 0xF028)
54#define SFM_PERIOD ((__u16) 0xF029)
Steve Frenchb6938552014-09-25 13:20:05 -050055
56/*
57 * Mapping mechanism to use when one of the seven reserved characters is
58 * encountered. We can only map using one of the mechanisms at a time
59 * since otherwise readdir could return directory entries which we would
60 * not be able to open
61 *
62 * NO_MAP_UNI_RSVD = do not perform any remapping of the character
63 * SFM_MAP_UNI_RSVD = map reserved characters using SFM scheme (MAC compatible)
64 * SFU_MAP_UNI_RSVD = map reserved characters ala SFU ("mapchars" option)
65 *
66 */
67#define NO_MAP_UNI_RSVD 0
68#define SFM_MAP_UNI_RSVD 1
69#define SFU_MAP_UNI_RSVD 2
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* Just define what we want from uniupr.h. We don't want to define the tables
72 * in each source file.
73 */
74#ifndef UNICASERANGE_DEFINED
75struct UniCaseRange {
76 wchar_t start;
77 wchar_t end;
78 signed char *table;
79};
80#endif /* UNICASERANGE_DEFINED */
81
82#ifndef UNIUPR_NOUPPER
83extern signed char CifsUniUpperTable[512];
84extern const struct UniCaseRange CifsUniUpperRange[];
85#endif /* UNIUPR_NOUPPER */
86
87#ifndef UNIUPR_NOLOWER
Igor Druzhininbf4f1212010-08-20 00:27:12 +040088extern signed char CifsUniLowerTable[512];
89extern const struct UniCaseRange CifsUniLowerRange[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#endif /* UNIUPR_NOLOWER */
91
92#ifdef __KERNEL__
Steve Frenchacbbb762012-01-18 22:32:33 -060093int cifs_from_utf16(char *to, const __le16 *from, int tolen, int fromlen,
Steve Frenchb6938552014-09-25 13:20:05 -050094 const struct nls_table *cp, int map_type);
Steve Frenchacbbb762012-01-18 22:32:33 -060095int cifs_utf16_bytes(const __le16 *from, int maxbytes,
96 const struct nls_table *codepage);
97int cifs_strtoUTF16(__le16 *, const char *, int, const struct nls_table *);
98char *cifs_strndup_from_utf16(const char *src, const int maxlen,
99 const bool is_unicode,
100 const struct nls_table *codepage);
101extern int cifsConvertToUTF16(__le16 *target, const char *source, int maxlen,
102 const struct nls_table *cp, int mapChars);
Steve French2baa2682014-09-27 02:19:01 -0500103extern int cifs_remap(struct cifs_sb_info *cifs_sb);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400104extern __le16 *cifs_strndup_to_utf16(const char *src, const int maxlen,
105 int *utf16_len, const struct nls_table *cp,
106 int remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#endif
108
Jeff Laytonc2ccf532013-09-05 08:38:11 -0400109wchar_t cifs_toupper(wchar_t in);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * UniStrcat: Concatenate the second string to the first
113 *
114 * Returns:
115 * Address of the first string
116 */
Steve French284316d2017-03-02 15:42:48 -0600117static inline __le16 *
118UniStrcat(__le16 *ucs1, const __le16 *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Steve French284316d2017-03-02 15:42:48 -0600120 __le16 *anchor = ucs1; /* save a pointer to start of ucs1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 while (*ucs1++) ; /* To end of first string */
123 ucs1--; /* Return to the null */
124 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
125 return anchor;
126}
127
128/*
129 * UniStrchr: Find a character in a string
130 *
131 * Returns:
132 * Address of first occurrence of character in string
133 * or NULL if the character is not in the string
134 */
135static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000136UniStrchr(const wchar_t *ucs, wchar_t uc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 while ((*ucs != uc) && *ucs)
139 ucs++;
140
141 if (*ucs == uc)
142 return (wchar_t *) ucs;
143 return NULL;
144}
145
146/*
147 * UniStrcmp: Compare two strings
148 *
149 * Returns:
150 * < 0: First string is less than second
151 * = 0: Strings are equal
152 * > 0: First string is greater than second
153 */
154static inline int
Steve French50c2f752007-07-13 00:33:32 +0000155UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 while ((*ucs1 == *ucs2) && *ucs1) {
158 ucs1++;
159 ucs2++;
160 }
161 return (int) *ucs1 - (int) *ucs2;
162}
163
164/*
165 * UniStrcpy: Copy a string
166 */
167static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000168UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 wchar_t *anchor = ucs1; /* save the start of result string */
171
172 while ((*ucs1++ = *ucs2++)) ;
173 return anchor;
174}
175
176/*
177 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
178 */
179static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000180UniStrlen(const wchar_t *ucs1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 int i = 0;
183
184 while (*ucs1++)
185 i++;
186 return i;
187}
188
189/*
Steve Frenchd38d8c72007-06-28 19:44:13 +0000190 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
191 * string (length limited)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
193static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000194UniStrnlen(const wchar_t *ucs1, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 int i = 0;
197
198 while (*ucs1++) {
199 i++;
200 if (i >= maxlen)
201 break;
202 }
203 return i;
204}
205
206/*
207 * UniStrncat: Concatenate length limited string
208 */
209static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000210UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 wchar_t *anchor = ucs1; /* save pointer to string 1 */
213
214 while (*ucs1++) ;
215 ucs1--; /* point to null terminator of s1 */
216 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
217 ucs1++;
218 ucs2++;
219 }
220 *ucs1 = 0; /* Null terminate the result */
221 return (anchor);
222}
223
224/*
225 * UniStrncmp: Compare length limited string
226 */
227static inline int
Steve French50c2f752007-07-13 00:33:32 +0000228UniStrncmp(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 == *ucs2) && *ucs1 && --n) {
233 ucs1++;
234 ucs2++;
235 }
236 return (int) *ucs1 - (int) *ucs2;
237}
238
239/*
240 * UniStrncmp_le: Compare length limited string - native to little-endian
241 */
242static inline int
Steve French50c2f752007-07-13 00:33:32 +0000243UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 if (!n)
246 return 0; /* Null strings are equal */
247 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
248 ucs1++;
249 ucs2++;
250 }
251 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
252}
253
254/*
255 * UniStrncpy: Copy length limited string with pad
256 */
257static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000258UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 wchar_t *anchor = ucs1;
261
262 while (n-- && *ucs2) /* Copy the strings */
263 *ucs1++ = *ucs2++;
264
265 n++;
266 while (n--) /* Pad with nulls */
267 *ucs1++ = 0;
268 return anchor;
269}
270
271/*
272 * UniStrncpy_le: Copy length limited string with pad to little-endian
273 */
274static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000275UniStrncpy_le(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++ = __le16_to_cpu(*ucs2++);
281
282 n++;
283 while (n--) /* Pad with nulls */
284 *ucs1++ = 0;
285 return anchor;
286}
287
288/*
289 * UniStrstr: Find a string in a string
290 *
291 * Returns:
292 * Address of first match found
293 * NULL if no matching string is found
294 */
295static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000296UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 const wchar_t *anchor1 = ucs1;
299 const wchar_t *anchor2 = ucs2;
300
301 while (*ucs1) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000302 if (*ucs1 == *ucs2) {
303 /* Partial match found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 ucs1++;
305 ucs2++;
306 } else {
307 if (!*ucs2) /* Match found */
308 return (wchar_t *) anchor1;
309 ucs1 = ++anchor1; /* No match */
310 ucs2 = anchor2;
311 }
312 }
313
314 if (!*ucs2) /* Both end together */
315 return (wchar_t *) anchor1; /* Match found */
316 return NULL; /* No match */
317}
318
319#ifndef UNIUPR_NOUPPER
320/*
321 * UniToupper: Convert a unicode character to upper case
322 */
323static inline wchar_t
324UniToupper(register wchar_t uc)
325{
326 register const struct UniCaseRange *rp;
327
Steve Frenchad7a2922008-02-07 23:25:02 +0000328 if (uc < sizeof(CifsUniUpperTable)) {
329 /* Latin characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return uc + CifsUniUpperTable[uc]; /* Use base tables */
331 } else {
332 rp = CifsUniUpperRange; /* Use range tables */
333 while (rp->start) {
334 if (uc < rp->start) /* Before start of range */
335 return uc; /* Uppercase = input */
336 if (uc <= rp->end) /* In range */
337 return uc + rp->table[uc - rp->start];
338 rp++; /* Try next range */
339 }
340 }
341 return uc; /* Past last range */
342}
343
344/*
345 * UniStrupr: Upper case a unicode string
346 */
Steve Frenchfdf96a92013-06-25 14:03:16 -0500347static inline __le16 *
348UniStrupr(register __le16 *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Steve Frenchfdf96a92013-06-25 14:03:16 -0500350 register __le16 *up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 up = upin;
353 while (*up) { /* For all characters */
Steve Frenchfdf96a92013-06-25 14:03:16 -0500354 *up = cpu_to_le16(UniToupper(le16_to_cpu(*up)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 up++;
356 }
357 return upin; /* Return input pointer */
358}
359#endif /* UNIUPR_NOUPPER */
360
361#ifndef UNIUPR_NOLOWER
362/*
363 * UniTolower: Convert a unicode character to lower case
364 */
365static inline wchar_t
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400366UniTolower(register wchar_t uc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400368 register const struct UniCaseRange *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400370 if (uc < sizeof(CifsUniLowerTable)) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000371 /* Latin characters */
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400372 return uc + CifsUniLowerTable[uc]; /* Use base tables */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 } else {
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400374 rp = CifsUniLowerRange; /* Use range tables */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 while (rp->start) {
376 if (uc < rp->start) /* Before start of range */
377 return uc; /* Uppercase = input */
378 if (uc <= rp->end) /* In range */
379 return uc + rp->table[uc - rp->start];
380 rp++; /* Try next range */
381 }
382 }
383 return uc; /* Past last range */
384}
385
386/*
387 * UniStrlwr: Lower case a unicode string
388 */
389static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000390UniStrlwr(register wchar_t *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 register wchar_t *up;
393
394 up = upin;
395 while (*up) { /* For all characters */
396 *up = UniTolower(*up);
397 up++;
398 }
399 return upin; /* Return input pointer */
400}
401
402#endif
Igor Druzhininbf4f1212010-08-20 00:27:12 +0400403
404#endif /* _CIFS_UNICODE_H */