2 * fs/cifs/cifs_unicode.c
4 * Copyright (c) International Business Machines Corp., 2000,2005
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 "cifs_unicode.h"
23 #include "cifs_uniupr.h"
26 #include "cifs_debug.h"
29 * cifs_mapchar - convert a little-endian char to proper char in codepage
30 * @target - where converted character should be copied
31 * @src_char - 2 byte little-endian source character
32 * @cp - codepage to which character should be converted
33 * @mapchar - should character be mapped according to mapchars mount option?
35 * This function handles the conversion of a single character. It is the
36 * responsibility of the caller to ensure that the target buffer is large
37 * enough to hold the result of the conversion (at least NLS_MAX_CHARSET_SIZE).
40 cifs_mapchar(char *target, const __le16 src_char, const struct nls_table *cp,
49 * BB: Cannot handle remapping UNI_SLASH until all the calls to
50 * build_path_from_dentry are modified, as they use slash as
53 switch (le16_to_cpu(src_char)) {
80 len = cp->uni2char(le16_to_cpu(src_char), target,
81 NLS_MAX_CHARSET_SIZE);
90 * cifs_from_ucs2 - convert utf16le string to local charset
91 * @to - destination buffer
92 * @from - source buffer
93 * @tolen - destination buffer size (in bytes)
94 * @fromlen - source buffer size (in bytes)
95 * @codepage - codepage to which characters should be converted
96 * @mapchar - should characters be remapped according to the mapchars option?
98 * Convert a little-endian ucs2le string (as sent by the server) to a string
99 * in the provided codepage. The tolen and fromlen parameters are to ensure
100 * that the code doesn't walk off of the end of the buffer (which is always
101 * a danger if the alignment of the source buffer is off). The destination
102 * string is always properly null terminated and fits in the destination
103 * buffer. Returns the length of the destination string in bytes (including
106 * Note that some windows versions actually send multiword UTF-16 characters
107 * instead of straight UCS-2. The linux nls routines however aren't able to
108 * deal with those characters properly. In the event that we get some of
109 * those characters, they won't be translated properly.
112 cifs_from_ucs2(char *to, const __le16 *from, int tolen, int fromlen,
113 const struct nls_table *codepage, bool mapchar)
115 int i, charlen, safelen;
117 int nullsize = nls_nullsize(codepage);
118 int fromwords = fromlen / 2;
119 char tmp[NLS_MAX_CHARSET_SIZE];
122 * because the chars can be of varying widths, we need to take care
123 * not to overflow the destination buffer when we get close to the
124 * end of it. Until we get to this offset, we don't need to check
125 * for overflow however.
127 safelen = tolen - (NLS_MAX_CHARSET_SIZE + nullsize);
129 for (i = 0; i < fromwords && from[i]; i++) {
131 * check to see if converting this character might make the
132 * conversion bleed into the null terminator
134 if (outlen >= safelen) {
135 charlen = cifs_mapchar(tmp, from[i], codepage, mapchar);
136 if ((outlen + charlen) > (tolen - nullsize))
140 /* put converted char into 'to' buffer */
141 charlen = cifs_mapchar(&to[outlen], from[i], codepage, mapchar);
145 /* properly null-terminate string */
146 for (i = 0; i < nullsize; i++)
153 * NAME: cifs_strfromUCS()
155 * FUNCTION: Convert little-endian unicode string to character string
159 cifs_strfromUCS_le(char *to, const __le16 *from,
160 int len, const struct nls_table *codepage)
165 for (i = 0; (i < len) && from[i]; i++) {
167 /* 2.4.0 kernel or greater */
169 codepage->uni2char(le16_to_cpu(from[i]), &to[outlen],
170 NLS_MAX_CHARSET_SIZE);
182 * NAME: cifs_strtoUCS()
184 * FUNCTION: Convert character string to unicode string
188 cifs_strtoUCS(__le16 *to, const char *from, int len,
189 const struct nls_table *codepage)
193 wchar_t *wchar_to = (wchar_t *)to; /* needed to quiet sparse */
195 for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
197 /* works for 2.4.0 kernel or later */
198 charlen = codepage->char2uni(from, len, &wchar_to[i]);
201 ("strtoUCS: char2uni of %d returned %d",
202 (int)*from, charlen));
203 /* A question mark */
204 to[i] = cpu_to_le16(0x003f);
207 to[i] = cpu_to_le16(wchar_to[i]);