cifs_unicode.c 16 KB

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