#define IN_LIBXML
#include "libxml.h"
#ifdef LIBXML_LZMA_ENABLED
#include <string.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_ZLIB_H
#include <zlib.h>
#endif
#ifdef HAVE_LZMA_H
#include <lzma.h>
#endif
#include "xzlib.h"
#include <libxml/xmlmemory.h>
#define LOOK 0
#define COPY 1
#define GZIP 2
#define LZMA 3
typedef struct {
int mode;
int fd;
char *path;
uint64_t pos;
unsigned int size;
unsigned int want;
unsigned char *in;
unsigned char *out;
unsigned char *next;
unsigned int have;
int eof;
uint64_t start;
uint64_t raw;
int how;
int direct;
uint64_t skip;
int seek;
int err;
char *msg;
int init;
lzma_stream strm;
char padding1[32];
#ifdef HAVE_ZLIB_H
z_stream zstrm;
#endif
char padding2[32];
} xz_state, *xz_statep;
static void
xz_error(xz_statep state, int err, const char *msg)
{
if (state->msg != NULL) {
if (state->err != LZMA_MEM_ERROR)
xmlFree(state->msg);
state->msg = NULL;
}
state->err = err;
if (msg == NULL)
return;
if (err == LZMA_MEM_ERROR) {
state->msg = (char *) msg;
return;
}
{
const size_t msgLength = strlen(state->path) + 2 + strlen(msg) + 1;
if ((state->msg = xmlMalloc(msgLength)) == NULL) {
state->err = LZMA_MEM_ERROR;
state->msg = (char *) "out of memory";
return;
}
snprintf(state->msg, msgLength, "%s: %s", state->path, msg);
}
return;
}
static void
xz_reset(xz_statep state)
{
state->have = 0;
state->eof = 0;
state->how = LOOK;
state->direct = 1;
state->seek = 0;
xz_error(state, LZMA_OK, NULL);
state->pos = 0;
state->strm.avail_in = 0;
#ifdef HAVE_ZLIB_H
state->zstrm.avail_in = 0;
#endif
}
static xzFile
xz_open(const char *path, int fd, const char *mode ATTRIBUTE_UNUSED)
{
const size_t pathLength = strlen(path) + 1;
xz_statep state;
state = xmlMalloc(sizeof(xz_state));
if (state == NULL)
return NULL;
state->size = 0;
state->want = BUFSIZ;
state->msg = NULL;
state->init = 0;
state->path = xmlMalloc(pathLength);
if (state->path == NULL) {
xmlFree(state);
return NULL;
}
strncpy(state->path, path, pathLength);
state->path[pathLength - 1] = '\0';
state->fd = fd != -1 ? fd : open(path,
#ifdef O_LARGEFILE
O_LARGEFILE |
#endif
#ifdef O_BINARY
O_BINARY |
#endif
O_RDONLY, 0666);
if (state->fd == -1) {
xmlFree(state->path);
xmlFree(state);
return NULL;
}
state->start = lseek(state->fd, 0, SEEK_CUR);
if (state->start == (uint64_t) - 1)
state->start = 0;
xz_reset(state);
return (xzFile) state;
}
static int
xz_compressed(xzFile f) {
xz_statep state;
if (f == NULL)
return(-1);
state = (xz_statep) f;
if (state->init <= 0)
return(-1);
switch (state->how) {
case COPY:
return(0);
case GZIP:
case LZMA:
return(1);
}
return(-1);
}
xzFile
__libxml2_xzopen(const char *path, const char *mode)
{
return xz_open(path, -1, mode);
}
int
__libxml2_xzcompressed(xzFile f) {
return xz_compressed(f);
}
xzFile
__libxml2_xzdopen(int fd, const char *mode)
{
char *path;
xzFile xz;
size_t pathLength = 7 + (3 * sizeof(int)) + 1;
if (fd == -1 || (path = xmlMalloc(pathLength)) == NULL)
return NULL;
snprintf(path, pathLength, "<fd:%d>", fd);
xz = xz_open(path, fd, mode);
xmlFree(path);
return xz;
}
static int
xz_load(xz_statep state, unsigned char *buf, unsigned int len,
unsigned int *have)
{
int ret;
*have = 0;
do {
ret = read(state->fd, buf + *have, len - *have);
if (ret <= 0)
break;
*have += ret;
} while (*have < len);
if (ret < 0) {
xz_error(state, -1, strerror(errno));
return -1;
}
if (ret == 0)
state->eof = 1;
return 0;
}
static int
xz_avail(xz_statep state)
{
lzma_stream *strm = &(state->strm);
if (state->err != LZMA_OK)
return -1;
if (state->eof == 0) {
unsigned tmp = strm->avail_in;
if (xz_load(state, state->in, state->size, &tmp) == -1) {
strm->avail_in = tmp;
return -1;
}
strm->avail_in = tmp;
strm->next_in = state->in;
}
return 0;
}
#ifdef HAVE_ZLIB_H
static int
xz_avail_zstrm(xz_statep state)
{
int ret;
state->strm.avail_in = state->zstrm.avail_in;
state->strm.next_in = state->zstrm.next_in;
ret = xz_avail(state);
state->zstrm.avail_in = (uInt) state->strm.avail_in;
state->zstrm.next_in = (Bytef *) state->strm.next_in;
return ret;
}
#endif
static int
is_format_xz(xz_statep state)
{
lzma_stream *strm = &(state->strm);
return strm->avail_in >= 6 && memcmp(state->in, "\3757zXZ", 6) == 0;
}
static int
is_format_lzma(xz_statep state)
{
lzma_stream *strm = &(state->strm);
lzma_filter filter;
lzma_options_lzma *opt;
uint32_t dict_size;
uint64_t uncompressed_size;
size_t i;
if (strm->avail_in < 13)
return 0;
filter.id = LZMA_FILTER_LZMA1;
if (lzma_properties_decode(&filter, NULL, state->in, 5) != LZMA_OK)
return 0;
opt = filter.options;
dict_size = opt->dict_size;
free(opt);
if (dict_size != UINT32_MAX) {
uint32_t d = dict_size - 1;
d |= d >> 2;
d |= d >> 3;
d |= d >> 4;
d |= d >> 8;
d |= d >> 16;
++d;
if (d != dict_size || dict_size == 0)
return 0;
}
uncompressed_size = 0;
for (i = 0; i < 8; ++i)
uncompressed_size |= (uint64_t) (state->in[5 + i]) << (i * 8);
if (uncompressed_size != UINT64_MAX
&& uncompressed_size > (UINT64_C(1) << 38))
return 0;
return 1;
}
#ifdef HAVE_ZLIB_H
#define NEXT() ((strm->avail_in == 0 && xz_avail(state) == -1) ? -1 : \
(strm->avail_in == 0 ? -1 : \
(strm->avail_in--, *(strm->next_in)++)))
#define NEXTZ() ((strm->avail_in == 0 && xz_avail_zstrm(state) == -1) ? -1 : \
(strm->avail_in == 0 ? -1 : \
(strm->avail_in--, *(strm->next_in)++)))
static int
gz_next4(xz_statep state, unsigned long *ret)
{
int ch;
unsigned long val;
z_streamp strm = &(state->zstrm);
val = NEXTZ();
val += (unsigned) NEXTZ() << 8;
val += (unsigned long) NEXTZ() << 16;
ch = NEXTZ();
if (ch == -1)
return -1;
val += (unsigned long) ch << 24;
*ret = val;
return 0;
}
#endif
static int
xz_head(xz_statep state)
{
lzma_stream *strm = &(state->strm);
lzma_stream init = LZMA_STREAM_INIT;
int flags;
unsigned len;
if (state->size == 0) {
state->in = xmlMalloc(state->want);
state->out = xmlMalloc(state->want << 1);
if (state->in == NULL || state->out == NULL) {
if (state->out != NULL)
xmlFree(state->out);
if (state->in != NULL)
xmlFree(state->in);
xz_error(state, LZMA_MEM_ERROR, "out of memory");
return -1;
}
state->size = state->want;
state->strm = init;
state->strm.avail_in = 0;
state->strm.next_in = NULL;
if (lzma_auto_decoder(&state->strm, UINT64_MAX, 0) != LZMA_OK) {
xmlFree(state->out);
xmlFree(state->in);
state->size = 0;
xz_error(state, LZMA_MEM_ERROR, "out of memory");
return -1;
}
#ifdef HAVE_ZLIB_H
state->zstrm.zalloc = Z_NULL;
state->zstrm.zfree = Z_NULL;
state->zstrm.opaque = Z_NULL;
state->zstrm.avail_in = 0;
state->zstrm.next_in = Z_NULL;
if (state->init == 0) {
if (inflateInit2(&(state->zstrm), -15) != Z_OK) {
xmlFree(state->out);
xmlFree(state->in);
state->size = 0;
xz_error(state, LZMA_MEM_ERROR, "out of memory");
return -1;
}
state->init = 1;
}
#endif
}
if (strm->avail_in == 0) {
if (xz_avail(state) == -1)
return -1;
if (strm->avail_in == 0)
return 0;
}
if (is_format_xz(state) || is_format_lzma(state)) {
state->how = LZMA;
state->direct = 0;
return 0;
}
#ifdef HAVE_ZLIB_H
if (strm->next_in[0] == 31) {
strm->avail_in--;
strm->next_in++;
if (strm->avail_in == 0 && xz_avail(state) == -1)
return -1;
if (strm->avail_in && strm->next_in[0] == 139) {
strm->avail_in--;
strm->next_in++;
if (NEXT() != 8) {
xz_error(state, LZMA_DATA_ERROR,
"unknown compression method");
return -1;
}
flags = NEXT();
if (flags & 0xe0) {
xz_error(state, LZMA_DATA_ERROR,
"unknown header flags set");
return -1;
}
NEXT();
NEXT();
NEXT();
NEXT();
NEXT();
NEXT();
if (flags & 4) {
len = (unsigned) NEXT();
len += (unsigned) NEXT() << 8;
while (len--)
if (NEXT() < 0)
break;
}
if (flags & 8)
while (NEXT() > 0) ;
if (flags & 16)
while (NEXT() > 0) ;
if (flags & 2) {
NEXT();
NEXT();
}
inflateReset(&state->zstrm);
state->zstrm.adler = crc32(0L, Z_NULL, 0);
state->how = GZIP;
state->direct = 0;
return 0;
} else {
state->out[0] = 31;
state->have = 1;
}
}
#endif
state->raw = state->pos;
state->next = state->out;
if (strm->avail_in) {
memcpy(state->next + state->have, strm->next_in, strm->avail_in);
state->have += strm->avail_in;
strm->avail_in = 0;
}
state->how = COPY;
state->direct = 1;
return 0;
}
static int
xz_decomp(xz_statep state)
{
int ret;
unsigned had;
unsigned long crc, len;
lzma_stream *strm = &(state->strm);
lzma_action action = LZMA_RUN;
had = strm->avail_out;
do {
if (strm->avail_in == 0 && xz_avail(state) == -1)
return -1;
if (strm->avail_in == 0) {
xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
return -1;
}
if (state->eof)
action = LZMA_FINISH;
#ifdef HAVE_ZLIB_H
if (state->how == GZIP) {
state->zstrm.avail_in = (uInt) state->strm.avail_in;
state->zstrm.next_in = (Bytef *) state->strm.next_in;
state->zstrm.avail_out = (uInt) state->strm.avail_out;
state->zstrm.next_out = (Bytef *) state->strm.next_out;
ret = inflate(&state->zstrm, Z_NO_FLUSH);
if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
xz_error(state, Z_STREAM_ERROR,
"internal error: inflate stream corrupt");
return -1;
}
if (ret == Z_MEM_ERROR)
ret = LZMA_MEM_ERROR;
if (ret == Z_DATA_ERROR)
ret = LZMA_DATA_ERROR;
if (ret == Z_STREAM_END)
ret = LZMA_STREAM_END;
state->strm.avail_in = state->zstrm.avail_in;
state->strm.next_in = state->zstrm.next_in;
state->strm.avail_out = state->zstrm.avail_out;
state->strm.next_out = state->zstrm.next_out;
} else
#endif
ret = lzma_code(strm, action);
if (ret == LZMA_MEM_ERROR) {
xz_error(state, LZMA_MEM_ERROR, "out of memory");
return -1;
}
if (ret == LZMA_DATA_ERROR) {
xz_error(state, LZMA_DATA_ERROR, "compressed data error");
return -1;
}
if (ret == LZMA_PROG_ERROR) {
xz_error(state, LZMA_PROG_ERROR, "compression error");
return -1;
}
} while (strm->avail_out && ret != LZMA_STREAM_END);
state->have = had - strm->avail_out;
state->next = strm->next_out - state->have;
#ifdef HAVE_ZLIB_H
state->zstrm.adler =
crc32(state->zstrm.adler, state->next, state->have);
#endif
if (ret == LZMA_STREAM_END) {
#ifdef HAVE_ZLIB_H
if (state->how == GZIP) {
if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) {
xz_error(state, LZMA_DATA_ERROR, "unexpected end of file");
return -1;
}
if (crc != state->zstrm.adler) {
xz_error(state, LZMA_DATA_ERROR, "incorrect data check");
return -1;
}
if (len != (state->zstrm.total_out & 0xffffffffL)) {
xz_error(state, LZMA_DATA_ERROR, "incorrect length check");
return -1;
}
state->strm.avail_in = 0;
state->strm.next_in = NULL;
state->strm.avail_out = 0;
state->strm.next_out = NULL;
} else
#endif
if (strm->avail_in != 0 || !state->eof) {
xz_error(state, LZMA_DATA_ERROR, "trailing garbage");
return -1;
}
state->how = LOOK;
}
return 0;
}
static int
xz_make(xz_statep state)
{
lzma_stream *strm = &(state->strm);
if (state->how == LOOK) {
if (xz_head(state) == -1)
return -1;
if (state->have)
return 0;
}
if (state->how == COPY) {
if (xz_load(state, state->out, state->size << 1, &(state->have)) ==
-1)
return -1;
state->next = state->out;
} else if (state->how == LZMA || state->how == GZIP) {
strm->avail_out = state->size << 1;
strm->next_out = state->out;
if (xz_decomp(state) == -1)
return -1;
}
return 0;
}
static int
xz_skip(xz_statep state, uint64_t len)
{
unsigned n;
while (len)
if (state->have) {
n = (uint64_t) state->have > len ?
(unsigned) len : state->have;
state->have -= n;
state->next += n;
state->pos += n;
len -= n;
}
else if (state->eof && state->strm.avail_in == 0)
break;
else {
if (xz_make(state) == -1)
return -1;
}
return 0;
}
int
__libxml2_xzread(xzFile file, void *buf, unsigned len)
{
unsigned got, n;
xz_statep state;
lzma_stream *strm;
if (file == NULL)
return -1;
state = (xz_statep) file;
strm = &(state->strm);
if (state->err != LZMA_OK)
return -1;
if ((int) len < 0) {
xz_error(state, LZMA_BUF_ERROR,
"requested length does not fit in int");
return -1;
}
if (len == 0)
return 0;
if (state->seek) {
state->seek = 0;
if (xz_skip(state, state->skip) == -1)
return -1;
}
got = 0;
do {
if (state->have) {
n = state->have > len ? len : state->have;
memcpy(buf, state->next, n);
state->next += n;
state->have -= n;
}
else if (state->eof && strm->avail_in == 0)
break;
else if (state->how == LOOK || len < (state->size << 1)) {
if (xz_make(state) == -1)
return -1;
continue;
}
else if (state->how == COPY) {
if (xz_load(state, buf, len, &n) == -1)
return -1;
}
else {
strm->avail_out = len;
strm->next_out = buf;
if (xz_decomp(state) == -1)
return -1;
n = state->have;
state->have = 0;
}
len -= n;
buf = (char *) buf + n;
got += n;
state->pos += n;
} while (len);
return (int) got;
}
int
__libxml2_xzclose(xzFile file)
{
int ret;
xz_statep state;
if (file == NULL)
return LZMA_DATA_ERROR;
state = (xz_statep) file;
if (state->size) {
lzma_end(&(state->strm));
#ifdef HAVE_ZLIB_H
if (state->init == 1)
inflateEnd(&(state->zstrm));
state->init = 0;
#endif
xmlFree(state->out);
xmlFree(state->in);
}
xmlFree(state->path);
ret = close(state->fd);
xmlFree(state);
return ret ? ret : LZMA_OK;
}
#endif