From 417b4381dda4b88a1e382f821c8964cf8954307e Mon Sep 17 00:00:00 2001 From: David Monniaux Date: Tue, 29 Jan 2019 22:31:50 +0100 Subject: https://github.com/pfalcon/uzlib --- test/monniaux/uzlib/examples/tgunzip/makefile | 31 ++++ test/monniaux/uzlib/examples/tgunzip/makefile.b32 | 31 ++++ test/monniaux/uzlib/examples/tgunzip/makefile.dj2 | 30 ++++ test/monniaux/uzlib/examples/tgunzip/makefile.dmc | 31 ++++ test/monniaux/uzlib/examples/tgunzip/makefile.mgw | 28 ++++ test/monniaux/uzlib/examples/tgunzip/makefile.vc | 28 ++++ test/monniaux/uzlib/examples/tgunzip/makefile.wat | 31 ++++ test/monniaux/uzlib/examples/tgunzip/tgunzip.c | 167 ++++++++++++++++++++++ test/monniaux/uzlib/examples/tgzip/makefile | 31 ++++ test/monniaux/uzlib/examples/tgzip/tgzip.c | 120 ++++++++++++++++ 10 files changed, 528 insertions(+) create mode 100644 test/monniaux/uzlib/examples/tgunzip/makefile create mode 100644 test/monniaux/uzlib/examples/tgunzip/makefile.b32 create mode 100644 test/monniaux/uzlib/examples/tgunzip/makefile.dj2 create mode 100644 test/monniaux/uzlib/examples/tgunzip/makefile.dmc create mode 100644 test/monniaux/uzlib/examples/tgunzip/makefile.mgw create mode 100644 test/monniaux/uzlib/examples/tgunzip/makefile.vc create mode 100644 test/monniaux/uzlib/examples/tgunzip/makefile.wat create mode 100644 test/monniaux/uzlib/examples/tgunzip/tgunzip.c create mode 100644 test/monniaux/uzlib/examples/tgzip/makefile create mode 100644 test/monniaux/uzlib/examples/tgzip/tgzip.c (limited to 'test/monniaux/uzlib/examples') diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile b/test/monniaux/uzlib/examples/tgunzip/makefile new file mode 100644 index 00000000..bcc90815 --- /dev/null +++ b/test/monniaux/uzlib/examples/tgunzip/makefile @@ -0,0 +1,31 @@ +## +## tgunzip - gzip decompressor example +## +## GCC makefile (Linux, FreeBSD, BeOS and QNX) +## +## Copyright (c) 2003 by Joergen Ibsen / Jibz +## All Rights Reserved +## +## http://www.ibsensoftware.com/ +## + +target = tgunzip +objects = tgunzip.o +libs = ../../lib/libtinf.a + +COPT = -Os +CFLAGS = -Wall -I../../src $(COPT) +LDFLAGS = $(CFLAGS) + +.PHONY: all clean + +all: $(target) + +$(target): $(objects) $(libs) + $(CC) $(LDFLAGS) -o $@ $^ $(libs) + +%.o : %.c + $(CC) $(CFLAGS) -c $< + +clean: + $(RM) $(objects) $(target) diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile.b32 b/test/monniaux/uzlib/examples/tgunzip/makefile.b32 new file mode 100644 index 00000000..7b559c9a --- /dev/null +++ b/test/monniaux/uzlib/examples/tgunzip/makefile.b32 @@ -0,0 +1,31 @@ +## +## tgunzip - gzip decompressor example +## +## Borland C/C++ makefile (GNU Make) +## +## Copyright (c) 2003 by Joergen Ibsen / Jibz +## All Rights Reserved +## +## http://www.ibsensoftware.com/ +## + +target = tgunzip.exe +objects = tgunzip.obj +libs = ..\..\lib\tinf.lib +temps = tgunzip.tds + +cflags = -a16 -K -O2 -OS -I..\..\src +ldflags = -C -q -Gn -x -Gz -ap -Tpe + +.PHONY: all clean + +all: $(target) + +$(target): $(objects) + ilink32 $(ldflags) c0x32 $(objects), $@ , , import32 cw32 $(libs) , , + +%.obj : %.c + bcc32 $(cflags) -c $< + +clean: + $(RM) $(objects) $(target) $(temps) diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile.dj2 b/test/monniaux/uzlib/examples/tgunzip/makefile.dj2 new file mode 100644 index 00000000..c09312dc --- /dev/null +++ b/test/monniaux/uzlib/examples/tgunzip/makefile.dj2 @@ -0,0 +1,30 @@ +## +## tgunzip - gzip decompressor example +## +## DJGPP makefile +## +## Copyright (c) 2003 by Joergen Ibsen / Jibz +## All Rights Reserved +## +## http://www.ibsensoftware.com/ +## + +target = tgunzip.exe +objects = tgunzip.o +libs = ../../lib/libtinf.a + +cflags = -s -Wall -Os -fomit-frame-pointer -I../../src +ldflags = $(cflags) + +.PHONY: all clean + +all: $(target) + +$(target): $(objects) + gcc $(ldflags) -o $@ $^ $(libs) + +%.o : %.c + gcc $(cflags) -o $@ -c $< + +clean: + $(RM) $(objects) $(target) diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile.dmc b/test/monniaux/uzlib/examples/tgunzip/makefile.dmc new file mode 100644 index 00000000..4c3d0297 --- /dev/null +++ b/test/monniaux/uzlib/examples/tgunzip/makefile.dmc @@ -0,0 +1,31 @@ +## +## tgunzip - gzip decompressor example +## +## Digital Mars C/C++ makefile (GNU Make) +## +## Copyright (c) 2003 by Joergen Ibsen / Jibz +## All Rights Reserved +## +## http://www.ibsensoftware.com/ +## + +target = tgunzip.exe +objects = tgunzip.obj +libs = ..\..\lib\tinf.lib +temps = tgunzip.map + +cflags = -s -mn -o+all -I..\..\src +ldflags = $(cflags) + +.PHONY: all clean + +all: $(target) + +$(target): $(objects) + dmc $(ldflags) -o$@ $^ $(libs) + +%.obj : %.c + dmc $(cflags) -c $< + +clean: + $(RM) $(objects) $(target) $(temps) diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile.mgw b/test/monniaux/uzlib/examples/tgunzip/makefile.mgw new file mode 100644 index 00000000..b48b4c10 --- /dev/null +++ b/test/monniaux/uzlib/examples/tgunzip/makefile.mgw @@ -0,0 +1,28 @@ +## +## tgunzip - gzip decompressor example +## +## MinGW / Cygwin makefile +## +## Copyright (c) 1998-2003 by Joergen Ibsen / Jibz +## All Rights Reserved +## + +target = tgunzip.exe +objects = tgunzip.o +libs = ../../lib/libtinf.a + +cflags = -s -Wall -Os -fomit-frame-pointer -I../../src +ldflags = $(cflags) + +.PHONY: all clean + +all: $(target) + +$(target): $(objects) + gcc $(ldflags) -o $@ $^ $(libs) + +%.o : %.c + gcc $(cflags) -o $@ -c $< + +clean: + $(RM) $(target) $(objects) diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile.vc b/test/monniaux/uzlib/examples/tgunzip/makefile.vc new file mode 100644 index 00000000..64dd2e6a --- /dev/null +++ b/test/monniaux/uzlib/examples/tgunzip/makefile.vc @@ -0,0 +1,28 @@ +## +## tgunzip - gzip decompressor example +## +## Visual C/C++ makefile (GNU Make) +## +## Copyright (c) 2003 by Joergen Ibsen / Jibz +## All Rights Reserved +## + +target = tgunzip.exe +objects = tgunzip.obj +libs = ../../lib/tinf.lib + +cflags = /nologo /W3 /O1 /G6 /W3 /Gy /GF /I..\..\src +ldflags = /nologo /release /opt:ref /opt:icf + +.PHONY: all clean + +all: $(target) + +$(target): $(objects) + link $(ldflags) /out:$@ $^ $(libs) + +%.obj : %.c + cl $(cflags) -c $< + +clean: + $(RM) $(target) $(objects) diff --git a/test/monniaux/uzlib/examples/tgunzip/makefile.wat b/test/monniaux/uzlib/examples/tgunzip/makefile.wat new file mode 100644 index 00000000..2d27a985 --- /dev/null +++ b/test/monniaux/uzlib/examples/tgunzip/makefile.wat @@ -0,0 +1,31 @@ +## +## tgunzip - gzip decompressor example +## +## Watcom / OpenWatcom C/C++ makefile (GNU Make) +## +## Copyright (c) 2003 by Joergen Ibsen / Jibz +## All Rights Reserved +## +## http://www.ibsensoftware.com/ +## + +target = tgunzip.exe +objects = tgunzip.obj +libs = ../../lib/tinf.lib +system = nt + +cflags = -bt=$(system) -d0 -ox -I..\..\src +ldflags = system $(system) + +.PHONY: all clean + +all: $(target) + +$(target): $(objects) + wlink $(ldflags) name $@ file {$^} library {$(libs)} + +%.obj : %.c + wcc386 $(cflags) $< + +clean: + $(RM) $(objects) $(target) diff --git a/test/monniaux/uzlib/examples/tgunzip/tgunzip.c b/test/monniaux/uzlib/examples/tgunzip/tgunzip.c new file mode 100644 index 00000000..dcf15eca --- /dev/null +++ b/test/monniaux/uzlib/examples/tgunzip/tgunzip.c @@ -0,0 +1,167 @@ +/* + * tgunzip - gzip decompressor example + * + * Copyright (c) 2003 by Joergen Ibsen / Jibz + * All Rights Reserved + * + * http://www.ibsensoftware.com/ + * + * Copyright (c) 2014-2016 by Paul Sokolovsky + * + * This software is provided 'as-is', without any express + * or implied warranty. In no event will the authors be + * held liable for any damages arising from the use of + * this software. + * + * Permission is granted to anyone to use this software + * for any purpose, including commercial applications, + * and to alter it and redistribute it freely, subject to + * the following restrictions: + * + * 1. The origin of this software must not be + * misrepresented; you must not claim that you + * wrote the original software. If you use this + * software in a product, an acknowledgment in + * the product documentation would be appreciated + * but is not required. + * + * 2. Altered source versions must be plainly marked + * as such, and must not be misrepresented as + * being the original software. + * + * 3. This notice may not be removed or altered from + * any source distribution. + */ + +#include +#include + +#include "uzlib.h" + +/* produce decompressed output in chunks of this size */ +/* defauly is to decompress byte by byte; can be any other length */ +#define OUT_CHUNK_SIZE 1 + +void exit_error(const char *what) +{ + printf("ERROR: %s\n", what); + exit(1); +} + +int main(int argc, char *argv[]) +{ + FILE *fin, *fout; + unsigned int len, dlen, outlen; + const unsigned char *source; + unsigned char *dest; + int res; + + printf("tgunzip - example from the tiny inflate library (www.ibsensoftware.com)\n\n"); + + if (argc < 3) + { + printf( + "Syntax: tgunzip \n\n" + "Both input and output are kept in memory, so do not use this on huge files.\n"); + + return 1; + } + + uzlib_init(); + + /* -- open files -- */ + + if ((fin = fopen(argv[1], "rb")) == NULL) exit_error("source file"); + + if ((fout = fopen(argv[2], "wb")) == NULL) exit_error("destination file"); + + /* -- read source -- */ + + fseek(fin, 0, SEEK_END); + + len = ftell(fin); + + fseek(fin, 0, SEEK_SET); + + source = (unsigned char *)malloc(len); + + if (source == NULL) exit_error("memory"); + + if (fread((unsigned char*)source, 1, len, fin) != len) exit_error("read"); + + fclose(fin); + + if (len < 4) exit_error("file too small"); + + /* -- get decompressed length -- */ + + dlen = source[len - 1]; + dlen = 256*dlen + source[len - 2]; + dlen = 256*dlen + source[len - 3]; + dlen = 256*dlen + source[len - 4]; + + outlen = dlen; + + /* there can be mismatch between length in the trailer and actual + data stream; to avoid buffer overruns on overlong streams, reserve + one extra byte */ + dlen++; + + dest = (unsigned char *)malloc(dlen); + + if (dest == NULL) exit_error("memory"); + + /* -- decompress data -- */ + + struct uzlib_uncomp d; +// uzlib_uncompress_init(&d, malloc(32768), 32768); + uzlib_uncompress_init(&d, NULL, 0); + + /* all 3 fields below must be initialized by user */ + d.source = source; + d.source_limit = source + len - 4; + d.source_read_cb = NULL; + + res = uzlib_gzip_parse_header(&d); + if (res != TINF_OK) { + printf("Error parsing header: %d\n", res); + exit(1); + } + + d.dest_start = d.dest = dest; + + while (dlen) { + unsigned int chunk_len = dlen < OUT_CHUNK_SIZE ? dlen : OUT_CHUNK_SIZE; + d.dest_limit = d.dest + chunk_len; + res = uzlib_uncompress_chksum(&d); + dlen -= chunk_len; + if (res != TINF_OK) { + break; + } + } + + if (res != TINF_DONE) { + printf("Error during decompression: %d\n", res); + exit(-res); + } + + printf("decompressed %lu bytes\n", d.dest - dest); + +#if 0 + if (d.dest - dest != gz.dlen) { + printf("Invalid decompressed length: %lu vs %u\n", d.dest - dest, gz.dlen); + } + + if (tinf_crc32(dest, gz.dlen) != gz.crc32) { + printf("Invalid decompressed crc32\n"); + } +#endif + + /* -- write output -- */ + + fwrite(dest, 1, outlen, fout); + + fclose(fout); + + return 0; +} diff --git a/test/monniaux/uzlib/examples/tgzip/makefile b/test/monniaux/uzlib/examples/tgzip/makefile new file mode 100644 index 00000000..fa064157 --- /dev/null +++ b/test/monniaux/uzlib/examples/tgzip/makefile @@ -0,0 +1,31 @@ +## +## tgzip - gzip compressor example +## +## GCC makefile (Linux, FreeBSD, BeOS and QNX) +## +## Copyright (c) 2003 by Joergen Ibsen / Jibz +## Copyright (c) 2014 by Paul Sokolovsky +## +## http://www.ibsensoftware.com/ +## + +target = tgzip +objects = tgzip.o +libs = ../../lib/libtinf.a + +COPT = -Os +CFLAGS = -Wall -I../../src $(COPT) +LDFLAGS = $(CFLAGS) + +.PHONY: all clean + +all: $(target) + +$(target): $(objects) $(libs) + $(CC) $(LDFLAGS) -o $@ $^ $(libs) + +%.o : %.c + $(CC) $(CFLAGS) -c $< + +clean: + $(RM) $(objects) $(target) diff --git a/test/monniaux/uzlib/examples/tgzip/tgzip.c b/test/monniaux/uzlib/examples/tgzip/tgzip.c new file mode 100644 index 00000000..505169cb --- /dev/null +++ b/test/monniaux/uzlib/examples/tgzip/tgzip.c @@ -0,0 +1,120 @@ +/* + * tgzip - gzip compressor example + * + * Copyright (c) 2003 by Joergen Ibsen / Jibz + * Copyright (c) 2014-2018 by Paul Sokolovsky + * + * http://www.ibsensoftware.com/ + * + * This software is provided 'as-is', without any express + * or implied warranty. In no event will the authors be + * held liable for any damages arising from the use of + * this software. + * + * Permission is granted to anyone to use this software + * for any purpose, including commercial applications, + * and to alter it and redistribute it freely, subject to + * the following restrictions: + * + * 1. The origin of this software must not be + * misrepresented; you must not claim that you + * wrote the original software. If you use this + * software in a product, an acknowledgment in + * the product documentation would be appreciated + * but is not required. + * + * 2. Altered source versions must be plainly marked + * as such, and must not be misrepresented as + * being the original software. + * + * 3. This notice may not be removed or altered from + * any source distribution. + */ + +#include +#include +#include + +#include "uzlib.h" + +void exit_error(const char *what) +{ + printf("ERROR: %s\n", what); + exit(1); +} + +int main(int argc, char *argv[]) +{ + FILE *fin, *fout; + unsigned int len; + unsigned char *source; + + printf("tgzip - example from the uzlib library\n\n"); + + if (argc < 3) + { + printf( + "Syntax: tgunzip \n\n" + "Both input and output are kept in memory, so do not use this on huge files.\n"); + + return 1; + } + + /* -- open files -- */ + + if ((fin = fopen(argv[1], "rb")) == NULL) exit_error("source file"); + + if ((fout = fopen(argv[2], "wb")) == NULL) exit_error("destination file"); + + /* -- read source -- */ + + fseek(fin, 0, SEEK_END); + + len = ftell(fin); + + fseek(fin, 0, SEEK_SET); + + source = (unsigned char *)malloc(len); + + if (source == NULL) exit_error("memory"); + + if (fread(source, 1, len, fin) != len) exit_error("read"); + + fclose(fin); + + /* -- compress data -- */ + + struct uzlib_comp comp = {0}; + comp.dict_size = 32768; + comp.hash_bits = 12; + size_t hash_size = sizeof(uzlib_hash_entry_t) * (1 << comp.hash_bits); + comp.hash_table = malloc(hash_size); + memset(comp.hash_table, 0, hash_size); + + zlib_start_block(&comp.out); + uzlib_compress(&comp, source, len); + zlib_finish_block(&comp.out); + + printf("compressed to %u raw bytes\n", comp.out.outlen); + + /* -- write output -- */ + + putc(0x1f, fout); + putc(0x8b, fout); + putc(0x08, fout); + putc(0x00, fout); // FLG + int mtime = 0; + fwrite(&mtime, sizeof(mtime), 1, fout); + putc(0x04, fout); // XFL + putc(0x03, fout); // OS + + fwrite(comp.out.outbuf, 1, comp.out.outlen, fout); + + unsigned crc = ~uzlib_crc32(source, len, ~0); + fwrite(&crc, sizeof(crc), 1, fout); + fwrite(&len, sizeof(len), 1, fout); + + fclose(fout); + + return 0; +} -- cgit