gzread.c 19 KB

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