Skip to content

Commit 8e94a4f

Browse files
adapted old code to use LZSSDecoder
1 parent 34f9657 commit 8e94a4f

File tree

1 file changed

+19
-132
lines changed

1 file changed

+19
-132
lines changed

src/decompress/lzss.cpp

Lines changed: 19 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,16 @@
55
#include "lzss.h"
66

77
#include <stdlib.h>
8-
#include <stdint.h>
9-
10-
/**************************************************************************************
11-
DEFINE
12-
**************************************************************************************/
13-
14-
#define EI 11 /* typically 10..13 */
15-
#define EJ 4 /* typically 4..5 */
16-
#define P 1 /* If match length <= P then output one character */
17-
#define N (1 << EI) /* buffer size */
18-
#define F ((1 << EJ) + 1) /* lookahead buffer size */
19-
20-
#define LZSS_EOF (-1)
21-
22-
#define FPUTC_BUF_SIZE (64)
23-
#define FGETC_BUF_SIZE (64)
248

259
/**************************************************************************************
2610
GLOBAL VARIABLES
2711
**************************************************************************************/
2812

29-
static uint32_t LZSS_FILE_SIZE = 0;
30-
static FILE * update_file = 0;
31-
static FILE * target_file = 0;
32-
33-
int bit_buffer = 0, bit_mask = 128;
34-
unsigned char buffer[N * 2];
13+
static FILE * update_file = nullptr;
14+
static FILE * target_file = nullptr;
3515

36-
static char write_buf[FPUTC_BUF_SIZE];
37-
static size_t write_buf_num_bytes = 0;
38-
static size_t bytes_written_fputc = 0;
39-
static ArduinoPortentaOtaWatchdogResetFuncPointer wdog_feed_func = 0;
16+
static ArduinoPortentaOtaWatchdogResetFuncPointer wdog_feed_func = nullptr;
17+
static LZSSDecoder* decoder = nullptr;
4018

4119
/**************************************************************************************
4220
PUBLIC FUNCTIONS
@@ -57,122 +35,31 @@ void lzss_init(
5735
}
5836

5937
decoder = new LZSSDecoder(
60-
[](const uint8_t c) {
38+
[target_file](const uint8_t c) {
6139
fwrite(&c, 1, 1, target_file);
6240
}
6341
);
6442
}
6543

66-
void lzss_flush()
67-
{
68-
bytes_written_fputc += write_buf_num_bytes;
69-
70-
if (wdog_feed_func)
71-
wdog_feed_func();
72-
73-
fwrite(write_buf, 1, write_buf_num_bytes, target_file);
74-
75-
write_buf_num_bytes = 0;
76-
}
77-
78-
/**************************************************************************************
79-
PRIVATE FUNCTIONS
80-
**************************************************************************************/
81-
82-
void lzss_fputc(int const c)
83-
{
84-
/* Buffer the decompressed data into a buffer so
85-
* we can perform block writes and don't need to
86-
* write every byte singly on the flash (which
87-
* wouldn't be possible anyway).
88-
*/
89-
write_buf[write_buf_num_bytes] = static_cast<char>(c);
90-
write_buf_num_bytes++;
91-
92-
/* The write buffer is full of decompressed
93-
* data, write it to the flash now.
94-
*/
95-
if (write_buf_num_bytes == FPUTC_BUF_SIZE)
96-
lzss_flush();
97-
}
98-
99-
int lzss_fgetc()
100-
{
101-
static uint8_t read_buf[FGETC_BUF_SIZE];
102-
static size_t read_buf_pos = FGETC_BUF_SIZE;
103-
static size_t bytes_read_fgetc = 0;
104-
static size_t bytes_read_from_modem = 0;
105-
106-
/* lzss_file_size is set within SSUBoot:main
107-
* and contains the size of the LZSS file. Once
108-
* all those bytes have been read its time to return
109-
* LZSS_EOF in order to signal that the end of
110-
* the file has been reached.
111-
*/
112-
if (bytes_read_fgetc == LZSS_FILE_SIZE)
113-
return LZSS_EOF;
114-
115-
/* If there is no data left to be read from the read buffer
116-
* than read a new block and store it into the read buffer.
117-
*/
118-
if (read_buf_pos == FGETC_BUF_SIZE)
119-
{
120-
/* Read the next block from the flash memory. */
121-
bytes_read_from_modem += fread(read_buf, 1, FGETC_BUF_SIZE, update_file);
122-
/* Reset the read buffer position. */
123-
read_buf_pos = 0;
124-
}
125-
126-
uint8_t const c = read_buf[read_buf_pos];
127-
read_buf_pos++;
128-
bytes_read_fgetc++;
129-
130-
return c;
44+
void lzss_flush() {
45+
fflush(target_file);
13146
}
13247

133-
/**************************************************************************************
134-
LZSS FUNCTIONS
135-
**************************************************************************************/
136-
137-
int getbit(int n) /* get n bits */
138-
{
139-
int i, x;
140-
static int buf, mask = 0;
141-
142-
x = 0;
143-
for (i = 0; i < n; i++) {
144-
if (mask == 0) {
145-
if ((buf = lzss_fgetc()) == LZSS_EOF) return LZSS_EOF;
146-
mask = 128;
147-
}
148-
x <<= 1;
149-
if (buf & mask) x++;
150-
mask >>= 1;
48+
void lzss_decode() {
49+
if(decoder == nullptr) {
50+
return;
15151
}
152-
return x;
153-
}
52+
const size_t buf_size = 64;
53+
uint8_t buffer[buf_size];
54+
size_t res = 0;
15455

155-
void lzss_decode(void)
156-
{
157-
int i, j, k, r, c;
158-
159-
for (i = 0; i < N - F; i++) buffer[i] = ' ';
160-
r = N - F;
161-
while ((c = getbit(1)) != LZSS_EOF) {
162-
if (c) {
163-
if ((c = getbit(8)) == LZSS_EOF) break;
164-
lzss_fputc(c);
165-
buffer[r++] = c; r &= (N - 1);
166-
} else {
167-
if ((i = getbit(EI)) == LZSS_EOF) break;
168-
if ((j = getbit(EJ)) == LZSS_EOF) break;
169-
for (k = 0; k <= j + 1; k++) {
170-
c = buffer[(i + k) & (N - 1)];
171-
lzss_fputc(c);
172-
buffer[r++] = c; r &= (N - 1);
173-
}
56+
do {
57+
if(wdog_feed_func) {
58+
wdog_feed_func();
17459
}
175-
}
60+
res = fread(buffer, sizeof(uint8_t), buf_size, update_file);
61+
decoder->decompress(buffer, res);
62+
} while(res == buf_size);
17663
}
17764

17865

0 commit comments

Comments
 (0)