gzio.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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
  67. 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[46]; /* allow for up to 128-bit integers */
  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 = (uInt)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 = (uInt)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
  314. int destroy (s)
  315. gz_stream *s;
  316. {
  317. int err = Z_OK;
  318. if (!s) return Z_STREAM_ERROR;
  319. TRYFREE(s->msg);
  320. if (s->stream.state != NULL) {
  321. if (s->mode == 'w') {
  322. #ifdef NO_GZCOMPRESS
  323. err = Z_STREAM_ERROR;
  324. #else
  325. err = deflateEnd(&(s->stream));
  326. #endif
  327. } else if (s->mode == 'r') {
  328. err = inflateEnd(&(s->stream));
  329. }
  330. }
  331. if (s->file != NULL && fclose(s->file)) {
  332. #ifdef ESPIPE
  333. if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
  334. #endif
  335. err = Z_ERRNO;
  336. }
  337. if (s->z_err < 0) err = s->z_err;
  338. TRYFREE(s->inbuf);
  339. TRYFREE(s->outbuf);
  340. TRYFREE(s->path);
  341. TRYFREE(s);
  342. return err;
  343. }
  344. /* ===========================================================================
  345. Reads the given number of uncompressed bytes from the compressed file.
  346. gzread returns the number of bytes actually read (0 for end of file).
  347. */
  348. int ZEXPORT gzread (file, buf, len)
  349. gzFile file;
  350. voidp buf;
  351. unsigned len;
  352. {
  353. gz_stream *s = (gz_stream*)file;
  354. Bytef *start = (Bytef*)buf; /* starting point for crc computation */
  355. Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */
  356. if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
  357. if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
  358. if (s->z_err == Z_STREAM_END) return 0; /* EOF */
  359. next_out = (Byte*)buf;
  360. s->stream.next_out = (Bytef*)buf;
  361. s->stream.avail_out = len;
  362. if (s->stream.avail_out && s->back != EOF) {
  363. *next_out++ = s->back;
  364. s->stream.next_out++;
  365. s->stream.avail_out--;
  366. s->back = EOF;
  367. s->out++;
  368. start++;
  369. if (s->last) {
  370. s->z_err = Z_STREAM_END;
  371. return 1;
  372. }
  373. }
  374. while (s->stream.avail_out != 0) {
  375. if (s->transparent) {
  376. /* Copy first the lookahead bytes: */
  377. uInt n = s->stream.avail_in;
  378. if (n > s->stream.avail_out) n = s->stream.avail_out;
  379. if (n > 0) {
  380. zmemcpy(s->stream.next_out, s->stream.next_in, n);
  381. next_out += n;
  382. s->stream.next_out = next_out;
  383. s->stream.next_in += n;
  384. s->stream.avail_out -= n;
  385. s->stream.avail_in -= n;
  386. }
  387. if (s->stream.avail_out > 0) {
  388. s->stream.avail_out -=
  389. (uInt)fread(next_out, 1, s->stream.avail_out, s->file);
  390. }
  391. len -= s->stream.avail_out;
  392. s->in += len;
  393. s->out += len;
  394. if (len == 0) s->z_eof = 1;
  395. return (int)len;
  396. }
  397. if (s->stream.avail_in == 0 && !s->z_eof) {
  398. errno = 0;
  399. s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
  400. if (s->stream.avail_in == 0) {
  401. s->z_eof = 1;
  402. if (ferror(s->file)) {
  403. s->z_err = Z_ERRNO;
  404. break;
  405. }
  406. }
  407. s->stream.next_in = s->inbuf;
  408. }
  409. s->in += s->stream.avail_in;
  410. s->out += s->stream.avail_out;
  411. s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
  412. s->in -= s->stream.avail_in;
  413. s->out -= s->stream.avail_out;
  414. if (s->z_err == Z_STREAM_END) {
  415. /* Check CRC and original size */
  416. s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
  417. start = s->stream.next_out;
  418. if (getLong(s) != s->crc) {
  419. s->z_err = Z_DATA_ERROR;
  420. } else {
  421. (void)getLong(s);
  422. /* The uncompressed length returned by above getlong() may be
  423. * different from s->out in case of concatenated .gz files.
  424. * Check for such files:
  425. */
  426. check_header(s);
  427. if (s->z_err == Z_OK) {
  428. inflateReset(&(s->stream));
  429. s->crc = crc32(0L, Z_NULL, 0);
  430. }
  431. }
  432. }
  433. if (s->z_err != Z_OK || s->z_eof) break;
  434. }
  435. s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
  436. if (len == s->stream.avail_out &&
  437. (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO))
  438. return -1;
  439. return (int)(len - s->stream.avail_out);
  440. }
  441. /* ===========================================================================
  442. Reads one byte from the compressed file. gzgetc returns this byte
  443. or -1 in case of end of file or error.
  444. */
  445. int ZEXPORT gzgetc(file)
  446. gzFile file;
  447. {
  448. unsigned char c;
  449. return gzread(file, &c, 1) == 1 ? c : -1;
  450. }
  451. /* ===========================================================================
  452. Push one byte back onto the stream.
  453. */
  454. int ZEXPORT gzungetc(c, file)
  455. int c;
  456. gzFile file;
  457. {
  458. gz_stream *s = (gz_stream*)file;
  459. if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
  460. s->back = c;
  461. s->out--;
  462. s->last = (s->z_err == Z_STREAM_END);
  463. if (s->last) s->z_err = Z_OK;
  464. s->z_eof = 0;
  465. return c;
  466. }
  467. /* ===========================================================================
  468. Reads bytes from the compressed file until len-1 characters are
  469. read, or a newline character is read and transferred to buf, or an
  470. end-of-file condition is encountered. The string is then terminated
  471. with a null character.
  472. gzgets returns buf, or Z_NULL in case of error.
  473. The current implementation is not optimized at all.
  474. */
  475. char * ZEXPORT gzgets(file, buf, len)
  476. gzFile file;
  477. char *buf;
  478. int len;
  479. {
  480. char *b = buf;
  481. if (buf == Z_NULL || len <= 0) return Z_NULL;
  482. while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
  483. *buf = '\0';
  484. return b == buf && len > 0 ? Z_NULL : b;
  485. }
  486. #ifndef NO_GZCOMPRESS
  487. /* ===========================================================================
  488. Writes the given number of uncompressed bytes into the compressed file.
  489. gzwrite returns the number of bytes actually written (0 in case of error).
  490. */
  491. int ZEXPORT gzwrite (file, buf, len)
  492. gzFile file;
  493. voidpc buf;
  494. unsigned len;
  495. {
  496. gz_stream *s = (gz_stream*)file;
  497. if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
  498. s->stream.next_in = (Bytef*)buf;
  499. s->stream.avail_in = len;
  500. while (s->stream.avail_in != 0) {
  501. if (s->stream.avail_out == 0) {
  502. s->stream.next_out = s->outbuf;
  503. if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
  504. s->z_err = Z_ERRNO;
  505. break;
  506. }
  507. s->stream.avail_out = Z_BUFSIZE;
  508. }
  509. s->in += s->stream.avail_in;
  510. s->out += s->stream.avail_out;
  511. s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
  512. s->in -= s->stream.avail_in;
  513. s->out -= s->stream.avail_out;
  514. if (s->z_err != Z_OK) break;
  515. }
  516. s->crc = crc32(s->crc, (const Bytef *)buf, len);
  517. return (int)(len - s->stream.avail_in);
  518. }
  519. /* ===========================================================================
  520. Converts, formats, and writes the args to the compressed file under
  521. control of the format string, as in fprintf. gzprintf returns the number of
  522. uncompressed bytes actually written (0 in case of error).
  523. */
  524. #ifdef STDC
  525. #include <stdarg.h>
  526. int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
  527. {
  528. char buf[Z_PRINTF_BUFSIZE];
  529. va_list va;
  530. int len;
  531. buf[sizeof(buf) - 1] = 0;
  532. va_start(va, format);
  533. #ifdef NO_vsnprintf
  534. # ifdef HAS_vsprintf_void
  535. (void)vsprintf(buf, format, va);
  536. va_end(va);
  537. for (len = 0; len < sizeof(buf); len++)
  538. if (buf[len] == 0) break;
  539. # else
  540. len = vsprintf(buf, format, va);
  541. va_end(va);
  542. # endif
  543. #else
  544. # ifdef HAS_vsnprintf_void
  545. (void)vsnprintf(buf, sizeof(buf), format, va);
  546. va_end(va);
  547. len = strlen(buf);
  548. # else
  549. len = vsnprintf(buf, sizeof(buf), format, va);
  550. va_end(va);
  551. # endif
  552. #endif
  553. if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0)
  554. return 0;
  555. return gzwrite(file, buf, (unsigned)len);
  556. }
  557. #else /* not ANSI C */
  558. int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
  559. a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
  560. gzFile file;
  561. const char *format;
  562. int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
  563. a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
  564. {
  565. char buf[Z_PRINTF_BUFSIZE];
  566. int len;
  567. buf[sizeof(buf) - 1] = 0;
  568. #ifdef NO_snprintf
  569. # ifdef HAS_sprintf_void
  570. 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. for (len = 0; len < sizeof(buf); len++)
  573. if (buf[len] == 0) break;
  574. # else
  575. len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
  576. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  577. # endif
  578. #else
  579. # ifdef HAS_snprintf_void
  580. snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
  581. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  582. len = strlen(buf);
  583. # else
  584. len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
  585. a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
  586. # endif
  587. #endif
  588. if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0)
  589. return 0;
  590. return gzwrite(file, buf, len);
  591. }
  592. #endif
  593. /* ===========================================================================
  594. Writes c, converted to an unsigned char, into the compressed file.
  595. gzputc returns the value that was written, or -1 in case of error.
  596. */
  597. int ZEXPORT gzputc(file, c)
  598. gzFile file;
  599. int c;
  600. {
  601. unsigned char cc = (unsigned char) c; /* required for big endian systems */
  602. return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
  603. }
  604. /* ===========================================================================
  605. Writes the given null-terminated string to the compressed file, excluding
  606. the terminating null character.
  607. gzputs returns the number of characters written, or -1 in case of error.
  608. */
  609. int ZEXPORT gzputs(file, s)
  610. gzFile file;
  611. const char *s;
  612. {
  613. return gzwrite(file, (char*)s, (unsigned)strlen(s));
  614. }
  615. /* ===========================================================================
  616. Flushes all pending output into the compressed file. The parameter
  617. flush is as in the deflate() function.
  618. */
  619. local int do_flush (file, flush)
  620. gzFile file;
  621. int flush;
  622. {
  623. uInt len;
  624. int done = 0;
  625. gz_stream *s = (gz_stream*)file;
  626. if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
  627. s->stream.avail_in = 0; /* should be zero already anyway */
  628. for (;;) {
  629. len = Z_BUFSIZE - s->stream.avail_out;
  630. if (len != 0) {
  631. if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
  632. s->z_err = Z_ERRNO;
  633. return Z_ERRNO;
  634. }
  635. s->stream.next_out = s->outbuf;
  636. s->stream.avail_out = Z_BUFSIZE;
  637. }
  638. if (done) break;
  639. s->out += s->stream.avail_out;
  640. s->z_err = deflate(&(s->stream), flush);
  641. s->out -= s->stream.avail_out;
  642. /* Ignore the second of two consecutive flushes: */
  643. if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
  644. /* deflate has finished flushing only when it hasn't used up
  645. * all the available space in the output buffer:
  646. */
  647. done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
  648. if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
  649. }
  650. return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
  651. }
  652. int ZEXPORT gzflush (file, flush)
  653. gzFile file;
  654. int flush;
  655. {
  656. gz_stream *s = (gz_stream*)file;
  657. int err = do_flush (file, flush);
  658. if (err) return err;
  659. fflush(s->file);
  660. return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
  661. }
  662. #endif /* NO_GZCOMPRESS */
  663. /* ===========================================================================
  664. Sets the starting position for the next gzread or gzwrite on the given
  665. compressed file. The offset represents a number of bytes in the
  666. gzseek returns the resulting offset location as measured in bytes from
  667. the beginning of the uncompressed stream, or -1 in case of error.
  668. SEEK_END is not implemented, returns error.
  669. In this version of the library, gzseek can be extremely slow.
  670. */
  671. z_off_t ZEXPORT gzseek (file, offset, whence)
  672. gzFile file;
  673. z_off_t offset;
  674. int whence;
  675. {
  676. gz_stream *s = (gz_stream*)file;
  677. if (s == NULL || whence == SEEK_END ||
  678. s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
  679. return -1L;
  680. }
  681. if (s->mode == 'w') {
  682. #ifdef NO_GZCOMPRESS
  683. return -1L;
  684. #else
  685. if (whence == SEEK_SET) {
  686. offset -= s->in;
  687. }
  688. if (offset < 0) return -1L;
  689. /* At this point, offset is the number of zero bytes to write. */
  690. if (s->inbuf == Z_NULL) {
  691. s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
  692. if (s->inbuf == Z_NULL) return -1L;
  693. zmemzero(s->inbuf, Z_BUFSIZE);
  694. }
  695. while (offset > 0) {
  696. uInt size = Z_BUFSIZE;
  697. if (offset < Z_BUFSIZE) size = (uInt)offset;
  698. size = gzwrite(file, s->inbuf, size);
  699. if (size == 0) return -1L;
  700. offset -= size;
  701. }
  702. return s->in;
  703. #endif
  704. }
  705. /* Rest of function is for reading only */
  706. /* compute absolute position */
  707. if (whence == SEEK_CUR) {
  708. offset += s->out;
  709. }
  710. if (offset < 0) return -1L;
  711. if (s->transparent) {
  712. /* map to fseek */
  713. s->back = EOF;
  714. s->stream.avail_in = 0;
  715. s->stream.next_in = s->inbuf;
  716. if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
  717. s->in = s->out = offset;
  718. return offset;
  719. }
  720. /* For a negative seek, rewind and use positive seek */
  721. if (offset >= s->out) {
  722. offset -= s->out;
  723. } else if (gzrewind(file) < 0) {
  724. return -1L;
  725. }
  726. /* offset is now the number of bytes to skip. */
  727. if (offset != 0 && s->outbuf == Z_NULL) {
  728. s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
  729. if (s->outbuf == Z_NULL) return -1L;
  730. }
  731. if (offset && s->back != EOF) {
  732. s->back = EOF;
  733. s->out++;
  734. offset--;
  735. if (s->last) s->z_err = Z_STREAM_END;
  736. }
  737. while (offset > 0) {
  738. int size = Z_BUFSIZE;
  739. if (offset < Z_BUFSIZE) size = (int)offset;
  740. size = gzread(file, s->outbuf, (uInt)size);
  741. if (size <= 0) return -1L;
  742. offset -= size;
  743. }
  744. return s->out;
  745. }
  746. /* ===========================================================================
  747. Rewinds input file.
  748. */
  749. int ZEXPORT gzrewind (file)
  750. gzFile file;
  751. {
  752. gz_stream *s = (gz_stream*)file;
  753. if (s == NULL || s->mode != 'r') return -1;
  754. s->z_err = Z_OK;
  755. s->z_eof = 0;
  756. s->back = EOF;
  757. s->stream.avail_in = 0;
  758. s->stream.next_in = s->inbuf;
  759. s->crc = crc32(0L, Z_NULL, 0);
  760. if (!s->transparent) (void)inflateReset(&s->stream);
  761. s->in = 0;
  762. s->out = 0;
  763. return fseek(s->file, s->start, SEEK_SET);
  764. }
  765. /* ===========================================================================
  766. Returns the starting position for the next gzread or gzwrite on the
  767. given compressed file. This position represents a number of bytes in the
  768. uncompressed data stream.
  769. */
  770. z_off_t ZEXPORT gztell (file)
  771. gzFile file;
  772. {
  773. return gzseek(file, 0L, SEEK_CUR);
  774. }
  775. /* ===========================================================================
  776. Returns 1 when EOF has previously been detected reading the given
  777. input stream, otherwise zero.
  778. */
  779. int ZEXPORT gzeof (file)
  780. gzFile file;
  781. {
  782. gz_stream *s = (gz_stream*)file;
  783. /* With concatenated compressed files that can have embedded
  784. * crc trailers, z_eof is no longer the only/best indicator of EOF
  785. * on a gz_stream. Handle end-of-stream error explicitly here.
  786. */
  787. if (s == NULL || s->mode != 'r') return 0;
  788. if (s->z_eof) return 1;
  789. return s->z_err == Z_STREAM_END;
  790. }
  791. /* ===========================================================================
  792. Returns 1 if reading and doing so transparently, otherwise zero.
  793. */
  794. int ZEXPORT gzdirect (file)
  795. gzFile file;
  796. {
  797. gz_stream *s = (gz_stream*)file;
  798. if (s == NULL || s->mode != 'r') return 0;
  799. return s->transparent;
  800. }
  801. /* ===========================================================================
  802. Outputs a long in LSB order to the given file
  803. */
  804. local void putLong (file, x)
  805. FILE *file;
  806. uLong x;
  807. {
  808. int n;
  809. for (n = 0; n < 4; n++) {
  810. fputc((int)(x & 0xff), file);
  811. x >>= 8;
  812. }
  813. }
  814. /* ===========================================================================
  815. Reads a long in LSB order from the given gz_stream. Sets z_err in case
  816. of error.
  817. */
  818. local uLong getLong (s)
  819. gz_stream *s;
  820. {
  821. uLong x = (uLong)get_byte(s);
  822. int c;
  823. x += ((uLong)get_byte(s))<<8;
  824. x += ((uLong)get_byte(s))<<16;
  825. c = get_byte(s);
  826. if (c == EOF) s->z_err = Z_DATA_ERROR;
  827. x += ((uLong)c)<<24;
  828. return x;
  829. }
  830. /* ===========================================================================
  831. Flushes all pending output if necessary, closes the compressed file
  832. and deallocates all the (de)compression state.
  833. */
  834. int ZEXPORT gzclose (file)
  835. gzFile file;
  836. {
  837. gz_stream *s = (gz_stream*)file;
  838. if (s == NULL) return Z_STREAM_ERROR;
  839. if (s->mode == 'w') {
  840. #ifdef NO_GZCOMPRESS
  841. return Z_STREAM_ERROR;
  842. #else
  843. if (do_flush (file, Z_FINISH) != Z_OK)
  844. return destroy((gz_stream*)file);
  845. putLong (s->file, s->crc);
  846. putLong (s->file, (uLong)(s->in & 0xffffffff));
  847. #endif
  848. }
  849. return destroy((gz_stream*)file);
  850. }
  851. #ifdef STDC
  852. # define zstrerror(errnum) strerror(errnum)
  853. #else
  854. # define zstrerror(errnum) ""
  855. #endif
  856. /* ===========================================================================
  857. Returns the error message for the last error which occurred on the
  858. given compressed file. errnum is set to zlib error number. If an
  859. error occurred in the file system and not in the compression library,
  860. errnum is set to Z_ERRNO and the application may consult errno
  861. to get the exact error code.
  862. */
  863. const char * ZEXPORT gzerror (file, errnum)
  864. gzFile file;
  865. int *errnum;
  866. {
  867. char *m;
  868. gz_stream *s = (gz_stream*)file;
  869. if (s == NULL) {
  870. *errnum = Z_STREAM_ERROR;
  871. return (const char*)ERR_MSG(Z_STREAM_ERROR);
  872. }
  873. *errnum = s->z_err;
  874. if (*errnum == Z_OK) return (const char*)"";
  875. m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
  876. if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
  877. TRYFREE(s->msg);
  878. s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
  879. if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
  880. strcpy(s->msg, s->path);
  881. strcat(s->msg, ": ");
  882. strcat(s->msg, m);
  883. return (const char*)s->msg;
  884. }
  885. /* ===========================================================================
  886. Clear the error and end-of-file flags, and do the same for the real file.
  887. */
  888. void ZEXPORT gzclearerr (file)
  889. gzFile file;
  890. {
  891. gz_stream *s = (gz_stream*)file;
  892. if (s == NULL) return;
  893. if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;
  894. s->z_eof = 0;
  895. clearerr(s->file);
  896. }