compression.zlib: Just deal with raw bytes, no compressed object tuple.
							parent
							
								
									0371fa3137
								
							
						
					
					
						commit
						501f990971
					
				| 
						 | 
				
			
			@ -3,36 +3,19 @@
 | 
			
		|||
USING: help.markup help.syntax kernel math strings byte-arrays ;
 | 
			
		||||
IN: compression.zlib
 | 
			
		||||
 | 
			
		||||
HELP: <compressed>
 | 
			
		||||
{ $values
 | 
			
		||||
    { "data" byte-array } { "length" integer }
 | 
			
		||||
    { "compressed" compressed }
 | 
			
		||||
}
 | 
			
		||||
{ $description "Creates a new " { $link compressed } ", using the provided bytes as the compressed data and the provided length as the uncompressed length.  You should almost always use " { $link compress } ", rather than using this constructor directly." } ;
 | 
			
		||||
 | 
			
		||||
HELP: compress
 | 
			
		||||
{ $values
 | 
			
		||||
    { "byte-array" byte-array }
 | 
			
		||||
    { "compressed" compressed }
 | 
			
		||||
    { "byte-array'" byte-array }
 | 
			
		||||
}
 | 
			
		||||
{ $description "Compresses the given byte-array, returning a Factor object holding the compressed data." } ;
 | 
			
		||||
 | 
			
		||||
HELP: compressed
 | 
			
		||||
{ $class-description "The class used to hold compressed data." } ;
 | 
			
		||||
 | 
			
		||||
HELP: compressed-size
 | 
			
		||||
{ $values
 | 
			
		||||
    { "byte-array" byte-array }
 | 
			
		||||
    { "n" integer }
 | 
			
		||||
}
 | 
			
		||||
{ $description "Returns the maximum number of bytes required to store the compressed version of a byte array." } ;
 | 
			
		||||
{ $description "Returns a byte-array of compressed bytes." } ;
 | 
			
		||||
 | 
			
		||||
HELP: uncompress
 | 
			
		||||
{ $values
 | 
			
		||||
    { "compressed" compressed }
 | 
			
		||||
    { "byte-array" byte-array }
 | 
			
		||||
    { "byte-array'" byte-array }
 | 
			
		||||
}
 | 
			
		||||
{ $description "Uncompresses a compressed object, returning a byte-array of the underlying data." } ;
 | 
			
		||||
{ $description "Takes a zlib-compressed byte-array and uncompresses it to another byte-array." } ;
 | 
			
		||||
 | 
			
		||||
ARTICLE: "compression.zlib" "Compression (ZLIB)"
 | 
			
		||||
"The " { $vocab-link "compression.zlib" } " vocabulary provides support for ZLIB:"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,6 +7,5 @@ IN: compression.zlib.tests
 | 
			
		|||
: compress-me ( -- byte-array ) B{ 1 2 3 4 5 } ;
 | 
			
		||||
 | 
			
		||||
[ t ] [ compress-me [ compress uncompress ] keep = ] unit-test
 | 
			
		||||
[ t ] [ compress-me compress compressed instance? ] unit-test
 | 
			
		||||
 | 
			
		||||
[ ffi:Z_DATA_ERROR zlib-error-message ] [ string>> "data error" = ] must-fail-with
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,13 +6,6 @@ fry kernel libc math math.functions math.ranges sequences system ;
 | 
			
		|||
QUALIFIED: compression.zlib.ffi
 | 
			
		||||
IN: compression.zlib
 | 
			
		||||
 | 
			
		||||
TUPLE: compressed data length ;
 | 
			
		||||
 | 
			
		||||
: <compressed> ( data length -- compressed )
 | 
			
		||||
    compressed new
 | 
			
		||||
        swap >>length
 | 
			
		||||
        swap >>data ;
 | 
			
		||||
 | 
			
		||||
ERROR: zlib-failed n string ;
 | 
			
		||||
 | 
			
		||||
: zlib-error-message ( n -- * )
 | 
			
		||||
| 
						 | 
				
			
			@ -34,26 +27,13 @@ ERROR: zlib-failed n string ;
 | 
			
		|||
: compressed-size ( byte-array -- n )
 | 
			
		||||
    length 1001/1000 * ceiling 12 + ;
 | 
			
		||||
 | 
			
		||||
: compress ( byte-array -- compressed )
 | 
			
		||||
: compress ( byte-array -- byte-array' )
 | 
			
		||||
    [
 | 
			
		||||
        [
 | 
			
		||||
            compressed-size
 | 
			
		||||
            [ <byte-vector> dup underlying>> ] keep ulong <ref>
 | 
			
		||||
        ] keep [
 | 
			
		||||
            dup length compression.zlib.ffi:compress zlib-error
 | 
			
		||||
        ] 2keep drop ulong deref >>length B{ } like
 | 
			
		||||
    ] keep length <compressed> ;
 | 
			
		||||
 | 
			
		||||
GENERIC: uncompress ( obj -- byte-array )
 | 
			
		||||
 | 
			
		||||
M: compressed uncompress ( compressed -- byte-array )
 | 
			
		||||
    [
 | 
			
		||||
        length>> [ <byte-vector> dup underlying>> ] keep
 | 
			
		||||
        ulong <ref>
 | 
			
		||||
    ] [
 | 
			
		||||
        data>> dup length pick
 | 
			
		||||
        [ compression.zlib.ffi:uncompress zlib-error ] dip
 | 
			
		||||
    ] bi ulong deref >>length B{ } like ;
 | 
			
		||||
        compressed-size
 | 
			
		||||
        [ <byte-vector> dup underlying>> ] keep ulong <ref>
 | 
			
		||||
    ] keep [
 | 
			
		||||
        dup length compression.zlib.ffi:compress zlib-error
 | 
			
		||||
    ] 2keep drop ulong deref >>length B{ } like ;
 | 
			
		||||
 | 
			
		||||
: (uncompress) ( length byte-array -- byte-array )
 | 
			
		||||
    [
 | 
			
		||||
| 
						 | 
				
			
			@ -63,6 +43,6 @@ M: compressed uncompress ( compressed -- byte-array )
 | 
			
		|||
        2drop ulong deref memory>byte-array
 | 
			
		||||
    ] with-destructors ;
 | 
			
		||||
 | 
			
		||||
M: byte-array uncompress ( byte-array -- byte-array )
 | 
			
		||||
: uncompress ( byte-array -- byte-array' )
 | 
			
		||||
    [ length 5 [0,b) [ 2^ * ] with map ] keep
 | 
			
		||||
    '[ _ (uncompress) ] attempt-all ;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue