cifs_unicode.h (9e74938954749ecc3e0da63d0e211238ad4b2425) cifs_unicode.h (de54845290cee3f65dcd03b35a2bd7f2f7aed2ac)
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
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 *

--- 7 unchanged lines hidden (view full) ---

16 * This is a compressed table of upper and lower case conversion.
17 */
18#ifndef _CIFS_UNICODE_H
19#define _CIFS_UNICODE_H
20
21#include <asm/byteorder.h>
22#include <linux/types.h>
23#include <linux/nls.h>
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
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 *

--- 7 unchanged lines hidden (view full) ---

16 * This is a compressed table of upper and lower case conversion.
17 */
18#ifndef _CIFS_UNICODE_H
19#define _CIFS_UNICODE_H
20
21#include <asm/byteorder.h>
22#include <linux/types.h>
23#include <linux/nls.h>
24#include "../../nls/nls_ucs2_utils.h"
24
25/*
25
26/*
26 * Windows maps these to the user defined 16 bit Unicode range since they are
27 * reserved symbols (along with \ and /), otherwise illegal to store
28 * in filenames in NTFS
29 */
30#define UNI_ASTERISK (__u16) ('*' + 0xF000)
31#define UNI_QUESTION (__u16) ('?' + 0xF000)
32#define UNI_COLON (__u16) (':' + 0xF000)
33#define UNI_GRTRTHAN (__u16) ('>' + 0xF000)
34#define UNI_LESSTHAN (__u16) ('<' + 0xF000)
35#define UNI_PIPE (__u16) ('|' + 0xF000)
36#define UNI_SLASH (__u16) ('\\' + 0xF000)
37
38/*
39 * Macs use an older "SFM" mapping of the symbols above. Fortunately it does
40 * not conflict (although almost does) with the mapping above.
41 */
42
43#define SFM_DOUBLEQUOTE ((__u16) 0xF020)
44#define SFM_ASTERISK ((__u16) 0xF021)
45#define SFM_QUESTION ((__u16) 0xF025)
46#define SFM_COLON ((__u16) 0xF022)

--- 14 unchanged lines hidden (view full) ---

61 * SFM_MAP_UNI_RSVD = map reserved characters using SFM scheme (MAC compatible)
62 * SFU_MAP_UNI_RSVD = map reserved characters ala SFU ("mapchars" option)
63 *
64 */
65#define NO_MAP_UNI_RSVD 0
66#define SFM_MAP_UNI_RSVD 1
67#define SFU_MAP_UNI_RSVD 2
68
27 * Macs use an older "SFM" mapping of the symbols above. Fortunately it does
28 * not conflict (although almost does) with the mapping above.
29 */
30
31#define SFM_DOUBLEQUOTE ((__u16) 0xF020)
32#define SFM_ASTERISK ((__u16) 0xF021)
33#define SFM_QUESTION ((__u16) 0xF025)
34#define SFM_COLON ((__u16) 0xF022)

--- 14 unchanged lines hidden (view full) ---

49 * SFM_MAP_UNI_RSVD = map reserved characters using SFM scheme (MAC compatible)
50 * SFU_MAP_UNI_RSVD = map reserved characters ala SFU ("mapchars" option)
51 *
52 */
53#define NO_MAP_UNI_RSVD 0
54#define SFM_MAP_UNI_RSVD 1
55#define SFU_MAP_UNI_RSVD 2
56
69/* Just define what we want from uniupr.h. We don't want to define the tables
70 * in each source file.
71 */
72#ifndef UNICASERANGE_DEFINED
73struct UniCaseRange {
74 wchar_t start;
75 wchar_t end;
76 signed char *table;
77};
78#endif /* UNICASERANGE_DEFINED */
79
80#ifndef UNIUPR_NOUPPER
81extern signed char CifsUniUpperTable[512];
82extern const struct UniCaseRange CifsUniUpperRange[];
83#endif /* UNIUPR_NOUPPER */
84
85#ifdef __KERNEL__
86int cifs_from_utf16(char *to, const __le16 *from, int tolen, int fromlen,
87 const struct nls_table *cp, int map_type);
88int cifs_utf16_bytes(const __le16 *from, int maxbytes,
89 const struct nls_table *codepage);
90int cifs_strtoUTF16(__le16 *, const char *, int, const struct nls_table *);
91char *cifs_strndup_from_utf16(const char *src, const int maxlen,
92 const bool is_unicode,
93 const struct nls_table *codepage);
94extern int cifsConvertToUTF16(__le16 *target, const char *source, int maxlen,
95 const struct nls_table *cp, int mapChars);
96extern int cifs_remap(struct cifs_sb_info *cifs_sb);
97extern __le16 *cifs_strndup_to_utf16(const char *src, const int maxlen,
98 int *utf16_len, const struct nls_table *cp,
99 int remap);
100#endif
101
102wchar_t cifs_toupper(wchar_t in);
103
57#ifdef __KERNEL__
58int cifs_from_utf16(char *to, const __le16 *from, int tolen, int fromlen,
59 const struct nls_table *cp, int map_type);
60int cifs_utf16_bytes(const __le16 *from, int maxbytes,
61 const struct nls_table *codepage);
62int cifs_strtoUTF16(__le16 *, const char *, int, const struct nls_table *);
63char *cifs_strndup_from_utf16(const char *src, const int maxlen,
64 const bool is_unicode,
65 const struct nls_table *codepage);
66extern int cifsConvertToUTF16(__le16 *target, const char *source, int maxlen,
67 const struct nls_table *cp, int mapChars);
68extern int cifs_remap(struct cifs_sb_info *cifs_sb);
69extern __le16 *cifs_strndup_to_utf16(const char *src, const int maxlen,
70 int *utf16_len, const struct nls_table *cp,
71 int remap);
72#endif
73
74wchar_t cifs_toupper(wchar_t in);
75
104/*
105 * UniStrcat: Concatenate the second string to the first
106 *
107 * Returns:
108 * Address of the first string
109 */
110static inline __le16 *
111UniStrcat(__le16 *ucs1, const __le16 *ucs2)
112{
113 __le16 *anchor = ucs1; /* save a pointer to start of ucs1 */
114
115 while (*ucs1++) ; /* To end of first string */
116 ucs1--; /* Return to the null */
117 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
118 return anchor;
119}
120
121/*
122 * UniStrchr: Find a character in a string
123 *
124 * Returns:
125 * Address of first occurrence of character in string
126 * or NULL if the character is not in the string
127 */
128static inline wchar_t *
129UniStrchr(const wchar_t *ucs, wchar_t uc)
130{
131 while ((*ucs != uc) && *ucs)
132 ucs++;
133
134 if (*ucs == uc)
135 return (wchar_t *) ucs;
136 return NULL;
137}
138
139/*
140 * UniStrcmp: Compare two strings
141 *
142 * Returns:
143 * < 0: First string is less than second
144 * = 0: Strings are equal
145 * > 0: First string is greater than second
146 */
147static inline int
148UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
149{
150 while ((*ucs1 == *ucs2) && *ucs1) {
151 ucs1++;
152 ucs2++;
153 }
154 return (int) *ucs1 - (int) *ucs2;
155}
156
157/*
158 * UniStrcpy: Copy a string
159 */
160static inline wchar_t *
161UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
162{
163 wchar_t *anchor = ucs1; /* save the start of result string */
164
165 while ((*ucs1++ = *ucs2++)) ;
166 return anchor;
167}
168
169/*
170 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
171 */
172static inline size_t
173UniStrlen(const wchar_t *ucs1)
174{
175 int i = 0;
176
177 while (*ucs1++)
178 i++;
179 return i;
180}
181
182/*
183 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
184 * string (length limited)
185 */
186static inline size_t
187UniStrnlen(const wchar_t *ucs1, int maxlen)
188{
189 int i = 0;
190
191 while (*ucs1++) {
192 i++;
193 if (i >= maxlen)
194 break;
195 }
196 return i;
197}
198
199/*
200 * UniStrncat: Concatenate length limited string
201 */
202static inline wchar_t *
203UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
204{
205 wchar_t *anchor = ucs1; /* save pointer to string 1 */
206
207 while (*ucs1++) ;
208 ucs1--; /* point to null terminator of s1 */
209 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
210 ucs1++;
211 ucs2++;
212 }
213 *ucs1 = 0; /* Null terminate the result */
214 return (anchor);
215}
216
217/*
218 * UniStrncmp: Compare length limited string
219 */
220static inline int
221UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
222{
223 if (!n)
224 return 0; /* Null strings are equal */
225 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
226 ucs1++;
227 ucs2++;
228 }
229 return (int) *ucs1 - (int) *ucs2;
230}
231
232/*
233 * UniStrncmp_le: Compare length limited string - native to little-endian
234 */
235static inline int
236UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
237{
238 if (!n)
239 return 0; /* Null strings are equal */
240 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
241 ucs1++;
242 ucs2++;
243 }
244 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
245}
246
247/*
248 * UniStrncpy: Copy length limited string with pad
249 */
250static inline wchar_t *
251UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
252{
253 wchar_t *anchor = ucs1;
254
255 while (n-- && *ucs2) /* Copy the strings */
256 *ucs1++ = *ucs2++;
257
258 n++;
259 while (n--) /* Pad with nulls */
260 *ucs1++ = 0;
261 return anchor;
262}
263
264/*
265 * UniStrncpy_le: Copy length limited string with pad to little-endian
266 */
267static inline wchar_t *
268UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
269{
270 wchar_t *anchor = ucs1;
271
272 while (n-- && *ucs2) /* Copy the strings */
273 *ucs1++ = __le16_to_cpu(*ucs2++);
274
275 n++;
276 while (n--) /* Pad with nulls */
277 *ucs1++ = 0;
278 return anchor;
279}
280
281/*
282 * UniStrstr: Find a string in a string
283 *
284 * Returns:
285 * Address of first match found
286 * NULL if no matching string is found
287 */
288static inline wchar_t *
289UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
290{
291 const wchar_t *anchor1 = ucs1;
292 const wchar_t *anchor2 = ucs2;
293
294 while (*ucs1) {
295 if (*ucs1 == *ucs2) {
296 /* Partial match found */
297 ucs1++;
298 ucs2++;
299 } else {
300 if (!*ucs2) /* Match found */
301 return (wchar_t *) anchor1;
302 ucs1 = ++anchor1; /* No match */
303 ucs2 = anchor2;
304 }
305 }
306
307 if (!*ucs2) /* Both end together */
308 return (wchar_t *) anchor1; /* Match found */
309 return NULL; /* No match */
310}
311
312#ifndef UNIUPR_NOUPPER
313/*
314 * UniToupper: Convert a unicode character to upper case
315 */
316static inline wchar_t
317UniToupper(register wchar_t uc)
318{
319 register const struct UniCaseRange *rp;
320
321 if (uc < sizeof(CifsUniUpperTable)) {
322 /* Latin characters */
323 return uc + CifsUniUpperTable[uc]; /* Use base tables */
324 } else {
325 rp = CifsUniUpperRange; /* Use range tables */
326 while (rp->start) {
327 if (uc < rp->start) /* Before start of range */
328 return uc; /* Uppercase = input */
329 if (uc <= rp->end) /* In range */
330 return uc + rp->table[uc - rp->start];
331 rp++; /* Try next range */
332 }
333 }
334 return uc; /* Past last range */
335}
336
337/*
338 * UniStrupr: Upper case a unicode string
339 */
340static inline __le16 *
341UniStrupr(register __le16 *upin)
342{
343 register __le16 *up;
344
345 up = upin;
346 while (*up) { /* For all characters */
347 *up = cpu_to_le16(UniToupper(le16_to_cpu(*up)));
348 up++;
349 }
350 return upin; /* Return input pointer */
351}
352#endif /* UNIUPR_NOUPPER */
353
354#endif /* _CIFS_UNICODE_H */
76#endif /* _CIFS_UNICODE_H */