xzlib.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /**
  2. * xzlib.c: front end for the transparent support of lzma compression
  3. * at the I/O layer, based on an example file from lzma project
  4. *
  5. * See Copyright for the status of this software.
  6. *
  7. * Anders F Bjorklund <afb@users.sourceforge.net>
  8. */
  9. #define IN_LIBXML
  10. #include "libxml.h"
  11. #ifdef LIBXML_LZMA_ENABLED
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <errno.h>
  15. #ifdef HAVE_SYS_STAT_H
  16. #include <sys/stat.h>
  17. #endif
  18. #ifdef HAVE_FCNTL_H
  19. #include <fcntl.h>
  20. #endif
  21. #ifdef HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #elif defined (_WIN32)
  24. #include <io.h>
  25. #endif
  26. #ifdef LIBXML_ZLIB_ENABLED
  27. #include <zlib.h>
  28. #endif
  29. #ifdef LIBXML_LZMA_ENABLED
  30. #include <lzma.h>
  31. #endif
  32. #include "private/xzlib.h"
  33. #include <libxml/xmlmemory.h>
  34. /* values for xz_state how */
  35. #define LOOK 0 /* look for a gzip/lzma header */
  36. #define COPY 1 /* copy input directly */
  37. #define GZIP 2 /* decompress a gzip stream */
  38. #define LZMA 3 /* decompress a lzma stream */
  39. /* internal lzma file state data structure */
  40. typedef struct {
  41. int mode; /* see lzma modes above */
  42. int fd; /* file descriptor */
  43. char *path; /* path or fd for error messages */
  44. uint64_t pos; /* current position in uncompressed data */
  45. unsigned int size; /* buffer size, zero if not allocated yet */
  46. unsigned int want; /* requested buffer size, default is BUFSIZ */
  47. unsigned char *in; /* input buffer */
  48. unsigned char *out; /* output buffer (double-sized when reading) */
  49. unsigned char *next; /* next output data to deliver or write */
  50. unsigned int have; /* amount of output data unused at next */
  51. int eof; /* true if end of input file reached */
  52. uint64_t start; /* where the lzma data started, for rewinding */
  53. uint64_t raw; /* where the raw data started, for seeking */
  54. int how; /* 0: get header, 1: copy, 2: decompress */
  55. int direct; /* true if last read direct, false if lzma */
  56. /* seek request */
  57. uint64_t skip; /* amount to skip (already rewound if backwards) */
  58. int seek; /* true if seek request pending */
  59. /* error information */
  60. int err; /* error code */
  61. char *msg; /* error message */
  62. /* lzma stream */
  63. int init; /* is the inflate stream initialized */
  64. lzma_stream strm; /* stream structure in-place (not a pointer) */
  65. char padding1[32]; /* padding allowing to cope with possible
  66. extensions of above structure without
  67. too much side effect */
  68. #ifdef LIBXML_ZLIB_ENABLED
  69. /* zlib inflate or deflate stream */
  70. z_stream zstrm; /* stream structure in-place (not a pointer) */
  71. #endif
  72. char padding2[32]; /* padding allowing to cope with possible
  73. extensions of above structure without
  74. too much side effect */
  75. } xz_state, *xz_statep;
  76. static void
  77. xz_error(xz_statep state, int err, const char *msg)
  78. {
  79. /* free previously allocated message and clear */
  80. if (state->msg != NULL) {
  81. if (state->err != LZMA_MEM_ERROR)
  82. xmlFree(state->msg);
  83. state->msg = NULL;
  84. }
  85. /* set error code, and if no message, then done */
  86. state->err = err;
  87. if (msg == NULL)
  88. return;
  89. /* for an out of memory error, save as static string */
  90. if (err == LZMA_MEM_ERROR) {
  91. state->msg = (char *) msg;
  92. return;
  93. }
  94. /* construct error message with path */
  95. if ((state->msg =
  96. xmlMalloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
  97. state->err = LZMA_MEM_ERROR;
  98. state->msg = (char *) "out of memory";
  99. return;
  100. }
  101. strcpy(state->msg, state->path);
  102. strcat(state->msg, ": ");
  103. strcat(state->msg, msg);
  104. return;
  105. }
  106. static void
  107. xz_reset(xz_statep state)
  108. {
  109. state->have = 0; /* no output data available */
  110. state->eof = 0; /* not at end of file */
  111. state->how = LOOK; /* look for gzip header */
  112. state->direct = 1; /* default for empty file */
  113. state->seek = 0; /* no seek request pending */
  114. xz_error(state, LZMA_OK, NULL); /* clear error */
  115. state->pos = 0; /* no uncompressed data yet */
  116. state->strm.avail_in = 0; /* no input data yet */
  117. #ifdef LIBXML_ZLIB_ENABLED
  118. state->zstrm.avail_in = 0; /* no input data yet */
  119. #endif
  120. }
  121. static xzFile
  122. xz_open(const char *path, int fd, const char *mode ATTRIBUTE_UNUSED)
  123. {
  124. xz_statep state;
  125. off_t offset;
  126. /* allocate xzFile structure to return */
  127. state = xmlMalloc(sizeof(xz_state));
  128. if (state == NULL)
  129. return NULL;
  130. state->size = 0; /* no buffers allocated yet */
  131. state->want = BUFSIZ; /* requested buffer size */
  132. state->msg = NULL; /* no error message yet */
  133. state->init = 0; /* initialization of zlib data */
  134. /* save the path name for error messages */
  135. state->path = xmlMalloc(strlen(path) + 1);
  136. if (state->path == NULL) {
  137. xmlFree(state);
  138. return NULL;
  139. }
  140. strcpy(state->path, path);
  141. /* open the file with the appropriate mode (or just use fd) */
  142. state->fd = fd != -1 ? fd : open(path,
  143. #ifdef O_LARGEFILE
  144. O_LARGEFILE |
  145. #endif
  146. #ifdef O_BINARY
  147. O_BINARY |
  148. #endif
  149. O_RDONLY, 0666);
  150. if (state->fd == -1) {
  151. xmlFree(state->path);
  152. xmlFree(state);
  153. return NULL;
  154. }
  155. /* save the current position for rewinding (only if reading) */
  156. offset = lseek(state->fd, 0, SEEK_CUR);
  157. if (offset == -1)
  158. state->start = 0;
  159. else
  160. state->start = offset;
  161. /* initialize stream */
  162. xz_reset(state);
  163. /* return stream */
  164. return (xzFile) state;
  165. }
  166. static int
  167. xz_compressed(xzFile f) {
  168. xz_statep state;
  169. if (f == NULL)
  170. return(-1);
  171. state = (xz_statep) f;
  172. if (state->init <= 0)
  173. return(-1);
  174. switch (state->how) {
  175. case COPY:
  176. return(0);
  177. case GZIP:
  178. case LZMA:
  179. return(1);
  180. }
  181. return(-1);
  182. }
  183. xzFile
  184. __libxml2_xzopen(const char *path, const char *mode)
  185. {
  186. return xz_open(path, -1, mode);
  187. }
  188. int
  189. __libxml2_xzcompressed(xzFile f) {
  190. return xz_compressed(f);
  191. }
  192. xzFile
  193. __libxml2_xzdopen(int fd, const char *mode)
  194. {
  195. char *path; /* identifier for error messages */
  196. size_t path_size = 7 + 3 * sizeof(int);
  197. xzFile xz;
  198. if (fd == -1 || (path = xmlMalloc(path_size)) == NULL)
  199. return NULL;
  200. snprintf(path, path_size, "<fd:%d>", fd); /* for debugging */
  201. xz = xz_open(path, fd, mode);
  202. xmlFree(path);
  203. return xz;
  204. }
  205. static int
  206. xz_load(xz_statep state, unsigned char *buf, unsigned int len,
  207. unsigned int *have)
  208. {
  209. int ret;
  210. *have = 0;
  211. do {
  212. ret = read(state->fd, buf + *have, len - *have);
  213. if (ret <= 0)
  214. break;
  215. *have += ret;
  216. } while (*have < len);
  217. if (ret < 0) {
  218. xz_error(state, -1, strerror(errno));
  219. return -1;
  220. }
  221. if (ret == 0)
  222. state->eof = 1;
  223. return 0;
  224. }
  225. static int
  226. xz_avail(xz_statep state)
  227. {
  228. lzma_stream *strm = &(state->strm);
  229. if (state->err != LZMA_OK)
  230. return -1;
  231. if (state->eof == 0) {
  232. /* avail_in is size_t, which is not necessary sizeof(unsigned) */
  233. unsigned tmp = strm->avail_in;
  234. if (xz_load(state, state->in, state->size, &tmp) == -1) {
  235. strm->avail_in = tmp;
  236. return -1;
  237. }
  238. strm->avail_in = tmp;
  239. strm->next_in = state->in;
  240. }
  241. return 0;
  242. }
  243. #ifdef LIBXML_ZLIB_ENABLED
  244. static int
  245. xz_avail_zstrm(xz_statep state)
  246. {
  247. int ret;
  248. state->strm.avail_in = state->zstrm.avail_in;
  249. state->strm.next_in = state->zstrm.next_in;
  250. ret = xz_avail(state);
  251. state->zstrm.avail_in = (uInt) state->strm.avail_in;
  252. state->zstrm.next_in = (Bytef *) state->strm.next_in;
  253. return ret;
  254. }
  255. #endif
  256. static int
  257. is_format_xz(xz_statep state)
  258. {
  259. lzma_stream *strm = &(state->strm);
  260. return strm->avail_in >= 6 && memcmp(state->in, "\3757zXZ", 6) == 0;
  261. }
  262. static int
  263. is_format_lzma(xz_statep state)
  264. {
  265. lzma_stream *strm = &(state->strm);
  266. lzma_filter filter;
  267. lzma_options_lzma *opt;
  268. uint32_t dict_size;
  269. uint64_t uncompressed_size;
  270. size_t i;
  271. if (strm->avail_in < 13)
  272. return 0;
  273. filter.id = LZMA_FILTER_LZMA1;
  274. if (lzma_properties_decode(&filter, NULL, state->in, 5) != LZMA_OK)
  275. return 0;
  276. opt = filter.options;
  277. dict_size = opt->dict_size;
  278. free(opt); /* we can't use xmlFree on a string returned by zlib */
  279. /* A hack to ditch tons of false positives: We allow only dictionary
  280. * sizes that are 2^n or 2^n + 2^(n-1) or UINT32_MAX. LZMA_Alone
  281. * created only files with 2^n, but accepts any dictionary size.
  282. * If someone complains, this will be reconsidered.
  283. */
  284. if (dict_size != UINT32_MAX) {
  285. uint32_t d = dict_size - 1;
  286. d |= d >> 2;
  287. d |= d >> 3;
  288. d |= d >> 4;
  289. d |= d >> 8;
  290. d |= d >> 16;
  291. ++d;
  292. if (d != dict_size || dict_size == 0)
  293. return 0;
  294. }
  295. /* Another hack to ditch false positives: Assume that if the
  296. * uncompressed size is known, it must be less than 256 GiB.
  297. * Again, if someone complains, this will be reconsidered.
  298. */
  299. uncompressed_size = 0;
  300. for (i = 0; i < 8; ++i)
  301. uncompressed_size |= (uint64_t) (state->in[5 + i]) << (i * 8);
  302. if (uncompressed_size != UINT64_MAX
  303. && uncompressed_size > (UINT64_C(1) << 38))
  304. return 0;
  305. return 1;
  306. }
  307. #ifdef LIBXML_ZLIB_ENABLED
  308. /* Get next byte from input, or -1 if end or error. */
  309. #define NEXT() ((strm->avail_in == 0 && xz_avail(state) == -1) ? -1 : \
  310. (strm->avail_in == 0 ? -1 : \
  311. (strm->avail_in--, *(strm->next_in)++)))
  312. /* Same thing, but from zstrm */
  313. #define NEXTZ() ((strm->avail_in == 0 && xz_avail_zstrm(state) == -1) ? -1 : \
  314. (strm->avail_in == 0 ? -1 : \
  315. (strm->avail_in--, *(strm->next_in)++)))
  316. /* Get a four-byte little-endian integer and return 0 on success and the value
  317. in *ret. Otherwise -1 is returned and *ret is not modified. */
  318. static int
  319. gz_next4(xz_statep state, unsigned long *ret)
  320. {
  321. int ch;
  322. unsigned long val;
  323. z_streamp strm = &(state->zstrm);
  324. val = NEXTZ();
  325. val += (unsigned) NEXTZ() << 8;
  326. val += (unsigned long) NEXTZ() << 16;
  327. ch = NEXTZ();
  328. if (ch == -1)
  329. return -1;
  330. val += (unsigned long) ch << 24;
  331. *ret = val;
  332. return 0;
  333. }
  334. #endif
  335. static int
  336. xz_head(xz_statep state)
  337. {
  338. lzma_stream *strm = &(state->strm);
  339. lzma_stream init = LZMA_STREAM_INIT;
  340. int flags;
  341. unsigned len;
  342. /* Avoid unused variable warning if features are disabled. */
  343. (void) flags;
  344. (void) len;
  345. /* allocate read buffers and inflate memory */
  346. if (state->size == 0) {
  347. /* allocate buffers */
  348. state->in = xmlMalloc(state->want);
  349. state->out = xmlMalloc(state->want << 1);
  350. if (state->in == NULL || state->out == NULL) {
  351. if (state->out != NULL)
  352. xmlFree(state->out);
  353. if (state->in != NULL)
  354. xmlFree(state->in);
  355. xz_error(state, LZMA_MEM_ERROR, "out of memory");
  356. return -1;
  357. }
  358. state->size = state->want;
  359. /* allocate decoder memory */
  360. state->strm = init;
  361. state->strm.avail_in = 0;
  362. state->strm.next_in = NULL;
  363. if (lzma_auto_decoder(&state->strm, 100000000, 0) != LZMA_OK) {
  364. xmlFree(state->out);
  365. xmlFree(state->in);
  366. state->size = 0;
  367. xz_error(state, LZMA_MEM_ERROR, "out of memory");
  368. return -1;
  369. }
  370. #ifdef LIBXML_ZLIB_ENABLED
  371. /* allocate inflate memory */
  372. state->zstrm.zalloc = Z_NULL;
  373. state->zstrm.zfree = Z_NULL;
  374. state->zstrm.opaque = Z_NULL;
  375. state->zstrm.avail_in = 0;
  376. state->zstrm.next_in = Z_NULL;
  377. if (state->init == 0) {
  378. if (inflateInit2(&(state->zstrm), -15) != Z_OK) {/* raw inflate */
  379. xmlFree(state->out);
  380. xmlFree(state->in);
  381. state->size = 0;
  382. xz_error(state, LZMA_MEM_ERROR, "out of memory");
  383. return -1;
  384. }
  385. state->init = 1;
  386. }
  387. #endif
  388. }
  389. /* get some data in the input buffer */
  390. if (strm->avail_in == 0) {
  391. if (xz_avail(state) == -1)
  392. return -1;
  393. if (strm->avail_in == 0)
  394. return 0;
  395. }
  396. /* look for the xz magic header bytes */
  397. if (is_format_xz(state) || is_format_lzma(state)) {
  398. state->how = LZMA;
  399. state->direct = 0;
  400. return 0;
  401. }
  402. #ifdef LIBXML_ZLIB_ENABLED
  403. /* look for the gzip magic header bytes 31 and 139 */
  404. if (strm->next_in[0] == 31) {
  405. strm->avail_in--;
  406. strm->next_in++;
  407. if (strm->avail_in == 0 && xz_avail(state) == -1)
  408. return -1;
  409. if (strm->avail_in && strm->next_in[0] == 139) {
  410. /* we have a gzip header, woo hoo! */
  411. strm->avail_in--;
  412. strm->next_in++;
  413. /* skip rest of header */
  414. if (NEXT() != 8) { /* compression method */
  415. xz_error(state, LZMA_DATA_ERROR,
  416. "unknown compression method");
  417. return -1;
  418. }
  419. flags = NEXT();
  420. if (flags & 0xe0) { /* reserved flag bits */
  421. xz_error(state, LZMA_DATA_ERROR,
  422. "unknown header flags set");
  423. return -1;
  424. }
  425. NEXT(); /* modification time */
  426. NEXT();
  427. NEXT();
  428. NEXT();
  429. NEXT(); /* extra flags */
  430. NEXT(); /* operating system */
  431. if (flags & 4) { /* extra field */
  432. len = (unsigned) NEXT();
  433. len += (unsigned) NEXT() << 8;
  434. while (len--)
  435. if (NEXT() < 0)
  436. break;
  437. }
  438. if (flags & 8) /* file name */
  439. while (NEXT() > 0) ;
  440. if (flags & 16) /* comment */
  441. while (NEXT() > 0) ;
  442. if (flags & 2) { /* header crc */
  443. NEXT();
  444. NEXT();
  445. }
  446. /* an unexpected end of file is not checked for here -- it will be
  447. * noticed on the first request for uncompressed data */
  448. /* set up for decompression */
  449. inflateReset(&state->zstrm);
  450. state->zstrm.adler = crc32(0L, Z_NULL, 0);
  451. state->how = GZIP;
  452. state->direct = 0;
  453. return 0;
  454. } else {
  455. /* not a gzip file -- save first byte (31) and fall to raw i/o */
  456. state->out[0] = 31;
  457. state->have = 1;
  458. }
  459. }
  460. #endif
  461. /* doing raw i/o, save start of raw data for seeking, copy any leftover
  462. * input to output -- this assumes that the output buffer is larger than
  463. * the input buffer, which also assures space for gzungetc() */
  464. state->raw = state->pos;
  465. state->next = state->out;
  466. if (strm->avail_in) {
  467. memcpy(state->next + state->have, strm->next_in, strm->avail_in);
  468. state->have += strm->avail_in;
  469. strm->avail_in = 0;
  470. }
  471. state->how = COPY;
  472. state->direct = 1;
  473. return 0;
  474. }
  475. static int
  476. xz_decomp(xz_statep state)
  477. {
  478. int ret;
  479. unsigned had;
  480. unsigned long crc, len;
  481. lzma_stream *strm = &(state->strm);
  482. lzma_action action = LZMA_RUN;
  483. /* Avoid unused variable warning if features are disabled. */
  484. (void) crc;
  485. (void) len;
  486. /* fill output buffer up to end of deflate stream */
  487. had = strm->avail_out;
  488. do {
  489. /* get more input for inflate() */
  490. if (strm->avail_in == 0 && xz_avail(state) == -1)
  491. return -1;
  492. if (strm->avail_in == 0) {
  493. xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
  494. return -1;
  495. }
  496. if (state->eof)
  497. action = LZMA_FINISH;
  498. /* decompress and handle errors */
  499. #ifdef LIBXML_ZLIB_ENABLED
  500. if (state->how == GZIP) {
  501. state->zstrm.avail_in = (uInt) state->strm.avail_in;
  502. state->zstrm.next_in = (Bytef *) state->strm.next_in;
  503. state->zstrm.avail_out = (uInt) state->strm.avail_out;
  504. state->zstrm.next_out = (Bytef *) state->strm.next_out;
  505. ret = inflate(&state->zstrm, Z_NO_FLUSH);
  506. if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
  507. xz_error(state, Z_STREAM_ERROR,
  508. "internal error: inflate stream corrupt");
  509. return -1;
  510. }
  511. /*
  512. * FIXME: Remapping a couple of error codes and falling through
  513. * to the LZMA error handling looks fragile.
  514. */
  515. if (ret == Z_MEM_ERROR)
  516. ret = LZMA_MEM_ERROR;
  517. if (ret == Z_DATA_ERROR)
  518. ret = LZMA_DATA_ERROR;
  519. if (ret == Z_STREAM_END)
  520. ret = LZMA_STREAM_END;
  521. state->strm.avail_in = state->zstrm.avail_in;
  522. state->strm.next_in = state->zstrm.next_in;
  523. state->strm.avail_out = state->zstrm.avail_out;
  524. state->strm.next_out = state->zstrm.next_out;
  525. } else /* state->how == LZMA */
  526. #endif
  527. ret = lzma_code(strm, action);
  528. if (ret == LZMA_MEM_ERROR) {
  529. xz_error(state, LZMA_MEM_ERROR, "out of memory");
  530. return -1;
  531. }
  532. if (ret == LZMA_DATA_ERROR) {
  533. xz_error(state, LZMA_DATA_ERROR, "compressed data error");
  534. return -1;
  535. }
  536. if (ret == LZMA_PROG_ERROR) {
  537. xz_error(state, LZMA_PROG_ERROR, "compression error");
  538. return -1;
  539. }
  540. if ((state->how != GZIP) &&
  541. (ret != LZMA_OK) && (ret != LZMA_STREAM_END)) {
  542. xz_error(state, ret, "lzma error");
  543. return -1;
  544. }
  545. } while (strm->avail_out && ret != LZMA_STREAM_END);
  546. /* update available output and crc check value */
  547. state->have = had - strm->avail_out;
  548. state->next = strm->next_out - state->have;
  549. #ifdef LIBXML_ZLIB_ENABLED
  550. state->zstrm.adler =
  551. crc32(state->zstrm.adler, state->next, state->have);
  552. #endif
  553. if (ret == LZMA_STREAM_END) {
  554. #ifdef LIBXML_ZLIB_ENABLED
  555. if (state->how == GZIP) {
  556. if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) {
  557. xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
  558. return -1;
  559. }
  560. if (crc != state->zstrm.adler) {
  561. xz_error(state, LZMA_DATA_ERROR, "incorrect data check");
  562. return -1;
  563. }
  564. if (len != (state->zstrm.total_out & 0xffffffffL)) {
  565. xz_error(state, LZMA_DATA_ERROR, "incorrect length check");
  566. return -1;
  567. }
  568. state->strm.avail_in = 0;
  569. state->strm.next_in = NULL;
  570. state->strm.avail_out = 0;
  571. state->strm.next_out = NULL;
  572. } else
  573. #endif
  574. if (strm->avail_in != 0 || !state->eof) {
  575. xz_error(state, LZMA_DATA_ERROR, "trailing garbage");
  576. return -1;
  577. }
  578. state->how = LOOK; /* ready for next stream, once have is 0 (leave
  579. * state->direct unchanged to remember how) */
  580. }
  581. /* good decompression */
  582. return 0;
  583. }
  584. static int
  585. xz_make(xz_statep state)
  586. {
  587. lzma_stream *strm = &(state->strm);
  588. if (state->how == LOOK) { /* look for lzma / gzip header */
  589. if (xz_head(state) == -1)
  590. return -1;
  591. if (state->have) /* got some data from xz_head() */
  592. return 0;
  593. }
  594. if (state->how == COPY) { /* straight copy */
  595. if (xz_load(state, state->out, state->size << 1, &(state->have)) ==
  596. -1)
  597. return -1;
  598. state->next = state->out;
  599. } else if (state->how == LZMA || state->how == GZIP) { /* decompress */
  600. strm->avail_out = state->size << 1;
  601. strm->next_out = state->out;
  602. if (xz_decomp(state) == -1)
  603. return -1;
  604. }
  605. return 0;
  606. }
  607. static int
  608. xz_skip(xz_statep state, uint64_t len)
  609. {
  610. unsigned n;
  611. /* skip over len bytes or reach end-of-file, whichever comes first */
  612. while (len)
  613. /* skip over whatever is in output buffer */
  614. if (state->have) {
  615. n = (uint64_t) state->have > len ?
  616. (unsigned) len : state->have;
  617. state->have -= n;
  618. state->next += n;
  619. state->pos += n;
  620. len -= n;
  621. }
  622. /* output buffer empty -- return if we're at the end of the input */
  623. else if (state->eof && state->strm.avail_in == 0)
  624. break;
  625. /* need more data to skip -- load up output buffer */
  626. else {
  627. /* get more output, looking for header if required */
  628. if (xz_make(state) == -1)
  629. return -1;
  630. }
  631. return 0;
  632. }
  633. int
  634. __libxml2_xzread(xzFile file, void *buf, unsigned len)
  635. {
  636. unsigned got, n;
  637. xz_statep state;
  638. lzma_stream *strm;
  639. /* get internal structure */
  640. if (file == NULL)
  641. return -1;
  642. state = (xz_statep) file;
  643. strm = &(state->strm);
  644. /* check that we're reading and that there's no error */
  645. if (state->err != LZMA_OK)
  646. return -1;
  647. /* since an int is returned, make sure len fits in one, otherwise return
  648. * with an error (this avoids the flaw in the interface) */
  649. if ((int) len < 0) {
  650. xz_error(state, LZMA_BUF_ERROR,
  651. "requested length does not fit in int");
  652. return -1;
  653. }
  654. /* if len is zero, avoid unnecessary operations */
  655. if (len == 0)
  656. return 0;
  657. /* process a skip request */
  658. if (state->seek) {
  659. state->seek = 0;
  660. if (xz_skip(state, state->skip) == -1)
  661. return -1;
  662. }
  663. /* get len bytes to buf, or less than len if at the end */
  664. got = 0;
  665. do {
  666. /* first just try copying data from the output buffer */
  667. if (state->have) {
  668. n = state->have > len ? len : state->have;
  669. memcpy(buf, state->next, n);
  670. state->next += n;
  671. state->have -= n;
  672. }
  673. /* output buffer empty -- return if we're at the end of the input */
  674. else if (state->eof && strm->avail_in == 0)
  675. break;
  676. /* need output data -- for small len or new stream load up our output
  677. * buffer */
  678. else if (state->how == LOOK || len < (state->size << 1)) {
  679. /* get more output, looking for header if required */
  680. if (xz_make(state) == -1)
  681. return -1;
  682. continue; /* no progress yet -- go back to memcpy() above */
  683. /* the copy above assures that we will leave with space in the
  684. * output buffer, allowing at least one gzungetc() to succeed */
  685. }
  686. /* large len -- read directly into user buffer */
  687. else if (state->how == COPY) { /* read directly */
  688. if (xz_load(state, buf, len, &n) == -1)
  689. return -1;
  690. }
  691. /* large len -- decompress directly into user buffer */
  692. else { /* state->how == LZMA */
  693. strm->avail_out = len;
  694. strm->next_out = buf;
  695. if (xz_decomp(state) == -1)
  696. return -1;
  697. n = state->have;
  698. state->have = 0;
  699. }
  700. /* update progress */
  701. len -= n;
  702. buf = (char *) buf + n;
  703. got += n;
  704. state->pos += n;
  705. } while (len);
  706. /* return number of bytes read into user buffer (will fit in int) */
  707. return (int) got;
  708. }
  709. int
  710. __libxml2_xzclose(xzFile file)
  711. {
  712. int ret;
  713. xz_statep state;
  714. /* get internal structure */
  715. if (file == NULL)
  716. return LZMA_DATA_ERROR;
  717. state = (xz_statep) file;
  718. /* free memory and close file */
  719. if (state->size) {
  720. lzma_end(&(state->strm));
  721. #ifdef LIBXML_ZLIB_ENABLED
  722. if (state->init == 1)
  723. inflateEnd(&(state->zstrm));
  724. state->init = 0;
  725. #endif
  726. xmlFree(state->out);
  727. xmlFree(state->in);
  728. }
  729. xmlFree(state->path);
  730. if ((state->msg != NULL) && (state->err != LZMA_MEM_ERROR))
  731. xmlFree(state->msg);
  732. ret = close(state->fd);
  733. xmlFree(state);
  734. return ret ? ret : LZMA_OK;
  735. }
  736. #endif /* LIBXML_LZMA_ENABLED */