Various fixes.
[gnulib.git] / lib / unistr / u16-to-u8.c
1 /* Convert UTF-16 string to UTF-8 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 u16_to_u8
26 #define SRC_UNIT uint16_t
27 #define DST_UNIT uint8_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       count = u16_mbtouc_safe (&uc, s, s_end - s);
64       if (count < 0)
65         {
66           if (!(result == resultbuf || result == NULL))
67             free (result);
68           errno = EILSEQ;
69           return NULL;
70         }
71       s += count;
72
73       /* Store it in the output string.  */
74       count = u8_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 + 6 > allocated)
88             allocated = length + 6;
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 = u8_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 }