gzread.c 18 KB

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