cifs_unicode.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * fs/cifs/cifs_unicode.c
  3. *
  4. * Copyright (c) International Business Machines Corp., 2000,2005
  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 "cifs_unicode.h"
  23. #include "cifs_uniupr.h"
  24. #include "cifspdu.h"
  25. #include "cifsglob.h"
  26. #include "cifs_debug.h"
  27. /*
  28. * NAME: cifs_strfromUCS()
  29. *
  30. * FUNCTION: Convert little-endian unicode string to character string
  31. *
  32. */
  33. int
  34. cifs_strfromUCS_le(char *to, const __le16 * from,
  35. int len, const struct nls_table *codepage)
  36. {
  37. int i;
  38. int outlen = 0;
  39. for (i = 0; (i < len) && from[i]; i++) {
  40. int charlen;
  41. /* 2.4.0 kernel or greater */
  42. charlen =
  43. codepage->uni2char(le16_to_cpu(from[i]), &to[outlen],
  44. NLS_MAX_CHARSET_SIZE);
  45. if (charlen > 0) {
  46. outlen += charlen;
  47. } else {
  48. to[outlen++] = '?';
  49. }
  50. }
  51. to[outlen] = 0;
  52. return outlen;
  53. }
  54. /*
  55. * NAME: cifs_strtoUCS()
  56. *
  57. * FUNCTION: Convert character string to unicode string
  58. *
  59. */
  60. int
  61. cifs_strtoUCS(__le16 * to, const char *from, int len,
  62. const struct nls_table *codepage)
  63. {
  64. int charlen;
  65. int i;
  66. wchar_t * wchar_to = (wchar_t *)to; /* needed to quiet sparse */
  67. for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
  68. /* works for 2.4.0 kernel or later */
  69. charlen = codepage->char2uni(from, len, &wchar_to[i]);
  70. if (charlen < 1) {
  71. cERROR(1,
  72. ("cifs_strtoUCS: char2uni returned %d",
  73. charlen));
  74. /* A question mark */
  75. to[i] = cpu_to_le16(0x003f);
  76. charlen = 1;
  77. } else
  78. to[i] = cpu_to_le16(wchar_to[i]);
  79. }
  80. to[i] = 0;
  81. return i;
  82. }