1 /* Convert UTF-16 string to UTF-8 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_u8
32 #define SRC_UNIT uint16_t
33 #define DST_UNIT uint8_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 count = u8_uctomb (result + length, uc, allocated - length);
81 if (count == -1)
82 {
83 if (!(result == resultbuf || result == NULL))
84 free (result);
85 errno = EILSEQ;
86 return NULL;
87 }
88 if (count == -2)
89 {
90 DST_UNIT *memory;
91
92 allocated = (allocated > 0 ? 2 * allocated : 12);
93 if (length + 6 > allocated)
94 allocated = length + 6;
95 if (result == resultbuf || result == NULL)
96 memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
97 else
98 memory =
99 (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
100
101 if (memory == NULL)
102 {
103 if (!(result == resultbuf || result == NULL))
104 free (result);
105 errno = ENOMEM;
106 return NULL;
107 }
108 if (result == resultbuf && length > 0)
109 memcpy ((char *) memory, (char *) result,
110 length * sizeof (DST_UNIT));
111 result = memory;
112 count = u8_uctomb (result + length, uc, allocated - length);
113 if (count < 0)
114 abort ();
115 }
116 length += count;
117 }
118
119 if (length == 0)
120 {
121 if (result == NULL)
122 {
123 /* Return a non-NULL value. NULL means error. */
124 result = (DST_UNIT *) malloc (1);
125 if (result == NULL)
126 {
127 errno = ENOMEM;
128 return NULL;
129 }
130 }
131 }
132 else if (result != resultbuf && length < allocated)
133 {
134 /* Shrink the allocated memory if possible. */
135 DST_UNIT *memory;
136
137 memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
138 if (memory != NULL)
139 result = memory;
140 }
141
142 *lengthp = length;
143 return result;
144 }