summaryrefslogtreecommitdiff
path: root/os/boot/libflate/inflatezlib.c
diff options
context:
space:
mode:
authorCharles.Forsyth <devnull@localhost>2006-12-22 21:39:35 +0000
committerCharles.Forsyth <devnull@localhost>2006-12-22 21:39:35 +0000
commit74a4d8c26dd3c1e9febcb717cfd6cb6512991a7a (patch)
treec6e220ba61db3a6ea4052e6841296d829654e664 /os/boot/libflate/inflatezlib.c
parent46439007cf417cbd9ac8049bb4122c890097a0fa (diff)
20060303
Diffstat (limited to 'os/boot/libflate/inflatezlib.c')
-rw-r--r--os/boot/libflate/inflatezlib.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/os/boot/libflate/inflatezlib.c b/os/boot/libflate/inflatezlib.c
new file mode 100644
index 00000000..961a191e
--- /dev/null
+++ b/os/boot/libflate/inflatezlib.c
@@ -0,0 +1,65 @@
+#include "lib9.h"
+#include <flate.h>
+#include "zlib.h"
+
+typedef struct ZWrite ZWrite;
+
+struct ZWrite
+{
+ ulong adler;
+ void *wr;
+ int (*w)(void*, void*, int);
+};
+
+static int
+zlwrite(void *vzw, void *buf, int n)
+{
+ ZWrite *zw;
+
+ zw = vzw;
+ zw->adler = adler32(zw->adler, buf, n);
+ n = (*zw->w)(zw->wr, buf, n);
+ if(n <= 0)
+ return n;
+ return n;
+}
+
+int
+inflatezlib(void *wr, int (*w)(void*, void*, int), void *getr, int (*get)(void*))
+{
+ ZWrite zw;
+ ulong v;
+ int c, i;
+
+ c = (*get)(getr);
+ if(c < 0)
+ return FlateInputFail;
+ i = (*get)(getr);
+ if(i < 0)
+ return FlateInputFail;
+
+ if(((c << 8) | i) % 31)
+ return FlateCorrupted;
+ if((c & ZlibMeth) != ZlibDeflate
+ || (c & ZlibCInfo) > ZlibWin32k)
+ return FlateCorrupted;
+
+ zw.wr = wr;
+ zw.w = w;
+ zw.adler = 1;
+ i = inflate(&zw, zlwrite, getr, get);
+ if(i != FlateOk)
+ return i;
+
+ v = 0;
+ for(i = 0; i < 4; i++){
+ c = (*get)(getr);
+ if(c < 0)
+ return FlateInputFail;
+ v = (v << 8) | c;
+ }
+ if(zw.adler != v)
+ return FlateCorrupted;
+
+ return FlateOk;
+}