Change copyright notice from LGPLv2.0+ to LGPLv3+.
[gnulib.git] / lib / uniconv.h
1 /* Conversions between Unicode and legacy encodings.
2    Copyright (C) 2002, 2005, 2007 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 "striconveh.h"
27
28 /* Get iconv_register_autodetect(), mem_iconveha() 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 *LENGTH 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 string is stored in *RESULTP and its length
55    in *LENGTHP.  *RESULTP is set to a freshly allocated memory block, or is
56    unchanged if no dynamic memory allocation was necessary.
57    Particular errno values: EINVAL, EILSEQ, ENOMEM.  */
58 extern int
59        u8_conv_from_encoding (const char *fromcode,
60                               enum iconv_ilseq_handler handler,
61                               const char *src, size_t srclen,
62                               size_t *offsets,
63                               uint8_t **resultp, size_t *lengthp);
64 extern int
65        u16_conv_from_encoding (const char *fromcode,
66                                enum iconv_ilseq_handler handler,
67                                const char *src, size_t srclen,
68                                size_t *offsets,
69                                uint16_t **resultp, size_t *lengthp);
70 extern int
71        u32_conv_from_encoding (const char *fromcode,
72                                enum iconv_ilseq_handler handler,
73                                const char *src, size_t srclen,
74                                size_t *offsets,
75                                uint32_t **resultp, size_t *lengthp);
76
77 /* Converts an entire Unicode string, possibly including NUL units, from a
78    Unicode encoding to a given encoding.
79    Converts a memory region to encoding TOCODE.  TOCODE is as for
80    iconv_open(3).
81    The input is in the memory region between SRC (inclusive) and SRC + SRCLEN
82    (exclusive).
83    If OFFSETS is not NULL, it should point to an array of SRCLEN integers; this
84    array is filled with offsets into the result, i.e. the character starting
85    at SRC[i] corresponds to the character starting at (*RESULTP)[OFFSETS[i]],
86    and other offsets are set to (size_t)(-1).
87    *RESULTP and *LENGTH should initially be a scratch buffer and its size,
88    or *RESULTP can initially be NULL.
89    May erase the contents of the memory at *RESULTP.
90    Return value: 0 if successful, otherwise -1 and errno set.
91    If successful: The resulting string is stored in *RESULTP and its length
92    in *LENGTHP.  *RESULTP is set to a freshly allocated memory block, or is
93    unchanged if no dynamic memory allocation was necessary.
94    Particular errno values: EINVAL, EILSEQ, ENOMEM.  */
95 extern int
96        u8_conv_to_encoding (const char *tocode,
97                             enum iconv_ilseq_handler handler,
98                             const uint8_t *src, size_t srclen,
99                             size_t *offsets,
100                             char **resultp, size_t *lengthp);
101 extern int
102        u16_conv_to_encoding (const char *tocode,
103                              enum iconv_ilseq_handler handler,
104                              const uint16_t *src, size_t srclen,
105                              size_t *offsets,
106                              char **resultp, size_t *lengthp);
107 extern int
108        u32_conv_to_encoding (const char *tocode,
109                              enum iconv_ilseq_handler handler,
110                              const uint32_t *src, size_t srclen,
111                              size_t *offsets,
112                              char **resultp, size_t *lengthp);
113
114 /* Converts a NUL terminated string from a given encoding.
115    The result is malloc allocated, or NULL (with errno set) in case of error.
116    Particular errno values: EILSEQ, ENOMEM.  */
117 extern uint8_t *
118        u8_strconv_from_encoding (const char *string,
119                                  const char *fromcode,
120                                  enum iconv_ilseq_handler handler);
121 extern uint16_t *
122        u16_strconv_from_encoding (const char *string,
123                                   const char *fromcode,
124                                   enum iconv_ilseq_handler handler);
125 extern uint32_t *
126        u32_strconv_from_encoding (const char *string,
127                                   const char *fromcode,
128                                   enum iconv_ilseq_handler handler);
129
130 /* Converts a NUL terminated string to a given encoding.
131    The result is malloc allocated, or NULL (with errno set) in case of error.
132    Particular errno values: EILSEQ, ENOMEM.  */
133 extern char *
134        u8_strconv_to_encoding (const uint8_t *string,
135                                const char *tocode,
136                                enum iconv_ilseq_handler handler);
137 extern char *
138        u16_strconv_to_encoding (const uint16_t *string,
139                                 const char *tocode,
140                                 enum iconv_ilseq_handler handler);
141 extern char *
142        u32_strconv_to_encoding (const uint32_t *string,
143                                 const char *tocode,
144                                 enum iconv_ilseq_handler handler);
145
146 /* Converts a NUL terminated string from the locale encoding.
147    The result is malloc allocated, or NULL (with errno set) in case of error.
148    Particular errno values: ENOMEM.  */
149 extern uint8_t *
150        u8_strconv_from_locale (const char *string);
151 extern uint16_t *
152        u16_strconv_from_locale (const char *string);
153 extern uint32_t *
154        u32_strconv_from_locale (const char *string);
155
156 /* Converts a NUL terminated string to the locale encoding.
157    The result is malloc allocated, or NULL (with errno set) in case of error.
158    Particular errno values: ENOMEM.  */
159 extern char *
160        u8_strconv_to_locale (const uint8_t *string);
161 extern char *
162        u16_strconv_to_locale (const uint16_t *string);
163 extern char *
164        u32_strconv_to_locale (const uint32_t *string);
165
166
167 #ifdef __cplusplus
168 }
169 #endif
170
171 #endif /* _UNICONV_H */