blob: 0aa2c5c2cfe2165dd94ccb6ae885f241490a29b3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/cifs_unicode.c
3 *
Steve Frenchd185cda2009-04-30 17:45:10 +00004 * Copyright (c) International Business Machines Corp., 2000,2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 French221601c2007-06-05 20:35:06 +00009 * the Free Software Foundation; either version 2 of the License, or
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * (at your option) any later version.
Steve French221601c2007-06-05 20:35:06 +000011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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 French221601c2007-06-05 20:35:06 +000018 * along with this program; if not, write to the Free Software
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "cifs_unicode.h"
24#include "cifs_uniupr.h"
25#include "cifspdu.h"
Steve French39798772006-05-31 22:40:51 +000026#include "cifsglob.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "cifs_debug.h"
28
29/*
Steve Frenchacbbb762012-01-18 22:32:33 -060030 * cifs_utf16_bytes - how long will a string be after conversion?
31 * @utf16 - pointer to input string
Jeff Layton69f801f2009-04-30 06:46:32 -040032 * @maxbytes - don't go past this many bytes of input string
33 * @codepage - destination codepage
34 *
Steve Frenchacbbb762012-01-18 22:32:33 -060035 * Walk a utf16le string and return the number of bytes that the string will
Jeff Layton69f801f2009-04-30 06:46:32 -040036 * 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 */
39int
Steve Frenchacbbb762012-01-18 22:32:33 -060040cifs_utf16_bytes(const __le16 *from, int maxbytes,
Jeff Layton69f801f2009-04-30 06:46:32 -040041 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];
Jeff Laytonba2dbf32011-01-20 13:36:51 -050047 __u16 ftmp;
Jeff Layton69f801f2009-04-30 06:46:32 -040048
Jeff Laytonba2dbf32011-01-20 13:36:51 -050049 for (i = 0; i < maxwords; i++) {
50 ftmp = get_unaligned_le16(&from[i]);
51 if (ftmp == 0)
52 break;
53
54 charlen = codepage->uni2char(ftmp, tmp, NLS_MAX_CHARSET_SIZE);
Jeff Layton69f801f2009-04-30 06:46:32 -040055 if (charlen > 0)
56 outlen += charlen;
57 else
58 outlen++;
59 }
60
61 return outlen;
62}
63
Steve Frenchb6938552014-09-25 13:20:05 -050064/* Convert character using the SFU - "Services for Unix" remapping range */
65static bool
66convert_sfu_char(const __u16 src_char, char *target)
Jeff Layton7fabf0c2009-04-30 06:46:15 -040067{
Jeff Layton7fabf0c2009-04-30 06:46:15 -040068 /*
69 * BB: Cannot handle remapping UNI_SLASH until all the calls to
70 * build_path_from_dentry are modified, as they use slash as
71 * separator.
72 */
Jeff Laytonba2dbf32011-01-20 13:36:51 -050073 switch (src_char) {
Jeff Layton7fabf0c2009-04-30 06:46:15 -040074 case UNI_COLON:
75 *target = ':';
76 break;
Jeff Layton581ade42011-04-05 15:02:37 -040077 case UNI_ASTERISK:
Jeff Layton7fabf0c2009-04-30 06:46:15 -040078 *target = '*';
79 break;
80 case UNI_QUESTION:
81 *target = '?';
82 break;
83 case UNI_PIPE:
84 *target = '|';
85 break;
86 case UNI_GRTRTHAN:
87 *target = '>';
88 break;
89 case UNI_LESSTHAN:
90 *target = '<';
91 break;
92 default:
Steve Frenchb6938552014-09-25 13:20:05 -050093 return false;
Jeff Layton7fabf0c2009-04-30 06:46:15 -040094 }
Steve Frenchb6938552014-09-25 13:20:05 -050095 return true;
96}
Jeff Layton7fabf0c2009-04-30 06:46:15 -040097
Steve Frenchb6938552014-09-25 13:20:05 -050098/* Convert character using the SFM - "Services for Mac" remapping range */
99static bool
100convert_sfm_char(const __u16 src_char, char *target)
101{
102 switch (src_char) {
103 case SFM_COLON:
104 *target = ':';
105 break;
106 case SFM_ASTERISK:
107 *target = '*';
108 break;
109 case SFM_QUESTION:
110 *target = '?';
111 break;
112 case SFM_PIPE:
113 *target = '|';
114 break;
115 case SFM_GRTRTHAN:
116 *target = '>';
117 break;
118 case SFM_LESSTHAN:
119 *target = '<';
120 break;
121 case SFM_SLASH:
122 *target = '\\';
123 break;
124 default:
125 return false;
126 }
127 return true;
128}
Jeff Layton7fabf0c2009-04-30 06:46:15 -0400129
Steve Frenchb6938552014-09-25 13:20:05 -0500130
131/*
132 * cifs_mapchar - convert a host-endian char to proper char in codepage
133 * @target - where converted character should be copied
134 * @src_char - 2 byte host-endian source character
135 * @cp - codepage to which character should be converted
136 * @map_type - How should the 7 NTFS/SMB reserved characters be mapped to UCS2?
137 *
138 * This function handles the conversion of a single character. It is the
139 * responsibility of the caller to ensure that the target buffer is large
140 * enough to hold the result of the conversion (at least NLS_MAX_CHARSET_SIZE).
141 */
142static int
143cifs_mapchar(char *target, const __u16 src_char, const struct nls_table *cp,
144 int maptype)
145{
146 int len = 1;
147
148 if ((maptype == SFM_MAP_UNI_RSVD) && convert_sfm_char(src_char, target))
149 return len;
150 else if ((maptype == SFU_MAP_UNI_RSVD) &&
151 convert_sfu_char(src_char, target))
152 return len;
153
154 /* if character not one of seven in special remap set */
Jeff Laytonba2dbf32011-01-20 13:36:51 -0500155 len = cp->uni2char(src_char, target, NLS_MAX_CHARSET_SIZE);
Jeff Layton7fabf0c2009-04-30 06:46:15 -0400156 if (len <= 0) {
157 *target = '?';
158 len = 1;
159 }
Steve Frenchb6938552014-09-25 13:20:05 -0500160 return len;
Jeff Layton7fabf0c2009-04-30 06:46:15 -0400161}
162
163/*
Steve Frenchacbbb762012-01-18 22:32:33 -0600164 * cifs_from_utf16 - convert utf16le string to local charset
Jeff Layton7fabf0c2009-04-30 06:46:15 -0400165 * @to - destination buffer
166 * @from - source buffer
167 * @tolen - destination buffer size (in bytes)
168 * @fromlen - source buffer size (in bytes)
169 * @codepage - codepage to which characters should be converted
170 * @mapchar - should characters be remapped according to the mapchars option?
171 *
Steve Frenchacbbb762012-01-18 22:32:33 -0600172 * Convert a little-endian utf16le string (as sent by the server) to a string
Jeff Layton7fabf0c2009-04-30 06:46:15 -0400173 * in the provided codepage. The tolen and fromlen parameters are to ensure
174 * that the code doesn't walk off of the end of the buffer (which is always
175 * a danger if the alignment of the source buffer is off). The destination
176 * string is always properly null terminated and fits in the destination
177 * buffer. Returns the length of the destination string in bytes (including
178 * null terminator).
179 *
180 * Note that some windows versions actually send multiword UTF-16 characters
Steve Frenchacbbb762012-01-18 22:32:33 -0600181 * instead of straight UTF16-2. The linux nls routines however aren't able to
Jeff Layton7fabf0c2009-04-30 06:46:15 -0400182 * deal with those characters properly. In the event that we get some of
183 * those characters, they won't be translated properly.
184 */
185int
Steve Frenchacbbb762012-01-18 22:32:33 -0600186cifs_from_utf16(char *to, const __le16 *from, int tolen, int fromlen,
Steve Frenchb6938552014-09-25 13:20:05 -0500187 const struct nls_table *codepage, int map_type)
Jeff Layton7fabf0c2009-04-30 06:46:15 -0400188{
189 int i, charlen, safelen;
190 int outlen = 0;
191 int nullsize = nls_nullsize(codepage);
192 int fromwords = fromlen / 2;
193 char tmp[NLS_MAX_CHARSET_SIZE];
Jeff Laytonba2dbf32011-01-20 13:36:51 -0500194 __u16 ftmp;
Jeff Layton7fabf0c2009-04-30 06:46:15 -0400195
196 /*
197 * because the chars can be of varying widths, we need to take care
198 * not to overflow the destination buffer when we get close to the
199 * end of it. Until we get to this offset, we don't need to check
200 * for overflow however.
201 */
202 safelen = tolen - (NLS_MAX_CHARSET_SIZE + nullsize);
203
Jeff Laytonba2dbf32011-01-20 13:36:51 -0500204 for (i = 0; i < fromwords; i++) {
205 ftmp = get_unaligned_le16(&from[i]);
206 if (ftmp == 0)
207 break;
208
Jeff Layton7fabf0c2009-04-30 06:46:15 -0400209 /*
210 * check to see if converting this character might make the
211 * conversion bleed into the null terminator
212 */
213 if (outlen >= safelen) {
Steve Frenchb6938552014-09-25 13:20:05 -0500214 charlen = cifs_mapchar(tmp, ftmp, codepage, map_type);
Jeff Layton7fabf0c2009-04-30 06:46:15 -0400215 if ((outlen + charlen) > (tolen - nullsize))
216 break;
217 }
218
219 /* put converted char into 'to' buffer */
Steve Frenchb6938552014-09-25 13:20:05 -0500220 charlen = cifs_mapchar(&to[outlen], ftmp, codepage, map_type);
Jeff Layton7fabf0c2009-04-30 06:46:15 -0400221 outlen += charlen;
222 }
223
224 /* properly null-terminate string */
225 for (i = 0; i < nullsize; i++)
226 to[outlen++] = 0;
227
228 return outlen;
229}
230
231/*
Steve Frenchacbbb762012-01-18 22:32:33 -0600232 * NAME: cifs_strtoUTF16()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 *
234 * FUNCTION: Convert character string to unicode string
235 *
236 */
237int
Steve Frenchacbbb762012-01-18 22:32:33 -0600238cifs_strtoUTF16(__le16 *to, const char *from, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 const struct nls_table *codepage)
240{
241 int charlen;
242 int i;
Jeff Laytonba2dbf32011-01-20 13:36:51 -0500243 wchar_t wchar_to; /* needed to quiet sparse */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Frediano Zigliofd3ba422012-08-07 04:33:03 -0500245 /* special case for utf8 to handle no plane0 chars */
246 if (!strcmp(codepage->charset, "utf8")) {
247 /*
248 * convert utf8 -> utf16, we assume we have enough space
249 * as caller should have assumed conversion does not overflow
250 * in destination len is length in wchar_t units (16bits)
251 */
252 i = utf8s_to_utf16s(from, len, UTF16_LITTLE_ENDIAN,
253 (wchar_t *) to, len);
254
255 /* if success terminate and exit */
256 if (i >= 0)
257 goto success;
258 /*
259 * if fails fall back to UCS encoding as this
260 * function should not return negative values
261 * currently can fail only if source contains
262 * invalid encoded characters
263 */
264 }
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
Jeff Laytonba2dbf32011-01-20 13:36:51 -0500267 charlen = codepage->char2uni(from, len, &wchar_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (charlen < 1) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500269 cifs_dbg(VFS, "strtoUTF16: char2uni of 0x%x returned %d\n",
270 *from, charlen);
Steve French69114082005-11-10 19:28:44 -0800271 /* A question mark */
Jeff Laytonba2dbf32011-01-20 13:36:51 -0500272 wchar_to = 0x003f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 charlen = 1;
Jeff Laytonba2dbf32011-01-20 13:36:51 -0500274 }
275 put_unaligned_le16(wchar_to, &to[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277
Frediano Zigliofd3ba422012-08-07 04:33:03 -0500278success:
Jeff Laytonba2dbf32011-01-20 13:36:51 -0500279 put_unaligned_le16(0, &to[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return i;
281}
282
Jeff Layton066ce682009-04-30 07:16:14 -0400283/*
Steve Frenchacbbb762012-01-18 22:32:33 -0600284 * cifs_strndup_from_utf16 - copy a string from wire format to the local
285 * codepage
Jeff Layton066ce682009-04-30 07:16:14 -0400286 * @src - source string
287 * @maxlen - don't walk past this many bytes in the source string
288 * @is_unicode - is this a unicode string?
289 * @codepage - destination codepage
290 *
291 * Take a string given by the server, convert it to the local codepage and
292 * put it in a new buffer. Returns a pointer to the new string or NULL on
293 * error.
294 */
295char *
Steve Frenchacbbb762012-01-18 22:32:33 -0600296cifs_strndup_from_utf16(const char *src, const int maxlen,
297 const bool is_unicode, const struct nls_table *codepage)
Jeff Layton066ce682009-04-30 07:16:14 -0400298{
299 int len;
300 char *dst;
301
302 if (is_unicode) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600303 len = cifs_utf16_bytes((__le16 *) src, maxlen, codepage);
Jeff Layton066ce682009-04-30 07:16:14 -0400304 len += nls_nullsize(codepage);
305 dst = kmalloc(len, GFP_KERNEL);
306 if (!dst)
307 return NULL;
Steve Frenchacbbb762012-01-18 22:32:33 -0600308 cifs_from_utf16(dst, (__le16 *) src, len, maxlen, codepage,
Steve Frenchb6938552014-09-25 13:20:05 -0500309 NO_MAP_UNI_RSVD);
Jeff Layton066ce682009-04-30 07:16:14 -0400310 } else {
311 len = strnlen(src, maxlen);
312 len++;
313 dst = kmalloc(len, GFP_KERNEL);
314 if (!dst)
315 return NULL;
316 strlcpy(dst, src, len);
317 }
318
319 return dst;
320}
321
Steve Frencha4153cb2014-09-25 14:01:34 -0500322static __le16 convert_to_sfu_char(char src_char)
323{
324 __le16 dest_char;
325
326 switch (src_char) {
327 case ':':
328 dest_char = cpu_to_le16(UNI_COLON);
329 break;
330 case '*':
331 dest_char = cpu_to_le16(UNI_ASTERISK);
332 break;
333 case '?':
334 dest_char = cpu_to_le16(UNI_QUESTION);
335 break;
336 case '<':
337 dest_char = cpu_to_le16(UNI_LESSTHAN);
338 break;
339 case '>':
340 dest_char = cpu_to_le16(UNI_GRTRTHAN);
341 break;
342 case '|':
343 dest_char = cpu_to_le16(UNI_PIPE);
344 break;
345 default:
346 dest_char = 0;
347 }
348
349 return dest_char;
350}
351
352static __le16 convert_to_sfm_char(char src_char)
353{
354 __le16 dest_char;
355
356 switch (src_char) {
357 case ':':
358 dest_char = cpu_to_le16(SFM_COLON);
359 break;
360 case '*':
361 dest_char = cpu_to_le16(SFM_ASTERISK);
362 break;
363 case '?':
364 dest_char = cpu_to_le16(SFM_QUESTION);
365 break;
366 case '<':
367 dest_char = cpu_to_le16(SFM_LESSTHAN);
368 break;
369 case '>':
370 dest_char = cpu_to_le16(SFM_GRTRTHAN);
371 break;
372 case '|':
373 dest_char = cpu_to_le16(SFM_PIPE);
374 break;
375 default:
376 dest_char = 0;
377 }
378
379 return dest_char;
380}
381
Jeff Layton84cdf742011-01-20 13:36:51 -0500382/*
383 * Convert 16 bit Unicode pathname to wire format from string in current code
384 * page. Conversion may involve remapping up the six characters that are
385 * only legal in POSIX-like OS (if they are present in the string). Path
386 * names are little endian 16 bit Unicode on the wire
387 */
388int
Steve Frenchacbbb762012-01-18 22:32:33 -0600389cifsConvertToUTF16(__le16 *target, const char *source, int srclen,
Steve Frencha4153cb2014-09-25 14:01:34 -0500390 const struct nls_table *cp, int map_chars)
Jeff Layton84cdf742011-01-20 13:36:51 -0500391{
Steve Frenchce36d9a2014-06-22 20:38:49 -0500392 int i, charlen;
393 int j = 0;
Jeff Layton84cdf742011-01-20 13:36:51 -0500394 char src_char;
Jeff Layton581ade42011-04-05 15:02:37 -0400395 __le16 dst_char;
396 wchar_t tmp;
Jeff Layton84cdf742011-01-20 13:36:51 -0500397
Steve Frencha4153cb2014-09-25 14:01:34 -0500398 if (map_chars == NO_MAP_UNI_RSVD)
Steve Frenchacbbb762012-01-18 22:32:33 -0600399 return cifs_strtoUTF16(target, source, PATH_MAX, cp);
Jeff Layton84cdf742011-01-20 13:36:51 -0500400
Steve Frenchce36d9a2014-06-22 20:38:49 -0500401 for (i = 0; i < srclen; j++) {
Jeff Layton84cdf742011-01-20 13:36:51 -0500402 src_char = source[i];
Jeff Layton11379b52011-05-17 15:28:21 -0400403 charlen = 1;
Steve Frencha4153cb2014-09-25 14:01:34 -0500404
405 /* check if end of string */
406 if (src_char == 0)
Steve Frenchacbbb762012-01-18 22:32:33 -0600407 goto ctoUTF16_out;
Steve Frencha4153cb2014-09-25 14:01:34 -0500408
409 /* see if we must remap this char */
410 if (map_chars == SFU_MAP_UNI_RSVD)
411 dst_char = convert_to_sfu_char(src_char);
412 else if (map_chars == SFM_MAP_UNI_RSVD)
413 dst_char = convert_to_sfm_char(src_char);
414 else
415 dst_char = 0;
Jeff Layton84cdf742011-01-20 13:36:51 -0500416 /*
417 * FIXME: We can not handle remapping backslash (UNI_SLASH)
418 * until all the calls to build_path_from_dentry are modified,
419 * as they use backslash as separator.
420 */
Steve Frencha4153cb2014-09-25 14:01:34 -0500421 if (dst_char == 0) {
Jeff Layton581ade42011-04-05 15:02:37 -0400422 charlen = cp->char2uni(source + i, srclen - i, &tmp);
423 dst_char = cpu_to_le16(tmp);
424
Jeff Layton84cdf742011-01-20 13:36:51 -0500425 /*
426 * if no match, use question mark, which at least in
427 * some cases serves as wild card
428 */
429 if (charlen < 1) {
Jeff Layton581ade42011-04-05 15:02:37 -0400430 dst_char = cpu_to_le16(0x003f);
Jeff Layton84cdf742011-01-20 13:36:51 -0500431 charlen = 1;
432 }
Jeff Layton84cdf742011-01-20 13:36:51 -0500433 }
Jeff Layton11379b52011-05-17 15:28:21 -0400434 /*
435 * character may take more than one byte in the source string,
436 * but will take exactly two bytes in the target string
437 */
438 i += charlen;
Jeff Layton581ade42011-04-05 15:02:37 -0400439 put_unaligned(dst_char, &target[j]);
Jeff Layton84cdf742011-01-20 13:36:51 -0500440 }
441
Steve Frenchacbbb762012-01-18 22:32:33 -0600442ctoUTF16_out:
Steve Frenchce36d9a2014-06-22 20:38:49 -0500443 put_unaligned(0, &target[j]); /* Null terminate target unicode string */
Jeff Laytonc73f6932012-09-18 14:21:01 -0400444 return j;
Jeff Layton84cdf742011-01-20 13:36:51 -0500445}
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400446
447#ifdef CONFIG_CIFS_SMB2
448/*
449 * cifs_local_to_utf16_bytes - how long will a string be after conversion?
450 * @from - pointer to input string
451 * @maxbytes - don't go past this many bytes of input string
452 * @codepage - source codepage
453 *
454 * Walk a string and return the number of bytes that the string will
455 * be after being converted to the given charset, not including any null
456 * termination required. Don't walk past maxbytes in the source buffer.
457 */
458
459static int
460cifs_local_to_utf16_bytes(const char *from, int len,
461 const struct nls_table *codepage)
462{
463 int charlen;
464 int i;
465 wchar_t wchar_to;
466
467 for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
468 charlen = codepage->char2uni(from, len, &wchar_to);
469 /* Failed conversion defaults to a question mark */
470 if (charlen < 1)
471 charlen = 1;
472 }
473 return 2 * i; /* UTF16 characters are two bytes */
474}
475
476/*
477 * cifs_strndup_to_utf16 - copy a string to wire format from the local codepage
478 * @src - source string
479 * @maxlen - don't walk past this many bytes in the source string
480 * @utf16_len - the length of the allocated string in bytes (including null)
481 * @cp - source codepage
482 * @remap - map special chars
483 *
484 * Take a string convert it from the local codepage to UTF16 and
485 * put it in a new buffer. Returns a pointer to the new string or NULL on
486 * error.
487 */
488__le16 *
489cifs_strndup_to_utf16(const char *src, const int maxlen, int *utf16_len,
490 const struct nls_table *cp, int remap)
491{
492 int len;
493 __le16 *dst;
494
495 len = cifs_local_to_utf16_bytes(src, maxlen, cp);
496 len += 2; /* NULL */
497 dst = kmalloc(len, GFP_KERNEL);
498 if (!dst) {
499 *utf16_len = 0;
500 return NULL;
501 }
502 cifsConvertToUTF16(dst, src, strlen(src), cp, remap);
503 *utf16_len = len;
504 return dst;
505}
506#endif /* CONFIG_CIFS_SMB2 */