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