1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Some of the source code in this file came from fs/cifs/cifs_unicode.c
5 * Copyright (c) International Business Machines Corp., 2000,2009
6 * Modified by Steve French (sfrench@us.ibm.com)
7 * Modified by Namjae Jeon (linkinjeon@kernel.org)
10 #include <linux/slab.h>
11 #include <asm/unaligned.h>
14 #include "smb_common.h"
17 * smb_utf16_bytes() - how long will a string be after conversion?
18 * @from: pointer to input string
19 * @maxbytes: don't go past this many bytes of input string
20 * @codepage: destination codepage
22 * Walk a utf16le string and return the number of bytes that the string will
23 * be after being converted to the given charset, not including any null
24 * termination required. Don't walk past maxbytes in the source buffer.
26 * Return: string length after conversion
28 static int smb_utf16_bytes(const __le16 *from, int maxbytes,
29 const struct nls_table *codepage)
32 int charlen, outlen = 0;
33 int maxwords = maxbytes / 2;
34 char tmp[NLS_MAX_CHARSET_SIZE];
37 for (i = 0; i < maxwords; i++) {
38 ftmp = get_unaligned_le16(&from[i]);
42 charlen = codepage->uni2char(ftmp, tmp, NLS_MAX_CHARSET_SIZE);
53 * cifs_mapchar() - convert a host-endian char to proper char in codepage
54 * @target: where converted character should be copied
55 * @src_char: 2 byte host-endian source character
56 * @cp: codepage to which character should be converted
57 * @mapchar: should character be mapped according to mapchars mount option?
59 * This function handles the conversion of a single character. It is the
60 * responsibility of the caller to ensure that the target buffer is large
61 * enough to hold the result of the conversion (at least NLS_MAX_CHARSET_SIZE).
63 * Return: string length after conversion
66 cifs_mapchar(char *target, const __u16 src_char, const struct nls_table *cp,
75 * BB: Cannot handle remapping UNI_SLASH until all the calls to
76 * build_path_from_dentry are modified, as they use slash as
106 len = cp->uni2char(src_char, target, NLS_MAX_CHARSET_SIZE);
116 * smb_from_utf16() - convert utf16le string to local charset
117 * @to: destination buffer
118 * @from: source buffer
119 * @tolen: destination buffer size (in bytes)
120 * @fromlen: source buffer size (in bytes)
121 * @codepage: codepage to which characters should be converted
122 * @mapchar: should characters be remapped according to the mapchars option?
124 * Convert a little-endian utf16le string (as sent by the server) to a string
125 * in the provided codepage. The tolen and fromlen parameters are to ensure
126 * that the code doesn't walk off of the end of the buffer (which is always
127 * a danger if the alignment of the source buffer is off). The destination
128 * string is always properly null terminated and fits in the destination
129 * buffer. Returns the length of the destination string in bytes (including
132 * Note that some windows versions actually send multiword UTF-16 characters
133 * instead of straight UTF16-2. The linux nls routines however aren't able to
134 * deal with those characters properly. In the event that we get some of
135 * those characters, they won't be translated properly.
137 * Return: string length after conversion
139 static int smb_from_utf16(char *to, const __le16 *from, int tolen, int fromlen,
140 const struct nls_table *codepage, bool mapchar)
142 int i, charlen, safelen;
144 int nullsize = nls_nullsize(codepage);
145 int fromwords = fromlen / 2;
146 char tmp[NLS_MAX_CHARSET_SIZE];
150 * because the chars can be of varying widths, we need to take care
151 * not to overflow the destination buffer when we get close to the
152 * end of it. Until we get to this offset, we don't need to check
153 * for overflow however.
155 safelen = tolen - (NLS_MAX_CHARSET_SIZE + nullsize);
157 for (i = 0; i < fromwords; i++) {
158 ftmp = get_unaligned_le16(&from[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, ftmp, codepage, mapchar);
168 if ((outlen + charlen) > (tolen - nullsize))
172 /* put converted char into 'to' buffer */
173 charlen = cifs_mapchar(&to[outlen], ftmp, codepage, mapchar);
177 /* properly null-terminate string */
178 for (i = 0; i < nullsize; i++)
185 * smb_strtoUTF16() - Convert character string to unicode string
186 * @to: destination buffer
187 * @from: source buffer
188 * @len: destination buffer size (in bytes)
189 * @codepage: codepage to which characters should be converted
191 * Return: string length after conversion
193 int smb_strtoUTF16(__le16 *to, const char *from, int len,
194 const struct nls_table *codepage)
198 wchar_t wchar_to; /* needed to quiet sparse */
200 /* special case for utf8 to handle no plane0 chars */
201 if (!strcmp(codepage->charset, "utf8")) {
203 * convert utf8 -> utf16, we assume we have enough space
204 * as caller should have assumed conversion does not overflow
205 * in destination len is length in wchar_t units (16bits)
207 i = utf8s_to_utf16s(from, len, UTF16_LITTLE_ENDIAN,
210 /* if success terminate and exit */
214 * if fails fall back to UCS encoding as this
215 * function should not return negative values
216 * currently can fail only if source contains
217 * invalid encoded characters
221 for (i = 0; len > 0 && *from; i++, from += charlen, len -= charlen) {
222 charlen = codepage->char2uni(from, len, &wchar_to);
224 /* A question mark */
228 put_unaligned_le16(wchar_to, &to[i]);
232 put_unaligned_le16(0, &to[i]);
237 * smb_strndup_from_utf16() - copy a string from wire format to the local
239 * @src: source string
240 * @maxlen: don't walk past this many bytes in the source string
241 * @is_unicode: is this a unicode string?
242 * @codepage: destination codepage
244 * Take a string given by the server, convert it to the local codepage and
245 * put it in a new buffer. Returns a pointer to the new string or NULL on
248 * Return: destination string buffer or error ptr
250 char *smb_strndup_from_utf16(const char *src, const int maxlen,
251 const bool is_unicode,
252 const struct nls_table *codepage)
258 len = smb_utf16_bytes((__le16 *)src, maxlen, codepage);
259 len += nls_nullsize(codepage);
260 dst = kmalloc(len, GFP_KERNEL);
262 return ERR_PTR(-ENOMEM);
263 ret = smb_from_utf16(dst, (__le16 *)src, len, maxlen, codepage,
267 return ERR_PTR(-EINVAL);
270 len = strnlen(src, maxlen);
272 dst = kmalloc(len, GFP_KERNEL);
274 return ERR_PTR(-ENOMEM);
275 strscpy(dst, src, len);
282 * Convert 16 bit Unicode pathname to wire format from string in current code
283 * page. Conversion may involve remapping up the six characters that are
284 * only legal in POSIX-like OS (if they are present in the string). Path
285 * names are little endian 16 bit Unicode on the wire
288 * smbConvertToUTF16() - convert string from local charset to utf16
289 * @target: destination buffer
290 * @source: source buffer
291 * @srclen: source buffer size (in bytes)
292 * @cp: codepage to which characters should be converted
293 * @mapchar: should characters be remapped according to the mapchars option?
295 * Convert 16 bit Unicode pathname to wire format from string in current code
296 * page. Conversion may involve remapping up the six characters that are
297 * only legal in POSIX-like OS (if they are present in the string). Path
298 * names are little endian 16 bit Unicode on the wire
300 * Return: char length after conversion
302 int smbConvertToUTF16(__le16 *target, const char *source, int srclen,
303 const struct nls_table *cp, int mapchars)
311 return smb_strtoUTF16(target, source, srclen, cp);
313 for (i = 0, j = 0; i < srclen; j++) {
314 src_char = source[i];
318 put_unaligned(0, &target[j]);
321 dst_char = cpu_to_le16(UNI_COLON);
324 dst_char = cpu_to_le16(UNI_ASTERISK);
327 dst_char = cpu_to_le16(UNI_QUESTION);
330 dst_char = cpu_to_le16(UNI_LESSTHAN);
333 dst_char = cpu_to_le16(UNI_GRTRTHAN);
336 dst_char = cpu_to_le16(UNI_PIPE);
339 * FIXME: We can not handle remapping backslash (UNI_SLASH)
340 * until all the calls to build_path_from_dentry are modified,
341 * as they use backslash as separator.
344 charlen = cp->char2uni(source + i, srclen - i, &tmp);
345 dst_char = cpu_to_le16(tmp);
348 * if no match, use question mark, which at least in
349 * some cases serves as wild card
352 dst_char = cpu_to_le16(0x003f);
357 * character may take more than one byte in the source string,
358 * but will take exactly two bytes in the target string
361 put_unaligned(dst_char, &target[j]);