Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* example.c -- usage example of the zlib compression library |
michael@0 | 2 | * Copyright (C) 1995-2006 Jean-loup Gailly. |
michael@0 | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | /* @(#) $Id$ */ |
michael@0 | 7 | |
michael@0 | 8 | #include "zlib.h" |
michael@0 | 9 | #include <stdio.h> |
michael@0 | 10 | |
michael@0 | 11 | #ifdef STDC |
michael@0 | 12 | # include <string.h> |
michael@0 | 13 | # include <stdlib.h> |
michael@0 | 14 | #endif |
michael@0 | 15 | |
michael@0 | 16 | #if defined(VMS) || defined(RISCOS) |
michael@0 | 17 | # define TESTFILE "foo-gz" |
michael@0 | 18 | #else |
michael@0 | 19 | # define TESTFILE "foo.gz" |
michael@0 | 20 | #endif |
michael@0 | 21 | |
michael@0 | 22 | #define CHECK_ERR(err, msg) { \ |
michael@0 | 23 | if (err != Z_OK) { \ |
michael@0 | 24 | fprintf(stderr, "%s error: %d\n", msg, err); \ |
michael@0 | 25 | exit(1); \ |
michael@0 | 26 | } \ |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | const char hello[] = "hello, hello!"; |
michael@0 | 30 | /* "hello world" would be more standard, but the repeated "hello" |
michael@0 | 31 | * stresses the compression code better, sorry... |
michael@0 | 32 | */ |
michael@0 | 33 | |
michael@0 | 34 | const char dictionary[] = "hello"; |
michael@0 | 35 | uLong dictId; /* Adler32 value of the dictionary */ |
michael@0 | 36 | |
michael@0 | 37 | void test_compress OF((Byte *compr, uLong comprLen, |
michael@0 | 38 | Byte *uncompr, uLong uncomprLen)); |
michael@0 | 39 | void test_gzio OF((const char *fname, |
michael@0 | 40 | Byte *uncompr, uLong uncomprLen)); |
michael@0 | 41 | void test_deflate OF((Byte *compr, uLong comprLen)); |
michael@0 | 42 | void test_inflate OF((Byte *compr, uLong comprLen, |
michael@0 | 43 | Byte *uncompr, uLong uncomprLen)); |
michael@0 | 44 | void test_large_deflate OF((Byte *compr, uLong comprLen, |
michael@0 | 45 | Byte *uncompr, uLong uncomprLen)); |
michael@0 | 46 | void test_large_inflate OF((Byte *compr, uLong comprLen, |
michael@0 | 47 | Byte *uncompr, uLong uncomprLen)); |
michael@0 | 48 | void test_flush OF((Byte *compr, uLong *comprLen)); |
michael@0 | 49 | void test_sync OF((Byte *compr, uLong comprLen, |
michael@0 | 50 | Byte *uncompr, uLong uncomprLen)); |
michael@0 | 51 | void test_dict_deflate OF((Byte *compr, uLong comprLen)); |
michael@0 | 52 | void test_dict_inflate OF((Byte *compr, uLong comprLen, |
michael@0 | 53 | Byte *uncompr, uLong uncomprLen)); |
michael@0 | 54 | int main OF((int argc, char *argv[])); |
michael@0 | 55 | |
michael@0 | 56 | /* =========================================================================== |
michael@0 | 57 | * Test compress() and uncompress() |
michael@0 | 58 | */ |
michael@0 | 59 | void test_compress(compr, comprLen, uncompr, uncomprLen) |
michael@0 | 60 | Byte *compr, *uncompr; |
michael@0 | 61 | uLong comprLen, uncomprLen; |
michael@0 | 62 | { |
michael@0 | 63 | int err; |
michael@0 | 64 | uLong len = (uLong)strlen(hello)+1; |
michael@0 | 65 | |
michael@0 | 66 | err = compress(compr, &comprLen, (const Bytef*)hello, len); |
michael@0 | 67 | CHECK_ERR(err, "compress"); |
michael@0 | 68 | |
michael@0 | 69 | strcpy((char*)uncompr, "garbage"); |
michael@0 | 70 | |
michael@0 | 71 | err = uncompress(uncompr, &uncomprLen, compr, comprLen); |
michael@0 | 72 | CHECK_ERR(err, "uncompress"); |
michael@0 | 73 | |
michael@0 | 74 | if (strcmp((char*)uncompr, hello)) { |
michael@0 | 75 | fprintf(stderr, "bad uncompress\n"); |
michael@0 | 76 | exit(1); |
michael@0 | 77 | } else { |
michael@0 | 78 | printf("uncompress(): %s\n", (char *)uncompr); |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | /* =========================================================================== |
michael@0 | 83 | * Test read/write of .gz files |
michael@0 | 84 | */ |
michael@0 | 85 | void test_gzio(fname, uncompr, uncomprLen) |
michael@0 | 86 | const char *fname; /* compressed file name */ |
michael@0 | 87 | Byte *uncompr; |
michael@0 | 88 | uLong uncomprLen; |
michael@0 | 89 | { |
michael@0 | 90 | #ifdef NO_GZCOMPRESS |
michael@0 | 91 | fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n"); |
michael@0 | 92 | #else |
michael@0 | 93 | int err; |
michael@0 | 94 | int len = (int)strlen(hello)+1; |
michael@0 | 95 | gzFile file; |
michael@0 | 96 | z_off_t pos; |
michael@0 | 97 | |
michael@0 | 98 | file = gzopen(fname, "wb"); |
michael@0 | 99 | if (file == NULL) { |
michael@0 | 100 | fprintf(stderr, "gzopen error\n"); |
michael@0 | 101 | exit(1); |
michael@0 | 102 | } |
michael@0 | 103 | gzputc(file, 'h'); |
michael@0 | 104 | if (gzputs(file, "ello") != 4) { |
michael@0 | 105 | fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err)); |
michael@0 | 106 | exit(1); |
michael@0 | 107 | } |
michael@0 | 108 | if (gzprintf(file, ", %s!", "hello") != 8) { |
michael@0 | 109 | fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err)); |
michael@0 | 110 | exit(1); |
michael@0 | 111 | } |
michael@0 | 112 | gzseek(file, 1L, SEEK_CUR); /* add one zero byte */ |
michael@0 | 113 | gzclose(file); |
michael@0 | 114 | |
michael@0 | 115 | file = gzopen(fname, "rb"); |
michael@0 | 116 | if (file == NULL) { |
michael@0 | 117 | fprintf(stderr, "gzopen error\n"); |
michael@0 | 118 | exit(1); |
michael@0 | 119 | } |
michael@0 | 120 | strcpy((char*)uncompr, "garbage"); |
michael@0 | 121 | |
michael@0 | 122 | if (gzread(file, uncompr, (unsigned)uncomprLen) != len) { |
michael@0 | 123 | fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); |
michael@0 | 124 | exit(1); |
michael@0 | 125 | } |
michael@0 | 126 | if (strcmp((char*)uncompr, hello)) { |
michael@0 | 127 | fprintf(stderr, "bad gzread: %s\n", (char*)uncompr); |
michael@0 | 128 | exit(1); |
michael@0 | 129 | } else { |
michael@0 | 130 | printf("gzread(): %s\n", (char*)uncompr); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | pos = gzseek(file, -8L, SEEK_CUR); |
michael@0 | 134 | if (pos != 6 || gztell(file) != pos) { |
michael@0 | 135 | fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n", |
michael@0 | 136 | (long)pos, (long)gztell(file)); |
michael@0 | 137 | exit(1); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | if (gzgetc(file) != ' ') { |
michael@0 | 141 | fprintf(stderr, "gzgetc error\n"); |
michael@0 | 142 | exit(1); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | if (gzungetc(' ', file) != ' ') { |
michael@0 | 146 | fprintf(stderr, "gzungetc error\n"); |
michael@0 | 147 | exit(1); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | gzgets(file, (char*)uncompr, (int)uncomprLen); |
michael@0 | 151 | if (strlen((char*)uncompr) != 7) { /* " hello!" */ |
michael@0 | 152 | fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err)); |
michael@0 | 153 | exit(1); |
michael@0 | 154 | } |
michael@0 | 155 | if (strcmp((char*)uncompr, hello + 6)) { |
michael@0 | 156 | fprintf(stderr, "bad gzgets after gzseek\n"); |
michael@0 | 157 | exit(1); |
michael@0 | 158 | } else { |
michael@0 | 159 | printf("gzgets() after gzseek: %s\n", (char*)uncompr); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | gzclose(file); |
michael@0 | 163 | #endif |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | /* =========================================================================== |
michael@0 | 167 | * Test deflate() with small buffers |
michael@0 | 168 | */ |
michael@0 | 169 | void test_deflate(compr, comprLen) |
michael@0 | 170 | Byte *compr; |
michael@0 | 171 | uLong comprLen; |
michael@0 | 172 | { |
michael@0 | 173 | z_stream c_stream; /* compression stream */ |
michael@0 | 174 | int err; |
michael@0 | 175 | uLong len = (uLong)strlen(hello)+1; |
michael@0 | 176 | |
michael@0 | 177 | c_stream.zalloc = (alloc_func)0; |
michael@0 | 178 | c_stream.zfree = (free_func)0; |
michael@0 | 179 | c_stream.opaque = (voidpf)0; |
michael@0 | 180 | |
michael@0 | 181 | err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); |
michael@0 | 182 | CHECK_ERR(err, "deflateInit"); |
michael@0 | 183 | |
michael@0 | 184 | c_stream.next_in = (Bytef*)hello; |
michael@0 | 185 | c_stream.next_out = compr; |
michael@0 | 186 | |
michael@0 | 187 | while (c_stream.total_in != len && c_stream.total_out < comprLen) { |
michael@0 | 188 | c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */ |
michael@0 | 189 | err = deflate(&c_stream, Z_NO_FLUSH); |
michael@0 | 190 | CHECK_ERR(err, "deflate"); |
michael@0 | 191 | } |
michael@0 | 192 | /* Finish the stream, still forcing small buffers: */ |
michael@0 | 193 | for (;;) { |
michael@0 | 194 | c_stream.avail_out = 1; |
michael@0 | 195 | err = deflate(&c_stream, Z_FINISH); |
michael@0 | 196 | if (err == Z_STREAM_END) break; |
michael@0 | 197 | CHECK_ERR(err, "deflate"); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | err = deflateEnd(&c_stream); |
michael@0 | 201 | CHECK_ERR(err, "deflateEnd"); |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | /* =========================================================================== |
michael@0 | 205 | * Test inflate() with small buffers |
michael@0 | 206 | */ |
michael@0 | 207 | void test_inflate(compr, comprLen, uncompr, uncomprLen) |
michael@0 | 208 | Byte *compr, *uncompr; |
michael@0 | 209 | uLong comprLen, uncomprLen; |
michael@0 | 210 | { |
michael@0 | 211 | int err; |
michael@0 | 212 | z_stream d_stream; /* decompression stream */ |
michael@0 | 213 | |
michael@0 | 214 | strcpy((char*)uncompr, "garbage"); |
michael@0 | 215 | |
michael@0 | 216 | d_stream.zalloc = (alloc_func)0; |
michael@0 | 217 | d_stream.zfree = (free_func)0; |
michael@0 | 218 | d_stream.opaque = (voidpf)0; |
michael@0 | 219 | |
michael@0 | 220 | d_stream.next_in = compr; |
michael@0 | 221 | d_stream.avail_in = 0; |
michael@0 | 222 | d_stream.next_out = uncompr; |
michael@0 | 223 | |
michael@0 | 224 | err = inflateInit(&d_stream); |
michael@0 | 225 | CHECK_ERR(err, "inflateInit"); |
michael@0 | 226 | |
michael@0 | 227 | while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { |
michael@0 | 228 | d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ |
michael@0 | 229 | err = inflate(&d_stream, Z_NO_FLUSH); |
michael@0 | 230 | if (err == Z_STREAM_END) break; |
michael@0 | 231 | CHECK_ERR(err, "inflate"); |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | err = inflateEnd(&d_stream); |
michael@0 | 235 | CHECK_ERR(err, "inflateEnd"); |
michael@0 | 236 | |
michael@0 | 237 | if (strcmp((char*)uncompr, hello)) { |
michael@0 | 238 | fprintf(stderr, "bad inflate\n"); |
michael@0 | 239 | exit(1); |
michael@0 | 240 | } else { |
michael@0 | 241 | printf("inflate(): %s\n", (char *)uncompr); |
michael@0 | 242 | } |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | /* =========================================================================== |
michael@0 | 246 | * Test deflate() with large buffers and dynamic change of compression level |
michael@0 | 247 | */ |
michael@0 | 248 | void test_large_deflate(compr, comprLen, uncompr, uncomprLen) |
michael@0 | 249 | Byte *compr, *uncompr; |
michael@0 | 250 | uLong comprLen, uncomprLen; |
michael@0 | 251 | { |
michael@0 | 252 | z_stream c_stream; /* compression stream */ |
michael@0 | 253 | int err; |
michael@0 | 254 | |
michael@0 | 255 | c_stream.zalloc = (alloc_func)0; |
michael@0 | 256 | c_stream.zfree = (free_func)0; |
michael@0 | 257 | c_stream.opaque = (voidpf)0; |
michael@0 | 258 | |
michael@0 | 259 | err = deflateInit(&c_stream, Z_BEST_SPEED); |
michael@0 | 260 | CHECK_ERR(err, "deflateInit"); |
michael@0 | 261 | |
michael@0 | 262 | c_stream.next_out = compr; |
michael@0 | 263 | c_stream.avail_out = (uInt)comprLen; |
michael@0 | 264 | |
michael@0 | 265 | /* At this point, uncompr is still mostly zeroes, so it should compress |
michael@0 | 266 | * very well: |
michael@0 | 267 | */ |
michael@0 | 268 | c_stream.next_in = uncompr; |
michael@0 | 269 | c_stream.avail_in = (uInt)uncomprLen; |
michael@0 | 270 | err = deflate(&c_stream, Z_NO_FLUSH); |
michael@0 | 271 | CHECK_ERR(err, "deflate"); |
michael@0 | 272 | if (c_stream.avail_in != 0) { |
michael@0 | 273 | fprintf(stderr, "deflate not greedy\n"); |
michael@0 | 274 | exit(1); |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | /* Feed in already compressed data and switch to no compression: */ |
michael@0 | 278 | deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY); |
michael@0 | 279 | c_stream.next_in = compr; |
michael@0 | 280 | c_stream.avail_in = (uInt)comprLen/2; |
michael@0 | 281 | err = deflate(&c_stream, Z_NO_FLUSH); |
michael@0 | 282 | CHECK_ERR(err, "deflate"); |
michael@0 | 283 | |
michael@0 | 284 | /* Switch back to compressing mode: */ |
michael@0 | 285 | deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED); |
michael@0 | 286 | c_stream.next_in = uncompr; |
michael@0 | 287 | c_stream.avail_in = (uInt)uncomprLen; |
michael@0 | 288 | err = deflate(&c_stream, Z_NO_FLUSH); |
michael@0 | 289 | CHECK_ERR(err, "deflate"); |
michael@0 | 290 | |
michael@0 | 291 | err = deflate(&c_stream, Z_FINISH); |
michael@0 | 292 | if (err != Z_STREAM_END) { |
michael@0 | 293 | fprintf(stderr, "deflate should report Z_STREAM_END\n"); |
michael@0 | 294 | exit(1); |
michael@0 | 295 | } |
michael@0 | 296 | err = deflateEnd(&c_stream); |
michael@0 | 297 | CHECK_ERR(err, "deflateEnd"); |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | /* =========================================================================== |
michael@0 | 301 | * Test inflate() with large buffers |
michael@0 | 302 | */ |
michael@0 | 303 | void test_large_inflate(compr, comprLen, uncompr, uncomprLen) |
michael@0 | 304 | Byte *compr, *uncompr; |
michael@0 | 305 | uLong comprLen, uncomprLen; |
michael@0 | 306 | { |
michael@0 | 307 | int err; |
michael@0 | 308 | z_stream d_stream; /* decompression stream */ |
michael@0 | 309 | |
michael@0 | 310 | strcpy((char*)uncompr, "garbage"); |
michael@0 | 311 | |
michael@0 | 312 | d_stream.zalloc = (alloc_func)0; |
michael@0 | 313 | d_stream.zfree = (free_func)0; |
michael@0 | 314 | d_stream.opaque = (voidpf)0; |
michael@0 | 315 | |
michael@0 | 316 | d_stream.next_in = compr; |
michael@0 | 317 | d_stream.avail_in = (uInt)comprLen; |
michael@0 | 318 | |
michael@0 | 319 | err = inflateInit(&d_stream); |
michael@0 | 320 | CHECK_ERR(err, "inflateInit"); |
michael@0 | 321 | |
michael@0 | 322 | for (;;) { |
michael@0 | 323 | d_stream.next_out = uncompr; /* discard the output */ |
michael@0 | 324 | d_stream.avail_out = (uInt)uncomprLen; |
michael@0 | 325 | err = inflate(&d_stream, Z_NO_FLUSH); |
michael@0 | 326 | if (err == Z_STREAM_END) break; |
michael@0 | 327 | CHECK_ERR(err, "large inflate"); |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | err = inflateEnd(&d_stream); |
michael@0 | 331 | CHECK_ERR(err, "inflateEnd"); |
michael@0 | 332 | |
michael@0 | 333 | if (d_stream.total_out != 2*uncomprLen + comprLen/2) { |
michael@0 | 334 | fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out); |
michael@0 | 335 | exit(1); |
michael@0 | 336 | } else { |
michael@0 | 337 | printf("large_inflate(): OK\n"); |
michael@0 | 338 | } |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | /* =========================================================================== |
michael@0 | 342 | * Test deflate() with full flush |
michael@0 | 343 | */ |
michael@0 | 344 | void test_flush(compr, comprLen) |
michael@0 | 345 | Byte *compr; |
michael@0 | 346 | uLong *comprLen; |
michael@0 | 347 | { |
michael@0 | 348 | z_stream c_stream; /* compression stream */ |
michael@0 | 349 | int err; |
michael@0 | 350 | uInt len = (uInt)strlen(hello)+1; |
michael@0 | 351 | |
michael@0 | 352 | c_stream.zalloc = (alloc_func)0; |
michael@0 | 353 | c_stream.zfree = (free_func)0; |
michael@0 | 354 | c_stream.opaque = (voidpf)0; |
michael@0 | 355 | |
michael@0 | 356 | err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); |
michael@0 | 357 | CHECK_ERR(err, "deflateInit"); |
michael@0 | 358 | |
michael@0 | 359 | c_stream.next_in = (Bytef*)hello; |
michael@0 | 360 | c_stream.next_out = compr; |
michael@0 | 361 | c_stream.avail_in = 3; |
michael@0 | 362 | c_stream.avail_out = (uInt)*comprLen; |
michael@0 | 363 | err = deflate(&c_stream, Z_FULL_FLUSH); |
michael@0 | 364 | CHECK_ERR(err, "deflate"); |
michael@0 | 365 | |
michael@0 | 366 | compr[3]++; /* force an error in first compressed block */ |
michael@0 | 367 | c_stream.avail_in = len - 3; |
michael@0 | 368 | |
michael@0 | 369 | err = deflate(&c_stream, Z_FINISH); |
michael@0 | 370 | if (err != Z_STREAM_END) { |
michael@0 | 371 | CHECK_ERR(err, "deflate"); |
michael@0 | 372 | } |
michael@0 | 373 | err = deflateEnd(&c_stream); |
michael@0 | 374 | CHECK_ERR(err, "deflateEnd"); |
michael@0 | 375 | |
michael@0 | 376 | *comprLen = c_stream.total_out; |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | /* =========================================================================== |
michael@0 | 380 | * Test inflateSync() |
michael@0 | 381 | */ |
michael@0 | 382 | void test_sync(compr, comprLen, uncompr, uncomprLen) |
michael@0 | 383 | Byte *compr, *uncompr; |
michael@0 | 384 | uLong comprLen, uncomprLen; |
michael@0 | 385 | { |
michael@0 | 386 | int err; |
michael@0 | 387 | z_stream d_stream; /* decompression stream */ |
michael@0 | 388 | |
michael@0 | 389 | strcpy((char*)uncompr, "garbage"); |
michael@0 | 390 | |
michael@0 | 391 | d_stream.zalloc = (alloc_func)0; |
michael@0 | 392 | d_stream.zfree = (free_func)0; |
michael@0 | 393 | d_stream.opaque = (voidpf)0; |
michael@0 | 394 | |
michael@0 | 395 | d_stream.next_in = compr; |
michael@0 | 396 | d_stream.avail_in = 2; /* just read the zlib header */ |
michael@0 | 397 | |
michael@0 | 398 | err = inflateInit(&d_stream); |
michael@0 | 399 | CHECK_ERR(err, "inflateInit"); |
michael@0 | 400 | |
michael@0 | 401 | d_stream.next_out = uncompr; |
michael@0 | 402 | d_stream.avail_out = (uInt)uncomprLen; |
michael@0 | 403 | |
michael@0 | 404 | inflate(&d_stream, Z_NO_FLUSH); |
michael@0 | 405 | CHECK_ERR(err, "inflate"); |
michael@0 | 406 | |
michael@0 | 407 | d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */ |
michael@0 | 408 | err = inflateSync(&d_stream); /* but skip the damaged part */ |
michael@0 | 409 | CHECK_ERR(err, "inflateSync"); |
michael@0 | 410 | |
michael@0 | 411 | err = inflate(&d_stream, Z_FINISH); |
michael@0 | 412 | if (err != Z_DATA_ERROR) { |
michael@0 | 413 | fprintf(stderr, "inflate should report DATA_ERROR\n"); |
michael@0 | 414 | /* Because of incorrect adler32 */ |
michael@0 | 415 | exit(1); |
michael@0 | 416 | } |
michael@0 | 417 | err = inflateEnd(&d_stream); |
michael@0 | 418 | CHECK_ERR(err, "inflateEnd"); |
michael@0 | 419 | |
michael@0 | 420 | printf("after inflateSync(): hel%s\n", (char *)uncompr); |
michael@0 | 421 | } |
michael@0 | 422 | |
michael@0 | 423 | /* =========================================================================== |
michael@0 | 424 | * Test deflate() with preset dictionary |
michael@0 | 425 | */ |
michael@0 | 426 | void test_dict_deflate(compr, comprLen) |
michael@0 | 427 | Byte *compr; |
michael@0 | 428 | uLong comprLen; |
michael@0 | 429 | { |
michael@0 | 430 | z_stream c_stream; /* compression stream */ |
michael@0 | 431 | int err; |
michael@0 | 432 | |
michael@0 | 433 | c_stream.zalloc = (alloc_func)0; |
michael@0 | 434 | c_stream.zfree = (free_func)0; |
michael@0 | 435 | c_stream.opaque = (voidpf)0; |
michael@0 | 436 | |
michael@0 | 437 | err = deflateInit(&c_stream, Z_BEST_COMPRESSION); |
michael@0 | 438 | CHECK_ERR(err, "deflateInit"); |
michael@0 | 439 | |
michael@0 | 440 | err = deflateSetDictionary(&c_stream, |
michael@0 | 441 | (const Bytef*)dictionary, sizeof(dictionary)); |
michael@0 | 442 | CHECK_ERR(err, "deflateSetDictionary"); |
michael@0 | 443 | |
michael@0 | 444 | dictId = c_stream.adler; |
michael@0 | 445 | c_stream.next_out = compr; |
michael@0 | 446 | c_stream.avail_out = (uInt)comprLen; |
michael@0 | 447 | |
michael@0 | 448 | c_stream.next_in = (Bytef*)hello; |
michael@0 | 449 | c_stream.avail_in = (uInt)strlen(hello)+1; |
michael@0 | 450 | |
michael@0 | 451 | err = deflate(&c_stream, Z_FINISH); |
michael@0 | 452 | if (err != Z_STREAM_END) { |
michael@0 | 453 | fprintf(stderr, "deflate should report Z_STREAM_END\n"); |
michael@0 | 454 | exit(1); |
michael@0 | 455 | } |
michael@0 | 456 | err = deflateEnd(&c_stream); |
michael@0 | 457 | CHECK_ERR(err, "deflateEnd"); |
michael@0 | 458 | } |
michael@0 | 459 | |
michael@0 | 460 | /* =========================================================================== |
michael@0 | 461 | * Test inflate() with a preset dictionary |
michael@0 | 462 | */ |
michael@0 | 463 | void test_dict_inflate(compr, comprLen, uncompr, uncomprLen) |
michael@0 | 464 | Byte *compr, *uncompr; |
michael@0 | 465 | uLong comprLen, uncomprLen; |
michael@0 | 466 | { |
michael@0 | 467 | int err; |
michael@0 | 468 | z_stream d_stream; /* decompression stream */ |
michael@0 | 469 | |
michael@0 | 470 | strcpy((char*)uncompr, "garbage"); |
michael@0 | 471 | |
michael@0 | 472 | d_stream.zalloc = (alloc_func)0; |
michael@0 | 473 | d_stream.zfree = (free_func)0; |
michael@0 | 474 | d_stream.opaque = (voidpf)0; |
michael@0 | 475 | |
michael@0 | 476 | d_stream.next_in = compr; |
michael@0 | 477 | d_stream.avail_in = (uInt)comprLen; |
michael@0 | 478 | |
michael@0 | 479 | err = inflateInit(&d_stream); |
michael@0 | 480 | CHECK_ERR(err, "inflateInit"); |
michael@0 | 481 | |
michael@0 | 482 | d_stream.next_out = uncompr; |
michael@0 | 483 | d_stream.avail_out = (uInt)uncomprLen; |
michael@0 | 484 | |
michael@0 | 485 | for (;;) { |
michael@0 | 486 | err = inflate(&d_stream, Z_NO_FLUSH); |
michael@0 | 487 | if (err == Z_STREAM_END) break; |
michael@0 | 488 | if (err == Z_NEED_DICT) { |
michael@0 | 489 | if (d_stream.adler != dictId) { |
michael@0 | 490 | fprintf(stderr, "unexpected dictionary"); |
michael@0 | 491 | exit(1); |
michael@0 | 492 | } |
michael@0 | 493 | err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary, |
michael@0 | 494 | sizeof(dictionary)); |
michael@0 | 495 | } |
michael@0 | 496 | CHECK_ERR(err, "inflate with dict"); |
michael@0 | 497 | } |
michael@0 | 498 | |
michael@0 | 499 | err = inflateEnd(&d_stream); |
michael@0 | 500 | CHECK_ERR(err, "inflateEnd"); |
michael@0 | 501 | |
michael@0 | 502 | if (strcmp((char*)uncompr, hello)) { |
michael@0 | 503 | fprintf(stderr, "bad inflate with dict\n"); |
michael@0 | 504 | exit(1); |
michael@0 | 505 | } else { |
michael@0 | 506 | printf("inflate with dictionary: %s\n", (char *)uncompr); |
michael@0 | 507 | } |
michael@0 | 508 | } |
michael@0 | 509 | |
michael@0 | 510 | /* =========================================================================== |
michael@0 | 511 | * Usage: example [output.gz [input.gz]] |
michael@0 | 512 | */ |
michael@0 | 513 | |
michael@0 | 514 | int main(argc, argv) |
michael@0 | 515 | int argc; |
michael@0 | 516 | char *argv[]; |
michael@0 | 517 | { |
michael@0 | 518 | Byte *compr, *uncompr; |
michael@0 | 519 | uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */ |
michael@0 | 520 | uLong uncomprLen = comprLen; |
michael@0 | 521 | static const char* myVersion = ZLIB_VERSION; |
michael@0 | 522 | |
michael@0 | 523 | if (zlibVersion()[0] != myVersion[0]) { |
michael@0 | 524 | fprintf(stderr, "incompatible zlib version\n"); |
michael@0 | 525 | exit(1); |
michael@0 | 526 | |
michael@0 | 527 | } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) { |
michael@0 | 528 | fprintf(stderr, "warning: different zlib version\n"); |
michael@0 | 529 | } |
michael@0 | 530 | |
michael@0 | 531 | printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n", |
michael@0 | 532 | ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags()); |
michael@0 | 533 | |
michael@0 | 534 | compr = (Byte*)calloc((uInt)comprLen, 1); |
michael@0 | 535 | uncompr = (Byte*)calloc((uInt)uncomprLen, 1); |
michael@0 | 536 | /* compr and uncompr are cleared to avoid reading uninitialized |
michael@0 | 537 | * data and to ensure that uncompr compresses well. |
michael@0 | 538 | */ |
michael@0 | 539 | if (compr == Z_NULL || uncompr == Z_NULL) { |
michael@0 | 540 | printf("out of memory\n"); |
michael@0 | 541 | exit(1); |
michael@0 | 542 | } |
michael@0 | 543 | test_compress(compr, comprLen, uncompr, uncomprLen); |
michael@0 | 544 | |
michael@0 | 545 | test_gzio((argc > 1 ? argv[1] : TESTFILE), |
michael@0 | 546 | uncompr, uncomprLen); |
michael@0 | 547 | |
michael@0 | 548 | test_deflate(compr, comprLen); |
michael@0 | 549 | test_inflate(compr, comprLen, uncompr, uncomprLen); |
michael@0 | 550 | |
michael@0 | 551 | test_large_deflate(compr, comprLen, uncompr, uncomprLen); |
michael@0 | 552 | test_large_inflate(compr, comprLen, uncompr, uncomprLen); |
michael@0 | 553 | |
michael@0 | 554 | test_flush(compr, &comprLen); |
michael@0 | 555 | test_sync(compr, comprLen, uncompr, uncomprLen); |
michael@0 | 556 | comprLen = uncomprLen; |
michael@0 | 557 | |
michael@0 | 558 | test_dict_deflate(compr, comprLen); |
michael@0 | 559 | test_dict_inflate(compr, comprLen, uncompr, uncomprLen); |
michael@0 | 560 | |
michael@0 | 561 | free(compr); |
michael@0 | 562 | free(uncompr); |
michael@0 | 563 | |
michael@0 | 564 | return 0; |
michael@0 | 565 | } |