blob: d6fe8ecd1ffc295756b0357574d9c9f8e7ad4b41 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * cifs_unicode: Unicode kernel case support
3 *
4 * Function:
5 * Convert a unicode character to upper or lower case using
6 * compressed tables.
7 *
Steve Frenchd38d8c72007-06-28 19:44:13 +00008 * Copyright (c) International Business Machines Corp., 2000,2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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
Steve Frenchd38d8c72007-06-28 19:44:13 +000012 * the Free Software Foundation; either version 2 of the License, or
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * (at your option) any later version.
Steve Frenchd38d8c72007-06-28 19:44:13 +000014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * 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.
19 *
20 * You should have received a copy of the GNU General Public License
Steve Frenchd38d8c72007-06-28 19:44:13 +000021 * along with this program; if not, write to the Free Software
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 *
25 * Notes:
26 * These APIs are based on the C library functions. The semantics
27 * should match the C functions but with expanded size operands.
28 *
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.
31 *
32 */
33
34#include <asm/byteorder.h>
35#include <linux/types.h>
36#include <linux/nls.h>
37
38#define UNIUPR_NOLOWER /* Example to not expand lower case tables */
39
Jeff Layton66345f52009-04-30 06:45:08 -040040/*
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
44 */
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)
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* Just define what we want from uniupr.h. We don't want to define the tables
54 * in each source file.
55 */
56#ifndef UNICASERANGE_DEFINED
57struct UniCaseRange {
58 wchar_t start;
59 wchar_t end;
60 signed char *table;
61};
62#endif /* UNICASERANGE_DEFINED */
63
64#ifndef UNIUPR_NOUPPER
65extern signed char CifsUniUpperTable[512];
66extern const struct UniCaseRange CifsUniUpperRange[];
67#endif /* UNIUPR_NOUPPER */
68
69#ifndef UNIUPR_NOLOWER
70extern signed char UniLowerTable[512];
71extern struct UniCaseRange UniLowerRange[];
72#endif /* UNIUPR_NOLOWER */
73
74#ifdef __KERNEL__
Steve Frenche89dc922005-11-11 15:18:19 -080075int cifs_strfromUCS_le(char *, const __le16 *, int, const struct nls_table *);
76int cifs_strtoUCS(__le16 *, const char *, int, const struct nls_table *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
78
79/*
80 * UniStrcat: Concatenate the second string to the first
81 *
82 * Returns:
83 * Address of the first string
84 */
85static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +000086UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
89
90 while (*ucs1++) ; /* To end of first string */
91 ucs1--; /* Return to the null */
92 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
93 return anchor;
94}
95
96/*
97 * UniStrchr: Find a character in a string
98 *
99 * Returns:
100 * Address of first occurrence of character in string
101 * or NULL if the character is not in the string
102 */
103static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000104UniStrchr(const wchar_t *ucs, wchar_t uc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 while ((*ucs != uc) && *ucs)
107 ucs++;
108
109 if (*ucs == uc)
110 return (wchar_t *) ucs;
111 return NULL;
112}
113
114/*
115 * UniStrcmp: Compare two strings
116 *
117 * Returns:
118 * < 0: First string is less than second
119 * = 0: Strings are equal
120 * > 0: First string is greater than second
121 */
122static inline int
Steve French50c2f752007-07-13 00:33:32 +0000123UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 while ((*ucs1 == *ucs2) && *ucs1) {
126 ucs1++;
127 ucs2++;
128 }
129 return (int) *ucs1 - (int) *ucs2;
130}
131
132/*
133 * UniStrcpy: Copy a string
134 */
135static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000136UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 wchar_t *anchor = ucs1; /* save the start of result string */
139
140 while ((*ucs1++ = *ucs2++)) ;
141 return anchor;
142}
143
144/*
145 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
146 */
147static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000148UniStrlen(const wchar_t *ucs1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 int i = 0;
151
152 while (*ucs1++)
153 i++;
154 return i;
155}
156
157/*
Steve Frenchd38d8c72007-06-28 19:44:13 +0000158 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
159 * string (length limited)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
161static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000162UniStrnlen(const wchar_t *ucs1, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 int i = 0;
165
166 while (*ucs1++) {
167 i++;
168 if (i >= maxlen)
169 break;
170 }
171 return i;
172}
173
174/*
175 * UniStrncat: Concatenate length limited string
176 */
177static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000178UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 wchar_t *anchor = ucs1; /* save pointer to string 1 */
181
182 while (*ucs1++) ;
183 ucs1--; /* point to null terminator of s1 */
184 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
185 ucs1++;
186 ucs2++;
187 }
188 *ucs1 = 0; /* Null terminate the result */
189 return (anchor);
190}
191
192/*
193 * UniStrncmp: Compare length limited string
194 */
195static inline int
Steve French50c2f752007-07-13 00:33:32 +0000196UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 if (!n)
199 return 0; /* Null strings are equal */
200 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
201 ucs1++;
202 ucs2++;
203 }
204 return (int) *ucs1 - (int) *ucs2;
205}
206
207/*
208 * UniStrncmp_le: Compare length limited string - native to little-endian
209 */
210static inline int
Steve French50c2f752007-07-13 00:33:32 +0000211UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 if (!n)
214 return 0; /* Null strings are equal */
215 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
216 ucs1++;
217 ucs2++;
218 }
219 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
220}
221
222/*
223 * UniStrncpy: Copy length limited string with pad
224 */
225static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000226UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 wchar_t *anchor = ucs1;
229
230 while (n-- && *ucs2) /* Copy the strings */
231 *ucs1++ = *ucs2++;
232
233 n++;
234 while (n--) /* Pad with nulls */
235 *ucs1++ = 0;
236 return anchor;
237}
238
239/*
240 * UniStrncpy_le: Copy length limited string with pad to little-endian
241 */
242static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000243UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 wchar_t *anchor = ucs1;
246
247 while (n-- && *ucs2) /* Copy the strings */
248 *ucs1++ = __le16_to_cpu(*ucs2++);
249
250 n++;
251 while (n--) /* Pad with nulls */
252 *ucs1++ = 0;
253 return anchor;
254}
255
256/*
257 * UniStrstr: Find a string in a string
258 *
259 * Returns:
260 * Address of first match found
261 * NULL if no matching string is found
262 */
263static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000264UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 const wchar_t *anchor1 = ucs1;
267 const wchar_t *anchor2 = ucs2;
268
269 while (*ucs1) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000270 if (*ucs1 == *ucs2) {
271 /* Partial match found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 ucs1++;
273 ucs2++;
274 } else {
275 if (!*ucs2) /* Match found */
276 return (wchar_t *) anchor1;
277 ucs1 = ++anchor1; /* No match */
278 ucs2 = anchor2;
279 }
280 }
281
282 if (!*ucs2) /* Both end together */
283 return (wchar_t *) anchor1; /* Match found */
284 return NULL; /* No match */
285}
286
287#ifndef UNIUPR_NOUPPER
288/*
289 * UniToupper: Convert a unicode character to upper case
290 */
291static inline wchar_t
292UniToupper(register wchar_t uc)
293{
294 register const struct UniCaseRange *rp;
295
Steve Frenchad7a2922008-02-07 23:25:02 +0000296 if (uc < sizeof(CifsUniUpperTable)) {
297 /* Latin characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return uc + CifsUniUpperTable[uc]; /* Use base tables */
299 } else {
300 rp = CifsUniUpperRange; /* Use range tables */
301 while (rp->start) {
302 if (uc < rp->start) /* Before start of range */
303 return uc; /* Uppercase = input */
304 if (uc <= rp->end) /* In range */
305 return uc + rp->table[uc - rp->start];
306 rp++; /* Try next range */
307 }
308 }
309 return uc; /* Past last range */
310}
311
312/*
313 * UniStrupr: Upper case a unicode string
314 */
315static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000316UniStrupr(register wchar_t *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 register wchar_t *up;
319
320 up = upin;
321 while (*up) { /* For all characters */
322 *up = UniToupper(*up);
323 up++;
324 }
325 return upin; /* Return input pointer */
326}
327#endif /* UNIUPR_NOUPPER */
328
329#ifndef UNIUPR_NOLOWER
330/*
331 * UniTolower: Convert a unicode character to lower case
332 */
333static inline wchar_t
334UniTolower(wchar_t uc)
335{
336 register struct UniCaseRange *rp;
337
Steve Frenchad7a2922008-02-07 23:25:02 +0000338 if (uc < sizeof(UniLowerTable)) {
339 /* Latin characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return uc + UniLowerTable[uc]; /* Use base tables */
341 } else {
342 rp = UniLowerRange; /* Use range tables */
343 while (rp->start) {
344 if (uc < rp->start) /* Before start of range */
345 return uc; /* Uppercase = input */
346 if (uc <= rp->end) /* In range */
347 return uc + rp->table[uc - rp->start];
348 rp++; /* Try next range */
349 }
350 }
351 return uc; /* Past last range */
352}
353
354/*
355 * UniStrlwr: Lower case a unicode string
356 */
357static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000358UniStrlwr(register wchar_t *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 register wchar_t *up;
361
362 up = upin;
363 while (*up) { /* For all characters */
364 *up = UniTolower(*up);
365 up++;
366 }
367 return upin; /* Return input pointer */
368}
369
370#endif