GCC Code Coverage Report


Directory: ../src/
File: extensions/ecc_extensions.c
Date: 2026-07-02 10:10:49
Coverage Exec Excl Total
Lines: 95.5% 42 0 44
Functions: 100.0% 4 0 4
Branches: 60.9% 28 0 46

Line Branch Exec Source
1 /*
2 Copyright 2026 Joel Svensson svenssonjoel@yahoo.se
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "extensions.h"
19 #include "lbm_c_interop.h"
20 #include "ecc_extensions.h"
21 #include "ecc.h"
22
23 #ifdef LBM_OPT_ECC_EXTENSIONS_SIZE
24 #pragma GCC optimize ("-Os")
25 #endif
26 #ifdef LBM_OPT_ECC_EXTENSIONS_SIZE_AGGRESSIVE
27 #pragma GCC optimize ("-Oz")
28 #endif
29
30 18 static lbm_value ext_rs_encoded_size(lbm_value *args, lbm_uint argn) {
31 18 lbm_value res = ENC_SYM_TERROR;
32
3/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 1 time.
36 if (argn == 2 &&
33
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 1 time.
35 lbm_is_number(args[0]) &&
34 17 lbm_is_number(args[1])) {
35 16 int payload_size = (int)lbm_dec_as_i32(args[0]);
36 16 int nroots = (int)lbm_dec_as_i32(args[1]);
37
5/6
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 1 time.
16 if (payload_size <= 0 || nroots < 2 || nroots % 2 != 0 ||
38
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 12 times.
13 payload_size + nroots > 255) {
39 4 res = ENC_SYM_EERROR;
40 } else {
41 12 res = lbm_enc_i(ecc_rs_encoded_size(payload_size, nroots));
42 }
43 }
44 18 return res;
45 }
46
47 8 static lbm_value ext_rs_encode(lbm_value *args, lbm_uint argn) {
48 8 lbm_value res = ENC_SYM_TERROR;
49
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
16 if (argn == 2 &&
50
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
16 lbm_is_array_r(args[0]) &&
51 8 lbm_is_number(args[1])) {
52 8 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(args[0]);
53 8 int nroots = (int)lbm_dec_as_i32(args[1]);
54 8 int total = (int)arr->size;
55 8 int payload_len = total - nroots;
56
5/10
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 8 times.
8 if (payload_len <= 0 || nroots < 2 || nroots > RS_MAX_ROOTS || nroots % 2 != 0 || total > 255) {
57 ✗ res = ENC_SYM_EERROR;
58 } else {
59 8 ecc_rs_encode((uint8_t*)arr->data, payload_len, nroots);
60 8 res = args[0];
61 }
62 }
63 8 return res;
64 }
65
66 8 static lbm_value ext_rs_decode(lbm_value *args, lbm_uint argn) {
67 8 lbm_value res = ENC_SYM_TERROR;
68
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
16 if (argn == 2 &&
69
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
16 lbm_is_array_r(args[0]) &&
70 8 lbm_is_number(args[1])) {
71 8 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(args[0]);
72 8 int nroots = (int)lbm_dec_as_i32(args[1]);
73 8 int total = (int)arr->size;
74 8 int payload = total - nroots;
75
5/10
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 8 times.
8 if (payload <= 0 || nroots < 2 || nroots > RS_MAX_ROOTS || nroots % 2 != 0 || total > 255) {
76 ✗ res = ENC_SYM_EERROR;
77 } else {
78 8 res = lbm_enc_i(ecc_rs_decode((uint8_t*)arr->data, payload, nroots));
79 }
80 }
81 8 return res;
82 }
83
84 310 void lbm_ecc_extensions_init(void) {
85 310 lbm_add_extension("rs-encode", ext_rs_encode);
86 310 lbm_add_extension("rs-decode", ext_rs_decode);
87 310 lbm_add_extension("rs-encoded-size", ext_rs_encoded_size);
88 310 }
89