Skip to content

Commit

Permalink
Add defines for lz commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ariscop authored and Rangi42 committed Mar 8, 2023
1 parent 2d48f4f commit 8444a78
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
12 changes: 6 additions & 6 deletions tools/lz/dpcomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void process_input(void) {
unsigned char current_byte = data[plen - 1];
for (unsigned prev = plen > MAX_COMMAND_COUNT ? plen - MAX_COMMAND_COUNT : 0; prev < plen; prev++) {
consider(plen, (struct command) {
.command = 0,
.command = LZ_DATA,
.count = plen - prev,
.value = prev,
});
Expand All @@ -68,7 +68,7 @@ void process_input(void) {
do {
count++;
consider(plen, (struct command) {
.command = current_byte ? 1 : 3,
.command = current_byte ? LZ_REPEAT : LZ_ZERO,
.count = count,
.value = current_byte,
});
Expand All @@ -79,7 +79,7 @@ void process_input(void) {
do {
count++;
consider(plen, (struct command) {
.command = 2,
.command = LZ_ALTERNATE,
.count = count,
.value = (data[plen - count + 1] << 8) | (data[plen - count]),
});
Expand All @@ -90,7 +90,7 @@ void process_input(void) {
unsigned k = min(MAX_COMMAND_COUNT, match_right(plen - 1, at));
for (unsigned i = 2; i <= k; i++) {
consider(plen + i - 1, (struct command) {
.command = 4,
.command = LZ_COPY_NORMAL,
.count = i,
.value = encode_delta(plen - 1, at),
});
Expand All @@ -99,7 +99,7 @@ void process_input(void) {
k = min(MAX_COMMAND_COUNT, match_left(plen - 1, at));
for (unsigned i = 2; i <= k; i++) {
consider(plen + i - 1, (struct command) {
.command = 6,
.command = LZ_COPY_REVERSED,
.count = i,
.value = encode_delta(plen - 1, at),
});
Expand All @@ -108,7 +108,7 @@ void process_input(void) {
k = min(MAX_COMMAND_COUNT, match_flipped(plen - 1, at));
for (unsigned i = 2; i <= k; i++) {
consider(plen + i - 1, (struct command) {
.command = 5,
.command = LZ_COPY_FLIPPED,
.count = i,
.value = encode_delta(plen - 1, at),
});
Expand Down
20 changes: 11 additions & 9 deletions tools/lz/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,29 @@ void write_command_to_textfile (FILE * fp, struct command command, const unsigne
int rv, pos;
const char * kind;
switch (command.command) {
case 0:
case LZ_DATA:
if ((rv = fprintf(fp, "\tlzdata")) < 0) break;
for (pos = 0; pos < command.count; pos ++) if ((rv = fprintf(fp, "%s$%02hhx", pos ? ", " : " ", input_stream[command.value + pos])) < 0) break;
rv = putc('\n', fp);
break;
case 1:
case LZ_REPEAT:
if ((command.value < 0) || (command.value > 255)) error_exit(2, "invalid command in output stream");
rv = fprintf(fp, "\tlzrepeat %u, $%02hhx\n", command.count, (unsigned char) command.value);
break;
case 2:
case LZ_ALTERNATE:
if (command.value < 0) error_exit(2, "invalid command in output stream");
rv = fprintf(fp, "\tlzrepeat %u, $%02hhx, $%02hhx\n", command.count, (unsigned char) command.value, (unsigned char) (command.value >> 8));
break;
case 3:
case LZ_ZERO:
rv = fprintf(fp, "\tlzzero %u\n", command.count);
break;
case 4:
case LZ_COPY_NORMAL:
kind = "normal";
goto copy;
case 5:
case LZ_COPY_FLIPPED:
kind = "flipped";
goto copy;
case 6:
case LZ_COPY_REVERSED:
kind = "reversed";
copy:
if ((command.value < -LOOKBACK_LIMIT) || (command.value >= MAX_FILE_SIZE)) error_exit(2, "invalid command in output stream");
Expand Down Expand Up @@ -113,10 +113,12 @@ void write_command_to_file (FILE * fp, struct command command, const unsigned ch
*(pos ++) = command.count;
}
switch (command.command) {
case 1: case 2:
case LZ_REPEAT:
case LZ_ALTERNATE:
if ((command.value < 0) || (command.value >= (1 << (command.command << 3)))) error_exit(2, "invalid command in output stream");
for (n = 0; n < command.command; n ++) *(pos ++) = command.value >> (n << 3);
case 0: case 3:
case LZ_DATA:
case LZ_ZERO:
break;
default:
if ((command.value < -LOOKBACK_LIMIT) || (command.value >= MAX_FILE_SIZE)) error_exit(2, "invalid command in output stream");
Expand Down
9 changes: 9 additions & 0 deletions tools/lz/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
#define MAX_COMMAND_COUNT 512
#define LOOKBACK_LIMIT 128 /* highest negative valid count for a copy command */

#define LZ_DATA 0 /* Read literal data for n bytes. */
#define LZ_REPEAT 1 /* Write the same byte for n bytes. */
#define LZ_ALTERNATE 2 /* Alternate two bytes for n bytes. */
#define LZ_ZERO 3 /* Write 0 for n bytes. */
#define LZ_COPY_NORMAL 4 /* Repeat n bytes from the offset. */
#define LZ_COPY_FLIPPED 5 /* Repeat n bitflipped bytes. */
#define LZ_COPY_REVERSED 6 /* Repeat n bytes in reverse. */
#define LZ_LONG 7 /* Expand n to 9 bits */

#if __STDC_VERSION__ >= 201112L
// <noreturn.h> forces "noreturn void", which is silly and redundant; this is simpler
#define noreturn _Noreturn void
Expand Down
22 changes: 12 additions & 10 deletions tools/lz/uncomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ struct command * get_commands_from_file (const unsigned char * data, unsigned sh
if (!(remaining --)) goto error;
current -> command = *rp >> 5;
current -> count = *(rp ++) & 31;
if (current -> command == 7) {
if (current -> command == LZ_LONG) {
current -> command = current -> count >> 2;
current -> count = (current -> count & 3) << 8;
if (current -> command == 7) {
if (current -> command == LZ_LONG) {
// long commands inside long commands are not allowed, but if the count is 0x300 here, it means that the original byte was 0xff
if (current -> count == 0x300) break;
goto error;
Expand All @@ -22,14 +22,15 @@ struct command * get_commands_from_file (const unsigned char * data, unsigned sh
}
current -> count ++;
switch (current -> command) {
case 0:
case LZ_DATA:
if (remaining <= current -> count) goto error;
current -> value = rp - data;
rp += current -> count;
remaining -= current -> count;
case 3:
case LZ_ZERO:
break;
case 1: case 2: {
case LZ_REPEAT:
case LZ_ALTERNATE: {
unsigned char p;
if (remaining <= current -> command) goto error;
current -> value = 0;
Expand Down Expand Up @@ -62,22 +63,23 @@ unsigned char * get_uncompressed_data (const struct command * commands, const un
unsigned short p;
for (; commands < limit; commands ++) {
switch (commands -> command) {
case 0:
case LZ_DATA:
memcpy(current, compressed + commands -> value, commands -> count);
current += commands -> count;
break;
case 1: case 2:
case LZ_REPEAT:
case LZ_ALTERNATE:
for (p = 0; p < commands -> count; p ++) *(current ++) = commands -> value >> ((p % commands -> command) << 3);
break;
case 3:
case LZ_ZERO:
memset(current, 0, commands -> count);
current += commands -> count;
break;
default: {
const unsigned char * ref = ((commands -> value < 0) ? current : result) + commands -> value;
for (p = 0; p < commands -> count; p ++) {
current[p] = ref[(commands -> command == 6) ? -(int) p : p];
if (commands -> command == 5) current[p] = bit_flipping_table[current[p]];
current[p] = ref[(commands -> command == LZ_COPY_REVERSED) ? -(int) p : p];
if (commands -> command == LZ_COPY_FLIPPED) current[p] = bit_flipping_table[current[p]];
}
current += commands -> count;
}
Expand Down

0 comments on commit 8444a78

Please sign in to comment.