xref: /dflybsd-src/contrib/grep/lib/xstriconv.h (revision 91b9ed38d3db6a8a8ac5b66da1d43e6e331e259a)
195b7b453SJohn Marino /* Charset conversion with out-of-memory checking.
2*09d4459fSDaniel Fojt    Copyright (C) 2001-2004, 2006-2007, 2009-2020 Free Software Foundation, Inc.
395b7b453SJohn Marino    Written by Bruno Haible and Simon Josefsson.
495b7b453SJohn Marino 
595b7b453SJohn Marino    This program is free software: you can redistribute it and/or modify
695b7b453SJohn Marino    it under the terms of the GNU General Public License as published by
795b7b453SJohn Marino    the Free Software Foundation; either version 3 of the License, or
895b7b453SJohn Marino    (at your option) any later version.
995b7b453SJohn Marino 
1095b7b453SJohn Marino    This program is distributed in the hope that it will be useful,
1195b7b453SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
1295b7b453SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1395b7b453SJohn Marino    GNU General Public License for more details.
1495b7b453SJohn Marino 
1595b7b453SJohn Marino    You should have received a copy of the GNU General Public License
16*09d4459fSDaniel Fojt    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
1795b7b453SJohn Marino 
1895b7b453SJohn Marino #ifndef _XSTRICONV_H
1995b7b453SJohn Marino #define _XSTRICONV_H
2095b7b453SJohn Marino 
2195b7b453SJohn Marino #include <stddef.h>
2295b7b453SJohn Marino #if HAVE_ICONV
2395b7b453SJohn Marino #include <iconv.h>
2495b7b453SJohn Marino #endif
2595b7b453SJohn Marino 
2695b7b453SJohn Marino 
2795b7b453SJohn Marino #ifdef __cplusplus
2895b7b453SJohn Marino extern "C" {
2995b7b453SJohn Marino #endif
3095b7b453SJohn Marino 
3195b7b453SJohn Marino 
3295b7b453SJohn Marino #if HAVE_ICONV
3395b7b453SJohn Marino 
3495b7b453SJohn Marino /* Convert an entire string from one encoding to another, using iconv.
3595b7b453SJohn Marino    The original string is at [SRC,...,SRC+SRCLEN-1].
3695b7b453SJohn Marino    The conversion descriptor is passed as CD.
3795b7b453SJohn Marino    *RESULTP and *LENGTH should initially be a scratch buffer and its size,
3895b7b453SJohn Marino    or *RESULTP can initially be NULL.
3995b7b453SJohn Marino    May erase the contents of the memory at *RESULTP.
4095b7b453SJohn Marino    Upon memory allocation failure, report the error and exit.
4195b7b453SJohn Marino    Return value: 0 if successful, otherwise -1 and errno set.
4295b7b453SJohn Marino    If successful: The resulting string is stored in *RESULTP and its length
4395b7b453SJohn Marino    in *LENGTHP.  *RESULTP is set to a freshly allocated memory block, or is
4495b7b453SJohn Marino    unchanged if no dynamic memory allocation was necessary.  */
4595b7b453SJohn Marino extern int xmem_cd_iconv (const char *src, size_t srclen, iconv_t cd,
4695b7b453SJohn Marino                           char **resultp, size_t *lengthp);
4795b7b453SJohn Marino 
4895b7b453SJohn Marino /* Convert an entire string from one encoding to another, using iconv.
4995b7b453SJohn Marino    The original string is the NUL-terminated string starting at SRC.
5095b7b453SJohn Marino    The conversion descriptor is passed as CD.  Both the "from" and the "to"
5195b7b453SJohn Marino    encoding must use a single NUL byte at the end of the string (i.e. not
5295b7b453SJohn Marino    UCS-2, UCS-4, UTF-16, UTF-32).
5395b7b453SJohn Marino    Allocate a malloced memory block for the result.
5495b7b453SJohn Marino    Upon memory allocation failure, report the error and exit.
5595b7b453SJohn Marino    Return value: the freshly allocated resulting NUL-terminated string if
5695b7b453SJohn Marino    successful, otherwise NULL and errno set.  */
5795b7b453SJohn Marino extern char * xstr_cd_iconv (const char *src, iconv_t cd);
5895b7b453SJohn Marino 
5995b7b453SJohn Marino #endif
6095b7b453SJohn Marino 
6195b7b453SJohn Marino /* Convert an entire string from one encoding to another, using iconv.
6295b7b453SJohn Marino    The original string is the NUL-terminated string starting at SRC.
6395b7b453SJohn Marino    Both the "from" and the "to" encoding must use a single NUL byte at the
6495b7b453SJohn Marino    end of the string (i.e. not UCS-2, UCS-4, UTF-16, UTF-32).
6595b7b453SJohn Marino    Allocate a malloced memory block for the result.
6695b7b453SJohn Marino    Upon memory allocation failure, report the error and exit.
6795b7b453SJohn Marino    Return value: the freshly allocated resulting NUL-terminated string if
6895b7b453SJohn Marino    successful, otherwise NULL and errno set.  */
6995b7b453SJohn Marino extern char * xstr_iconv (const char *src,
7095b7b453SJohn Marino                           const char *from_codeset, const char *to_codeset);
7195b7b453SJohn Marino 
7295b7b453SJohn Marino 
7395b7b453SJohn Marino #ifdef __cplusplus
7495b7b453SJohn Marino }
7595b7b453SJohn Marino #endif
7695b7b453SJohn Marino 
7795b7b453SJohn Marino 
7895b7b453SJohn Marino #endif /* _XSTRICONV_H */
79