jfs_unicode.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) International Business Machines Corp., 2000-2004
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/slab.h>
  20. #include "jfs_incore.h"
  21. #include "jfs_filsys.h"
  22. #include "jfs_unicode.h"
  23. #include "jfs_debug.h"
  24. /*
  25. * NAME: jfs_strfromUCS()
  26. *
  27. * FUNCTION: Convert little-endian unicode string to character string
  28. *
  29. */
  30. int jfs_strfromUCS_le(char *to, const __le16 * from,
  31. int len, struct nls_table *codepage)
  32. {
  33. int i;
  34. int outlen = 0;
  35. static int warn_again = 5; /* Only warn up to 5 times total */
  36. int warn = !!warn_again; /* once per string */
  37. if (codepage) {
  38. for (i = 0; (i < len) && from[i]; i++) {
  39. int charlen;
  40. charlen =
  41. codepage->uni2char(le16_to_cpu(from[i]),
  42. &to[outlen],
  43. NLS_MAX_CHARSET_SIZE);
  44. if (charlen > 0)
  45. outlen += charlen;
  46. else
  47. to[outlen++] = '?';
  48. }
  49. } else {
  50. for (i = 0; (i < len) && from[i]; i++) {
  51. if (unlikely(le16_to_cpu(from[i]) & 0xff00)) {
  52. to[i] = '?';
  53. if (unlikely(warn)) {
  54. warn--;
  55. warn_again--;
  56. printk(KERN_ERR
  57. "non-latin1 character 0x%x found in JFS file name\n",
  58. le16_to_cpu(from[i]));
  59. printk(KERN_ERR
  60. "mount with iocharset=utf8 to access\n");
  61. }
  62. }
  63. else
  64. to[i] = (char) (le16_to_cpu(from[i]));
  65. }
  66. outlen = i;
  67. }
  68. to[outlen] = 0;
  69. return outlen;
  70. }
  71. /*
  72. * NAME: jfs_strtoUCS()
  73. *
  74. * FUNCTION: Convert character string to unicode string
  75. *
  76. */
  77. static int jfs_strtoUCS(wchar_t * to, const unsigned char *from, int len,
  78. struct nls_table *codepage)
  79. {
  80. int charlen;
  81. int i;
  82. if (codepage) {
  83. for (i = 0; len && *from; i++, from += charlen, len -= charlen)
  84. {
  85. charlen = codepage->char2uni(from, len, &to[i]);
  86. if (charlen < 1) {
  87. jfs_err("jfs_strtoUCS: char2uni returned %d.",
  88. charlen);
  89. jfs_err("charset = %s, char = 0x%x",
  90. codepage->charset, *from);
  91. return charlen;
  92. }
  93. }
  94. } else {
  95. for (i = 0; (i < len) && from[i]; i++)
  96. to[i] = (wchar_t) from[i];
  97. }
  98. to[i] = 0;
  99. return i;
  100. }
  101. /*
  102. * NAME: get_UCSname()
  103. *
  104. * FUNCTION: Allocate and translate to unicode string
  105. *
  106. */
  107. int get_UCSname(struct component_name * uniName, struct dentry *dentry)
  108. {
  109. struct nls_table *nls_tab = JFS_SBI(dentry->d_sb)->nls_tab;
  110. int length = dentry->d_name.len;
  111. if (length > JFS_NAME_MAX)
  112. return -ENAMETOOLONG;
  113. uniName->name =
  114. kmalloc((length + 1) * sizeof(wchar_t), GFP_NOFS);
  115. if (uniName->name == NULL)
  116. return -ENOMEM;
  117. uniName->namlen = jfs_strtoUCS(uniName->name, dentry->d_name.name,
  118. length, nls_tab);
  119. if (uniName->namlen < 0) {
  120. kfree(uniName->name);
  121. return uniName->namlen;
  122. }
  123. return 0;
  124. }