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_ucs2_bytes(const __le16 *from, int maxbytes,
78 const struct nls_table *codepage);
79 int cifs_strfromUCS_le(char *, const __le16 *, int, const struct nls_table *);
80 int cifs_strtoUCS(__le16 *, const char *, int, const struct nls_table *);
84 * UniStrcat: Concatenate the second string to the first
87 * Address of the first string
89 static inline wchar_t *
90 UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
92 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
94 while (*ucs1++) ; /* To end of first string */
95 ucs1--; /* Return to the null */
96 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
101 * UniStrchr: Find a character in a string
104 * Address of first occurrence of character in string
105 * or NULL if the character is not in the string
107 static inline wchar_t *
108 UniStrchr(const wchar_t *ucs, wchar_t uc)
110 while ((*ucs != uc) && *ucs)
114 return (wchar_t *) ucs;
119 * UniStrcmp: Compare two strings
122 * < 0: First string is less than second
123 * = 0: Strings are equal
124 * > 0: First string is greater than second
127 UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
129 while ((*ucs1 == *ucs2) && *ucs1) {
133 return (int) *ucs1 - (int) *ucs2;
137 * UniStrcpy: Copy a string
139 static inline wchar_t *
140 UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
142 wchar_t *anchor = ucs1; /* save the start of result string */
144 while ((*ucs1++ = *ucs2++)) ;
149 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
152 UniStrlen(const wchar_t *ucs1)
162 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
163 * string (length limited)
166 UniStrnlen(const wchar_t *ucs1, int maxlen)
179 * UniStrncat: Concatenate length limited string
181 static inline wchar_t *
182 UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
184 wchar_t *anchor = ucs1; /* save pointer to string 1 */
187 ucs1--; /* point to null terminator of s1 */
188 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
192 *ucs1 = 0; /* Null terminate the result */
197 * UniStrncmp: Compare length limited string
200 UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
203 return 0; /* Null strings are equal */
204 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
208 return (int) *ucs1 - (int) *ucs2;
212 * UniStrncmp_le: Compare length limited string - native to little-endian
215 UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
218 return 0; /* Null strings are equal */
219 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
223 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
227 * UniStrncpy: Copy length limited string with pad
229 static inline wchar_t *
230 UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
232 wchar_t *anchor = ucs1;
234 while (n-- && *ucs2) /* Copy the strings */
238 while (n--) /* Pad with nulls */
244 * UniStrncpy_le: Copy length limited string with pad to little-endian
246 static inline wchar_t *
247 UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
249 wchar_t *anchor = ucs1;
251 while (n-- && *ucs2) /* Copy the strings */
252 *ucs1++ = __le16_to_cpu(*ucs2++);
255 while (n--) /* Pad with nulls */
261 * UniStrstr: Find a string in a string
264 * Address of first match found
265 * NULL if no matching string is found
267 static inline wchar_t *
268 UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
270 const wchar_t *anchor1 = ucs1;
271 const wchar_t *anchor2 = ucs2;
274 if (*ucs1 == *ucs2) {
275 /* Partial match found */
279 if (!*ucs2) /* Match found */
280 return (wchar_t *) anchor1;
281 ucs1 = ++anchor1; /* No match */
286 if (!*ucs2) /* Both end together */
287 return (wchar_t *) anchor1; /* Match found */
288 return NULL; /* No match */
291 #ifndef UNIUPR_NOUPPER
293 * UniToupper: Convert a unicode character to upper case
295 static inline wchar_t
296 UniToupper(register wchar_t uc)
298 register const struct UniCaseRange *rp;
300 if (uc < sizeof(CifsUniUpperTable)) {
301 /* Latin characters */
302 return uc + CifsUniUpperTable[uc]; /* Use base tables */
304 rp = CifsUniUpperRange; /* Use range tables */
306 if (uc < rp->start) /* Before start of range */
307 return uc; /* Uppercase = input */
308 if (uc <= rp->end) /* In range */
309 return uc + rp->table[uc - rp->start];
310 rp++; /* Try next range */
313 return uc; /* Past last range */
317 * UniStrupr: Upper case a unicode string
319 static inline wchar_t *
320 UniStrupr(register wchar_t *upin)
322 register wchar_t *up;
325 while (*up) { /* For all characters */
326 *up = UniToupper(*up);
329 return upin; /* Return input pointer */
331 #endif /* UNIUPR_NOUPPER */
333 #ifndef UNIUPR_NOLOWER
335 * UniTolower: Convert a unicode character to lower case
337 static inline wchar_t
338 UniTolower(wchar_t uc)
340 register struct UniCaseRange *rp;
342 if (uc < sizeof(UniLowerTable)) {
343 /* Latin characters */
344 return uc + UniLowerTable[uc]; /* Use base tables */
346 rp = UniLowerRange; /* Use range tables */
348 if (uc < rp->start) /* Before start of range */
349 return uc; /* Uppercase = input */
350 if (uc <= rp->end) /* In range */
351 return uc + rp->table[uc - rp->start];
352 rp++; /* Try next range */
355 return uc; /* Past last range */
359 * UniStrlwr: Lower case a unicode string
361 static inline wchar_t *
362 UniStrlwr(register wchar_t *upin)
364 register wchar_t *up;
367 while (*up) { /* For all characters */
368 *up = UniTolower(*up);
371 return upin; /* Return input pointer */