--- /dev/null
+! Copyright (C) 2022 Raghu Ranganathan.
+! See http://factorcode.org/license.txt for BSD license.
+USING: tools.test byte-arrays random locals sequences ;
+IN: compression.bzip3.tests
+
+! Simple compression and decompression
+{ B{
+ 0 0 0 0 0 0 0 12 66 90 51 118 49 0 4 1 0 1 0 0 0 20 0 0 0 12
+ 0 0 0 177 144 125 228 255 255 255 255 72 101 108 108 111 32
+ 87 111 114 108 100 33 0 0 0
+} } [ B{ 72 101 108 108 111 32 87 111 114 108 100 33 } f compress ] unit-test
+{ B{
+ 72 101 108 108 111 32 87 111 114 108 100 33
+} } [ B{
+ 0 0 0 0 0 0 0 12 66 90 51 118 49 0 4 1 0 1 0 0 0 20 0 0 0 12 0 0 0 177 144
+ 125 228 255 255 255 255 72 101 108 108 111 32 87 111 114 108
+ 100 33 0 0 0
+} decompress ] unit-test
+
+! Random byte sequences
+40 [| |
+ 36 [ 256 random ] B{ } replicate-as :> input
+ { input } [ input f compress decompress ] unit-test
+] times
+
+! Incorrect block sizes must error
+[ B{ } 0 compress ] must-fail
+[ B{ } 536870912 compress ] must-fail
-USING: alien alien.libraries alien.c-types alien.data alien.syntax
+USING: alien alien.libraries alien.c-types alien.data alien.syntax endian
kernel io.encodings.string byte-arrays sequences combinators syntax
compression.bzip3.ffi locals math math.order summary pair-rocket ;
IN: compression.bzip3
M: invalid-block-size summary drop "Block size must be between 65 KiB and 511 MiB" ;
ERROR: internal-error msg ;
M: internal-error summary drop "bzip3: Internal Error" ;
+
<PRIVATE
CONSTANT: dsize 1048576 ! placeholder block size
out-size <byte-array> :> out
block-size/f [ dsize ] unless* validate-block-size
byte-array out in-size out-size size_t <ref> bz3_compress
- dup 0 = [ drop out ] [ throw-internal-error ] if
+ dup 0 = [ drop in-size 8 >be out append ] [ throw-internal-error ] if
;
:: decompress ( byte-array -- byte-array' )
- byte-array length :> in-size
- in-size bz3_bound :> out-size
+ byte-array 8 cut-slice :> ( head in )
+ in length :> in-size
+ head be> :> out-size
out-size <byte-array> :> out
- byte-array out in-size out-size size_t <ref> bz3_decompress
+ in out in-size out-size size_t <ref> bz3_decompress
dup 0 = [ drop out ] [ throw-internal-error ] if
;