gzread.c.in 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /* gzread.c -- zlib functions for reading gzip files
  2. * Copyright (C) 2004-2017 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. #include "zbuild.h"
  6. #include "zutil_p.h"
  7. #include "gzguts.h"
  8. /* Local functions */
  9. static int gz_load(gz_state *, unsigned char *, unsigned, unsigned *);
  10. static int gz_avail(gz_state *);
  11. static int gz_look(gz_state *);
  12. static int gz_decomp(gz_state *);
  13. static int gz_fetch(gz_state *);
  14. static int gz_skip(gz_state *, z_off64_t);
  15. static size_t gz_read(gz_state *, void *, size_t);
  16. /* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from
  17. state->fd, and update state->eof, state->err, and state->msg as appropriate.
  18. This function needs to loop on read(), since read() is not guaranteed to
  19. read the number of bytes requested, depending on the type of descriptor. */
  20. static int gz_load(gz_state *state, unsigned char *buf, unsigned len, unsigned *have) {
  21. ssize_t ret;
  22. *have = 0;
  23. do {
  24. ret = read(state->fd, buf + *have, len - *have);
  25. if (ret <= 0)
  26. break;
  27. *have += (unsigned)ret;
  28. } while (*have < len);
  29. if (ret < 0) {
  30. gz_error(state, Z_ERRNO, zstrerror());
  31. return -1;
  32. }
  33. if (ret == 0)
  34. state->eof = 1;
  35. return 0;
  36. }
  37. /* Load up input buffer and set eof flag if last data loaded -- return -1 on
  38. error, 0 otherwise. Note that the eof flag is set when the end of the input
  39. file is reached, even though there may be unused data in the buffer. Once
  40. that data has been used, no more attempts will be made to read the file.
  41. If strm->avail_in != 0, then the current data is moved to the beginning of
  42. the input buffer, and then the remainder of the buffer is loaded with the
  43. available data from the input file. */
  44. static int gz_avail(gz_state *state) {
  45. unsigned got;
  46. PREFIX3(stream) *strm = &(state->strm);
  47. if (state->err != Z_OK && state->err != Z_BUF_ERROR)
  48. return -1;
  49. if (state->eof == 0) {
  50. if (strm->avail_in) { /* copy what's there to the start */
  51. unsigned char *p = state->in;
  52. unsigned const char *q = strm->next_in;
  53. unsigned n = strm->avail_in;
  54. do {
  55. *p++ = *q++;
  56. } while (--n);
  57. }
  58. if (gz_load(state, state->in + strm->avail_in, state->size - strm->avail_in, &got) == -1)
  59. return -1;
  60. strm->avail_in += got;
  61. strm->next_in = state->in;
  62. }
  63. return 0;
  64. }
  65. /* Look for gzip header, set up for inflate or copy. state->x.have must be 0.
  66. If this is the first time in, allocate required memory. state->how will be
  67. left unchanged if there is no more input data available, will be set to COPY
  68. if there is no gzip header and direct copying will be performed, or it will
  69. be set to GZIP for decompression. If direct copying, then leftover input
  70. data from the input buffer will be copied to the output buffer. In that
  71. case, all further file reads will be directly to either the output buffer or
  72. a user buffer. If decompressing, the inflate state will be initialized.
  73. gz_look() will return 0 on success or -1 on failure. */
  74. static int gz_look(gz_state *state) {
  75. PREFIX3(stream) *strm = &(state->strm);
  76. /* allocate read buffers and inflate memory */
  77. if (state->size == 0) {
  78. /* allocate buffers */
  79. state->in = (unsigned char *)zng_alloc(state->want);
  80. state->out = (unsigned char *)zng_alloc(state->want << 1);
  81. if (state->in == NULL || state->out == NULL) {
  82. zng_free(state->out);
  83. zng_free(state->in);
  84. gz_error(state, Z_MEM_ERROR, "out of memory");
  85. return -1;
  86. }
  87. state->size = state->want;
  88. /* allocate inflate memory */
  89. state->strm.zalloc = NULL;
  90. state->strm.zfree = NULL;
  91. state->strm.opaque = NULL;
  92. state->strm.avail_in = 0;
  93. state->strm.next_in = NULL;
  94. if (PREFIX(inflateInit2)(&(state->strm), MAX_WBITS + 16) != Z_OK) { /* gunzip */
  95. zng_free(state->out);
  96. zng_free(state->in);
  97. state->size = 0;
  98. gz_error(state, Z_MEM_ERROR, "out of memory");
  99. return -1;
  100. }
  101. }
  102. /* get at least the magic bytes in the input buffer */
  103. if (strm->avail_in < 2) {
  104. if (gz_avail(state) == -1)
  105. return -1;
  106. if (strm->avail_in == 0)
  107. return 0;
  108. }
  109. /* look for gzip magic bytes -- if there, do gzip decoding (note: there is
  110. a logical dilemma here when considering the case of a partially written
  111. gzip file, to wit, if a single 31 byte is written, then we cannot tell
  112. whether this is a single-byte file, or just a partially written gzip
  113. file -- for here we assume that if a gzip file is being written, then
  114. the header will be written in a single operation, so that reading a
  115. single byte is sufficient indication that it is not a gzip file) */
  116. if (strm->avail_in > 1 &&
  117. strm->next_in[0] == 31 && strm->next_in[1] == 139) {
  118. PREFIX(inflateReset)(strm);
  119. state->how = GZIP;
  120. state->direct = 0;
  121. return 0;
  122. }
  123. /* no gzip header -- if we were decoding gzip before, then this is trailing
  124. garbage. Ignore the trailing garbage and finish. */
  125. if (state->direct == 0) {
  126. strm->avail_in = 0;
  127. state->eof = 1;
  128. state->x.have = 0;
  129. return 0;
  130. }
  131. /* doing raw i/o, copy any leftover input to output -- this assumes that
  132. the output buffer is larger than the input buffer, which also assures
  133. space for gzungetc() */
  134. state->x.next = state->out;
  135. memcpy(state->x.next, strm->next_in, strm->avail_in);
  136. state->x.have = strm->avail_in;
  137. strm->avail_in = 0;
  138. state->how = COPY;
  139. state->direct = 1;
  140. return 0;
  141. }
  142. /* Decompress from input to the provided next_out and avail_out in the state.
  143. On return, state->x.have and state->x.next point to the just decompressed
  144. data. If the gzip stream completes, state->how is reset to LOOK to look for
  145. the next gzip stream or raw data, once state->x.have is depleted. Returns 0
  146. on success, -1 on failure. */
  147. static int gz_decomp(gz_state *state) {
  148. int ret = Z_OK;
  149. unsigned had;
  150. PREFIX3(stream) *strm = &(state->strm);
  151. /* fill output buffer up to end of deflate stream */
  152. had = strm->avail_out;
  153. do {
  154. /* get more input for inflate() */
  155. if (strm->avail_in == 0 && gz_avail(state) == -1)
  156. return -1;
  157. if (strm->avail_in == 0) {
  158. gz_error(state, Z_BUF_ERROR, "unexpected end of file");
  159. break;
  160. }
  161. /* decompress and handle errors */
  162. ret = PREFIX(inflate)(strm, Z_NO_FLUSH);
  163. if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
  164. gz_error(state, Z_STREAM_ERROR, "internal error: inflate stream corrupt");
  165. return -1;
  166. }
  167. if (ret == Z_MEM_ERROR) {
  168. gz_error(state, Z_MEM_ERROR, "out of memory");
  169. return -1;
  170. }
  171. if (ret == Z_DATA_ERROR) { /* deflate stream invalid */
  172. gz_error(state, Z_DATA_ERROR, strm->msg == NULL ? "compressed data error" : strm->msg);
  173. return -1;
  174. }
  175. } while (strm->avail_out && ret != Z_STREAM_END);
  176. /* update available output */
  177. state->x.have = had - strm->avail_out;
  178. state->x.next = strm->next_out - state->x.have;
  179. /* if the gzip stream completed successfully, look for another */
  180. if (ret == Z_STREAM_END)
  181. state->how = LOOK;
  182. /* good decompression */
  183. return 0;
  184. }
  185. /* Fetch data and put it in the output buffer. Assumes state->x.have is 0.
  186. Data is either copied from the input file or decompressed from the input
  187. file depending on state->how. If state->how is LOOK, then a gzip header is
  188. looked for to determine whether to copy or decompress. Returns -1 on error,
  189. otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the
  190. end of the input file has been reached and all data has been processed. */
  191. static int gz_fetch(gz_state *state) {
  192. PREFIX3(stream) *strm = &(state->strm);
  193. do {
  194. switch (state->how) {
  195. case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
  196. if (gz_look(state) == -1)
  197. return -1;
  198. if (state->how == LOOK)
  199. return 0;
  200. break;
  201. case COPY: /* -> COPY */
  202. if (gz_load(state, state->out, state->size << 1, &(state->x.have))
  203. == -1)
  204. return -1;
  205. state->x.next = state->out;
  206. return 0;
  207. case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */
  208. strm->avail_out = state->size << 1;
  209. strm->next_out = state->out;
  210. if (gz_decomp(state) == -1)
  211. return -1;
  212. }
  213. } while (state->x.have == 0 && (!state->eof || strm->avail_in));
  214. return 0;
  215. }
  216. /* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
  217. static int gz_skip(gz_state *state, z_off64_t len) {
  218. unsigned n;
  219. /* skip over len bytes or reach end-of-file, whichever comes first */
  220. while (len)
  221. /* skip over whatever is in output buffer */
  222. if (state->x.have) {
  223. n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
  224. (unsigned)len : state->x.have;
  225. state->x.have -= n;
  226. state->x.next += n;
  227. state->x.pos += n;
  228. len -= n;
  229. } else if (state->eof && state->strm.avail_in == 0) {
  230. /* output buffer empty -- return if we're at the end of the input */
  231. break;
  232. } else {
  233. /* need more data to skip -- load up output buffer */
  234. /* get more output, looking for header if required */
  235. if (gz_fetch(state) == -1)
  236. return -1;
  237. }
  238. return 0;
  239. }
  240. /* Read len bytes into buf from file, or less than len up to the end of the
  241. input. Return the number of bytes read. If zero is returned, either the
  242. end of file was reached, or there was an error. state->err must be
  243. consulted in that case to determine which. */
  244. static size_t gz_read(gz_state *state, void *buf, size_t len) {
  245. size_t got;
  246. unsigned n;
  247. /* if len is zero, avoid unnecessary operations */
  248. if (len == 0)
  249. return 0;
  250. /* process a skip request */
  251. if (state->seek) {
  252. state->seek = 0;
  253. if (gz_skip(state, state->skip) == -1)
  254. return 0;
  255. }
  256. /* get len bytes to buf, or less than len if at the end */
  257. got = 0;
  258. do {
  259. /* set n to the maximum amount of len that fits in an unsigned int */
  260. n = (unsigned)-1;
  261. if (n > len)
  262. n = (unsigned)len;
  263. /* first just try copying data from the output buffer */
  264. if (state->x.have) {
  265. if (state->x.have < n)
  266. n = state->x.have;
  267. memcpy(buf, state->x.next, n);
  268. state->x.next += n;
  269. state->x.have -= n;
  270. }
  271. /* output buffer empty -- return if we're at the end of the input */
  272. else if (state->eof && state->strm.avail_in == 0) {
  273. state->past = 1; /* tried to read past end */
  274. break;
  275. }
  276. /* need output data -- for small len or new stream load up our output
  277. buffer */
  278. else if (state->how == LOOK || n < (state->size << 1)) {
  279. /* get more output, looking for header if required */
  280. if (gz_fetch(state) == -1)
  281. return 0;
  282. continue; /* no progress yet -- go back to copy above */
  283. /* the copy above assures that we will leave with space in the
  284. output buffer, allowing at least one gzungetc() to succeed */
  285. }
  286. /* large len -- read directly into user buffer */
  287. else if (state->how == COPY) { /* read directly */
  288. if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
  289. return 0;
  290. }
  291. /* large len -- decompress directly into user buffer */
  292. else { /* state->how == GZIP */
  293. state->strm.avail_out = n;
  294. state->strm.next_out = (unsigned char *)buf;
  295. if (gz_decomp(state) == -1)
  296. return 0;
  297. n = state->x.have;
  298. state->x.have = 0;
  299. }
  300. /* update progress */
  301. len -= n;
  302. buf = (char *)buf + n;
  303. got += n;
  304. state->x.pos += n;
  305. } while (len);
  306. /* return number of bytes read into user buffer */
  307. return got;
  308. }
  309. /* -- see zlib.h -- */
  310. int Z_EXPORT PREFIX(gzread)(gzFile file, void *buf, unsigned len) {
  311. gz_state *state;
  312. /* get internal structure */
  313. if (file == NULL)
  314. return -1;
  315. state = (gz_state *)file;
  316. /* check that we're reading and that there's no (serious) error */
  317. if (state->mode != GZ_READ ||
  318. (state->err != Z_OK && state->err != Z_BUF_ERROR))
  319. return -1;
  320. /* since an int is returned, make sure len fits in one, otherwise return
  321. with an error (this avoids a flaw in the interface) */
  322. if ((int)len < 0) {
  323. gz_error(state, Z_STREAM_ERROR, "request does not fit in an int");
  324. return -1;
  325. }
  326. /* read len or fewer bytes to buf */
  327. len = (unsigned)gz_read(state, buf, len);
  328. /* check for an error */
  329. if (len == 0 && state->err != Z_OK && state->err != Z_BUF_ERROR)
  330. return -1;
  331. /* return the number of bytes read (this is assured to fit in an int) */
  332. return (int)len;
  333. }
  334. /* -- see zlib.h -- */
  335. size_t Z_EXPORT PREFIX(gzfread)(void *buf, size_t size, size_t nitems, gzFile file) {
  336. size_t len;
  337. gz_state *state;
  338. /* Exit early if size is zero, also prevents potential division by zero */
  339. if (size == 0)
  340. return 0;
  341. /* get internal structure */
  342. if (file == NULL)
  343. return 0;
  344. state = (gz_state *)file;
  345. /* check that we're reading and that there's no (serious) error */
  346. if (state->mode != GZ_READ ||
  347. (state->err != Z_OK && state->err != Z_BUF_ERROR))
  348. return 0;
  349. /* compute bytes to read -- error on overflow */
  350. if (size && SIZE_MAX / size < nitems) {
  351. gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
  352. return 0;
  353. }
  354. len = nitems * size;
  355. /* read len or fewer bytes to buf, return the number of full items read */
  356. return len ? gz_read(state, buf, len) / size : 0;
  357. }
  358. /* -- see zlib.h -- */
  359. #undef @ZLIB_SYMBOL_PREFIX@gzgetc
  360. #undef @ZLIB_SYMBOL_PREFIX@zng_gzgetc
  361. int Z_EXPORT PREFIX(gzgetc)(gzFile file) {
  362. unsigned char buf[1];
  363. gz_state *state;
  364. /* get internal structure */
  365. if (file == NULL)
  366. return -1;
  367. state = (gz_state *)file;
  368. /* check that we're reading and that there's no (serious) error */
  369. if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
  370. return -1;
  371. /* try output buffer (no need to check for skip request) */
  372. if (state->x.have) {
  373. state->x.have--;
  374. state->x.pos++;
  375. return *(state->x.next)++;
  376. }
  377. /* nothing there -- try gz_read() */
  378. return gz_read(state, buf, 1) < 1 ? -1 : buf[0];
  379. }
  380. #ifdef ZLIB_COMPAT
  381. int Z_EXPORT PREFIX(gzgetc_)(gzFile file) {
  382. return PREFIX(gzgetc)(file);
  383. }
  384. #endif
  385. /* -- see zlib.h -- */
  386. int Z_EXPORT PREFIX(gzungetc)(int c, gzFile file) {
  387. gz_state *state;
  388. /* get internal structure */
  389. if (file == NULL)
  390. return -1;
  391. state = (gz_state *)file;
  392. /* in case this was just opened, set up the input buffer */
  393. if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
  394. (void)gz_look(state);
  395. /* check that we're reading and that there's no (serious) error */
  396. if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
  397. return -1;
  398. /* process a skip request */
  399. if (state->seek) {
  400. state->seek = 0;
  401. if (gz_skip(state, state->skip) == -1)
  402. return -1;
  403. }
  404. /* can't push EOF */
  405. if (c < 0)
  406. return -1;
  407. /* if output buffer empty, put byte at end (allows more pushing) */
  408. if (state->x.have == 0) {
  409. state->x.have = 1;
  410. state->x.next = state->out + (state->size << 1) - 1;
  411. state->x.next[0] = (unsigned char)c;
  412. state->x.pos--;
  413. state->past = 0;
  414. return c;
  415. }
  416. /* if no room, give up (must have already done a gzungetc()) */
  417. if (state->x.have == (state->size << 1)) {
  418. gz_error(state, Z_DATA_ERROR, "out of room to push characters");
  419. return -1;
  420. }
  421. /* slide output data if needed and insert byte before existing data */
  422. if (state->x.next == state->out) {
  423. unsigned char *src = state->out + state->x.have;
  424. unsigned char *dest = state->out + (state->size << 1);
  425. while (src > state->out)
  426. *--dest = *--src;
  427. state->x.next = dest;
  428. }
  429. state->x.have++;
  430. state->x.next--;
  431. state->x.next[0] = (unsigned char)c;
  432. state->x.pos--;
  433. state->past = 0;
  434. return c;
  435. }
  436. /* -- see zlib.h -- */
  437. char * Z_EXPORT PREFIX(gzgets)(gzFile file, char *buf, int len) {
  438. unsigned left, n;
  439. char *str;
  440. unsigned char *eol;
  441. gz_state *state;
  442. /* check parameters and get internal structure */
  443. if (file == NULL || buf == NULL || len < 1)
  444. return NULL;
  445. state = (gz_state *)file;
  446. /* check that we're reading and that there's no (serious) error */
  447. if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
  448. return NULL;
  449. /* process a skip request */
  450. if (state->seek) {
  451. state->seek = 0;
  452. if (gz_skip(state, state->skip) == -1)
  453. return NULL;
  454. }
  455. /* copy output bytes up to new line or len - 1, whichever comes first --
  456. append a terminating zero to the string (we don't check for a zero in
  457. the contents, let the user worry about that) */
  458. str = buf;
  459. left = (unsigned)len - 1;
  460. if (left) {
  461. do {
  462. /* assure that something is in the output buffer */
  463. if (state->x.have == 0 && gz_fetch(state) == -1)
  464. return NULL; /* error */
  465. if (state->x.have == 0) { /* end of file */
  466. state->past = 1; /* read past end */
  467. break; /* return what we have */
  468. }
  469. /* look for end-of-line in current output buffer */
  470. n = state->x.have > left ? left : state->x.have;
  471. eol = (unsigned char *)memchr(state->x.next, '\n', n);
  472. if (eol != NULL)
  473. n = (unsigned)(eol - state->x.next) + 1;
  474. /* copy through end-of-line, or remainder if not found */
  475. memcpy(buf, state->x.next, n);
  476. state->x.have -= n;
  477. state->x.next += n;
  478. state->x.pos += n;
  479. left -= n;
  480. buf += n;
  481. } while (left && eol == NULL);
  482. }
  483. /* return terminated string, or if nothing, end of file */
  484. if (buf == str)
  485. return NULL;
  486. buf[0] = 0;
  487. return str;
  488. }
  489. /* -- see zlib.h -- */
  490. int Z_EXPORT PREFIX(gzdirect)(gzFile file) {
  491. gz_state *state;
  492. /* get internal structure */
  493. if (file == NULL)
  494. return 0;
  495. state = (gz_state *)file;
  496. /* if the state is not known, but we can find out, then do so (this is
  497. mainly for right after a gzopen() or gzdopen()) */
  498. if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
  499. (void)gz_look(state);
  500. /* return 1 if transparent, 0 if processing a gzip stream */
  501. return state->direct;
  502. }
  503. /* -- see zlib.h -- */
  504. int Z_EXPORT PREFIX(gzclose_r)(gzFile file) {
  505. int ret, err;
  506. gz_state *state;
  507. /* get internal structure */
  508. if (file == NULL)
  509. return Z_STREAM_ERROR;
  510. state = (gz_state *)file;
  511. /* check that we're reading */
  512. if (state->mode != GZ_READ)
  513. return Z_STREAM_ERROR;
  514. /* free memory and close file */
  515. if (state->size) {
  516. PREFIX(inflateEnd)(&(state->strm));
  517. zng_free(state->out);
  518. zng_free(state->in);
  519. }
  520. err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
  521. gz_error(state, Z_OK, NULL);
  522. free(state->path);
  523. ret = close(state->fd);
  524. zng_free(state);
  525. return ret ? Z_ERRNO : err;
  526. }