blob: 1570a701bf3fabe0af681fad236ec75284acd01c [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 Frenchd185cda2009-04-30 17:45:10 +00008 * Copyright (c) International Business Machines Corp., 2000,2009
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 *);
Steve Frenchd185cda2009-04-30 17:45:10 +000081char *cifs_strndup_from_ucs(const char *src, const int maxlen,
82 const bool is_unicode,
83 const struct nls_table *codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#endif
85
86/*
87 * UniStrcat: Concatenate the second string to the first
88 *
89 * Returns:
90 * Address of the first string
91 */
92static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +000093UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
96
97 while (*ucs1++) ; /* To end of first string */
98 ucs1--; /* Return to the null */
99 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
100 return anchor;
101}
102
103/*
104 * UniStrchr: Find a character in a string
105 *
106 * Returns:
107 * Address of first occurrence of character in string
108 * or NULL if the character is not in the string
109 */
110static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000111UniStrchr(const wchar_t *ucs, wchar_t uc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 while ((*ucs != uc) && *ucs)
114 ucs++;
115
116 if (*ucs == uc)
117 return (wchar_t *) ucs;
118 return NULL;
119}
120
121/*
122 * UniStrcmp: Compare two strings
123 *
124 * Returns:
125 * < 0: First string is less than second
126 * = 0: Strings are equal
127 * > 0: First string is greater than second
128 */
129static inline int
Steve French50c2f752007-07-13 00:33:32 +0000130UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 while ((*ucs1 == *ucs2) && *ucs1) {
133 ucs1++;
134 ucs2++;
135 }
136 return (int) *ucs1 - (int) *ucs2;
137}
138
139/*
140 * UniStrcpy: Copy a string
141 */
142static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000143UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 wchar_t *anchor = ucs1; /* save the start of result string */
146
147 while ((*ucs1++ = *ucs2++)) ;
148 return anchor;
149}
150
151/*
152 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
153 */
154static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000155UniStrlen(const wchar_t *ucs1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 int i = 0;
158
159 while (*ucs1++)
160 i++;
161 return i;
162}
163
164/*
Steve Frenchd38d8c72007-06-28 19:44:13 +0000165 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
166 * string (length limited)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
168static inline size_t
Steve French50c2f752007-07-13 00:33:32 +0000169UniStrnlen(const wchar_t *ucs1, int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 int i = 0;
172
173 while (*ucs1++) {
174 i++;
175 if (i >= maxlen)
176 break;
177 }
178 return i;
179}
180
181/*
182 * UniStrncat: Concatenate length limited string
183 */
184static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000185UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 wchar_t *anchor = ucs1; /* save pointer to string 1 */
188
189 while (*ucs1++) ;
190 ucs1--; /* point to null terminator of s1 */
191 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
192 ucs1++;
193 ucs2++;
194 }
195 *ucs1 = 0; /* Null terminate the result */
196 return (anchor);
197}
198
199/*
200 * UniStrncmp: Compare length limited string
201 */
202static inline int
Steve French50c2f752007-07-13 00:33:32 +0000203UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 if (!n)
206 return 0; /* Null strings are equal */
207 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
208 ucs1++;
209 ucs2++;
210 }
211 return (int) *ucs1 - (int) *ucs2;
212}
213
214/*
215 * UniStrncmp_le: Compare length limited string - native to little-endian
216 */
217static inline int
Steve French50c2f752007-07-13 00:33:32 +0000218UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 if (!n)
221 return 0; /* Null strings are equal */
222 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
223 ucs1++;
224 ucs2++;
225 }
226 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
227}
228
229/*
230 * UniStrncpy: Copy length limited string with pad
231 */
232static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000233UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 wchar_t *anchor = ucs1;
236
237 while (n-- && *ucs2) /* Copy the strings */
238 *ucs1++ = *ucs2++;
239
240 n++;
241 while (n--) /* Pad with nulls */
242 *ucs1++ = 0;
243 return anchor;
244}
245
246/*
247 * UniStrncpy_le: Copy length limited string with pad to little-endian
248 */
249static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000250UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 wchar_t *anchor = ucs1;
253
254 while (n-- && *ucs2) /* Copy the strings */
255 *ucs1++ = __le16_to_cpu(*ucs2++);
256
257 n++;
258 while (n--) /* Pad with nulls */
259 *ucs1++ = 0;
260 return anchor;
261}
262
263/*
264 * UniStrstr: Find a string in a string
265 *
266 * Returns:
267 * Address of first match found
268 * NULL if no matching string is found
269 */
270static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000271UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 const wchar_t *anchor1 = ucs1;
274 const wchar_t *anchor2 = ucs2;
275
276 while (*ucs1) {
Steve Frenchad7a2922008-02-07 23:25:02 +0000277 if (*ucs1 == *ucs2) {
278 /* Partial match found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 ucs1++;
280 ucs2++;
281 } else {
282 if (!*ucs2) /* Match found */
283 return (wchar_t *) anchor1;
284 ucs1 = ++anchor1; /* No match */
285 ucs2 = anchor2;
286 }
287 }
288
289 if (!*ucs2) /* Both end together */
290 return (wchar_t *) anchor1; /* Match found */
291 return NULL; /* No match */
292}
293
294#ifndef UNIUPR_NOUPPER
295/*
296 * UniToupper: Convert a unicode character to upper case
297 */
298static inline wchar_t
299UniToupper(register wchar_t uc)
300{
301 register const struct UniCaseRange *rp;
302
Steve Frenchad7a2922008-02-07 23:25:02 +0000303 if (uc < sizeof(CifsUniUpperTable)) {
304 /* Latin characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return uc + CifsUniUpperTable[uc]; /* Use base tables */
306 } else {
307 rp = CifsUniUpperRange; /* Use range tables */
308 while (rp->start) {
309 if (uc < rp->start) /* Before start of range */
310 return uc; /* Uppercase = input */
311 if (uc <= rp->end) /* In range */
312 return uc + rp->table[uc - rp->start];
313 rp++; /* Try next range */
314 }
315 }
316 return uc; /* Past last range */
317}
318
319/*
320 * UniStrupr: Upper case a unicode string
321 */
322static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000323UniStrupr(register wchar_t *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 register wchar_t *up;
326
327 up = upin;
328 while (*up) { /* For all characters */
329 *up = UniToupper(*up);
330 up++;
331 }
332 return upin; /* Return input pointer */
333}
334#endif /* UNIUPR_NOUPPER */
335
336#ifndef UNIUPR_NOLOWER
337/*
338 * UniTolower: Convert a unicode character to lower case
339 */
340static inline wchar_t
341UniTolower(wchar_t uc)
342{
343 register struct UniCaseRange *rp;
344
Steve Frenchad7a2922008-02-07 23:25:02 +0000345 if (uc < sizeof(UniLowerTable)) {
346 /* Latin characters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return uc + UniLowerTable[uc]; /* Use base tables */
348 } else {
349 rp = UniLowerRange; /* Use range tables */
350 while (rp->start) {
351 if (uc < rp->start) /* Before start of range */
352 return uc; /* Uppercase = input */
353 if (uc <= rp->end) /* In range */
354 return uc + rp->table[uc - rp->start];
355 rp++; /* Try next range */
356 }
357 }
358 return uc; /* Past last range */
359}
360
361/*
362 * UniStrlwr: Lower case a unicode string
363 */
364static inline wchar_t *
Steve French50c2f752007-07-13 00:33:32 +0000365UniStrlwr(register wchar_t *upin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 register wchar_t *up;
368
369 up = upin;
370 while (*up) { /* For all characters */
371 *up = UniTolower(*up);
372 up++;
373 }
374 return upin; /* Return input pointer */
375}
376
377#endif