GCC Code Coverage Report


Directory: src/gate/
File: src/gate/blobs.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 154 165 93.3%
Functions: 22 22 100.0%
Branches: 51 74 68.9%

Line Branch Exec Source
1
2 /* GATE PROJECT LICENSE:
3 +----------------------------------------------------------------------------+
4 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
5 | All rights reserved. |
6 | |
7 | Redistribution and use in source and binary forms, with or without |
8 | modification, are permitted provided that the following conditions are met:|
9 | |
10 | 1. Redistributions of source code must retain the above copyright notice, |
11 | this list of conditions and the following disclaimer. |
12 | 2. Redistributions in binary form must reproduce the above copyright |
13 | notice, this list of conditions and the following disclaimer in the |
14 | documentation and/or other materials provided with the distribution. |
15 | |
16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
26 | THE POSSIBILITY OF SUCH DAMAGE. |
27 +----------------------------------------------------------------------------+
28 */
29 #include "gate/blobs.h"
30 #include "gate/results.h"
31 #include "gate/memalloc.h"
32
33 80 gate_blob_t* gate_blob_create(gate_blob_t* blob, void const* data, gate_size_t length)
34 {
35 gate_blobbuffer_t* ptr_buffer;
36
37
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 76 times.
80 if (length == 0)
38 {
39 4 return gate_blob_create_empty(blob);
40 }
41
42 76 ptr_buffer = gate_mem_alloc(sizeof(gate_blobbuffer_t) + length);
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (ptr_buffer == NULL)
44 {
45 ✗ return NULL;
46 }
47 76 gate_atomic_int_set(&ptr_buffer->refcount, 1);
48
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 1 times.
76 if (data)
49 {
50 75 gate_mem_copy(&ptr_buffer->data[0], data, length);
51 }
52 76 blob->data = &ptr_buffer->data[0];
53 76 blob->length = length;
54 76 blob->buffer = ptr_buffer;
55 76 return blob;
56 }
57 9 gate_blob_t* gate_blob_create_static(gate_blob_t* blob, void const* data, gate_size_t length)
58 {
59 9 blob->data = data;
60 9 blob->length = length;
61 9 blob->buffer = NULL;
62 9 return blob;
63 }
64 189 gate_blob_t* gate_blob_create_empty(gate_blob_t* blob)
65 {
66 189 blob->data = NULL;
67 189 blob->length = 0;
68 189 blob->buffer = NULL;
69 189 return blob;
70 }
71 2 gate_blob_t* gate_blob_create_copy(gate_blob_t* blob, gate_blob_t const* srcblob)
72 {
73 2 gate_size_t length = gate_blob_length(srcblob);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (length == 0)
75 {
76 ✗ return gate_blob_create_empty(blob);
77 }
78 else
79 {
80 2 return gate_blob_create(blob, gate_blob_data(srcblob), length);
81 }
82 }
83 6 gate_blob_t* gate_blob_create_clone(gate_blob_t* blob, gate_blob_t const* srcblob)
84 {
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!srcblob)
86 {
87 ✗ return gate_blob_create_empty(blob);
88 }
89 else
90 {
91
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (srcblob->buffer)
92 {
93 3 blob->data = srcblob->data;
94 3 blob->length = srcblob->length;
95 3 blob->buffer = srcblob->buffer;
96 3 gate_atomic_int_inc(&blob->buffer->refcount);
97 3 return blob;
98 }
99 else
100 {
101 3 return gate_blob_create(blob, gate_blob_data(srcblob), gate_blob_length(srcblob));
102 }
103 }
104 }
105 26 gate_blob_t* gate_blob_create_duplicate(gate_blob_t* blob, gate_blob_t const* srcblob)
106 {
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (!srcblob)
108 {
109 ✗ return gate_blob_create_empty(blob);
110 }
111 else
112 {
113 26 blob->data = srcblob->data;
114 26 blob->length = srcblob->length;
115 26 blob->buffer = srcblob->buffer;
116
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 7 times.
26 if (blob->buffer)
117 {
118 19 gate_atomic_int_inc(&blob->buffer->refcount);
119 }
120 26 return blob;
121 }
122 }
123
124 2 gate_blob_t* gate_blob_create_subset(gate_blob_t* blob, gate_blob_t const* srcblob, gate_size_t offset, gate_size_t length)
125 {
126
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 if (!srcblob || (offset >= srcblob->length) || (length == 0))
127 {
128 ✗ return gate_blob_create_empty(blob);
129 }
130 else
131 {
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (offset + length > srcblob->length)
133 {
134 ✗ length = srcblob->length - offset;
135 }
136
137 2 blob->data = (void const*)((gate_uint8_t const*)srcblob->data + offset);
138 2 blob->length = length;
139 2 blob->buffer = srcblob->buffer;
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (blob->buffer)
141 {
142 ✗ gate_atomic_int_inc(&blob->buffer->refcount);
143 }
144 2 return blob;
145 }
146 }
147
148 158 void gate_blob_release(gate_blob_t* blob)
149 {
150
1/2
✓ Branch 0 taken 158 times.
✗ Branch 1 not taken.
158 if (blob)
151 {
152
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 63 times.
158 if (blob->buffer)
153 {
154
2/2
✓ Branch 1 taken 73 times.
✓ Branch 2 taken 22 times.
95 if (gate_atomic_int_dec(&blob->buffer->refcount) == 0)
155 {
156 73 gate_mem_dealloc(blob->buffer);
157 }
158 }
159 158 gate_blob_create_empty(blob);
160 }
161 158 }
162
163 158 gate_size_t gate_blob_length(gate_blob_t const* blob)
164 {
165 158 gate_size_t ret = 0;
166
1/2
✓ Branch 0 taken 158 times.
✗ Branch 1 not taken.
158 if (blob)
167 {
168 158 ret = blob->length;
169 }
170 158 return ret;
171 }
172 79 void const* gate_blob_data(gate_blob_t const* blob)
173 {
174 79 void const* data = NULL;
175
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 if (blob)
176 {
177 79 data = blob->data;
178 }
179 79 return data;
180 }
181 2 void* gate_blob_databuffer(gate_blob_t* blob)
182 {
183 2 void* data = NULL;
184
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (blob && blob->buffer)
185 {
186 2 data = &blob->buffer->data[0];
187 }
188 2 return data;
189 }
190 29 gate_uint8_t gate_blob_get_byte(gate_blob_t const* blob, gate_size_t index)
191 {
192 29 gate_uint8_t ret = 0;
193 29 gate_size_t const length = gate_blob_length(blob);
194 gate_uint8_t const* data;
195
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1 times.
29 if (length != 0)
196 {
197 28 data = (gate_uint8_t const*)blob->data;
198
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1 times.
28 if (index < length)
199 {
200 27 ret = data[index];
201 }
202 }
203 29 return ret;
204 }
205 5 gate_result_t gate_blob_set_byte(gate_blob_t* blob, gate_size_t index, gate_uint8_t content)
206 {
207 5 gate_result_t ret = GATE_RESULT_FAILED;
208 5 gate_size_t const length = gate_blob_length(blob);
209
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 if ((length != 0) && (blob->buffer != NULL))
210 {
211 5 gate_uint8_t* data = (gate_uint8_t*)blob->buffer->data;
212
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (index < length)
213 {
214 4 data[index] = content;
215 4 ret = GATE_RESULT_OK;
216 }
217 else
218 {
219 1 ret = GATE_RESULT_OUTOFBOUNDS;
220 }
221 }
222 5 return ret;
223 }
224 1 gate_size_t gate_blob_get_bytes(gate_blob_t const* blob, gate_size_t index, void* ptr_dest, gate_size_t dest_length)
225 {
226 1 gate_size_t length = gate_blob_length(blob);
227 gate_uint8_t const* ptr_data;
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (index >= length)
229 {
230 ✗ return 0;
231 }
232
233 1 length -= index;
234
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (dest_length < length)
235 {
236 1 length = dest_length;
237 }
238 1 ptr_data = (gate_uint8_t const*)blob->data;
239 1 gate_mem_copy(ptr_dest, &ptr_data[index], length);
240 1 return length;
241 }
242 1 gate_size_t gate_blob_set_bytes(gate_blob_t* blob, gate_size_t index, void const* ptr_src, gate_size_t src_length)
243 {
244 1 gate_size_t length = gate_blob_length(blob);
245 gate_uint8_t* ptr_data;
246
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if ((index >= length) || (!blob->buffer))
247 {
248 ✗ return 0;
249 }
250
251 1 length -= index;
252
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (src_length < length)
253 {
254 1 length = src_length;
255 }
256 1 ptr_data = &blob->buffer->data[0];
257 1 gate_mem_copy(&ptr_data[index], ptr_src, length);
258 1 return length;
259 }
260
261 9 int gate_blob_compare(gate_blob_t const* blob1, gate_blob_t const* blob2)
262 {
263 9 gate_uint8_t const * p1 = (gate_uint8_t const *)gate_blob_data(blob1);
264 9 gate_uint8_t const * p2 = (gate_uint8_t const *)gate_blob_data(blob2);
265 9 gate_size_t const l1 = gate_blob_length(blob1);
266 9 gate_size_t const l2 = gate_blob_length(blob2);
267 9 gate_size_t l = l1 < l2 ? l1 : l2;
268
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 4 times.
32 for (; l != 0; --l, ++p1, ++p2)
269 {
270
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 25 times.
28 if (*p1 < *p2)
271 {
272 3 return -1;
273 }
274
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 23 times.
25 else if (*p1 > *p2)
275 {
276 2 return 1;
277 }
278 }
279
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (l1 == l2)
280 {
281 2 return 0;
282 }
283 else
284 {
285
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 return (l1 < l2 ? -1 : 1);
286 }
287 }
288
289
290 1 gate_result_t gate_blob_copy_constructor(void* dest_mem, void const* src_mem)
291 {
292 1 gate_blob_t* dst = (gate_blob_t*)dest_mem;
293 1 gate_blob_t const* src = (gate_blob_t const*)src_mem;
294
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_blob_create_clone(dst, src))
295 {
296 ✗ return GATE_RESULT_OUTOFMEMORY;
297 }
298 else
299 {
300 1 return GATE_RESULT_OK;
301 }
302 }
303
304 1 gate_result_t gate_blob_duplicate_constructor(void* dest_mem, void const* src_mem)
305 {
306 1 gate_blob_t* dst = (gate_blob_t*)dest_mem;
307 1 gate_blob_t const* src = (gate_blob_t const*)src_mem;
308
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_blob_create_duplicate(dst, src))
309 {
310 ✗ return GATE_RESULT_OUTOFMEMORY;
311 }
312 else
313 {
314 1 return GATE_RESULT_OK;
315 }
316 }
317
318 2 void gate_blob_destructor(void* dest)
319 {
320 2 gate_blob_t* dst = (gate_blob_t*)dest;
321 2 gate_blob_release(dst);
322 2 }
323
324 /*****************************
325 * *
326 * BitField implementation *
327 * *
328 *****************************/
329
330 28 gate_bool_t gate_bit_is_set(void const* data, gate_size_t bitindex)
331 {
332 28 unsigned char const* ptr = (unsigned char const*)data;
333 28 gate_size_t byte_index = bitindex / 8;
334 28 gate_size_t bit_pos = bitindex % 8;
335 28 return (ptr[byte_index] & (1 << bit_pos)) == 0 ? false : true;
336 }
337 11 void gate_bit_set(void* data, gate_size_t bitindex)
338 {
339 11 unsigned char* ptr = (unsigned char*)data;
340 11 gate_size_t byte_index = bitindex / 8;
341 11 gate_size_t bit_pos = bitindex % 8;
342
343 11 ptr[byte_index] |= (unsigned char)(1 << bit_pos);
344 11 }
345 11 void gate_bit_clear(void* data, gate_size_t bitindex)
346 {
347 11 unsigned char* ptr = (unsigned char*)data;
348 11 gate_size_t byte_index = bitindex / 8;
349 11 gate_size_t bit_pos = bitindex % 8;
350
351 11 ptr[byte_index] &= ~(unsigned char)(1 << bit_pos);
352 11 }
353