Debian Codegraph @

lib/unistring/unistr/u16-to-u8.c

raw
1 /* Convert UTF-16 string to UTF-8 string.
2 Copyright (C) 2002, 2006-2007, 2009-2026 Free Software Foundation, Inc.
3 Written by Bruno Haible <[email protected]>, 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)
41 {
42 const SRC_UNIT *s_end = s + n;
43
44 /* Output string accumulator. */
45 DST_UNIT *result;
46 size_t allocated;
47 if (resultbuf != NULL)
48 {
49 result = resultbuf;
50 allocated = *lengthp;
51 }
52 else
53 {
54 result = NULL;
55 allocated = 0;
56 }
57 size_t length = 0;
58 /* Invariants:
59 result is either == resultbuf or == NULL or malloc-allocated.
60 If length > 0, then result != NULL. */
61
62 while (s < s_end)
63 {
64 /* Fetch a Unicode character from the input string. */
65 ucs4_t uc;
66 int count = u16_mbtoucr (&uc, s, s_end - s);
67 if (count < 0)
68 {
69 if (!(result == resultbuf || result == NULL))
70 free (result);
71 errno = EILSEQ;
72 return NULL;
73 }
74 s += count;
75
76 /* Store it in the output string. */
77 count = u8_uctomb (result + length, uc, allocated - length);
78 if (count == -1)
79 {
80 if (!(result == resultbuf || result == NULL))
81 free (result);
82 errno = EILSEQ;
83 return NULL;
84 }
85 if (count == -2)
86 {
87 allocated = (allocated > 0 ? 2 * allocated : 12);
88 if (length + 6 > allocated)
89 allocated = length + 6;
90
91 DST_UNIT *memory;
92 if (result == resultbuf || result == NULL)
93 memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
94 else
95 memory =
96 (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
97
98 if (memory == NULL)
99 {
100 if (!(result == resultbuf || result == NULL))
101 free (result);
102 errno = ENOMEM;
103 return NULL;
104 }
105 if (result == resultbuf && length > 0)
106 memcpy ((char *) memory, (char *) result,
107 length * sizeof (DST_UNIT));
108 result = memory;
109 count = u8_uctomb (result + length, uc, allocated - length);
110 if (count < 0)
111 abort ();
112 }
113 length += count;
114 }
115
116 if (length == 0)
117 {
118 if (result == NULL)
119 {
120 /* Return a non-NULL value. NULL means error. */
121 result = (DST_UNIT *) malloc (1);
122 if (result == NULL)
123 {
124 errno = ENOMEM;
125 return NULL;
126 }
127 }
128 }
129 else if (result != resultbuf && length < allocated)
130 {
131 /* Shrink the allocated memory if possible. */
132 DST_UNIT *memory =
133 (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
134 if (memory != NULL)
135 result = memory;
136 }
137
138 *lengthp = length;
139 return result;
140 }