2 * cifs_unicode: Unicode kernel case support
5 * Convert a unicode character to upper or lower case using
8 * Copyright (c) International Business Machines Corp., 2000,2007
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * These APIs are based on the C library functions. The semantics
27 * should match the C functions but with expanded size operands.
29 * The upper/lower functions are based on a table created by mkupr.
30 * This is a compressed table of upper and lower case conversion.
34 #include <asm/byteorder.h>
35 #include <linux/types.h>
36 #include <linux/nls.h>
38 #define UNIUPR_NOLOWER /* Example to not expand lower case tables */
41 * Windows maps these to the user defined 16 bit Unicode range since they are
42 * reserved symbols (along with \ and /), otherwise illegal to store
43 * in filenames in NTFS
45 #define UNI_ASTERIK (__u16) ('*' + 0xF000)
46 #define UNI_QUESTION (__u16) ('?' + 0xF000)
47 #define UNI_COLON (__u16) (':' + 0xF000)
48 #define UNI_GRTRTHAN (__u16) ('>' + 0xF000)
49 #define UNI_LESSTHAN (__u16) ('<' + 0xF000)
50 #define UNI_PIPE (__u16) ('|' + 0xF000)
51 #define UNI_SLASH (__u16) ('\\' + 0xF000)
53 /* Just define what we want from uniupr.h. We don't want to define the tables
54 * in each source file.
56 #ifndef UNICASERANGE_DEFINED
62 #endif /* UNICASERANGE_DEFINED */
64 #ifndef UNIUPR_NOUPPER
65 extern signed char CifsUniUpperTable[512];
66 extern const struct UniCaseRange CifsUniUpperRange[];
67 #endif /* UNIUPR_NOUPPER */
69 #ifndef UNIUPR_NOLOWER
70 extern signed char UniLowerTable[512];
71 extern struct UniCaseRange UniLowerRange[];
72 #endif /* UNIUPR_NOLOWER */
75 int cifs_from_ucs2(char *to, const __le16 *from, int tolen, int fromlen,
76 const struct nls_table *codepage, bool mapchar);
77 int cifs_strfromUCS_le(char *, const __le16 *, int, const struct nls_table *);
78 int cifs_strtoUCS(__le16 *, const char *, int, const struct nls_table *);
82 * UniStrcat: Concatenate the second string to the first
85 * Address of the first string
87 static inline wchar_t *
88 UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
90 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
92 while (*ucs1++) ; /* To end of first string */
93 ucs1--; /* Return to the null */
94 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
99 * UniStrchr: Find a character in a string
102 * Address of first occurrence of character in string
103 * or NULL if the character is not in the string
105 static inline wchar_t *
106 UniStrchr(const wchar_t *ucs, wchar_t uc)
108 while ((*ucs != uc) && *ucs)
112 return (wchar_t *) ucs;
117 * UniStrcmp: Compare two strings
120 * < 0: First string is less than second
121 * = 0: Strings are equal
122 * > 0: First string is greater than second
125 UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
127 while ((*ucs1 == *ucs2) && *ucs1) {
131 return (int) *ucs1 - (int) *ucs2;
135 * UniStrcpy: Copy a string
137 static inline wchar_t *
138 UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
140 wchar_t *anchor = ucs1; /* save the start of result string */
142 while ((*ucs1++ = *ucs2++)) ;
147 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
150 UniStrlen(const wchar_t *ucs1)
160 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
161 * string (length limited)
164 UniStrnlen(const wchar_t *ucs1, int maxlen)
177 * UniStrncat: Concatenate length limited string
179 static inline wchar_t *
180 UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
182 wchar_t *anchor = ucs1; /* save pointer to string 1 */
185 ucs1--; /* point to null terminator of s1 */
186 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
190 *ucs1 = 0; /* Null terminate the result */
195 * UniStrncmp: Compare length limited string
198 UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
201 return 0; /* Null strings are equal */
202 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
206 return (int) *ucs1 - (int) *ucs2;
210 * UniStrncmp_le: Compare length limited string - native to little-endian
213 UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
216 return 0; /* Null strings are equal */
217 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
221 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
225 * UniStrncpy: Copy length limited string with pad
227 static inline wchar_t *
228 UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
230 wchar_t *anchor = ucs1;
232 while (n-- && *ucs2) /* Copy the strings */
236 while (n--) /* Pad with nulls */
242 * UniStrncpy_le: Copy length limited string with pad to little-endian
244 static inline wchar_t *
245 UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
247 wchar_t *anchor = ucs1;
249 while (n-- && *ucs2) /* Copy the strings */
250 *ucs1++ = __le16_to_cpu(*ucs2++);
253 while (n--) /* Pad with nulls */
259 * UniStrstr: Find a string in a string
262 * Address of first match found
263 * NULL if no matching string is found
265 static inline wchar_t *
266 UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
268 const wchar_t *anchor1 = ucs1;
269 const wchar_t *anchor2 = ucs2;
272 if (*ucs1 == *ucs2) {
273 /* Partial match found */
277 if (!*ucs2) /* Match found */
278 return (wchar_t *) anchor1;
279 ucs1 = ++anchor1; /* No match */
284 if (!*ucs2) /* Both end together */
285 return (wchar_t *) anchor1; /* Match found */
286 return NULL; /* No match */
289 #ifndef UNIUPR_NOUPPER
291 * UniToupper: Convert a unicode character to upper case
293 static inline wchar_t
294 UniToupper(register wchar_t uc)
296 register const struct UniCaseRange *rp;
298 if (uc < sizeof(CifsUniUpperTable)) {
299 /* Latin characters */
300 return uc + CifsUniUpperTable[uc]; /* Use base tables */
302 rp = CifsUniUpperRange; /* Use range tables */
304 if (uc < rp->start) /* Before start of range */
305 return uc; /* Uppercase = input */
306 if (uc <= rp->end) /* In range */
307 return uc + rp->table[uc - rp->start];
308 rp++; /* Try next range */
311 return uc; /* Past last range */
315 * UniStrupr: Upper case a unicode string
317 static inline wchar_t *
318 UniStrupr(register wchar_t *upin)
320 register wchar_t *up;
323 while (*up) { /* For all characters */
324 *up = UniToupper(*up);
327 return upin; /* Return input pointer */
329 #endif /* UNIUPR_NOUPPER */
331 #ifndef UNIUPR_NOLOWER
333 * UniTolower: Convert a unicode character to lower case
335 static inline wchar_t
336 UniTolower(wchar_t uc)
338 register struct UniCaseRange *rp;
340 if (uc < sizeof(UniLowerTable)) {
341 /* Latin characters */
342 return uc + UniLowerTable[uc]; /* Use base tables */
344 rp = UniLowerRange; /* Use range tables */
346 if (uc < rp->start) /* Before start of range */
347 return uc; /* Uppercase = input */
348 if (uc <= rp->end) /* In range */
349 return uc + rp->table[uc - rp->start];
350 rp++; /* Try next range */
353 return uc; /* Past last range */
357 * UniStrlwr: Lower case a unicode string
359 static inline wchar_t *
360 UniStrlwr(register wchar_t *upin)
362 register wchar_t *up;
365 while (*up) { /* For all characters */
366 *up = UniTolower(*up);
369 return upin; /* Return input pointer */