diff --git a/cli/lz/build.sh b/cli/lz/build.sh index bd31f63..eaecffc 100755 --- a/cli/lz/build.sh +++ b/cli/lz/build.sh @@ -1,5 +1,9 @@ #!/bin/sh -e +RELEASE_flags="-DNDEBUG -O2 -g0 -s" +DEBUG_flags="-std=c89 -D_POSIX_C_SOURCE=199309L -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined" + + LIN="uxncli $HOME/roms/uxnlin.rom" ASM="uxncli $HOME/roms/drifblim.rom" @@ -7,12 +11,12 @@ if [[ "$*" == *"--lint"* ]] then $LIN decoder.tal clang-format -i lz_main.c + clang-format -i ulzcdec.c fi # Make c file -cc lz_main.c -o main && ./main -./main # read example.txt, write compressed.bin +cc lz_main.c -o main && ./main && ./main # read example.txt, write compressed.bin # Decoding diff --git a/cli/lz/ulzdec.c b/cli/lz/ulzdec.c new file mode 100644 index 0000000..60cffbd --- /dev/null +++ b/cli/lz/ulzdec.c @@ -0,0 +1,52 @@ +#include +#include + +/* 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 +decode_byte(FILE *fp, char *res, unsigned char c) +{ + 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; + printf("LIT%02x ", length); + for(i = 0; i < length; ++i) { + unsigned char c2 = getc(fp); + printf("%c", c2 == '\n' ? ' ' : c2); + } + 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]); + res = malloc(0x10000); + while((c = getc(src)) != EOF) + decode_byte(src, res, c); + return 0; +} \ No newline at end of file