Debian Codegraph @

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

raw symbols SCIP
1 /* Convert UTF-16 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.
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>cxx . . $ `<file>/liblouis/config.h`/
27
28 /* Specification. */
29 #include "unistr.h"cxx . . $ `<file>/tools/gnulib/unistr.h`/
30
31 #define FUNCcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:31:9`! u16_to_u8
32 #define SRC_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:32:9`! uint16_t
33 #define DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`! uint8_t
34
35 #include <errno.h>
36 #include <stdlib.h>cxx . . $ `<file>/tools/gnulib/stdlib.h`/
37 #include <string.h>cxx . . $ `<file>/tools/gnulib/string.h`/
38
39 DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`! *
40 FUNCcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:31:9`! (const SRC_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:32:9`! *s, size_tcxx . . $ size_t# n, DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`! *resultbuf, size_tcxx . . $ size_t# *lengthp)
41 {
42 const SRC_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:32:9`! *s_end = s + n;
43 /* Output string accumulator. */
44 DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`! *result;
45 size_tcxx . . $ size_t# allocated;
46 size_tcxx . . $ size_t# length;
47
48 if (resultbuf != NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
49 {
50 result = resultbuf;
51 allocated = *lengthp;
52 }
53 else
54 {
55 result = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
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_tcxx . . $ ucs4_t# uc;
66 int count;
67
68 /* Fetch a Unicode character from the input string. */
69 count = u16_mbtoucrcxx . . $ u16_mbtoucr(81eaa12c138ed0d9). (&uc, s, s_end - s);
70 if (count < 0)
71 {
72 if (!(result == resultbuf || result == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!))
73 freecxx . . $ free(5d22bdacc48458e8). (result);
74 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EILSEQcxx . . $ `<external>/17922322188510340118/errno.h:68:9`!;
75 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
76 }
77 s += count;
78
79 /* Store it in the output string. */
80 count = u8_uctombcxx . . $ u8_uctomb(139565b42220505b). (result + length, uc, allocated - length);
81 if (count == -1)
82 {
83 if (!(result == resultbuf || result == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!))
84 freecxx . . $ free(5d22bdacc48458e8). (result);
85 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EILSEQcxx . . $ `<external>/17922322188510340118/errno.h:68:9`!;
86 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
87 }
88 if (count == -2)
89 {
90 DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`! *memory;
91
92 allocated = (allocated > 0 ? 2 * allocated : 12);
93 if (length + 6 > allocated)
94 allocated = length + 6;
95 if (result == resultbuf || result == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
96 memory = (DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`! *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (allocated * sizeof (DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`!));
97 else
98 memory =
99 (DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`! *) realloccxx . . $ realloc(417ab160f6baadc6). (result, allocated * sizeof (DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`!));
100
101 if (memory == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
102 {
103 if (!(result == resultbuf || result == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!))
104 freecxx . . $ free(5d22bdacc48458e8). (result);
105 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
106 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
107 }
108 if (result == resultbuf && length > 0)
109 memcpycxx . . $ memcpy(cc817cb175f282d). ((char *) memory, (char *) result,
110 length * sizeof (DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`!));
111 result = memory;
112 count = u8_uctombcxx . . $ u8_uctomb(139565b42220505b). (result + length, uc, allocated - length);
113 if (count < 0)
114 abortcxx . . $ abort(8df32a62017fbbf6). ();
115 }
116 length += count;
117 }
118
119 if (length == 0)
120 {
121 if (result == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
122 {
123 /* Return a non-NULL value. NULL means error. */
124 result = (DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`! *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (1);
125 if (result == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
126 {
127 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
128 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
129 }
130 }
131 }
132 else if (result != resultbuf && length < allocated)
133 {
134 /* Shrink the allocated memory if possible. */
135 DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`! *memory;
136
137 memory = (DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`! *) realloccxx . . $ realloc(417ab160f6baadc6). (result, length * sizeof (DST_UNITcxx . . $ `tools/gnulib/unistr/u16-to-u8.c:33:9`!));
138 if (memory != NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!)
139 result = memory;
140 }
141
142 *lengthp = length;
143 return result;
144 }