annotate src/zlib-1.2.7/contrib/puff/pufftest.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 8a15ff55d9af
children
rev   line source
cannam@89 1 /*
cannam@89 2 * pufftest.c
cannam@89 3 * Copyright (C) 2002-2010 Mark Adler
cannam@89 4 * For conditions of distribution and use, see copyright notice in puff.h
cannam@89 5 * version 2.2, 25 Apr 2010
cannam@89 6 */
cannam@89 7
cannam@89 8 /* Example of how to use puff().
cannam@89 9
cannam@89 10 Usage: puff [-w] [-f] [-nnn] file
cannam@89 11 ... | puff [-w] [-f] [-nnn]
cannam@89 12
cannam@89 13 where file is the input file with deflate data, nnn is the number of bytes
cannam@89 14 of input to skip before inflating (e.g. to skip a zlib or gzip header), and
cannam@89 15 -w is used to write the decompressed data to stdout. -f is for coverage
cannam@89 16 testing, and causes pufftest to fail with not enough output space (-f does
cannam@89 17 a write like -w, so -w is not required). */
cannam@89 18
cannam@89 19 #include <stdio.h>
cannam@89 20 #include <stdlib.h>
cannam@89 21 #include "puff.h"
cannam@89 22
cannam@89 23 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
cannam@89 24 # include <fcntl.h>
cannam@89 25 # include <io.h>
cannam@89 26 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
cannam@89 27 #else
cannam@89 28 # define SET_BINARY_MODE(file)
cannam@89 29 #endif
cannam@89 30
cannam@89 31 #define local static
cannam@89 32
cannam@89 33 /* Return size times approximately the cube root of 2, keeping the result as 1,
cannam@89 34 3, or 5 times a power of 2 -- the result is always > size, until the result
cannam@89 35 is the maximum value of an unsigned long, where it remains. This is useful
cannam@89 36 to keep reallocations less than ~33% over the actual data. */
cannam@89 37 local size_t bythirds(size_t size)
cannam@89 38 {
cannam@89 39 int n;
cannam@89 40 size_t m;
cannam@89 41
cannam@89 42 m = size;
cannam@89 43 for (n = 0; m; n++)
cannam@89 44 m >>= 1;
cannam@89 45 if (n < 3)
cannam@89 46 return size + 1;
cannam@89 47 n -= 3;
cannam@89 48 m = size >> n;
cannam@89 49 m += m == 6 ? 2 : 1;
cannam@89 50 m <<= n;
cannam@89 51 return m > size ? m : (size_t)(-1);
cannam@89 52 }
cannam@89 53
cannam@89 54 /* Read the input file *name, or stdin if name is NULL, into allocated memory.
cannam@89 55 Reallocate to larger buffers until the entire file is read in. Return a
cannam@89 56 pointer to the allocated data, or NULL if there was a memory allocation
cannam@89 57 failure. *len is the number of bytes of data read from the input file (even
cannam@89 58 if load() returns NULL). If the input file was empty or could not be opened
cannam@89 59 or read, *len is zero. */
cannam@89 60 local void *load(const char *name, size_t *len)
cannam@89 61 {
cannam@89 62 size_t size;
cannam@89 63 void *buf, *swap;
cannam@89 64 FILE *in;
cannam@89 65
cannam@89 66 *len = 0;
cannam@89 67 buf = malloc(size = 4096);
cannam@89 68 if (buf == NULL)
cannam@89 69 return NULL;
cannam@89 70 in = name == NULL ? stdin : fopen(name, "rb");
cannam@89 71 if (in != NULL) {
cannam@89 72 for (;;) {
cannam@89 73 *len += fread((char *)buf + *len, 1, size - *len, in);
cannam@89 74 if (*len < size) break;
cannam@89 75 size = bythirds(size);
cannam@89 76 if (size == *len || (swap = realloc(buf, size)) == NULL) {
cannam@89 77 free(buf);
cannam@89 78 buf = NULL;
cannam@89 79 break;
cannam@89 80 }
cannam@89 81 buf = swap;
cannam@89 82 }
cannam@89 83 fclose(in);
cannam@89 84 }
cannam@89 85 return buf;
cannam@89 86 }
cannam@89 87
cannam@89 88 int main(int argc, char **argv)
cannam@89 89 {
cannam@89 90 int ret, put = 0, fail = 0;
cannam@89 91 unsigned skip = 0;
cannam@89 92 char *arg, *name = NULL;
cannam@89 93 unsigned char *source = NULL, *dest;
cannam@89 94 size_t len = 0;
cannam@89 95 unsigned long sourcelen, destlen;
cannam@89 96
cannam@89 97 /* process arguments */
cannam@89 98 while (arg = *++argv, --argc)
cannam@89 99 if (arg[0] == '-') {
cannam@89 100 if (arg[1] == 'w' && arg[2] == 0)
cannam@89 101 put = 1;
cannam@89 102 else if (arg[1] == 'f' && arg[2] == 0)
cannam@89 103 fail = 1, put = 1;
cannam@89 104 else if (arg[1] >= '0' && arg[1] <= '9')
cannam@89 105 skip = (unsigned)atoi(arg + 1);
cannam@89 106 else {
cannam@89 107 fprintf(stderr, "invalid option %s\n", arg);
cannam@89 108 return 3;
cannam@89 109 }
cannam@89 110 }
cannam@89 111 else if (name != NULL) {
cannam@89 112 fprintf(stderr, "only one file name allowed\n");
cannam@89 113 return 3;
cannam@89 114 }
cannam@89 115 else
cannam@89 116 name = arg;
cannam@89 117 source = load(name, &len);
cannam@89 118 if (source == NULL) {
cannam@89 119 fprintf(stderr, "memory allocation failure\n");
cannam@89 120 return 4;
cannam@89 121 }
cannam@89 122 if (len == 0) {
cannam@89 123 fprintf(stderr, "could not read %s, or it was empty\n",
cannam@89 124 name == NULL ? "<stdin>" : name);
cannam@89 125 free(source);
cannam@89 126 return 3;
cannam@89 127 }
cannam@89 128 if (skip >= len) {
cannam@89 129 fprintf(stderr, "skip request of %d leaves no input\n", skip);
cannam@89 130 free(source);
cannam@89 131 return 3;
cannam@89 132 }
cannam@89 133
cannam@89 134 /* test inflate data with offset skip */
cannam@89 135 len -= skip;
cannam@89 136 sourcelen = (unsigned long)len;
cannam@89 137 ret = puff(NIL, &destlen, source + skip, &sourcelen);
cannam@89 138 if (ret)
cannam@89 139 fprintf(stderr, "puff() failed with return code %d\n", ret);
cannam@89 140 else {
cannam@89 141 fprintf(stderr, "puff() succeeded uncompressing %lu bytes\n", destlen);
cannam@89 142 if (sourcelen < len) fprintf(stderr, "%lu compressed bytes unused\n",
cannam@89 143 len - sourcelen);
cannam@89 144 }
cannam@89 145
cannam@89 146 /* if requested, inflate again and write decompressd data to stdout */
cannam@89 147 if (put && ret == 0) {
cannam@89 148 if (fail)
cannam@89 149 destlen >>= 1;
cannam@89 150 dest = malloc(destlen);
cannam@89 151 if (dest == NULL) {
cannam@89 152 fprintf(stderr, "memory allocation failure\n");
cannam@89 153 free(source);
cannam@89 154 return 4;
cannam@89 155 }
cannam@89 156 puff(dest, &destlen, source + skip, &sourcelen);
cannam@89 157 SET_BINARY_MODE(stdout);
cannam@89 158 fwrite(dest, 1, destlen, stdout);
cannam@89 159 free(dest);
cannam@89 160 }
cannam@89 161
cannam@89 162 /* clean up */
cannam@89 163 free(source);
cannam@89 164 return ret;
cannam@89 165 }