diff --git a/src/decompress/lzss.cpp b/src/decompress/lzss.cpp index 3eba067..fc96a5f 100644 --- a/src/decompress/lzss.cpp +++ b/src/decompress/lzss.cpp @@ -5,38 +5,16 @@ #include "lzss.h" #include -#include - -/************************************************************************************** - DEFINE - **************************************************************************************/ - -#define EI 11 /* typically 10..13 */ -#define EJ 4 /* typically 4..5 */ -#define P 1 /* If match length <= P then output one character */ -#define N (1 << EI) /* buffer size */ -#define F ((1 << EJ) + 1) /* lookahead buffer size */ - -#define LZSS_EOF (-1) - -#define FPUTC_BUF_SIZE (64) -#define FGETC_BUF_SIZE (64) /************************************************************************************** GLOBAL VARIABLES **************************************************************************************/ -static uint32_t LZSS_FILE_SIZE = 0; -static FILE * update_file = 0; -static FILE * target_file = 0; - -int bit_buffer = 0, bit_mask = 128; -unsigned char buffer[N * 2]; +static FILE * update_file = nullptr; +static FILE * target_file = nullptr; -static char write_buf[FPUTC_BUF_SIZE]; -static size_t write_buf_num_bytes = 0; -static size_t bytes_written_fputc = 0; -static ArduinoPortentaOtaWatchdogResetFuncPointer wdog_feed_func = 0; +static ArduinoPortentaOtaWatchdogResetFuncPointer wdog_feed_func = nullptr; +static LZSSDecoder* decoder = nullptr; /************************************************************************************** PUBLIC FUNCTIONS @@ -57,122 +35,31 @@ void lzss_init( } decoder = new LZSSDecoder( - [](const uint8_t c) { + [target_file](const uint8_t c) { fwrite(&c, 1, 1, target_file); } ); } -void lzss_flush() -{ - bytes_written_fputc += write_buf_num_bytes; - - if (wdog_feed_func) - wdog_feed_func(); - - fwrite(write_buf, 1, write_buf_num_bytes, target_file); - - write_buf_num_bytes = 0; -} - -/************************************************************************************** - PRIVATE FUNCTIONS - **************************************************************************************/ - -void lzss_fputc(int const c) -{ - /* Buffer the decompressed data into a buffer so - * we can perform block writes and don't need to - * write every byte singly on the flash (which - * wouldn't be possible anyway). - */ - write_buf[write_buf_num_bytes] = static_cast(c); - write_buf_num_bytes++; - - /* The write buffer is full of decompressed - * data, write it to the flash now. - */ - if (write_buf_num_bytes == FPUTC_BUF_SIZE) - lzss_flush(); -} - -int lzss_fgetc() -{ - static uint8_t read_buf[FGETC_BUF_SIZE]; - static size_t read_buf_pos = FGETC_BUF_SIZE; - static size_t bytes_read_fgetc = 0; - static size_t bytes_read_from_modem = 0; - - /* lzss_file_size is set within SSUBoot:main - * and contains the size of the LZSS file. Once - * all those bytes have been read its time to return - * LZSS_EOF in order to signal that the end of - * the file has been reached. - */ - if (bytes_read_fgetc == LZSS_FILE_SIZE) - return LZSS_EOF; - - /* If there is no data left to be read from the read buffer - * than read a new block and store it into the read buffer. - */ - if (read_buf_pos == FGETC_BUF_SIZE) - { - /* Read the next block from the flash memory. */ - bytes_read_from_modem += fread(read_buf, 1, FGETC_BUF_SIZE, update_file); - /* Reset the read buffer position. */ - read_buf_pos = 0; - } - - uint8_t const c = read_buf[read_buf_pos]; - read_buf_pos++; - bytes_read_fgetc++; - - return c; +void lzss_flush() { + fflush(target_file); } -/************************************************************************************** - LZSS FUNCTIONS - **************************************************************************************/ - -int getbit(int n) /* get n bits */ -{ - int i, x; - static int buf, mask = 0; - - x = 0; - for (i = 0; i < n; i++) { - if (mask == 0) { - if ((buf = lzss_fgetc()) == LZSS_EOF) return LZSS_EOF; - mask = 128; - } - x <<= 1; - if (buf & mask) x++; - mask >>= 1; +void lzss_decode() { + if(decoder == nullptr) { + return; } - return x; -} + const size_t buf_size = 64; + uint8_t buffer[buf_size]; + size_t res = 0; -void lzss_decode(void) -{ - int i, j, k, r, c; - - for (i = 0; i < N - F; i++) buffer[i] = ' '; - r = N - F; - while ((c = getbit(1)) != LZSS_EOF) { - if (c) { - if ((c = getbit(8)) == LZSS_EOF) break; - lzss_fputc(c); - buffer[r++] = c; r &= (N - 1); - } else { - if ((i = getbit(EI)) == LZSS_EOF) break; - if ((j = getbit(EJ)) == LZSS_EOF) break; - for (k = 0; k <= j + 1; k++) { - c = buffer[(i + k) & (N - 1)]; - lzss_fputc(c); - buffer[r++] = c; r &= (N - 1); - } + do { + if(wdog_feed_func) { + wdog_feed_func(); } - } + res = fread(buffer, sizeof(uint8_t), buf_size, update_file); + decoder->decompress(buffer, res); + } while(res == buf_size); }