cifs_unicode.c 16 KB

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