summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2021-01-21 20:48:28 +0100
committerDaniel Friesel <derf@finalrewind.org>2021-01-21 20:48:28 +0100
commit480ceca5f4e4a26d3d10eaf2360d548b1821ce41 (patch)
treee7307f256c8c76aa89bd6f3c9708a9ade999506f /src
parent9a8071e7e6c7c8c9f73bd54d32d2ce6b62b1ba8b (diff)
deflate: return number of bytes written to output
Diffstat (limited to 'src')
-rw-r--r--src/app/deflatetest/main.cc2
-rw-r--r--src/lib/deflate.cc33
2 files changed, 22 insertions, 13 deletions
diff --git a/src/app/deflatetest/main.cc b/src/app/deflatetest/main.cc
index 1fb394d..6829041 100644
--- a/src/app/deflatetest/main.cc
+++ b/src/app/deflatetest/main.cc
@@ -51,7 +51,7 @@ int main(void)
for (uint8_t i = 0; i < 5; i++) {
counter.start();
- int8_t ret = deflate_zlib((unsigned char*)deflate_input, sizeof(deflate_input), deflate_output, sizeof(deflate_output));
+ int16_t ret = deflate_zlib((unsigned char*)deflate_input, sizeof(deflate_input), deflate_output, sizeof(deflate_output));
counter.stop();
kout << "deflate returned " << ret << endl;
kout << "Output: " << (char*)deflate_output << endl;
diff --git a/src/lib/deflate.cc b/src/lib/deflate.cc
index 078dcbc..308da7c 100644
--- a/src/lib/deflate.cc
+++ b/src/lib/deflate.cc
@@ -400,11 +400,12 @@ static int8_t deflate_dynamic_huffman()
deflate_lld_lengths + hlit, hdist);
}
-int8_t deflate(unsigned char *input_buf, uint16_t input_len,
+int16_t deflate(unsigned char *input_buf, uint16_t input_len,
unsigned char *output_buf, uint16_t output_len)
{
uint8_t is_final = input_buf[0] & 0x01;
uint8_t block_type = (input_buf[0] & 0x06) >> 1;
+ int8_t ret;
#ifdef DEFLATE_DEBUG
kout << "is_final=" << is_final << " block_type=" << block_type << endl;
#endif
@@ -416,20 +417,28 @@ int8_t deflate(unsigned char *input_buf, uint16_t input_len,
deflate_output_now = output_buf;
deflate_output_end = output_buf + output_len;
- if (block_type == 0) {
- return deflate_uncompressed();
+ switch (block_type) {
+ case 0:
+ ret = deflate_uncompressed();
+ break;
+ case 1:
+ ret = deflate_static_huffman();
+ break;
+ case 2:
+ ret = deflate_dynamic_huffman();
+ break;
+ default:
+ return DEFLATE_ERR_BLOCK;
}
- if (block_type == 1) {
- return deflate_static_huffman();
- }
- if (block_type == 2) {
- return deflate_dynamic_huffman();
+
+ if (ret < 0) {
+ return ret;
}
- return DEFLATE_ERR_BLOCK;
+ return deflate_output_now - output_buf;
}
-int8_t deflate_zlib(unsigned char *input_buf, uint16_t input_len,
+int16_t deflate_zlib(unsigned char *input_buf, uint16_t input_len,
unsigned char *output_buf, uint16_t output_len)
{
if (input_len < 4) {
@@ -457,11 +466,11 @@ int8_t deflate_zlib(unsigned char *input_buf, uint16_t input_len,
return DEFLATE_ERR_FCHECK;
}
- uint8_t ret =
+ int16_t ret =
deflate(input_buf + 2, input_len - 2, output_buf, output_len);
#ifdef DEFLATE_CHECKSUM
- if (ret == 0) {
+ if (ret >= 0) {
uint16_t deflate_s1 = 1;
uint16_t deflate_s2 = 0;