jfs_unicode.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:3k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *   Copyright (c) International Business Machines Corp., 2000-2002
  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_types.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 wchar_t * from, /* LITTLE ENDIAN */
  31.       int len, struct nls_table *codepage)
  32. {
  33. int i;
  34. int outlen = 0;
  35. for (i = 0; (i < len) && from[i]; i++) {
  36. int charlen;
  37. charlen =
  38.     codepage->uni2char(le16_to_cpu(from[i]), &to[outlen],
  39.        NLS_MAX_CHARSET_SIZE);
  40. if (charlen > 0) {
  41. outlen += charlen;
  42. } else {
  43. to[outlen++] = '?';
  44. }
  45. }
  46. to[outlen] = 0;
  47. jEVENT(0, ("jfs_strfromUCS returning %d - '%s'n", outlen, to));
  48. return outlen;
  49. }
  50. /*
  51.  * NAME: jfs_strtoUCS()
  52.  *
  53.  * FUNCTION: Convert character string to unicode string
  54.  *
  55.  */
  56. int jfs_strtoUCS(wchar_t * to,
  57.  const char *from, int len, struct nls_table *codepage)
  58. {
  59. int charlen;
  60. int i;
  61. jEVENT(0, ("jfs_strtoUCS - '%s'n", from));
  62. for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
  63. charlen = codepage->char2uni(from, len, &to[i]);
  64. if (charlen < 1) {
  65. jERROR(1, ("jfs_strtoUCS: char2uni returned %d.n",
  66.    charlen));
  67. jERROR(1, ("charset = %s, char = 0x%xn",
  68.    codepage->charset, (unsigned char) *from));
  69. to[i] = 0x003f; /* a question mark */
  70. charlen = 1;
  71. }
  72. }
  73. jEVENT(0, (" returning %dn", i));
  74. to[i] = 0;
  75. return i;
  76. }
  77. /*
  78.  * NAME: get_UCSname()
  79.  *
  80.  * FUNCTION: Allocate and translate to unicode string
  81.  *
  82.  */
  83. int get_UCSname(struct component_name * uniName, struct dentry *dentry,
  84. struct nls_table *nls_tab)
  85. {
  86. int length = dentry->d_name.len;
  87. if (length > JFS_NAME_MAX)
  88. return ENAMETOOLONG;
  89. uniName->name =
  90.     kmalloc((length + 1) * sizeof(wchar_t), GFP_NOFS);
  91. if (uniName->name == NULL)
  92. return ENOSPC;
  93. uniName->namlen = jfs_strtoUCS(uniName->name, dentry->d_name.name,
  94.        length, nls_tab);
  95. return 0;
  96. }