cifs_unicode.c 16 KB

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