2023-11-15 23:30:28 -05:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2023-11-16 14:02:10 -05:00
|
|
|
char *ptr, *a, mem[0x10000];
|
2023-11-15 23:30:28 -05:00
|
|
|
|
|
|
|
int
|
2023-11-16 14:02:10 -05:00
|
|
|
get_lit(int i)
|
2023-11-15 23:30:28 -05:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-16 14:02:10 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-15 23:30:28 -05:00
|
|
|
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]);
|
2023-11-16 14:02:10 -05:00
|
|
|
fwrite(&mem, res, 1, dst);
|
2023-11-15 23:30:28 -05:00
|
|
|
printf("Compressed %s -> %s(%d bytes).\n", argv[1], argv[2], res);
|
|
|
|
return 0;
|
|
|
|
}
|