cifs_unicode.c 16 KB

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