Debian Codegraph @

src/buf.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2013-2022 Joris Vink <[email protected]>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <sys/types.h>
18
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23
24 #include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/
25
26 struct kore_bufcxx . . $ kore_buf# *
27 kore_buf_alloccxx . . $ kore_buf_alloc(ae982dcc82edde78).(size_t initial)
28 {
29 struct kore_bufcxx . . $ kore_buf# *buf;
30
31 buf = kore_malloccxx . . $ kore_malloc(bfcfa4d6b7f7ef64).(sizeof(*buf));
32 kore_buf_initcxx . . $ kore_buf_init(bd90e0817a56138f).(buf, initial);
33 buf->flagscxx . . $ kore_buf#flags. = KORE_BUF_OWNER_APIcxx . . $ `include/kore/kore.h:501:9`!;
34
35 return (buf);
36 }
37
38 void
39 kore_buf_initcxx . . $ kore_buf_init(bd90e0817a56138f).(struct kore_bufcxx . . $ kore_buf# *buf, size_t initial)
40 {
41 if (initial > 0)
42 buf->datacxx . . $ kore_buf#data. = kore_malloccxx . . $ kore_malloc(bfcfa4d6b7f7ef64).(initial);
43 else
44 buf->datacxx . . $ kore_buf#data. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
45
46 buf->lengthcxx . . $ kore_buf#length. = initial;
47 buf->offsetcxx . . $ kore_buf#offset. = 0;
48 buf->flagscxx . . $ kore_buf#flags. = 0;
49 }
50
51 void
52 kore_buf_cleanupcxx . . $ kore_buf_cleanup(2648f45d13d14762).(struct kore_bufcxx . . $ kore_buf# *buf)
53 {
54 kore_freecxx . . $ kore_free(5d22bdacc48458e8).(buf->datacxx . . $ kore_buf#data.);
55 buf->datacxx . . $ kore_buf#data. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
56 buf->offsetcxx . . $ kore_buf#offset. = 0;
57 buf->lengthcxx . . $ kore_buf#length. = 0;
58 }
59
60 void
61 kore_buf_freecxx . . $ kore_buf_free(2648f45d13d14762).(struct kore_bufcxx . . $ kore_buf# *buf)
62 {
63 kore_buf_cleanupcxx . . $ kore_buf_cleanup(2648f45d13d14762).(buf);
64 if (buf->flagscxx . . $ kore_buf#flags. & KORE_BUF_OWNER_APIcxx . . $ `include/kore/kore.h:501:9`!)
65 kore_freecxx . . $ kore_free(5d22bdacc48458e8).(buf);
66 }
67
68 void
69 kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(struct kore_bufcxx . . $ kore_buf# *buf, const void *data, size_t len)
70 {
71 if ((buf->offsetcxx . . $ kore_buf#offset. + len) < len)
72 fatalcxx . . $ fatal(f6bc48f0426f26ee).("overflow in kore_buf_append");
73
74 if ((buf->offsetcxx . . $ kore_buf#offset. + len) > buf->lengthcxx . . $ kore_buf#length.) {
75 buf->lengthcxx . . $ kore_buf#length. += len;
76 buf->datacxx . . $ kore_buf#data. = kore_realloccxx . . $ kore_realloc(417ab160f6baadc6).(buf->datacxx . . $ kore_buf#data., buf->lengthcxx . . $ kore_buf#length.);
77 }
78
79 memcpycxx . . $ memcpy(cc817cb175f282d).((buf->datacxx . . $ kore_buf#data. + buf->offsetcxx . . $ kore_buf#offset.), data, len);
80 buf->offsetcxx . . $ kore_buf#offset. += len;
81 }
82
83 void
84 kore_buf_appendvcxx . . $ kore_buf_appendv(c929ca63aaa59654).(struct kore_bufcxx . . $ kore_buf# *buf, const char *fmt, va_listcxx . . $ va_list# args)
85 {
86 int l;
87 va_listcxx . . $ va_list# copy;
88 char *b, sb[BUFSIZcxx . . $ `<external>/232824719582829767/stdio.h:100:9`!];
89
90 va_copycxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:56:9`!(copy, args);
91
92 l = vsnprintfcxx . . $ vsnprintf(a85eea89d1bd834f).(sb, sizeof(sb), fmt, args);
93 if (l == -1)
94 fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_buf_appendv(): vsnprintf error");
95
96 if ((size_t)l >= sizeof(sb)) {
97 l = vasprintfcxx . . $ vasprintf(592150f15fbaad71).(&b, fmt, copy);
98 if (l == -1)
99 fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_buf_appendv(): error or truncation");
100 } else {
101 b = sb;
102 }
103
104 kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(buf, b, l);
105 if (b != sb)
106 freecxx . . $ free(5d22bdacc48458e8).(b);
107
108 va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:52:9`!(copy);
109 }
110
111 void
112 kore_buf_appendfcxx . . $ kore_buf_appendf(e492ffcef53d871e).(struct kore_bufcxx . . $ kore_buf# *buf, const char *fmt, ...)
113 {
114 va_listcxx . . $ va_list# args;
115
116 va_startcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:50:9`!(args, fmt);
117 kore_buf_appendvcxx . . $ kore_buf_appendv(c929ca63aaa59654).(buf, fmt, args);
118 va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:52:9`!(args);
119 }
120
121 char *
122 kore_buf_stringifycxx . . $ kore_buf_stringify(4bc5b263e10e2f7b).(struct kore_bufcxx . . $ kore_buf# *buf, size_t *len)
123 {
124 char c;
125
126 if (len != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
127 *len = buf->offsetcxx . . $ kore_buf#offset.;
128
129 c = '\0';
130 kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(buf, &c, sizeof(c));
131
132 return ((char *)buf->datacxx . . $ kore_buf#data.);
133 }
134
135 u_int8_tcxx . . $ u_int8_t# *
136 kore_buf_releasecxx . . $ kore_buf_release(d59a61570302ba3d).(struct kore_bufcxx . . $ kore_buf# *buf, size_t *len)
137 {
138 u_int8_tcxx . . $ u_int8_t# *p;
139
140 p = buf->datacxx . . $ kore_buf#data.;
141 *len = buf->offsetcxx . . $ kore_buf#offset.;
142
143 buf->datacxx . . $ kore_buf#data. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
144 kore_buf_freecxx . . $ kore_buf_free(2648f45d13d14762).(buf);
145
146 return (p);
147 }
148
149 void
150 kore_buf_replace_stringcxx . . $ kore_buf_replace_string(8008de6d84fb5eac).(struct kore_bufcxx . . $ kore_buf# *b, const char *src,
151 const void *dst, size_t len)
152 {
153 char *key, *end, *tmp, *p;
154 size_t blen, off, off2, nlen, klen;
155
156 off = 0;
157 klen = strlencxx . . $ strlen(751346f8406fd082).(src);
158 for (;;) {
159 blen = b->offsetcxx . . $ kore_buf#offset.;
160 nlen = blen + len;
161 p = (char *)b->datacxx . . $ kore_buf#data.;
162
163 key = kore_mem_findcxx . . $ kore_mem_find(d6408e6ff8ad1a7e).(p + off, b->offsetcxx . . $ kore_buf#offset. - off, src, klen);
164 if (key == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
165 break;
166
167 end = key + klen;
168 off = key - p;
169 off2 = ((char *)(b->datacxx . . $ kore_buf#data. + b->offsetcxx . . $ kore_buf#offset.) - end);
170
171 tmp = kore_malloccxx . . $ kore_malloc(bfcfa4d6b7f7ef64).(nlen);
172 memcpycxx . . $ memcpy(cc817cb175f282d).(tmp, p, off);
173 if (dst != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
174 memcpycxx . . $ memcpy(cc817cb175f282d).((tmp + off), dst, len);
175 memcpycxx . . $ memcpy(cc817cb175f282d).((tmp + off + len), end, off2);
176
177 kore_freecxx . . $ kore_free(5d22bdacc48458e8).(b->datacxx . . $ kore_buf#data.);
178 b->datacxx . . $ kore_buf#data. = (u_int8_tcxx . . $ u_int8_t# *)tmp;
179 b->offsetcxx . . $ kore_buf#offset. = off + len + off2;
180 b->lengthcxx . . $ kore_buf#length. = nlen;
181
182 off = off + len;
183 }
184 }
185
186 void
187 kore_buf_resetcxx . . $ kore_buf_reset(2648f45d13d14762).(struct kore_bufcxx . . $ kore_buf# *buf)
188 {
189 buf->offsetcxx . . $ kore_buf#offset. = 0;
190 }