29template <
int bytecount,
class block_t>
45 size_t write(
const uint8_t* data,
size_t len)
override{
47 for (
int j=0;j<len;j++){
61 size_t readBytes(uint8_t* data,
size_t len)
override{
83 int bits =
sizeof(block_t) * 8;
86 int messageBits = bits - log2(bits) - 1;
89 int block_ts = ceil((
float) bytecount / messageBits);
98 int encode(uint8_t *input,
int len) {
101 int bits =
sizeof(block_t) * 8;
104 int messageBits = bits - log2(bits) - 1;
107 int block_ts = ceil((
float) len / messageBits);
113 for (
int i = 0; i < block_ts+1; i++) {
116 block_t thisBlock = 0;
128 for (
int j = 0; j < bits; j++) {
131 if ((j & (j - 1)) == 0) {
141 int currentBit = i*messageBits + (j-skipped);
144 int currentChar = currentBit/(
sizeof(uint8_t)*8);
147 thisBit = currentBit < len*
sizeof(uint8_t)*8 ?
getCharBit(input[currentChar], currentBit-currentChar*8) : 0;
151 thisBit =
getBit(len/8, j-skipped+(
sizeof(block_t)*8-messageBits));
161 thisBlock =
modifyBit(thisBlock, j, thisBit);
168 for (
int k = 1; k < skipped; k++) {
171 if (
getBit(parityBits,
sizeof(block_t)*8-skipped+k)) {
176 thisBlock =
modifyBit(thisBlock, (
int) pow(2, skipped-k-1) ,
getBit(parityBits,
sizeof(block_t)*8-skipped+k));
180 thisBlock =
modifyBit(thisBlock, 0, onCount & 1);
191 uint8_t* output = (uint8_t*)
raw.
data();
194 int bits =
sizeof(block_t) * 8;
196 for (
int b = 0; b < (len/
sizeof(block_t)); b++) {
205 for (
int i = 1; i < bits; i++) {
207 if (
getBit(input[b], i)) {
219 if (!(onCount & 1 ^
getBit(input[b], 0))) {
220 LOGE(
"\nMore than one error detected. Aborting.\n");
226 input[b] =
toggleBit(input[b], (bits-1) - errorLoc);
232 int messageBits = bits - log2(bits) - 1;
234 int currentBit, currentChar;
238 for (
int i = 0; i < len/
sizeof(block_t); i++) {
244 for (
int j = 0; j < bits; j++) {
247 if ((j & (j - 1)) == 0) {
253 currentBit = i*messageBits + (j-skipped);
256 currentChar = currentBit/(
sizeof(uint8_t)*8);
259 bool thisBit =
getBit(input[i], j);
261 if (i != len/
sizeof(block_t)-1) {
264 output[currentChar] =
modifyCharBit(output[currentChar], currentBit-currentChar*
sizeof(uint8_t)*8, thisBit);
268 uint8_ts =
modifyBit(uint8_ts, j-skipped+(
sizeof(block_t)*8-messageBits), thisBit);
279 int val = indicies[0];
280 for (
int i = 1; i < len; i++) {
281 val = val ^ indicies[i];
287 return ((n & ~(1 << (
sizeof(block_t)*8-1-p))) | (b << (
sizeof(block_t)*8-1-p)));
291 return ((n & ~(1 << (
sizeof(uint8_t)*8-1-p))) | (b << (
sizeof(uint8_t)*8-1-p)));
295 return (b << i) & (int) pow(2, (
sizeof(block_t)*8-1));
299 return (b << i) & (int) pow(2, (
sizeof(uint8_t)*8-1));
306 int inList(uint8_t **list, uint8_t *testString,
int listLen) {
307 for (
int i = 0; i < listLen; i++) {
308 if (strcmp(list[i], testString) == 0) {
#define LOGE(...)
Definition AudioLoggerIDF.h:30
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
virtual size_t readBytes(uint8_t *data, size_t len)
Definition Arduino.h:140