* README: Document signed integer overflow situation more
[gnulib.git] / lib / xstriconv.h
1 /* Charset conversion with out-of-memory checking.
2    Copyright (C) 2001-2004, 2006-2007 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 2, or (at your option)
8    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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #ifndef _XSTRICONV_H
20 #define _XSTRICONV_H
21
22 #include <stddef.h>
23 #if HAVE_ICONV
24 #include <iconv.h>
25 #endif
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    The conversion descriptor is passed as CD.
38    *RESULTP and *LENGTH should initially be a scratch buffer and its size,
39    or *RESULTP can initially be NULL.
40    May erase the contents of the memory at *RESULTP.
41    Upon memory allocation failure, report the error and exit.
42    Return value: 0 if successful, otherwise -1 and errno set.
43    If successful: The resulting string is stored in *RESULTP and its length
44    in *LENGTHP.  *RESULTP is set to a freshly allocated memory block, or is
45    unchanged if no dynamic memory allocation was necessary.  */
46 extern int xmem_cd_iconv (const char *src, size_t srclen, iconv_t cd,
47                           char **resultp, size_t *lengthp);
48
49 /* Convert an entire string from one encoding to another, using iconv.
50    The original string is the NUL-terminated string starting at SRC.
51    The conversion descriptor is passed as CD.  Both the "from" and the "to"
52    encoding must use a single NUL byte at the end of the string (i.e. not
53    UCS-2, UCS-4, UTF-16, UTF-32).
54    Allocate a malloced memory block for the result.
55    Upon memory allocation failure, report the error and exit.
56    Return value: the freshly allocated resulting NUL-terminated string if
57    successful, otherwise NULL and errno set.  */
58 extern char * xstr_cd_iconv (const char *src, iconv_t cd);
59
60 #endif
61
62 /* Convert an entire string from one encoding to another, using iconv.
63    The original string is the NUL-terminated string starting at SRC.
64    Both the "from" and the "to" encoding must use a single NUL byte at the
65    end of the string (i.e. not UCS-2, UCS-4, UTF-16, UTF-32).
66    Allocate a malloced memory block for the result.
67    Upon memory allocation failure, report the error and exit.
68    Return value: the freshly allocated resulting NUL-terminated string if
69    successful, otherwise NULL and errno set.  */
70 extern char * xstr_iconv (const char *src,
71                           const char *from_codeset, const char *to_codeset);
72
73
74 #ifdef __cplusplus
75 }
76 #endif
77
78
79 #endif /* _XSTRICONV_H */