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 */
40 /* Just define what we want from uniupr.h. We don't want to define the tables
41 * in each source file.
43 #ifndef UNICASERANGE_DEFINED
49 #endif /* UNICASERANGE_DEFINED */
51 #ifndef UNIUPR_NOUPPER
52 extern signed char CifsUniUpperTable
[512];
53 extern const struct UniCaseRange CifsUniUpperRange
[];
54 #endif /* UNIUPR_NOUPPER */
56 #ifndef UNIUPR_NOLOWER
57 extern signed char UniLowerTable
[512];
58 extern struct UniCaseRange UniLowerRange
[];
59 #endif /* UNIUPR_NOLOWER */
62 int cifs_strfromUCS_le(char *, const __le16
*, int, const struct nls_table
*);
63 int cifs_strtoUCS(__le16
*, const char *, int, const struct nls_table
*);
67 * UniStrcat: Concatenate the second string to the first
70 * Address of the first string
72 static inline wchar_t *
73 UniStrcat(wchar_t *ucs1
, const wchar_t *ucs2
)
75 wchar_t *anchor
= ucs1
; /* save a pointer to start of ucs1 */
77 while (*ucs1
++) ; /* To end of first string */
78 ucs1
--; /* Return to the null */
79 while ((*ucs1
++ = *ucs2
++)) ; /* copy string 2 over */
84 * UniStrchr: Find a character in a string
87 * Address of first occurrence of character in string
88 * or NULL if the character is not in the string
90 static inline wchar_t *
91 UniStrchr(const wchar_t *ucs
, wchar_t uc
)
93 while ((*ucs
!= uc
) && *ucs
)
97 return (wchar_t *) ucs
;
102 * UniStrcmp: Compare two strings
105 * < 0: First string is less than second
106 * = 0: Strings are equal
107 * > 0: First string is greater than second
110 UniStrcmp(const wchar_t *ucs1
, const wchar_t *ucs2
)
112 while ((*ucs1
== *ucs2
) && *ucs1
) {
116 return (int) *ucs1
- (int) *ucs2
;
120 * UniStrcpy: Copy a string
122 static inline wchar_t *
123 UniStrcpy(wchar_t *ucs1
, const wchar_t *ucs2
)
125 wchar_t *anchor
= ucs1
; /* save the start of result string */
127 while ((*ucs1
++ = *ucs2
++)) ;
132 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
135 UniStrlen(const wchar_t *ucs1
)
145 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
146 * string (length limited)
149 UniStrnlen(const wchar_t *ucs1
, int maxlen
)
162 * UniStrncat: Concatenate length limited string
164 static inline wchar_t *
165 UniStrncat(wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
167 wchar_t *anchor
= ucs1
; /* save pointer to string 1 */
170 ucs1
--; /* point to null terminator of s1 */
171 while (n
-- && (*ucs1
= *ucs2
)) { /* copy s2 after s1 */
175 *ucs1
= 0; /* Null terminate the result */
180 * UniStrncmp: Compare length limited string
183 UniStrncmp(const wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
186 return 0; /* Null strings are equal */
187 while ((*ucs1
== *ucs2
) && *ucs1
&& --n
) {
191 return (int) *ucs1
- (int) *ucs2
;
195 * UniStrncmp_le: Compare length limited string - native to little-endian
198 UniStrncmp_le(const wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
201 return 0; /* Null strings are equal */
202 while ((*ucs1
== __le16_to_cpu(*ucs2
)) && *ucs1
&& --n
) {
206 return (int) *ucs1
- (int) __le16_to_cpu(*ucs2
);
210 * UniStrncpy: Copy length limited string with pad
212 static inline wchar_t *
213 UniStrncpy(wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
215 wchar_t *anchor
= ucs1
;
217 while (n
-- && *ucs2
) /* Copy the strings */
221 while (n
--) /* Pad with nulls */
227 * UniStrncpy_le: Copy length limited string with pad to little-endian
229 static inline wchar_t *
230 UniStrncpy_le(wchar_t *ucs1
, const wchar_t *ucs2
, size_t n
)
232 wchar_t *anchor
= ucs1
;
234 while (n
-- && *ucs2
) /* Copy the strings */
235 *ucs1
++ = __le16_to_cpu(*ucs2
++);
238 while (n
--) /* Pad with nulls */
244 * UniStrstr: Find a string in a string
247 * Address of first match found
248 * NULL if no matching string is found
250 static inline wchar_t *
251 UniStrstr(const wchar_t *ucs1
, const wchar_t *ucs2
)
253 const wchar_t *anchor1
= ucs1
;
254 const wchar_t *anchor2
= ucs2
;
257 if (*ucs1
== *ucs2
) { /* Partial match found */
261 if (!*ucs2
) /* Match found */
262 return (wchar_t *) anchor1
;
263 ucs1
= ++anchor1
; /* No match */
268 if (!*ucs2
) /* Both end together */
269 return (wchar_t *) anchor1
; /* Match found */
270 return NULL
; /* No match */
273 #ifndef UNIUPR_NOUPPER
275 * UniToupper: Convert a unicode character to upper case
277 static inline wchar_t
278 UniToupper(register wchar_t uc
)
280 register const struct UniCaseRange
*rp
;
282 if (uc
< sizeof (CifsUniUpperTable
)) { /* Latin characters */
283 return uc
+ CifsUniUpperTable
[uc
]; /* Use base tables */
285 rp
= CifsUniUpperRange
; /* Use range tables */
287 if (uc
< rp
->start
) /* Before start of range */
288 return uc
; /* Uppercase = input */
289 if (uc
<= rp
->end
) /* In range */
290 return uc
+ rp
->table
[uc
- rp
->start
];
291 rp
++; /* Try next range */
294 return uc
; /* Past last range */
298 * UniStrupr: Upper case a unicode string
300 static inline wchar_t *
301 UniStrupr(register wchar_t *upin
)
303 register wchar_t *up
;
306 while (*up
) { /* For all characters */
307 *up
= UniToupper(*up
);
310 return upin
; /* Return input pointer */
312 #endif /* UNIUPR_NOUPPER */
314 #ifndef UNIUPR_NOLOWER
316 * UniTolower: Convert a unicode character to lower case
318 static inline wchar_t
319 UniTolower(wchar_t uc
)
321 register struct UniCaseRange
*rp
;
323 if (uc
< sizeof (UniLowerTable
)) { /* Latin characters */
324 return uc
+ UniLowerTable
[uc
]; /* Use base tables */
326 rp
= UniLowerRange
; /* Use range tables */
328 if (uc
< rp
->start
) /* Before start of range */
329 return uc
; /* Uppercase = input */
330 if (uc
<= rp
->end
) /* In range */
331 return uc
+ rp
->table
[uc
- rp
->start
];
332 rp
++; /* Try next range */
335 return uc
; /* Past last range */
339 * UniStrlwr: Lower case a unicode string
341 static inline wchar_t *
342 UniStrlwr(register wchar_t *upin
)
344 register wchar_t *up
;
347 while (*up
) { /* For all characters */
348 *up
= UniTolower(*up
);
351 return upin
; /* Return input pointer */