Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

556 lines
16 KiB

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