root/maint/gnulib/lib/unistr/u32-to-u16.c

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

DEFINITIONS

This source file includes following definitions.
  1. FUNC

   1 /* Convert UTF-32 string to UTF-16 string.
   2    Copyright (C) 2002, 2006-2007, 2009-2021 Free Software Foundation, Inc.
   3    Written by Bruno Haible <bruno@clisp.org>, 2002.
   4 
   5    This file is free software.
   6    It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
   7    You can redistribute it and/or modify it under either
   8      - the terms of the GNU Lesser General Public License as published
   9        by the Free Software Foundation; either version 3, or (at your
  10        option) any later version, or
  11      - the terms of the GNU General Public License as published by the
  12        Free Software Foundation; either version 2, or (at your option)
  13        any later version, or
  14      - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
  15 
  16    This file is distributed in the hope that it will be useful,
  17    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19    Lesser General Public License and the GNU General Public License
  20    for more details.
  21 
  22    You should have received a copy of the GNU Lesser General Public
  23    License and of the GNU General Public License along with this
  24    program.  If not, see <https://www.gnu.org/licenses/>.  */
  25 
  26 #include <config.h>
  27 
  28 /* Specification.  */
  29 #include "unistr.h"
  30 
  31 #define FUNC u32_to_u16
  32 #define SRC_UNIT uint32_t
  33 #define DST_UNIT uint16_t
  34 
  35 #include <errno.h>
  36 #include <stdlib.h>
  37 #include <string.h>
  38 
  39 DST_UNIT *
  40 FUNC (const SRC_UNIT *s, size_t n, DST_UNIT *resultbuf, size_t *lengthp)
     /* [previous][next][first][last][top][bottom][index][help] */
  41 {
  42   const SRC_UNIT *s_end = s + n;
  43   /* Output string accumulator.  */
  44   DST_UNIT *result;
  45   size_t allocated;
  46   size_t length;
  47 
  48   if (resultbuf != NULL)
  49     {
  50       result = resultbuf;
  51       allocated = *lengthp;
  52     }
  53   else
  54     {
  55       result = NULL;
  56       allocated = 0;
  57     }
  58   length = 0;
  59   /* Invariants:
  60      result is either == resultbuf or == NULL or malloc-allocated.
  61      If length > 0, then result != NULL.  */
  62 
  63   while (s < s_end)
  64     {
  65       ucs4_t uc;
  66       int count;
  67 
  68       /* Fetch a Unicode character from the input string.  */
  69       uc = *s++;
  70       /* No need to call the safe variant u32_mbtouc, because
  71          u16_uctomb will verify uc anyway.  */
  72 
  73       /* Store it in the output string.  */
  74       count = u16_uctomb (result + length, uc, allocated - length);
  75       if (count == -1)
  76         {
  77           if (!(result == resultbuf || result == NULL))
  78             free (result);
  79           errno = EILSEQ;
  80           return NULL;
  81         }
  82       if (count == -2)
  83         {
  84           DST_UNIT *memory;
  85 
  86           allocated = (allocated > 0 ? 2 * allocated : 12);
  87           if (length + 2 > allocated)
  88             allocated = length + 2;
  89           if (result == resultbuf || result == NULL)
  90             memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
  91           else
  92             memory =
  93               (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
  94 
  95           if (memory == NULL)
  96             {
  97               if (!(result == resultbuf || result == NULL))
  98                 free (result);
  99               errno = ENOMEM;
 100               return NULL;
 101             }
 102           if (result == resultbuf && length > 0)
 103             memcpy ((char *) memory, (char *) result,
 104                     length * sizeof (DST_UNIT));
 105           result = memory;
 106           count = u16_uctomb (result + length, uc, allocated - length);
 107           if (count < 0)
 108             abort ();
 109         }
 110       length += count;
 111     }
 112 
 113   if (length == 0)
 114     {
 115       if (result == NULL)
 116         {
 117           /* Return a non-NULL value.  NULL means error.  */
 118           result = (DST_UNIT *) malloc (1);
 119           if (result == NULL)
 120             {
 121               errno = ENOMEM;
 122               return NULL;
 123             }
 124         }
 125     }
 126   else if (result != resultbuf && length < allocated)
 127     {
 128       /* Shrink the allocated memory if possible.  */
 129       DST_UNIT *memory;
 130 
 131       memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
 132       if (memory != NULL)
 133         result = memory;
 134     }
 135 
 136   *lengthp = length;
 137   return result;
 138 }

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