striconveh.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Character set conversion with error handling.
  2. Copyright (C) 2001-2007, 2009-2017 Free Software Foundation, Inc.
  3. Written by Bruno Haible and Simon Josefsson.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser 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 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 <http://www.gnu.org/licenses/>. */
  14. #ifndef _STRICONVEH_H
  15. #define _STRICONVEH_H
  16. #include <stddef.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. #endif
  85. /* Convert an entire string from one encoding to another, using iconv.
  86. The original string is at [SRC,...,SRC+SRCLEN-1].
  87. If OFFSETS is not NULL, it should point to an array of SRCLEN integers; this
  88. array is filled with offsets into the result, i.e. the character starting
  89. at SRC[i] corresponds to the character starting at (*RESULTP)[OFFSETS[i]],
  90. and other offsets are set to (size_t)(-1).
  91. *RESULTP and *LENGTH should initially be a scratch buffer and its size,
  92. or *RESULTP can initially be NULL.
  93. May erase the contents of the memory at *RESULTP.
  94. Return value: 0 if successful, otherwise -1 and errno set.
  95. If successful: The resulting string is stored in *RESULTP and its length
  96. in *LENGTHP. *RESULTP is set to a freshly allocated memory block, or is
  97. unchanged if no dynamic memory allocation was necessary. */
  98. extern int
  99. mem_iconveh (const char *src, size_t srclen,
  100. const char *from_codeset, const char *to_codeset,
  101. enum iconv_ilseq_handler handler,
  102. size_t *offsets,
  103. char **resultp, size_t *lengthp);
  104. /* Convert an entire string from one encoding to another, using iconv.
  105. The original string is the NUL-terminated string starting at SRC.
  106. Both the "from" and the "to" encoding must use a single NUL byte at the
  107. end of the string (i.e. not UCS-2, UCS-4, UTF-16, UTF-32).
  108. Allocate a malloced memory block for the result.
  109. Return value: the freshly allocated resulting NUL-terminated string if
  110. successful, otherwise NULL and errno set. */
  111. extern char *
  112. str_iconveh (const char *src,
  113. const char *from_codeset, const char *to_codeset,
  114. enum iconv_ilseq_handler handler);
  115. #ifdef __cplusplus
  116. }
  117. #endif
  118. #endif /* _STRICONVEH_H */