1857f5ff9337983739ccc1e0b605dd15d509e8e9
[pandora-kernel.git] / fs / cifs / cifs_unicode.h
1 /*
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  *
8  *   Copyright (c) International Business Machines  Corp., 2000,2007
9  *
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.
14  *
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.
19  *
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
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
40 /*
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
53 /* 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
57 struct UniCaseRange {
58         wchar_t start;
59         wchar_t end;
60         signed char *table;
61 };
62 #endif                          /* UNICASERANGE_DEFINED */
63
64 #ifndef UNIUPR_NOUPPER
65 extern signed char CifsUniUpperTable[512];
66 extern const struct UniCaseRange CifsUniUpperRange[];
67 #endif                          /* UNIUPR_NOUPPER */
68
69 #ifndef UNIUPR_NOLOWER
70 extern signed char UniLowerTable[512];
71 extern struct UniCaseRange UniLowerRange[];
72 #endif                          /* UNIUPR_NOLOWER */
73
74 #ifdef __KERNEL__
75 int cifs_from_ucs2(char *to, const __le16 *from, int tolen, int fromlen,
76                    const struct nls_table *codepage, bool mapchar);
77 int cifs_ucs2_bytes(const __le16 *from, int maxbytes,
78                     const struct nls_table *codepage);
79 int cifs_strfromUCS_le(char *, const __le16 *, int, const struct nls_table *);
80 int cifs_strtoUCS(__le16 *, const char *, int, const struct nls_table *);
81 #endif
82
83 /*
84  * UniStrcat:  Concatenate the second string to the first
85  *
86  * Returns:
87  *     Address of the first string
88  */
89 static inline wchar_t *
90 UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
91 {
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  */
107 static inline wchar_t *
108 UniStrchr(const wchar_t *ucs, wchar_t uc)
109 {
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  */
126 static inline int
127 UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
128 {
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  */
139 static inline wchar_t *
140 UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
141 {
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  */
151 static inline size_t
152 UniStrlen(const wchar_t *ucs1)
153 {
154         int i = 0;
155
156         while (*ucs1++)
157                 i++;
158         return i;
159 }
160
161 /*
162  * UniStrnlen:  Return the length (in 16 bit Unicode chars not bytes) of a
163  *              string (length limited)
164  */
165 static inline size_t
166 UniStrnlen(const wchar_t *ucs1, int maxlen)
167 {
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  */
181 static inline wchar_t *
182 UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
183 {
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  */
199 static inline int
200 UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
201 {
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  */
214 static inline int
215 UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
216 {
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  */
229 static inline wchar_t *
230 UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
231 {
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  */
246 static inline wchar_t *
247 UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
248 {
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  */
267 static inline wchar_t *
268 UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
269 {
270         const wchar_t *anchor1 = ucs1;
271         const wchar_t *anchor2 = ucs2;
272
273         while (*ucs1) {
274                 if (*ucs1 == *ucs2) {
275                         /* Partial match found */
276                         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  */
295 static inline wchar_t
296 UniToupper(register wchar_t uc)
297 {
298         register const struct UniCaseRange *rp;
299
300         if (uc < sizeof(CifsUniUpperTable)) {
301                 /* Latin characters */
302                 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  */
319 static inline wchar_t *
320 UniStrupr(register wchar_t *upin)
321 {
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  */
337 static inline wchar_t
338 UniTolower(wchar_t uc)
339 {
340         register struct UniCaseRange *rp;
341
342         if (uc < sizeof(UniLowerTable)) {
343                 /* Latin characters */
344                 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  */
361 static inline wchar_t *
362 UniStrlwr(register wchar_t *upin)
363 {
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