5
5
#include " lzss.h"
6
6
7
7
#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 )
24
8
25
9
/* *************************************************************************************
26
10
GLOBAL VARIABLES
27
11
**************************************************************************************/
28
12
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 ;
35
15
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 ;
40
18
41
19
/* *************************************************************************************
42
20
PUBLIC FUNCTIONS
@@ -57,122 +35,31 @@ void lzss_init(
57
35
}
58
36
59
37
decoder = new LZSSDecoder (
60
- [](const uint8_t c) {
38
+ [target_file ](const uint8_t c) {
61
39
fwrite (&c, 1 , 1 , target_file);
62
40
}
63
41
);
64
42
}
65
43
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);
131
46
}
132
47
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 ;
151
51
}
152
- return x;
153
- }
52
+ const size_t buf_size = 64 ;
53
+ uint8_t buffer[buf_size];
54
+ size_t res = 0 ;
154
55
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 ();
174
59
}
175
- }
60
+ res = fread (buffer, sizeof (uint8_t ), buf_size, update_file);
61
+ decoder->decompress (buffer, res);
62
+ } while (res == buf_size);
176
63
}
177
64
178
65
0 commit comments