cifs_unicode.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. for (i = 0; i < maxwords && from[i]; i++) {
  47. charlen = codepage->uni2char(le16_to_cpu(from[i]), tmp,
  48. NLS_MAX_CHARSET_SIZE);
  49. if (charlen > 0)
  50. outlen += charlen;
  51. else
  52. outlen++;
  53. }
  54. return outlen;
  55. }
  56. /*
  57. * cifs_mapchar - convert a little-endian char to proper char in codepage
  58. * @target - where converted character should be copied
  59. * @src_char - 2 byte little-endian source character
  60. * @cp - codepage to which character should be converted
  61. * @mapchar - should character be mapped according to mapchars mount option?
  62. *
  63. * This function handles the conversion of a single character. It is the
  64. * responsibility of the caller to ensure that the target buffer is large
  65. * enough to hold the result of the conversion (at least NLS_MAX_CHARSET_SIZE).
  66. */
  67. static int
  68. cifs_mapchar(char *target, const __le16 src_char, const struct nls_table *cp,
  69. bool mapchar)
  70. {
  71. int len = 1;
  72. if (!mapchar)
  73. goto cp_convert;
  74. /*
  75. * BB: Cannot handle remapping UNI_SLASH until all the calls to
  76. * build_path_from_dentry are modified, as they use slash as
  77. * separator.
  78. */
  79. switch (le16_to_cpu(src_char)) {
  80. case UNI_COLON:
  81. *target = ':';
  82. break;
  83. case UNI_ASTERIK:
  84. *target = '*';
  85. break;
  86. case UNI_QUESTION:
  87. *target = '?';
  88. break;
  89. case UNI_PIPE:
  90. *target = '|';
  91. break;
  92. case UNI_GRTRTHAN:
  93. *target = '>';
  94. break;
  95. case UNI_LESSTHAN:
  96. *target = '<';
  97. break;
  98. default:
  99. goto cp_convert;
  100. }
  101. out:
  102. return len;
  103. cp_convert:
  104. len = cp->uni2char(le16_to_cpu(src_char), target,
  105. NLS_MAX_CHARSET_SIZE);
  106. if (len <= 0) {
  107. *target = '?';
  108. len = 1;
  109. }
  110. goto out;
  111. }
  112. /*
  113. * cifs_from_ucs2 - convert utf16le string to local charset
  114. * @to - destination buffer
  115. * @from - source buffer
  116. * @tolen - destination buffer size (in bytes)
  117. * @fromlen - source buffer size (in bytes)
  118. * @codepage - codepage to which characters should be converted
  119. * @mapchar - should characters be remapped according to the mapchars option?
  120. *
  121. * Convert a little-endian ucs2le string (as sent by the server) to a string
  122. * in the provided codepage. The tolen and fromlen parameters are to ensure
  123. * that the code doesn't walk off of the end of the buffer (which is always
  124. * a danger if the alignment of the source buffer is off). The destination
  125. * string is always properly null terminated and fits in the destination
  126. * buffer. Returns the length of the destination string in bytes (including
  127. * null terminator).
  128. *
  129. * Note that some windows versions actually send multiword UTF-16 characters
  130. * instead of straight UCS-2. The linux nls routines however aren't able to
  131. * deal with those characters properly. In the event that we get some of
  132. * those characters, they won't be translated properly.
  133. */
  134. int
  135. cifs_from_ucs2(char *to, const __le16 *from, int tolen, int fromlen,
  136. const struct nls_table *codepage, bool mapchar)
  137. {
  138. int i, charlen, safelen;
  139. int outlen = 0;
  140. int nullsize = nls_nullsize(codepage);
  141. int fromwords = fromlen / 2;
  142. char tmp[NLS_MAX_CHARSET_SIZE];
  143. /*
  144. * because the chars can be of varying widths, we need to take care
  145. * not to overflow the destination buffer when we get close to the
  146. * end of it. Until we get to this offset, we don't need to check
  147. * for overflow however.
  148. */
  149. safelen = tolen - (NLS_MAX_CHARSET_SIZE + nullsize);
  150. for (i = 0; i < fromwords && from[i]; i++) {
  151. /*
  152. * check to see if converting this character might make the
  153. * conversion bleed into the null terminator
  154. */
  155. if (outlen >= safelen) {
  156. charlen = cifs_mapchar(tmp, from[i], codepage, mapchar);
  157. if ((outlen + charlen) > (tolen - nullsize))
  158. break;
  159. }
  160. /* put converted char into 'to' buffer */
  161. charlen = cifs_mapchar(&to[outlen], from[i], codepage, mapchar);
  162. outlen += charlen;
  163. }
  164. /* properly null-terminate string */
  165. for (i = 0; i < nullsize; i++)
  166. to[outlen++] = 0;
  167. return outlen;
  168. }
  169. /*
  170. * NAME: cifs_strtoUCS()
  171. *
  172. * FUNCTION: Convert character string to unicode string
  173. *
  174. */
  175. int
  176. cifs_strtoUCS(__le16 *to, const char *from, int len,
  177. const struct nls_table *codepage)
  178. {
  179. int charlen;
  180. int i;
  181. wchar_t *wchar_to = (wchar_t *)to; /* needed to quiet sparse */
  182. for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
  183. /* works for 2.4.0 kernel or later */
  184. charlen = codepage->char2uni(from, len, &wchar_to[i]);
  185. if (charlen < 1) {
  186. cERROR(1, "strtoUCS: char2uni of %d returned %d",
  187. (int)*from, charlen);
  188. /* A question mark */
  189. to[i] = cpu_to_le16(0x003f);
  190. charlen = 1;
  191. } else
  192. to[i] = cpu_to_le16(wchar_to[i]);
  193. }
  194. to[i] = 0;
  195. return i;
  196. }
  197. /*
  198. * cifs_strndup_from_ucs - copy a string from wire format to the local codepage
  199. * @src - source string
  200. * @maxlen - don't walk past this many bytes in the source string
  201. * @is_unicode - is this a unicode string?
  202. * @codepage - destination codepage
  203. *
  204. * Take a string given by the server, convert it to the local codepage and
  205. * put it in a new buffer. Returns a pointer to the new string or NULL on
  206. * error.
  207. */
  208. char *
  209. cifs_strndup_from_ucs(const char *src, const int maxlen, const bool is_unicode,
  210. const struct nls_table *codepage)
  211. {
  212. int len;
  213. char *dst;
  214. if (is_unicode) {
  215. len = cifs_ucs2_bytes((__le16 *) src, maxlen, codepage);
  216. len += nls_nullsize(codepage);
  217. dst = kmalloc(len, GFP_KERNEL);
  218. if (!dst)
  219. return NULL;
  220. cifs_from_ucs2(dst, (__le16 *) src, len, maxlen, codepage,
  221. false);
  222. } else {
  223. len = strnlen(src, maxlen);
  224. len++;
  225. dst = kmalloc(len, GFP_KERNEL);
  226. if (!dst)
  227. return NULL;
  228. strlcpy(dst, src, len);
  229. }
  230. return dst;
  231. }