cifs_unicode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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_fs_sb.h"
  24. #include "cifs_unicode.h"
  25. #include "cifs_uniupr.h"
  26. #include "cifspdu.h"
  27. #include "cifsglob.h"
  28. #include "cifs_debug.h"
  29. /*
  30. * cifs_utf16_bytes - how long will a string be after conversion?
  31. * @utf16 - pointer to input string
  32. * @maxbytes - don't go past this many bytes of input string
  33. * @codepage - destination codepage
  34. *
  35. * Walk a utf16le string and return the number of bytes that the string will
  36. * be after being converted to the given charset, not including any null
  37. * termination required. Don't walk past maxbytes in the source buffer.
  38. */
  39. int
  40. cifs_utf16_bytes(const __le16 *from, int maxbytes,
  41. const struct nls_table *codepage)
  42. {
  43. int i;
  44. int charlen, outlen = 0;
  45. int maxwords = maxbytes / 2;
  46. char tmp[NLS_MAX_CHARSET_SIZE];
  47. __u16 ftmp;
  48. for (i = 0; i < maxwords; i++) {
  49. ftmp = get_unaligned_le16(&from[i]);
  50. if (ftmp == 0)
  51. break;
  52. charlen = codepage->uni2char(ftmp, tmp, NLS_MAX_CHARSET_SIZE);
  53. if (charlen > 0)
  54. outlen += charlen;
  55. else
  56. outlen++;
  57. }
  58. return outlen;
  59. }
  60. int cifs_remap(struct cifs_sb_info *cifs_sb)
  61. {
  62. int map_type;
  63. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
  64. map_type = SFM_MAP_UNI_RSVD;
  65. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
  66. map_type = SFU_MAP_UNI_RSVD;
  67. else
  68. map_type = NO_MAP_UNI_RSVD;
  69. return map_type;
  70. }
  71. /* Convert character using the SFU - "Services for Unix" remapping range */
  72. static bool
  73. convert_sfu_char(const __u16 src_char, char *target)
  74. {
  75. /*
  76. * BB: Cannot handle remapping UNI_SLASH until all the calls to
  77. * build_path_from_dentry are modified, as they use slash as
  78. * separator.
  79. */
  80. switch (src_char) {
  81. case UNI_COLON:
  82. *target = ':';
  83. break;
  84. case UNI_ASTERISK:
  85. *target = '*';
  86. break;
  87. case UNI_QUESTION:
  88. *target = '?';
  89. break;
  90. case UNI_PIPE:
  91. *target = '|';
  92. break;
  93. case UNI_GRTRTHAN:
  94. *target = '>';
  95. break;
  96. case UNI_LESSTHAN:
  97. *target = '<';
  98. break;
  99. default:
  100. return false;
  101. }
  102. return true;
  103. }
  104. /* Convert character using the SFM - "Services for Mac" remapping range */
  105. static bool
  106. convert_sfm_char(const __u16 src_char, char *target)
  107. {
  108. switch (src_char) {
  109. case SFM_COLON:
  110. *target = ':';
  111. break;
  112. case SFM_ASTERISK:
  113. *target = '*';
  114. break;
  115. case SFM_QUESTION:
  116. *target = '?';
  117. break;
  118. case SFM_PIPE:
  119. *target = '|';
  120. break;
  121. case SFM_GRTRTHAN:
  122. *target = '>';
  123. break;
  124. case SFM_LESSTHAN:
  125. *target = '<';
  126. break;
  127. case SFM_SLASH:
  128. *target = '\\';
  129. break;
  130. default:
  131. return false;
  132. }
  133. return true;
  134. }
  135. /*
  136. * cifs_mapchar - convert a host-endian char to proper char in codepage
  137. * @target - where converted character should be copied
  138. * @src_char - 2 byte host-endian source character
  139. * @cp - codepage to which character should be converted
  140. * @map_type - How should the 7 NTFS/SMB reserved characters be mapped to UCS2?
  141. *
  142. * This function handles the conversion of a single character. It is the
  143. * responsibility of the caller to ensure that the target buffer is large
  144. * enough to hold the result of the conversion (at least NLS_MAX_CHARSET_SIZE).
  145. */
  146. static int
  147. cifs_mapchar(char *target, const __u16 src_char, const struct nls_table *cp,
  148. int maptype)
  149. {
  150. int len = 1;
  151. if ((maptype == SFM_MAP_UNI_RSVD) && convert_sfm_char(src_char, target))
  152. return len;
  153. else if ((maptype == SFU_MAP_UNI_RSVD) &&
  154. convert_sfu_char(src_char, target))
  155. return len;
  156. /* if character not one of seven in special remap set */
  157. len = cp->uni2char(src_char, target, NLS_MAX_CHARSET_SIZE);
  158. if (len <= 0) {
  159. *target = '?';
  160. len = 1;
  161. }
  162. return len;
  163. }
  164. /*
  165. * cifs_from_utf16 - convert utf16le string to local charset
  166. * @to - destination buffer
  167. * @from - source buffer
  168. * @tolen - destination buffer size (in bytes)
  169. * @fromlen - source buffer size (in bytes)
  170. * @codepage - codepage to which characters should be converted
  171. * @mapchar - should characters be remapped according to the mapchars option?
  172. *
  173. * Convert a little-endian utf16le string (as sent by the server) to a string
  174. * in the provided codepage. The tolen and fromlen parameters are to ensure
  175. * that the code doesn't walk off of the end of the buffer (which is always
  176. * a danger if the alignment of the source buffer is off). The destination
  177. * string is always properly null terminated and fits in the destination
  178. * buffer. Returns the length of the destination string in bytes (including
  179. * null terminator).
  180. *
  181. * Note that some windows versions actually send multiword UTF-16 characters
  182. * instead of straight UTF16-2. The linux nls routines however aren't able to
  183. * deal with those characters properly. In the event that we get some of
  184. * those characters, they won't be translated properly.
  185. */
  186. int
  187. cifs_from_utf16(char *to, const __le16 *from, int tolen, int fromlen,
  188. const struct nls_table *codepage, int map_type)
  189. {
  190. int i, charlen, safelen;
  191. int outlen = 0;
  192. int nullsize = nls_nullsize(codepage);
  193. int fromwords = fromlen / 2;
  194. char tmp[NLS_MAX_CHARSET_SIZE];
  195. __u16 ftmp;
  196. /*
  197. * because the chars can be of varying widths, we need to take care
  198. * not to overflow the destination buffer when we get close to the
  199. * end of it. Until we get to this offset, we don't need to check
  200. * for overflow however.
  201. */
  202. safelen = tolen - (NLS_MAX_CHARSET_SIZE + nullsize);
  203. for (i = 0; i < fromwords; i++) {
  204. ftmp = get_unaligned_le16(&from[i]);
  205. if (ftmp == 0)
  206. break;
  207. /*
  208. * check to see if converting this character might make the
  209. * conversion bleed into the null terminator
  210. */
  211. if (outlen >= safelen) {
  212. charlen = cifs_mapchar(tmp, ftmp, codepage, map_type);
  213. if ((outlen + charlen) > (tolen - nullsize))
  214. break;
  215. }
  216. /* put converted char into 'to' buffer */
  217. charlen = cifs_mapchar(&to[outlen], ftmp, codepage, map_type);
  218. outlen += charlen;
  219. }
  220. /* properly null-terminate string */
  221. for (i = 0; i < nullsize; i++)
  222. to[outlen++] = 0;
  223. return outlen;
  224. }
  225. /*
  226. * NAME: cifs_strtoUTF16()
  227. *
  228. * FUNCTION: Convert character string to unicode string
  229. *
  230. */
  231. int
  232. cifs_strtoUTF16(__le16 *to, const char *from, int len,
  233. const struct nls_table *codepage)
  234. {
  235. int charlen;
  236. int i;
  237. wchar_t wchar_to; /* needed to quiet sparse */
  238. /* special case for utf8 to handle no plane0 chars */
  239. if (!strcmp(codepage->charset, "utf8")) {
  240. /*
  241. * convert utf8 -> utf16, we assume we have enough space
  242. * as caller should have assumed conversion does not overflow
  243. * in destination len is length in wchar_t units (16bits)
  244. */
  245. i = utf8s_to_utf16s(from, len, UTF16_LITTLE_ENDIAN,
  246. (wchar_t *) to, len);
  247. /* if success terminate and exit */
  248. if (i >= 0)
  249. goto success;
  250. /*
  251. * if fails fall back to UCS encoding as this
  252. * function should not return negative values
  253. * currently can fail only if source contains
  254. * invalid encoded characters
  255. */
  256. }
  257. for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
  258. charlen = codepage->char2uni(from, len, &wchar_to);
  259. if (charlen < 1) {
  260. cifs_dbg(VFS, "strtoUTF16: char2uni of 0x%x returned %d\n",
  261. *from, charlen);
  262. /* A question mark */
  263. wchar_to = 0x003f;
  264. charlen = 1;
  265. }
  266. put_unaligned_le16(wchar_to, &to[i]);
  267. }
  268. success:
  269. put_unaligned_le16(0, &to[i]);
  270. return i;
  271. }
  272. /*
  273. * cifs_strndup_from_utf16 - copy a string from wire format to the local
  274. * codepage
  275. * @src - source string
  276. * @maxlen - don't walk past this many bytes in the source string
  277. * @is_unicode - is this a unicode string?
  278. * @codepage - destination codepage
  279. *
  280. * Take a string given by the server, convert it to the local codepage and
  281. * put it in a new buffer. Returns a pointer to the new string or NULL on
  282. * error.
  283. */
  284. char *
  285. cifs_strndup_from_utf16(const char *src, const int maxlen,
  286. const bool is_unicode, const struct nls_table *codepage)
  287. {
  288. int len;
  289. char *dst;
  290. if (is_unicode) {
  291. len = cifs_utf16_bytes((__le16 *) src, maxlen, codepage);
  292. len += nls_nullsize(codepage);
  293. dst = kmalloc(len, GFP_KERNEL);
  294. if (!dst)
  295. return NULL;
  296. cifs_from_utf16(dst, (__le16 *) src, len, maxlen, codepage,
  297. NO_MAP_UNI_RSVD);
  298. } else {
  299. len = strnlen(src, maxlen);
  300. len++;
  301. dst = kmalloc(len, GFP_KERNEL);
  302. if (!dst)
  303. return NULL;
  304. strlcpy(dst, src, len);
  305. }
  306. return dst;
  307. }
  308. static __le16 convert_to_sfu_char(char src_char)
  309. {
  310. __le16 dest_char;
  311. switch (src_char) {
  312. case ':':
  313. dest_char = cpu_to_le16(UNI_COLON);
  314. break;
  315. case '*':
  316. dest_char = cpu_to_le16(UNI_ASTERISK);
  317. break;
  318. case '?':
  319. dest_char = cpu_to_le16(UNI_QUESTION);
  320. break;
  321. case '<':
  322. dest_char = cpu_to_le16(UNI_LESSTHAN);
  323. break;
  324. case '>':
  325. dest_char = cpu_to_le16(UNI_GRTRTHAN);
  326. break;
  327. case '|':
  328. dest_char = cpu_to_le16(UNI_PIPE);
  329. break;
  330. default:
  331. dest_char = 0;
  332. }
  333. return dest_char;
  334. }
  335. static __le16 convert_to_sfm_char(char src_char)
  336. {
  337. __le16 dest_char;
  338. switch (src_char) {
  339. case ':':
  340. dest_char = cpu_to_le16(SFM_COLON);
  341. break;
  342. case '*':
  343. dest_char = cpu_to_le16(SFM_ASTERISK);
  344. break;
  345. case '?':
  346. dest_char = cpu_to_le16(SFM_QUESTION);
  347. break;
  348. case '<':
  349. dest_char = cpu_to_le16(SFM_LESSTHAN);
  350. break;
  351. case '>':
  352. dest_char = cpu_to_le16(SFM_GRTRTHAN);
  353. break;
  354. case '|':
  355. dest_char = cpu_to_le16(SFM_PIPE);
  356. break;
  357. default:
  358. dest_char = 0;
  359. }
  360. return dest_char;
  361. }
  362. /*
  363. * Convert 16 bit Unicode pathname to wire format from string in current code
  364. * page. Conversion may involve remapping up the six characters that are
  365. * only legal in POSIX-like OS (if they are present in the string). Path
  366. * names are little endian 16 bit Unicode on the wire
  367. */
  368. int
  369. cifsConvertToUTF16(__le16 *target, const char *source, int srclen,
  370. const struct nls_table *cp, int map_chars)
  371. {
  372. int i, charlen;
  373. int j = 0;
  374. char src_char;
  375. __le16 dst_char;
  376. wchar_t tmp;
  377. if (map_chars == NO_MAP_UNI_RSVD)
  378. return cifs_strtoUTF16(target, source, PATH_MAX, cp);
  379. for (i = 0; i < srclen; j++) {
  380. src_char = source[i];
  381. charlen = 1;
  382. /* check if end of string */
  383. if (src_char == 0)
  384. goto ctoUTF16_out;
  385. /* see if we must remap this char */
  386. if (map_chars == SFU_MAP_UNI_RSVD)
  387. dst_char = convert_to_sfu_char(src_char);
  388. else if (map_chars == SFM_MAP_UNI_RSVD)
  389. dst_char = convert_to_sfm_char(src_char);
  390. else
  391. dst_char = 0;
  392. /*
  393. * FIXME: We can not handle remapping backslash (UNI_SLASH)
  394. * until all the calls to build_path_from_dentry are modified,
  395. * as they use backslash as separator.
  396. */
  397. if (dst_char == 0) {
  398. charlen = cp->char2uni(source + i, srclen - i, &tmp);
  399. dst_char = cpu_to_le16(tmp);
  400. /*
  401. * if no match, use question mark, which at least in
  402. * some cases serves as wild card
  403. */
  404. if (charlen < 1) {
  405. dst_char = cpu_to_le16(0x003f);
  406. charlen = 1;
  407. }
  408. }
  409. /*
  410. * character may take more than one byte in the source string,
  411. * but will take exactly two bytes in the target string
  412. */
  413. i += charlen;
  414. put_unaligned(dst_char, &target[j]);
  415. }
  416. ctoUTF16_out:
  417. put_unaligned(0, &target[j]); /* Null terminate target unicode string */
  418. return j;
  419. }
  420. #ifdef CONFIG_CIFS_SMB2
  421. /*
  422. * cifs_local_to_utf16_bytes - how long will a string be after conversion?
  423. * @from - pointer to input string
  424. * @maxbytes - don't go past this many bytes of input string
  425. * @codepage - source codepage
  426. *
  427. * Walk a string and return the number of bytes that the string will
  428. * be after being converted to the given charset, not including any null
  429. * termination required. Don't walk past maxbytes in the source buffer.
  430. */
  431. static int
  432. cifs_local_to_utf16_bytes(const char *from, int len,
  433. const struct nls_table *codepage)
  434. {
  435. int charlen;
  436. int i;
  437. wchar_t wchar_to;
  438. for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
  439. charlen = codepage->char2uni(from, len, &wchar_to);
  440. /* Failed conversion defaults to a question mark */
  441. if (charlen < 1)
  442. charlen = 1;
  443. }
  444. return 2 * i; /* UTF16 characters are two bytes */
  445. }
  446. /*
  447. * cifs_strndup_to_utf16 - copy a string to wire format from the local codepage
  448. * @src - source string
  449. * @maxlen - don't walk past this many bytes in the source string
  450. * @utf16_len - the length of the allocated string in bytes (including null)
  451. * @cp - source codepage
  452. * @remap - map special chars
  453. *
  454. * Take a string convert it from the local codepage to UTF16 and
  455. * put it in a new buffer. Returns a pointer to the new string or NULL on
  456. * error.
  457. */
  458. __le16 *
  459. cifs_strndup_to_utf16(const char *src, const int maxlen, int *utf16_len,
  460. const struct nls_table *cp, int remap)
  461. {
  462. int len;
  463. __le16 *dst;
  464. len = cifs_local_to_utf16_bytes(src, maxlen, cp);
  465. len += 2; /* NULL */
  466. dst = kmalloc(len, GFP_KERNEL);
  467. if (!dst) {
  468. *utf16_len = 0;
  469. return NULL;
  470. }
  471. cifsConvertToUTF16(dst, src, strlen(src), cp, remap);
  472. *utf16_len = len;
  473. return dst;
  474. }
  475. #endif /* CONFIG_CIFS_SMB2 */