Blame gllib/unistr/u32-to-u8.c

rpm-build 858c0f
/* Convert UTF-32 string to UTF-8 string.
rpm-build 858c0f
   Copyright (C) 2002, 2006-2007, 2009-2017 Free Software Foundation, Inc.
rpm-build 858c0f
   Written by Bruno Haible <bruno@clisp.org>, 2002.
rpm-build 858c0f
rpm-build 858c0f
   This program is free software: you can redistribute it and/or modify it
rpm-build 858c0f
   under the terms of the GNU General Public License as published
rpm-build 858c0f
   by the Free Software Foundation; either version 3 of the License, or
rpm-build 858c0f
   (at your option) any later version.
rpm-build 858c0f
rpm-build 858c0f
   This program is distributed in the hope that it will be useful,
rpm-build 858c0f
   but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 858c0f
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 858c0f
   General Public License for more details.
rpm-build 858c0f
rpm-build 858c0f
   You should have received a copy of the GNU General Public License
rpm-build 858c0f
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
rpm-build 858c0f
rpm-build 858c0f
#include <config.h>
rpm-build 858c0f
rpm-build 858c0f
/* Specification.  */
rpm-build 858c0f
#include "unistr.h"
rpm-build 858c0f
rpm-build 858c0f
#define FUNC u32_to_u8
rpm-build 858c0f
#define SRC_UNIT uint32_t
rpm-build 858c0f
#define DST_UNIT uint8_t
rpm-build 858c0f
rpm-build 858c0f
#include <errno.h>
rpm-build 858c0f
#include <stdlib.h>
rpm-build 858c0f
#include <string.h>
rpm-build 858c0f
rpm-build 858c0f
DST_UNIT *
rpm-build 858c0f
FUNC (const SRC_UNIT *s, size_t n, DST_UNIT *resultbuf, size_t *lengthp)
rpm-build 858c0f
{
rpm-build 858c0f
  const SRC_UNIT *s_end = s + n;
rpm-build 858c0f
  /* Output string accumulator.  */
rpm-build 858c0f
  DST_UNIT *result;
rpm-build 858c0f
  size_t allocated;
rpm-build 858c0f
  size_t length;
rpm-build 858c0f
rpm-build 858c0f
  if (resultbuf != NULL)
rpm-build 858c0f
    {
rpm-build 858c0f
      result = resultbuf;
rpm-build 858c0f
      allocated = *lengthp;
rpm-build 858c0f
    }
rpm-build 858c0f
  else
rpm-build 858c0f
    {
rpm-build 858c0f
      result = NULL;
rpm-build 858c0f
      allocated = 0;
rpm-build 858c0f
    }
rpm-build 858c0f
  length = 0;
rpm-build 858c0f
  /* Invariants:
rpm-build 858c0f
     result is either == resultbuf or == NULL or malloc-allocated.
rpm-build 858c0f
     If length > 0, then result != NULL.  */
rpm-build 858c0f
rpm-build 858c0f
  while (s < s_end)
rpm-build 858c0f
    {
rpm-build 858c0f
      ucs4_t uc;
rpm-build 858c0f
      int count;
rpm-build 858c0f
rpm-build 858c0f
      /* Fetch a Unicode character from the input string.  */
rpm-build 858c0f
      uc = *s++;
rpm-build 858c0f
      /* No need to call the safe variant u32_mbtouc, because
rpm-build 858c0f
         u8_uctomb will verify uc anyway.  */
rpm-build 858c0f
rpm-build 858c0f
      /* Store it in the output string.  */
rpm-build 858c0f
      count = u8_uctomb (result + length, uc, allocated - length);
rpm-build 858c0f
      if (count == -1)
rpm-build 858c0f
        {
rpm-build 858c0f
          if (!(result == resultbuf || result == NULL))
rpm-build 858c0f
            free (result);
rpm-build 858c0f
          errno = EILSEQ;
rpm-build 858c0f
          return NULL;
rpm-build 858c0f
        }
rpm-build 858c0f
      if (count == -2)
rpm-build 858c0f
        {
rpm-build 858c0f
          DST_UNIT *memory;
rpm-build 858c0f
rpm-build 858c0f
          allocated = (allocated > 0 ? 2 * allocated : 12);
rpm-build 858c0f
          if (length + 6 > allocated)
rpm-build 858c0f
            allocated = length + 6;
rpm-build 858c0f
          if (result == resultbuf || result == NULL)
rpm-build 858c0f
            memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
rpm-build 858c0f
          else
rpm-build 858c0f
            memory =
rpm-build 858c0f
              (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
rpm-build 858c0f
rpm-build 858c0f
          if (memory == NULL)
rpm-build 858c0f
            {
rpm-build 858c0f
              if (!(result == resultbuf || result == NULL))
rpm-build 858c0f
                free (result);
rpm-build 858c0f
              errno = ENOMEM;
rpm-build 858c0f
              return NULL;
rpm-build 858c0f
            }
rpm-build 858c0f
          if (result == resultbuf && length > 0)
rpm-build 858c0f
            memcpy ((char *) memory, (char *) result,
rpm-build 858c0f
                    length * sizeof (DST_UNIT));
rpm-build 858c0f
          result = memory;
rpm-build 858c0f
          count = u8_uctomb (result + length, uc, allocated - length);
rpm-build 858c0f
          if (count < 0)
rpm-build 858c0f
            abort ();
rpm-build 858c0f
        }
rpm-build 858c0f
      length += count;
rpm-build 858c0f
    }
rpm-build 858c0f
rpm-build 858c0f
  if (length == 0)
rpm-build 858c0f
    {
rpm-build 858c0f
      if (result == NULL)
rpm-build 858c0f
        {
rpm-build 858c0f
          /* Return a non-NULL value.  NULL means error.  */
rpm-build 858c0f
          result = (DST_UNIT *) malloc (1);
rpm-build 858c0f
          if (result == NULL)
rpm-build 858c0f
            {
rpm-build 858c0f
              errno = ENOMEM;
rpm-build 858c0f
              return NULL;
rpm-build 858c0f
            }
rpm-build 858c0f
        }
rpm-build 858c0f
    }
rpm-build 858c0f
  else if (result != resultbuf && length < allocated)
rpm-build 858c0f
    {
rpm-build 858c0f
      /* Shrink the allocated memory if possible.  */
rpm-build 858c0f
      DST_UNIT *memory;
rpm-build 858c0f
rpm-build 858c0f
      memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
rpm-build 858c0f
      if (memory != NULL)
rpm-build 858c0f
        result = memory;
rpm-build 858c0f
    }
rpm-build 858c0f
rpm-build 858c0f
  *lengthp = length;
rpm-build 858c0f
  return result;
rpm-build 858c0f
}