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.
33 #ifndef _CIFS_UNICODE_H
34 #define _CIFS_UNICODE_H
36 #include <asm/byteorder.h>
37 #include <linux/types.h>
38 #include <linux/nls.h>
40 #define UNIUPR_NOLOWER /* Example to not expand lower case tables */
43 * Windows maps these to the user defined 16 bit Unicode range since they are
44 * reserved symbols (along with \ and /), otherwise illegal to store
45 * in filenames in NTFS
47 #define UNI_ASTERISK (__u16) ('*' + 0xF000)
48 #define UNI_QUESTION (__u16) ('?' + 0xF000)
49 #define UNI_COLON (__u16) (':' + 0xF000)
50 #define UNI_GRTRTHAN (__u16) ('>' + 0xF000)
51 #define UNI_LESSTHAN (__u16) ('<' + 0xF000)
52 #define UNI_PIPE (__u16) ('|' + 0xF000)
53 #define UNI_SLASH (__u16) ('\\' + 0xF000)
56 * Macs use an older "SFM" mapping of the symbols above. Fortunately it does
57 * not conflict (although almost does) with the mapping above.
60 #define SFM_ASTERISK ((__u16) 0xF021)
61 #define SFM_QUESTION ((__u16) 0xF025)
62 #define SFM_COLON ((__u16) 0xF022)
63 #define SFM_GRTRTHAN ((__u16) 0xF024)
64 #define SFM_LESSTHAN ((__u16) 0xF023)
65 #define SFM_PIPE ((__u16) 0xF027)
66 #define SFM_SLASH ((__u16) 0xF026)
67 #define SFM_PERIOD ((__u16) 0xF028)
68 #define SFM_SPACE ((__u16) 0xF029)
71 * Mapping mechanism to use when one of the seven reserved characters is
72 * encountered. We can only map using one of the mechanisms at a time
73 * since otherwise readdir could return directory entries which we would
76 * NO_MAP_UNI_RSVD = do not perform any remapping of the character
77 * SFM_MAP_UNI_RSVD = map reserved characters using SFM scheme (MAC compatible)
78 * SFU_MAP_UNI_RSVD = map reserved characters ala SFU ("mapchars" option)
81 #define NO_MAP_UNI_RSVD 0
82 #define SFM_MAP_UNI_RSVD 1
83 #define SFU_MAP_UNI_RSVD 2
85 /* Just define what we want from uniupr.h. We don't want to define the tables
86 * in each source file.
88 #ifndef UNICASERANGE_DEFINED
94 #endif /* UNICASERANGE_DEFINED */
96 #ifndef UNIUPR_NOUPPER
97 extern signed char CifsUniUpperTable[512];
98 extern const struct UniCaseRange CifsUniUpperRange[];
99 #endif /* UNIUPR_NOUPPER */
101 #ifndef UNIUPR_NOLOWER
102 extern signed char CifsUniLowerTable[512];
103 extern const struct UniCaseRange CifsUniLowerRange[];
104 #endif /* UNIUPR_NOLOWER */
107 int cifs_from_utf16(char *to, const __le16 *from, int tolen, int fromlen,
108 const struct nls_table *cp, int map_type);
109 int cifs_utf16_bytes(const __le16 *from, int maxbytes,
110 const struct nls_table *codepage);
111 int cifs_strtoUTF16(__le16 *, const char *, int, const struct nls_table *);
112 char *cifs_strndup_from_utf16(const char *src, const int maxlen,
113 const bool is_unicode,
114 const struct nls_table *codepage);
115 extern int cifsConvertToUTF16(__le16 *target, const char *source, int maxlen,
116 const struct nls_table *cp, int mapChars);
117 extern int cifs_remap(struct cifs_sb_info *cifs_sb);
118 #ifdef CONFIG_CIFS_SMB2
119 extern __le16 *cifs_strndup_to_utf16(const char *src, const int maxlen,
120 int *utf16_len, const struct nls_table *cp,
122 #endif /* CONFIG_CIFS_SMB2 */
125 wchar_t cifs_toupper(wchar_t in);
128 * UniStrcat: Concatenate the second string to the first
131 * Address of the first string
133 static inline __le16 *
134 UniStrcat(__le16 *ucs1, const __le16 *ucs2)
136 __le16 *anchor = ucs1; /* save a pointer to start of ucs1 */
138 while (*ucs1++) ; /* To end of first string */
139 ucs1--; /* Return to the null */
140 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
145 * UniStrchr: Find a character in a string
148 * Address of first occurrence of character in string
149 * or NULL if the character is not in the string
151 static inline wchar_t *
152 UniStrchr(const wchar_t *ucs, wchar_t uc)
154 while ((*ucs != uc) && *ucs)
158 return (wchar_t *) ucs;
163 * UniStrcmp: Compare two strings
166 * < 0: First string is less than second
167 * = 0: Strings are equal
168 * > 0: First string is greater than second
171 UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
173 while ((*ucs1 == *ucs2) && *ucs1) {
177 return (int) *ucs1 - (int) *ucs2;
181 * UniStrcpy: Copy a string
183 static inline wchar_t *
184 UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
186 wchar_t *anchor = ucs1; /* save the start of result string */
188 while ((*ucs1++ = *ucs2++)) ;
193 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
196 UniStrlen(const wchar_t *ucs1)
206 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
207 * string (length limited)
210 UniStrnlen(const wchar_t *ucs1, int maxlen)
223 * UniStrncat: Concatenate length limited string
225 static inline wchar_t *
226 UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
228 wchar_t *anchor = ucs1; /* save pointer to string 1 */
231 ucs1--; /* point to null terminator of s1 */
232 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
236 *ucs1 = 0; /* Null terminate the result */
241 * UniStrncmp: Compare length limited string
244 UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
247 return 0; /* Null strings are equal */
248 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
252 return (int) *ucs1 - (int) *ucs2;
256 * UniStrncmp_le: Compare length limited string - native to little-endian
259 UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
262 return 0; /* Null strings are equal */
263 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
267 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
271 * UniStrncpy: Copy length limited string with pad
273 static inline wchar_t *
274 UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
276 wchar_t *anchor = ucs1;
278 while (n-- && *ucs2) /* Copy the strings */
282 while (n--) /* Pad with nulls */
288 * UniStrncpy_le: Copy length limited string with pad to little-endian
290 static inline wchar_t *
291 UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
293 wchar_t *anchor = ucs1;
295 while (n-- && *ucs2) /* Copy the strings */
296 *ucs1++ = __le16_to_cpu(*ucs2++);
299 while (n--) /* Pad with nulls */
305 * UniStrstr: Find a string in a string
308 * Address of first match found
309 * NULL if no matching string is found
311 static inline wchar_t *
312 UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
314 const wchar_t *anchor1 = ucs1;
315 const wchar_t *anchor2 = ucs2;
318 if (*ucs1 == *ucs2) {
319 /* Partial match found */
323 if (!*ucs2) /* Match found */
324 return (wchar_t *) anchor1;
325 ucs1 = ++anchor1; /* No match */
330 if (!*ucs2) /* Both end together */
331 return (wchar_t *) anchor1; /* Match found */
332 return NULL; /* No match */
335 #ifndef UNIUPR_NOUPPER
337 * UniToupper: Convert a unicode character to upper case
339 static inline wchar_t
340 UniToupper(register wchar_t uc)
342 register const struct UniCaseRange *rp;
344 if (uc < sizeof(CifsUniUpperTable)) {
345 /* Latin characters */
346 return uc + CifsUniUpperTable[uc]; /* Use base tables */
348 rp = CifsUniUpperRange; /* Use range tables */
350 if (uc < rp->start) /* Before start of range */
351 return uc; /* Uppercase = input */
352 if (uc <= rp->end) /* In range */
353 return uc + rp->table[uc - rp->start];
354 rp++; /* Try next range */
357 return uc; /* Past last range */
361 * UniStrupr: Upper case a unicode string
363 static inline __le16 *
364 UniStrupr(register __le16 *upin)
369 while (*up) { /* For all characters */
370 *up = cpu_to_le16(UniToupper(le16_to_cpu(*up)));
373 return upin; /* Return input pointer */
375 #endif /* UNIUPR_NOUPPER */
377 #ifndef UNIUPR_NOLOWER
379 * UniTolower: Convert a unicode character to lower case
381 static inline wchar_t
382 UniTolower(register wchar_t uc)
384 register const struct UniCaseRange *rp;
386 if (uc < sizeof(CifsUniLowerTable)) {
387 /* Latin characters */
388 return uc + CifsUniLowerTable[uc]; /* Use base tables */
390 rp = CifsUniLowerRange; /* Use range tables */
392 if (uc < rp->start) /* Before start of range */
393 return uc; /* Uppercase = input */
394 if (uc <= rp->end) /* In range */
395 return uc + rp->table[uc - rp->start];
396 rp++; /* Try next range */
399 return uc; /* Past last range */
403 * UniStrlwr: Lower case a unicode string
405 static inline wchar_t *
406 UniStrlwr(register wchar_t *upin)
408 register wchar_t *up;
411 while (*up) { /* For all characters */
412 *up = UniTolower(*up);
415 return upin; /* Return input pointer */
420 #endif /* _CIFS_UNICODE_H */