cifs_unicode.c 16 KB

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