6aa6533e49fa0a5178b9bb9b22eb25473f36cdda
[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_strfromUCS_le(char *, const __le16 *, int, const struct nls_table *);
78 int cifs_strtoUCS(__le16 *, const char *, int, const struct nls_table *);
79 #endif
80
81 /*
82  * UniStrcat:  Concatenate the second string to the first
83  *
84  * Returns:
85  *     Address of the first string
86  */
87 static inline wchar_t *
88 UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
89 {
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  */
105 static inline wchar_t *
106 UniStrchr(const wchar_t *ucs, wchar_t uc)
107 {
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  */
124 static inline int
125 UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
126 {
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  */
137 static inline wchar_t *
138 UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
139 {
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  */
149 static inline size_t
150 UniStrlen(const wchar_t *ucs1)
151 {
152         int i = 0;
153
154         while (*ucs1++)
155                 i++;
156         return i;
157 }
158
159 /*
160  * UniStrnlen:  Return the length (in 16 bit Unicode chars not bytes) of a
161  *              string (length limited)
162  */
163 static inline size_t
164 UniStrnlen(const wchar_t *ucs1, int maxlen)
165 {
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  */
179 static inline wchar_t *
180 UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
181 {
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  */
197 static inline int
198 UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
199 {
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  */
212 static inline int
213 UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
214 {
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  */
227 static inline wchar_t *
228 UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
229 {
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  */
244 static inline wchar_t *
245 UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
246 {
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  */
265 static inline wchar_t *
266 UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
267 {
268         const wchar_t *anchor1 = ucs1;
269         const wchar_t *anchor2 = ucs2;
270
271         while (*ucs1) {
272                 if (*ucs1 == *ucs2) {
273                         /* Partial match found */
274                         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  */
293 static inline wchar_t
294 UniToupper(register wchar_t uc)
295 {
296         register const struct UniCaseRange *rp;
297
298         if (uc < sizeof(CifsUniUpperTable)) {
299                 /* Latin characters */
300                 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  */
317 static inline wchar_t *
318 UniStrupr(register wchar_t *upin)
319 {
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  */
335 static inline wchar_t
336 UniTolower(wchar_t uc)
337 {
338         register struct UniCaseRange *rp;
339
340         if (uc < sizeof(UniLowerTable)) {
341                 /* Latin characters */
342                 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  */
359 static inline wchar_t *
360 UniStrlwr(register wchar_t *upin)
361 {
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