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,2009
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 *);
81 char *cifs_strndup_from_ucs(const char *src, const int maxlen,
82 const bool is_unicode,
83 const struct nls_table *codepage);
87 * UniStrcat: Concatenate the second string to the first
90 * Address of the first string
92 static inline wchar_t *
93 UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
95 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
97 while (*ucs1++) ; /* To end of first string */
98 ucs1--; /* Return to the null */
99 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
104 * UniStrchr: Find a character in a string
107 * Address of first occurrence of character in string
108 * or NULL if the character is not in the string
110 static inline wchar_t *
111 UniStrchr(const wchar_t *ucs, wchar_t uc)
113 while ((*ucs != uc) && *ucs)
117 return (wchar_t *) ucs;
122 * UniStrcmp: Compare two strings
125 * < 0: First string is less than second
126 * = 0: Strings are equal
127 * > 0: First string is greater than second
130 UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
132 while ((*ucs1 == *ucs2) && *ucs1) {
136 return (int) *ucs1 - (int) *ucs2;
140 * UniStrcpy: Copy a string
142 static inline wchar_t *
143 UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
145 wchar_t *anchor = ucs1; /* save the start of result string */
147 while ((*ucs1++ = *ucs2++)) ;
152 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
155 UniStrlen(const wchar_t *ucs1)
165 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
166 * string (length limited)
169 UniStrnlen(const wchar_t *ucs1, int maxlen)
182 * UniStrncat: Concatenate length limited string
184 static inline wchar_t *
185 UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
187 wchar_t *anchor = ucs1; /* save pointer to string 1 */
190 ucs1--; /* point to null terminator of s1 */
191 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
195 *ucs1 = 0; /* Null terminate the result */
200 * UniStrncmp: Compare length limited string
203 UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
206 return 0; /* Null strings are equal */
207 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
211 return (int) *ucs1 - (int) *ucs2;
215 * UniStrncmp_le: Compare length limited string - native to little-endian
218 UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
221 return 0; /* Null strings are equal */
222 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
226 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
230 * UniStrncpy: Copy length limited string with pad
232 static inline wchar_t *
233 UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
235 wchar_t *anchor = ucs1;
237 while (n-- && *ucs2) /* Copy the strings */
241 while (n--) /* Pad with nulls */
247 * UniStrncpy_le: Copy length limited string with pad to little-endian
249 static inline wchar_t *
250 UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
252 wchar_t *anchor = ucs1;
254 while (n-- && *ucs2) /* Copy the strings */
255 *ucs1++ = __le16_to_cpu(*ucs2++);
258 while (n--) /* Pad with nulls */
264 * UniStrstr: Find a string in a string
267 * Address of first match found
268 * NULL if no matching string is found
270 static inline wchar_t *
271 UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
273 const wchar_t *anchor1 = ucs1;
274 const wchar_t *anchor2 = ucs2;
277 if (*ucs1 == *ucs2) {
278 /* Partial match found */
282 if (!*ucs2) /* Match found */
283 return (wchar_t *) anchor1;
284 ucs1 = ++anchor1; /* No match */
289 if (!*ucs2) /* Both end together */
290 return (wchar_t *) anchor1; /* Match found */
291 return NULL; /* No match */
294 #ifndef UNIUPR_NOUPPER
296 * UniToupper: Convert a unicode character to upper case
298 static inline wchar_t
299 UniToupper(register wchar_t uc)
301 register const struct UniCaseRange *rp;
303 if (uc < sizeof(CifsUniUpperTable)) {
304 /* Latin characters */
305 return uc + CifsUniUpperTable[uc]; /* Use base tables */
307 rp = CifsUniUpperRange; /* Use range tables */
309 if (uc < rp->start) /* Before start of range */
310 return uc; /* Uppercase = input */
311 if (uc <= rp->end) /* In range */
312 return uc + rp->table[uc - rp->start];
313 rp++; /* Try next range */
316 return uc; /* Past last range */
320 * UniStrupr: Upper case a unicode string
322 static inline wchar_t *
323 UniStrupr(register wchar_t *upin)
325 register wchar_t *up;
328 while (*up) { /* For all characters */
329 *up = UniToupper(*up);
332 return upin; /* Return input pointer */
334 #endif /* UNIUPR_NOUPPER */
336 #ifndef UNIUPR_NOLOWER
338 * UniTolower: Convert a unicode character to lower case
340 static inline wchar_t
341 UniTolower(wchar_t uc)
343 register struct UniCaseRange *rp;
345 if (uc < sizeof(UniLowerTable)) {
346 /* Latin characters */
347 return uc + UniLowerTable[uc]; /* Use base tables */
349 rp = UniLowerRange; /* Use range tables */
351 if (uc < rp->start) /* Before start of range */
352 return uc; /* Uppercase = input */
353 if (uc <= rp->end) /* In range */
354 return uc + rp->table[uc - rp->start];
355 rp++; /* Try next range */
358 return uc; /* Past last range */
362 * UniStrlwr: Lower case a unicode string
364 static inline wchar_t *
365 UniStrlwr(register wchar_t *upin)
367 register wchar_t *up;
370 while (*up) { /* For all characters */
371 *up = UniTolower(*up);
374 return upin; /* Return input pointer */