uxn-utils/cli/lz/ulzdec.c

59 lines
1.1 KiB
C
Raw Normal View History

2023-11-15 15:06:28 -05:00
#include <stdio.h>
#include <stdlib.h>
2023-11-15 16:15:07 -05:00
char *ptr;
char *mem;
2023-11-15 15:06:28 -05:00
/* 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;
}
void
2023-11-15 16:15:07 -05:00
decode_byte(FILE *fp, char c)
2023-11-15 15:06:28 -05:00
{
2023-11-15 16:15:07 -05:00
short i, length, offset;
/* DICT */
2023-11-15 15:06:28 -05:00
if(c & 0x80) {
2023-11-15 16:15:07 -05:00
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;
2023-11-15 15:06:28 -05:00
printf("LIT%02x ", length);
for(i = 0; i < length; ++i) {
unsigned char c2 = getc(fp);
printf("%c", c2 == '\n' ? ' ' : c2);
2023-11-15 16:15:07 -05:00
*(ptr++) = c2;
2023-11-15 15:06:28 -05:00
}
printf("\n");
}
}
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 15:06:28 -05:00
while((c = getc(src)) != EOF)
2023-11-15 16:15:07 -05:00
decode_byte(src, c);
*(ptr) = 0;
printf("%s\n", mem);
2023-11-15 15:06:28 -05:00
return 0;
}