Skip to content

Commit 2e7d18d

Browse files
add lz4hc compression
1 parent fa62060 commit 2e7d18d

6 files changed

Lines changed: 47 additions & 11 deletions

File tree

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@ SOURCES = $(SRCDIR)/appvar.c \
7575
$(DEPDIR)/zx/zx7/compress.c \
7676
$(DEPDIR)/zx/zx0/compress.c \
7777
$(DEPDIR)/libyaml/src/api.c \
78-
$(DEPDIR)/lz4/lib/lz4.c \
7978
$(DEPDIR)/libyaml/src/dumper.c \
8079
$(DEPDIR)/libyaml/src/loader.c \
8180
$(DEPDIR)/libyaml/src/parser.c \
8281
$(DEPDIR)/libyaml/src/reader.c \
8382
$(DEPDIR)/libyaml/src/scanner.c \
83+
$(DEPDIR)/lz4/lib/lz4.c \
84+
$(DEPDIR)/lz4/lib/lz4hc.c \
8485
$(DEPDIR)/tinycthread/source/tinycthread.c
8586

8687
ifeq ($(OS),Windows_NT)

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,15 @@ It primarily is used for the TI-84+CE and related calculator series, however can
343343
These different compression modes can be used to achieve different results.
344344
Some provide better compression ratio at the cost of slower compression,
345345
and potentially slower decompression as well.
346+
The following are ranked from best to worst exprected compression ratio.
346347

347-
"zx0" : Best ratio, slowest (de)compression
348+
"zx0" : Slowest compression time
348349

349-
"zx7" : Moderate ratio, moderate (de)compression
350+
"zx7" : Moderate compression time
350351

351-
"lz4" : Worst ratio, fastest (de)compression
352+
"lz4hc" : Moderate compression time
353+
354+
"lz4" : Fastest compression time
352355

353356
--------------------------------------------------------------------------------
354357

src/compress.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
#include "deps/zx/zx7/zx7.h"
3636
#include "deps/zx/zx0/zx0.h"
3737
#include "deps/lz4/lib/lz4.h"
38+
#include "deps/lz4/lib/lz4hc.h"
3839

40+
#include <stdbool.h>
3941
#include <string.h>
4042

4143
#define LZ4_SIZE_PREFIX_BYTES 3
@@ -98,7 +100,7 @@ static uint8_t *compress_zx0(void *data, size_t *size)
98100
return compressed_data;
99101
}
100102

101-
static uint8_t *compress_lz4(void *data, size_t *size)
103+
static uint8_t *compress_lz4_block(void *data, size_t *size, bool high_compression)
102104
{
103105
uint8_t *compressed_data;
104106
const char *input;
@@ -130,8 +132,16 @@ static uint8_t *compress_lz4(void *data, size_t *size)
130132
return NULL;
131133
}
132134

133-
compressed_size = LZ4_compress_default(input, (char *)(compressed_data + LZ4_SIZE_PREFIX_BYTES),
134-
input_size, max_compressed_size);
135+
if (high_compression)
136+
{
137+
compressed_size = LZ4_compress_HC(input, (char *)(compressed_data + LZ4_SIZE_PREFIX_BYTES),
138+
input_size, max_compressed_size, LZ4HC_CLEVEL_MAX);
139+
}
140+
else
141+
{
142+
compressed_size = LZ4_compress_default(input, (char *)(compressed_data + LZ4_SIZE_PREFIX_BYTES),
143+
input_size, max_compressed_size);
144+
}
135145

136146
if (compressed_size <= 0)
137147
{
@@ -152,13 +162,24 @@ static uint8_t *compress_lz4(void *data, size_t *size)
152162
compressed_data[2] = (uint8_t)((compressed_size >> 16) & 0xFF);
153163

154164
total_size = (size_t)compressed_size + LZ4_SIZE_PREFIX_BYTES;
155-
LOG_DEBUG("Compressed size: %zu -> %zu (lz4 block)\n", orig_size, total_size);
165+
LOG_DEBUG("Compressed size: %zu -> %zu (lz4%s block)\n",
166+
orig_size, total_size, high_compression ? "hc" : "");
156167

157168
*size = total_size;
158169

159170
return compressed_data;
160171
}
161172

173+
static uint8_t *compress_lz4(void *data, size_t *size)
174+
{
175+
return compress_lz4_block(data, size, false);
176+
}
177+
178+
static uint8_t *compress_lz4hc(void *data, size_t *size)
179+
{
180+
return compress_lz4_block(data, size, true);
181+
}
182+
162183
uint8_t *compress_array(uint8_t *data, size_t *size, compress_mode_t mode)
163184
{
164185
switch (mode)
@@ -172,6 +193,9 @@ uint8_t *compress_array(uint8_t *data, size_t *size, compress_mode_t mode)
172193
case COMPRESS_LZ4:
173194
return compress_lz4(data, size);
174195

196+
case COMPRESS_LZ4HC:
197+
return compress_lz4hc(data, size);
198+
175199
default:
176200
return NULL;
177201
}

src/compress.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ typedef enum
4444
COMPRESS_ZX7,
4545
COMPRESS_ZX0,
4646
COMPRESS_LZ4,
47+
COMPRESS_LZ4HC,
4748
} compress_mode_t;
4849

4950
uint8_t *compress_array(uint8_t *data, size_t *size, compress_mode_t mode);

src/options.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,15 @@ static void options_show(const char *prgm)
383383
LOG_PRINT(" These different compression modes can be used to achieve different results.\n");
384384
LOG_PRINT(" Some provide better compression ratio at the cost of slower compression,\n");
385385
LOG_PRINT(" and potentially slower decompression as well.\n");
386+
LOG_PRINT(" The following are ranked from best to worst exprected compression ratio.\n");
386387
LOG_PRINT("\n");
387-
LOG_PRINT(" \"zx0\" : Best ratio, slowest (de)compression\n");
388+
LOG_PRINT(" \"zx0\" : Slowest compression time\n");
388389
LOG_PRINT("\n");
389-
LOG_PRINT(" \"zx7\" : Moderate ratio, moderate (de)compression\n");
390+
LOG_PRINT(" \"zx7\" : Moderate compression time\n");
390391
LOG_PRINT("\n");
391-
LOG_PRINT(" \"lz4\" : Worst ratio, fastest (de)compression\n");
392+
LOG_PRINT(" \"lz4hc\" : Moderate compression time\n");
393+
LOG_PRINT("\n");
394+
LOG_PRINT(" \"lz4\" : Fastest compression time\n");
392395
LOG_PRINT("\n");
393396
LOG_PRINT("--------------------------------------------------------------------------------\n");
394397
LOG_PRINT("\n");

src/parser.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ static compress_mode_t parse_compression_mode(void *value)
7676
{
7777
return COMPRESS_LZ4;
7878
}
79+
else if (parse_str_cmp("lz4hc", value))
80+
{
81+
return COMPRESS_LZ4HC;
82+
}
7983

8084
return COMPRESS_NONE;
8185
}

0 commit comments

Comments
 (0)