uxn-utils/cli/lz/ulzenc.c

63 lines
1.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
/* cc ulzenc.c -o ulzenc && ./ulzenc example.txt */
static int
error(const char *name, const char *msg)
{
fprintf(stderr, "%s: %s\n", name, msg);
return 0;
}
char *ptr, *a, mem[0x10000];
int
get_lit(int i)
{
return 0;
}
int
encode_ulz(FILE *src)
{
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
main(int argc, char *argv[])
{
int res;
FILE *src, *dst;
if(argv[1][0] == '-' && argv[1][1] == 'v')
return !fprintf(stdout, "Ulzenc - ULZ Encoder, 15 Nov 2023.\n");
if(argc != 3)
return error("usage", "ulzenc [-v] a.bin b.ulz ");
if(!(src = fopen(argv[1], "rb")))
return !error("Invalid input file", argv[1]);
res = encode_ulz(src);
if(!(dst = fopen(argv[2], "wb")))
return !error("Invalid output file", argv[1]);
fwrite(&mem, res, 1, dst);
printf("Compressed %s -> %s(%d bytes).\n", argv[1], argv[2], res);
return 0;
}