flate

deflate implementation
git clone git://git.suckless.org/flate
Log | Files | Refs | README

commit 19f6bb4724641e60bd8f633ae3bd5a794b135ee2
parent bc3229678243ab7844b5e6d8c9ac7ef348aed039
Author: nsz <nszabolcs@gmail.com>
Date:   Fri, 21 Aug 2009 15:39:26 +0200

-inflate_example -deflate_example
Diffstat:
Makefile | 17++++++-----------
deflate_example.c | 109-------------------------------------------------------------------------------
inflate_example.c | 111-------------------------------------------------------------------------------
3 files changed, 6 insertions(+), 231 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,21 +1,16 @@ #CFLAGS=-g -Wall -ansi -pedantic CFLAGS=-O3 -Wall -ansi -pedantic LDFLAGS= -SRC=inflate.c inflate_example.c inflate_simple.c \ - deflate.c deflate_example.c \ +SRC=inflate.c inflate_simple.c deflate.c \ sflate.c crc.c adler.c OBJ=${SRC:.c=.o} -EXE=sflate inflate inflate_simple deflate +EXE=sflate inflate_simple all: ${EXE} sflate: sflate.o crc.o adler.o inflate.o deflate.o ${CC} -o $@ $^ ${LDFLAGS} -inflate: inflate.o inflate_example.o - ${CC} -o $@ $^ ${LDFLAGS} inflate_simple: inflate_simple.o ${CC} -o $@ $^ ${LDFLAGS} -deflate: deflate.o deflate_example.o - ${CC} -o $@ $^ ${LDFLAGS} ${OBJ}: Makefile flate.h .c.o: ${CC} -c ${CFLAGS} $< @@ -24,18 +19,18 @@ clean: prof: profinf profdef -profinf: inflate_example.c inflate.c +profinf: inflate.c deflate.c sflate.c crc.c adler.c gcc -O3 -fprofile-arcs -ftest-coverage -pg -g -Wall $+ - ./a.out <a.z >/dev/null + ./a.out -d <a.z >/dev/null gcov -b $+ >/dev/null gprof a.out >$<.gprof # gcc -g -Wall $< -# valgrind -v --leak-check=yes ./a.out <a.dat >/dev/null 2>a.valgrind +# valgrind -v --leak-check=yes ./a.out -d <a.z >/dev/null 2>a.valgrind # grep ERROR a.valgrind # grep alloc a.valgrind rm -f a.out a.valgrind *.gcno *.gcda gmon.out -profdef: deflate_example.c deflate.c +profdef: inflate.c deflate.c sflate.c crc.c adler.c gcc -O1 -fprofile-arcs -ftest-coverage -pg -g -Wall $+ ./a.out <a.u >/dev/null gcov -b $+ >/dev/null diff --git a/deflate_example.c b/deflate_example.c @@ -1,109 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include "flate.h" - -/* compress stream using stream struct interface */ -int compress_stream(FILE *in, FILE *out) { - FlateStream s; - int n, nin, nout; - enum {Nin = 1<<12, Nout = 1<<15}; - - s.in = malloc(Nin); - s.out = malloc(Nout); - s.nout = Nout; - s.err = 0; - s.state = 0; - nin = nout = 0; - - for (n = FlateIn; ; n = deflate(&s)) - switch (n) { - case FlateOk: - case FlateErr: - fprintf(stderr, "in: %d out: %d err: %s\n", nin, nout, s.err); - free(s.in); - free(s.out); - return n; - case FlateIn: - s.nin = fread(s.in, 1, Nin, in); - nin += s.nin; - break; - case FlateOut: - if (s.nout != fwrite(s.out, 1, s.nout, out)) - s.err = "write error."; - nout += s.nout; - s.nout = Nout; - break; - } -} - -/* compress stream using callback interface */ -struct data { - FILE *f; - uint n; -}; - -int r(void *p, int siz, void *data) { - struct data *d = data; - uint n; - - n = fread(p, 1, siz, d->f); - d->n += n; - return n; -} - -int w(void *p, int siz, void *data) { - struct data *d = data; - uint n; - - n = fwrite(p, 1, siz, d->f); - d->n += n; - return n; -} - -int compress_callback(FILE *in, FILE *out) { - struct data rr; - struct data ww; - int err; - - rr.f = in; - ww.f = out; - rr.n = ww.n = 0; - err = deflate_callback(r, &rr, w, &ww); - fprintf(stderr, "in: %d out: %d err: %d\n", rr.n, ww.n, err); - return err; -} - -/* in memory compression */ -int compress_mem(void *src, int srclen, void *dst, int dstlen) { - FlateStream s; - int n; - - s.in = src; - s.nin = srclen; - s.out = dst; - s.nout = dstlen; - s.err = 0; - s.state = 0; - - while ((n = deflate(&s)) == FlateOut) { - dstlen -= s.nout; - s.out += s.nout; - s.nout = dstlen; - if (dstlen < 0) - s.err = "not enough output space."; - } - if (n == FlateIn) { - s.err = "input is not terminated."; - n = deflate(&s); - } - if (n == FlateOk) { - fprintf(stderr, "in: %d out: %d\n", srclen, (char *)s.out - (char *)dst); - return (char *)s.out - (char *)dst; - } - fprintf(stderr, "in: %d out: %d err: %s\n", srclen, (char *)s.out - (char *)dst, s.err); - return -1; -} - -int main(void) { - return compress_stream(stdin, stdout); -} diff --git a/inflate_example.c b/inflate_example.c @@ -1,111 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include "flate.h" - -/* decompress stream using stream struct interface */ -int decompress_stream(FILE *in, FILE *out) { - FlateStream s; - int n, nin, nout; - enum {Nin = 1<<12, Nout = 1<<15}; - - s.in = malloc(Nin); - s.out = malloc(Nout); - s.nout = Nout; - s.err = 0; - s.state = 0; - nin = nout = 0; - - for (n = FlateIn; ; n = inflate(&s)) - switch (n) { - case FlateOk: - case FlateErr: - fprintf(stderr, "in: %d out: %d err: %s\n", nin, nout, s.err); - free(s.in); - free(s.out); - return n; - case FlateIn: - s.nin = fread(s.in, 1, Nin, in); - if (s.nin == 0) - s.err = "read error."; - nin += s.nin; - break; - case FlateOut: - if (s.nout != fwrite(s.out, 1, s.nout, out)) - s.err = "write error."; - nout += s.nout; - s.nout = Nout; - break; - } -} - -/* decompress stream using callback interface */ -struct data { - FILE *f; - uint n; -}; - -int r(void *p, int siz, void *data) { - struct data *d = data; - uint n; - - n = fread(p, 1, siz, d->f); - d->n += n; - return n; -} - -int w(void *p, int siz, void *data) { - struct data *d = data; - uint n; - - n = fwrite(p, 1, siz, d->f); - d->n += n; - return n; -} - -int decompress_callback(FILE *in, FILE *out) { - struct data rr; - struct data ww; - int err; - - rr.f = in; - ww.f = out; - rr.n = ww.n = 0; - err = inflate_callback(r, &rr, w, &ww); - fprintf(stderr, "in: %d out: %d err: %d\n", rr.n, ww.n, err); - return err; -} - -/* in memory decompression */ -int decompress_mem(void *src, int srclen, void *dst, int dstlen) { - FlateStream s; - int n; - - s.in = src; - s.nin = srclen; - s.out = dst; - s.nout = dstlen; - s.err = 0; - s.state = 0; - - while ((n = inflate(&s)) == FlateOut) { - dstlen -= s.nout; - s.out += s.nout; - s.nout = dstlen; - if (dstlen < 0) - s.err = "not enough output space."; - } - if (n == FlateIn) { - s.err = "input is not terminated."; - n = inflate(&s); - } - if (n == FlateOk) { - fprintf(stderr, "in: %d out: %d\n", srclen, (char *)s.out - (char *)dst); - return (char *)s.out - (char *)dst; - } - fprintf(stderr, "in: %d out: %d err: %s\n", srclen, (char *)s.out - (char *)dst, s.err); - return -1; -} - -int main(void) { - return decompress_callback(stdin, stdout); -}