| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *   fs/cifs/cifs_unicode.c | 
 | 3 |  * | 
| Steve French | e89dc92 | 2005-11-11 15:18:19 -0800 | [diff] [blame] | 4 |  *   Copyright (c) International Business Machines  Corp., 2000,2005 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 |  *   Modified by Steve French (sfrench@us.ibm.com) | 
 | 6 |  * | 
 | 7 |  *   This program is free software;  you can redistribute it and/or modify | 
 | 8 |  *   it under the terms of the GNU General Public License as published by | 
| Steve French | 221601c | 2007-06-05 20:35:06 +0000 | [diff] [blame] | 9 |  *   the Free Software Foundation; either version 2 of the License, or | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 |  *   (at your option) any later version. | 
| Steve French | 221601c | 2007-06-05 20:35:06 +0000 | [diff] [blame] | 11 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 |  *   This program is distributed in the hope that it will be useful, | 
 | 13 |  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of | 
 | 14 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See | 
 | 15 |  *   the GNU General Public License for more details. | 
 | 16 |  * | 
 | 17 |  *   You should have received a copy of the GNU General Public License | 
| Steve French | 221601c | 2007-06-05 20:35:06 +0000 | [diff] [blame] | 18 |  *   along with this program;  if not, write to the Free Software | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 |  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 
 | 20 |  */ | 
 | 21 | #include <linux/fs.h> | 
 | 22 | #include "cifs_unicode.h" | 
 | 23 | #include "cifs_uniupr.h" | 
 | 24 | #include "cifspdu.h" | 
| Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 25 | #include "cifsglob.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "cifs_debug.h" | 
 | 27 |  | 
 | 28 | /* | 
 | 29 |  * NAME:	cifs_strfromUCS() | 
 | 30 |  * | 
 | 31 |  * FUNCTION:	Convert little-endian unicode string to character string | 
 | 32 |  * | 
 | 33 |  */ | 
 | 34 | int | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 35 | cifs_strfromUCS_le(char *to, const __le16 *from, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | 		   int len, const struct nls_table *codepage) | 
 | 37 | { | 
 | 38 | 	int i; | 
 | 39 | 	int outlen = 0; | 
 | 40 |  | 
 | 41 | 	for (i = 0; (i < len) && from[i]; i++) { | 
 | 42 | 		int charlen; | 
 | 43 | 		/* 2.4.0 kernel or greater */ | 
 | 44 | 		charlen = | 
 | 45 | 		    codepage->uni2char(le16_to_cpu(from[i]), &to[outlen], | 
 | 46 | 				       NLS_MAX_CHARSET_SIZE); | 
 | 47 | 		if (charlen > 0) { | 
 | 48 | 			outlen += charlen; | 
 | 49 | 		} else { | 
 | 50 | 			to[outlen++] = '?'; | 
 | 51 | 		} | 
 | 52 | 	} | 
 | 53 | 	to[outlen] = 0; | 
 | 54 | 	return outlen; | 
 | 55 | } | 
 | 56 |  | 
 | 57 | /* | 
 | 58 |  * NAME:	cifs_strtoUCS() | 
 | 59 |  * | 
 | 60 |  * FUNCTION:	Convert character string to unicode string | 
 | 61 |  * | 
 | 62 |  */ | 
 | 63 | int | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 64 | cifs_strtoUCS(__le16 *to, const char *from, int len, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | 	      const struct nls_table *codepage) | 
 | 66 | { | 
 | 67 | 	int charlen; | 
 | 68 | 	int i; | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 69 | 	wchar_t *wchar_to = (wchar_t *)to; /* needed to quiet sparse */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 |  | 
 | 71 | 	for (i = 0; len && *from; i++, from += charlen, len -= charlen) { | 
 | 72 |  | 
 | 73 | 		/* works for 2.4.0 kernel or later */ | 
| Steve French | e89dc92 | 2005-11-11 15:18:19 -0800 | [diff] [blame] | 74 | 		charlen = codepage->char2uni(from, len, &wchar_to[i]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | 		if (charlen < 1) { | 
 | 76 | 			cERROR(1, | 
| Steve French | 3a9f462 | 2007-04-04 17:10:24 +0000 | [diff] [blame] | 77 | 			       ("strtoUCS: char2uni of %d returned %d", | 
 | 78 | 				(int)*from, charlen)); | 
| Steve French | 6911408 | 2005-11-10 19:28:44 -0800 | [diff] [blame] | 79 | 			/* A question mark */ | 
| Steve French | e89dc92 | 2005-11-11 15:18:19 -0800 | [diff] [blame] | 80 | 			to[i] = cpu_to_le16(0x003f); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | 			charlen = 1; | 
| Steve French | 221601c | 2007-06-05 20:35:06 +0000 | [diff] [blame] | 82 | 		} else | 
| Steve French | e89dc92 | 2005-11-11 15:18:19 -0800 | [diff] [blame] | 83 | 			to[i] = cpu_to_le16(wchar_to[i]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 |  | 
 | 85 | 	} | 
 | 86 |  | 
 | 87 | 	to[i] = 0; | 
 | 88 | 	return i; | 
 | 89 | } | 
 | 90 |  |