cifs_unicode.c 16 KB

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