cifs_unicode.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * fs/cifs/cifs_unicode.c
  3. *
  4. * Copyright (c) International Business Machines Corp., 2000,2009
  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
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  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
  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
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/slab.h>
  23. #include "cifs_unicode.h"
  24. #include "cifs_uniupr.h"
  25. #include "cifspdu.h"
  26. #include "cifsglob.h"
  27. #include "cifs_debug.h"
  28. /*
  29. * cifs_ucs2_bytes - how long will a string be after conversion?
  30. * @ucs - pointer to input string
  31. * @maxbytes - don't go past this many bytes of input string
  32. * @codepage - destination codepage
  33. *
  34. * Walk a ucs2le string and return the number of bytes that the string will
  35. * be after being converted to the given charset, not including any null
  36. * termination required. Don't walk past maxbytes in the source buffer.
  37. */
  38. int
  39. cifs_ucs2_bytes(const __le16 *from, int maxbytes,
  40. const struct nls_table *codepage)
  41. {
  42. int i;
  43. int charlen, outlen = 0;
  44. int maxwords = maxbytes / 2;
  45. char tmp[NLS_MAX_CHARSET_SIZE];
  46. __u16 ftmp;
  47. for (i = 0; i < maxwords; i++) {
  48. ftmp = get_unaligned_le16(&from[i]);
  49. if (ftmp == 0)
  50. break;
  51. charlen = codepage->uni2char(ftmp, tmp, NLS_MAX_CHARSET_SIZE);
  52. if (charlen > 0)
  53. outlen += charlen;
  54. else
  55. outlen++;
  56. }
  57. return outlen;
  58. }
  59. /*
  60. * cifs_mapchar - convert a host-endian char to proper char in codepage
  61. * @target - where converted character should be copied
  62. * @src_char - 2 byte host-endian source character
  63. * @cp - codepage to which character should be converted
  64. * @mapchar - should character be mapped according to mapchars mount option?
  65. *
  66. * This function handles the conversion of a single character. It is the
  67. * responsibility of the caller to ensure that the target buffer is large
  68. * enough to hold the result of the conversion (at least NLS_MAX_CHARSET_SIZE).
  69. */
  70. static int
  71. cifs_mapchar(char *target, const __u16 src_char, const struct nls_table *cp,
  72. bool mapchar)
  73. {
  74. int len = 1;
  75. if (!mapchar)
  76. goto cp_convert;
  77. /*
  78. * BB: Cannot handle remapping UNI_SLASH until all the calls to
  79. * build_path_from_dentry are modified, as they use slash as
  80. * separator.
  81. */
  82. switch (src_char) {
  83. case UNI_COLON:
  84. *target = ':';
  85. break;
  86. case UNI_ASTERISK:
  87. *target = '*';
  88. break;
  89. case UNI_QUESTION:
  90. *target = '?';
  91. break;
  92. case UNI_PIPE:
  93. *target = '|';
  94. break;
  95. case UNI_GRTRTHAN:
  96. *target = '>';
  97. break;
  98. case UNI_LESSTHAN:
  99. *target = '<';
  100. break;
  101. default:
  102. goto cp_convert;
  103. }
  104. out:
  105. return len;
  106. cp_convert:
  107. len = cp->uni2char(src_char, target, NLS_MAX_CHARSET_SIZE);
  108. if (len <= 0) {
  109. *target = '?';
  110. len = 1;
  111. }
  112. goto out;
  113. }
  114. /*
  115. * cifs_from_ucs2 - convert utf16le string to local charset
  116. * @to - destination buffer
  117. * @from - source buffer
  118. * @tolen - destination buffer size (in bytes)
  119. * @fromlen - source buffer size (in bytes)
  120. * @codepage - codepage to which characters should be converted
  121. * @mapchar - should characters be remapped according to the mapchars option?
  122. *
  123. * Convert a little-endian ucs2le string (as sent by the server) to a string
  124. * in the provided codepage. The tolen and fromlen parameters are to ensure
  125. * that the code doesn't walk off of the end of the buffer (which is always
  126. * a danger if the alignment of the source buffer is off). The destination
  127. * string is always properly null terminated and fits in the destination
  128. * buffer. Returns the length of the destination string in bytes (including
  129. * null terminator).
  130. *
  131. * Note that some windows versions actually send multiword UTF-16 characters
  132. * instead of straight UCS-2. The linux nls routines however aren't able to
  133. * deal with those characters properly. In the event that we get some of
  134. * those characters, they won't be translated properly.
  135. */
  136. int
  137. cifs_from_ucs2(char *to, const __le16 *from, int tolen, int fromlen,
  138. const struct nls_table *codepage, bool mapchar)
  139. {
  140. int i, charlen, safelen;
  141. int outlen = 0;
  142. int nullsize = nls_nullsize(codepage);
  143. int fromwords = fromlen / 2;
  144. char tmp[NLS_MAX_CHARSET_SIZE];
  145. __u16 ftmp;
  146. /*
  147. * because the chars can be of varying widths, we need to take care
  148. * not to overflow the destination buffer when we get close to the
  149. * end of it. Until we get to this offset, we don't need to check
  150. * for overflow however.
  151. */
  152. safelen = tolen - (NLS_MAX_CHARSET_SIZE + nullsize);
  153. for (i = 0; i < fromwords; i++) {
  154. ftmp = get_unaligned_le16(&from[i]);
  155. if (ftmp == 0)
  156. break;
  157. /*
  158. * check to see if converting this character might make the
  159. * conversion bleed into the null terminator
  160. */
  161. if (outlen >= safelen) {
  162. charlen = cifs_mapchar(tmp, ftmp, codepage, mapchar);
  163. if ((outlen + charlen) > (tolen - nullsize))
  164. break;
  165. }
  166. /* put converted char into 'to' buffer */
  167. charlen = cifs_mapchar(&to[outlen], ftmp, codepage, mapchar);
  168. outlen += charlen;
  169. }
  170. /* properly null-terminate string */
  171. for (i = 0; i < nullsize; i++)
  172. to[outlen++] = 0;
  173. return outlen;
  174. }
  175. /*
  176. * NAME: cifs_strtoUCS()
  177. *
  178. * FUNCTION: Convert character string to unicode string
  179. *
  180. */
  181. int
  182. cifs_strtoUCS(__le16 *to, const char *from, int len,
  183. const struct nls_table *codepage)
  184. {
  185. int charlen;
  186. int i;
  187. wchar_t wchar_to; /* needed to quiet sparse */
  188. for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
  189. charlen = codepage->char2uni(from, len, &wchar_to);
  190. if (charlen < 1) {
  191. cERROR(1, "strtoUCS: char2uni of 0x%x returned %d",
  192. *from, charlen);
  193. /* A question mark */
  194. wchar_to = 0x003f;
  195. charlen = 1;
  196. }
  197. put_unaligned_le16(wchar_to, &to[i]);
  198. }
  199. put_unaligned_le16(0, &to[i]);
  200. return i;
  201. }
  202. /*
  203. * cifs_strndup_from_ucs - copy a string from wire format to the local codepage
  204. * @src - source string
  205. * @maxlen - don't walk past this many bytes in the source string
  206. * @is_unicode - is this a unicode string?
  207. * @codepage - destination codepage
  208. *
  209. * Take a string given by the server, convert it to the local codepage and
  210. * put it in a new buffer. Returns a pointer to the new string or NULL on
  211. * error.
  212. */
  213. char *
  214. cifs_strndup_from_ucs(const char *src, const int maxlen, const bool is_unicode,
  215. const struct nls_table *codepage)
  216. {
  217. int len;
  218. char *dst;
  219. if (is_unicode) {
  220. len = cifs_ucs2_bytes((__le16 *) src, maxlen, codepage);
  221. len += nls_nullsize(codepage);
  222. dst = kmalloc(len, GFP_KERNEL);
  223. if (!dst)
  224. return NULL;
  225. cifs_from_ucs2(dst, (__le16 *) src, len, maxlen, codepage,
  226. false);
  227. } else {
  228. len = strnlen(src, maxlen);
  229. len++;
  230. dst = kmalloc(len, GFP_KERNEL);
  231. if (!dst)
  232. return NULL;
  233. strlcpy(dst, src, len);
  234. }
  235. return dst;
  236. }
  237. /*
  238. * Convert 16 bit Unicode pathname to wire format from string in current code
  239. * page. Conversion may involve remapping up the six characters that are
  240. * only legal in POSIX-like OS (if they are present in the string). Path
  241. * names are little endian 16 bit Unicode on the wire
  242. */
  243. int
  244. cifsConvertToUCS(__le16 *target, const char *source, int srclen,
  245. const struct nls_table *cp, int mapChars)
  246. {
  247. int i, j, charlen;
  248. char src_char;
  249. __le16 dst_char;
  250. wchar_t tmp;
  251. if (!mapChars)
  252. return cifs_strtoUCS(target, source, PATH_MAX, cp);
  253. for (i = 0, j = 0; i < srclen; j++) {
  254. src_char = source[i];
  255. charlen = 1;
  256. switch (src_char) {
  257. case 0:
  258. put_unaligned(0, &target[j]);
  259. goto ctoUCS_out;
  260. case ':':
  261. dst_char = cpu_to_le16(UNI_COLON);
  262. break;
  263. case '*':
  264. dst_char = cpu_to_le16(UNI_ASTERISK);
  265. break;
  266. case '?':
  267. dst_char = cpu_to_le16(UNI_QUESTION);
  268. break;
  269. case '<':
  270. dst_char = cpu_to_le16(UNI_LESSTHAN);
  271. break;
  272. case '>':
  273. dst_char = cpu_to_le16(UNI_GRTRTHAN);
  274. break;
  275. case '|':
  276. dst_char = cpu_to_le16(UNI_PIPE);
  277. break;
  278. /*
  279. * FIXME: We can not handle remapping backslash (UNI_SLASH)
  280. * until all the calls to build_path_from_dentry are modified,
  281. * as they use backslash as separator.
  282. */
  283. default:
  284. charlen = cp->char2uni(source + i, srclen - i, &tmp);
  285. dst_char = cpu_to_le16(tmp);
  286. /*
  287. * if no match, use question mark, which at least in
  288. * some cases serves as wild card
  289. */
  290. if (charlen < 1) {
  291. dst_char = cpu_to_le16(0x003f);
  292. charlen = 1;
  293. }
  294. }
  295. /*
  296. * character may take more than one byte in the source string,
  297. * but will take exactly two bytes in the target string
  298. */
  299. i += charlen;
  300. put_unaligned(dst_char, &target[j]);
  301. }
  302. ctoUCS_out:
  303. return i;
  304. }