gzio.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /* gzio.c -- IO on .gz files
  2. * Copyright (C) 1995-2003 Jean-loup Gailly.
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. *
  5. * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
  6. */
  7. /* \param (#) $Id$ */
  8. #include <stdio.h>
  9. #include "zutil.h"
  10. #include "gzguts.h"
  11. #ifdef NO_DEFLATE /* for compatiblity with old definition */
  12. # define NO_GZCOMPRESS
  13. #endif
  14. #ifndef NO_DUMMY_DECL
  15. struct internal_state {int dummy;}; /* for buggy compilers */
  16. #endif
  17. #ifndef Z_BUFSIZE
  18. # ifdef MAXSEG_64K
  19. # define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
  20. # else
  21. # define Z_BUFSIZE 16384
  22. # endif
  23. #endif
  24. #ifndef Z_PRINTF_BUFSIZE
  25. # define Z_PRINTF_BUFSIZE 4096
  26. #endif
  27. #ifdef __MVS__
  28. # pragma map (fdopen , "\174\174FDOPEN")
  29. FILE *fdopen(int, const char *);
  30. #endif
  31. #ifndef STDC
  32. extern voidp malloc OF((uInt size));
  33. extern void free OF((voidpf ptr));
  34. #endif
  35. #define ALLOC(size) malloc(size)
  36. #define TRYFREE(p) {if (p) free(p);}
  37. static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
  38. /* gzip flag byte */
  39. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  40. #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
  41. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  42. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  43. #define COMMENT 0x10 /* bit 4 set: file comment present */
  44. #define RESERVED 0xE0 /* bits 5..7: reserved */
  45. typedef struct gz_stream {
  46. z_stream stream;
  47. int z_err; /* error code for last stream operation */
  48. int z_eof; /* set if end of input file */
  49. FILE *file; /* .gz file */
  50. Byte *inbuf; /* input buffer */
  51. Byte *outbuf; /* output buffer */
  52. uLong crc; /* crc32 of uncompressed data */
  53. char *msg; /* error message */
  54. char *path; /* path name for debugging only */
  55. int transparent; /* 1 if input file is not a .gz file */
  56. char mode; /* 'w' or 'r' */
  57. z_off_t start; /* start of compressed data in file (header skipped) */
  58. z_off_t in; /* bytes into deflate or inflate */
  59. z_off_t out; /* bytes out of deflate or inflate */
  60. int back; /* one character push-back */
  61. int last; /* true if push-back is last character */
  62. } gz_stream;
  63. local gzFile gz_open OF((const char *path, const char *mode, int fd));
  64. local int do_flush OF((gzFile file, int flush));
  65. local int get_byte OF((gz_stream *s));
  66. local void check_header OF((gz_stream *s));
  67. local int destroy OF((gz_stream *s));
  68. local void putLong OF((FILE *file, uLong x));
  69. local uLong getLong OF((gz_stream *s));
  70. /* ===========================================================================
  71. Opens a gzip (.gz) file for reading or writing. The mode parameter
  72. is as in fopen ("rb" or "wb"). The file is given either by file descriptor
  73. or path name (if fd == -1).
  74. gz_open returns NULL if the file could not be opened or if there was
  75. insufficient memory to allocate the (de)compression state; errno
  76. can be checked to distinguish the two cases (if errno is zero, the
  77. zlib error is Z_MEM_ERROR).
  78. */
  79. local gzFile gz_open (path, mode, fd)
  80. const char *path;
  81. const char *mode;
  82. int fd;
  83. {
  84. int err;
  85. int level = Z_DEFAULT_COMPRESSION; /* compression level */
  86. int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
  87. char *p = (char*)mode;
  88. gz_stream *s;
  89. char fmode[80]; /* copy of mode, without the compression level */
  90. char *m = fmode;
  91. if (!path || !mode) return Z_NULL;
  92. s = (gz_stream *)ALLOC(sizeof(gz_stream));
  93. if (!s) return Z_NULL;
  94. s->stream.zalloc = (alloc_func)0;
  95. s->stream.zfree = (free_func)0;
  96. s->stream.opaque = (voidpf)0;
  97. s->stream.next_in = s->inbuf = Z_NULL;
  98. s->stream.next_out = s->outbuf = Z_NULL;
  99. s->stream.avail_in = s->stream.avail_out = 0;
  100. s->file = NULL;
  101. s->z_err = Z_OK;
  102. s->z_eof = 0;
  103. s->in = 0;
  104. s->out = 0;
  105. s->back = EOF;
  106. s->crc = crc32(0L, Z_NULL, 0);
  107. s->msg = NULL;
  108. s->transparent = 0;
  109. s->path = (char*)ALLOC(strlen(path)+1);
  110. if (s->path == NULL) {
  111. return destroy(s), (gzFile)Z_NULL;
  112. }
  113. strcpy(s->path, path); /* do this early for debugging */
  114. s->mode = '\0';
  115. do {
  116. if (*p == 'r') s->mode = 'r';
  117. if (*p == 'w' || *p == 'a') s->mode = 'w';
  118. if (*p >= '0' && *p <= '9') {
  119. level = *p - '0';
  120. } else if (*p == 'f') {
  121. strategy = Z_FILTERED;
  122. } else if (*p == 'h') {
  123. strategy = Z_HUFFMAN_ONLY;
  124. } else if (*p == 'R') {
  125. strategy = Z_RLE;
  126. } else {
  127. *m++ = *p; /* copy the mode */
  128. }
  129. } while (*p++ && m != fmode + sizeof(fmode));
  130. if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
  131. if (s->mode == 'w') {
  132. #ifdef NO_GZCOMPRESS
  133. err = Z_STREAM_ERROR;
  134. #else
  135. err = deflateInit2(&(s->stream), level,
  136. Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
  137. /* windowBits is passed < 0 to suppress zlib header */
  138. s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
  139. #endif
  140. if (err != Z_OK || s->outbuf == Z_NULL) {
  141. return destroy(s), (gzFile)Z_NULL;
  142. }
  143. } else {
  144. s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
  145. err = inflateInit2(&(s->stream), -MAX_WBITS);
  146. /* windowBits is passed < 0 to tell that there is no zlib header.
  147. * Note that in this case inflate *requires* an extra "dummy" byte
  148. * after the compressed stream in order to complete decompression and
  149. * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
  150. * present after the compressed stream.
  151. */
  152. if (err != Z_OK || s->inbuf == Z_NULL) {
  153. return destroy(s), (gzFile)Z_NULL;
  154. }
  155. }
  156. s->stream.avail_out = Z_BUFSIZE;
  157. errno = 0;
  158. s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
  159. if (s->file == NULL) {
  160. return destroy(s), (gzFile)Z_NULL;
  161. }
  162. if (s->mode == 'w') {
  163. /* Write a very simple .gz header:
  164. */
  165. fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
  166. Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
  167. s->start = 10L;
  168. /* We use 10L instead of ftell(s->file) to because ftell causes an
  169. * fflush on some systems. This version of the library doesn't use
  170. * start anyway in write mode, so this initialization is not
  171. * necessary.
  172. */
  173. } else {
  174. check_header(s); /* skip the .gz header */
  175. s->start = ftell(s->file) - s->stream.avail_in;
  176. }
  177. return (gzFile)s;
  178. }
  179. /* ===========================================================================
  180. Opens a gzip (.gz) file for reading or writing.
  181. */
  182. gzFile ZEXPORT gzopen (path, mode)
  183. const char *path;
  184. const char *mode;
  185. {
  186. return gz_open (path, mode, -1);
  187. }
  188. /* ===========================================================================
  189. Associate a gzFile with the file descriptor fd. fd is not dup'ed here
  190. to mimic the behavio(u)r of fdopen.
  191. */
  192. gzFile ZEXPORT gzdopen (fd, mode)
  193. int fd;
  194. const char *mode;
  195. {
  196. char name[20];
  197. if (fd < 0) return (gzFile)Z_NULL;
  198. sprintf(name, "<fd:%d>", fd); /* for debugging */
  199. return gz_open (name, mode, fd);
  200. }
  201. /* ===========================================================================
  202. * Update the compression level and strategy
  203. */
  204. int ZEXPORT gzsetparams (file, level, strategy)
  205. gzFile file;
  206. int level;
  207. int strategy;
  208. {
  209. gz_stream *s = (gz_stream*)file;
  210. if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
  211. /* Make room to allow flushing */
  212. if (s->stream.avail_out == 0) {
  213. s->stream.next_out = s->outbuf;
  214. if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
  215. s->z_err = Z_ERRNO;
  216. }
  217. s->stream.avail_out = Z_BUFSIZE;
  218. }
  219. return deflateParams (&(s->stream), level, strategy);
  220. }
  221. /* ===========================================================================
  222. Read a byte from a gz_stream; update next_in and avail_in. Return EOF
  223. for end of file.
  224. IN assertion: the stream s has been sucessfully opened for reading.
  225. */
  226. local int get_byte(s)
  227. gz_stream *s;
  228. {
  229. if (s->z_eof) return EOF;
  230. if (s->stream.avail_in == 0) {
  231. errno = 0;
  232. s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
  233. if (s->stream.avail_in == 0) {
  234. s->z_eof = 1;
  235. if (ferror(s->file)) s->z_err = Z_ERRNO;
  236. return EOF;
  237. }
  238. s->stream.next_in = s->inbuf;
  239. }
  240. s->stream.avail_in--;
  241. return *(s->stream.next_in)++;
  242. }
  243. /* ===========================================================================
  244. Check the gzip header of a gz_stream opened for reading. Set the stream
  245. mode to transparent if the gzip magic header is not present; set s->err
  246. to Z_DATA_ERROR if the magic header is present but the rest of the header
  247. is incorrect.
  248. IN assertion: the stream s has already been created sucessfully;
  249. s->stream.avail_in is zero for the first time, but may be non-zero
  250. for concatenated .gz files.
  251. */
  252. local void check_header(s)
  253. gz_stream *s;
  254. {
  255. int method; /* method byte */
  256. int flags; /* flags byte */
  257. uInt len;
  258. int c;
  259. /* Assure two bytes in the buffer so we can peek ahead -- handle case
  260. where first byte of header is at the end of the buffer after the last
  261. gzip segment */
  262. len = s->stream.avail_in;
  263. if (len < 2) {
  264. if (len) s->inbuf[0] = s->stream.next_in[0];
  265. errno = 0;
  266. len = fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
  267. if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
  268. s->stream.avail_in += len;
  269. s->stream.next_in = s->inbuf;
  270. if (s->stream.avail_in < 2) {
  271. s->transparent = s->stream.avail_in;
  272. return;
  273. }
  274. }
  275. /* Peek ahead to check the gzip magic header */
  276. if (s->stream.next_in[0] != gz_magic[0] ||
  277. s->stream.next_in[1] != gz_magic[1]) {
  278. s->transparent = 1;
  279. return;
  280. }
  281. s->stream.avail_in -= 2;
  282. s->stream.next_in += 2;
  283. /* Check the rest of the gzip header */
  284. method = get_byte(s);
  285. flags = get_byte(s);
  286. if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
  287. s->z_err = Z_DATA_ERROR;
  288. return;
  289. }
  290. /* Discard time, xflags and OS code: */
  291. for (len = 0; len < 6; len++) (void)get_byte(s);
  292. if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
  293. len = (uInt)get_byte(s);
  294. len += ((uInt)get_byte(s))<<8;
  295. /* len is garbage if EOF but the loop below will quit anyway */
  296. while (len-- != 0 && get_byte(s) != EOF) ;
  297. }
  298. if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
  299. while ((c = get_byte(s)) != 0 && c != EOF) ;
  300. }
  301. if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
  302. while ((c = get_byte(s)) != 0 && c != EOF) ;
  303. }
  304. if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
  305. for (len = 0; len < 2; len++) (void)get_byte(s);
  306. }
  307. s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
  308. }
  309. /* ===========================================================================
  310. * Cleanup then free the given gz_stream. Return a zlib error code.
  311. Try freeing in the reverse order of allocations.
  312. */
  313. local int destroy (s)
  314. gz_stream *s;
  315. {
  316. int err = Z_OK;
  317. if (!s) return Z_STREAM_ERROR;
  318. TRYFREE(s->msg);
  319. if (s->stream.state != NULL) {
  320. if (s->mode == 'w') {
  321. #ifdef NO_GZCOMPRESS
  322. err = Z_STREAM_ERROR;
  323. #else
  324. err = deflateEnd(&(s->stream));
  325. #endif
  326. } else if (s->mode == 'r') {
  327. err = inflateEnd(&(s->stream));
  328. }
  329. }
  330. if (s->file != NULL && fclose(s->file)) {
  331. #ifdef ESPIPE
  332. if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
  333. #endif
  334. err = Z_ERRNO;
  335. }
  336. if (s->z_err < 0) err = s->z_err;
  337. TRYFREE(s->inbuf);
  338. TRYFREE(s->outbuf);
  339. TRYFREE(s->path);
  340. TRYFREE(s);
  341. return err;
  342. }
  343. /* ===========================================================================
  344. Reads the given number of uncompressed bytes from the compressed file.
  345. gzread returns the number of bytes actually read (0 for end of file).
  346. */
  347. int ZEXPORT gzread (file, buf, len)
  348. gzFile file;
  349. voidp buf;
  350. unsigned len;
  351. {
  352. gz_stream *s = (gz_stream*)file;
  353. Bytef *start = (Bytef*)buf; /* starting point for crc computation */
  354. Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */
  355. if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
  356. if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
  357. if (s->z_err == Z_STREAM_END) return 0; /* EOF */
  358. next_out = (Byte*)buf;
  359. s->stream.next_out = (Bytef*)buf;
  360. s->stream.avail_out = len;
  361. if (s->stream.avail_out && s->back != EOF) {
  362. *next_out++ = s->back;
  363. s->stream.next_out++;
  364. s->stream.avail_out--;
  365. s->back = EOF;
  366. s->out++;
  367. if (s->last) {
  368. s->z_err = Z_STREAM_END;
  369. return 1;
  370. }
  371. }
  372. while (s->stream.avail_out != 0) {
  373. if (s->transparent) {
  374. /* Copy first the lookahead bytes: */
  375. uInt n = s->stream.avail_in;
  376. if (n > s->stream.avail_out) n = s->stream.avail_out;
  377. if (n > 0) {
  378. zmemcpy(s->stream.next_out, s->stream.next_in, n);
  379. next_out += n;
  380. s->stream.next_out = next_out;
  381. s->stream.next_in += n;
  382. s->stream.avail_out -= n;
  383. s->stream.avail_in -= n;
  384. }
  385. if (s->stream.avail_out > 0) {
  386. s->stream.avail_out -= fread(next_out, 1, s->stream.avail_out,
  387. s->file);
  388. }
  389. len -= s->stream.avail_out;
  390. s->in += len;
  391. s->out += len;
  392. if (len == 0) s->z_eof = 1;
  393. return (int)len;
  394. }
  395. if (s->stream.avail_in == 0 && !s->z_eof) {
  396. errno = 0;
  397. s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
  398. if (s->stream.avail_in == 0) {
  399. s->z_eof = 1;
  400. if (ferror(s->file)) {
  401. s->z_err = Z_ERRNO;
  402. break;
  403. }
  404. }
  405. s->stream.next_in = s->inbuf;
  406. }
  407. s->in += s->stream.avail_in;
  408. s->out += s->stream.avail_out;
  409. s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
  410. s->in -= s->stream.avail_in;
  411. s->out -= s->stream.avail_out;
  412. if (s->z_err == Z_STREAM_END) {
  413. /* Check CRC and original size */
  414. s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
  415. start = s->stream.next_out;
  416. if (getLong(s) != s->crc) {
  417. s->z_err = Z_DATA_ERROR;
  418. } else {
  419. (void)getLong(s);
  420. /* The uncompressed length returned by above getlong() may be
  421. * different from s->out in case of concatenated .gz files.
  422. * Check for such files:
  423. */
  424. check_header(s);
  425. if (s->z_err == Z_OK) {
  426. inflateReset(&(s->stream));
  427. s->crc = crc32(0L, Z_NULL, 0);
  428. }
  429. }
  430. }
  431. if (s->z_err != Z_OK || s->z_eof) break;
  432. }
  433. s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
  434. return (int)(len - s->stream.avail_out);
  435. }
  436. /* ===========================================================================
  437. Reads one byte from the compressed file. gzgetc returns this byte
  438. or -1 in case of end of file or error.
  439. */
  440. int ZEXPORT gzgetc(file)
  441. gzFile file;
  442. {
  443. unsigned char c;
  444. return gzread(file, &c, 1) == 1 ? c : -1;
  445. }
  446. /* ===========================================================================
  447. Push one byte back onto the stream.
  448. */
  449. int ZEXPORT gzungetc(c, file)
  450. int c;
  451. gzFile file;
  452. {
  453. gz_stream *s = (gz_stream*)file;
  454. if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
  455. s->back = c;
  456. s->out--;
  457. s->last = (s->z_err == Z_STREAM_END);
  458. if (s->last) s->z_err = Z_OK;
  459. s->z_eof = 0;
  460. return c;
  461. }
  462. /* ===========================================================================
  463. Reads bytes from the compressed file until len-1 characters are
  464. read, or a newline character is read and transferred to buf, or an
  465. end-of-file condition is encountered. The string is then terminated
  466. with a null character.
  467. gzgets returns buf, or Z_NULL in case of error.
  468. The current implementation is not optimized at all.
  469. */
  470. char * ZEXPORT gzgets(file, buf, len)
  471. gzFile file;
  472. char *buf;
  473. int len;
  474. {
  475. char *b = buf;
  476. if (buf == Z_NULL || len <= 0) return Z_NULL;
  477. while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
  478. *buf = '\0';
  479. return b == buf && len > 0 ? Z_NULL : b;
  480. }
  481. #ifndef NO_GZCOMPRESS
  482. /* ===========================================================================
  483. Writes the given number of uncompressed bytes into the compressed file.
  484. gzwrite returns the number of bytes actually written (0 in case of error).
  485. */
  486. int ZEXPORT gzwrite (file, buf, len)
  487. gzFile file;
  488. voidpc buf;
  489. unsigned len;
  490. {
  491. gz_stream *s = (gz_stream*)file;
  492. if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
  493. s->stream.next_in = (Bytef*)buf;
  494. s->stream.avail_in = len;
  495. while (s->stream.avail_in != 0) {
  496. if (s->stream.avail_out == 0) {
  497. s->stream.next_out = s->outbuf;
  498. if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
  499. s->z_err = Z_ERRNO;
  500. break;
  501. }
  502. s->stream.avail_out = Z_BUFSIZE;
  503. }
  504. s->in += s->stream.avail_in;
  505. s->out += s->stream.avail_out;
  506. s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
  507. s->in -= s->stream.avail_in;
  508. s->out -= s->stream.avail_out;
  509. if (s->z_err != Z_OK) break;
  510. }
  511. s->crc = crc32(s->crc, (const Bytef *)buf, len);
  512. return (int)(len - s->stream.avail_in);
  513. }
  514. /* ===========================================================================
  515. Converts, formats, and writes the args to the compressed file under
  516. control of the format string, as in fprintf. gzprintf returns the number of
  517. uncompressed bytes actually written (0 in case of error).
  518. */
  519. #ifdef STDC
  520. #include <stdarg.h>
  521. int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
  522. {
  523. char buf[Z_PRINTF_BUFSIZE];
  524. va_list va;
  525. int len;
  526. buf[sizeof(buf) - 1] = 0;
  527. va_start(va, format);
  528. #ifdef NO_vsnprintf
  529. # ifdef HAS_vsprintf_void
  530. (void)vsprintf(buf, format, va);
  531. va_end(va);
  532. for (len = 0; len < sizeof(buf); len++)
  533. if (buf[len] == 0) break;
  534. # else
  535. len = vsprintf(buf, format, va);
  536. va_end(va);
  537. # endif
  538. #else
  539. # ifdef HAS_vsnprintf_void
  540. (void)vsnprintf(buf, sizeof(buf), format, va);
  541. va_end(va);
  542. len = strlen(buf);
  543. # else
  544. len = vsnprintf(buf, sizeof(buf), format, va);
  545. va_end(va);
  546. # endif
  547. #endif
  548. if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0)
  549. return 0;
  550. return gzwrite(file, buf, (unsigned)len);
  551. }
  552. #else /* not ANSI C */
  553. int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
  554. a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
  555. gzFile file;
  556. const char *format;
  557. int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
  558. a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
  559. {
  560. char buf[Z_PRINTF_BUFSIZE];
  561. int len;
  562. buf[sizeof(buf) - 1] = 0;
  563. #ifdef NO_snprintf
  564. # ifdef HAS_sprintf_void
  565. sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
  566. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  567. for (len = 0; len < sizeof(buf); len++)
  568. if (buf[len] == 0) break;
  569. # else
  570. len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
  571. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  572. # endif
  573. #else
  574. # ifdef HAS_snprintf_void
  575. snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
  576. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  577. len = strlen(buf);
  578. # else
  579. len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
  580. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  581. # endif
  582. #endif
  583. if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0)
  584. return 0;
  585. return gzwrite(file, buf, len);
  586. }
  587. #endif
  588. /* ===========================================================================
  589. Writes c, converted to an unsigned char, into the compressed file.
  590. gzputc returns the value that was written, or -1 in case of error.
  591. */
  592. int ZEXPORT gzputc(file, c)
  593. gzFile file;
  594. int c;
  595. {
  596. unsigned char cc = (unsigned char) c; /* required for big endian systems */
  597. return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
  598. }
  599. /* ===========================================================================
  600. Writes the given null-terminated string to the compressed file, excluding
  601. the terminating null character.
  602. gzputs returns the number of characters written, or -1 in case of error.
  603. */
  604. int ZEXPORT gzputs(file, s)
  605. gzFile file;
  606. const char *s;
  607. {
  608. return gzwrite(file, (char*)s, (unsigned)strlen(s));
  609. }
  610. /* ===========================================================================
  611. Flushes all pending output into the compressed file. The parameter
  612. flush is as in the deflate() function.
  613. */
  614. local int do_flush (file, flush)
  615. gzFile file;
  616. int flush;
  617. {
  618. uInt len;
  619. int done = 0;
  620. gz_stream *s = (gz_stream*)file;
  621. if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
  622. s->stream.avail_in = 0; /* should be zero already anyway */
  623. for (;;) {
  624. len = Z_BUFSIZE - s->stream.avail_out;
  625. if (len != 0) {
  626. if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
  627. s->z_err = Z_ERRNO;
  628. return Z_ERRNO;
  629. }
  630. s->stream.next_out = s->outbuf;
  631. s->stream.avail_out = Z_BUFSIZE;
  632. }
  633. if (done) break;
  634. s->out += s->stream.avail_out;
  635. s->z_err = deflate(&(s->stream), flush);
  636. s->out -= s->stream.avail_out;
  637. /* Ignore the second of two consecutive flushes: */
  638. if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
  639. /* deflate has finished flushing only when it hasn't used up
  640. * all the available space in the output buffer:
  641. */
  642. done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
  643. if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
  644. }
  645. return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
  646. }
  647. int ZEXPORT gzflush (file, flush)
  648. gzFile file;
  649. int flush;
  650. {
  651. gz_stream *s = (gz_stream*)file;
  652. int err = do_flush (file, flush);
  653. if (err) return err;
  654. fflush(s->file);
  655. return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
  656. }
  657. #endif /* NO_GZCOMPRESS */
  658. /* ===========================================================================
  659. Sets the starting position for the next gzread or gzwrite on the given
  660. compressed file. The offset represents a number of bytes in the
  661. gzseek returns the resulting offset location as measured in bytes from
  662. the beginning of the uncompressed stream, or -1 in case of error.
  663. SEEK_END is not implemented, returns error.
  664. In this version of the library, gzseek can be extremely slow.
  665. */
  666. z_off_t ZEXPORT gzseek (file, offset, whence)
  667. gzFile file;
  668. z_off_t offset;
  669. int whence;
  670. {
  671. gz_stream *s = (gz_stream*)file;
  672. if (s == NULL || whence == SEEK_END ||
  673. s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
  674. return -1L;
  675. }
  676. if (s->mode == 'w') {
  677. #ifdef NO_GZCOMPRESS
  678. return -1L;
  679. #else
  680. if (whence == SEEK_SET) {
  681. offset -= s->in;
  682. }
  683. if (offset < 0) return -1L;
  684. /* At this point, offset is the number of zero bytes to write. */
  685. if (s->inbuf == Z_NULL) {
  686. s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
  687. if (s->inbuf == Z_NULL) return -1L;
  688. zmemzero(s->inbuf, Z_BUFSIZE);
  689. }
  690. while (offset > 0) {
  691. uInt size = Z_BUFSIZE;
  692. if (offset < Z_BUFSIZE) size = (uInt)offset;
  693. size = gzwrite(file, s->inbuf, size);
  694. if (size == 0) return -1L;
  695. offset -= size;
  696. }
  697. return s->in;
  698. #endif
  699. }
  700. /* Rest of function is for reading only */
  701. /* compute absolute position */
  702. if (whence == SEEK_CUR) {
  703. offset += s->out;
  704. }
  705. if (offset < 0) return -1L;
  706. if (s->transparent) {
  707. /* map to fseek */
  708. s->back = EOF;
  709. s->stream.avail_in = 0;
  710. s->stream.next_in = s->inbuf;
  711. if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
  712. s->in = s->out = offset;
  713. return offset;
  714. }
  715. /* For a negative seek, rewind and use positive seek */
  716. if (offset >= s->out) {
  717. offset -= s->out;
  718. } else if (gzrewind(file) < 0) {
  719. return -1L;
  720. }
  721. /* offset is now the number of bytes to skip. */
  722. if (offset != 0 && s->outbuf == Z_NULL) {
  723. s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
  724. if (s->outbuf == Z_NULL) return -1L;
  725. }
  726. if (offset && s->back != EOF) {
  727. s->back = EOF;
  728. s->out++;
  729. offset--;
  730. if (s->last) s->z_err = Z_STREAM_END;
  731. }
  732. while (offset > 0) {
  733. int size = Z_BUFSIZE;
  734. if (offset < Z_BUFSIZE) size = (int)offset;
  735. size = gzread(file, s->outbuf, (uInt)size);
  736. if (size <= 0) return -1L;
  737. offset -= size;
  738. }
  739. return s->out;
  740. }
  741. /* ===========================================================================
  742. Rewinds input file.
  743. */
  744. int ZEXPORT gzrewind (file)
  745. gzFile file;
  746. {
  747. gz_stream *s = (gz_stream*)file;
  748. if (s == NULL || s->mode != 'r') return -1;
  749. s->z_err = Z_OK;
  750. s->z_eof = 0;
  751. s->back = EOF;
  752. s->stream.avail_in = 0;
  753. s->stream.next_in = s->inbuf;
  754. s->crc = crc32(0L, Z_NULL, 0);
  755. if (!s->transparent) (void)inflateReset(&s->stream);
  756. s->in = 0;
  757. s->out = 0;
  758. return fseek(s->file, s->start, SEEK_SET);
  759. }
  760. /* ===========================================================================
  761. Returns the starting position for the next gzread or gzwrite on the
  762. given compressed file. This position represents a number of bytes in the
  763. uncompressed data stream.
  764. */
  765. z_off_t ZEXPORT gztell (file)
  766. gzFile file;
  767. {
  768. return gzseek(file, 0L, SEEK_CUR);
  769. }
  770. /* ===========================================================================
  771. Returns 1 when EOF has previously been detected reading the given
  772. input stream, otherwise zero.
  773. */
  774. int ZEXPORT gzeof (file)
  775. gzFile file;
  776. {
  777. gz_stream *s = (gz_stream*)file;
  778. /* With concatenated compressed files that can have embedded
  779. * crc trailers, z_eof is no longer the only/best indicator of EOF
  780. * on a gz_stream. Handle end-of-stream error explicitly here.
  781. */
  782. if (s == NULL || s->mode != 'r') return 0;
  783. if (s->z_eof) return 1;
  784. return s->z_err == Z_STREAM_END;
  785. }
  786. /* ===========================================================================
  787. Outputs a long in LSB order to the given file
  788. */
  789. local void putLong (file, x)
  790. FILE *file;
  791. uLong x;
  792. {
  793. int n;
  794. for (n = 0; n < 4; n++) {
  795. fputc((int)(x & 0xff), file);
  796. x >>= 8;
  797. }
  798. }
  799. /* ===========================================================================
  800. Reads a long in LSB order from the given gz_stream. Sets z_err in case
  801. of error.
  802. */
  803. local uLong getLong (s)
  804. gz_stream *s;
  805. {
  806. uLong x = (uLong)get_byte(s);
  807. int c;
  808. x += ((uLong)get_byte(s))<<8;
  809. x += ((uLong)get_byte(s))<<16;
  810. c = get_byte(s);
  811. if (c == EOF) s->z_err = Z_DATA_ERROR;
  812. x += ((uLong)c)<<24;
  813. return x;
  814. }
  815. /* ===========================================================================
  816. Flushes all pending output if necessary, closes the compressed file
  817. and deallocates all the (de)compression state.
  818. */
  819. int ZEXPORT gzclose (file)
  820. gzFile file;
  821. {
  822. int err;
  823. gz_stream *s = (gz_stream*)file;
  824. if (s == NULL) return Z_STREAM_ERROR;
  825. if (s->mode == 'w') {
  826. #ifdef NO_GZCOMPRESS
  827. return Z_STREAM_ERROR;
  828. #else
  829. err = do_flush (file, Z_FINISH);
  830. if (err != Z_OK) return destroy((gz_stream*)file);
  831. putLong (s->file, s->crc);
  832. putLong (s->file, (uLong)(s->in & 0xffffffff));
  833. #endif
  834. }
  835. return destroy((gz_stream*)file);
  836. }
  837. /* ===========================================================================
  838. Returns the error message for the last error which occured on the
  839. given compressed file. errnum is set to zlib error number. If an
  840. error occured in the file system and not in the compression library,
  841. errnum is set to Z_ERRNO and the application may consult errno
  842. to get the exact error code.
  843. */
  844. const char * ZEXPORT gzerror (file, errnum)
  845. gzFile file;
  846. int *errnum;
  847. {
  848. char *m;
  849. gz_stream *s = (gz_stream*)file;
  850. if (s == NULL) {
  851. *errnum = Z_STREAM_ERROR;
  852. return (const char*)ERR_MSG(Z_STREAM_ERROR);
  853. }
  854. *errnum = s->z_err;
  855. if (*errnum == Z_OK) return (const char*)"";
  856. m = (char*)(*errnum == Z_ERRNO ? zstrerror() : s->stream.msg);
  857. if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
  858. TRYFREE(s->msg);
  859. s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
  860. if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
  861. strcpy(s->msg, s->path);
  862. strcat(s->msg, ": ");
  863. strcat(s->msg, m);
  864. return (const char*)s->msg;
  865. }
  866. /* ===========================================================================
  867. Clear the error and end-of-file flags, and do the same for the real file.
  868. */
  869. void ZEXPORT gzclearerr (file)
  870. gzFile file;
  871. {
  872. gz_stream *s = (gz_stream*)file;
  873. if (s == NULL) return;
  874. if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;
  875. s->z_eof = 0;
  876. clearerr(s->file);
  877. }