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

875 lines
25 KiB

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