net dst: need linux/cache.h for ____cacheline_aligned_in_smp.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / cifs / cifs_unicode.h
blob7fe6b52df5076a2eb14a7ec563403795e2e4c041
1 /*
2 * cifs_unicode: Unicode kernel case support
4 * Function:
5 * Convert a unicode character to upper or lower case using
6 * compressed tables.
8 * Copyright (c) International Business Machines Corp., 2000,2009
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.
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.
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
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.
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.
33 #ifndef _CIFS_UNICODE_H
34 #define _CIFS_UNICODE_H
36 #include <asm/byteorder.h>
37 #include <linux/types.h>
38 #include <linux/nls.h>
40 #define UNIUPR_NOLOWER /* Example to not expand lower case tables */
43 * Windows maps these to the user defined 16 bit Unicode range since they are
44 * reserved symbols (along with \ and /), otherwise illegal to store
45 * in filenames in NTFS
47 #define UNI_ASTERIK (__u16) ('*' + 0xF000)
48 #define UNI_QUESTION (__u16) ('?' + 0xF000)
49 #define UNI_COLON (__u16) (':' + 0xF000)
50 #define UNI_GRTRTHAN (__u16) ('>' + 0xF000)
51 #define UNI_LESSTHAN (__u16) ('<' + 0xF000)
52 #define UNI_PIPE (__u16) ('|' + 0xF000)
53 #define UNI_SLASH (__u16) ('\\' + 0xF000)
55 /* Just define what we want from uniupr.h. We don't want to define the tables
56 * in each source file.
58 #ifndef UNICASERANGE_DEFINED
59 struct UniCaseRange {
60 wchar_t start;
61 wchar_t end;
62 signed char *table;
64 #endif /* UNICASERANGE_DEFINED */
66 #ifndef UNIUPR_NOUPPER
67 extern signed char CifsUniUpperTable[512];
68 extern const struct UniCaseRange CifsUniUpperRange[];
69 #endif /* UNIUPR_NOUPPER */
71 #ifndef UNIUPR_NOLOWER
72 extern signed char CifsUniLowerTable[512];
73 extern const struct UniCaseRange CifsUniLowerRange[];
74 #endif /* UNIUPR_NOLOWER */
76 #ifdef __KERNEL__
77 int cifs_from_ucs2(char *to, const __le16 *from, int tolen, int fromlen,
78 const struct nls_table *codepage, bool mapchar);
79 int cifs_ucs2_bytes(const __le16 *from, int maxbytes,
80 const struct nls_table *codepage);
81 int cifs_strtoUCS(__le16 *, const char *, int, const struct nls_table *);
82 char *cifs_strndup_from_ucs(const char *src, const int maxlen,
83 const bool is_unicode,
84 const struct nls_table *codepage);
85 #endif
88 * UniStrcat: Concatenate the second string to the first
90 * Returns:
91 * Address of the first string
93 static inline wchar_t *
94 UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
96 wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
98 while (*ucs1++) ; /* To end of first string */
99 ucs1--; /* Return to the null */
100 while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
101 return anchor;
105 * UniStrchr: Find a character in a string
107 * Returns:
108 * Address of first occurrence of character in string
109 * or NULL if the character is not in the string
111 static inline wchar_t *
112 UniStrchr(const wchar_t *ucs, wchar_t uc)
114 while ((*ucs != uc) && *ucs)
115 ucs++;
117 if (*ucs == uc)
118 return (wchar_t *) ucs;
119 return NULL;
123 * UniStrcmp: Compare two strings
125 * Returns:
126 * < 0: First string is less than second
127 * = 0: Strings are equal
128 * > 0: First string is greater than second
130 static inline int
131 UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
133 while ((*ucs1 == *ucs2) && *ucs1) {
134 ucs1++;
135 ucs2++;
137 return (int) *ucs1 - (int) *ucs2;
141 * UniStrcpy: Copy a string
143 static inline wchar_t *
144 UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
146 wchar_t *anchor = ucs1; /* save the start of result string */
148 while ((*ucs1++ = *ucs2++)) ;
149 return anchor;
153 * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
155 static inline size_t
156 UniStrlen(const wchar_t *ucs1)
158 int i = 0;
160 while (*ucs1++)
161 i++;
162 return i;
166 * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
167 * string (length limited)
169 static inline size_t
170 UniStrnlen(const wchar_t *ucs1, int maxlen)
172 int i = 0;
174 while (*ucs1++) {
175 i++;
176 if (i >= maxlen)
177 break;
179 return i;
183 * UniStrncat: Concatenate length limited string
185 static inline wchar_t *
186 UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
188 wchar_t *anchor = ucs1; /* save pointer to string 1 */
190 while (*ucs1++) ;
191 ucs1--; /* point to null terminator of s1 */
192 while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
193 ucs1++;
194 ucs2++;
196 *ucs1 = 0; /* Null terminate the result */
197 return (anchor);
201 * UniStrncmp: Compare length limited string
203 static inline int
204 UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
206 if (!n)
207 return 0; /* Null strings are equal */
208 while ((*ucs1 == *ucs2) && *ucs1 && --n) {
209 ucs1++;
210 ucs2++;
212 return (int) *ucs1 - (int) *ucs2;
216 * UniStrncmp_le: Compare length limited string - native to little-endian
218 static inline int
219 UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
221 if (!n)
222 return 0; /* Null strings are equal */
223 while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
224 ucs1++;
225 ucs2++;
227 return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
231 * UniStrncpy: Copy length limited string with pad
233 static inline wchar_t *
234 UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
236 wchar_t *anchor = ucs1;
238 while (n-- && *ucs2) /* Copy the strings */
239 *ucs1++ = *ucs2++;
241 n++;
242 while (n--) /* Pad with nulls */
243 *ucs1++ = 0;
244 return anchor;
248 * UniStrncpy_le: Copy length limited string with pad to little-endian
250 static inline wchar_t *
251 UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
253 wchar_t *anchor = ucs1;
255 while (n-- && *ucs2) /* Copy the strings */
256 *ucs1++ = __le16_to_cpu(*ucs2++);
258 n++;
259 while (n--) /* Pad with nulls */
260 *ucs1++ = 0;
261 return anchor;
265 * UniStrstr: Find a string in a string
267 * Returns:
268 * Address of first match found
269 * NULL if no matching string is found
271 static inline wchar_t *
272 UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
274 const wchar_t *anchor1 = ucs1;
275 const wchar_t *anchor2 = ucs2;
277 while (*ucs1) {
278 if (*ucs1 == *ucs2) {
279 /* Partial match found */
280 ucs1++;
281 ucs2++;
282 } else {
283 if (!*ucs2) /* Match found */
284 return (wchar_t *) anchor1;
285 ucs1 = ++anchor1; /* No match */
286 ucs2 = anchor2;
290 if (!*ucs2) /* Both end together */
291 return (wchar_t *) anchor1; /* Match found */
292 return NULL; /* No match */
295 #ifndef UNIUPR_NOUPPER
297 * UniToupper: Convert a unicode character to upper case
299 static inline wchar_t
300 UniToupper(register wchar_t uc)
302 register const struct UniCaseRange *rp;
304 if (uc < sizeof(CifsUniUpperTable)) {
305 /* Latin characters */
306 return uc + CifsUniUpperTable[uc]; /* Use base tables */
307 } else {
308 rp = CifsUniUpperRange; /* Use range tables */
309 while (rp->start) {
310 if (uc < rp->start) /* Before start of range */
311 return uc; /* Uppercase = input */
312 if (uc <= rp->end) /* In range */
313 return uc + rp->table[uc - rp->start];
314 rp++; /* Try next range */
317 return uc; /* Past last range */
321 * UniStrupr: Upper case a unicode string
323 static inline wchar_t *
324 UniStrupr(register wchar_t *upin)
326 register wchar_t *up;
328 up = upin;
329 while (*up) { /* For all characters */
330 *up = UniToupper(*up);
331 up++;
333 return upin; /* Return input pointer */
335 #endif /* UNIUPR_NOUPPER */
337 #ifndef UNIUPR_NOLOWER
339 * UniTolower: Convert a unicode character to lower case
341 static inline wchar_t
342 UniTolower(register wchar_t uc)
344 register const struct UniCaseRange *rp;
346 if (uc < sizeof(CifsUniLowerTable)) {
347 /* Latin characters */
348 return uc + CifsUniLowerTable[uc]; /* Use base tables */
349 } else {
350 rp = CifsUniLowerRange; /* Use range tables */
351 while (rp->start) {
352 if (uc < rp->start) /* Before start of range */
353 return uc; /* Uppercase = input */
354 if (uc <= rp->end) /* In range */
355 return uc + rp->table[uc - rp->start];
356 rp++; /* Try next range */
359 return uc; /* Past last range */
363 * UniStrlwr: Lower case a unicode string
365 static inline wchar_t *
366 UniStrlwr(register wchar_t *upin)
368 register wchar_t *up;
370 up = upin;
371 while (*up) { /* For all characters */
372 *up = UniTolower(*up);
373 up++;
375 return upin; /* Return input pointer */
378 #endif
380 #endif /* _CIFS_UNICODE_H */