cifs_unicode.c 16 KB

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