charset.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Character set conversion support for GDB.
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  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 3 of the License, or
  7. (at your option) any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef CHARSET_H
  15. #define CHARSET_H
  16. #include "gdbsupport/def-vector.h"
  17. /* If the target program uses a different character set than the host,
  18. GDB has some support for translating between the two; GDB converts
  19. characters and strings to the host character set before displaying
  20. them, and converts characters and strings appearing in expressions
  21. entered by the user to the target character set.
  22. GDB's code pretty much assumes that the host character set is some
  23. superset of ASCII; there are plenty if ('0' + n) expressions and
  24. the like. */
  25. /* Return the name of the current host/target character set. The
  26. result is owned by the charset module; the caller should not free
  27. it. */
  28. const char *host_charset (void);
  29. const char *target_charset (struct gdbarch *gdbarch);
  30. const char *target_wide_charset (struct gdbarch *gdbarch);
  31. /* These values are used to specify the type of transliteration done
  32. by convert_between_encodings. */
  33. enum transliterations
  34. {
  35. /* Error on failure to convert. */
  36. translit_none,
  37. /* Transliterate to host char. */
  38. translit_char
  39. };
  40. /* Convert between two encodings.
  41. FROM is the name of the source encoding.
  42. TO is the name of the target encoding.
  43. BYTES holds the bytes to convert; this is assumed to be characters
  44. in the target encoding.
  45. NUM_BYTES is the number of bytes.
  46. WIDTH is the width of a character from the FROM charset, in bytes.
  47. For a variable width encoding, WIDTH should be the size of a "base
  48. character".
  49. OUTPUT is an obstack where the converted data is written. The
  50. caller is responsible for initializing the obstack, and for
  51. destroying the obstack should an error occur.
  52. TRANSLIT specifies how invalid conversions should be handled. */
  53. void convert_between_encodings (const char *from, const char *to,
  54. const gdb_byte *bytes,
  55. unsigned int num_bytes,
  56. int width, struct obstack *output,
  57. enum transliterations translit);
  58. /* These values are used by wchar_iterate to report errors. */
  59. enum wchar_iterate_result
  60. {
  61. /* Ordinary return. */
  62. wchar_iterate_ok,
  63. /* Invalid input sequence. */
  64. wchar_iterate_invalid,
  65. /* Incomplete input sequence at the end of the input. */
  66. wchar_iterate_incomplete,
  67. /* EOF. */
  68. wchar_iterate_eof
  69. };
  70. /* An iterator that returns host wchar_t's from a target string. */
  71. class wchar_iterator
  72. {
  73. public:
  74. /* Create a new character iterator which returns wchar_t's. INPUT is
  75. the input buffer. BYTES is the number of bytes in the input
  76. buffer. CHARSET is the name of the character set in which INPUT is
  77. encoded. WIDTH is the number of bytes in a base character of
  78. CHARSET.
  79. This constructor can throw on error. */
  80. wchar_iterator (const gdb_byte *input, size_t bytes, const char *charset,
  81. size_t width);
  82. ~wchar_iterator ();
  83. /* Perform a single iteration of a wchar_t iterator.
  84. Returns the number of characters converted. A negative result
  85. means that EOF has been reached. A positive result indicates the
  86. number of valid wchar_ts in the result; *OUT_CHARS is updated to
  87. point to the first valid character.
  88. In all cases aside from EOF, *PTR is set to point to the first
  89. converted target byte. *LEN is set to the number of bytes
  90. converted.
  91. A zero result means one of several unusual results. *OUT_RESULT is
  92. set to indicate the type of un-ordinary return.
  93. wchar_iterate_invalid means that an invalid input character was
  94. seen. The iterator is advanced by WIDTH (the argument to
  95. the wchar_iterator constructor) bytes.
  96. wchar_iterate_incomplete means that an incomplete character was
  97. seen at the end of the input sequence.
  98. wchar_iterate_eof means that all bytes were successfully
  99. converted. The other output arguments are not set. */
  100. int iterate (enum wchar_iterate_result *out_result, gdb_wchar_t **out_chars,
  101. const gdb_byte **ptr, size_t *len);
  102. private:
  103. /* The underlying iconv descriptor. */
  104. #ifdef PHONY_ICONV
  105. int m_desc;
  106. #else
  107. iconv_t m_desc;
  108. #endif
  109. /* The input string. This is updated as we convert characters. */
  110. const gdb_byte *m_input;
  111. /* The number of bytes remaining in the input. */
  112. size_t m_bytes;
  113. /* The width of an input character. */
  114. size_t m_width;
  115. /* The output buffer. */
  116. gdb::def_vector<gdb_wchar_t> m_out;
  117. };
  118. /* GDB needs to know a few details of its execution character set.
  119. This knowledge is isolated here and in charset.c. */
  120. /* The escape character. */
  121. #define HOST_ESCAPE_CHAR 27
  122. /* Convert a letter, like 'c', to its corresponding control
  123. character. */
  124. char host_letter_to_control_character (char c);
  125. #if WORDS_BIGENDIAN
  126. #define HOST_UTF32 "UTF-32BE"
  127. #else
  128. #define HOST_UTF32 "UTF-32LE"
  129. #endif
  130. #endif /* CHARSET_H */