Debian Codegraph @

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

raw
1 /* Convert UTF-32 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: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file 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
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 /* Specification. */
21 #include "unistr.h"
22
23 #define FUNC u32_to_u8
24 #define SRC_UNIT uint32_t
25 #define DST_UNIT uint8_t
26
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 DST_UNIT *
32 FUNC (const SRC_UNIT *s, size_t n, DST_UNIT *resultbuf, size_t *lengthp)
33 {
34 const SRC_UNIT *s_end = s + n;
35
36 /* Output string accumulator. */
37 DST_UNIT *result;
38 size_t allocated;
39 if (resultbuf != NULL)
40 {
41 result = resultbuf;
42 allocated = *lengthp;
43 }
44 else
45 {
46 result = NULL;
47 allocated = 0;
48 }
49 size_t length = 0;
50 /* Invariants:
51 result is either == resultbuf or == NULL or malloc-allocated.
52 If length > 0, then result != NULL. */
53
54 while (s < s_end)
55 {
56 /* Fetch a Unicode character from the input string. */
57 ucs4_t uc = *s++;
58 /* No need to call the safe variant u32_mbtouc, because
59 u8_uctomb will verify uc anyway. */
60
61 /* Store it in the output string. */
62 int count = u8_uctomb (result + length, uc, allocated - length);
63 if (count == -1)
64 {
65 if (!(result == resultbuf || result == NULL))
66 free (result);
67 errno = EILSEQ;
68 return NULL;
69 }
70 if (count == -2)
71 {
72 allocated = (allocated > 0 ? 2 * allocated : 12);
73 if (length + 6 > allocated)
74 allocated = length + 6;
75
76 DST_UNIT *memory;
77 if (result == resultbuf || result == NULL)
78 memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
79 else
80 memory =
81 (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
82
83 if (memory == NULL)
84 {
85 if (!(result == resultbuf || result == NULL))
86 free (result);
87 errno = ENOMEM;
88 return NULL;
89 }
90 if (result == resultbuf && length > 0)
91 memcpy ((char *) memory, (char *) result,
92 length * sizeof (DST_UNIT));
93 result = memory;
94 count = u8_uctomb (result + length, uc, allocated - length);
95 if (count < 0)
96 abort ();
97 }
98 length += count;
99 }
100
101 if (length == 0)
102 {
103 if (result == NULL)
104 {
105 /* Return a non-NULL value. NULL means error. */
106 result = (DST_UNIT *) malloc (1);
107 if (result == NULL)
108 {
109 errno = ENOMEM;
110 return NULL;
111 }
112 }
113 }
114 else if (result != resultbuf && length < allocated)
115 {
116 /* Shrink the allocated memory if possible. */
117 DST_UNIT *memory =
118 (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
119 if (memory != NULL)
120 result = memory;
121 }
122
123 *lengthp = length;
124 return result;
125 }