Debian Codegraph @

tools/gnulib/unistr/u32-to-u8.c

raw symbols SCIP
1 /* Convert UTF-32 string to UTF-8 string.
2 Copyright (C) 2002, 2006-2007, 2009-2024 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>cxx . . $ `<file>/liblouis/config.h`/
19
20 /* Specification. */
21 #include "unistr.h"cxx . . $ `<file>/tools/gnulib/unistr.h`/
22
23 #define FUNCcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:23:9`! u32_to_u8
24 #define SRC_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:24:9`! uint32_t
25 #define DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`! uint8_t
26
27 #include <errno.h>
28 #include <stdlib.h>cxx . . $ `<file>/tools/gnulib/stdlib.h`/
29 #include <string.h>cxx . . $ `<file>/tools/gnulib/string.h`/
30
31 DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`! *
32 FUNCcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:23:9`! (const SRC_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:24:9`! *s, size_tcxx . . $ size_t# n, DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`! *resultbuf, size_tcxx . . $ size_t# *lengthp)
33 {
34 const SRC_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:24:9`! *s_end = s + n;
35 /* Output string accumulator. */
36 DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`! *result;
37 size_tcxx . . $ size_t# allocated;
38 size_tcxx . . $ size_t# length;
39
40 if (resultbuf != NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
41 {
42 result = resultbuf;
43 allocated = *lengthp;
44 }
45 else
46 {
47 result = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
48 allocated = 0;
49 }
50 length = 0;
51 /* Invariants:
52 result is either == resultbuf or == NULL or malloc-allocated.
53 If length > 0, then result != NULL. */
54
55 while (s < s_end)
56 {
57 ucs4_tcxx . . $ ucs4_t# uc;
58 int count;
59
60 /* Fetch a Unicode character from the input string. */
61 uc = *s++;
62 /* No need to call the safe variant u32_mbtouc, because
63 u8_uctomb will verify uc anyway. */
64
65 /* Store it in the output string. */
66 count = u8_uctombcxx . . $ u8_uctomb(139565b42220505b). (result + length, uc, allocated - length);
67 if (count == -1)
68 {
69 if (!(result == resultbuf || result == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!))
70 freecxx . . $ free(5d22bdacc48458e8). (result);
71 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EILSEQcxx . . $ `<external>/17922322188510340118/errno.h:68:9`!;
72 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
73 }
74 if (count == -2)
75 {
76 DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`! *memory;
77
78 allocated = (allocated > 0 ? 2 * allocated : 12);
79 if (length + 6 > allocated)
80 allocated = length + 6;
81 if (result == resultbuf || result == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
82 memory = (DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`! *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (allocated * sizeof (DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`!));
83 else
84 memory =
85 (DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`! *) realloccxx . . $ realloc(417ab160f6baadc6). (result, allocated * sizeof (DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`!));
86
87 if (memory == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
88 {
89 if (!(result == resultbuf || result == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!))
90 freecxx . . $ free(5d22bdacc48458e8). (result);
91 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
92 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
93 }
94 if (result == resultbuf && length > 0)
95 memcpycxx . . $ memcpy(cc817cb175f282d). ((char *) memory, (char *) result,
96 length * sizeof (DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`!));
97 result = memory;
98 count = u8_uctombcxx . . $ u8_uctomb(139565b42220505b). (result + length, uc, allocated - length);
99 if (count < 0)
100 abortcxx . . $ abort(8df32a62017fbbf6). ();
101 }
102 length += count;
103 }
104
105 if (length == 0)
106 {
107 if (result == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
108 {
109 /* Return a non-NULL value. NULL means error. */
110 result = (DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`! *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (1);
111 if (result == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
112 {
113 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
114 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
115 }
116 }
117 }
118 else if (result != resultbuf && length < allocated)
119 {
120 /* Shrink the allocated memory if possible. */
121 DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`! *memory;
122
123 memory = (DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`! *) realloccxx . . $ realloc(417ab160f6baadc6). (result, length * sizeof (DST_UNITcxx . . $ `tools/gnulib/unistr/u32-to-u8.c:25:9`!));
124 if (memory != NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
125 result = memory;
126 }
127
128 *lengthp = length;
129 return result;
130 }