Bug Summary

File:extensions/array_extensions.c
Warning:line 822, column 39
The result of the left shift is undefined due to shifting by '32', which is greater or equal to the width of type 'unsigned int'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple i386-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name array_extensions.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu i686 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/joels/Current/lispbm -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -I .//include -I .//include/extensions -I .//src -I .//utils -I platform/linux/include -D _PRELUDE -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/11/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/i386-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -std=c99 -fdebug-compilation-dir=/home/joels/Current/lispbm -ferror-limit 19 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/joels/Current/lispbm/test_reports/version_0.41.0/scan-build/2026-10-16-144956-1359644-1 -x c src/extensions/array_extensions.c
1/*
2 Copyright 2022 - 2025 Joel Svensson svenssonjoel@yahoo.se
3 Copyright 2022, 2023 Benjamin Vedder
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include "extensions/array_extensions.h"
20
21#include "extensions.h"
22#include "symrepr.h"
23#include "lbm_memory.h"
24
25#include <math.h>
26
27#ifdef LBM_OPT_ARRAY_EXTENSIONS_SIZE
28#pragma GCC optimize ("-Os")
29#endif
30#ifdef LBM_OPT_ARRAY_EXTENSIONS_SIZE_AGGRESSIVE
31#pragma GCC optimize ("-Oz")
32#endif
33
34static lbm_uint little_endian = 0;
35static lbm_uint big_endian = 0;
36static lbm_uint dbc_sym = 0;
37
38static lbm_value array_extension_unsafe_free_array(lbm_value *args, lbm_uint argn);
39static lbm_value array_extension_buffer_append_i8(lbm_value *args, lbm_uint argn);
40static lbm_value array_extension_buffer_append_i16(lbm_value *args, lbm_uint argn);
41static lbm_value array_extension_buffer_append_i32(lbm_value *args, lbm_uint argn);
42static lbm_value array_extension_buffer_append_u8(lbm_value *args, lbm_uint argn);
43static lbm_value array_extension_buffer_append_u16(lbm_value *args, lbm_uint argn);
44static lbm_value array_extension_buffer_append_u24(lbm_value *args, lbm_uint argn);
45static lbm_value array_extension_buffer_append_u32(lbm_value *args, lbm_uint argn);
46static lbm_value array_extension_buffer_append_f32(lbm_value *args, lbm_uint argn);
47
48static lbm_value array_extension_buffer_get_i8(lbm_value *args, lbm_uint argn);
49static lbm_value array_extension_buffer_get_i16(lbm_value *args, lbm_uint argn);
50static lbm_value array_extension_buffer_get_i32(lbm_value *args, lbm_uint argn);
51static lbm_value array_extension_buffer_get_u8(lbm_value *args, lbm_uint argn);
52static lbm_value array_extension_buffer_get_u16(lbm_value *args, lbm_uint argn);
53static lbm_value array_extension_buffer_get_u24(lbm_value *args, lbm_uint argn);
54static lbm_value array_extension_buffer_get_u32(lbm_value *args, lbm_uint argn);
55static lbm_value array_extension_buffer_get_f32(lbm_value *args, lbm_uint argn);
56
57static lbm_value array_extension_buffer_length(lbm_value *args, lbm_uint argn);
58
59static lbm_value array_extensions_bufclear(lbm_value *args, lbm_uint argn);
60static lbm_value array_extensions_bufcpy(lbm_value *args, lbm_uint argn);
61static lbm_value array_extensions_bufset_bit(lbm_value *args, lbm_uint argn);
62
63static lbm_value array_extensions_bufset_bits_u(lbm_value *args, lbm_uint argn);
64static lbm_value array_extensions_bufset_bits_i(lbm_value *args, lbm_uint argn);
65static lbm_value array_extensions_bufget_bits_u(lbm_value *args, lbm_uint argn);
66static lbm_value array_extensions_bufget_bits_i(lbm_value *args, lbm_uint argn);
67static lbm_value array_extensions_bufset_bits_f32(lbm_value *args, lbm_uint argn);
68static lbm_value array_extensions_bufget_bits_f32(lbm_value *args, lbm_uint argn);
69
70void lbm_array_extensions_init(void) {
71
72 lbm_add_symbol_const("little-endian", &little_endian);
73 lbm_add_symbol_const("big-endian", &big_endian);
74 lbm_add_symbol_const("dbc", &dbc_sym);
75
76 lbm_add_extension("free", array_extension_unsafe_free_array);
77 lbm_add_extension("bufset-i8", array_extension_buffer_append_i8);
78 lbm_add_extension("bufset-i16", array_extension_buffer_append_i16);
79 lbm_add_extension("bufset-i32", array_extension_buffer_append_i32);
80 lbm_add_extension("bufset-u8", array_extension_buffer_append_u8);
81 lbm_add_extension("bufset-u16", array_extension_buffer_append_u16);
82 lbm_add_extension("bufset-u24", array_extension_buffer_append_u24);
83 lbm_add_extension("bufset-u32", array_extension_buffer_append_u32);
84 lbm_add_extension("bufset-f32", array_extension_buffer_append_f32);
85
86 lbm_add_extension("bufget-i8", array_extension_buffer_get_i8);
87 lbm_add_extension("bufget-i16", array_extension_buffer_get_i16);
88 lbm_add_extension("bufget-i32", array_extension_buffer_get_i32);
89 lbm_add_extension("bufget-u8", array_extension_buffer_get_u8);
90 lbm_add_extension("bufget-u16", array_extension_buffer_get_u16);
91 lbm_add_extension("bufget-u24", array_extension_buffer_get_u24);
92 lbm_add_extension("bufget-u32", array_extension_buffer_get_u32);
93 lbm_add_extension("bufget-f32", array_extension_buffer_get_f32);
94
95 lbm_add_extension("buflen", array_extension_buffer_length);
96 lbm_add_extension("bufclear", array_extensions_bufclear);
97 lbm_add_extension("bufcpy", array_extensions_bufcpy);
98 lbm_add_extension("bufset-bit", array_extensions_bufset_bit);
99
100 lbm_add_extension("bufset-bits-u", array_extensions_bufset_bits_u);
101 lbm_add_extension("bufset-bits-i", array_extensions_bufset_bits_i);
102 lbm_add_extension("bufget-bits-u", array_extensions_bufget_bits_u);
103 lbm_add_extension("bufget-bits-i", array_extensions_bufget_bits_i);
104 lbm_add_extension("bufset-bits-f32", array_extensions_bufset_bits_f32);
105 lbm_add_extension("bufget-bits-f32", array_extensions_bufget_bits_f32);
106}
107
108lbm_value array_extension_unsafe_free_array(lbm_value *args, lbm_uint argn) {
109 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
110 if (argn == 1) {
111 if (lbm_is_array_rw(args[0])) {
112 if (lbm_heap_explicit_free_array(args[0])) {
113 res = ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u);
114 } else {
115 res = ENC_SYM_NIL(((0x0) << 4) | 0x00000000u);
116 }
117 } else {
118 res = ENC_SYM_TERROR(((0x21) << 4) | 0x00000000u);
119 }
120 }
121 return res;
122}
123
124static bool_Bool decode_append_args(lbm_value *error, lbm_value *args, lbm_uint argn, lbm_uint *index, bool_Bool *be, lbm_uint *a_size, uint8_t **a_data) {
125 *be = true1;
126 *error = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
127 bool_Bool res = false0;
128 switch(argn) {
129 case 4:
130 if (lbm_type_of(args[3]) == LBM_TYPE_SYMBOL0x00000000u &&
131 lbm_dec_sym(args[3]) == little_endian) {
132 *be = false0;
133 }
134 /* fall through */
135 case 3: {
136 lbm_array_header_t *array = lbm_dec_array_rw(args[0]);
137 if(array &&
138 lbm_is_number(args[1]) &&
139 lbm_is_number(args[2])) {
140 *a_size = array->size;
141 *a_data = (uint8_t*)array->data;
142 *index = lbm_dec_as_u32(args[1]);
143 res = true1;
144 } else {
145 *error = ENC_SYM_TERROR(((0x21) << 4) | 0x00000000u);
146 }
147 }
148 }
149 return res;
150}
151
152static bool_Bool buffer_append_bytes(uint8_t *data, lbm_uint d_size, bool_Bool be, lbm_uint index, lbm_uint nbytes, lbm_uint value) {
153
154 lbm_uint last_index = index + (nbytes - 1);
155 bool_Bool res = false0;
156 if (last_index < d_size) {
157 res = true1;
158 switch(nbytes) {
159 case 1:
160 data[index] = (uint8_t) value;
161 break;
162 case 2:
163 if (be) {
164 data[index+1] = (uint8_t)value;
165 data[index] = (uint8_t)(value >> 8);
166 } else {
167 data[index] = (uint8_t)value;
168 data[index +1] = (uint8_t)(value >> 8);
169 }
170 break;
171 case 3:
172 if (be) {
173 data[index+2] = (uint8_t)value;
174 data[index+1] = (uint8_t)(value >> 8);
175 data[index] = (uint8_t)(value >> 16);
176 } else {
177 data[index] = (uint8_t)value;
178 data[index+1] = (uint8_t)(value >> 8);
179 data[index+2] = (uint8_t)(value >> 16);
180 }
181 break;
182 default:
183 if (be) {
184 data[index+3] = (uint8_t) value;
185 data[index+2] = (uint8_t) (value >> 8);
186 data[index+1] = (uint8_t) (value >> 16);
187 data[index] = (uint8_t) (value >> 24);
188 } else {
189 data[index] = (uint8_t) value;
190 data[index+1] = (uint8_t) (value >> 8);
191 data[index+2] = (uint8_t) (value >> 16);
192 data[index+3] = (uint8_t) (value >> 24);
193 }
194 break;
195 }
196 }
197 return res;
198}
199
200lbm_value array_extension_buffer_append_i8(lbm_value *args, lbm_uint argn) {
201
202 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
203 uint8_t *data = NULL((void*)0);
204 lbm_uint d_size = 0;
205 bool_Bool be = false0;
206 lbm_uint index = 0;
207
208 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
209 if (buffer_append_bytes(data, d_size, be, index, 1, (lbm_uint)lbm_dec_as_i32(args[2]))) {
210 res = ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u);
211 }
212 }
213 return res;
214}
215
216lbm_value array_extension_buffer_append_i16(lbm_value *args, lbm_uint argn) {
217
218 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
219 uint8_t *data = NULL((void*)0);
220 lbm_uint d_size = 0;
221 bool_Bool be = false0;
222 lbm_uint index = 0;
223
224 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
225 if (buffer_append_bytes(data, d_size, be, index, 2, (lbm_uint)lbm_dec_as_i32(args[2]))) {
226 res = ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u);
227 }
228 }
229 return res;
230}
231
232lbm_value array_extension_buffer_append_i32(lbm_value *args, lbm_uint argn) {
233
234 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
235 uint8_t *data = NULL((void*)0);
236 lbm_uint d_size = 0;
237 bool_Bool be = false0;
238 lbm_uint index = 0;
239
240 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
241 if (buffer_append_bytes(data, d_size, be, index, 4, (lbm_uint)lbm_dec_as_i32(args[2]))) {
242 res = ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u);
243 }
244 }
245 return res;
246}
247
248
249lbm_value array_extension_buffer_append_u8(lbm_value *args, lbm_uint argn) {
250
251 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
252 uint8_t *data = NULL((void*)0);
253 lbm_uint d_size = 0;
254 bool_Bool be = false0;
255 lbm_uint index = 0;
256
257 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
258 if (buffer_append_bytes(data, d_size, be, index, 1, (lbm_uint)lbm_dec_as_u32(args[2]))) {
259 res = ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u);
260 }
261 }
262 return res;
263}
264
265lbm_value array_extension_buffer_append_u16(lbm_value *args, lbm_uint argn) {
266
267 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
268 uint8_t *data = NULL((void*)0);
269 lbm_uint d_size = 0;
270 bool_Bool be = false0;
271 lbm_uint index = 0;
272
273 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
274 if (buffer_append_bytes(data, d_size, be, index, 2, (lbm_uint)lbm_dec_as_u32(args[2]))) {
275 res = ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u);
276 }
277 }
278 return res;
279}
280
281lbm_value array_extension_buffer_append_u24(lbm_value *args, lbm_uint argn) {
282
283 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
284 uint8_t *data = NULL((void*)0);
285 lbm_uint d_size = 0;
286 bool_Bool be = false0;
287 lbm_uint index = 0;
288
289 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
290 if (buffer_append_bytes(data, d_size, be, index, 3, (lbm_uint)lbm_dec_as_u32(args[2]))) {
291 res = ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u);
292 }
293 }
294 return res;
295}
296
297lbm_value array_extension_buffer_append_u32(lbm_value *args, lbm_uint argn) {
298
299 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
300 uint8_t *data = NULL((void*)0);
301 lbm_uint d_size = 0;
302 bool_Bool be = false0;
303 lbm_uint index = 0;
304
305 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
306 if (buffer_append_bytes(data, d_size, be, index, 4, (lbm_uint)lbm_dec_as_u32(args[2]))) {
307 res = ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u);
308 }
309 }
310 return res;
311}
312
313static lbm_uint float_to_u(float number) {
314 // Set subnormal numbers to 0 as they are not handled properly
315 // using this method.
316 if (fabsf(number) < 1.5e-38) {
317 number = 0.0;
318 }
319
320 int e = 0;
321 float sig = frexpf(number, &e);
322 float sig_abs = fabsf(sig);
323 uint32_t sig_i = 0;
324
325 if (sig_abs >= 0.5) {
326 sig_i = (uint32_t)((sig_abs - 0.5f) * 2.0f * 8388608.0f);
327 e += 126;
328 }
329
330 uint32_t res = (((uint32_t)e & 0xFFu) << 23) | (uint32_t)(sig_i & 0x7FFFFFu);
331 if (sig < 0) {
332 res |= 1U << 31;
333 }
334
335 return res;
336}
337
338static lbm_float u_to_float(uint32_t v) {
339
340 int e = (v >> 23) & 0xFF;
341 uint32_t sig_i = v & 0x7FFFFF;
342 bool_Bool neg = v & (1U << 31);
343
344 float sig = 0.0;
345 if (e != 0 || sig_i != 0) {
346 sig = (float)sig_i / (8388608.0f * 2.0f) + 0.5f;
347 e -= 126;
348 }
349
350 if (neg) {
351 sig = -sig;
352 }
353
354 return ldexpf(sig, e);
355}
356
357lbm_value array_extension_buffer_append_f32(lbm_value *args, lbm_uint argn) {
358
359 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
360 uint8_t *data = NULL((void*)0);
361 lbm_uint d_size = 0;
362 bool_Bool be = false0;
363 lbm_uint index = 0;
364
365 if (decode_append_args(&res, args, argn, &index, &be, &d_size, &data)) {
366 if (buffer_append_bytes(data, d_size, be, index, 4, (lbm_uint)float_to_u(lbm_dec_as_float(args[2])))) {
367 res = ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u);
368 }
369 }
370 return res;
371}
372
373/* (buffer-get-i8 buffer index) */
374/* (buffer-get-i16 buffer index little-endian) */
375
376static bool_Bool decode_get_args(lbm_value *error, lbm_value *args, lbm_uint argn, lbm_uint *index, bool_Bool *be, lbm_uint *a_size, uint8_t **a_data) {
377 bool_Bool res = false0;
378
379 *be=true1;
380
381 switch(argn) {
382 case 3:
383 if (lbm_type_of(args[2]) == LBM_TYPE_SYMBOL0x00000000u &&
384 lbm_dec_sym(args[2]) == little_endian) {
385 *be = false0;
386 }
387 /* fall through */
388 case 2: {
389 lbm_array_header_t *array = lbm_dec_array_r(args[0]);
390 if (array &&
391 lbm_is_number(args[1])) {
392 *a_size = array->size;
393 *a_data = (uint8_t*)array->data;
394 *index = lbm_dec_as_u32(args[1]);
395 res = true1;
396 } else {
397 *error = ENC_SYM_TERROR(((0x21) << 4) | 0x00000000u);
398 }
399 }
400 }
401 return res;
402}
403
404static bool_Bool buffer_get_uint(lbm_uint *r_value, uint8_t *data, lbm_uint d_size, bool_Bool be, lbm_uint index, lbm_uint nbytes) {
405
406 bool_Bool res = false0;
407 lbm_uint last_index = index + (nbytes - 1);
408
409 if (last_index < d_size) {
410 lbm_uint value = 0;
411 res = true1;
412 switch(nbytes) {
413 case 1:
414 value = (lbm_uint)data[index];
415 break;
416 case 2:
417 if (be) {
418 value =
419 (lbm_uint) data[index+1] |
420 (lbm_uint) data[index] << 8;
421 } else {
422 value =
423 (lbm_uint) data[index] |
424 (lbm_uint) data[index+1] << 8;
425 }
426 break;
427 case 3:
428 if (be) {
429 value =
430 (lbm_uint) data[index+2] |
431 (lbm_uint) data[index+1] << 8 |
432 (lbm_uint) data[index] << 16;
433 } else {
434 value =
435 (lbm_uint) data[index] |
436 (lbm_uint) data[index+1] << 8 |
437 (lbm_uint) data[index+2] << 16;
438 }
439 break;
440 case 4:
441 if (be) {
442 value =
443 (uint32_t) data[index+3] |
444 (uint32_t) data[index+2] << 8 |
445 (uint32_t) data[index+1] << 16 |
446 (uint32_t) data[index] << 24;
447 } else {
448 value =
449 (uint32_t) data[index] |
450 (uint32_t) data[index+1] << 8 |
451 (uint32_t) data[index+2] << 16 |
452 (uint32_t) data[index+3] << 24;
453 }
454 break;
455 default:
456 res = false0;
457 }
458 *r_value = value;
459 }
460 return res;
461}
462
463
464
465lbm_value array_extension_buffer_get_i8(lbm_value *args, lbm_uint argn) {
466 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
467 uint8_t *data = NULL((void*)0);
468 lbm_uint d_size = 0;
469 bool_Bool be = false0;
470 lbm_uint index = 0;
471 lbm_uint value = 0;
472
473 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
474 if (buffer_get_uint(&value, data, d_size, be, index, 1)) {
475 res =lbm_enc_i((int8_t)value);
476 }
477 }
478 return res;
479}
480
481lbm_value array_extension_buffer_get_i16(lbm_value *args, lbm_uint argn) {
482 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
483 uint8_t *data = NULL((void*)0);
484 lbm_uint d_size = 0;
485 bool_Bool be = false0;
486 lbm_uint index = 0;
487 lbm_uint value = 0;
488
489 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
490 if (buffer_get_uint(&value, data, d_size, be, index, 2)) {
491 res =lbm_enc_i((int16_t)value);
492 }
493 }
494 return res;
495}
496
497lbm_value array_extension_buffer_get_i32(lbm_value *args, lbm_uint argn) {
498 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
499 uint8_t *data = NULL((void*)0);
500 lbm_uint d_size = 0;
501 bool_Bool be = false0;
502 lbm_uint index = 0;
503 lbm_uint value = 0;
504
505 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
506 if (buffer_get_uint(&value, data, d_size, be, index, 4)) {
507 res =lbm_enc_i((int32_t)value);
508 }
509 }
510 return res;
511}
512
513lbm_value array_extension_buffer_get_u8(lbm_value *args, lbm_uint argn) {
514 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
515 uint8_t *data = NULL((void*)0);
516 lbm_uint d_size = 0;
517 bool_Bool be = false0;
518 lbm_uint index = 0;
519 lbm_uint value = 0;
520
521 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
522 if (buffer_get_uint(&value, data, d_size, be, index, 1)) {
523 res = lbm_enc_i((uint8_t)value);
524 }
525 }
526 return res;
527}
528
529lbm_value array_extension_buffer_get_u16(lbm_value *args, lbm_uint argn) {
530 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
531 uint8_t *data = NULL((void*)0);
532 lbm_uint d_size = 0;
533 bool_Bool be = false0;
534 lbm_uint index = 0;
535 lbm_uint value = 0;
536
537 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
538 if (buffer_get_uint(&value, data, d_size, be, index, 2)) {
539 res = lbm_enc_i((uint16_t)value);
540 }
541 }
542 return res;
543}
544
545lbm_value array_extension_buffer_get_u24(lbm_value *args, lbm_uint argn) {
546 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
547 uint8_t *data = NULL((void*)0);
548 lbm_uint d_size = 0;
549 bool_Bool be = false0;
550 lbm_uint index = 0;
551 lbm_uint value = 0;
552
553 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
554 if (buffer_get_uint(&value, data, d_size, be, index, 3)) {
555 res = lbm_enc_i((int32_t)value);
556 }
557 }
558 return res;
559}
560
561lbm_value array_extension_buffer_get_u32(lbm_value *args, lbm_uint argn) {
562 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
563 uint8_t *data = NULL((void*)0);
564 lbm_uint d_size = 0;
565 bool_Bool be = false0;
566 lbm_uint index = 0;
567 lbm_uint value = 0;
568
569 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
570 if (buffer_get_uint(&value, data, d_size, be, index, 4)) {
571 res = lbm_enc_u32((uint32_t)value);
572 }
573 }
574 return res;
575}
576
577lbm_value array_extension_buffer_get_f32(lbm_value *args, lbm_uint argn) {
578 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
579 uint8_t *data = NULL((void*)0);
580 lbm_uint d_size = 0;
581 bool_Bool be = false0;
582 lbm_uint index = 0;
583 lbm_uint value = 0;
584
585 if (decode_get_args(&res, args, argn, &index, &be, &d_size, &data)) {
586 if (buffer_get_uint(&value, data, d_size, be, index, 4)) {
587 res = lbm_enc_float(u_to_float((uint32_t)value));
588 }
589 }
590 return res;
591}
592
593lbm_value array_extension_buffer_length(lbm_value *args, lbm_uint argn) {
594 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
595 lbm_array_header_t *array;
596 if (argn == 1 &&
597 (array = lbm_dec_array_r(args[0])) &&
598 lbm_heap_array_valid(args[0])) {
599 res = lbm_enc_i((lbm_int)array->size);
600 }
601 return res;
602}
603
604//TODO: Have to think about 32 vs 64 bit here
605static lbm_value array_extensions_bufclear(lbm_value *args, lbm_uint argn) {
606 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
607 if (argn >= 1 && argn <= 4) {
608 res = ENC_SYM_TERROR(((0x21) << 4) | 0x00000000u);
609 if (lbm_is_array_rw(args[0])) {
610 lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(args[0]);
611
612 uint8_t clear_byte = 0;
613 if (argn >= 2) {
614 if (!lbm_is_number(args[1])) {
615 return res;
616 }
617 clear_byte = (uint8_t)lbm_dec_as_u32(args[1]);
618 }
619
620 uint32_t start = 0;
621 if (argn >= 3) {
622 if (!lbm_is_number(args[2])) {
623 return res;
624 }
625 uint32_t start_new = lbm_dec_as_u32(args[2]);
626 if (start_new < array->size) {
627 start = start_new;
628 } else {
629 return res;
630 }
631 }
632 // Truncates size on 64 bit build
633 uint32_t len = (uint32_t)array->size - start;
634 if (argn >= 4) {
635 if (!lbm_is_number(args[3])) {
636 return res;
637 }
638 uint32_t len_new = lbm_dec_as_u32(args[3]);
639 if (len_new <= len) {
640 len = len_new;
641 }
642 }
643
644 memset((char*)array->data + start, clear_byte, len);
645 res = ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u);
646 }
647 }
648 return res;
649}
650
651static lbm_value array_extensions_bufcpy(lbm_value *args, lbm_uint argn) {
652 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
653
654 if (argn == 5) {
655 res = ENC_SYM_TERROR(((0x21) << 4) | 0x00000000u);
656 if (lbm_is_array_rw(args[0]) && lbm_is_number(args[1]) &&
657 lbm_is_array_r(args[2]) && lbm_is_number(args[3]) &&lbm_is_number(args[4])) {
658 lbm_array_header_t *array1 = (lbm_array_header_t *)lbm_car(args[0]);
659
660 uint32_t start1 = lbm_dec_as_u32(args[1]);
661
662 lbm_array_header_t *array2 = (lbm_array_header_t *)lbm_car(args[2]);
663
664 uint32_t start2 = lbm_dec_as_u32(args[3]);
665 uint32_t len = lbm_dec_as_u32(args[4]);
666
667 if (start1 < array1->size && start2 < array2->size) {
668 if (len > (array1->size - start1)) {
669 len = ((uint32_t)array1->size - start1);
670 }
671 if (len > (array2->size - start2)) {
672 len = ((uint32_t)array2->size - start2);
673 }
674
675 memcpy((char*)array1->data + start1, (char*)array2->data + start2, len);
676 }
677 res = ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u);
678 }
679 }
680 return res;
681}
682
683static lbm_value array_extensions_bufset_bit(lbm_value *args, lbm_uint argn) {
684 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
685
686 if (argn == 3) {
687 res = ENC_SYM_TERROR(((0x21) << 4) | 0x00000000u);
688 if (lbm_is_array_rw(args[0]) &&
689 lbm_is_number(args[1]) && lbm_is_number(args[2])) {
690 lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(args[0]);
691
692 unsigned int pos = lbm_dec_as_u32(args[1]);
693 unsigned int bit = lbm_dec_as_u32(args[2]) ? 1 : 0;
694
695 unsigned int bytepos = pos / 8;
696
697 if (bytepos < array->size) {
698 unsigned int bitpos = pos % 8;
699 ((uint8_t*)array->data)[bytepos] &= (uint8_t)~(1 << bitpos);
700 ((uint8_t*)array->data)[bytepos] |= (uint8_t)(bit << bitpos);
701 }
702
703 res = ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u);
704 }
705 }
706 return res;
707}
708
709/* extract/set sequence of bits at arbitrary bit positon
710 within a byte-array. Alternatively allowing DBC bit order
711 with using the 'dbc symbol argument.
712*/
713
714static lbm_uint dbc_bit_pos(lbm_uint pos) {
715 return (7 - (pos % 8)) + (pos / 8) * 8;
716}
717
718static int64_t sign_extend64(uint64_t v, lbm_uint len) {
719 if (len >= 64) return (int64_t)v;
720 uint64_t sign_bit = (uint64_t)1 << (len - 1);
721 return (int64_t)((v ^ sign_bit) - sign_bit);
722}
723
724static void decode_bits_flags(lbm_value *args, lbm_uint argn, lbm_uint fixed_argn, bool_Bool *be, bool_Bool *dbc) {
725 *be = true1;
726 *dbc = false0;
727 for (lbm_uint i = fixed_argn; i < argn; i ++) {
9
Assuming 'i' is < 'argn'
10
Loop condition is true. Entering loop body
16
Assuming 'i' is >= 'argn'
17
Loop condition is false. Execution continues on line 727
728 if (lbm_type_of(args[i]) == LBM_TYPE_SYMBOL0x00000000u) {
11
Assuming the condition is true
12
Taking true branch
729 lbm_uint s = lbm_dec_sym(args[i]);
730 if (s == little_endian) *be = false0;
13
Assuming 's' is equal to 'little_endian'
14
Taking true branch
15
The value 0 is assigned to 'be', which participates in a condition later
731 else if (s == big_endian) *be = true1;
732 else if (s == dbc_sym) *dbc = true1;
733 }
734 }
735}
736
737static bool_Bool decode_bits_get_args(lbm_value *error, lbm_value *args, lbm_uint argn,
738 lbm_uint *pos, lbm_uint *len, bool_Bool *be, bool_Bool *dbc,
739 lbm_uint *a_size, uint8_t **a_data) {
740 *error = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
741 if (argn < 3 || argn > 5) return false0;
742 lbm_array_header_t *array = lbm_dec_array_r(args[0]);
743 if (!(array && lbm_is_number(args[1]) && lbm_is_number(args[2]))) {
744 *error = ENC_SYM_TERROR(((0x21) << 4) | 0x00000000u);
745 return false0;
746 }
747 *a_size = array->size;
748 *a_data = (uint8_t*)array->data;
749 *pos = lbm_dec_as_u32(args[1]);
750 *len = lbm_dec_as_u32(args[2]);
751 decode_bits_flags(args, argn, 3, be, dbc);
752 return true1;
753}
754
755static bool_Bool decode_bits_set_args(lbm_value *error, lbm_value *args, lbm_uint argn,
756 lbm_uint *pos, lbm_uint *len, bool_Bool *be, bool_Bool *dbc,
757 lbm_uint *a_size, uint8_t **a_data) {
758 *error = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
759 if (argn < 4 || argn > 6) return false0;
760 lbm_array_header_t *array = lbm_dec_array_rw(args[0]);
761 if (!(array && lbm_is_number(args[1]) && lbm_is_number(args[2]) && lbm_is_number(args[3]))) {
762 *error = ENC_SYM_TERROR(((0x21) << 4) | 0x00000000u);
763 return false0;
764 }
765 *a_size = array->size;
766 *a_data = (uint8_t*)array->data;
767 *pos = lbm_dec_as_u32(args[1]);
768 *len = lbm_dec_as_u32(args[2]);
769 decode_bits_flags(args, argn, 4, be, dbc);
770 return true1;
771}
772
773static bool_Bool bits_insert(uint8_t *data, lbm_uint d_size, lbm_uint pos, lbm_uint len,
774 bool_Bool be, bool_Bool dbc, uint64_t number) {
775 if (len == 0 || len > 64) return false0;
776 if (dbc && be) pos = dbc_bit_pos(pos);
777 if (be) number <<= (64 - len);
778
779 lbm_uint bitcnt = 0, remaining = len;
780 while (remaining > 0) {
781 lbm_uint bytepos = (pos + bitcnt) / 8;
782 if (bytepos >= d_size) return false0;
783 lbm_uint shift = (pos + bitcnt) % 8;
784 lbm_uint bits = 8 - shift;
785 if (bits > remaining) bits = remaining;
786
787 uint8_t bval, mask;
788 if (be) {
789 bval = (uint8_t)((number >> (64 - bits)) << (8 - bits - shift));
790 mask = (uint8_t)(~(0xFFu << (8 - bits - shift)) | (0xFFu << (8 - shift)));
791 number <<= bits;
792 } else {
793 bval = (uint8_t)(number << shift);
794 mask = (uint8_t)(~(0xFFu >> (8 - bits - shift)) | (0xFFu >> (8 - shift)));
795 number >>= bits;
796 }
797 data[bytepos] = (uint8_t)((data[bytepos] & mask) | bval);
798 bitcnt += bits;
799 remaining -= bits;
800 }
801 return true1;
802}
803
804static bool_Bool bits_extract(const uint8_t *data, lbm_uint d_size, lbm_uint pos, lbm_uint len,
805 bool_Bool be, bool_Bool dbc, uint64_t *out) {
806 if (len
22.1
'len' is not equal to 0
== 0 || len
22.2
'len' is <= 64
> 64) return false0;
807 if (dbc
22.3
'dbc' is false
&& be) pos = dbc_bit_pos(pos);
808
809 uint64_t res = 0;
810 lbm_uint bitcnt = 0, remaining = len;
23
'remaining' initialized to 32
811 while (remaining > 0) {
24
Loop condition is true. Entering loop body
812 lbm_uint bytepos = (pos + bitcnt) / 8;
813 if (bytepos >= d_size) return false0;
25
Assuming 'bytepos' is < 'd_size'
26
Taking false branch
814 lbm_uint shift = (pos + bitcnt) % 8;
815 lbm_uint bits = 8 - shift;
816 if (bits > remaining) bits = remaining;
27
Assuming 'bits' is > 'remaining'
28
Taking true branch
29
The value 32 is assigned to 'bits'
817
818 if (be
29.1
'be' is false
) {
30
Taking false branch
819 uint8_t bval = (uint8_t)(data[bytepos] & (0xFFu >> shift));
820 res = (res + bval) << (bits + shift);
821 } else {
822 uint8_t mask = (uint8_t)~(0xFFu << bits);
31
The result of the left shift is undefined due to shifting by '32', which is greater or equal to the width of type 'unsigned int'
823 uint8_t bval = (uint8_t)((data[bytepos] >> shift) & mask);
824 res |= ((uint64_t)bval) << bitcnt;
825 }
826 bitcnt += bits;
827 remaining -= bits;
828 }
829 if (be) res >>= 8;
830 *out = res;
831 return true1;
832}
833
834// Number of bits that fit in a u/i type.
835#define BITS_IMMEDIATE_SAFE(((lbm_uint)sizeof(lbm_uint) * 8) - 4) (((lbm_uint)sizeof(lbm_uint) * 8) - LBM_VAL_SHIFT4)
836
837static lbm_value array_extensions_bufset_bits_u(lbm_value *args, lbm_uint argn) {
838 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
839 lbm_uint pos, len, d_size; uint8_t *data; bool_Bool be, dbc;
840 if (decode_bits_set_args(&res, args, argn, &pos, &len, &be, &dbc, &d_size, &data)) {
841 res = bits_insert(data, d_size, pos, len, be, dbc,
842 lbm_dec_as_u64(args[3])) ? ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u) : ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
843 }
844 return res;
845}
846
847static lbm_value array_extensions_bufset_bits_i(lbm_value *args, lbm_uint argn) {
848 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
849 lbm_uint pos, len, d_size; uint8_t *data; bool_Bool be, dbc;
850 if (decode_bits_set_args(&res, args, argn, &pos, &len, &be, &dbc, &d_size, &data)) {
851 res = bits_insert(data, d_size, pos, len, be, dbc,
852 (uint64_t)lbm_dec_as_i64(args[3])) ? ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u) : ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
853 }
854 return res;
855}
856
857static lbm_value array_extensions_bufget_bits_u(lbm_value *args, lbm_uint argn) {
858 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
859 lbm_uint pos, len, d_size; uint8_t *data; bool_Bool be, dbc; uint64_t raw;
860 if (decode_bits_get_args(&res, args, argn, &pos, &len, &be, &dbc, &d_size, &data) &&
861 bits_extract(data, d_size, pos, len, be, dbc, &raw)) {
862 if (len <= BITS_IMMEDIATE_SAFE(((lbm_uint)sizeof(lbm_uint) * 8) - 4)) res = lbm_enc_u((lbm_uint)raw);
863 else if (len <= 32) res = lbm_enc_u32((uint32_t)raw);
864 else res = lbm_enc_u64(raw);
865 }
866 return res;
867}
868
869static lbm_value array_extensions_bufget_bits_i(lbm_value *args, lbm_uint argn) {
870 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
871 lbm_uint pos, len, d_size; uint8_t *data; bool_Bool be, dbc; uint64_t raw;
872 if (decode_bits_get_args(&res, args, argn, &pos, &len, &be, &dbc, &d_size, &data) &&
873 bits_extract(data, d_size, pos, len, be, dbc, &raw)) {
874 int64_t sv = sign_extend64(raw, len);
875 if (len <= BITS_IMMEDIATE_SAFE(((lbm_uint)sizeof(lbm_uint) * 8) - 4)) res = lbm_enc_i((lbm_int)sv);
876 else if (len <= 32) res = lbm_enc_i32((int32_t)sv);
877 else res = lbm_enc_i64(sv);
878 }
879 return res;
880}
881
882static bool_Bool decode_bits_f32_get_args(lbm_value *error, lbm_value *args, lbm_uint argn,
883 lbm_uint *pos, bool_Bool *be, bool_Bool *dbc,
884 lbm_uint *a_size, uint8_t **a_data) {
885 *error = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
886 if (argn < 2 || argn > 4) return false0;
2
Assuming 'argn' is >= 2
3
Assuming 'argn' is <= 4
4
Taking false branch
887 lbm_array_header_t *array = lbm_dec_array_r(args[0]);
888 if (!(array && lbm_is_number(args[1]))) {
5
Assuming 'array' is non-null
6
Assuming the condition is false
7
Taking false branch
889 *error = ENC_SYM_TERROR(((0x21) << 4) | 0x00000000u);
890 return false0;
891 }
892 *a_size = array->size;
893 *a_data = (uint8_t*)array->data;
894 *pos = lbm_dec_as_u32(args[1]);
895 decode_bits_flags(args, argn, 2, be, dbc);
8
Calling 'decode_bits_flags'
18
Returning from 'decode_bits_flags'
896 return true1;
19
Returning the value 1, which participates in a condition later
897}
898
899static bool_Bool decode_bits_f32_set_args(lbm_value *error, lbm_value *args, lbm_uint argn,
900 lbm_uint *pos, bool_Bool *be, bool_Bool *dbc,
901 lbm_uint *a_size, uint8_t **a_data) {
902 *error = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
903 if (argn < 3 || argn > 5) return false0;
904 lbm_array_header_t *array = lbm_dec_array_rw(args[0]);
905 if (!(array && lbm_is_number(args[1]) && lbm_is_number(args[2]))) {
906 *error = ENC_SYM_TERROR(((0x21) << 4) | 0x00000000u);
907 return false0;
908 }
909 *a_size = array->size;
910 *a_data = (uint8_t*)array->data;
911 *pos = lbm_dec_as_u32(args[1]);
912 decode_bits_flags(args, argn, 3, be, dbc);
913 return true1;
914}
915
916static lbm_value array_extensions_bufset_bits_f32(lbm_value *args, lbm_uint argn) {
917 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
918 lbm_uint pos, d_size; uint8_t *data; bool_Bool be, dbc;
919 if (decode_bits_f32_set_args(&res, args, argn, &pos, &be, &dbc, &d_size, &data)) {
920 res = bits_insert(data, d_size, pos, 32, be, dbc,
921 (uint64_t)float_to_u(lbm_dec_as_float(args[2]))) ? ENC_SYM_TRUE(((0x2) << 4) | 0x00000000u) : ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
922 }
923 return res;
924}
925
926static lbm_value array_extensions_bufget_bits_f32(lbm_value *args, lbm_uint argn) {
927 lbm_value res = ENC_SYM_EERROR(((0x22) << 4) | 0x00000000u);
928 lbm_uint pos, d_size; uint8_t *data; bool_Bool be, dbc; uint64_t raw;
929 if (decode_bits_f32_get_args(&res, args, argn, &pos, &be, &dbc, &d_size, &data) &&
1
Calling 'decode_bits_f32_get_args'
20
Returning from 'decode_bits_f32_get_args'
930 bits_extract(data, d_size, pos, 32, be, dbc, &raw)) {
21
Passing the value 32 via 4th parameter 'len'
22
Calling 'bits_extract'
931 res = lbm_enc_float(u_to_float((uint32_t)raw));
932 }
933 return res;
934}