| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  * | 
| Steve French | d38d8c7 | 2007-06-28 19:44:13 +0000 | [diff] [blame] | 8 |  *   Copyright (c) International Business Machines  Corp., 2000,2007 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 
| Steve French | d38d8c7 | 2007-06-28 19:44:13 +0000 | [diff] [blame] | 12 |  *   the Free Software Foundation; either version 2 of the License, or | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  *   (at your option) any later version. | 
| Steve French | d38d8c7 | 2007-06-28 19:44:13 +0000 | [diff] [blame] | 14 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 
| Steve French | d38d8c7 | 2007-06-28 19:44:13 +0000 | [diff] [blame] | 21 |  *   along with this program;  if not, write to the Free Software | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /* Just define what we want from uniupr.h.  We don't want to define the tables | 
 | 41 |  * in each source file. | 
 | 42 |  */ | 
 | 43 | #ifndef	UNICASERANGE_DEFINED | 
 | 44 | struct UniCaseRange { | 
 | 45 | 	wchar_t start; | 
 | 46 | 	wchar_t end; | 
 | 47 | 	signed char *table; | 
 | 48 | }; | 
 | 49 | #endif				/* UNICASERANGE_DEFINED */ | 
 | 50 |  | 
 | 51 | #ifndef UNIUPR_NOUPPER | 
 | 52 | extern signed char CifsUniUpperTable[512]; | 
 | 53 | extern const struct UniCaseRange CifsUniUpperRange[]; | 
 | 54 | #endif				/* UNIUPR_NOUPPER */ | 
 | 55 |  | 
 | 56 | #ifndef UNIUPR_NOLOWER | 
 | 57 | extern signed char UniLowerTable[512]; | 
 | 58 | extern struct UniCaseRange UniLowerRange[]; | 
 | 59 | #endif				/* UNIUPR_NOLOWER */ | 
 | 60 |  | 
 | 61 | #ifdef __KERNEL__ | 
| Steve French | e89dc92 | 2005-11-11 15:18:19 -0800 | [diff] [blame] | 62 | int cifs_strfromUCS_le(char *, const __le16 *, int, const struct nls_table *); | 
 | 63 | int cifs_strtoUCS(__le16 *, const char *, int, const struct nls_table *); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #endif | 
 | 65 |  | 
 | 66 | /* | 
 | 67 |  * UniStrcat:  Concatenate the second string to the first | 
 | 68 |  * | 
 | 69 |  * Returns: | 
 | 70 |  *     Address of the first string | 
 | 71 |  */ | 
 | 72 | static inline wchar_t * | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 73 | UniStrcat(wchar_t *ucs1, const wchar_t *ucs2) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { | 
 | 75 | 	wchar_t *anchor = ucs1;	/* save a pointer to start of ucs1 */ | 
 | 76 |  | 
 | 77 | 	while (*ucs1++) ;	/* To end of first string */ | 
 | 78 | 	ucs1--;			/* Return to the null */ | 
 | 79 | 	while ((*ucs1++ = *ucs2++)) ;	/* copy string 2 over */ | 
 | 80 | 	return anchor; | 
 | 81 | } | 
 | 82 |  | 
 | 83 | /* | 
 | 84 |  * UniStrchr:  Find a character in a string | 
 | 85 |  * | 
 | 86 |  * Returns: | 
 | 87 |  *     Address of first occurrence of character in string | 
 | 88 |  *     or NULL if the character is not in the string | 
 | 89 |  */ | 
 | 90 | static inline wchar_t * | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 91 | UniStrchr(const wchar_t *ucs, wchar_t uc) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { | 
 | 93 | 	while ((*ucs != uc) && *ucs) | 
 | 94 | 		ucs++; | 
 | 95 |  | 
 | 96 | 	if (*ucs == uc) | 
 | 97 | 		return (wchar_t *) ucs; | 
 | 98 | 	return NULL; | 
 | 99 | } | 
 | 100 |  | 
 | 101 | /* | 
 | 102 |  * UniStrcmp:  Compare two strings | 
 | 103 |  * | 
 | 104 |  * Returns: | 
 | 105 |  *     < 0:  First string is less than second | 
 | 106 |  *     = 0:  Strings are equal | 
 | 107 |  *     > 0:  First string is greater than second | 
 | 108 |  */ | 
 | 109 | static inline int | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 110 | UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { | 
 | 112 | 	while ((*ucs1 == *ucs2) && *ucs1) { | 
 | 113 | 		ucs1++; | 
 | 114 | 		ucs2++; | 
 | 115 | 	} | 
 | 116 | 	return (int) *ucs1 - (int) *ucs2; | 
 | 117 | } | 
 | 118 |  | 
 | 119 | /* | 
 | 120 |  * UniStrcpy:  Copy a string | 
 | 121 |  */ | 
 | 122 | static inline wchar_t * | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 123 | UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { | 
 | 125 | 	wchar_t *anchor = ucs1;	/* save the start of result string */ | 
 | 126 |  | 
 | 127 | 	while ((*ucs1++ = *ucs2++)) ; | 
 | 128 | 	return anchor; | 
 | 129 | } | 
 | 130 |  | 
 | 131 | /* | 
 | 132 |  * UniStrlen:  Return the length of a string (in 16 bit Unicode chars not bytes) | 
 | 133 |  */ | 
 | 134 | static inline size_t | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 135 | UniStrlen(const wchar_t *ucs1) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { | 
 | 137 | 	int i = 0; | 
 | 138 |  | 
 | 139 | 	while (*ucs1++) | 
 | 140 | 		i++; | 
 | 141 | 	return i; | 
 | 142 | } | 
 | 143 |  | 
 | 144 | /* | 
| Steve French | d38d8c7 | 2007-06-28 19:44:13 +0000 | [diff] [blame] | 145 |  * UniStrnlen:  Return the length (in 16 bit Unicode chars not bytes) of a | 
 | 146 |  *		string (length limited) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 |  */ | 
 | 148 | static inline size_t | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 149 | UniStrnlen(const wchar_t *ucs1, int maxlen) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { | 
 | 151 | 	int i = 0; | 
 | 152 |  | 
 | 153 | 	while (*ucs1++) { | 
 | 154 | 		i++; | 
 | 155 | 		if (i >= maxlen) | 
 | 156 | 			break; | 
 | 157 | 	} | 
 | 158 | 	return i; | 
 | 159 | } | 
 | 160 |  | 
 | 161 | /* | 
 | 162 |  * UniStrncat:  Concatenate length limited string | 
 | 163 |  */ | 
 | 164 | static inline wchar_t * | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 165 | UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { | 
 | 167 | 	wchar_t *anchor = ucs1;	/* save pointer to string 1 */ | 
 | 168 |  | 
 | 169 | 	while (*ucs1++) ; | 
 | 170 | 	ucs1--;			/* point to null terminator of s1 */ | 
 | 171 | 	while (n-- && (*ucs1 = *ucs2)) {	/* copy s2 after s1 */ | 
 | 172 | 		ucs1++; | 
 | 173 | 		ucs2++; | 
 | 174 | 	} | 
 | 175 | 	*ucs1 = 0;		/* Null terminate the result */ | 
 | 176 | 	return (anchor); | 
 | 177 | } | 
 | 178 |  | 
 | 179 | /* | 
 | 180 |  * UniStrncmp:  Compare length limited string | 
 | 181 |  */ | 
 | 182 | static inline int | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 183 | UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { | 
 | 185 | 	if (!n) | 
 | 186 | 		return 0;	/* Null strings are equal */ | 
 | 187 | 	while ((*ucs1 == *ucs2) && *ucs1 && --n) { | 
 | 188 | 		ucs1++; | 
 | 189 | 		ucs2++; | 
 | 190 | 	} | 
 | 191 | 	return (int) *ucs1 - (int) *ucs2; | 
 | 192 | } | 
 | 193 |  | 
 | 194 | /* | 
 | 195 |  * UniStrncmp_le:  Compare length limited string - native to little-endian | 
 | 196 |  */ | 
 | 197 | static inline int | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 198 | UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { | 
 | 200 | 	if (!n) | 
 | 201 | 		return 0;	/* Null strings are equal */ | 
 | 202 | 	while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) { | 
 | 203 | 		ucs1++; | 
 | 204 | 		ucs2++; | 
 | 205 | 	} | 
 | 206 | 	return (int) *ucs1 - (int) __le16_to_cpu(*ucs2); | 
 | 207 | } | 
 | 208 |  | 
 | 209 | /* | 
 | 210 |  * UniStrncpy:  Copy length limited string with pad | 
 | 211 |  */ | 
 | 212 | static inline wchar_t * | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 213 | UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { | 
 | 215 | 	wchar_t *anchor = ucs1; | 
 | 216 |  | 
 | 217 | 	while (n-- && *ucs2)	/* Copy the strings */ | 
 | 218 | 		*ucs1++ = *ucs2++; | 
 | 219 |  | 
 | 220 | 	n++; | 
 | 221 | 	while (n--)		/* Pad with nulls */ | 
 | 222 | 		*ucs1++ = 0; | 
 | 223 | 	return anchor; | 
 | 224 | } | 
 | 225 |  | 
 | 226 | /* | 
 | 227 |  * UniStrncpy_le:  Copy length limited string with pad to little-endian | 
 | 228 |  */ | 
 | 229 | static inline wchar_t * | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 230 | UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { | 
 | 232 | 	wchar_t *anchor = ucs1; | 
 | 233 |  | 
 | 234 | 	while (n-- && *ucs2)	/* Copy the strings */ | 
 | 235 | 		*ucs1++ = __le16_to_cpu(*ucs2++); | 
 | 236 |  | 
 | 237 | 	n++; | 
 | 238 | 	while (n--)		/* Pad with nulls */ | 
 | 239 | 		*ucs1++ = 0; | 
 | 240 | 	return anchor; | 
 | 241 | } | 
 | 242 |  | 
 | 243 | /* | 
 | 244 |  * UniStrstr:  Find a string in a string | 
 | 245 |  * | 
 | 246 |  * Returns: | 
 | 247 |  *     Address of first match found | 
 | 248 |  *     NULL if no matching string is found | 
 | 249 |  */ | 
 | 250 | static inline wchar_t * | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 251 | UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { | 
 | 253 | 	const wchar_t *anchor1 = ucs1; | 
 | 254 | 	const wchar_t *anchor2 = ucs2; | 
 | 255 |  | 
 | 256 | 	while (*ucs1) { | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 257 | 		if (*ucs1 == *ucs2) { | 
 | 258 | 			/* Partial match found */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | 			ucs1++; | 
 | 260 | 			ucs2++; | 
 | 261 | 		} else { | 
 | 262 | 			if (!*ucs2)	/* Match found */ | 
 | 263 | 				return (wchar_t *) anchor1; | 
 | 264 | 			ucs1 = ++anchor1;	/* No match */ | 
 | 265 | 			ucs2 = anchor2; | 
 | 266 | 		} | 
 | 267 | 	} | 
 | 268 |  | 
 | 269 | 	if (!*ucs2)		/* Both end together */ | 
 | 270 | 		return (wchar_t *) anchor1;	/* Match found */ | 
 | 271 | 	return NULL;		/* No match */ | 
 | 272 | } | 
 | 273 |  | 
 | 274 | #ifndef UNIUPR_NOUPPER | 
 | 275 | /* | 
 | 276 |  * UniToupper:  Convert a unicode character to upper case | 
 | 277 |  */ | 
 | 278 | static inline wchar_t | 
 | 279 | UniToupper(register wchar_t uc) | 
 | 280 | { | 
 | 281 | 	register const struct UniCaseRange *rp; | 
 | 282 |  | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 283 | 	if (uc < sizeof(CifsUniUpperTable)) { | 
 | 284 | 		/* Latin characters */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | 		return uc + CifsUniUpperTable[uc];	/* Use base tables */ | 
 | 286 | 	} else { | 
 | 287 | 		rp = CifsUniUpperRange;	/* Use range tables */ | 
 | 288 | 		while (rp->start) { | 
 | 289 | 			if (uc < rp->start)	/* Before start of range */ | 
 | 290 | 				return uc;	/* Uppercase = input */ | 
 | 291 | 			if (uc <= rp->end)	/* In range */ | 
 | 292 | 				return uc + rp->table[uc - rp->start]; | 
 | 293 | 			rp++;	/* Try next range */ | 
 | 294 | 		} | 
 | 295 | 	} | 
 | 296 | 	return uc;		/* Past last range */ | 
 | 297 | } | 
 | 298 |  | 
 | 299 | /* | 
 | 300 |  * UniStrupr:  Upper case a unicode string | 
 | 301 |  */ | 
 | 302 | static inline wchar_t * | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 303 | UniStrupr(register wchar_t *upin) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { | 
 | 305 | 	register wchar_t *up; | 
 | 306 |  | 
 | 307 | 	up = upin; | 
 | 308 | 	while (*up) {		/* For all characters */ | 
 | 309 | 		*up = UniToupper(*up); | 
 | 310 | 		up++; | 
 | 311 | 	} | 
 | 312 | 	return upin;		/* Return input pointer */ | 
 | 313 | } | 
 | 314 | #endif				/* UNIUPR_NOUPPER */ | 
 | 315 |  | 
 | 316 | #ifndef UNIUPR_NOLOWER | 
 | 317 | /* | 
 | 318 |  * UniTolower:  Convert a unicode character to lower case | 
 | 319 |  */ | 
 | 320 | static inline wchar_t | 
 | 321 | UniTolower(wchar_t uc) | 
 | 322 | { | 
 | 323 | 	register struct UniCaseRange *rp; | 
 | 324 |  | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 325 | 	if (uc < sizeof(UniLowerTable)) { | 
 | 326 | 		/* Latin characters */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | 		return uc + UniLowerTable[uc];	/* Use base tables */ | 
 | 328 | 	} else { | 
 | 329 | 		rp = UniLowerRange;	/* Use range tables */ | 
 | 330 | 		while (rp->start) { | 
 | 331 | 			if (uc < rp->start)	/* Before start of range */ | 
 | 332 | 				return uc;	/* Uppercase = input */ | 
 | 333 | 			if (uc <= rp->end)	/* In range */ | 
 | 334 | 				return uc + rp->table[uc - rp->start]; | 
 | 335 | 			rp++;	/* Try next range */ | 
 | 336 | 		} | 
 | 337 | 	} | 
 | 338 | 	return uc;		/* Past last range */ | 
 | 339 | } | 
 | 340 |  | 
 | 341 | /* | 
 | 342 |  * UniStrlwr:  Lower case a unicode string | 
 | 343 |  */ | 
 | 344 | static inline wchar_t * | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 345 | UniStrlwr(register wchar_t *upin) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { | 
 | 347 | 	register wchar_t *up; | 
 | 348 |  | 
 | 349 | 	up = upin; | 
 | 350 | 	while (*up) {		/* For all characters */ | 
 | 351 | 		*up = UniTolower(*up); | 
 | 352 | 		up++; | 
 | 353 | 	} | 
 | 354 | 	return upin;		/* Return input pointer */ | 
 | 355 | } | 
 | 356 |  | 
 | 357 | #endif |