xref: /dragonfly/contrib/diffutils/lib/xstriconv.h (revision e6d22e9b)
1 /* Charset conversion with out-of-memory checking.
2    Copyright (C) 2001-2004, 2006-2007, 2009-2018 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 _XSTRICONV_H
19 #define _XSTRICONV_H
20 
21 #include <stddef.h>
22 #if HAVE_ICONV
23 #include <iconv.h>
24 #endif
25 
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 
32 #if HAVE_ICONV
33 
34 /* Convert an entire string from one encoding to another, using iconv.
35    The original string is at [SRC,...,SRC+SRCLEN-1].
36    The conversion descriptor is passed as CD.
37    *RESULTP and *LENGTH should initially be a scratch buffer and its size,
38    or *RESULTP can initially be NULL.
39    May erase the contents of the memory at *RESULTP.
40    Upon memory allocation failure, report the error and exit.
41    Return value: 0 if successful, otherwise -1 and errno set.
42    If successful: The resulting string is stored in *RESULTP and its length
43    in *LENGTHP.  *RESULTP is set to a freshly allocated memory block, or is
44    unchanged if no dynamic memory allocation was necessary.  */
45 extern int xmem_cd_iconv (const char *src, size_t srclen, iconv_t cd,
46                           char **resultp, size_t *lengthp);
47 
48 /* Convert an entire string from one encoding to another, using iconv.
49    The original string is the NUL-terminated string starting at SRC.
50    The conversion descriptor is passed as CD.  Both the "from" and the "to"
51    encoding must use a single NUL byte at the end of the string (i.e. not
52    UCS-2, UCS-4, UTF-16, UTF-32).
53    Allocate a malloced memory block for the result.
54    Upon memory allocation failure, report the error and exit.
55    Return value: the freshly allocated resulting NUL-terminated string if
56    successful, otherwise NULL and errno set.  */
57 extern char * xstr_cd_iconv (const char *src, iconv_t cd);
58 
59 #endif
60 
61 /* Convert an entire string from one encoding to another, using iconv.
62    The original string is the NUL-terminated string starting at SRC.
63    Both the "from" and the "to" encoding must use a single NUL byte at the
64    end of the string (i.e. not UCS-2, UCS-4, UTF-16, UTF-32).
65    Allocate a malloced memory block for the result.
66    Upon memory allocation failure, report the error and exit.
67    Return value: the freshly allocated resulting NUL-terminated string if
68    successful, otherwise NULL and errno set.  */
69 extern char * xstr_iconv (const char *src,
70                           const char *from_codeset, const char *to_codeset);
71 
72 
73 #ifdef __cplusplus
74 }
75 #endif
76 
77 
78 #endif /* _XSTRICONV_H */
79