Line data Source code
1 : /*
2 : * libpoporon - encode.c
3 : *
4 : * Copyright (c) 2025 Go Kudo
5 : *
6 : * This library is licensed under the BSD 3-Clause License.
7 : * For license details, please refer to the LICENSE file.
8 : *
9 : * SPDX-FileCopyrightText: Go Kudo <zeriyoshi@gmail.com>
10 : * SPDX-License-Identifier: BSD-3-Clause
11 : */
12 :
13 : #include "poporon_internal.h"
14 :
15 7 : extern bool poporon_encode_u8(poporon_t *pprn, uint8_t *data, size_t size, uint8_t *parity)
16 : {
17 : uint16_t i, j, fb;
18 :
19 7 : if (!pprn || !data || !parity) {
20 3 : return false;
21 : }
22 :
23 4 : pmemset(parity, 0, pprn->rs->num_roots * sizeof(uint8_t));
24 :
25 260 : for (i = 0; i < size; i++) {
26 256 : fb = pprn->rs->gf->exp2log[(((uint16_t)data[i]) & ((uint16_t)pprn->rs->gf->field_size)) ^ parity[0]];
27 :
28 256 : if (fb != pprn->rs->gf->field_size) {
29 8192 : for (j = 1; j < pprn->rs->num_roots; j++) {
30 7936 : parity[j] ^= pprn->rs->gf->log2exp[gf_mod(pprn->rs->gf, fb + pprn->rs->generator_polynomial[pprn->rs->num_roots - j])];
31 : }
32 : }
33 :
34 256 : pmemmove(&parity[0], &parity[1], sizeof(uint8_t) * (pprn->rs->num_roots - 1));
35 :
36 256 : if (fb != pprn->rs->gf->field_size) {
37 256 : parity[pprn->rs->num_roots - 1] = pprn->rs->gf->log2exp[gf_mod(pprn->rs->gf, fb + pprn->rs->generator_polynomial[0])];
38 : } else {
39 0 : parity[pprn->rs->num_roots - 1] = 0;
40 : }
41 : }
42 :
43 4 : return true;
44 : }
|