shithub: sl

ref: 1820b6e70a22e92f69dab14a1d2368b98b96d501
dir: /tools/lzpack.c/

View raw version
#include "platform.h"
#include "brieflz.h"

int
main(int argc, char **argv)
{
	unsigned long sz;
	int n, level;

	level = 9;
	if(argc == 2 && argv[1][0] == '-' && isdigit(argv[1][1])){
		level = atoi(argv[1]+1);
		argc--;
	}
	if(argc != 1)
		abort();

	u8int *in = nil;
	unsigned long max = 32768;
	for(sz = 0;; sz += n){
		if(max < sz+65536){
			max *= 2;
			if((in = realloc(in, max)) == nil)
				abort();
		}
		if((n = read(0, in+sz, max-sz)) < 0)
			abort();
		if(n == 0)
			break;
	}
	if(sz == sizeof(in))
		abort();
	u8int *out = malloc(1+4+blz_max_packed_size(sz));
	if(out == nil)
		abort();
	u8int *w = malloc(blz_workmem_size_level(sz, level));
	if(w == nil)
		abort();
	unsigned long osz = blz_pack_level(in, out+1+4, sz, w, level);
	if(osz == BLZ_ERROR)
		abort();
	out[0] = 0;
	out[1] = sz >> 0;
	out[2] = sz >> 8;
	out[3] = sz >> 16;
	out[4] = sz >> 24;
	osz += 1+4;
	if((unsigned long)write(1, out, osz) != osz)
		abort();
	free(w);
	free(out);
	exit(0);
}