gconv_charset.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Charset name normalization.
  2. Copyright (C) 2020-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17. #include <locale.h>
  18. #include <stdbool.h>
  19. #include <string.h>
  20. #include <sys/stat.h>
  21. #include "gconv_int.h"
  22. #include "gconv_charset.h"
  23. /* This function returns a pointer to the last suffix in a conversion code
  24. string. Valid suffixes matched by this function are of the form: '/' or ','
  25. followed by arbitrary text that doesn't contain '/' or ','. It does not
  26. edit the string in any way. The caller is expected to parse the suffix and
  27. remove it (by e.g. truncating the string) before the next call. */
  28. static char *
  29. find_suffix (char *s)
  30. {
  31. /* The conversion code is in the form of a triplet, separated by '/' chars.
  32. The third component of the triplet contains suffixes. If we don't have two
  33. slashes, we don't have a suffix. */
  34. int slash_count = 0;
  35. char *suffix_term = NULL;
  36. for (int i = 0; s[i] != '\0'; i++)
  37. switch (s[i])
  38. {
  39. case '/':
  40. slash_count++;
  41. [[fallthrough]];
  42. case ',':
  43. suffix_term = &s[i];
  44. }
  45. if (slash_count >= 2)
  46. return suffix_term;
  47. return NULL;
  48. }
  49. struct gconv_parsed_code
  50. {
  51. char *code;
  52. bool translit;
  53. bool ignore;
  54. };
  55. /* This function parses an iconv_open encoding PC.CODE, strips any suffixes
  56. (such as TRANSLIT or IGNORE) from it and sets corresponding flags in it. */
  57. static void
  58. gconv_parse_code (struct gconv_parsed_code *pc)
  59. {
  60. pc->translit = false;
  61. pc->ignore = false;
  62. while (1)
  63. {
  64. /* First drop any trailing whitespaces and separators. */
  65. size_t len = strlen (pc->code);
  66. while ((len > 0)
  67. && (isspace (pc->code[len - 1])
  68. || pc->code[len - 1] == ','
  69. || pc->code[len - 1] == '/'))
  70. len--;
  71. pc->code[len] = '\0';
  72. if (len == 0)
  73. return;
  74. char * suffix = find_suffix (pc->code);
  75. if (suffix == NULL)
  76. {
  77. /* At this point, we have processed and removed all suffixes from the
  78. code and what remains of the code is suffix free. */
  79. return;
  80. }
  81. else
  82. {
  83. /* A suffix is processed from the end of the code array going
  84. backwards, one suffix at a time. The suffix is an index into the
  85. code character array and points to: one past the end of the code
  86. and any unprocessed suffixes, and to the beginning of the suffix
  87. currently being processed during this iteration. We must process
  88. this suffix and then drop it from the code by terminating the
  89. preceding text with NULL.
  90. We want to allow and recognize suffixes such as:
  91. "/TRANSLIT" i.e. single suffix
  92. "//TRANSLIT" i.e. single suffix and multiple separators
  93. "//TRANSLIT/IGNORE" i.e. suffixes separated by "/"
  94. "/TRANSLIT//IGNORE" i.e. suffixes separated by "//"
  95. "//IGNORE,TRANSLIT" i.e. suffixes separated by ","
  96. "//IGNORE," i.e. trailing ","
  97. "//TRANSLIT/" i.e. trailing "/"
  98. "//TRANSLIT//" i.e. trailing "//"
  99. "/" i.e. empty suffix.
  100. Unknown suffixes are silently discarded and ignored. */
  101. if ((__strcasecmp_l (suffix,
  102. GCONV_TRIPLE_SEPARATOR
  103. GCONV_TRANSLIT_SUFFIX,
  104. _nl_C_locobj_ptr) == 0)
  105. || (__strcasecmp_l (suffix,
  106. GCONV_SUFFIX_SEPARATOR
  107. GCONV_TRANSLIT_SUFFIX,
  108. _nl_C_locobj_ptr) == 0))
  109. pc->translit = true;
  110. if ((__strcasecmp_l (suffix,
  111. GCONV_TRIPLE_SEPARATOR
  112. GCONV_IGNORE_ERRORS_SUFFIX,
  113. _nl_C_locobj_ptr) == 0)
  114. || (__strcasecmp_l (suffix,
  115. GCONV_SUFFIX_SEPARATOR
  116. GCONV_IGNORE_ERRORS_SUFFIX,
  117. _nl_C_locobj_ptr) == 0))
  118. pc->ignore = true;
  119. /* We just processed this suffix. We can now drop it from the
  120. code string by truncating it at the suffix's position. */
  121. suffix[0] = '\0';
  122. }
  123. }
  124. }
  125. /* This function accepts the charset names of the source and destination of the
  126. conversion and populates *conv_spec with an equivalent conversion
  127. specification that may later be used by __gconv_open. The charset names
  128. might contain options in the form of suffixes that alter the conversion,
  129. e.g. "ISO-10646/UTF-8/TRANSLIT". It processes the charset names, ignoring
  130. and truncating any suffix options in fromcode, and processing and truncating
  131. any suffix options in tocode. Supported suffix options ("TRANSLIT" or
  132. "IGNORE") when found in tocode lead to the corresponding flag in *conv_spec
  133. to be set to true. Unrecognized suffix options are silently discarded. If
  134. the function succeeds, it returns conv_spec back to the caller. It returns
  135. NULL upon failure. conv_spec must be allocated and freed by the caller. */
  136. struct gconv_spec *
  137. __gconv_create_spec (struct gconv_spec *conv_spec, const char *fromcode,
  138. const char *tocode)
  139. {
  140. struct gconv_parsed_code pfc, ptc;
  141. struct gconv_spec *ret = NULL;
  142. pfc.code = __strdup (fromcode);
  143. ptc.code = __strdup (tocode);
  144. if ((pfc.code == NULL)
  145. || (ptc.code == NULL))
  146. goto out;
  147. gconv_parse_code (&pfc);
  148. gconv_parse_code (&ptc);
  149. /* We ignore suffixes in the fromcode because that is how the current
  150. implementation has always handled them. Only suffixes in the tocode are
  151. processed and handled. The reality is that invalid input in the input
  152. character set should only be ignored if the fromcode specifies IGNORE.
  153. The current implementation ignores invalid input in the input character
  154. set if the tocode contains IGNORE. We preserve this behavior for
  155. backwards compatibility. In the future we may split the handling of
  156. IGNORE to allow a finer grained specification of ignoring invalid input
  157. and/or ignoring invalid output. */
  158. conv_spec->translit = ptc.translit;
  159. conv_spec->ignore = ptc.ignore;
  160. /* 3 extra bytes because 1 extra for '\0', and 2 extra so strip might
  161. be able to add one or two trailing '/' characters if necessary. */
  162. conv_spec->fromcode = malloc (strlen (fromcode) + 3);
  163. if (conv_spec->fromcode == NULL)
  164. goto out;
  165. conv_spec->tocode = malloc (strlen (tocode) + 3);
  166. if (conv_spec->tocode == NULL)
  167. {
  168. free (conv_spec->fromcode);
  169. conv_spec->fromcode = NULL;
  170. goto out;
  171. }
  172. /* Strip unrecognized characters and ensure that the code has two '/'
  173. characters as per conversion code triplet specification. */
  174. strip (conv_spec->fromcode, pfc.code);
  175. strip (conv_spec->tocode, ptc.code);
  176. ret = conv_spec;
  177. out:
  178. free (pfc.code);
  179. free (ptc.code);
  180. return ret;
  181. }
  182. libc_hidden_def (__gconv_create_spec)
  183. void
  184. __gconv_destroy_spec (struct gconv_spec *conv_spec)
  185. {
  186. free (conv_spec->fromcode);
  187. free (conv_spec->tocode);
  188. return;
  189. }
  190. libc_hidden_def (__gconv_destroy_spec)