1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Some of the source code in this file came from fs/cifs/cifs_unicode.c
4 * cifs_unicode: Unicode kernel case support
7 * Convert a unicode character to upper or lower case using
10 * Copyright (c) International Business Machines Corp., 2000,2009
14 * These APIs are based on the C library functions. The semantics
15 * should match the C functions but with expanded size operands.
17 * The upper/lower functions are based on a table created by mkupr.
18 * This is a compressed table of upper and lower case conversion.
21 #ifndef _CIFS_UNICODE_H
22 #define _CIFS_UNICODE_H
24 #include <asm/byteorder.h>
25 #include <linux/types.h>
26 #include <linux/nls.h>
28 #define UNIUPR_NOLOWER /* Example to not expand lower case tables */
31 * Windows maps these to the user defined 16 bit Unicode range since they are
32 * reserved symbols (along with \ and /), otherwise illegal to store
33 * in filenames in NTFS
35 #define UNI_ASTERISK ((__u16)('*' + 0xF000))
36 #define UNI_QUESTION ((__u16)('?' + 0xF000))
37 #define UNI_COLON ((__u16)(':' + 0xF000))
38 #define UNI_GRTRTHAN ((__u16)('>' + 0xF000))
39 #define UNI_LESSTHAN ((__u16)('<' + 0xF000))
40 #define UNI_PIPE ((__u16)('|' + 0xF000))
41 #define UNI_SLASH ((__u16)('\\' + 0xF000))
43 /* Just define what we want from uniupr.h. We don't want to define the tables
44 * in each source file.
46 #ifndef UNICASERANGE_DEFINED
52 #endif /* UNICASERANGE_DEFINED */
54 #ifndef UNIUPR_NOUPPER
55 extern signed char SmbUniUpperTable[512];
56 extern const struct UniCaseRange SmbUniUpperRange[];
57 #endif /* UNIUPR_NOUPPER */
59 #ifndef UNIUPR_NOLOWER
60 extern signed char CifsUniLowerTable[512];
61 extern const struct UniCaseRange CifsUniLowerRange[];
62 #endif /* UNIUPR_NOLOWER */
65 int smb_strtoUTF16(__le16 *to, const char *from, int len,
66 const struct nls_table *codepage);
67 char *smb_strndup_from_utf16(const char *src, const int maxlen,
68 const bool is_unicode,
69 const struct nls_table *codepage);
70 int smbConvertToUTF16(__le16 *target, const char *source, int srclen,
71 const struct nls_table *cp, int mapchars);
72 char *ksmbd_extract_sharename(char *treename);
76 * UniStrcat: Concatenate the second string to the first
79 * Address of the first string
81 static inline wchar_t *UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
83 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
86 /*NULL*/; /* To end of first string */
87 ucs1--; /* Return to the null */
88 while ((*ucs1++ = *ucs2++))
89 /*NULL*/; /* copy string 2 over */
94 * UniStrchr: Find a character in a string
97 * Address of first occurrence of character in string
98 * or NULL if the character is not in the string
100 static inline wchar_t *UniStrchr(const wchar_t *ucs, wchar_t uc)
102 while ((*ucs != uc) && *ucs)
106 return (wchar_t *)ucs;
111 * UniStrcmp: Compare two strings
114 * < 0: First string is less than second
115 * = 0: Strings are equal
116 * > 0: First string is greater than second
118 static inline int UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
120 while ((*ucs1 == *ucs2) && *ucs1) {
124 return (int)*ucs1 - (int)*ucs2;
128 * UniStrcpy: Copy a string
130 static inline wchar_t *UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
132 wchar_t *anchor = ucs1; /* save the start of result string */
134 while ((*ucs1++ = *ucs2++))
140 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
142 static inline size_t UniStrlen(const wchar_t *ucs1)
152 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
153 * string (length limited)
155 static inline size_t UniStrnlen(const wchar_t *ucs1, int maxlen)
168 * UniStrncat: Concatenate length limited string
170 static inline wchar_t *UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
172 wchar_t *anchor = ucs1; /* save pointer to string 1 */
176 ucs1--; /* point to null terminator of s1 */
177 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
181 *ucs1 = 0; /* Null terminate the result */
186 * UniStrncmp: Compare length limited string
188 static inline int UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
191 return 0; /* Null strings are equal */
192 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
196 return (int)*ucs1 - (int)*ucs2;
200 * UniStrncmp_le: Compare length limited string - native to little-endian
203 UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
206 return 0; /* Null strings are equal */
207 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
211 return (int)*ucs1 - (int)__le16_to_cpu(*ucs2);
215 * UniStrncpy: Copy length limited string with pad
217 static inline wchar_t *UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
219 wchar_t *anchor = ucs1;
221 while (n-- && *ucs2) /* Copy the strings */
225 while (n--) /* Pad with nulls */
231 * UniStrncpy_le: Copy length limited string with pad to little-endian
233 static inline wchar_t *UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
235 wchar_t *anchor = ucs1;
237 while (n-- && *ucs2) /* Copy the strings */
238 *ucs1++ = __le16_to_cpu(*ucs2++);
241 while (n--) /* Pad with nulls */
247 * UniStrstr: Find a string in a string
250 * Address of first match found
251 * NULL if no matching string is found
253 static inline wchar_t *UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
255 const wchar_t *anchor1 = ucs1;
256 const wchar_t *anchor2 = ucs2;
259 if (*ucs1 == *ucs2) {
260 /* Partial match found */
264 if (!*ucs2) /* Match found */
265 return (wchar_t *)anchor1;
266 ucs1 = ++anchor1; /* No match */
271 if (!*ucs2) /* Both end together */
272 return (wchar_t *)anchor1; /* Match found */
273 return NULL; /* No match */
276 #ifndef UNIUPR_NOUPPER
278 * UniToupper: Convert a unicode character to upper case
280 static inline wchar_t UniToupper(register wchar_t uc)
282 register const struct UniCaseRange *rp;
284 if (uc < sizeof(SmbUniUpperTable)) {
285 /* Latin characters */
286 return uc + SmbUniUpperTable[uc]; /* Use base tables */
289 rp = SmbUniUpperRange; /* Use range tables */
291 if (uc < rp->start) /* Before start of range */
292 return uc; /* Uppercase = input */
293 if (uc <= rp->end) /* In range */
294 return uc + rp->table[uc - rp->start];
295 rp++; /* Try next range */
297 return uc; /* Past last range */
301 * UniStrupr: Upper case a unicode string
303 static inline __le16 *UniStrupr(register __le16 *upin)
308 while (*up) { /* For all characters */
309 *up = cpu_to_le16(UniToupper(le16_to_cpu(*up)));
312 return upin; /* Return input pointer */
314 #endif /* UNIUPR_NOUPPER */
316 #ifndef UNIUPR_NOLOWER
318 * UniTolower: Convert a unicode character to lower case
320 static inline wchar_t UniTolower(register wchar_t uc)
322 register const struct UniCaseRange *rp;
324 if (uc < sizeof(CifsUniLowerTable)) {
325 /* Latin characters */
326 return uc + CifsUniLowerTable[uc]; /* Use base tables */
329 rp = CifsUniLowerRange; /* Use range tables */
331 if (uc < rp->start) /* Before start of range */
332 return uc; /* Uppercase = input */
333 if (uc <= rp->end) /* In range */
334 return uc + rp->table[uc - rp->start];
335 rp++; /* Try next range */
337 return uc; /* Past last range */
341 * UniStrlwr: Lower case a unicode string
343 static inline wchar_t *UniStrlwr(register wchar_t *upin)
345 register wchar_t *up;
348 while (*up) { /* For all characters */
349 *up = UniTolower(*up);
352 return upin; /* Return input pointer */
357 #endif /* _CIFS_UNICODE_H */