Mercurial > hg > dmlib
comparison tests/dzlib.c @ 1206:0f07f752cd73
Rename one module under tests/
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 05 Mar 2015 20:14:06 +0200 |
parents | tests/testdmzlib.c@ef5a9c51569c |
children |
comparison
equal
deleted
inserted
replaced
1205:76c5bde4b943 | 1206:0f07f752cd73 |
---|---|
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 unsigned int optSkip = 0; | |
14 unsigned int optCompressLevel = Z_BEST_COMPRESSION; | |
15 BOOL optCompress = FALSE, | |
16 optUseZLIB = FALSE; | |
17 | |
18 | |
19 static const DMOptArg optList[] = | |
20 { | |
21 { 0, '?', "help", "Show this help", OPT_NONE }, | |
22 { 1, 'q', "quiet", "Decrease verbosity", OPT_NONE }, | |
23 { 2, 'Z', "zlib", "Use ZLIB instead of dmzlib", OPT_NONE }, | |
24 { 3, 'c', "compress", "Compress instead of decompressing (ZLIB only)", OPT_NONE }, | |
25 { 4, 'l', "level", "Set zlib compression level 1-9", OPT_ARGREQ }, | |
26 { 5, 's', "skip", "Skip bytes from input start", OPT_ARGREQ }, | |
27 }; | |
28 | |
29 static const int optListN = sizeof(optList) / sizeof(optList[0]); | |
30 | |
31 | |
32 void argShowHelp() | |
33 { | |
34 dmPrintBanner(stdout, dmProgName, "[options] [input filename] [output filename]"); | |
35 dmArgsPrintHelp(stdout, optList, optListN, 0); | |
36 } | |
37 | |
38 | |
39 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | |
40 { | |
41 (void) currArg; | |
42 | |
43 switch (optN) | |
44 { | |
45 case 0: | |
46 argShowHelp(); | |
47 exit(0); | |
48 break; | |
49 | |
50 case 1: | |
51 dmVerbosity = 0; | |
52 break; | |
53 | |
54 case 2: | |
55 optUseZLIB = TRUE; | |
56 break; | |
57 | |
58 case 3: | |
59 optCompress = TRUE; | |
60 break; | |
61 | |
62 case 4: | |
63 if (!dmGetIntVal(optArg, &optCompressLevel) || | |
64 optCompressLevel < 1 || optCompressLevel > 9) | |
65 { | |
66 dmErrorMsg("Invalid compression level argument '%s', must be 1 .. 9.\n", optArg); | |
67 return FALSE; | |
68 } | |
69 break; | |
70 | |
71 case 5: | |
72 if (!dmGetIntVal(optArg, &optSkip)) | |
73 { | |
74 dmErrorMsg("Invalid skip value.\n", optArg); | |
75 return FALSE; | |
76 } | |
77 break; | |
78 | |
79 default: | |
80 return FALSE; | |
81 } | |
82 | |
83 return TRUE; | |
84 } | |
85 | |
86 | |
87 BOOL argHandleFile(char *currArg) | |
88 { | |
89 if (optInFilename == NULL) | |
90 optInFilename = currArg; | |
91 else | |
92 if (optOutFilename == NULL) | |
93 optOutFilename = currArg; | |
94 else | |
95 { | |
96 dmErrorMsg("Excess filenames specified.\n"); | |
97 return FALSE; | |
98 } | |
99 return TRUE; | |
100 } | |
101 | |
102 | |
103 int dmTestDMZlib(FILE *inFile, FILE *outFile, size_t *inSize, size_t *outSize, BOOL compress, int level) | |
104 { | |
105 Uint8 *inBuffer = NULL, *outBuffer = NULL; | |
106 DMZLibContext ctx; | |
107 int ret; | |
108 | |
109 (void) level; | |
110 | |
111 if (compress) | |
112 { | |
113 ret = dmError(DMERR_NOT_SUPPORTED, | |
114 "Compression is not supported with dmzlib.\n"); | |
115 goto out; | |
116 } | |
117 | |
118 if ((ret = dmReadDataFile(inFile, "-", &inBuffer, inSize)) != DMERR_OK) | |
119 { | |
120 dmErrorMsg("Failed to read file.\n"); | |
121 goto out; | |
122 } | |
123 | |
124 if ((outBuffer = dmMalloc(SET_TMPBUF_SIZE)) == NULL) | |
125 { | |
126 ret = dmError(DMERR_MALLOC, | |
127 "Malloc failed.\n"); | |
128 goto out; | |
129 } | |
130 | |
131 // Initialize decompression structures | |
132 if ((ret = dmZLibInitInflate(&ctx)) != DMERR_OK) | |
133 goto out; | |
134 | |
135 ctx.inBuffer = ctx.inBufferStart = inBuffer; | |
136 ctx.inBufferEnd = inBuffer + *inSize; | |
137 | |
138 ctx.outBuffer = ctx.outBufferStart = outBuffer; | |
139 ctx.outBufferEnd = outBuffer + SET_TMPBUF_SIZE; | |
140 ctx.expandable = TRUE; | |
141 | |
142 // Attempt decompression | |
143 if ((ret = dmZLibParseHeader(&ctx, TRUE)) != DMERR_OK) | |
144 { | |
145 dmErrorMsg("Error parsing ZLIB header: %d, %s\n", ret, dmErrorStr(ret)); | |
146 goto out; | |
147 } | |
148 | |
149 if ((ret = dmZLibInflate(&ctx)) != DMERR_OK) | |
150 { | |
151 dmErrorMsg("Error in ZLIB decompress: %d, %s\n", ret, dmErrorStr(ret)); | |
152 goto out; | |
153 } | |
154 | |
155 outBuffer = ctx.outBufferStart; | |
156 *outSize = ctx.outBuffer - ctx.outBufferStart; | |
157 | |
158 if (fwrite(outBuffer, sizeof(Uint8), *outSize, outFile) != *outSize) | |
159 { | |
160 ret = dmError(DMERR_FWRITE, "File write error.\n"); | |
161 goto out; | |
162 } | |
163 | |
164 out: | |
165 dmZLibCloseInflate(&ctx); | |
166 dmFree(inBuffer); | |
167 dmFree(outBuffer); | |
168 return ret; | |
169 } | |
170 | |
171 | |
172 int dmTestZlib(FILE *inFile, FILE *outFile, size_t *inSize, size_t *outSize, BOOL compress, int level) | |
173 { | |
174 Uint8 *inBuffer = NULL, *outBuffer = NULL; | |
175 int zret, zinit = FALSE; | |
176 int ret = DMERR_OK; | |
177 z_stream zstr; | |
178 | |
179 dmMsg(0, "Operating mode: %s\n", | |
180 compress ? "compress" : "decompress"); | |
181 | |
182 dmMemset(&zstr, 0, sizeof(zstr)); | |
183 | |
184 if ((inBuffer = malloc(SET_TMPBUF_SIZE)) == NULL || | |
185 (outBuffer = malloc(SET_TMPBUF_SIZE)) == NULL) | |
186 { | |
187 ret = dmError(DMERR_MALLOC, "Malloc failed.\n"); | |
188 goto out; | |
189 } | |
190 | |
191 if (compress) | |
192 zret = deflateInit(&zstr, level); | |
193 else | |
194 zret = inflateInit(&zstr); | |
195 | |
196 if (zret != Z_OK) | |
197 { | |
198 ret = dmError(DMERR_INIT_FAIL, "Zlib init fail.\n"); | |
199 goto out; | |
200 } | |
201 zinit = TRUE; | |
202 | |
203 // Initialize compression streams | |
204 zret = Z_OK; | |
205 *outSize = 0; | |
206 while (!feof(inFile) && zret == Z_OK) | |
207 { | |
208 zstr.avail_in = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, inFile); | |
209 | |
210 zstr.next_in = inBuffer; | |
211 zstr.next_out = outBuffer; | |
212 zstr.avail_out = SET_TMPBUF_SIZE; | |
213 zstr.total_out = 0; | |
214 | |
215 if (compress) | |
216 zret = deflate(&zstr, Z_FULL_FLUSH); | |
217 else | |
218 zret = inflate(&zstr, Z_FULL_FLUSH); | |
219 | |
220 if (zstr.total_out > 0) | |
221 { | |
222 *outSize += zstr.total_out; | |
223 if (fwrite(outBuffer, sizeof(Uint8), zstr.total_out, outFile) != zstr.total_out) | |
224 { | |
225 ret = dmError(DMERR_FWRITE, | |
226 "File write error.\n"); | |
227 goto out; | |
228 } | |
229 } | |
230 } | |
231 | |
232 | |
233 switch (zret) | |
234 { | |
235 case Z_OK: | |
236 break; | |
237 | |
238 case Z_ERRNO: | |
239 dmErrorMsg("Error: errno\n"); | |
240 break; | |
241 | |
242 case Z_STREAM_END: | |
243 dmErrorMsg("Stream end.\n"); | |
244 break; | |
245 | |
246 default: | |
247 ret = dmError(DMERR_COMPRESSION, | |
248 "Error: %d, %s\n", zret, zError(zret)); | |
249 } | |
250 | |
251 out: | |
252 *inSize = zstr.total_in; | |
253 | |
254 if (zinit) | |
255 { | |
256 if (compress) | |
257 deflateEnd(&zstr); | |
258 else | |
259 inflateEnd(&zstr); | |
260 } | |
261 | |
262 dmFree(inBuffer); | |
263 dmFree(outBuffer); | |
264 | |
265 return ret; | |
266 } | |
267 | |
268 | |
269 int main(int argc, char *argv[]) | |
270 { | |
271 FILE *inFile = NULL, *outFile = NULL; | |
272 size_t inSize = 0, outSize = 0; | |
273 int ret; | |
274 | |
275 dmInitProg("testdmzlib", "ZLIB/dmzlib tester", NULL, NULL, NULL); | |
276 dmVerbosity = 1; | |
277 | |
278 if (!dmArgsProcess(argc, argv, optList, optListN, | |
279 argHandleOpt, argHandleFile, OPTH_BAILOUT)) | |
280 exit(1); | |
281 | |
282 dmZLibInit(); | |
283 | |
284 // Input and output files | |
285 if (optInFilename == NULL) | |
286 inFile = stdin; | |
287 else | |
288 if ((inFile = fopen(optInFilename, "rb")) == NULL) | |
289 { | |
290 int res = dmGetErrno(); | |
291 dmErrorMsg("Failed to open input file '%s': %s\n", | |
292 optInFilename, dmErrorStr(res)); | |
293 goto out; | |
294 } | |
295 | |
296 if (optOutFilename == NULL) | |
297 outFile = stdout; | |
298 else | |
299 if ((outFile = fopen(optOutFilename, "wb")) == NULL) | |
300 { | |
301 int res = dmGetErrno(); | |
302 dmErrorMsg("Failed to open output file '%s': %s\n", | |
303 optOutFilename, dmErrorStr(res)); | |
304 goto out; | |
305 } | |
306 | |
307 if (optSkip > 0 && fseeko(inFile, optSkip, SEEK_CUR) != 0) | |
308 { | |
309 int res = dmGetErrno(); | |
310 dmErrorMsg("Failed to seek in input stream: %s\n", | |
311 dmErrorStr(res)); | |
312 goto out; | |
313 } | |
314 | |
315 if (optUseZLIB) | |
316 ret = dmTestZlib(inFile, outFile, &inSize, &outSize, optCompress, optCompressLevel); | |
317 else | |
318 ret = dmTestDMZlib(inFile, outFile, &inSize, &outSize, optCompress, optCompressLevel); | |
319 | |
320 dmMsg(0, "[%d] In %d, out %d bytes.\n", ret, inSize, outSize); | |
321 | |
322 out: | |
323 // Cleanup | |
324 if (outFile != NULL) | |
325 fclose(outFile); | |
326 | |
327 if (inFile != NULL) | |
328 fclose(inFile); | |
329 | |
330 dmZLibClose(); | |
331 return 0; | |
332 } |