Mercurial > hg > dmlib
comparison tests/testdmzlib.c @ 1064:a6c5be712b53
Add dmzlib / zlib test utility.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 02 Mar 2015 01:49:14 +0200 |
parents | |
children | 2e997dd888b9 |
comparison
equal
deleted
inserted
replaced
1063:ada47e30d0c9 | 1064:a6c5be712b53 |
---|---|
1 #include "dmlib.h" | |
2 #include "dmzlib.h" | |
3 #include "dmfile.h" | |
4 #include "dmargs.h" | |
5 #include <zlib.h> | |
6 | |
7 | |
8 #define SET_TMPBUF_SIZE (16 * 1024) | |
9 | |
10 char *optInFilename = NULL, | |
11 *optOutFilename = NULL; | |
12 | |
13 int optCompressLevel = Z_BEST_COMPRESSION; | |
14 | |
15 | |
16 static const DMOptArg optList[] = | |
17 { | |
18 { 0, '?', "help", "Show this help", OPT_NONE }, | |
19 { 1, 'q', "quiet", "Decrease verbosity", OPT_NONE }, | |
20 }; | |
21 | |
22 static const int optListN = sizeof(optList) / sizeof(optList[0]); | |
23 | |
24 | |
25 void argShowHelp() | |
26 { | |
27 dmPrintBanner(stdout, dmProgName, "[options] [input filename] [output filename]"); | |
28 dmArgsPrintHelp(stdout, optList, optListN, 0); | |
29 } | |
30 | |
31 | |
32 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | |
33 { | |
34 (void) currArg; | |
35 | |
36 switch (optN) | |
37 { | |
38 case 0: | |
39 argShowHelp(); | |
40 exit(0); | |
41 break; | |
42 | |
43 case 1: | |
44 dmVerbosity = 0; | |
45 break; | |
46 | |
47 case 10: | |
48 optCompressLevel = atoi(optArg); | |
49 if (optCompressLevel < 1 || optCompressLevel > 9) | |
50 { | |
51 dmErrorMsg("Invalid compression level argument '%s', must be 1 .. 9.\n", optArg); | |
52 return FALSE; | |
53 } | |
54 break; | |
55 | |
56 default: | |
57 return FALSE; | |
58 } | |
59 | |
60 return TRUE; | |
61 } | |
62 | |
63 | |
64 BOOL argHandleFile(char *currArg) | |
65 { | |
66 if (optInFilename == NULL) | |
67 optInFilename = currArg; | |
68 else | |
69 if (optOutFilename == NULL) | |
70 optOutFilename = currArg; | |
71 else | |
72 { | |
73 dmErrorMsg("Excess filenames specified.\n"); | |
74 return FALSE; | |
75 } | |
76 return TRUE; | |
77 } | |
78 | |
79 | |
80 int dmTestDMZlib(FILE *inFile, FILE *outFile, size_t *inSize, size_t *outSize) | |
81 { | |
82 Uint8 *inBuffer = NULL, *outBuffer = NULL; | |
83 DMZLibContext ctx; | |
84 int ret; | |
85 | |
86 if ((ret = dmReadDataFile(inFile, "-", &inBuffer, inSize)) != DMERR_OK) | |
87 { | |
88 dmErrorMsg("Failed to read file.\n"); | |
89 goto out; | |
90 } | |
91 | |
92 if ((outBuffer = dmMalloc(SET_TMPBUF_SIZE)) == NULL) | |
93 { | |
94 ret = dmError(DMERR_MALLOC, | |
95 "Malloc failed.\n"); | |
96 goto out; | |
97 } | |
98 | |
99 // Initialize decompression structures | |
100 ctx.zbuffer = inBuffer; | |
101 ctx.zbufferEnd = inBuffer + *inSize; | |
102 | |
103 ctx.zout = ctx.zoutStart = outBuffer; | |
104 ctx.zoutEnd = outBuffer + SET_TMPBUF_SIZE; | |
105 ctx.expandable = TRUE; | |
106 | |
107 // Attempt decompression | |
108 if ((ret = dmZLibParseHeader(&ctx, TRUE)) != DMERR_OK) | |
109 { | |
110 dmErrorMsg("Error parsing ZLIB header: %d, %s\n", ret, dmErrorStr(ret)); | |
111 goto out; | |
112 } | |
113 | |
114 if ((ret = dmZLibDecode(&ctx)) != DMERR_OK) | |
115 { | |
116 dmErrorMsg("Error in ZLIB decompress: %d, %s\n", ret, dmErrorStr(ret)); | |
117 goto out; | |
118 } | |
119 | |
120 outBuffer = ctx.zoutStart; | |
121 *outSize = ctx.zout - ctx.zoutStart; | |
122 | |
123 if (fwrite(outBuffer, sizeof(Uint8), *outSize, outFile) != *outSize) | |
124 { | |
125 ret = dmError(DMERR_FWRITE, "File write error.\n"); | |
126 goto out; | |
127 } | |
128 | |
129 out: | |
130 | |
131 dmFree(inBuffer); | |
132 dmFree(outBuffer); | |
133 return ret; | |
134 } | |
135 | |
136 | |
137 int dmTestZlib(FILE *inFile, FILE *outFile, size_t *inSize, size_t *outSize, BOOL compress, int level) | |
138 { | |
139 Uint8 *inBuffer = NULL, *outBuffer = NULL; | |
140 int zret, zinit = FALSE; | |
141 int ret = DMERR_OK; | |
142 z_stream zstr; | |
143 | |
144 dmMsg(0, "Operating mode: %s\n", | |
145 compress ? "compress" : "decompress"); | |
146 | |
147 if ((inBuffer = malloc(SET_TMPBUF_SIZE)) == NULL || | |
148 (outBuffer = malloc(SET_TMPBUF_SIZE)) == NULL) | |
149 { | |
150 ret = dmError(DMERR_MALLOC, "Malloc failed.\n"); | |
151 goto out; | |
152 } | |
153 | |
154 memset(&zstr, 0, sizeof(zstr)); | |
155 if (compress) | |
156 zret = deflateInit(&zstr, level); | |
157 else | |
158 zret = inflateInit(&zstr); | |
159 | |
160 if (zret != Z_OK) | |
161 { | |
162 ret = dmError(DMERR_INIT_FAIL, "Zlib init fail.\n"); | |
163 goto out; | |
164 } | |
165 zinit = TRUE; | |
166 | |
167 // Initialize compression streams | |
168 zret = Z_OK; | |
169 *outSize = 0; | |
170 while (!feof(inFile) && zret == Z_OK) | |
171 { | |
172 zstr.avail_in = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, inFile); | |
173 | |
174 zstr.next_in = inBuffer; | |
175 zstr.next_out = outBuffer; | |
176 zstr.avail_out = SET_TMPBUF_SIZE; | |
177 zstr.total_out = 0; | |
178 | |
179 if (compress) | |
180 zret = deflate(&zstr, Z_FULL_FLUSH); | |
181 else | |
182 zret = inflate(&zstr, Z_FULL_FLUSH); | |
183 | |
184 if (zret == Z_OK && zstr.total_out > 0) | |
185 { | |
186 outSize += zstr.total_out; | |
187 if (fwrite(outBuffer, sizeof(Uint8), zstr.total_out, outFile) != zstr.total_out) | |
188 { | |
189 ret = dmError(DMERR_FWRITE, | |
190 "File write error.\n"); | |
191 goto out; | |
192 } | |
193 } | |
194 } | |
195 | |
196 | |
197 switch (zret) | |
198 { | |
199 case Z_OK: | |
200 break; | |
201 | |
202 case Z_ERRNO: | |
203 dmErrorMsg("Error: errno\n"); | |
204 break; | |
205 | |
206 case Z_STREAM_END: | |
207 dmErrorMsg("Stream end.\n"); | |
208 break; | |
209 | |
210 default: | |
211 ret = dmError(DMERR_COMPRESSION, | |
212 "Error: %d, %s\n", zret, zError(zret)); | |
213 } | |
214 | |
215 out: | |
216 *inSize = zstr.total_in; | |
217 | |
218 if (zinit) | |
219 { | |
220 if (compress) | |
221 deflateEnd(&zstr); | |
222 else | |
223 inflateEnd(&zstr); | |
224 } | |
225 | |
226 dmFree(inBuffer); | |
227 dmFree(outBuffer); | |
228 | |
229 return ret; | |
230 } | |
231 | |
232 | |
233 int main(int argc, char *argv[]) | |
234 { | |
235 FILE *inFile = NULL, *outFile = NULL; | |
236 size_t inSize, outSize; | |
237 | |
238 dmInitProg("testdmzlib", "zlib/dmzlib test", NULL, NULL, NULL); | |
239 dmVerbosity = 1; | |
240 | |
241 if (!dmArgsProcess(argc, argv, optList, optListN, | |
242 argHandleOpt, argHandleFile, OPTH_BAILOUT)) | |
243 exit(1); | |
244 | |
245 // Input and output files | |
246 inFile = stdin; | |
247 outFile = stdout; | |
248 | |
249 dmMsg(0, "In: %d, out %d bytes.\n", inSize, outSize); | |
250 | |
251 // Cleanup | |
252 if (outFile != NULL) | |
253 fclose(outFile); | |
254 | |
255 if (inFile != NULL) | |
256 fclose(inFile); | |
257 | |
258 return 0; | |
259 } |