Line data Source code
1 : /*
2 : * libpoporon - poporon_internal.h
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 : #ifndef POPORON_INTERNAL_H
14 : #define POPORON_INTERNAL_H
15 :
16 : #include <stdbool.h>
17 : #include <stddef.h>
18 : #include <stdint.h>
19 :
20 : #include <poporon.h>
21 :
22 : #include <poporon/gf.h>
23 : #include <poporon/rs.h>
24 :
25 : #define POPORON_VERSION_ID 10000000
26 :
27 : #ifndef POPORON_BUILDTIME
28 : # define POPORON_BUILDTIME 0
29 : #endif
30 :
31 : struct _poporon_erasure_t {
32 : uint32_t capacity;
33 : uint32_t erasure_count;
34 : uint32_t *erasure_positions;
35 : uint16_t *corrections;
36 : };
37 :
38 : struct _poporon_gf_t {
39 : uint8_t symbol_size;
40 : uint8_t field_size;
41 : uint16_t *log2exp;
42 : uint16_t *exp2log;
43 : uint16_t generator_polynomial;
44 : };
45 :
46 : struct _poporon_rs_t {
47 : poporon_gf_t *gf;
48 : uint16_t first_consective_root;
49 : uint16_t primitive_element;
50 : uint16_t num_roots;
51 : uint16_t *generator_polynomial;
52 : };
53 :
54 : typedef struct {
55 : uint16_t *error_locator;
56 : uint16_t *syndrome;
57 : uint16_t *coefficients;
58 : uint16_t *polynomial;
59 : uint16_t *error_evaluator;
60 : uint16_t *error_roots;
61 : uint16_t *register_coefficients;
62 : uint16_t *error_locations;
63 : uint16_t primitive_inverse;
64 : } decoder_buffer_t;
65 :
66 : struct _poporon_t {
67 : poporon_rs_t *rs;
68 : decoder_buffer_t *buffer;
69 : };
70 :
71 : #define pmemcpy(dest, src, size) memcpy(dest, src, size)
72 : #define pmemmove(dest, src, size) memmove(dest, src, size)
73 : #define pmemcmp(s1, s2, size) memcmp(s1, s2, size)
74 : #define pmemset(dest, value, size) memset(dest, value, size)
75 : #define pmalloc(size) malloc(size)
76 : #define pcalloc(count, size) calloc(count, size)
77 : #define prealloc(ptr, size) realloc(ptr, size)
78 : #define pfree(ptr) free(ptr)
79 :
80 4 : static inline decoder_buffer_t *decoder_buffer_create(uint16_t num_roots)
81 : {
82 : decoder_buffer_t *buffer;
83 : uint16_t *raw_buffer;
84 : uint32_t i;
85 :
86 4 : buffer = (decoder_buffer_t *)pmalloc(sizeof(decoder_buffer_t));
87 4 : if (!buffer) {
88 : return NULL; /* LCOV_EXCL_LINE */
89 : }
90 :
91 4 : raw_buffer = (uint16_t *)pmalloc(8 * (num_roots + 1) * sizeof(uint16_t));
92 4 : if (!raw_buffer) {
93 : /* LCOV_EXCL_START */
94 : pfree(buffer);
95 :
96 : return NULL;
97 : /* LCOV_EXCL_STOP */
98 : }
99 :
100 4 : buffer->primitive_inverse = 0;
101 4 : buffer->error_locator = raw_buffer;
102 4 : buffer->syndrome = buffer->error_locator + (num_roots + 1);
103 4 : buffer->coefficients = buffer->syndrome + (num_roots + 1);
104 4 : buffer->polynomial = buffer->coefficients + (num_roots + 1);
105 4 : buffer->error_evaluator = buffer->polynomial + (num_roots + 1);
106 4 : buffer->error_roots = buffer->error_evaluator + (num_roots + 1);
107 4 : buffer->register_coefficients = buffer->error_roots + (num_roots + 1);
108 4 : buffer->error_locations = buffer->register_coefficients + (num_roots + 1);
109 :
110 1060 : for (i = 0; i < 8 * (num_roots + 1); i++) {
111 1056 : raw_buffer[i] = 0;
112 : }
113 :
114 4 : return buffer;
115 : }
116 :
117 4 : static inline void decoder_buffer_destroy(decoder_buffer_t *buffer)
118 : {
119 4 : pfree(buffer->error_locator);
120 4 : pfree(buffer);
121 4 : }
122 :
123 127676 : static inline uint8_t gf_mod(poporon_gf_t *gf, uint16_t value)
124 : {
125 146432 : while (value >= gf->field_size) {
126 18756 : value -= gf->field_size;
127 18756 : value = (value >> gf->symbol_size) + (value & gf->field_size);
128 : }
129 :
130 127676 : return value;
131 : }
132 :
133 : #endif /* POPORON_INTERNAL_H */
|