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