uxn-utils/cli/lz/ulzdec.c

52 lines
1.0 KiB
C
Raw Normal View History

2023-11-15 15:06:28 -05:00
#include <stdio.h>
#include <stdlib.h>
/* cc ulzdec.c -o ulzdec && ./ulzdec compressed.bin */
static int
error(const char *name, const char *msg)
{
fprintf(stderr, "%s: %s\n", name, msg);
return 0;
}
2023-11-15 17:21:05 -05:00
char *mem, *ptr;
2023-11-15 15:06:28 -05:00
void
2023-11-15 17:21:05 -05:00
decode_ulz(FILE *fp)
2023-11-15 15:06:28 -05:00
{
2023-11-15 17:21:05 -05:00
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);
2023-11-15 15:06:28 -05:00
}
}
int
main(int argc, char *argv[])
{
char c, *res;
FILE *src;
if(argc == 2 && argv[1][0] == '-' && argv[1][1] == 'v')
return !fprintf(stdout, "Ulzdec - ULZ Decoder, 15 Nov 2023.\n");
if(argc != 2)
return error("usage", "ulzdec [-v] compressed.bin");
if(!(src = fopen(argv[1], "rb")))
return !error("Invalid input", argv[1]);
2023-11-15 16:15:07 -05:00
mem = malloc(0x10000);
ptr = mem;
2023-11-15 17:21:05 -05:00
decode_ulz(src);
2023-11-15 16:15:07 -05:00
*(ptr) = 0;
printf("%s\n", mem);
2023-11-15 15:06:28 -05:00
return 0;
}