Bump to libxml2 2.9.4
[platform/upstream/libxml2.git] / xzlib.c
diff --git a/xzlib.c b/xzlib.c
index 928bd17..782957f 100644 (file)
--- a/xzlib.c
+++ b/xzlib.c
@@ -8,7 +8,7 @@
  */
 #define IN_LIBXML
 #include "libxml.h"
-#ifdef HAVE_LZMA_H
+#ifdef LIBXML_LZMA_ENABLED
 
 #include <string.h>
 #ifdef HAVE_ERRNO_H
@@ -34,7 +34,9 @@
 #ifdef HAVE_ZLIB_H
 #include <zlib.h>
 #endif
+#ifdef HAVE_LZMA_H
 #include <lzma.h>
+#endif
 
 #include "xzlib.h"
 #include <libxml/xmlmemory.h>
@@ -182,12 +184,37 @@ xz_open(const char *path, int fd, const char *mode ATTRIBUTE_UNUSED)
     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)
 {
@@ -245,6 +272,20 @@ xz_avail(xz_statep state)
     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)
 {
@@ -314,6 +355,10 @@ is_format_lzma(xz_statep state)
 #define NEXT() ((strm->avail_in == 0 && xz_avail(state) == -1) ? -1 : \
                 (strm->avail_in == 0 ? -1 : \
                  (strm->avail_in--, *(strm->next_in)++)))
+/* Same thing, but from zstrm */
+#define NEXTZ() ((strm->avail_in == 0 && xz_avail_zstrm(state) == -1) ? -1 : \
+                (strm->avail_in == 0 ? -1 : \
+                 (strm->avail_in--, *(strm->next_in)++)))
 
 /* 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. */
@@ -324,10 +369,10 @@ gz_next4(xz_statep state, unsigned long *ret)
     unsigned long val;
     z_streamp strm = &(state->zstrm);
 
-    val = NEXT();
-    val += (unsigned) NEXT() << 8;
-    val += (unsigned long) NEXT() << 16;
-    ch = NEXT();
+    val = NEXTZ();
+    val += (unsigned) NEXTZ() << 8;
+    val += (unsigned long) NEXTZ() << 16;
+    ch = NEXTZ();
     if (ch == -1)
         return -1;
     val += (unsigned long) ch << 24;
@@ -538,6 +583,10 @@ xz_decomp(xz_statep state)
             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);
 
     /* update available output and crc check value */
@@ -752,4 +801,4 @@ __libxml2_xzclose(xzFile file)
     xmlFree(state);
     return ret ? ret : LZMA_OK;
 }
-#endif /* HAVE_LZMA_H */
+#endif /* LIBXML_LZMA_ENABLED */