| 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 | 54 | static lbm_value ext_rs_encoded_size(lbm_value *args, lbm_uint argn) { | |
| 31 | 54 | lbm_value res = ENC_SYM_TERROR; | |
| 32 |
3/4✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 3 times.
|
108 | if (argn == 2 && |
| 33 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
|
105 | lbm_is_number(args[0]) && |
| 34 | 51 | lbm_is_number(args[1])) { | |
| 35 | 48 | int payload_size = (int)lbm_dec_as_i32(args[0]); | |
| 36 | 48 | int nroots = (int)lbm_dec_as_i32(args[1]); | |
| 37 |
5/6✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 39 times.
✓ Branch 5 taken 3 times.
|
48 | if (payload_size <= 0 || nroots < 2 || nroots % 2 != 0 || |
| 38 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 36 times.
|
39 | payload_size + nroots > 255) { |
| 39 | 12 | res = ENC_SYM_EERROR; | |
| 40 | } else { | ||
| 41 | 36 | res = lbm_enc_i(ecc_rs_encoded_size(payload_size, nroots)); | |
| 42 | } | ||
| 43 | } | ||
| 44 | 54 | return res; | |
| 45 | } | ||
| 46 | |||
| 47 | 24 | static lbm_value ext_rs_encode(lbm_value *args, lbm_uint argn) { | |
| 48 | 24 | lbm_value res = ENC_SYM_TERROR; | |
| 49 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
48 | if (argn == 2 && |
| 50 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
48 | lbm_is_array_r(args[0]) && |
| 51 | 24 | lbm_is_number(args[1])) { | |
| 52 | 24 | lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(args[0]); | |
| 53 | 24 | int nroots = (int)lbm_dec_as_i32(args[1]); | |
| 54 | 24 | int total = (int)arr->size; | |
| 55 | 24 | int payload_len = total - nroots; | |
| 56 |
5/10✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 24 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 24 times.
|
24 | if (payload_len <= 0 || nroots < 2 || nroots > RS_MAX_ROOTS || nroots % 2 != 0 || total > 255) { |
| 57 | ✗ | res = ENC_SYM_EERROR; | |
| 58 | } else { | ||
| 59 | 24 | ecc_rs_encode((uint8_t*)arr->data, payload_len, nroots); | |
| 60 | 24 | res = args[0]; | |
| 61 | } | ||
| 62 | } | ||
| 63 | 24 | return res; | |
| 64 | } | ||
| 65 | |||
| 66 | 24 | static lbm_value ext_rs_decode(lbm_value *args, lbm_uint argn) { | |
| 67 | 24 | lbm_value res = ENC_SYM_TERROR; | |
| 68 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
48 | if (argn == 2 && |
| 69 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
48 | lbm_is_array_r(args[0]) && |
| 70 | 24 | lbm_is_number(args[1])) { | |
| 71 | 24 | lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(args[0]); | |
| 72 | 24 | int nroots = (int)lbm_dec_as_i32(args[1]); | |
| 73 | 24 | int total = (int)arr->size; | |
| 74 | 24 | int payload = total - nroots; | |
| 75 |
5/10✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 24 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 24 times.
|
24 | if (payload <= 0 || nroots < 2 || nroots > RS_MAX_ROOTS || nroots % 2 != 0 || total > 255) { |
| 76 | ✗ | res = ENC_SYM_EERROR; | |
| 77 | } else { | ||
| 78 | 24 | res = lbm_enc_i(ecc_rs_decode((uint8_t*)arr->data, payload, nroots)); | |
| 79 | } | ||
| 80 | } | ||
| 81 | 24 | return res; | |
| 82 | } | ||
| 83 | |||
| 84 | 714 | void lbm_ecc_extensions_init(void) { | |
| 85 | 714 | lbm_add_extension("rs-encode", ext_rs_encode); | |
| 86 | 714 | lbm_add_extension("rs-decode", ext_rs_decode); | |
| 87 | 714 | lbm_add_extension("rs-encoded-size", ext_rs_encoded_size); | |
| 88 | 714 | } | |
| 89 |