1b33c96954667ba382fa595baf7b31290bfdd517vboxsync * Copyright (C) 2002-2010 Mark Adler
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync * For conditions of distribution and use, see copyright notice in puff.h
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync * version 2.2, 25 Apr 2010
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync/* Example of how to use puff().
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Usage: puff [-w] [-f] [-nnn] file
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync ... | puff [-w] [-f] [-nnn]
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync where file is the input file with deflate data, nnn is the number of bytes
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync of input to skip before inflating (e.g. to skip a zlib or gzip header), and
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync -w is used to write the decompressed data to stdout. -f is for coverage
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync testing, and causes pufftest to fail with not enough output space (-f does
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync a write like -w, so -w is not required). */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync/* Return size times approximately the cube root of 2, keeping the result as 1,
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync 3, or 5 times a power of 2 -- the result is always > size, until the result
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync is the maximum value of an unsigned long, where it remains. This is useful
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync to keep reallocations less than ~33% over the actual data. */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync for (n = 0; m; n++)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync/* Read the input file *name, or stdin if name is NULL, into allocated memory.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync Reallocate to larger buffers until the entire file is read in. Return a
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync pointer to the allocated data, or NULL if there was a memory allocation
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync failure. *len is the number of bytes of data read from the input file (even
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync if load() returns NULL). If the input file was empty or could not be opened
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync or read, *len is zero. */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync *len += fread((char *)buf + *len, 1, size - *len, in);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync if (size == *len || (swap = realloc(buf, size)) == NULL) {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* process arguments */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync if (len == 0) {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync fprintf(stderr, "could not read %s, or it was empty\n",
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync fprintf(stderr, "skip request of %d leaves no input\n", skip);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* test inflate data with offset skip */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync ret = puff(NIL, &destlen, source + skip, &sourcelen);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync fprintf(stderr, "puff() failed with return code %d\n", ret);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync fprintf(stderr, "puff() succeeded uncompressing %lu bytes\n", destlen);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync if (sourcelen < len) fprintf(stderr, "%lu compressed bytes unused\n",
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* if requested, inflate again and write decompressd data to stdout */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* clean up */