Simplify calling convention of u*_conv_from_encoding.
[pspp] / lib / uniconv.h
1 /* Conversions between Unicode and legacy encodings.
2    Copyright (C) 2002, 2005, 2007, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify it
5    under the terms of the GNU Lesser General Public License as published
6    by the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #ifndef _UNICONV_H
18 #define _UNICONV_H
19
20 /* Get size_t.  */
21 #include <stddef.h>
22
23 #include "unitypes.h"
24
25 /* Get enum iconv_ilseq_handler.  */
26 #include "iconveh.h"
27
28 /* Get uniconv_register_autodetect() declaration.  */
29 #include "striconveha.h"
30
31 /* Get locale_charset() declaration.  */
32 #include "localcharset.h"
33
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39
40 /* Converts an entire string, possibly including NUL bytes, from one encoding
41    to a Unicode encoding.
42    Converts a memory region given in encoding FROMCODE.  FROMCODE is as for
43    iconv_open(3).
44    The input is in the memory region between SRC (inclusive) and SRC + SRCLEN
45    (exclusive).
46    If OFFSETS is not NULL, it should point to an array of SRCLEN integers; this
47    array is filled with offsets into the result, i.e. the character starting
48    at SRC[i] corresponds to the character starting at (*RESULTP)[OFFSETS[i]],
49    and other offsets are set to (size_t)(-1).
50    *RESULTP and *LENGTHP should initially be a scratch buffer and its size,
51    or *RESULTP can initially be NULL.
52    May erase the contents of the memory at *RESULTP.
53    Return value: 0 if successful, otherwise -1 and errno set.
54    If successful: The resulting Unicode string (non-NULL) is returned and its
55    length stored in *LENGTHP.  The resulting string is RESULTBUF if no dynamic
56    memory allocation was necessary, or a freshly allocated memory block
57    otherwise.
58    In case of error: NULL is returned and errno is set.  Particular errno
59    values: EINVAL, EILSEQ, ENOMEM.  */
60 extern uint8_t *
61        u8_conv_from_encoding (const char *fromcode,
62                               enum iconv_ilseq_handler handler,
63                               const char *src, size_t srclen,
64                               size_t *offsets,
65                               uint8_t *resultbuf, size_t *lengthp);
66 extern uint16_t *
67        u16_conv_from_encoding (const char *fromcode,
68                                enum iconv_ilseq_handler handler,
69                                const char *src, size_t srclen,
70                                size_t *offsets,
71                                uint16_t *resultbuf, size_t *lengthp);
72 extern uint32_t *
73        u32_conv_from_encoding (const char *fromcode,
74                                enum iconv_ilseq_handler handler,
75                                const char *src, size_t srclen,
76                                size_t *offsets,
77                                uint32_t *resultbuf, size_t *lengthp);
78
79 /* Converts an entire Unicode string, possibly including NUL units, from a
80    Unicode encoding to a given encoding.
81    Converts a memory region to encoding TOCODE.  TOCODE is as for
82    iconv_open(3).
83    The input is in the memory region between SRC (inclusive) and SRC + SRCLEN
84    (exclusive).
85    If OFFSETS is not NULL, it should point to an array of SRCLEN integers; this
86    array is filled with offsets into the result, i.e. the character starting
87    at SRC[i] corresponds to the character starting at (*RESULTP)[OFFSETS[i]],
88    and other offsets are set to (size_t)(-1).
89    RESULTBUF and *LENGTHP should initially be a scratch buffer and its size,
90    or RESULTBUF can be NULL.
91    May erase the contents of the memory at *RESULTP.
92    If successful: The resulting string (non-NULL) is returned and its length
93    stored in *LENGTHP.  The resulting string is RESULTBUF if no dynamic memory
94    allocation was necessary, or a freshly allocated memory block otherwise.
95    In case of error: NULL is returned and errno is set.  Particular errno
96    values: EINVAL, EILSEQ, ENOMEM.  */
97 extern char *
98        u8_conv_to_encoding (const char *tocode,
99                             enum iconv_ilseq_handler handler,
100                             const uint8_t *src, size_t srclen,
101                             size_t *offsets,
102                             char *resultbuf, size_t *lengthp);
103 extern char *
104        u16_conv_to_encoding (const char *tocode,
105                              enum iconv_ilseq_handler handler,
106                              const uint16_t *src, size_t srclen,
107                              size_t *offsets,
108                              char *resultbuf, size_t *lengthp);
109 extern char *
110        u32_conv_to_encoding (const char *tocode,
111                              enum iconv_ilseq_handler handler,
112                              const uint32_t *src, size_t srclen,
113                              size_t *offsets,
114                              char *resultbuf, size_t *lengthp);
115
116 /* Converts a NUL terminated string from a given encoding.
117    The result is malloc allocated, or NULL (with errno set) in case of error.
118    Particular errno values: EILSEQ, ENOMEM.  */
119 extern uint8_t *
120        u8_strconv_from_encoding (const char *string,
121                                  const char *fromcode,
122                                  enum iconv_ilseq_handler handler);
123 extern uint16_t *
124        u16_strconv_from_encoding (const char *string,
125                                   const char *fromcode,
126                                   enum iconv_ilseq_handler handler);
127 extern uint32_t *
128        u32_strconv_from_encoding (const char *string,
129                                   const char *fromcode,
130                                   enum iconv_ilseq_handler handler);
131
132 /* Converts a NUL terminated string to a given encoding.
133    The result is malloc allocated, or NULL (with errno set) in case of error.
134    Particular errno values: EILSEQ, ENOMEM.  */
135 extern char *
136        u8_strconv_to_encoding (const uint8_t *string,
137                                const char *tocode,
138                                enum iconv_ilseq_handler handler);
139 extern char *
140        u16_strconv_to_encoding (const uint16_t *string,
141                                 const char *tocode,
142                                 enum iconv_ilseq_handler handler);
143 extern char *
144        u32_strconv_to_encoding (const uint32_t *string,
145                                 const char *tocode,
146                                 enum iconv_ilseq_handler handler);
147
148 /* Converts a NUL terminated string from the locale encoding.
149    The result is malloc allocated, or NULL (with errno set) in case of error.
150    Particular errno values: ENOMEM.  */
151 extern uint8_t *
152        u8_strconv_from_locale (const char *string);
153 extern uint16_t *
154        u16_strconv_from_locale (const char *string);
155 extern uint32_t *
156        u32_strconv_from_locale (const char *string);
157
158 /* Converts a NUL terminated string to the locale encoding.
159    The result is malloc allocated, or NULL (with errno set) in case of error.
160    Particular errno values: ENOMEM.  */
161 extern char *
162        u8_strconv_to_locale (const uint8_t *string);
163 extern char *
164        u16_strconv_to_locale (const uint16_t *string);
165 extern char *
166        u32_strconv_to_locale (const uint32_t *string);
167
168
169 #ifdef __cplusplus
170 }
171 #endif
172
173 #endif /* _UNICONV_H */