diff --git a/cli/lz/build.sh b/cli/lz/build.sh index d0bccfe..9ba0a67 100755 --- a/cli/lz/build.sh +++ b/cli/lz/build.sh @@ -12,6 +12,7 @@ then $LIN decoder.tal clang-format -i lz_main.c clang-format -i ulzdec.c + clang-format -i ulzenc.c fi # Building @@ -30,8 +31,7 @@ $ASM ulzdec.tal ulzdec.rom echo "" echo "C Decoder" echo "" -./ulzdec a.ulz b.bin -cat b.bin +./ulzdec a.ulz b.bin && cat b.bin # Uxn Decoding @@ -47,6 +47,10 @@ echo "C Encoder" echo "" ./ulzenc example.txt a.ulz +# Uxn Decoding +uxncli ulzdec.rom a.ulz b.bin && cat b.bin +# ./ulzdec a.ulz b.bin && cat b.bin + rm ./main rm ./ulzdec rm ./ulzenc diff --git a/cli/lz/ulzdec.tal b/cli/lz/ulzdec.tal index 5b53039..d2fae07 100644 --- a/cli/lz/ulzdec.tal +++ b/cli/lz/ulzdec.tal @@ -38,16 +38,15 @@ @op-lit ( byte -- ) #00 SWP INC2 DUP2 .File/length DEO2 - .ptr LDZ2 - DUP2 .File/read DEO2 - ADD2 .ptr STZ2 + .ptr LDZ2 + DUP2 .File/read DEO2 + ADD2 .ptr STZ2 JMP2r @op-cpy ( byte -- ) #7f AND - DUP #40 AND ?&long - #00 SWP !© -&long ( byte -- ) + DUP #40 AND ?{ + #00 SWP !© } #3f AND getc © ( length* -- ) .ptr LDZ2 #00 getc INC2 SUB2 STH2 diff --git a/cli/lz/ulzenc.c b/cli/lz/ulzenc.c index fb11072..9e0a6fe 100644 --- a/cli/lz/ulzenc.c +++ b/cli/lz/ulzenc.c @@ -10,13 +10,37 @@ error(const char *name, const char *msg) return 0; } -char *mem, *ptr; +char *ptr, *a, mem[0x10000]; + +int +get_lit(int i) +{ + return 0; +} int encode_ulz(FILE *src) { - - return 0; + int i, j, ptr = 0, length = 0; + a = malloc(0x10000); + /* load */ + while(fread(a + length, 1, 1, src) && ++length) {} + /* encode */ + for(i = 0; i < length; i++) { + /* try to make a CPY */ + + /* try to make a LIT */ + for(j = i; j - i < 127 && j < length; j++) {} + if(i != j) { + int litlen = j - i; + /* LIT */ + mem[ptr++] = litlen; + /* LIT(body) */ + for(j = i; j - i < litlen + 1; j++) mem[ptr++] = a[j]; + } + i += j - 1; + } + return ptr - 1; } int @@ -33,7 +57,7 @@ main(int argc, char *argv[]) res = encode_ulz(src); if(!(dst = fopen(argv[2], "wb"))) return !error("Invalid output file", argv[1]); - fwrite(mem, res, 1, dst); + fwrite(&mem, res, 1, dst); printf("Compressed %s -> %s(%d bytes).\n", argv[1], argv[2], res); return 0; } \ No newline at end of file