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_ASTERIK (__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_ucs2(char *to
, const __le16
*from
, int tolen
, int fromlen
,
78 const struct nls_table
*codepage
, bool mapchar
);
79 int cifs_ucs2_bytes(const __le16
*from
, int maxbytes
,
80 const struct nls_table
*codepage
);
81 int cifs_strtoUCS(__le16
*, const char *, int, const struct nls_table
*);
82 char *cifs_strndup_from_ucs(const char *src
, const int maxlen
,
83 const bool is_unicode
,
84 const struct nls_table
*codepage
);
88 * UniStrcat: Concatenate the second string to the first
91 * Address of the first string
93 static inline wchar_t *
94 UniStrcat(wchar_t *ucs1
, const wchar_t *ucs2
)
96 wchar_t *anchor
= ucs1
; /* save a pointer to start of ucs1 */
98 while (*ucs1
++) ; /* To end of first string */
99 ucs1
--; /* Return to the null */
100 while ((*ucs1
++ = *ucs2
++)) ; /* copy string 2 over */
105 * UniStrchr: Find a character in a string
108 * Address of first occurrence of character in string
109 * or NULL if the character is not in the string
111 static inline wchar_t *
112 UniStrchr(const wchar_t *ucs
, wchar_t uc
)
114 while ((*ucs
!= uc
) && *ucs
)
118 return (wchar_t *) ucs
;
123 * UniStrcmp: Compare two strings
126 * < 0: First string is less than second
127 * = 0: Strings are equal
128 * > 0: First string is greater than second
131 UniStrcmp(const wchar_t *ucs1
, const wchar_t *ucs2
)
133 while ((*ucs1
== *ucs2
) && *ucs1
) {
137 return (int) *ucs1
- (int) *ucs2
;
141 * UniStrcpy: Copy a string
143 static inline wchar_t *
144 UniStrcpy(wchar_t *ucs1
, const wchar_t *ucs2
)
146 wchar_t *anchor
= ucs1
; /* save the start of result string */
148 while ((*ucs1
++ = *ucs2
++)) ;
153 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
156 UniStrlen(const wchar_t *ucs1
)
166 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
167 * string (length limited)
170 UniStrnlen(const wchar_t *ucs1
, int maxlen
)
183 * UniStrncat: Concatenate length limited string
185 static inline wchar_t *
186 UniStrncat(wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
188 wchar_t *anchor
= ucs1
; /* save pointer to string 1 */
191 ucs1
--; /* point to null terminator of s1 */
192 while (n
-- && (*ucs1
= *ucs2
)) { /* copy s2 after s1 */
196 *ucs1
= 0; /* Null terminate the result */
201 * UniStrncmp: Compare length limited string
204 UniStrncmp(const wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
207 return 0; /* Null strings are equal */
208 while ((*ucs1
== *ucs2
) && *ucs1
&& --n
) {
212 return (int) *ucs1
- (int) *ucs2
;
216 * UniStrncmp_le: Compare length limited string - native to little-endian
219 UniStrncmp_le(const wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
222 return 0; /* Null strings are equal */
223 while ((*ucs1
== __le16_to_cpu(*ucs2
)) && *ucs1
&& --n
) {
227 return (int) *ucs1
- (int) __le16_to_cpu(*ucs2
);
231 * UniStrncpy: Copy length limited string with pad
233 static inline wchar_t *
234 UniStrncpy(wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
236 wchar_t *anchor
= ucs1
;
238 while (n
-- && *ucs2
) /* Copy the strings */
242 while (n
--) /* Pad with nulls */
248 * UniStrncpy_le: Copy length limited string with pad to little-endian
250 static inline wchar_t *
251 UniStrncpy_le(wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
253 wchar_t *anchor
= ucs1
;
255 while (n
-- && *ucs2
) /* Copy the strings */
256 *ucs1
++ = __le16_to_cpu(*ucs2
++);
259 while (n
--) /* Pad with nulls */
265 * UniStrstr: Find a string in a string
268 * Address of first match found
269 * NULL if no matching string is found
271 static inline wchar_t *
272 UniStrstr(const wchar_t *ucs1
, const wchar_t *ucs2
)
274 const wchar_t *anchor1
= ucs1
;
275 const wchar_t *anchor2
= ucs2
;
278 if (*ucs1
== *ucs2
) {
279 /* Partial match found */
283 if (!*ucs2
) /* Match found */
284 return (wchar_t *) anchor1
;
285 ucs1
= ++anchor1
; /* No match */
290 if (!*ucs2
) /* Both end together */
291 return (wchar_t *) anchor1
; /* Match found */
292 return NULL
; /* No match */
295 #ifndef UNIUPR_NOUPPER
297 * UniToupper: Convert a unicode character to upper case
299 static inline wchar_t
300 UniToupper(register wchar_t uc
)
302 register const struct UniCaseRange
*rp
;
304 if (uc
< sizeof(CifsUniUpperTable
)) {
305 /* Latin characters */
306 return uc
+ CifsUniUpperTable
[uc
]; /* Use base tables */
308 rp
= CifsUniUpperRange
; /* Use range tables */
310 if (uc
< rp
->start
) /* Before start of range */
311 return uc
; /* Uppercase = input */
312 if (uc
<= rp
->end
) /* In range */
313 return uc
+ rp
->table
[uc
- rp
->start
];
314 rp
++; /* Try next range */
317 return uc
; /* Past last range */
321 * UniStrupr: Upper case a unicode string
323 static inline wchar_t *
324 UniStrupr(register wchar_t *upin
)
326 register wchar_t *up
;
329 while (*up
) { /* For all characters */
330 *up
= UniToupper(*up
);
333 return upin
; /* Return input pointer */
335 #endif /* UNIUPR_NOUPPER */
337 #ifndef UNIUPR_NOLOWER
339 * UniTolower: Convert a unicode character to lower case
341 static inline wchar_t
342 UniTolower(register wchar_t uc
)
344 register const struct UniCaseRange
*rp
;
346 if (uc
< sizeof(CifsUniLowerTable
)) {
347 /* Latin characters */
348 return uc
+ CifsUniLowerTable
[uc
]; /* Use base tables */
350 rp
= CifsUniLowerRange
; /* Use range tables */
352 if (uc
< rp
->start
) /* Before start of range */
353 return uc
; /* Uppercase = input */
354 if (uc
<= rp
->end
) /* In range */
355 return uc
+ rp
->table
[uc
- rp
->start
];
356 rp
++; /* Try next range */
359 return uc
; /* Past last range */
363 * UniStrlwr: Lower case a unicode string
365 static inline wchar_t *
366 UniStrlwr(register wchar_t *upin
)
368 register wchar_t *up
;
371 while (*up
) { /* For all characters */
372 *up
= UniTolower(*up
);
375 return upin
; /* Return input pointer */
380 #endif /* _CIFS_UNICODE_H */