striconveh.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Character set conversion with error handling.
  2. Copyright (C) 2001-2007, 2009-2023 Free Software Foundation, Inc.
  3. Written by Bruno Haible and Simon Josefsson.
  4. This file is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. This file 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 Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #ifndef _STRICONVEH_H
  15. #define _STRICONVEH_H
  16. #include <stdlib.h>
  17. #if HAVE_ICONV
  18. #include <iconv.h>
  19. #endif
  20. #include "iconveh.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #if HAVE_ICONV
  25. /* A conversion descriptor for use by the iconveh functions. */
  26. typedef struct
  27. {
  28. /* Conversion descriptor from FROM_CODESET to TO_CODESET, or (iconv_t)(-1)
  29. if the system does not support a direct conversion from FROM_CODESET to
  30. TO_CODESET. */
  31. iconv_t cd;
  32. /* Conversion descriptor from FROM_CODESET to UTF-8 (or (iconv_t)(-1) if
  33. FROM_CODESET is UTF-8). */
  34. iconv_t cd1;
  35. /* Conversion descriptor from UTF-8 to TO_CODESET (or (iconv_t)(-1) if
  36. TO_CODESET is UTF-8). */
  37. iconv_t cd2;
  38. }
  39. iconveh_t;
  40. /* Open a conversion descriptor for use by the iconveh functions.
  41. If successful, fills *CDP and returns 0. Upon failure, return -1 with errno
  42. set. */
  43. extern int
  44. iconveh_open (const char *to_codeset, const char *from_codeset,
  45. iconveh_t *cdp);
  46. /* Close a conversion descriptor created by iconveh_open().
  47. Return value: 0 if successful, otherwise -1 and errno set. */
  48. extern int
  49. iconveh_close (const iconveh_t *cd);
  50. /* Convert an entire string from one encoding to another, using iconv.
  51. The original string is at [SRC,...,SRC+SRCLEN-1].
  52. CD points to the conversion descriptor from FROMCODE to TOCODE, created by
  53. the function iconveh_open().
  54. If OFFSETS is not NULL, it should point to an array of SRCLEN integers; this
  55. array is filled with offsets into the result, i.e. the character starting
  56. at SRC[i] corresponds to the character starting at (*RESULTP)[OFFSETS[i]],
  57. and other offsets are set to (size_t)(-1).
  58. *RESULTP and *LENGTH should initially be a scratch buffer and its size,
  59. or *RESULTP can initially be NULL.
  60. May erase the contents of the memory at *RESULTP.
  61. Return value: 0 if successful, otherwise -1 and errno set.
  62. If successful: The resulting string is stored in *RESULTP and its length
  63. in *LENGTHP. *RESULTP is set to a freshly allocated memory block, or is
  64. unchanged if no dynamic memory allocation was necessary. */
  65. extern int
  66. mem_cd_iconveh (const char *src, size_t srclen,
  67. const iconveh_t *cd,
  68. enum iconv_ilseq_handler handler,
  69. size_t *offsets,
  70. char **resultp, size_t *lengthp);
  71. /* Convert an entire string from one encoding to another, using iconv.
  72. The original string is the NUL-terminated string starting at SRC.
  73. CD points to the conversion descriptor from FROMCODE to TOCODE, created by
  74. the function iconveh_open().
  75. Both the "from" and the "to" encoding must use a single NUL byte at the end
  76. of the string (i.e. not UCS-2, UCS-4, UTF-16, UTF-32).
  77. Allocate a malloced memory block for the result.
  78. Return value: the freshly allocated resulting NUL-terminated string if
  79. successful, otherwise NULL and errno set. */
  80. extern char *
  81. str_cd_iconveh (const char *src,
  82. const iconveh_t *cd,
  83. enum iconv_ilseq_handler handler)
  84. _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
  85. #endif
  86. /* Convert an entire string from one encoding to another, using iconv.
  87. The original string is at [SRC,...,SRC+SRCLEN-1].
  88. If OFFSETS is not NULL, it should point to an array of SRCLEN integers; this
  89. array is filled with offsets into the result, i.e. the character starting
  90. at SRC[i] corresponds to the character starting at (*RESULTP)[OFFSETS[i]],
  91. and other offsets are set to (size_t)(-1).
  92. *RESULTP and *LENGTH should initially be a scratch buffer and its size,
  93. or *RESULTP can initially be NULL.
  94. May erase the contents of the memory at *RESULTP.
  95. Return value: 0 if successful, otherwise -1 and errno set.
  96. If successful: The resulting string is stored in *RESULTP and its length
  97. in *LENGTHP. *RESULTP is set to a freshly allocated memory block, or is
  98. unchanged if no dynamic memory allocation was necessary. */
  99. extern int
  100. mem_iconveh (const char *src, size_t srclen,
  101. const char *from_codeset, const char *to_codeset,
  102. enum iconv_ilseq_handler handler,
  103. size_t *offsets,
  104. char **resultp, size_t *lengthp);
  105. /* Convert an entire string from one encoding to another, using iconv.
  106. The original string is the NUL-terminated string starting at SRC.
  107. Both the "from" and the "to" encoding must use a single NUL byte at the
  108. end of the string (i.e. not UCS-2, UCS-4, UTF-16, UTF-32).
  109. Allocate a malloced memory block for the result.
  110. Return value: the freshly allocated resulting NUL-terminated string if
  111. successful, otherwise NULL and errno set. */
  112. extern char *
  113. str_iconveh (const char *src,
  114. const char *from_codeset, const char *to_codeset,
  115. enum iconv_ilseq_handler handler)
  116. _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
  117. #ifdef __cplusplus
  118. }
  119. #endif
  120. #endif /* _STRICONVEH_H */