[libxml2] Cleanups of lzma support



commit adf5ec9496828b18e683fb23bd1e670824cfb6d0
Author: Daniel Veillard <veillard redhat com>
Date:   Thu Jan 26 16:56:22 2012 +0800

    Cleanups of lzma support
    
    - fix inclusion of the separated file
    - use namespaced name for the 4 non-static routines
    - add padding after external structures included in-situ
    - add new requirement to spec file
    - general cleanup of code

 Makefile.am    |    6 +-
 libxml.spec.in |    3 +-
 xmlIO.c        |   10 +-
 xzlib.c        |  488 +++++++++++++++++++++++++++++++-------------------------
 xzlib.h        |   22 ++-
 5 files changed, 301 insertions(+), 228 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 859c7da..ae72711 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -40,7 +40,7 @@ libxml2_la_SOURCES = SAX.c entities.c encoding.c error.c parserInternals.c  \
 		xmlregexp.c xmlschemas.c xmlschemastypes.c xmlunicode.c \
 		triostr.c trio.c xmlreader.c relaxng.c dict.c SAX2.c \
 		xmlwriter.c legacy.c chvalid.c pattern.c xmlsave.c \
-		xmlmodule.c schematron.c
+		xmlmodule.c schematron.c xzlib.c
 else
 libxml2_la_SOURCES = SAX.c entities.c encoding.c error.c parserInternals.c  \
 		parser.c tree.c hash.c list.c xmlIO.c xmlmemory.c uri.c  \
@@ -50,7 +50,7 @@ libxml2_la_SOURCES = SAX.c entities.c encoding.c error.c parserInternals.c  \
 		xmlregexp.c xmlschemas.c xmlschemastypes.c xmlunicode.c \
 		xmlreader.c relaxng.c dict.c SAX2.c \
 		xmlwriter.c legacy.c chvalid.c pattern.c xmlsave.c \
-		xmlmodule.c schematron.c
+		xmlmodule.c schematron.c xzlib.c
 endif
 
 DEPS = $(top_builddir)/libxml2.la
@@ -1195,7 +1195,7 @@ EXTRA_DIST = xml2-config.in xml2Conf.sh.in libxml.spec.in libxml2.spec \
              example/Makefile.am example/gjobread.c example/gjobs.xml \
 	     $(man_MANS) libxml-2.0.pc.in libxml-2.0-uninstalled.pc.in \
 	     trionan.c trionan.h triostr.c triostr.h trio.c trio.h \
-	     triop.h triodef.h libxml.h elfgcchack.h xzlib.c xzlib.h \
+	     triop.h triodef.h libxml.h elfgcchack.h xzlib.h \
 	     testThreadsWin32.c genUnicode.py TODO_SCHEMAS \
 	     dbgen.pl dbgenattr.pl regressions.py regressions.xml \
 	     README.tests Makefile.tests libxml2.syms \
diff --git a/libxml.spec.in b/libxml.spec.in
index 688811a..03eb24e 100644
--- a/libxml.spec.in
+++ b/libxml.spec.in
@@ -6,7 +6,7 @@ License: MIT
 Group: Development/Libraries
 Source: ftp://xmlsoft.org/libxml2/libxml2-%{version}.tar.gz
 BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
-BuildRequires: python python-devel zlib-devel pkgconfig
+BuildRequires: python python-devel zlib-devel pkgconfig xz-devel
 URL: http://xmlsoft.org/
 
 %description
@@ -25,6 +25,7 @@ Summary: Libraries, includes, etc. to develop XML and HTML applications
 Group: Development/Libraries
 Requires: libxml2 = %{version}-%{release}
 Requires: zlib-devel
+Requires: xz-devel
 Requires: pkgconfig
 
 %description devel
diff --git a/xmlIO.c b/xmlIO.c
index 4398791..73a995d 100644
--- a/xmlIO.c
+++ b/xmlIO.c
@@ -1315,7 +1315,7 @@ xmlGzfileClose (void * context) {
  *		I/O for compressed file accesses			*
  *									*
  ************************************************************************/
-#include "xzlib.c"
+#include "xzlib.h"
 /**
  * xmlXzfileMatch:
  * @filename:  the URI for matching
@@ -1344,7 +1344,7 @@ xmlXzfileOpen_real (const char *filename) {
     xzFile fd;
 
     if (!strcmp(filename, "-")) {
-        fd = xzdopen(dup(0), "rb");
+        fd = __libxml2_xzdopen(dup(0), "rb");
 	return((void *) fd);
     }
 
@@ -1363,7 +1363,7 @@ xmlXzfileOpen_real (const char *filename) {
     if (!xmlCheckFilename(path))
         return(NULL);
 
-    fd = xzopen(path, "rb");
+    fd = __libxml2_xzopen(path, "rb");
     return((void *) fd);
 }
 
@@ -1407,7 +1407,7 @@ static int
 xmlXzfileRead (void * context, char * buffer, int len) {
     int ret;
 
-    ret = xzread((xzFile) context, &buffer[0], len);
+    ret = __libxml2_xzread((xzFile) context, &buffer[0], len);
     if (ret < 0) xmlIOErr(0, "xzread()");
     return(ret);
 }
@@ -1422,7 +1422,7 @@ static int
 xmlXzfileClose (void * context) {
     int ret;
 
-    ret =  (xzclose((xzFile) context) == LZMA_OK ) ? 0 : -1;
+    ret =  (__libxml2_xzclose((xzFile) context) == LZMA_OK ) ? 0 : -1;
     if (ret < 0) xmlIOErr(0, "xzclose()");
     return(ret);
 }
diff --git a/xzlib.c b/xzlib.c
index ba3dbf3..192b164 100644
--- a/xzlib.c
+++ b/xzlib.c
@@ -1,43 +1,88 @@
+/**
+ * xzlib.c: front end for the transparent suport of lzma compression
+ *          at the I/O layer, based on an example file from lzma project
+ *
+ * See Copyright for the status of this software.
+ *
+ * Anders F Bjorklund <afb users sourceforge net>
+ */
+#define IN_LIBXML
+#include "libxml.h"
+#ifdef HAVE_LZMA_H
+
+#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
+#include <lzma.h>
+
 #include "xzlib.h"
 
 /* values for xz_state how */
-#define LOOK 0      /* look for a gzip/lzma header */
-#define COPY 1      /* copy input directly */
-#define GZIP 2      /* decompress a gzip stream */
-#define LZMA 3      /* decompress a lzma stream */
+#define LOOK 0                  /* look for a gzip/lzma header */
+#define COPY 1                  /* copy input directly */
+#define GZIP 2                  /* decompress a gzip stream */
+#define LZMA 3                  /* decompress a lzma stream */
 
 /* internal lzma file state data structure */
 typedef struct {
-    int mode;               /* see lzma modes above */
-    int fd;                 /* file descriptor */
-    char *path;             /* path or fd for error messages */
-    uint64_t pos;           /* current position in uncompressed data */
-    unsigned size;          /* buffer size, zero if not allocated yet */
-    unsigned want;          /* requested buffer size, default is BUFSIZ */
-    unsigned char *in;      /* input buffer */
-    unsigned char *out;     /* output buffer (double-sized when reading) */
-    unsigned char *next;    /* next output data to deliver or write */
-    unsigned have;          /* amount of output data unused at next */
-    int eof;                /* true if end of input file reached */
-    uint64_t start;         /* where the lzma data started, for rewinding */
-    uint64_t raw;           /* where the raw data started, for seeking */
-    int how;                /* 0: get header, 1: copy, 2: decompress */
-    int direct;             /* true if last read direct, false if lzma */
-        /* seek request */
-    uint64_t skip;          /* amount to skip (already rewound if backwards) */
-    int seek;               /* true if seek request pending */
-        /* error information */
-    int err;                /* error code */
-    char *msg;              /* error message */
-        /* lzma stream */
-    lzma_stream strm;       /* stream structure in-place (not a pointer) */
+    int mode;                   /* see lzma modes above */
+    int fd;                     /* file descriptor */
+    char *path;                 /* path or fd for error messages */
+    uint64_t pos;               /* current position in uncompressed data */
+    unsigned size;              /* buffer size, zero if not allocated yet */
+    unsigned want;              /* requested buffer size, default is BUFSIZ */
+    unsigned char *in;          /* input buffer */
+    unsigned char *out;         /* output buffer (double-sized when reading) */
+    unsigned char *next;        /* next output data to deliver or write */
+    unsigned have;              /* amount of output data unused at next */
+    int eof;                    /* true if end of input file reached */
+    uint64_t start;             /* where the lzma data started, for rewinding */
+    uint64_t raw;               /* where the raw data started, for seeking */
+    int how;                    /* 0: get header, 1: copy, 2: decompress */
+    int direct;                 /* true if last read direct, false if lzma */
+    /* seek request */
+    uint64_t skip;              /* amount to skip (already rewound if backwards) */
+    int seek;                   /* true if seek request pending */
+    /* error information */
+    int err;                    /* error code */
+    char *msg;                  /* error message */
+    /* lzma stream */
+    lzma_stream strm;           /* stream structure in-place (not a pointer) */
+    char padding1[32];          /* padding allowing to cope with possible
+                                   extensions of above structure without
+				   too much side effect */
 #ifdef HAVE_ZLIB_H
-        /* zlib inflate or deflate stream */
-    z_stream zstrm;         /* stream structure in-place (not a pointer) */
+    /* zlib inflate or deflate stream */
+    z_stream zstrm;             /* stream structure in-place (not a pointer) */
 #endif
+    char padding2[32];          /* padding allowing to cope with possible
+                                   extensions of above structure without
+				   too much side effect */
 } xz_state, *xz_statep;
 
-static void xz_error(xz_statep state, int err, const char *msg)
+static void
+xz_error(xz_statep state, int err, const char *msg)
 {
     /* free previously allocated message and clear */
     if (state->msg != NULL) {
@@ -53,14 +98,15 @@ static void xz_error(xz_statep state, int err, const char *msg)
 
     /* for an out of memory error, save as static string */
     if (err == LZMA_MEM_ERROR) {
-        state->msg = (char *)msg;
+        state->msg = (char *) msg;
         return;
     }
 
     /* construct error message with path */
-    if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
+    if ((state->msg =
+         malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
         state->err = LZMA_MEM_ERROR;
-        state->msg = (char *)"out of memory";
+        state->msg = (char *) "out of memory";
         return;
     }
     strcpy(state->msg, state->path);
@@ -69,22 +115,24 @@ static void xz_error(xz_statep state, int err, const char *msg)
     return;
 }
 
-static void xz_reset(xz_statep state)
+static void
+xz_reset(xz_statep state)
 {
-    state->have = 0;                /* no output data available */
-    state->eof = 0;                 /* not at end of file */
-    state->how = LOOK;              /* look for gzip header */
-    state->direct = 1;              /* default for empty file */
-    state->seek = 0;                /* no seek request pending */
-    xz_error(state, LZMA_OK, NULL); /* clear error */
-    state->pos = 0;                 /* no uncompressed data yet */
-    state->strm.avail_in = 0;       /* no input data yet */
+    state->have = 0;            /* no output data available */
+    state->eof = 0;             /* not at end of file */
+    state->how = LOOK;          /* look for gzip header */
+    state->direct = 1;          /* default for empty file */
+    state->seek = 0;            /* no seek request pending */
+    xz_error(state, LZMA_OK, NULL);     /* clear error */
+    state->pos = 0;             /* no uncompressed data yet */
+    state->strm.avail_in = 0;   /* no input data yet */
 #ifdef HAVE_ZLIB_H
-    state->zstrm.avail_in = 0;      /* no input data yet */
+    state->zstrm.avail_in = 0;  /* no input data yet */
 #endif
 }
 
-static xzFile xz_open(const char *path, int fd, const char *mode ATTRIBUTE_UNUSED)
+static xzFile
+xz_open(const char *path, int fd, const char *mode ATTRIBUTE_UNUSED)
 {
     xz_statep state;
 
@@ -96,7 +144,7 @@ static xzFile xz_open(const char *path, int fd, const char *mode ATTRIBUTE_UNUSE
     state->want = BUFSIZ;       /* requested buffer size */
     state->msg = NULL;          /* no error message yet */
 
-     /* save the path name for error messages */
+    /* save the path name for error messages */
     state->path = malloc(strlen(path) + 1);
     if (state->path == NULL) {
         free(state);
@@ -105,16 +153,14 @@ static xzFile xz_open(const char *path, int fd, const char *mode ATTRIBUTE_UNUSE
     strcpy(state->path, path);
 
     /* open the file with the appropriate mode (or just use fd) */
-    state->fd = fd != -1 ? fd :
-        open(path,
+    state->fd = fd != -1 ? fd : open(path,
 #ifdef O_LARGEFILE
-            O_LARGEFILE |
+                                     O_LARGEFILE |
 #endif
 #ifdef O_BINARY
-            O_BINARY |
+                                     O_BINARY |
 #endif
-                O_RDONLY,
-            0666);
+                                     O_RDONLY, 0666);
     if (state->fd == -1) {
         free(state->path);
         free(state);
@@ -123,34 +169,38 @@ static xzFile xz_open(const char *path, int fd, const char *mode ATTRIBUTE_UNUSE
 
     /* save the current position for rewinding (only if reading) */
     state->start = lseek(state->fd, 0, SEEK_CUR);
-    if (state->start == (uint64_t) -1) state->start = 0;
+    if (state->start == (uint64_t) - 1)
+        state->start = 0;
 
     /* initialize stream */
     xz_reset(state);
 
     /* return stream */
-    return (xzFile)state;
+    return (xzFile) state;
 }
 
-xzFile xzopen(const char *path, const char *mode)
+xzFile
+__libxml2_xzopen(const char *path, const char *mode)
 {
     return xz_open(path, -1, mode);
 }
 
-xzFile xzdopen(int fd, const char *mode)
+xzFile
+__libxml2_xzdopen(int fd, const char *mode)
 {
-    char *path;         /* identifier for error messages */
+    char *path;                 /* identifier for error messages */
     xzFile xz;
 
     if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
         return NULL;
-    sprintf(path, "<fd:%d>", fd);   /* for debugging */
+    sprintf(path, "<fd:%d>", fd);       /* for debugging */
     xz = xz_open(path, fd, mode);
     free(path);
     return xz;
 }
 
-static int xz_load(xz_statep state, unsigned char *buf, unsigned len, unsigned *have)
+static int
+xz_load(xz_statep state, unsigned char *buf, unsigned len, unsigned *have)
 {
     int ret;
 
@@ -170,7 +220,8 @@ static int xz_load(xz_statep state, unsigned char *buf, unsigned len, unsigned *
     return 0;
 }
 
-static int xz_avail(xz_statep state)
+static int
+xz_avail(xz_statep state)
 {
     lzma_stream *strm = &(state->strm);
 
@@ -178,7 +229,7 @@ static int xz_avail(xz_statep state)
         return -1;
     if (state->eof == 0) {
         if (xz_load(state, state->in, state->size,
-                (unsigned *)&(strm->avail_in)) == -1)
+                    (unsigned *) &(strm->avail_in)) == -1)
             return -1;
         strm->next_in = state->in;
     }
@@ -190,7 +241,7 @@ is_format_xz(xz_statep state)
 {
     lzma_stream *strm = &(state->strm);
 
-	return strm->avail_in >= 6 && memcmp(state->in, "\3757zXZ", 6) == 0;
+    return strm->avail_in >= 6 && memcmp(state->in, "\3757zXZ", 6) == 0;
 }
 
 static int
@@ -198,56 +249,58 @@ 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);
-
-	/* A hack to ditch tons of false positives: We allow only dictionary
-	 * sizes that are 2^n or 2^n + 2^(n-1) or UINT32_MAX. LZMA_Alone
-	 * created only files with 2^n, but accepts any dictionary size.
-	 * If someone complains, this will be reconsidered.
-	*/
-	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;
-	}
-
-	/* Another hack to ditch false positives: Assume that if the
-	 * uncompressed size is known, it must be less than 256 GiB.
-	 * Again, if someone complains, this will be reconsidered.
-	 */
-	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;
+    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);
+
+    /* A hack to ditch tons of false positives: We allow only dictionary
+     * sizes that are 2^n or 2^n + 2^(n-1) or UINT32_MAX. LZMA_Alone
+     * created only files with 2^n, but accepts any dictionary size.
+     * If someone complains, this will be reconsidered.
+     */
+    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;
+    }
+
+    /* Another hack to ditch false positives: Assume that if the
+     * uncompressed size is known, it must be less than 256 GiB.
+     * Again, if someone complains, this will be reconsidered.
+     */
+    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
+
 /* Get next byte from input, or -1 if end or error. */
 #define NEXT() ((strm->avail_in == 0 && xz_avail(state) == -1) ? -1 : \
                 (strm->avail_in == 0 ? -1 : \
@@ -255,25 +308,27 @@ is_format_lzma(xz_statep state)
 
 /* Get a four-byte little-endian integer and return 0 on success and the value
    in *ret.  Otherwise -1 is returned and *ret is not modified. */
-static int gz_next4(xz_statep state, unsigned long *ret)
+static int
+gz_next4(xz_statep state, unsigned long *ret)
 {
     int ch;
     unsigned long val;
     z_streamp strm = &(state->zstrm);
 
     val = NEXT();
-    val += (unsigned)NEXT() << 8;
-    val += (unsigned long)NEXT() << 16;
+    val += (unsigned) NEXT() << 8;
+    val += (unsigned long) NEXT() << 16;
     ch = NEXT();
     if (ch == -1)
         return -1;
-    val += (unsigned long)ch << 24;
+    val += (unsigned long) ch << 24;
     *ret = val;
     return 0;
 }
 #endif
 
-static int xz_head(xz_statep state)
+static int
+xz_head(xz_statep state)
 {
     lzma_stream *strm = &(state->strm);
     lzma_stream init = LZMA_STREAM_INIT;
@@ -305,8 +360,7 @@ static int xz_head(xz_statep state)
             state->size = 0;
             xz_error(state, LZMA_MEM_ERROR, "out of memory");
             return -1;
-       }
-
+        }
 #ifdef HAVE_ZLIB_H
         /* allocate inflate memory */
         state->zstrm.zalloc = Z_NULL;
@@ -314,7 +368,7 @@ static int xz_head(xz_statep state)
         state->zstrm.opaque = Z_NULL;
         state->zstrm.avail_in = 0;
         state->zstrm.next_in = Z_NULL;
-        if (inflateInit2(&(state->zstrm), -15) != Z_OK) {    /* raw inflate */
+        if (inflateInit2(&(state->zstrm), -15) != Z_OK) {       /* raw inflate */
             free(state->out);
             free(state->in);
             state->size = 0;
@@ -332,15 +386,14 @@ static int xz_head(xz_statep state)
             return 0;
     }
 
-     /* look for the xz magic header bytes */
+    /* look for the xz magic header bytes */
     if (is_format_xz(state) || is_format_lzma(state)) {
-            state->how = LZMA;
-            state->direct = 0;
-            return 0;
+        state->how = LZMA;
+        state->direct = 0;
+        return 0;
     }
-
 #ifdef HAVE_ZLIB_H
-   /* look for the gzip magic header bytes 31 and 139 */
+    /* look for the gzip magic header bytes 31 and 139 */
     if (strm->next_in[0] == 31) {
         strm->avail_in--;
         strm->next_in++;
@@ -352,40 +405,40 @@ static int xz_head(xz_statep state)
             strm->next_in++;
 
             /* skip rest of header */
-            if (NEXT() != 8) {      /* compression method */
-                xz_error(state, LZMA_DATA_ERROR, "unknown compression method");
+            if (NEXT() != 8) {  /* compression method */
+                xz_error(state, LZMA_DATA_ERROR,
+                         "unknown compression method");
                 return -1;
             }
             flags = NEXT();
-            if (flags & 0xe0) {     /* reserved flag bits */
-                xz_error(state, LZMA_DATA_ERROR, "unknown header flags set");
+            if (flags & 0xe0) { /* reserved flag bits */
+                xz_error(state, LZMA_DATA_ERROR,
+                         "unknown header flags set");
                 return -1;
             }
-            NEXT();                 /* modification time */
+            NEXT();             /* modification time */
             NEXT();
             NEXT();
             NEXT();
-            NEXT();                 /* extra flags */
-            NEXT();                 /* operating system */
-            if (flags & 4) {        /* extra field */
-                len = (unsigned)NEXT();
-                len += (unsigned)NEXT() << 8;
+            NEXT();             /* extra flags */
+            NEXT();             /* operating system */
+            if (flags & 4) {    /* extra field */
+                len = (unsigned) NEXT();
+                len += (unsigned) NEXT() << 8;
                 while (len--)
                     if (NEXT() < 0)
                         break;
             }
-            if (flags & 8)          /* file name */
-                while (NEXT() > 0)
-                    ;
-            if (flags & 16)         /* comment */
-                while (NEXT() > 0)
-                    ;
-            if (flags & 2) {        /* header crc */
+            if (flags & 8)      /* file name */
+                while (NEXT() > 0) ;
+            if (flags & 16)     /* comment */
+                while (NEXT() > 0) ;
+            if (flags & 2) {    /* header crc */
                 NEXT();
                 NEXT();
             }
             /* an unexpected end of file is not checked for here -- it will be
-               noticed on the first request for uncompressed data */
+             * noticed on the first request for uncompressed data */
 
             /* set up for decompression */
             inflateReset(&state->zstrm);
@@ -393,8 +446,7 @@ static int xz_head(xz_statep state)
             state->how = GZIP;
             state->direct = 0;
             return 0;
-        }
-        else {
+        } else {
             /* not a gzip file -- save first byte (31) and fall to raw i/o */
             state->out[0] = 31;
             state->have = 1;
@@ -403,8 +455,8 @@ static int xz_head(xz_statep state)
 #endif
 
     /* doing raw i/o, save start of raw data for seeking, copy any leftover
-       input to output -- this assumes that the output buffer is larger than
-       the input buffer, which also assures space for gzungetc() */
+     * input to output -- this assumes that the output buffer is larger than
+     * the input buffer, which also assures space for gzungetc() */
     state->raw = state->pos;
     state->next = state->out;
     if (strm->avail_in) {
@@ -417,7 +469,8 @@ static int xz_head(xz_statep state)
     return 0;
 }
 
-static int xz_decomp(xz_statep state)
+static int
+xz_decomp(xz_statep state)
 {
     int ret;
     unsigned had;
@@ -438,31 +491,33 @@ static int xz_decomp(xz_statep state)
         }
         if (state->eof)
             action = LZMA_FINISH;
-        
+
         /* decompress and handle errors */
 #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 /* state->how == LZMA */
+            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                  /* state->how == LZMA */
 #endif
-	ret = lzma_code(strm, action);
+            ret = lzma_code(strm, action);
         if (ret == LZMA_MEM_ERROR) {
             xz_error(state, LZMA_MEM_ERROR, "out of memory");
             return -1;
@@ -477,59 +532,60 @@ static int xz_decomp(xz_statep state)
     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);
+    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
+            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) {
+        if (strm->avail_in != 0 || !state->eof) {
             xz_error(state, LZMA_DATA_ERROR, "trailing garbage");
             return -1;
         }
         state->how = LOOK;      /* ready for next stream, once have is 0 (leave
-                                   state->direct unchanged to remember how) */
+                                 * state->direct unchanged to remember how) */
     }
 
     /* good decompression */
     return 0;
 }
 
-static int xz_make(xz_statep state)
+static int
+xz_make(xz_statep state)
 {
     lzma_stream *strm = &(state->strm);
 
-    if (state->how == LOOK) {           /* look for lzma / gzip header */
+    if (state->how == LOOK) {   /* look for lzma / gzip header */
         if (xz_head(state) == -1)
             return -1;
-        if (state->have)                /* got some data from xz_head() */
+        if (state->have)        /* got some data from xz_head() */
             return 0;
     }
-    if (state->how == COPY) {           /* straight copy */
-        if (xz_load(state, state->out, state->size << 1, &(state->have)) == -1)
+    if (state->how == COPY) {   /* straight 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) {      /* decompress */
+    } else if (state->how == LZMA || state->how == GZIP) {      /* decompress */
         strm->avail_out = state->size << 1;
         strm->next_out = state->out;
         if (xz_decomp(state) == -1)
@@ -538,7 +594,8 @@ static int xz_make(xz_statep state)
     return 0;
 }
 
-static int xz_skip(xz_statep state, uint64_t len)
+static int
+xz_skip(xz_statep state, uint64_t len)
 {
     unsigned n;
 
@@ -546,19 +603,19 @@ static int xz_skip(xz_statep state, uint64_t len)
     while (len)
         /* skip over whatever is in output buffer */
         if (state->have) {
-            n = (uint64_t)state->have > len ?
-                (unsigned)len : state->have;
+            n = (uint64_t) state->have > len ?
+                (unsigned) len : state->have;
             state->have -= n;
             state->next += n;
             state->pos += n;
             len -= n;
         }
 
-        /* output buffer empty -- return if we're at the end of the input */
+    /* output buffer empty -- return if we're at the end of the input */
         else if (state->eof && state->strm.avail_in == 0)
             break;
 
-        /* need more data to skip -- load up output buffer */
+    /* need more data to skip -- load up output buffer */
         else {
             /* get more output, looking for header if required */
             if (xz_make(state) == -1)
@@ -567,7 +624,8 @@ static int xz_skip(xz_statep state, uint64_t len)
     return 0;
 }
 
-int xzread(xzFile file, void *buf, unsigned len)
+int
+__libxml2_xzread(xzFile file, void *buf, unsigned len)
 {
     unsigned got, n;
     xz_statep state;
@@ -576,7 +634,7 @@ int xzread(xzFile file, void *buf, unsigned len)
     /* get internal structure */
     if (file == NULL)
         return -1;
-    state = (xz_statep)file;
+    state = (xz_statep) file;
     strm = &(state->strm);
 
     /* check that we're reading and that there's no error */
@@ -584,9 +642,10 @@ int xzread(xzFile file, void *buf, unsigned len)
         return -1;
 
     /* since an int is returned, make sure len fits in one, otherwise return
-       with an error (this avoids the flaw in the interface) */
-    if ((int)len < 0) {
-        xz_error(state, LZMA_BUF_ERROR, "requested length does not fit in int");
+     * with an error (this avoids the flaw in the interface) */
+    if ((int) len < 0) {
+        xz_error(state, LZMA_BUF_ERROR,
+                 "requested length does not fit in int");
         return -1;
     }
 
@@ -617,24 +676,24 @@ int xzread(xzFile file, void *buf, unsigned len)
             break;
 
         /* need output data -- for small len or new stream load up our output
-           buffer */
+         * buffer */
         else if (state->how == LOOK || len < (state->size << 1)) {
             /* get more output, looking for header if required */
             if (xz_make(state) == -1)
                 return -1;
-            continue;       /* no progress yet -- go back to memcpy() above */
+            continue;           /* no progress yet -- go back to memcpy() above */
             /* the copy above assures that we will leave with space in the
-               output buffer, allowing at least one gzungetc() to succeed */
+             * output buffer, allowing at least one gzungetc() to succeed */
         }
 
         /* large len -- read directly into user buffer */
-        else if (state->how == COPY) {      /* read directly */
+        else if (state->how == COPY) {  /* read directly */
             if (xz_load(state, buf, len, &n) == -1)
                 return -1;
         }
 
         /* large len -- decompress directly into user buffer */
-        else {  /* state->how == LZMA */
+        else {                  /* state->how == LZMA */
             strm->avail_out = len;
             strm->next_out = buf;
             if (xz_decomp(state) == -1)
@@ -645,16 +704,17 @@ int xzread(xzFile file, void *buf, unsigned len)
 
         /* update progress */
         len -= n;
-        buf = (char *)buf + n;
+        buf = (char *) buf + n;
         got += n;
         state->pos += n;
     } while (len);
 
     /* return number of bytes read into user buffer (will fit in int) */
-    return (int)got;
+    return (int) got;
 }
 
-int xzclose(xzFile file)
+int
+__libxml2_xzclose(xzFile file)
 {
     int ret;
     xz_statep state;
@@ -662,7 +722,7 @@ int xzclose(xzFile file)
     /* get internal structure */
     if (file == NULL)
         return LZMA_DATA_ERROR;
-    state = (xz_statep)file;
+    state = (xz_statep) file;
 
     /* free memory and close file */
     if (state->size) {
@@ -678,4 +738,4 @@ int xzclose(xzFile file)
     free(state);
     return ret ? ret : LZMA_OK;
 }
-
+#endif /* HAVE_LZMA_H */
diff --git a/xzlib.h b/xzlib.h
index 2adb7b3..43c75e1 100644
--- a/xzlib.h
+++ b/xzlib.h
@@ -1,6 +1,18 @@
-typedef void* xzFile;       /* opaque lzma file descriptor */
+/**
+ * xzlib.h: header for the front end for the transparent suport of lzma
+ *          compression at the I/O layer
+ *
+ * See Copyright for the status of this software.
+ *
+ * Anders F Bjorklund <afb users sourceforge net>
+ */
 
-static xzFile xzopen(const char *path, const char *mode);
-static xzFile xzdopen(int fd, const char *mode);
-static int xzread(xzFile file, void *buf, unsigned len);
-static int xzclose(xzFile file);
+#ifndef LIBXML2_XZLIB_H
+#define LIBXML2_XZLIB_H
+typedef void *xzFile;           /* opaque lzma file descriptor */
+
+xzFile __libxml2_xzopen(const char *path, const char *mode);
+xzFile __libxml2_xzdopen(int fd, const char *mode);
+int __libxml2_xzread(xzFile file, void *buf, unsigned len);
+int __libxml2_xzclose(xzFile file);
+#endif /* LIBXML2_XZLIB_H */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]