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>
27 #include <linux/unicode.h>
29 #define UNIUPR_NOLOWER /* Example to not expand lower case tables */
32 * Windows maps these to the user defined 16 bit Unicode range since they are
33 * reserved symbols (along with \ and /), otherwise illegal to store
34 * in filenames in NTFS
36 #define UNI_ASTERISK ((__u16)('*' + 0xF000))
37 #define UNI_QUESTION ((__u16)('?' + 0xF000))
38 #define UNI_COLON ((__u16)(':' + 0xF000))
39 #define UNI_GRTRTHAN ((__u16)('>' + 0xF000))
40 #define UNI_LESSTHAN ((__u16)('<' + 0xF000))
41 #define UNI_PIPE ((__u16)('|' + 0xF000))
42 #define UNI_SLASH ((__u16)('\\' + 0xF000))
44 /* Just define what we want from uniupr.h. We don't want to define the tables
45 * in each source file.
47 #ifndef UNICASERANGE_DEFINED
53 #endif /* UNICASERANGE_DEFINED */
55 #ifndef UNIUPR_NOUPPER
56 extern signed char SmbUniUpperTable[512];
57 extern const struct UniCaseRange SmbUniUpperRange[];
58 #endif /* UNIUPR_NOUPPER */
60 #ifndef UNIUPR_NOLOWER
61 extern signed char CifsUniLowerTable[512];
62 extern const struct UniCaseRange CifsUniLowerRange[];
63 #endif /* UNIUPR_NOLOWER */
66 int smb_strtoUTF16(__le16 *to, const char *from, int len,
67 const struct nls_table *codepage);
68 char *smb_strndup_from_utf16(const char *src, const int maxlen,
69 const bool is_unicode,
70 const struct nls_table *codepage);
71 int smbConvertToUTF16(__le16 *target, const char *source, int srclen,
72 const struct nls_table *cp, int mapchars);
73 char *ksmbd_extract_sharename(struct unicode_map *um, const char *treename);
77 * UniStrcat: Concatenate the second string to the first
80 * Address of the first string
82 static inline wchar_t *UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
84 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
87 /*NULL*/; /* To end of first string */
88 ucs1--; /* Return to the null */
89 while ((*ucs1++ = *ucs2++))
90 /*NULL*/; /* copy string 2 over */
95 * UniStrchr: Find a character in a string
98 * Address of first occurrence of character in string
99 * or NULL if the character is not in the string
101 static inline wchar_t *UniStrchr(const wchar_t *ucs, wchar_t uc)
103 while ((*ucs != uc) && *ucs)
107 return (wchar_t *)ucs;
112 * UniStrcmp: Compare two strings
115 * < 0: First string is less than second
116 * = 0: Strings are equal
117 * > 0: First string is greater than second
119 static inline int UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
121 while ((*ucs1 == *ucs2) && *ucs1) {
125 return (int)*ucs1 - (int)*ucs2;
129 * UniStrcpy: Copy a string
131 static inline wchar_t *UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
133 wchar_t *anchor = ucs1; /* save the start of result string */
135 while ((*ucs1++ = *ucs2++))
141 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
143 static inline size_t UniStrlen(const wchar_t *ucs1)
153 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
154 * string (length limited)
156 static inline size_t UniStrnlen(const wchar_t *ucs1, int maxlen)
169 * UniStrncat: Concatenate length limited string
171 static inline wchar_t *UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
173 wchar_t *anchor = ucs1; /* save pointer to string 1 */
177 ucs1--; /* point to null terminator of s1 */
178 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
182 *ucs1 = 0; /* Null terminate the result */
187 * UniStrncmp: Compare length limited string
189 static inline int UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
192 return 0; /* Null strings are equal */
193 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
197 return (int)*ucs1 - (int)*ucs2;
201 * UniStrncmp_le: Compare length limited string - native to little-endian
204 UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
207 return 0; /* Null strings are equal */
208 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
212 return (int)*ucs1 - (int)__le16_to_cpu(*ucs2);
216 * UniStrncpy: Copy length limited string with pad
218 static inline wchar_t *UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
220 wchar_t *anchor = ucs1;
222 while (n-- && *ucs2) /* Copy the strings */
226 while (n--) /* Pad with nulls */
232 * UniStrncpy_le: Copy length limited string with pad to little-endian
234 static inline wchar_t *UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
236 wchar_t *anchor = ucs1;
238 while (n-- && *ucs2) /* Copy the strings */
239 *ucs1++ = __le16_to_cpu(*ucs2++);
242 while (n--) /* Pad with nulls */
248 * UniStrstr: Find a string in a string
251 * Address of first match found
252 * NULL if no matching string is found
254 static inline wchar_t *UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
256 const wchar_t *anchor1 = ucs1;
257 const wchar_t *anchor2 = ucs2;
260 if (*ucs1 == *ucs2) {
261 /* Partial match found */
265 if (!*ucs2) /* Match found */
266 return (wchar_t *)anchor1;
267 ucs1 = ++anchor1; /* No match */
272 if (!*ucs2) /* Both end together */
273 return (wchar_t *)anchor1; /* Match found */
274 return NULL; /* No match */
277 #ifndef UNIUPR_NOUPPER
279 * UniToupper: Convert a unicode character to upper case
281 static inline wchar_t UniToupper(register wchar_t uc)
283 register const struct UniCaseRange *rp;
285 if (uc < sizeof(SmbUniUpperTable)) {
286 /* Latin characters */
287 return uc + SmbUniUpperTable[uc]; /* Use base tables */
290 rp = SmbUniUpperRange; /* Use range tables */
292 if (uc < rp->start) /* Before start of range */
293 return uc; /* Uppercase = input */
294 if (uc <= rp->end) /* In range */
295 return uc + rp->table[uc - rp->start];
296 rp++; /* Try next range */
298 return uc; /* Past last range */
302 * UniStrupr: Upper case a unicode string
304 static inline __le16 *UniStrupr(register __le16 *upin)
309 while (*up) { /* For all characters */
310 *up = cpu_to_le16(UniToupper(le16_to_cpu(*up)));
313 return upin; /* Return input pointer */
315 #endif /* UNIUPR_NOUPPER */
317 #ifndef UNIUPR_NOLOWER
319 * UniTolower: Convert a unicode character to lower case
321 static inline wchar_t UniTolower(register wchar_t uc)
323 register const struct UniCaseRange *rp;
325 if (uc < sizeof(CifsUniLowerTable)) {
326 /* Latin characters */
327 return uc + CifsUniLowerTable[uc]; /* Use base tables */
330 rp = CifsUniLowerRange; /* Use range tables */
332 if (uc < rp->start) /* Before start of range */
333 return uc; /* Uppercase = input */
334 if (uc <= rp->end) /* In range */
335 return uc + rp->table[uc - rp->start];
336 rp++; /* Try next range */
338 return uc; /* Past last range */
342 * UniStrlwr: Lower case a unicode string
344 static inline wchar_t *UniStrlwr(register wchar_t *upin)
346 register wchar_t *up;
349 while (*up) { /* For all characters */
350 *up = UniTolower(*up);
353 return upin; /* Return input pointer */
358 #endif /* _CIFS_UNICODE_H */