1 /* Convert UTF-16 string to UTF-32 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 u16_to_u32
32 #define SRC_UNIT uint16_t
33 #define DST_UNIT uint32_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]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 count = u16_mbtoucr (&uc, s, s_end - s);
70 if (count < 0)
71 {
72 if (!(result == resultbuf || result == NULL))
73 free (result);
74 errno = EILSEQ;
75 return NULL;
76 }
77 s += count;
78
79 /* Store it in the output string. */
80 if (length + 1 > allocated)
81 {
82 DST_UNIT *memory;
83
84 allocated = (allocated > 0 ? 2 * allocated : 12);
85 if (length + 1 > allocated)
86 allocated = length + 1;
87 if (result == resultbuf || result == NULL)
88 memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
89 else
90 memory =
91 (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
92
93 if (memory == NULL)
94 {
95 if (!(result == resultbuf || result == NULL))
96 free (result);
97 errno = ENOMEM;
98 return NULL;
99 }
100 if (result == resultbuf && length > 0)
101 memcpy ((char *) memory, (char *) result,
102 length * sizeof (DST_UNIT));
103 result = memory;
104 }
105 result[length++] = uc;
106 }
107
108 if (length == 0)
109 {
110 if (result == NULL)
111 {
112 /* Return a non-NULL value. NULL means error. */
113 result = (DST_UNIT *) malloc (1);
114 if (result == NULL)
115 {
116 errno = ENOMEM;
117 return NULL;
118 }
119 }
120 }
121 else if (result != resultbuf && length < allocated)
122 {
123 /* Shrink the allocated memory if possible. */
124 DST_UNIT *memory;
125
126 memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
127 if (memory != NULL)
128 result = memory;
129 }
130
131 *lengthp = length;
132 return result;
133 }