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