annotate src/zlib-1.2.8/contrib/puff/pufftest.c @ 83:ae30d91d2ffe

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