gzread.c 20 KB

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