blob: 6aa6533e49fa0a5178b9bb9b22eb25473f36cdda [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);
Steve Frenche89dc922005-11-11 15:18:19 -080077int cifs_strfromUCS_le(char *, const __le16 *, int, const struct nls_table *);
78int cifs_strtoUCS(__le16 *, const char *, int, const struct nls_table *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#endif
80
81/*
82 * UniStrcat: Concatenate the second string to the first
83 *
84 * Returns:
85 * Address of the first string
86 */
87static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +000088UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
91
92 while (*ucs1++) ; /* To end of first string */
93 ucs1--; /* Return to the null */
94 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
95 return anchor;
96}
97
98/*
99 * UniStrchr: Find a character in a string
100 *
101 * Returns:
102 * Address of first occurrence of character in string
103 * or NULL if the character is not in the string
104 */
105static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000106UniStrchr(const wchar_t *ucs, wchar_t uc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 while ((*ucs != uc) && *ucs)
109 ucs++;
110
111 if (*ucs == uc)
112 return (wchar_t *) ucs;
113 return NULL;
114}
115
116/*
117 * UniStrcmp: Compare two strings
118 *
119 * Returns:
120 * < 0: First string is less than second
121 * = 0: Strings are equal
122 * > 0: First string is greater than second
123 */
124static inline int
Steve French50c2f752007-07-13 00:33:32 +0000125UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 while ((*ucs1 == *ucs2) && *ucs1) {
128 ucs1++;
129 ucs2++;
130 }
131 return (int) *ucs1 - (int) *ucs2;
132}
133
134/*
135 * UniStrcpy: Copy a string
136 */
137static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000138UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 wchar_t *anchor = ucs1; /* save the start of result string */
141
142 while ((*ucs1++ = *ucs2++)) ;
143 return anchor;
144}
145
146/*
147 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
148 */
149static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000150UniStrlen(const wchar_t *ucs1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 int i = 0;
153
154 while (*ucs1++)
155 i++;
156 return i;
157}
158
159/*
Steve Frenchd38d8c72007-06-28 19:44:13 +0000160 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
161 * string (length limited)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 */
163static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000164UniStrnlen(const wchar_t *ucs1, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 int i = 0;
167
168 while (*ucs1++) {
169 i++;
170 if (i >= maxlen)
171 break;
172 }
173 return i;
174}
175
176/*
177 * UniStrncat: Concatenate length limited string
178 */
179static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000180UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 wchar_t *anchor = ucs1; /* save pointer to string 1 */
183
184 while (*ucs1++) ;
185 ucs1--; /* point to null terminator of s1 */
186 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
187 ucs1++;
188 ucs2++;
189 }
190 *ucs1 = 0; /* Null terminate the result */
191 return (anchor);
192}
193
194/*
195 * UniStrncmp: Compare length limited string
196 */
197static inline int
Steve French50c2f752007-07-13 00:33:32 +0000198UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 if (!n)
201 return 0; /* Null strings are equal */
202 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
203 ucs1++;
204 ucs2++;
205 }
206 return (int) *ucs1 - (int) *ucs2;
207}
208
209/*
210 * UniStrncmp_le: Compare length limited string - native to little-endian
211 */
212static inline int
Steve French50c2f752007-07-13 00:33:32 +0000213UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 if (!n)
216 return 0; /* Null strings are equal */
217 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
218 ucs1++;
219 ucs2++;
220 }
221 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
222}
223
224/*
225 * UniStrncpy: Copy length limited string with pad
226 */
227static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000228UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 wchar_t *anchor = ucs1;
231
232 while (n-- && *ucs2) /* Copy the strings */
233 *ucs1++ = *ucs2++;
234
235 n++;
236 while (n--) /* Pad with nulls */
237 *ucs1++ = 0;
238 return anchor;
239}
240
241/*
242 * UniStrncpy_le: Copy length limited string with pad to little-endian
243 */
244static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000245UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 wchar_t *anchor = ucs1;
248
249 while (n-- && *ucs2) /* Copy the strings */
250 *ucs1++ = __le16_to_cpu(*ucs2++);
251
252 n++;
253 while (n--) /* Pad with nulls */
254 *ucs1++ = 0;
255 return anchor;
256}
257
258/*
259 * UniStrstr: Find a string in a string
260 *
261 * Returns:
262 * Address of first match found
263 * NULL if no matching string is found
264 */
265static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000266UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 const wchar_t *anchor1 = ucs1;
269 const wchar_t *anchor2 = ucs2;
270
271 while (*ucs1) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000272 if (*ucs1 == *ucs2) {
273 /* Partial match found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 ucs1++;
275 ucs2++;
276 } else {
277 if (!*ucs2) /* Match found */
278 return (wchar_t *) anchor1;
279 ucs1 = ++anchor1; /* No match */
280 ucs2 = anchor2;
281 }
282 }
283
284 if (!*ucs2) /* Both end together */
285 return (wchar_t *) anchor1; /* Match found */
286 return NULL; /* No match */
287}
288
289#ifndef UNIUPR_NOUPPER
290/*
291 * UniToupper: Convert a unicode character to upper case
292 */
293static inline wchar_t
294UniToupper(register wchar_t uc)
295{
296 register const struct UniCaseRange *rp;
297
Steve Frenchad7a2922008-02-07 23:25:02 +0000298 if (uc < sizeof(CifsUniUpperTable)) {
299 /* Latin characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return uc + CifsUniUpperTable[uc]; /* Use base tables */
301 } else {
302 rp = CifsUniUpperRange; /* Use range tables */
303 while (rp->start) {
304 if (uc < rp->start) /* Before start of range */
305 return uc; /* Uppercase = input */
306 if (uc <= rp->end) /* In range */
307 return uc + rp->table[uc - rp->start];
308 rp++; /* Try next range */
309 }
310 }
311 return uc; /* Past last range */
312}
313
314/*
315 * UniStrupr: Upper case a unicode string
316 */
317static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000318UniStrupr(register wchar_t *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 register wchar_t *up;
321
322 up = upin;
323 while (*up) { /* For all characters */
324 *up = UniToupper(*up);
325 up++;
326 }
327 return upin; /* Return input pointer */
328}
329#endif /* UNIUPR_NOUPPER */
330
331#ifndef UNIUPR_NOLOWER
332/*
333 * UniTolower: Convert a unicode character to lower case
334 */
335static inline wchar_t
336UniTolower(wchar_t uc)
337{
338 register struct UniCaseRange *rp;
339
Steve Frenchad7a2922008-02-07 23:25:02 +0000340 if (uc < sizeof(UniLowerTable)) {
341 /* Latin characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return uc + UniLowerTable[uc]; /* Use base tables */
343 } else {
344 rp = UniLowerRange; /* Use range tables */
345 while (rp->start) {
346 if (uc < rp->start) /* Before start of range */
347 return uc; /* Uppercase = input */
348 if (uc <= rp->end) /* In range */
349 return uc + rp->table[uc - rp->start];
350 rp++; /* Try next range */
351 }
352 }
353 return uc; /* Past last range */
354}
355
356/*
357 * UniStrlwr: Lower case a unicode string
358 */
359static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000360UniStrlwr(register wchar_t *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 register wchar_t *up;
363
364 up = upin;
365 while (*up) { /* For all characters */
366 *up = UniTolower(*up);
367 up++;
368 }
369 return upin; /* Return input pointer */
370}
371
372#endif