1
0

gzread.c 20 KB

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