example.c 16 KB

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