cifs_unicode.c 2.2 KB

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