Debian Codegraph @

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

raw
1 /* Convert UTF-8 string to UTF-32 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 u8_to_u32
24 #define SRC_UNIT uint8_t
25 #define DST_UNIT uint32_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;
58 int count = u8_mbtoucr (&uc, s, s_end - s);
59 if (count < 0)
60 {
61 if (!(result == resultbuf || result == NULL))
62 free (result);
63 errno = EILSEQ;
64 return NULL;
65 }
66 s += count;
67
68 /* Store it in the output string. */
69 if (length + 1 > allocated)
70 {
71 allocated = (allocated > 0 ? 2 * allocated : 12);
72 if (length + 1 > allocated)
73 allocated = length + 1;
74
75 DST_UNIT *memory;
76 if (result == resultbuf || result == NULL)
77 memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
78 else
79 memory =
80 (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
81
82 if (memory == NULL)
83 {
84 if (!(result == resultbuf || result == NULL))
85 free (result);
86 errno = ENOMEM;
87 return NULL;
88 }
89 if (result == resultbuf && length > 0)
90 memcpy ((char *) memory, (char *) result,
91 length * sizeof (DST_UNIT));
92 result = memory;
93 }
94 result[length++] = uc;
95 }
96
97 if (length == 0)
98 {
99 if (result == NULL)
100 {
101 /* Return a non-NULL value. NULL means error. */
102 result = (DST_UNIT *) malloc (1);
103 if (result == NULL)
104 {
105 errno = ENOMEM;
106 return NULL;
107 }
108 }
109 }
110 else if (result != resultbuf && length < allocated)
111 {
112 /* Shrink the allocated memory if possible. */
113 DST_UNIT *memory =
114 (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
115 if (memory != NULL)
116 result = memory;
117 }
118
119 *lengthp = length;
120 return result;
121 }