gzread.c 18 KB

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