root/maint/gnulib/lib/uniconv/u8-conv-to-enc.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. u8_conv_to_encoding

   1 /* Conversion from UTF-8 to legacy encodings.
   2    Copyright (C) 2002, 2006-2007, 2009-2021 Free Software Foundation, Inc.
   3 
   4    This file is free software.
   5    It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
   6    You can redistribute it and/or modify it under either
   7      - the terms of the GNU Lesser General Public License as published
   8        by the Free Software Foundation; either version 3, or (at your
   9        option) any later version, or
  10      - the terms of the GNU General Public License as published by the
  11        Free Software Foundation; either version 2, or (at your option)
  12        any later version, or
  13      - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
  14 
  15    This file is distributed in the hope that it will be useful,
  16    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18    Lesser General Public License and the GNU General Public License
  19    for more details.
  20 
  21    You should have received a copy of the GNU Lesser General Public
  22    License and of the GNU General Public License along with this
  23    program.  If not, see <https://www.gnu.org/licenses/>.  */
  24 
  25 /* Written by Bruno Haible <bruno@clisp.org>.  */
  26 
  27 #include <config.h>
  28 
  29 /* Specification.  */
  30 #include "uniconv.h"
  31 
  32 #include <errno.h>
  33 #include <stdlib.h>
  34 #include <string.h>
  35 
  36 #include "c-strcaseeq.h"
  37 #include "striconveha.h"
  38 #include "unistr.h"
  39 
  40 char *
  41 u8_conv_to_encoding (const char *tocode,
     /* [previous][next][first][last][top][bottom][index][help] */
  42                      enum iconv_ilseq_handler handler,
  43                      const uint8_t *src, size_t srclen,
  44                      size_t *offsets,
  45                      char *resultbuf, size_t *lengthp)
  46 {
  47   if (STRCASEEQ (tocode, "UTF-8", 'U','T','F','-','8',0,0,0,0))
  48     {
  49       char *result;
  50 
  51       /* Conversion from UTF-8 to UTF-8.  No need to go through iconv().  */
  52       if (u8_check (src, srclen))
  53         {
  54           errno = EILSEQ;
  55           return NULL;
  56         }
  57 
  58       /* Memory allocation.  */
  59       if (resultbuf != NULL && *lengthp >= srclen)
  60         result = resultbuf;
  61       else
  62         {
  63           result = (char *) malloc (srclen > 0 ? srclen : 1);
  64           if (result == NULL)
  65             {
  66               errno = ENOMEM;
  67               return NULL;
  68             }
  69         }
  70 
  71       if (srclen > 0)
  72         memcpy (result, (const char *) src, srclen);
  73       *lengthp = srclen;
  74       return result;
  75     }
  76   else
  77     {
  78       char *result = resultbuf;
  79       size_t length = *lengthp;
  80 
  81       if (mem_iconveha ((const char *) src, srclen,
  82                         "UTF-8", tocode,
  83                         handler == iconveh_question_mark, handler,
  84                         offsets, &result, &length) < 0)
  85         return NULL;
  86 
  87       if (result == NULL) /* when (resultbuf == NULL && length == 0)  */
  88         {
  89           result = (char *) malloc (1);
  90           if (result == NULL)
  91             {
  92               errno = ENOMEM;
  93               return NULL;
  94             }
  95         }
  96       *lengthp = length;
  97       return result;
  98     }
  99 }

/* [previous][next][first][last][top][bottom][index][help] */