iconv_open.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Character set conversion.
  2. Copyright (C) 2007, 2009-2014 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License along
  12. with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include <iconv.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include "c-ctype.h"
  19. #include "c-strcase.h"
  20. #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
  21. /* Namespace cleanliness. */
  22. #define mapping_lookup rpl_iconv_open_mapping_lookup
  23. /* The macro ICONV_FLAVOR is defined to one of these or undefined. */
  24. #define ICONV_FLAVOR_AIX "iconv_open-aix.h"
  25. #define ICONV_FLAVOR_HPUX "iconv_open-hpux.h"
  26. #define ICONV_FLAVOR_IRIX "iconv_open-irix.h"
  27. #define ICONV_FLAVOR_OSF "iconv_open-osf.h"
  28. #define ICONV_FLAVOR_SOLARIS "iconv_open-solaris.h"
  29. #ifdef ICONV_FLAVOR
  30. # include ICONV_FLAVOR
  31. #endif
  32. iconv_t
  33. rpl_iconv_open (const char *tocode, const char *fromcode)
  34. #undef iconv_open
  35. {
  36. char fromcode_upper[32];
  37. char tocode_upper[32];
  38. char *fromcode_upper_end;
  39. char *tocode_upper_end;
  40. #if REPLACE_ICONV_UTF
  41. /* Special handling of conversion between UTF-8 and UTF-{16,32}{BE,LE}.
  42. Do this here, before calling the real iconv_open(), because OSF/1 5.1
  43. iconv() to these encoding inserts a BOM, which is wrong.
  44. We do not need to handle conversion between arbitrary encodings and
  45. UTF-{16,32}{BE,LE}, because the 'striconveh' module implements two-step
  46. conversion through UTF-8.
  47. The _ICONV_* constants are chosen to be disjoint from any iconv_t
  48. returned by the system's iconv_open() functions. Recall that iconv_t
  49. is a scalar type. */
  50. if (c_toupper (fromcode[0]) == 'U'
  51. && c_toupper (fromcode[1]) == 'T'
  52. && c_toupper (fromcode[2]) == 'F'
  53. && fromcode[3] == '-')
  54. {
  55. if (c_toupper (tocode[0]) == 'U'
  56. && c_toupper (tocode[1]) == 'T'
  57. && c_toupper (tocode[2]) == 'F'
  58. && tocode[3] == '-')
  59. {
  60. if (strcmp (fromcode + 4, "8") == 0)
  61. {
  62. if (c_strcasecmp (tocode + 4, "16BE") == 0)
  63. return _ICONV_UTF8_UTF16BE;
  64. if (c_strcasecmp (tocode + 4, "16LE") == 0)
  65. return _ICONV_UTF8_UTF16LE;
  66. if (c_strcasecmp (tocode + 4, "32BE") == 0)
  67. return _ICONV_UTF8_UTF32BE;
  68. if (c_strcasecmp (tocode + 4, "32LE") == 0)
  69. return _ICONV_UTF8_UTF32LE;
  70. }
  71. else if (strcmp (tocode + 4, "8") == 0)
  72. {
  73. if (c_strcasecmp (fromcode + 4, "16BE") == 0)
  74. return _ICONV_UTF16BE_UTF8;
  75. if (c_strcasecmp (fromcode + 4, "16LE") == 0)
  76. return _ICONV_UTF16LE_UTF8;
  77. if (c_strcasecmp (fromcode + 4, "32BE") == 0)
  78. return _ICONV_UTF32BE_UTF8;
  79. if (c_strcasecmp (fromcode + 4, "32LE") == 0)
  80. return _ICONV_UTF32LE_UTF8;
  81. }
  82. }
  83. }
  84. #endif
  85. /* Do *not* add special support for 8-bit encodings like ASCII or ISO-8859-1
  86. here. This would lead to programs that work in some locales (such as the
  87. "C" or "en_US" locales) but do not work in East Asian locales. It is
  88. better if programmers make their programs depend on GNU libiconv (except
  89. on glibc systems), e.g. by using the AM_ICONV macro and documenting the
  90. dependency in an INSTALL or DEPENDENCIES file. */
  91. /* Try with the original names first.
  92. This covers the case when fromcode or tocode is a lowercase encoding name
  93. that is understood by the system's iconv_open but not listed in our
  94. mappings table. */
  95. {
  96. iconv_t cd = iconv_open (tocode, fromcode);
  97. if (cd != (iconv_t)(-1))
  98. return cd;
  99. }
  100. /* Convert the encodings to upper case, because
  101. 1. in the arguments of iconv_open() on AIX, HP-UX, and OSF/1 the case
  102. matters,
  103. 2. it makes searching in the table faster. */
  104. {
  105. const char *p = fromcode;
  106. char *q = fromcode_upper;
  107. while ((*q = c_toupper (*p)) != '\0')
  108. {
  109. p++;
  110. q++;
  111. if (q == &fromcode_upper[SIZEOF (fromcode_upper)])
  112. {
  113. errno = EINVAL;
  114. return (iconv_t)(-1);
  115. }
  116. }
  117. fromcode_upper_end = q;
  118. }
  119. {
  120. const char *p = tocode;
  121. char *q = tocode_upper;
  122. while ((*q = c_toupper (*p)) != '\0')
  123. {
  124. p++;
  125. q++;
  126. if (q == &tocode_upper[SIZEOF (tocode_upper)])
  127. {
  128. errno = EINVAL;
  129. return (iconv_t)(-1);
  130. }
  131. }
  132. tocode_upper_end = q;
  133. }
  134. #ifdef ICONV_FLAVOR
  135. /* Apply the mappings. */
  136. {
  137. const struct mapping *m =
  138. mapping_lookup (fromcode_upper, fromcode_upper_end - fromcode_upper);
  139. fromcode = (m != NULL ? m->vendor_name : fromcode_upper);
  140. }
  141. {
  142. const struct mapping *m =
  143. mapping_lookup (tocode_upper, tocode_upper_end - tocode_upper);
  144. tocode = (m != NULL ? m->vendor_name : tocode_upper);
  145. }
  146. #else
  147. fromcode = fromcode_upper;
  148. tocode = tocode_upper;
  149. #endif
  150. return iconv_open (tocode, fromcode);
  151. }