Improved ulzdec

This commit is contained in:
neauoire 2023-11-15 13:15:07 -08:00
parent 913616002c
commit 165783bb1e
1 changed files with 21 additions and 14 deletions

View File

@ -1,6 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
char *ptr;
char *mem;
/* cc ulzdec.c -o ulzdec && ./ulzdec compressed.bin */
static int
@ -11,24 +14,25 @@ error(const char *name, const char *msg)
}
void
decode_byte(FILE *fp, char *res, unsigned char c)
decode_byte(FILE *fp, char c)
{
short i, length, offset;
/* DICT */
if(c & 0x80) {
if(c & 0x40) { /* DICT3 */
unsigned short length = ((c & 0x3f) << 8 | getc(fp)) + 4;
unsigned char offset = getc(fp) + 1;
printf("DIC3 %04x, %02x\n", length, offset);
} else { /* DICT2 */
unsigned char length = (c & 0x3f) + 4;
unsigned char offset = getc(fp) + 1;
printf("DIC1 %02x, %02x\n", length, offset);
}
} else { /* LIT */
unsigned char i, length = c + 1;
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");
}
@ -45,8 +49,11 @@ main(int argc, char *argv[])
return error("usage", "ulzdec [-v] compressed.bin");
if(!(src = fopen(argv[1], "rb")))
return !error("Invalid input", argv[1]);
res = malloc(0x10000);
mem = malloc(0x10000);
ptr = mem;
while((c = getc(src)) != EOF)
decode_byte(src, res, c);
decode_byte(src, c);
*(ptr) = 0;
printf("%s\n", mem);
return 0;
}