2 * fs/cifs/cifs_unicode.c
4 * Copyright (c) International Business Machines Corp., 2000,2009
5 * Modified by Steve French (sfrench@us.ibm.com)
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
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/slab.h>
23 #include "cifs_unicode.h"
24 #include "cifs_uniupr.h"
27 #include "cifs_debug.h"
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
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.
40 cifs_ucs2_bytes(const __le16 *from, int maxbytes,
41 const struct nls_table *codepage)
44 int charlen, outlen = 0;
45 int maxwords = maxbytes / 2;
46 char tmp[NLS_MAX_CHARSET_SIZE];
48 for (i = 0; i < maxwords && from[i]; i++) {
49 charlen = codepage->uni2char(le16_to_cpu(from[i]), tmp,
50 NLS_MAX_CHARSET_SIZE);
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?
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).
72 cifs_mapchar(char *target, const __le16 src_char, const struct nls_table *cp,
81 * BB: Cannot handle remapping UNI_SLASH until all the calls to
82 * build_path_from_dentry are modified, as they use slash as
85 switch (le16_to_cpu(src_char)) {
112 len = cp->uni2char(le16_to_cpu(src_char), target,
113 NLS_MAX_CHARSET_SIZE);
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?
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
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.
144 cifs_from_ucs2(char *to, const __le16 *from, int tolen, int fromlen,
145 const struct nls_table *codepage, bool mapchar)
147 int i, charlen, safelen;
149 int nullsize = nls_nullsize(codepage);
150 int fromwords = fromlen / 2;
151 char tmp[NLS_MAX_CHARSET_SIZE];
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.
159 safelen = tolen - (NLS_MAX_CHARSET_SIZE + nullsize);
161 for (i = 0; i < fromwords && from[i]; i++) {
163 * check to see if converting this character might make the
164 * conversion bleed into the null terminator
166 if (outlen >= safelen) {
167 charlen = cifs_mapchar(tmp, from[i], codepage, mapchar);
168 if ((outlen + charlen) > (tolen - nullsize))
172 /* put converted char into 'to' buffer */
173 charlen = cifs_mapchar(&to[outlen], from[i], codepage, mapchar);
177 /* properly null-terminate string */
178 for (i = 0; i < nullsize; i++)
185 * NAME: cifs_strtoUCS()
187 * FUNCTION: Convert character string to unicode string
191 cifs_strtoUCS(__le16 *to, const char *from, int len,
192 const struct nls_table *codepage)
196 wchar_t *wchar_to = (wchar_t *)to; /* needed to quiet sparse */
198 for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
200 /* works for 2.4.0 kernel or later */
201 charlen = codepage->char2uni(from, len, &wchar_to[i]);
203 cERROR(1, "strtoUCS: char2uni of %d returned %d",
204 (int)*from, charlen);
205 /* A question mark */
206 to[i] = cpu_to_le16(0x003f);
209 to[i] = cpu_to_le16(wchar_to[i]);
218 * cifs_strndup_from_ucs - copy a string from wire format to the local codepage
219 * @src - source string
220 * @maxlen - don't walk past this many bytes in the source string
221 * @is_unicode - is this a unicode string?
222 * @codepage - destination codepage
224 * Take a string given by the server, convert it to the local codepage and
225 * put it in a new buffer. Returns a pointer to the new string or NULL on
229 cifs_strndup_from_ucs(const char *src, const int maxlen, const bool is_unicode,
230 const struct nls_table *codepage)
236 len = cifs_ucs2_bytes((__le16 *) src, maxlen, codepage);
237 len += nls_nullsize(codepage);
238 dst = kmalloc(len, GFP_KERNEL);
241 cifs_from_ucs2(dst, (__le16 *) src, len, maxlen, codepage,
244 len = strnlen(src, maxlen);
246 dst = kmalloc(len, GFP_KERNEL);
249 strlcpy(dst, src, len);