From 4cd85123996a0fe350b3a081a00c30f53e0bc7a1 Mon Sep 17 00:00:00 2001 From: neauoire Date: Wed, 15 Nov 2023 14:21:05 -0800 Subject: [PATCH] Cleanup --- cli/lz/ulzdec.c | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/cli/lz/ulzdec.c b/cli/lz/ulzdec.c index 73b2f22..409f8e3 100644 --- a/cli/lz/ulzdec.c +++ b/cli/lz/ulzdec.c @@ -1,9 +1,6 @@ #include #include -char *ptr; -char *mem; - /* cc ulzdec.c -o ulzdec && ./ulzdec compressed.bin */ static int @@ -13,28 +10,25 @@ error(const char *name, const char *msg) return 0; } +char *mem, *ptr; + void -decode_byte(FILE *fp, char c) +decode_ulz(FILE *fp) { - short i, length, offset; - /* DICT */ - if(c & 0x80) { - if(c & 0x40) - length = (c & 0x3f) << 8 | getc(fp); - else - length = c & 0x3f; - length += 4, offset = getc(fp) + 1; - printf("DICT %04x, %02x\n", length, offset); - } - /* LIT */ else { - length = c + 1; - printf("LIT%02x ", length); - for(i = 0; i < length; ++i) { - unsigned char c2 = getc(fp); - printf("%c", c2 == '\n' ? ' ' : c2); - *(ptr++) = c2; - } - printf("\n"); + char *dict; + short i, length; + while((c = getc(src)) != EOF) { + if(c & 0x80) { /* DICT */ + if(c & 0x40) + length = (c & 0x3f) << 8 | getc(src); + else + length = c & 0x3f; + dict = ptr - (getc(src) + 1); + for(i = 0; i < length + 4; i++) + *(ptr++) = *(dict++); + } else /* LIT */ + for(i = 0; i < c + 1; i++) + *(ptr++) = getc(src); } } @@ -51,8 +45,7 @@ main(int argc, char *argv[]) return !error("Invalid input", argv[1]); mem = malloc(0x10000); ptr = mem; - while((c = getc(src)) != EOF) - decode_byte(src, c); + decode_ulz(src); *(ptr) = 0; printf("%s\n", mem); return 0;