New module 'unistr/u16-to-u32'.
[gnulib.git] / lib / unistr / u16-to-u32.c
1 /* Convert UTF-16 string to UTF-32 string.
2    Copyright (C) 2002, 2006 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_u32
26 #define SRC_UNIT uint16_t
27 #define DST_UNIT uint32_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       if (length + 1 > allocated)
75         {
76           DST_UNIT *memory;
77
78           allocated = (allocated > 0 ? 2 * allocated : 12);
79           if (length + 1 > allocated)
80             allocated = length + 1;
81           if (result == resultbuf || result == NULL)
82             memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
83           else
84             memory =
85               (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
86
87           if (memory == NULL)
88             {
89               if (!(result == resultbuf || result == NULL))
90                 free (result);
91               errno = ENOMEM;
92               return NULL;
93             }
94           if (result == resultbuf && length > 0)
95             memcpy ((char *) memory, (char *) result,
96                     length * sizeof (DST_UNIT));
97           result = memory;
98         }
99       result[length++] = uc;
100     }
101
102   if (length == 0)
103     {
104       if (result == NULL)
105         {
106           /* Return a non-NULL value.  NULL means error.  */
107           result = (DST_UNIT *) malloc (1);
108           if (result == NULL)
109             {
110               errno = ENOMEM;
111               return NULL;
112             }
113         }
114     }
115   else if (result != resultbuf && length < allocated)
116     {
117       /* Shrink the allocated memory if possible.  */
118       DST_UNIT *memory;
119
120       memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
121       if (memory != NULL)
122         result = memory;
123     }
124
125   *lengthp = length;
126   return result;
127 }