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, const struct nls_table *codepage);
69 int smbConvertToUTF16(__le16 *target, const char *source, int srclen,
70 const struct nls_table *cp, int mapchars);
71 char *ksmbd_extract_sharename(char *treename);
75 * UniStrcat: Concatenate the second string to the first
78 * Address of the first string
80 static inline wchar_t *UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
82 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
85 /*NULL*/; /* To end of first string */
86 ucs1--; /* Return to the null */
87 while ((*ucs1++ = *ucs2++))
88 /*NULL*/; /* copy string 2 over */
93 * UniStrchr: Find a character in a string
96 * Address of first occurrence of character in string
97 * or NULL if the character is not in the string
99 static inline wchar_t *UniStrchr(const wchar_t *ucs, wchar_t uc)
101 while ((*ucs != uc) && *ucs)
105 return (wchar_t *)ucs;
110 * UniStrcmp: Compare two strings
113 * < 0: First string is less than second
114 * = 0: Strings are equal
115 * > 0: First string is greater than second
117 static inline int UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
119 while ((*ucs1 == *ucs2) && *ucs1) {
123 return (int)*ucs1 - (int)*ucs2;
127 * UniStrcpy: Copy a string
129 static inline wchar_t *UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
131 wchar_t *anchor = ucs1; /* save the start of result string */
133 while ((*ucs1++ = *ucs2++))
139 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
141 static inline size_t UniStrlen(const wchar_t *ucs1)
151 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
152 * string (length limited)
154 static inline size_t UniStrnlen(const wchar_t *ucs1, int maxlen)
167 * UniStrncat: Concatenate length limited string
169 static inline wchar_t *UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
171 wchar_t *anchor = ucs1; /* save pointer to string 1 */
175 ucs1--; /* point to null terminator of s1 */
176 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
180 *ucs1 = 0; /* Null terminate the result */
185 * UniStrncmp: Compare length limited string
187 static inline int UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
190 return 0; /* Null strings are equal */
191 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
195 return (int)*ucs1 - (int)*ucs2;
199 * UniStrncmp_le: Compare length limited string - native to little-endian
202 UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
205 return 0; /* Null strings are equal */
206 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
210 return (int)*ucs1 - (int)__le16_to_cpu(*ucs2);
214 * UniStrncpy: Copy length limited string with pad
216 static inline wchar_t *UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
218 wchar_t *anchor = ucs1;
220 while (n-- && *ucs2) /* Copy the strings */
224 while (n--) /* Pad with nulls */
230 * UniStrncpy_le: Copy length limited string with pad to little-endian
232 static inline wchar_t *UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
234 wchar_t *anchor = ucs1;
236 while (n-- && *ucs2) /* Copy the strings */
237 *ucs1++ = __le16_to_cpu(*ucs2++);
240 while (n--) /* Pad with nulls */
246 * UniStrstr: Find a string in a string
249 * Address of first match found
250 * NULL if no matching string is found
252 static inline wchar_t *UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
254 const wchar_t *anchor1 = ucs1;
255 const wchar_t *anchor2 = ucs2;
258 if (*ucs1 == *ucs2) {
259 /* Partial match found */
263 if (!*ucs2) /* Match found */
264 return (wchar_t *)anchor1;
265 ucs1 = ++anchor1; /* No match */
270 if (!*ucs2) /* Both end together */
271 return (wchar_t *)anchor1; /* Match found */
272 return NULL; /* No match */
275 #ifndef UNIUPR_NOUPPER
277 * UniToupper: Convert a unicode character to upper case
279 static inline wchar_t UniToupper(register wchar_t uc)
281 register const struct UniCaseRange *rp;
283 if (uc < sizeof(SmbUniUpperTable)) {
284 /* Latin characters */
285 return uc + SmbUniUpperTable[uc]; /* Use base tables */
288 rp = SmbUniUpperRange; /* Use range tables */
290 if (uc < rp->start) /* Before start of range */
291 return uc; /* Uppercase = input */
292 if (uc <= rp->end) /* In range */
293 return uc + rp->table[uc - rp->start];
294 rp++; /* Try next range */
296 return uc; /* Past last range */
300 * UniStrupr: Upper case a unicode string
302 static inline __le16 *UniStrupr(register __le16 *upin)
307 while (*up) { /* For all characters */
308 *up = cpu_to_le16(UniToupper(le16_to_cpu(*up)));
311 return upin; /* Return input pointer */
313 #endif /* UNIUPR_NOUPPER */
315 #ifndef UNIUPR_NOLOWER
317 * UniTolower: Convert a unicode character to lower case
319 static inline wchar_t UniTolower(register wchar_t uc)
321 register const struct UniCaseRange *rp;
323 if (uc < sizeof(CifsUniLowerTable)) {
324 /* Latin characters */
325 return uc + CifsUniLowerTable[uc]; /* Use base tables */
328 rp = CifsUniLowerRange; /* Use range tables */
330 if (uc < rp->start) /* Before start of range */
331 return uc; /* Uppercase = input */
332 if (uc <= rp->end) /* In range */
333 return uc + rp->table[uc - rp->start];
334 rp++; /* Try next range */
336 return uc; /* Past last range */
340 * UniStrlwr: Lower case a unicode string
342 static inline wchar_t *UniStrlwr(register wchar_t *upin)
344 register wchar_t *up;
347 while (*up) { /* For all characters */
348 *up = UniTolower(*up);
351 return upin; /* Return input pointer */
356 #endif /* _CIFS_UNICODE_H */