diff --git a/tools/lz/dpcomp.c b/tools/lz/dpcomp.c index ec6bce2248..19d904ff66 100644 --- a/tools/lz/dpcomp.c +++ b/tools/lz/dpcomp.c @@ -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, }); @@ -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, }); @@ -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]), }); @@ -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), }); @@ -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), }); @@ -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), }); diff --git a/tools/lz/output.c b/tools/lz/output.c index 3cef7b6454..053a5dce64 100644 --- a/tools/lz/output.c +++ b/tools/lz/output.c @@ -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"); @@ -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"); diff --git a/tools/lz/proto.h b/tools/lz/proto.h index 5b9ff10ca2..b31fdd45ba 100644 --- a/tools/lz/proto.h +++ b/tools/lz/proto.h @@ -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 // forces "noreturn void", which is silly and redundant; this is simpler #define noreturn _Noreturn void diff --git a/tools/lz/uncomp.c b/tools/lz/uncomp.c index b22fc75f65..0e5b078ed6 100644 --- a/tools/lz/uncomp.c +++ b/tools/lz/uncomp.c @@ -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; @@ -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; @@ -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; }