gconv_charset.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Charset name normalization.
  2. Copyright (C) 2001-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. <https://www.gnu.org/licenses/>. */
  15. #include <ctype.h>
  16. #include <locale.h>
  17. #include <stdbool.h>
  18. #include <string.h>
  19. #include <sys/stat.h>
  20. #include <stdlib.h>
  21. #include "gconv_int.h"
  22. /* An iconv encoding is in the form of a triplet, with parts separated by
  23. a '/' character. The first part is the standard name, the second part is
  24. the character set, and the third part is the error handler. If the first
  25. part is sufficient to identify both the standard and the character set
  26. then the second part can be empty e.g. UTF-8//. If the first part is not
  27. sufficient to identify both the standard and the character set then the
  28. second part is required e.g. ISO-10646/UTF8/. If neither the first or
  29. second parts are provided e.g. //, then the current locale is used.
  30. The actual values used in the first and second parts are not entirely
  31. relevant to the implementation. The values themselves are used in a hash
  32. table to lookup modules and so the naming convention of the first two parts
  33. is somewhat arbitrary and only helps locate the entries in the cache.
  34. The third part is the error handler and is comprised of a ',' or '/'
  35. separated list of suffixes. Currently, we support "TRANSLIT" for
  36. transliteration and "IGNORE" for ignoring conversion errors due to
  37. unrecognized input characters. */
  38. #define GCONV_TRIPLE_SEPARATOR "/"
  39. #define GCONV_SUFFIX_SEPARATOR ","
  40. #define GCONV_TRANSLIT_SUFFIX "TRANSLIT"
  41. #define GCONV_IGNORE_ERRORS_SUFFIX "IGNORE"
  42. /* This function copies in-order, characters from the source 's' that are
  43. either alphanumeric or one in one of these: "_-.,:/" - into the destination
  44. 'wp' while dropping all other characters. In the process, it converts all
  45. alphabetical characters to upper case. It then appends up to two '/'
  46. characters so that the total number of '/'es in the destination is 2. */
  47. static inline void __attribute__ ((unused, always_inline))
  48. strip (char *wp, const char *s)
  49. {
  50. int slash_count = 0;
  51. while (*s != '\0')
  52. {
  53. if (__isalnum_l (*s, _nl_C_locobj_ptr)
  54. || *s == '_' || *s == '-' || *s == '.' || *s == ',' || *s == ':')
  55. *wp++ = __toupper_l (*s, _nl_C_locobj_ptr);
  56. else if (*s == '/')
  57. {
  58. if (++slash_count == 3)
  59. break;
  60. *wp++ = '/';
  61. }
  62. ++s;
  63. }
  64. while (slash_count++ < 2)
  65. *wp++ = '/';
  66. *wp = '\0';
  67. }
  68. static inline char * __attribute__ ((unused, always_inline))
  69. upstr (char *dst, const char *str)
  70. {
  71. char *cp = dst;
  72. while ((*cp++ = __toupper_l (*str++, _nl_C_locobj_ptr)) != '\0')
  73. /* nothing */;
  74. return dst;
  75. }