gzio.c 30 KB

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