Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/cifs_unicode.c |
| 3 | * |
Steve French | d185cda | 2009-04-30 17:45:10 +0000 | [diff] [blame] | 4 | * Copyright (c) International Business Machines Corp., 2000,2009 |
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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "cifs_unicode.h" |
| 24 | #include "cifs_uniupr.h" |
| 25 | #include "cifspdu.h" |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 26 | #include "cifsglob.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "cifs_debug.h" |
| 28 | |
| 29 | /* |
Jeff Layton | 69f801f | 2009-04-30 06:46:32 -0400 | [diff] [blame] | 30 | * cifs_ucs2_bytes - how long will a string be after conversion? |
| 31 | * @ucs - pointer to input string |
| 32 | * @maxbytes - don't go past this many bytes of input string |
| 33 | * @codepage - destination codepage |
| 34 | * |
| 35 | * Walk a ucs2le string and return the number of bytes that the string will |
| 36 | * be after being converted to the given charset, not including any null |
| 37 | * termination required. Don't walk past maxbytes in the source buffer. |
| 38 | */ |
| 39 | int |
| 40 | cifs_ucs2_bytes(const __le16 *from, int maxbytes, |
| 41 | const struct nls_table *codepage) |
| 42 | { |
| 43 | int i; |
| 44 | int charlen, outlen = 0; |
| 45 | int maxwords = maxbytes / 2; |
| 46 | char tmp[NLS_MAX_CHARSET_SIZE]; |
| 47 | |
Roel Kluin | 24e2fb6 | 2009-08-02 13:00:18 +0200 | [diff] [blame] | 48 | for (i = 0; i < maxwords && from[i]; i++) { |
Jeff Layton | 69f801f | 2009-04-30 06:46:32 -0400 | [diff] [blame] | 49 | charlen = codepage->uni2char(le16_to_cpu(from[i]), tmp, |
| 50 | NLS_MAX_CHARSET_SIZE); |
| 51 | if (charlen > 0) |
| 52 | outlen += charlen; |
| 53 | else |
| 54 | outlen++; |
| 55 | } |
| 56 | |
| 57 | return outlen; |
| 58 | } |
| 59 | |
| 60 | /* |
Jeff Layton | 7fabf0c | 2009-04-30 06:46:15 -0400 | [diff] [blame] | 61 | * cifs_mapchar - convert a little-endian char to proper char in codepage |
| 62 | * @target - where converted character should be copied |
| 63 | * @src_char - 2 byte little-endian source character |
| 64 | * @cp - codepage to which character should be converted |
| 65 | * @mapchar - should character be mapped according to mapchars mount option? |
| 66 | * |
| 67 | * This function handles the conversion of a single character. It is the |
| 68 | * responsibility of the caller to ensure that the target buffer is large |
| 69 | * enough to hold the result of the conversion (at least NLS_MAX_CHARSET_SIZE). |
| 70 | */ |
| 71 | static int |
| 72 | cifs_mapchar(char *target, const __le16 src_char, const struct nls_table *cp, |
| 73 | bool mapchar) |
| 74 | { |
| 75 | int len = 1; |
| 76 | |
| 77 | if (!mapchar) |
| 78 | goto cp_convert; |
| 79 | |
| 80 | /* |
| 81 | * BB: Cannot handle remapping UNI_SLASH until all the calls to |
| 82 | * build_path_from_dentry are modified, as they use slash as |
| 83 | * separator. |
| 84 | */ |
| 85 | switch (le16_to_cpu(src_char)) { |
| 86 | case UNI_COLON: |
| 87 | *target = ':'; |
| 88 | break; |
| 89 | case UNI_ASTERIK: |
| 90 | *target = '*'; |
| 91 | break; |
| 92 | case UNI_QUESTION: |
| 93 | *target = '?'; |
| 94 | break; |
| 95 | case UNI_PIPE: |
| 96 | *target = '|'; |
| 97 | break; |
| 98 | case UNI_GRTRTHAN: |
| 99 | *target = '>'; |
| 100 | break; |
| 101 | case UNI_LESSTHAN: |
| 102 | *target = '<'; |
| 103 | break; |
| 104 | default: |
| 105 | goto cp_convert; |
| 106 | } |
| 107 | |
| 108 | out: |
| 109 | return len; |
| 110 | |
| 111 | cp_convert: |
| 112 | len = cp->uni2char(le16_to_cpu(src_char), target, |
| 113 | NLS_MAX_CHARSET_SIZE); |
| 114 | if (len <= 0) { |
| 115 | *target = '?'; |
| 116 | len = 1; |
| 117 | } |
| 118 | goto out; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * cifs_from_ucs2 - convert utf16le string to local charset |
| 123 | * @to - destination buffer |
| 124 | * @from - source buffer |
| 125 | * @tolen - destination buffer size (in bytes) |
| 126 | * @fromlen - source buffer size (in bytes) |
| 127 | * @codepage - codepage to which characters should be converted |
| 128 | * @mapchar - should characters be remapped according to the mapchars option? |
| 129 | * |
| 130 | * Convert a little-endian ucs2le string (as sent by the server) to a string |
| 131 | * in the provided codepage. The tolen and fromlen parameters are to ensure |
| 132 | * that the code doesn't walk off of the end of the buffer (which is always |
| 133 | * a danger if the alignment of the source buffer is off). The destination |
| 134 | * string is always properly null terminated and fits in the destination |
| 135 | * buffer. Returns the length of the destination string in bytes (including |
| 136 | * null terminator). |
| 137 | * |
| 138 | * Note that some windows versions actually send multiword UTF-16 characters |
| 139 | * instead of straight UCS-2. The linux nls routines however aren't able to |
| 140 | * deal with those characters properly. In the event that we get some of |
| 141 | * those characters, they won't be translated properly. |
| 142 | */ |
| 143 | int |
| 144 | cifs_from_ucs2(char *to, const __le16 *from, int tolen, int fromlen, |
| 145 | const struct nls_table *codepage, bool mapchar) |
| 146 | { |
| 147 | int i, charlen, safelen; |
| 148 | int outlen = 0; |
| 149 | int nullsize = nls_nullsize(codepage); |
| 150 | int fromwords = fromlen / 2; |
| 151 | char tmp[NLS_MAX_CHARSET_SIZE]; |
| 152 | |
| 153 | /* |
| 154 | * because the chars can be of varying widths, we need to take care |
| 155 | * not to overflow the destination buffer when we get close to the |
| 156 | * end of it. Until we get to this offset, we don't need to check |
| 157 | * for overflow however. |
| 158 | */ |
| 159 | safelen = tolen - (NLS_MAX_CHARSET_SIZE + nullsize); |
| 160 | |
| 161 | for (i = 0; i < fromwords && from[i]; i++) { |
| 162 | /* |
| 163 | * check to see if converting this character might make the |
| 164 | * conversion bleed into the null terminator |
| 165 | */ |
| 166 | if (outlen >= safelen) { |
| 167 | charlen = cifs_mapchar(tmp, from[i], codepage, mapchar); |
| 168 | if ((outlen + charlen) > (tolen - nullsize)) |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | /* put converted char into 'to' buffer */ |
| 173 | charlen = cifs_mapchar(&to[outlen], from[i], codepage, mapchar); |
| 174 | outlen += charlen; |
| 175 | } |
| 176 | |
| 177 | /* properly null-terminate string */ |
| 178 | for (i = 0; i < nullsize; i++) |
| 179 | to[outlen++] = 0; |
| 180 | |
| 181 | return outlen; |
| 182 | } |
| 183 | |
| 184 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | * NAME: cifs_strtoUCS() |
| 186 | * |
| 187 | * FUNCTION: Convert character string to unicode string |
| 188 | * |
| 189 | */ |
| 190 | int |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 191 | cifs_strtoUCS(__le16 *to, const char *from, int len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | const struct nls_table *codepage) |
| 193 | { |
| 194 | int charlen; |
| 195 | int i; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 196 | wchar_t *wchar_to = (wchar_t *)to; /* needed to quiet sparse */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | for (i = 0; len && *from; i++, from += charlen, len -= charlen) { |
| 199 | |
| 200 | /* works for 2.4.0 kernel or later */ |
Steve French | e89dc92 | 2005-11-11 15:18:19 -0800 | [diff] [blame] | 201 | charlen = codepage->char2uni(from, len, &wchar_to[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | if (charlen < 1) { |
| 203 | cERROR(1, |
Steve French | 3a9f462 | 2007-04-04 17:10:24 +0000 | [diff] [blame] | 204 | ("strtoUCS: char2uni of %d returned %d", |
| 205 | (int)*from, charlen)); |
Steve French | 6911408 | 2005-11-10 19:28:44 -0800 | [diff] [blame] | 206 | /* A question mark */ |
Steve French | e89dc92 | 2005-11-11 15:18:19 -0800 | [diff] [blame] | 207 | to[i] = cpu_to_le16(0x003f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | charlen = 1; |
Steve French | 221601c | 2007-06-05 20:35:06 +0000 | [diff] [blame] | 209 | } else |
Steve French | e89dc92 | 2005-11-11 15:18:19 -0800 | [diff] [blame] | 210 | to[i] = cpu_to_le16(wchar_to[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | } |
| 213 | |
| 214 | to[i] = 0; |
| 215 | return i; |
| 216 | } |
| 217 | |
Jeff Layton | 066ce68 | 2009-04-30 07:16:14 -0400 | [diff] [blame] | 218 | /* |
Steve French | d185cda | 2009-04-30 17:45:10 +0000 | [diff] [blame] | 219 | * cifs_strndup_from_ucs - copy a string from wire format to the local codepage |
Jeff Layton | 066ce68 | 2009-04-30 07:16:14 -0400 | [diff] [blame] | 220 | * @src - source string |
| 221 | * @maxlen - don't walk past this many bytes in the source string |
| 222 | * @is_unicode - is this a unicode string? |
| 223 | * @codepage - destination codepage |
| 224 | * |
| 225 | * Take a string given by the server, convert it to the local codepage and |
| 226 | * put it in a new buffer. Returns a pointer to the new string or NULL on |
| 227 | * error. |
| 228 | */ |
| 229 | char * |
Steve French | d185cda | 2009-04-30 17:45:10 +0000 | [diff] [blame] | 230 | cifs_strndup_from_ucs(const char *src, const int maxlen, const bool is_unicode, |
Jeff Layton | 066ce68 | 2009-04-30 07:16:14 -0400 | [diff] [blame] | 231 | const struct nls_table *codepage) |
| 232 | { |
| 233 | int len; |
| 234 | char *dst; |
| 235 | |
| 236 | if (is_unicode) { |
| 237 | len = cifs_ucs2_bytes((__le16 *) src, maxlen, codepage); |
| 238 | len += nls_nullsize(codepage); |
| 239 | dst = kmalloc(len, GFP_KERNEL); |
| 240 | if (!dst) |
| 241 | return NULL; |
| 242 | cifs_from_ucs2(dst, (__le16 *) src, len, maxlen, codepage, |
| 243 | false); |
| 244 | } else { |
| 245 | len = strnlen(src, maxlen); |
| 246 | len++; |
| 247 | dst = kmalloc(len, GFP_KERNEL); |
| 248 | if (!dst) |
| 249 | return NULL; |
| 250 | strlcpy(dst, src, len); |
| 251 | } |
| 252 | |
| 253 | return dst; |
| 254 | } |
| 255 | |