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 * and then via server/unicode.c
5 * cifs_unicode: Unicode kernel case support
8 * Convert a unicode character to upper or lower case using
11 * Copyright (c) International Business Machines Corp., 2000,2009
15 * These APIs are based on the C library functions. The semantics
16 * should match the C functions but with expanded size operands.
18 * The upper/lower functions are based on a table created by mkupr.
19 * This is a compressed table of upper and lower case conversion.
22 #ifndef _NLS_UCS2_UTILS_H
23 #define _NLS_UCS2_UTILS_H
25 #include <asm/byteorder.h>
26 #include <linux/types.h>
27 #include <linux/nls.h>
28 #include <linux/unicode.h>
29 #include "nls_ucs2_data.h"
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))
45 * UniStrcat: Concatenate the second string to the first
48 * Address of the first string
50 static inline wchar_t *UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
52 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
55 /*NULL*/; /* To end of first string */
56 ucs1--; /* Return to the null */
57 while ((*ucs1++ = *ucs2++))
58 /*NULL*/; /* copy string 2 over */
63 * UniStrchr: Find a character in a string
66 * Address of first occurrence of character in string
67 * or NULL if the character is not in the string
69 static inline wchar_t *UniStrchr(const wchar_t *ucs, wchar_t uc)
71 while ((*ucs != uc) && *ucs)
75 return (wchar_t *)ucs;
80 * UniStrcmp: Compare two strings
83 * < 0: First string is less than second
84 * = 0: Strings are equal
85 * > 0: First string is greater than second
87 static inline int UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
89 while ((*ucs1 == *ucs2) && *ucs1) {
93 return (int)*ucs1 - (int)*ucs2;
97 * UniStrcpy: Copy a string
99 static inline wchar_t *UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
101 wchar_t *anchor = ucs1; /* save the start of result string */
103 while ((*ucs1++ = *ucs2++))
109 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
111 static inline size_t UniStrlen(const wchar_t *ucs1)
121 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
122 * string (length limited)
124 static inline size_t UniStrnlen(const wchar_t *ucs1, int maxlen)
137 * UniStrncat: Concatenate length limited string
139 static inline wchar_t *UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
141 wchar_t *anchor = ucs1; /* save pointer to string 1 */
145 ucs1--; /* point to null terminator of s1 */
146 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
150 *ucs1 = 0; /* Null terminate the result */
155 * UniStrncmp: Compare length limited string
157 static inline int UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
160 return 0; /* Null strings are equal */
161 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
165 return (int)*ucs1 - (int)*ucs2;
169 * UniStrncmp_le: Compare length limited string - native to little-endian
172 UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
175 return 0; /* Null strings are equal */
176 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
180 return (int)*ucs1 - (int)__le16_to_cpu(*ucs2);
184 * UniStrncpy: Copy length limited string with pad
186 static inline wchar_t *UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
188 wchar_t *anchor = ucs1;
190 while (n-- && *ucs2) /* Copy the strings */
194 while (n--) /* Pad with nulls */
200 * UniStrncpy_le: Copy length limited string with pad to little-endian
202 static inline wchar_t *UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
204 wchar_t *anchor = ucs1;
206 while (n-- && *ucs2) /* Copy the strings */
207 *ucs1++ = __le16_to_cpu(*ucs2++);
210 while (n--) /* Pad with nulls */
216 * UniStrstr: Find a string in a string
219 * Address of first match found
220 * NULL if no matching string is found
222 static inline wchar_t *UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
224 const wchar_t *anchor1 = ucs1;
225 const wchar_t *anchor2 = ucs2;
228 if (*ucs1 == *ucs2) {
229 /* Partial match found */
233 if (!*ucs2) /* Match found */
234 return (wchar_t *)anchor1;
235 ucs1 = ++anchor1; /* No match */
240 if (!*ucs2) /* Both end together */
241 return (wchar_t *)anchor1; /* Match found */
242 return NULL; /* No match */
245 #ifndef UNIUPR_NOUPPER
247 * UniToupper: Convert a unicode character to upper case
249 static inline wchar_t UniToupper(register wchar_t uc)
251 register const struct UniCaseRange *rp;
253 if (uc < sizeof(NlsUniUpperTable)) {
254 /* Latin characters */
255 return uc + NlsUniUpperTable[uc]; /* Use base tables */
258 rp = NlsUniUpperRange; /* Use range tables */
260 if (uc < rp->start) /* Before start of range */
261 return uc; /* Uppercase = input */
262 if (uc <= rp->end) /* In range */
263 return uc + rp->table[uc - rp->start];
264 rp++; /* Try next range */
266 return uc; /* Past last range */
270 * UniStrupr: Upper case a unicode string
272 static inline __le16 *UniStrupr(register __le16 *upin)
277 while (*up) { /* For all characters */
278 *up = cpu_to_le16(UniToupper(le16_to_cpu(*up)));
281 return upin; /* Return input pointer */
283 #endif /* UNIUPR_NOUPPER */
285 #endif /* _NLS_UCS2_UTILS_H */