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 "cifs_unicode.h"
  23. #include "cifs_uniupr.h"
  24. #include "cifspdu.h"
  25. #include "cifsglob.h"
  26. #include "cifs_debug.h"
  27. /*
  28. * cifs_ucs2_bytes - how long will a string be after conversion?
  29. * @ucs - pointer to input string
  30. * @maxbytes - don't go past this many bytes of input string
  31. * @codepage - destination codepage
  32. *
  33. * Walk a ucs2le string and return the number of bytes that the string will
  34. * be after being converted to the given charset, not including any null
  35. * termination required. Don't walk past maxbytes in the source buffer.
  36. */
  37. int
  38. cifs_ucs2_bytes(const __le16 *from, int maxbytes,
  39. const struct nls_table *codepage)
  40. {
  41. int i;
  42. int charlen, outlen = 0;
  43. int maxwords = maxbytes / 2;
  44. char tmp[NLS_MAX_CHARSET_SIZE];
  45. for (i = 0; from[i] && i < maxwords; i++) {
  46. charlen = codepage->uni2char(le16_to_cpu(from[i]), tmp,
  47. NLS_MAX_CHARSET_SIZE);
  48. if (charlen > 0)
  49. outlen += charlen;
  50. else
  51. outlen++;
  52. }
  53. return outlen;
  54. }
  55. /*
  56. * cifs_mapchar - convert a little-endian char to proper char in codepage
  57. * @target - where converted character should be copied
  58. * @src_char - 2 byte little-endian source character
  59. * @cp - codepage to which character should be converted
  60. * @mapchar - should character be mapped according to mapchars mount option?
  61. *
  62. * This function handles the conversion of a single character. It is the
  63. * responsibility of the caller to ensure that the target buffer is large
  64. * enough to hold the result of the conversion (at least NLS_MAX_CHARSET_SIZE).
  65. */
  66. static int
  67. cifs_mapchar(char *target, const __le16 src_char, const struct nls_table *cp,
  68. bool mapchar)
  69. {
  70. int len = 1;
  71. if (!mapchar)
  72. goto cp_convert;
  73. /*
  74. * BB: Cannot handle remapping UNI_SLASH until all the calls to
  75. * build_path_from_dentry are modified, as they use slash as
  76. * separator.
  77. */
  78. switch (le16_to_cpu(src_char)) {
  79. case UNI_COLON:
  80. *target = ':';
  81. break;
  82. case UNI_ASTERIK:
  83. *target = '*';
  84. break;
  85. case UNI_QUESTION:
  86. *target = '?';
  87. break;
  88. case UNI_PIPE:
  89. *target = '|';
  90. break;
  91. case UNI_GRTRTHAN:
  92. *target = '>';
  93. break;
  94. case UNI_LESSTHAN:
  95. *target = '<';
  96. break;
  97. default:
  98. goto cp_convert;
  99. }
  100. out:
  101. return len;
  102. cp_convert:
  103. len = cp->uni2char(le16_to_cpu(src_char), target,
  104. NLS_MAX_CHARSET_SIZE);
  105. if (len <= 0) {
  106. *target = '?';
  107. len = 1;
  108. }
  109. goto out;
  110. }
  111. /*
  112. * cifs_from_ucs2 - convert utf16le string to local charset
  113. * @to - destination buffer
  114. * @from - source buffer
  115. * @tolen - destination buffer size (in bytes)
  116. * @fromlen - source buffer size (in bytes)
  117. * @codepage - codepage to which characters should be converted
  118. * @mapchar - should characters be remapped according to the mapchars option?
  119. *
  120. * Convert a little-endian ucs2le string (as sent by the server) to a string
  121. * in the provided codepage. The tolen and fromlen parameters are to ensure
  122. * that the code doesn't walk off of the end of the buffer (which is always
  123. * a danger if the alignment of the source buffer is off). The destination
  124. * string is always properly null terminated and fits in the destination
  125. * buffer. Returns the length of the destination string in bytes (including
  126. * null terminator).
  127. *
  128. * Note that some windows versions actually send multiword UTF-16 characters
  129. * instead of straight UCS-2. The linux nls routines however aren't able to
  130. * deal with those characters properly. In the event that we get some of
  131. * those characters, they won't be translated properly.
  132. */
  133. int
  134. cifs_from_ucs2(char *to, const __le16 *from, int tolen, int fromlen,
  135. const struct nls_table *codepage, bool mapchar)
  136. {
  137. int i, charlen, safelen;
  138. int outlen = 0;
  139. int nullsize = nls_nullsize(codepage);
  140. int fromwords = fromlen / 2;
  141. char tmp[NLS_MAX_CHARSET_SIZE];
  142. /*
  143. * because the chars can be of varying widths, we need to take care
  144. * not to overflow the destination buffer when we get close to the
  145. * end of it. Until we get to this offset, we don't need to check
  146. * for overflow however.
  147. */
  148. safelen = tolen - (NLS_MAX_CHARSET_SIZE + nullsize);
  149. for (i = 0; i < fromwords && from[i]; i++) {
  150. /*
  151. * check to see if converting this character might make the
  152. * conversion bleed into the null terminator
  153. */
  154. if (outlen >= safelen) {
  155. charlen = cifs_mapchar(tmp, from[i], codepage, mapchar);
  156. if ((outlen + charlen) > (tolen - nullsize))
  157. break;
  158. }
  159. /* put converted char into 'to' buffer */
  160. charlen = cifs_mapchar(&to[outlen], from[i], codepage, mapchar);
  161. outlen += charlen;
  162. }
  163. /* properly null-terminate string */
  164. for (i = 0; i < nullsize; i++)
  165. to[outlen++] = 0;
  166. return outlen;
  167. }
  168. /*
  169. * NAME: cifs_strtoUCS()
  170. *
  171. * FUNCTION: Convert character string to unicode string
  172. *
  173. */
  174. int
  175. cifs_strtoUCS(__le16 *to, const char *from, int len,
  176. const struct nls_table *codepage)
  177. {
  178. int charlen;
  179. int i;
  180. wchar_t *wchar_to = (wchar_t *)to; /* needed to quiet sparse */
  181. for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
  182. /* works for 2.4.0 kernel or later */
  183. charlen = codepage->char2uni(from, len, &wchar_to[i]);
  184. if (charlen < 1) {
  185. cERROR(1,
  186. ("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. }