Skip to content

Commit ee30aba

Browse files
added an example for lzss decoder
1 parent 64ad593 commit ee30aba

File tree

5 files changed

+93
-18
lines changed

5 files changed

+93
-18
lines changed

examples/lzssDecoder/lzssBuffer.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
/*
4+
* The following buffer is an hex dump of extras/test/test_files/test-1k.lzss
5+
* with the following command `hexdump extras/test/test_files/test-1k.lzss -e '32/1 "0x%02X, ""\n"'`
6+
*/
7+
8+
uint8_t lzssBuffer[] = {
9+
0x80, 0x3F, 0x7A, 0x40, 0x5F, 0xCC, 0x60, 0x4F, 0xEE, 0x30, 0x37, 0xFB, 0x18, 0x23, 0xFF, 0x8C, 0x14, 0x00, 0xC6, 0x0C, 0x00, 0xE3, 0x07, 0x00, 0xB1, 0x84, 0x00, 0x78, 0xC2, 0x40, 0x4C, 0x61,
10+
0x40, 0x2E, 0x30, 0xB0, 0x1B, 0x18, 0x60, 0x0F, 0x8C, 0x34, 0x08, 0xC6, 0x1C, 0x04, 0xE3, 0x0F, 0x02, 0xB1, 0x88, 0x01, 0x78, 0xC4, 0x40, 0xCC, 0x62, 0x40, 0x6E, 0x31, 0x30, 0x3B, 0x18, 0xA0,
11+
0x1F, 0x8C, 0x54, 0x10, 0xC6, 0x2C, 0x08, 0xE3, 0x17, 0x04, 0xB1, 0x8C, 0x02, 0x78, 0xC6, 0x41, 0x4C, 0x63, 0x40, 0xAE, 0x31, 0xB0, 0x5B, 0x18, 0xE0, 0x2F, 0x8C, 0x74, 0x18, 0xC6, 0x3C, 0x0C,
12+
0xE3, 0x1F, 0x06, 0xB1, 0x7E, 0xE2, 0x90, 0x83, 0x98, 0xC8, 0x81, 0xDC, 0x64, 0x60, 0xF6, 0x32, 0x40, 0x7F, 0x19, 0x28, 0x41, 0x8C, 0x98, 0x21, 0xC6, 0x4E, 0x11, 0x63, 0x28, 0x08, 0xF1, 0x94,
13+
0x84, 0x98, 0xCA, 0x82, 0x5C, 0x65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, 0x98, 0xF6, 0x8D, 0x5C, 0x7B, 0x66, 0xB6, 0x3D, 0xC3, 0x5F, 0x1E, 0xE9, 0xB1, 0x8F, 0x78, 0xD9, 0xC7, 0xBE, 0x6D, 0x63, 0xE0, 0x36, 0xF1, 0xF0, 0x9B,
31+
0x98, 0xF8, 0x8D, 0xDC, 0x7C, 0x66, 0xF6, 0x3E, 0x43, 0x7F, 0x1F, 0x29, 0xC1, 0x8F, 0x98, 0xE1, 0xC7, 0xCE, 0x71, 0x63, 0xE8, 0x38, 0xF1, 0xF4, 0x9C, 0x98, 0xFA, 0x8E, 0x5C, 0x7D, 0x67, 0x36,
32+
0x3E, 0xC3, 0x9F, 0x1F, 0x69, 0xD1, 0x8F, 0xB8, 0xE9, 0xC7, 0xDE, 0x75, 0x63, 0xF0, 0x3A, 0xF1, 0xF8, 0x9D, 0x98, 0xFC, 0x8E, 0xDC, 0x7E, 0x67, 0x76, 0x3F, 0x43, 0xBF, 0x1F, 0xA9, 0xE1, 0x8F,
33+
0xD8, 0xF1, 0xC7, 0xEE, 0x79, 0x63, 0xF8, 0x3C, 0xF1, 0xFC, 0x9E, 0x98, 0xFE, 0x8F, 0x5C, 0x7F, 0x67, 0xB6, 0x3F, 0xC3, 0xDF, 0x1F, 0xE9, 0xF1, 0x8F, 0xF8, 0xF9, 0xC7, 0xFE,
34+
};

examples/lzssDecoder/lzssDecoder.ino

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
12+
#include <Arduino_Lzss.h>
13+
#include "lzssBuffer.h"
14+
15+
void setup() {
16+
Serial.begin(9600);
17+
while(!Serial);
18+
19+
uint32_t value = 0;
20+
uint64_t position = 0;
21+
bool error = false;
22+
23+
// the lzss buffer provided as test, when decompressed, are made by incrementing a 4 byte integer
24+
// starting from 0, up to (file size-1)/4, thus it is easy to validate the correctness of the result
25+
auto decoder = arduino::lzss::Decoder([&value, &position, &error](const uint8_t b) {
26+
error = ((value >> ((3-(position%4))<<3)) & 0xFF) != b;
27+
28+
if(position % 4 == 0 && position != 0) {
29+
value++;
30+
}
31+
position++;
32+
});
33+
34+
for(size_t i = 0; i < sizeof(lzssBuffer); i++) {
35+
decoder.decompress(lzssBuffer + i, 1);
36+
37+
if(error) {
38+
Serial.println("Error during decompression of buffer");
39+
40+
return;
41+
}
42+
}
43+
44+
Serial.println("Decompression completed with success");
45+
}
46+
47+
void loop() { }
File renamed without changes.

src/lzss/decoder.h

+10-16
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515

1616
namespace arduino { namespace lzss {
1717

18-
enum status: uint8_t {
19-
DONE,
20-
IN_PROGRESS,
21-
NOT_COMPLETED
22-
};
23-
2418
constexpr int DefaultEI = 11; /* typically 10..13 */
2519
constexpr int DefaultEJ = 4; /* typically 4..5 */
2620
constexpr int DefaultN = (1 << DefaultEI); /* buffer size */
@@ -46,16 +40,16 @@ namespace arduino { namespace lzss {
4640
GenericDecoder(std::function<int()> getc_cbk, std::function<void(const uint8_t)> putc_cbk);
4741

4842
/**
49-
* this enum describes the result of the computation of a single FSM computation
50-
* DONE: the decompression is completed
51-
* IN_PROGRESS: the decompression cycle completed successfully, ready to compute next
52-
* NOT_COMPLETED: the current cycle didn't complete because the available data is not enough
53-
*/
54-
// enum status: uint8_t {
55-
// DONE,
56-
// IN_PROGRESS,
57-
// NOT_COMPLETED
58-
// };
43+
* this enum describes the result of the computation of a single FSM computation for LZSS decoder
44+
* DONE: the decompression is completed
45+
* IN_PROGRESS: the decompression cycle completed successfully, ready to compute next
46+
* NOT_COMPLETED: the current cycle didn't complete because the available data is not enough
47+
*/
48+
enum status: uint8_t {
49+
DONE,
50+
IN_PROGRESS,
51+
NOT_COMPLETED
52+
};
5953

6054
/**
6155
* decode the provided buffer until buffer ends, then pause the process

src/lzss/decoder.ipp

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ GenericDecoder<EI, EJ, N, F>::GenericDecoder(std::function<void(const uint8_t)>
4646
}
4747

4848
template<int EI, int EJ, int N, int F>
49-
status GenericDecoder<EI, EJ, N, F>::handle_state() {
49+
typename GenericDecoder<EI, EJ, N, F>::status GenericDecoder<EI, EJ, N, F>::handle_state() {
5050
status res = IN_PROGRESS;
5151

5252
int c = getbit(bits_required(this->state));
@@ -102,7 +102,7 @@ status GenericDecoder<EI, EJ, N, F>::handle_state() {
102102
}
103103

104104
template<int EI, int EJ, int N, int F>
105-
status GenericDecoder<EI, EJ, N, F>::decompress(uint8_t* const buffer, uint32_t size) {
105+
typename GenericDecoder<EI, EJ, N, F>::status GenericDecoder<EI, EJ, N, F>::decompress(uint8_t* const buffer, uint32_t size) {
106106
if(!get_char_cbk) {
107107
this->in_buffer = buffer;
108108
this->available += size;

0 commit comments

Comments
 (0)