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