]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | * | |
d185cda7 | 8 | * Copyright (c) International Business Machines Corp., 2000,2009 |
1da177e4 LT |
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 | |
d38d8c74 | 12 | * the Free Software Foundation; either version 2 of the License, or |
1da177e4 | 13 | * (at your option) any later version. |
d38d8c74 | 14 | * |
1da177e4 LT |
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 | |
d38d8c74 | 21 | * along with this program; if not, write to the Free Software |
1da177e4 LT |
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 | ||
66345f50 JL |
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 | ||
1da177e4 LT |
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__ | |
7fabf0c9 JL |
75 | int cifs_from_ucs2(char *to, const __le16 *from, int tolen, int fromlen, |
76 | const struct nls_table *codepage, bool mapchar); | |
69f801fc JL |
77 | int cifs_ucs2_bytes(const __le16 *from, int maxbytes, |
78 | const struct nls_table *codepage); | |
e89dc920 | 79 | int cifs_strtoUCS(__le16 *, const char *, int, const struct nls_table *); |
d185cda7 SF |
80 | char *cifs_strndup_from_ucs(const char *src, const int maxlen, |
81 | const bool is_unicode, | |
82 | const struct nls_table *codepage); | |
1da177e4 LT |
83 | #endif |
84 | ||
85 | /* | |
86 | * UniStrcat: Concatenate the second string to the first | |
87 | * | |
88 | * Returns: | |
89 | * Address of the first string | |
90 | */ | |
91 | static inline wchar_t * | |
50c2f753 | 92 | UniStrcat(wchar_t *ucs1, const wchar_t *ucs2) |
1da177e4 LT |
93 | { |
94 | wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */ | |
95 | ||
96 | while (*ucs1++) ; /* To end of first string */ | |
97 | ucs1--; /* Return to the null */ | |
98 | while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */ | |
99 | return anchor; | |
100 | } | |
101 | ||
102 | /* | |
103 | * UniStrchr: Find a character in a string | |
104 | * | |
105 | * Returns: | |
106 | * Address of first occurrence of character in string | |
107 | * or NULL if the character is not in the string | |
108 | */ | |
109 | static inline wchar_t * | |
50c2f753 | 110 | UniStrchr(const wchar_t *ucs, wchar_t uc) |
1da177e4 LT |
111 | { |
112 | while ((*ucs != uc) && *ucs) | |
113 | ucs++; | |
114 | ||
115 | if (*ucs == uc) | |
116 | return (wchar_t *) ucs; | |
117 | return NULL; | |
118 | } | |
119 | ||
120 | /* | |
121 | * UniStrcmp: Compare two strings | |
122 | * | |
123 | * Returns: | |
124 | * < 0: First string is less than second | |
125 | * = 0: Strings are equal | |
126 | * > 0: First string is greater than second | |
127 | */ | |
128 | static inline int | |
50c2f753 | 129 | UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2) |
1da177e4 LT |
130 | { |
131 | while ((*ucs1 == *ucs2) && *ucs1) { | |
132 | ucs1++; | |
133 | ucs2++; | |
134 | } | |
135 | return (int) *ucs1 - (int) *ucs2; | |
136 | } | |
137 | ||
138 | /* | |
139 | * UniStrcpy: Copy a string | |
140 | */ | |
141 | static inline wchar_t * | |
50c2f753 | 142 | UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2) |
1da177e4 LT |
143 | { |
144 | wchar_t *anchor = ucs1; /* save the start of result string */ | |
145 | ||
146 | while ((*ucs1++ = *ucs2++)) ; | |
147 | return anchor; | |
148 | } | |
149 | ||
150 | /* | |
151 | * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes) | |
152 | */ | |
153 | static inline size_t | |
50c2f753 | 154 | UniStrlen(const wchar_t *ucs1) |
1da177e4 LT |
155 | { |
156 | int i = 0; | |
157 | ||
158 | while (*ucs1++) | |
159 | i++; | |
160 | return i; | |
161 | } | |
162 | ||
163 | /* | |
d38d8c74 SF |
164 | * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a |
165 | * string (length limited) | |
1da177e4 LT |
166 | */ |
167 | static inline size_t | |
50c2f753 | 168 | UniStrnlen(const wchar_t *ucs1, int maxlen) |
1da177e4 LT |
169 | { |
170 | int i = 0; | |
171 | ||
172 | while (*ucs1++) { | |
173 | i++; | |
174 | if (i >= maxlen) | |
175 | break; | |
176 | } | |
177 | return i; | |
178 | } | |
179 | ||
180 | /* | |
181 | * UniStrncat: Concatenate length limited string | |
182 | */ | |
183 | static inline wchar_t * | |
50c2f753 | 184 | UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n) |
1da177e4 LT |
185 | { |
186 | wchar_t *anchor = ucs1; /* save pointer to string 1 */ | |
187 | ||
188 | while (*ucs1++) ; | |
189 | ucs1--; /* point to null terminator of s1 */ | |
190 | while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */ | |
191 | ucs1++; | |
192 | ucs2++; | |
193 | } | |
194 | *ucs1 = 0; /* Null terminate the result */ | |
195 | return (anchor); | |
196 | } | |
197 | ||
198 | /* | |
199 | * UniStrncmp: Compare length limited string | |
200 | */ | |
201 | static inline int | |
50c2f753 | 202 | UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n) |
1da177e4 LT |
203 | { |
204 | if (!n) | |
205 | return 0; /* Null strings are equal */ | |
206 | while ((*ucs1 == *ucs2) && *ucs1 && --n) { | |
207 | ucs1++; | |
208 | ucs2++; | |
209 | } | |
210 | return (int) *ucs1 - (int) *ucs2; | |
211 | } | |
212 | ||
213 | /* | |
214 | * UniStrncmp_le: Compare length limited string - native to little-endian | |
215 | */ | |
216 | static inline int | |
50c2f753 | 217 | UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n) |
1da177e4 LT |
218 | { |
219 | if (!n) | |
220 | return 0; /* Null strings are equal */ | |
221 | while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) { | |
222 | ucs1++; | |
223 | ucs2++; | |
224 | } | |
225 | return (int) *ucs1 - (int) __le16_to_cpu(*ucs2); | |
226 | } | |
227 | ||
228 | /* | |
229 | * UniStrncpy: Copy length limited string with pad | |
230 | */ | |
231 | static inline wchar_t * | |
50c2f753 | 232 | UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n) |
1da177e4 LT |
233 | { |
234 | wchar_t *anchor = ucs1; | |
235 | ||
236 | while (n-- && *ucs2) /* Copy the strings */ | |
237 | *ucs1++ = *ucs2++; | |
238 | ||
239 | n++; | |
240 | while (n--) /* Pad with nulls */ | |
241 | *ucs1++ = 0; | |
242 | return anchor; | |
243 | } | |
244 | ||
245 | /* | |
246 | * UniStrncpy_le: Copy length limited string with pad to little-endian | |
247 | */ | |
248 | static inline wchar_t * | |
50c2f753 | 249 | UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n) |
1da177e4 LT |
250 | { |
251 | wchar_t *anchor = ucs1; | |
252 | ||
253 | while (n-- && *ucs2) /* Copy the strings */ | |
254 | *ucs1++ = __le16_to_cpu(*ucs2++); | |
255 | ||
256 | n++; | |
257 | while (n--) /* Pad with nulls */ | |
258 | *ucs1++ = 0; | |
259 | return anchor; | |
260 | } | |
261 | ||
262 | /* | |
263 | * UniStrstr: Find a string in a string | |
264 | * | |
265 | * Returns: | |
266 | * Address of first match found | |
267 | * NULL if no matching string is found | |
268 | */ | |
269 | static inline wchar_t * | |
50c2f753 | 270 | UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2) |
1da177e4 LT |
271 | { |
272 | const wchar_t *anchor1 = ucs1; | |
273 | const wchar_t *anchor2 = ucs2; | |
274 | ||
275 | while (*ucs1) { | |
ad7a2926 SF |
276 | if (*ucs1 == *ucs2) { |
277 | /* Partial match found */ | |
1da177e4 LT |
278 | ucs1++; |
279 | ucs2++; | |
280 | } else { | |
281 | if (!*ucs2) /* Match found */ | |
282 | return (wchar_t *) anchor1; | |
283 | ucs1 = ++anchor1; /* No match */ | |
284 | ucs2 = anchor2; | |
285 | } | |
286 | } | |
287 | ||
288 | if (!*ucs2) /* Both end together */ | |
289 | return (wchar_t *) anchor1; /* Match found */ | |
290 | return NULL; /* No match */ | |
291 | } | |
292 | ||
293 | #ifndef UNIUPR_NOUPPER | |
294 | /* | |
295 | * UniToupper: Convert a unicode character to upper case | |
296 | */ | |
297 | static inline wchar_t | |
298 | UniToupper(register wchar_t uc) | |
299 | { | |
300 | register const struct UniCaseRange *rp; | |
301 | ||
ad7a2926 SF |
302 | if (uc < sizeof(CifsUniUpperTable)) { |
303 | /* Latin characters */ | |
1da177e4 LT |
304 | return uc + CifsUniUpperTable[uc]; /* Use base tables */ |
305 | } else { | |
306 | rp = CifsUniUpperRange; /* Use range tables */ | |
307 | while (rp->start) { | |
308 | if (uc < rp->start) /* Before start of range */ | |
309 | return uc; /* Uppercase = input */ | |
310 | if (uc <= rp->end) /* In range */ | |
311 | return uc + rp->table[uc - rp->start]; | |
312 | rp++; /* Try next range */ | |
313 | } | |
314 | } | |
315 | return uc; /* Past last range */ | |
316 | } | |
317 | ||
318 | /* | |
319 | * UniStrupr: Upper case a unicode string | |
320 | */ | |
321 | static inline wchar_t * | |
50c2f753 | 322 | UniStrupr(register wchar_t *upin) |
1da177e4 LT |
323 | { |
324 | register wchar_t *up; | |
325 | ||
326 | up = upin; | |
327 | while (*up) { /* For all characters */ | |
328 | *up = UniToupper(*up); | |
329 | up++; | |
330 | } | |
331 | return upin; /* Return input pointer */ | |
332 | } | |
333 | #endif /* UNIUPR_NOUPPER */ | |
334 | ||
335 | #ifndef UNIUPR_NOLOWER | |
336 | /* | |
337 | * UniTolower: Convert a unicode character to lower case | |
338 | */ | |
339 | static inline wchar_t | |
340 | UniTolower(wchar_t uc) | |
341 | { | |
342 | register struct UniCaseRange *rp; | |
343 | ||
ad7a2926 SF |
344 | if (uc < sizeof(UniLowerTable)) { |
345 | /* Latin characters */ | |
1da177e4 LT |
346 | return uc + UniLowerTable[uc]; /* Use base tables */ |
347 | } else { | |
348 | rp = UniLowerRange; /* Use range tables */ | |
349 | while (rp->start) { | |
350 | if (uc < rp->start) /* Before start of range */ | |
351 | return uc; /* Uppercase = input */ | |
352 | if (uc <= rp->end) /* In range */ | |
353 | return uc + rp->table[uc - rp->start]; | |
354 | rp++; /* Try next range */ | |
355 | } | |
356 | } | |
357 | return uc; /* Past last range */ | |
358 | } | |
359 | ||
360 | /* | |
361 | * UniStrlwr: Lower case a unicode string | |
362 | */ | |
363 | static inline wchar_t * | |
50c2f753 | 364 | UniStrlwr(register wchar_t *upin) |
1da177e4 LT |
365 | { |
366 | register wchar_t *up; | |
367 | ||
368 | up = upin; | |
369 | while (*up) { /* For all characters */ | |
370 | *up = UniTolower(*up); | |
371 | up++; | |
372 | } | |
373 | return upin; /* Return input pointer */ | |
374 | } | |
375 | ||
376 | #endif |