blob: 1857f5ff9337983739ccc1e0b605dd15d509e8e9 [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__
Jeff Layton7fabf0c2009-04-30 06:46:15 -040075int cifs_from_ucs2(char *to, const __le16 *from, int tolen, int fromlen,
76 const struct nls_table *codepage, bool mapchar);
Jeff Layton69f801f2009-04-30 06:46:32 -040077int cifs_ucs2_bytes(const __le16 *from, int maxbytes,
78 const struct nls_table *codepage);
Steve Frenche89dc922005-11-11 15:18:19 -080079int cifs_strfromUCS_le(char *, const __le16 *, int, const struct nls_table *);
80int cifs_strtoUCS(__le16 *, const char *, int, const struct nls_table *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif
82
83/*
84 * UniStrcat: Concatenate the second string to the first
85 *
86 * Returns:
87 * Address of the first string
88 */
89static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +000090UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
93
94 while (*ucs1++) ; /* To end of first string */
95 ucs1--; /* Return to the null */
96 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
97 return anchor;
98}
99
100/*
101 * UniStrchr: Find a character in a string
102 *
103 * Returns:
104 * Address of first occurrence of character in string
105 * or NULL if the character is not in the string
106 */
107static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000108UniStrchr(const wchar_t *ucs, wchar_t uc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 while ((*ucs != uc) && *ucs)
111 ucs++;
112
113 if (*ucs == uc)
114 return (wchar_t *) ucs;
115 return NULL;
116}
117
118/*
119 * UniStrcmp: Compare two strings
120 *
121 * Returns:
122 * < 0: First string is less than second
123 * = 0: Strings are equal
124 * > 0: First string is greater than second
125 */
126static inline int
Steve French50c2f752007-07-13 00:33:32 +0000127UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 while ((*ucs1 == *ucs2) && *ucs1) {
130 ucs1++;
131 ucs2++;
132 }
133 return (int) *ucs1 - (int) *ucs2;
134}
135
136/*
137 * UniStrcpy: Copy a string
138 */
139static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000140UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 wchar_t *anchor = ucs1; /* save the start of result string */
143
144 while ((*ucs1++ = *ucs2++)) ;
145 return anchor;
146}
147
148/*
149 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
150 */
151static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000152UniStrlen(const wchar_t *ucs1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 int i = 0;
155
156 while (*ucs1++)
157 i++;
158 return i;
159}
160
161/*
Steve Frenchd38d8c72007-06-28 19:44:13 +0000162 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
163 * string (length limited)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
165static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000166UniStrnlen(const wchar_t *ucs1, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 int i = 0;
169
170 while (*ucs1++) {
171 i++;
172 if (i >= maxlen)
173 break;
174 }
175 return i;
176}
177
178/*
179 * UniStrncat: Concatenate length limited string
180 */
181static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000182UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 wchar_t *anchor = ucs1; /* save pointer to string 1 */
185
186 while (*ucs1++) ;
187 ucs1--; /* point to null terminator of s1 */
188 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
189 ucs1++;
190 ucs2++;
191 }
192 *ucs1 = 0; /* Null terminate the result */
193 return (anchor);
194}
195
196/*
197 * UniStrncmp: Compare length limited string
198 */
199static inline int
Steve French50c2f752007-07-13 00:33:32 +0000200UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 if (!n)
203 return 0; /* Null strings are equal */
204 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
205 ucs1++;
206 ucs2++;
207 }
208 return (int) *ucs1 - (int) *ucs2;
209}
210
211/*
212 * UniStrncmp_le: Compare length limited string - native to little-endian
213 */
214static inline int
Steve French50c2f752007-07-13 00:33:32 +0000215UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 if (!n)
218 return 0; /* Null strings are equal */
219 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
220 ucs1++;
221 ucs2++;
222 }
223 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
224}
225
226/*
227 * UniStrncpy: Copy length limited string with pad
228 */
229static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000230UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 wchar_t *anchor = ucs1;
233
234 while (n-- && *ucs2) /* Copy the strings */
235 *ucs1++ = *ucs2++;
236
237 n++;
238 while (n--) /* Pad with nulls */
239 *ucs1++ = 0;
240 return anchor;
241}
242
243/*
244 * UniStrncpy_le: Copy length limited string with pad to little-endian
245 */
246static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000247UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 wchar_t *anchor = ucs1;
250
251 while (n-- && *ucs2) /* Copy the strings */
252 *ucs1++ = __le16_to_cpu(*ucs2++);
253
254 n++;
255 while (n--) /* Pad with nulls */
256 *ucs1++ = 0;
257 return anchor;
258}
259
260/*
261 * UniStrstr: Find a string in a string
262 *
263 * Returns:
264 * Address of first match found
265 * NULL if no matching string is found
266 */
267static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000268UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 const wchar_t *anchor1 = ucs1;
271 const wchar_t *anchor2 = ucs2;
272
273 while (*ucs1) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000274 if (*ucs1 == *ucs2) {
275 /* Partial match found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 ucs1++;
277 ucs2++;
278 } else {
279 if (!*ucs2) /* Match found */
280 return (wchar_t *) anchor1;
281 ucs1 = ++anchor1; /* No match */
282 ucs2 = anchor2;
283 }
284 }
285
286 if (!*ucs2) /* Both end together */
287 return (wchar_t *) anchor1; /* Match found */
288 return NULL; /* No match */
289}
290
291#ifndef UNIUPR_NOUPPER
292/*
293 * UniToupper: Convert a unicode character to upper case
294 */
295static inline wchar_t
296UniToupper(register wchar_t uc)
297{
298 register const struct UniCaseRange *rp;
299
Steve Frenchad7a2922008-02-07 23:25:02 +0000300 if (uc < sizeof(CifsUniUpperTable)) {
301 /* Latin characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return uc + CifsUniUpperTable[uc]; /* Use base tables */
303 } else {
304 rp = CifsUniUpperRange; /* Use range tables */
305 while (rp->start) {
306 if (uc < rp->start) /* Before start of range */
307 return uc; /* Uppercase = input */
308 if (uc <= rp->end) /* In range */
309 return uc + rp->table[uc - rp->start];
310 rp++; /* Try next range */
311 }
312 }
313 return uc; /* Past last range */
314}
315
316/*
317 * UniStrupr: Upper case a unicode string
318 */
319static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000320UniStrupr(register wchar_t *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 register wchar_t *up;
323
324 up = upin;
325 while (*up) { /* For all characters */
326 *up = UniToupper(*up);
327 up++;
328 }
329 return upin; /* Return input pointer */
330}
331#endif /* UNIUPR_NOUPPER */
332
333#ifndef UNIUPR_NOLOWER
334/*
335 * UniTolower: Convert a unicode character to lower case
336 */
337static inline wchar_t
338UniTolower(wchar_t uc)
339{
340 register struct UniCaseRange *rp;
341
Steve Frenchad7a2922008-02-07 23:25:02 +0000342 if (uc < sizeof(UniLowerTable)) {
343 /* Latin characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return uc + UniLowerTable[uc]; /* Use base tables */
345 } else {
346 rp = UniLowerRange; /* Use range tables */
347 while (rp->start) {
348 if (uc < rp->start) /* Before start of range */
349 return uc; /* Uppercase = input */
350 if (uc <= rp->end) /* In range */
351 return uc + rp->table[uc - rp->start];
352 rp++; /* Try next range */
353 }
354 }
355 return uc; /* Past last range */
356}
357
358/*
359 * UniStrlwr: Lower case a unicode string
360 */
361static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000362UniStrlwr(register wchar_t *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 register wchar_t *up;
365
366 up = upin;
367 while (*up) { /* For all characters */
368 *up = UniTolower(*up);
369 up++;
370 }
371 return upin; /* Return input pointer */
372}
373
374#endif