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)
55 /* Just define what we want from uniupr.h. We don't want to define the tables
56 * in each source file.
58 #ifndef UNICASERANGE_DEFINED
64 #endif /* UNICASERANGE_DEFINED */
66 #ifndef UNIUPR_NOUPPER
67 extern signed char CifsUniUpperTable
[512];
68 extern const struct UniCaseRange CifsUniUpperRange
[];
69 #endif /* UNIUPR_NOUPPER */
71 #ifndef UNIUPR_NOLOWER
72 extern signed char CifsUniLowerTable
[512];
73 extern const struct UniCaseRange CifsUniLowerRange
[];
74 #endif /* UNIUPR_NOLOWER */
77 int cifs_from_utf16(char *to
, const __le16
*from
, int tolen
, int fromlen
,
78 const struct nls_table
*codepage
, bool mapchar
);
79 int cifs_utf16_bytes(const __le16
*from
, int maxbytes
,
80 const struct nls_table
*codepage
);
81 int cifs_strtoUTF16(__le16
*, const char *, int, const struct nls_table
*);
82 char *cifs_strndup_from_utf16(const char *src
, const int maxlen
,
83 const bool is_unicode
,
84 const struct nls_table
*codepage
);
85 extern int cifsConvertToUTF16(__le16
*target
, const char *source
, int maxlen
,
86 const struct nls_table
*cp
, int mapChars
);
87 #ifdef CONFIG_CIFS_SMB2
88 extern __le16
*cifs_strndup_to_utf16(const char *src
, const int maxlen
,
89 int *utf16_len
, const struct nls_table
*cp
,
91 #endif /* CONFIG_CIFS_SMB2 */
94 wchar_t cifs_toupper(wchar_t in
);
97 * UniStrcat: Concatenate the second string to the first
100 * Address of the first string
102 static inline wchar_t *
103 UniStrcat(wchar_t *ucs1
, const wchar_t *ucs2
)
105 wchar_t *anchor
= ucs1
; /* save a pointer to start of ucs1 */
107 while (*ucs1
++) ; /* To end of first string */
108 ucs1
--; /* Return to the null */
109 while ((*ucs1
++ = *ucs2
++)) ; /* copy string 2 over */
114 * UniStrchr: Find a character in a string
117 * Address of first occurrence of character in string
118 * or NULL if the character is not in the string
120 static inline wchar_t *
121 UniStrchr(const wchar_t *ucs
, wchar_t uc
)
123 while ((*ucs
!= uc
) && *ucs
)
127 return (wchar_t *) ucs
;
132 * UniStrcmp: Compare two strings
135 * < 0: First string is less than second
136 * = 0: Strings are equal
137 * > 0: First string is greater than second
140 UniStrcmp(const wchar_t *ucs1
, const wchar_t *ucs2
)
142 while ((*ucs1
== *ucs2
) && *ucs1
) {
146 return (int) *ucs1
- (int) *ucs2
;
150 * UniStrcpy: Copy a string
152 static inline wchar_t *
153 UniStrcpy(wchar_t *ucs1
, const wchar_t *ucs2
)
155 wchar_t *anchor
= ucs1
; /* save the start of result string */
157 while ((*ucs1
++ = *ucs2
++)) ;
162 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
165 UniStrlen(const wchar_t *ucs1
)
175 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
176 * string (length limited)
179 UniStrnlen(const wchar_t *ucs1
, int maxlen
)
192 * UniStrncat: Concatenate length limited string
194 static inline wchar_t *
195 UniStrncat(wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
197 wchar_t *anchor
= ucs1
; /* save pointer to string 1 */
200 ucs1
--; /* point to null terminator of s1 */
201 while (n
-- && (*ucs1
= *ucs2
)) { /* copy s2 after s1 */
205 *ucs1
= 0; /* Null terminate the result */
210 * UniStrncmp: Compare length limited string
213 UniStrncmp(const wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
216 return 0; /* Null strings are equal */
217 while ((*ucs1
== *ucs2
) && *ucs1
&& --n
) {
221 return (int) *ucs1
- (int) *ucs2
;
225 * UniStrncmp_le: Compare length limited string - native to little-endian
228 UniStrncmp_le(const wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
231 return 0; /* Null strings are equal */
232 while ((*ucs1
== __le16_to_cpu(*ucs2
)) && *ucs1
&& --n
) {
236 return (int) *ucs1
- (int) __le16_to_cpu(*ucs2
);
240 * UniStrncpy: Copy length limited string with pad
242 static inline wchar_t *
243 UniStrncpy(wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
245 wchar_t *anchor
= ucs1
;
247 while (n
-- && *ucs2
) /* Copy the strings */
251 while (n
--) /* Pad with nulls */
257 * UniStrncpy_le: Copy length limited string with pad to little-endian
259 static inline wchar_t *
260 UniStrncpy_le(wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
262 wchar_t *anchor
= ucs1
;
264 while (n
-- && *ucs2
) /* Copy the strings */
265 *ucs1
++ = __le16_to_cpu(*ucs2
++);
268 while (n
--) /* Pad with nulls */
274 * UniStrstr: Find a string in a string
277 * Address of first match found
278 * NULL if no matching string is found
280 static inline wchar_t *
281 UniStrstr(const wchar_t *ucs1
, const wchar_t *ucs2
)
283 const wchar_t *anchor1
= ucs1
;
284 const wchar_t *anchor2
= ucs2
;
287 if (*ucs1
== *ucs2
) {
288 /* Partial match found */
292 if (!*ucs2
) /* Match found */
293 return (wchar_t *) anchor1
;
294 ucs1
= ++anchor1
; /* No match */
299 if (!*ucs2
) /* Both end together */
300 return (wchar_t *) anchor1
; /* Match found */
301 return NULL
; /* No match */
304 #ifndef UNIUPR_NOUPPER
306 * UniToupper: Convert a unicode character to upper case
308 static inline wchar_t
309 UniToupper(register wchar_t uc
)
311 register const struct UniCaseRange
*rp
;
313 if (uc
< sizeof(CifsUniUpperTable
)) {
314 /* Latin characters */
315 return uc
+ CifsUniUpperTable
[uc
]; /* Use base tables */
317 rp
= CifsUniUpperRange
; /* Use range tables */
319 if (uc
< rp
->start
) /* Before start of range */
320 return uc
; /* Uppercase = input */
321 if (uc
<= rp
->end
) /* In range */
322 return uc
+ rp
->table
[uc
- rp
->start
];
323 rp
++; /* Try next range */
326 return uc
; /* Past last range */
330 * UniStrupr: Upper case a unicode string
332 static inline __le16
*
333 UniStrupr(register __le16
*upin
)
338 while (*up
) { /* For all characters */
339 *up
= cpu_to_le16(UniToupper(le16_to_cpu(*up
)));
342 return upin
; /* Return input pointer */
344 #endif /* UNIUPR_NOUPPER */
346 #ifndef UNIUPR_NOLOWER
348 * UniTolower: Convert a unicode character to lower case
350 static inline wchar_t
351 UniTolower(register wchar_t uc
)
353 register const struct UniCaseRange
*rp
;
355 if (uc
< sizeof(CifsUniLowerTable
)) {
356 /* Latin characters */
357 return uc
+ CifsUniLowerTable
[uc
]; /* Use base tables */
359 rp
= CifsUniLowerRange
; /* Use range tables */
361 if (uc
< rp
->start
) /* Before start of range */
362 return uc
; /* Uppercase = input */
363 if (uc
<= rp
->end
) /* In range */
364 return uc
+ rp
->table
[uc
- rp
->start
];
365 rp
++; /* Try next range */
368 return uc
; /* Past last range */
372 * UniStrlwr: Lower case a unicode string
374 static inline wchar_t *
375 UniStrlwr(register wchar_t *upin
)
377 register wchar_t *up
;
380 while (*up
) { /* For all characters */
381 *up
= UniTolower(*up
);
384 return upin
; /* Return input pointer */
389 #endif /* _CIFS_UNICODE_H */