xref: /dflybsd-src/contrib/grep/lib/xstriconv.c (revision 91b9ed38d3db6a8a8ac5b66da1d43e6e331e259a)
195b7b453SJohn Marino /* Charset conversion with out-of-memory checking.
2*09d4459fSDaniel Fojt    Copyright (C) 2001-2004, 2006, 2009-2020 Free Software Foundation, Inc.
395b7b453SJohn Marino    Written by Bruno Haible.
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 #include <config.h>
1995b7b453SJohn Marino 
2095b7b453SJohn Marino /* Specification.  */
2195b7b453SJohn Marino #include "xstriconv.h"
2295b7b453SJohn Marino 
2395b7b453SJohn Marino #include <errno.h>
2495b7b453SJohn Marino 
2595b7b453SJohn Marino #include "striconv.h"
2695b7b453SJohn Marino #include "xalloc.h"
2795b7b453SJohn Marino 
2895b7b453SJohn Marino 
2995b7b453SJohn Marino #if HAVE_ICONV
3095b7b453SJohn Marino 
3195b7b453SJohn Marino int
xmem_cd_iconv(const char * src,size_t srclen,iconv_t cd,char ** resultp,size_t * lengthp)3295b7b453SJohn Marino xmem_cd_iconv (const char *src, size_t srclen, iconv_t cd,
3395b7b453SJohn Marino                char **resultp, size_t *lengthp)
3495b7b453SJohn Marino {
3595b7b453SJohn Marino   int retval = mem_cd_iconv (src, srclen, cd, resultp, lengthp);
3695b7b453SJohn Marino 
3795b7b453SJohn Marino   if (retval < 0 && errno == ENOMEM)
3895b7b453SJohn Marino     xalloc_die ();
3995b7b453SJohn Marino   return retval;
4095b7b453SJohn Marino }
4195b7b453SJohn Marino 
4295b7b453SJohn Marino char *
xstr_cd_iconv(const char * src,iconv_t cd)4395b7b453SJohn Marino xstr_cd_iconv (const char *src, iconv_t cd)
4495b7b453SJohn Marino {
4595b7b453SJohn Marino   char *result = str_cd_iconv (src, cd);
4695b7b453SJohn Marino 
4795b7b453SJohn Marino   if (result == NULL && errno == ENOMEM)
4895b7b453SJohn Marino     xalloc_die ();
4995b7b453SJohn Marino   return result;
5095b7b453SJohn Marino }
5195b7b453SJohn Marino 
5295b7b453SJohn Marino #endif
5395b7b453SJohn Marino 
5495b7b453SJohn Marino char *
xstr_iconv(const char * src,const char * from_codeset,const char * to_codeset)5595b7b453SJohn Marino xstr_iconv (const char *src, const char *from_codeset, const char *to_codeset)
5695b7b453SJohn Marino {
5795b7b453SJohn Marino   char *result = str_iconv (src, from_codeset, to_codeset);
5895b7b453SJohn Marino 
5995b7b453SJohn Marino   if (result == NULL && errno == ENOMEM)
6095b7b453SJohn Marino     xalloc_die ();
6195b7b453SJohn Marino   return result;
6295b7b453SJohn Marino }
63