]> gitweb.factorcode.org Git - factor.git/commitdiff
compression.zlib: Add some helper functions for streaming unzipping.
authorDoug Coleman <doug.coleman@gmail.com>
Tue, 19 May 2015 17:57:13 +0000 (10:57 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Tue, 19 May 2015 17:57:13 +0000 (10:57 -0700)
basis/compression/zlib/zlib.factor

index 111a41ea5ec5fa9c800e8fdee6505555e7287633..533d55bb68b68feaa05c0bbfa9e984f044e46f40 100644 (file)
@@ -1,8 +1,9 @@
 ! Copyright (C) 2009 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors alien alien.c-types alien.data alien.syntax
-byte-arrays byte-vectors combinators continuations destructors
-fry kernel libc math math.functions math.ranges sequences system ;
+byte-arrays byte-vectors classes.struct combinators
+compression.zlib.ffi continuations destructors fry kernel libc
+math math.functions math.ranges sequences system ;
 QUALIFIED: compression.zlib.ffi
 IN: compression.zlib
 
@@ -22,7 +23,11 @@ ERROR: zlib-failed n string ;
     ] if zlib-failed ;
 
 : zlib-error ( n -- )
-    dup compression.zlib.ffi:Z_OK = [ drop ] [ dup zlib-error-message zlib-failed ] if ;
+    dup {
+        { compression.zlib.ffi:Z_OK [ drop ] }
+        { compression.zlib.ffi:Z_STREAM_END [ drop ] }
+        [ dup zlib-error-message zlib-failed ]
+    } case ;
 
 : compressed-size ( byte-array -- n )
     length 1001/1000 * ceiling 12 + ;
@@ -46,3 +51,29 @@ ERROR: zlib-failed n string ;
 : uncompress ( byte-array -- byte-array' )
     [ length 5 [0,b) [ 2^ * ] with map ] keep
     '[ _ (uncompress) ] attempt-all ;
+
+
+: zlib-inflate-init ( -- z_stream_s )
+    z_stream <struct> ZLIB_VERSION over byte-length [
+        inflateInit_ zlib-error
+    ] 3keep 2drop  ;
+
+! window can be 0, 15, 32, 47 (others?)
+: zlib-inflate-init2 ( window -- z_stream_s )
+    [ z_stream <struct> ] dip ZLIB_VERSION pick byte-length [
+        inflateInit2_ zlib-error
+    ] 4keep 3drop  ;
+
+: zlib-inflate-end ( z_stream -- )
+    inflateEnd zlib-error ;
+
+: zlib-inflate-reset ( z_stream -- )
+    inflateReset zlib-error ;
+
+: zlib-inflate ( z_stream flush -- )
+    inflate zlib-error ;
+
+: zlib-inflate-get-header ( z_stream -- gz_header )
+    gz_header <struct> [
+        inflateGetHeader zlib-error
+    ] keep ;