]> gitweb.factorcode.org Git - factor.git/commitdiff
compression.snappy: Add support for snappy compression by Google. Could possibly...
authorDoug Coleman <doug.coleman@gmail.com>
Sat, 4 Oct 2014 05:56:21 +0000 (22:56 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Sat, 4 Oct 2014 05:56:21 +0000 (22:56 -0700)
basis/compression/snappy/authors.txt [new file with mode: 0644]
basis/compression/snappy/ffi/authors.txt [new file with mode: 0644]
basis/compression/snappy/ffi/ffi.factor [new file with mode: 0644]
basis/compression/snappy/snappy-tests.factor [new file with mode: 0644]
basis/compression/snappy/snappy.factor [new file with mode: 0644]

diff --git a/basis/compression/snappy/authors.txt b/basis/compression/snappy/authors.txt
new file mode 100644 (file)
index 0000000..7c1b2f2
--- /dev/null
@@ -0,0 +1 @@
+Doug Coleman
diff --git a/basis/compression/snappy/ffi/authors.txt b/basis/compression/snappy/ffi/authors.txt
new file mode 100644 (file)
index 0000000..7c1b2f2
--- /dev/null
@@ -0,0 +1 @@
+Doug Coleman
diff --git a/basis/compression/snappy/ffi/ffi.factor b/basis/compression/snappy/ffi/ffi.factor
new file mode 100644 (file)
index 0000000..5cdad5b
--- /dev/null
@@ -0,0 +1,32 @@
+! Copyright (C) 2014 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: alien alien.c-types alien.libraries
+alien.libraries.finder alien.syntax classes.struct ;
+USE: nested-comments
+IN: compression.snappy.ffi
+
+<< "snappy" "snappy" find-library cdecl add-library >>
+
+LIBRARY: snappy
+
+ENUM: snappy_status SNAPPY_OK SNAPPY_INVALID_INPUT SNAPPY_BUFFER_TOO_SMALL ;
+
+FUNCTION: snappy_status snappy_compress ( char* input,
+                                          size_t input_length,
+                                          char* compressed,
+                                          size_t* compressed_length ) ;
+
+FUNCTION: snappy_status snappy_uncompress ( char* compressed,
+                                            size_t compressed_length,
+                                            char* uncompressed,
+                                            size_t* uncompressed_length ) ;
+
+FUNCTION: size_t snappy_max_compressed_length ( size_t source_length ) ;
+
+FUNCTION: snappy_status snappy_uncompressed_length ( char* compressed,
+                                                     size_t compressed_length,
+                                                     size_t* result ) ;
+
+FUNCTION: snappy_status snappy_validate_compressed_buffer ( char* compressed,
+                                                            size_t compressed_length ) ; 
+
diff --git a/basis/compression/snappy/snappy-tests.factor b/basis/compression/snappy/snappy-tests.factor
new file mode 100644 (file)
index 0000000..9411ac0
--- /dev/null
@@ -0,0 +1,24 @@
+! Copyright (C) 2014 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: arrays byte-arrays compression.snappy kernel tools.test ;
+IN: compression.snappy.tests
+
+[ t ] [
+    1000 2 <array> >byte-array [ snappy-compress snappy-uncompress ] keep =
+] unit-test
+
+[ t ] [
+    B{ } [ snappy-compress snappy-uncompress ] keep =
+] unit-test
+
+[ t ] [
+    B{ 1 } [ snappy-compress snappy-uncompress ] keep =
+] unit-test
+
+[ t ] [
+    B{ 1 2 } [ snappy-compress snappy-uncompress ] keep =
+] unit-test
+
+[ t ] [
+    B{ 1 2 3 } [ snappy-compress snappy-uncompress ] keep =
+] unit-test
diff --git a/basis/compression/snappy/snappy.factor b/basis/compression/snappy/snappy.factor
new file mode 100644 (file)
index 0000000..10bd78f
--- /dev/null
@@ -0,0 +1,33 @@
+! Copyright (C) 2014 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: alien.c-types alien.data byte-arrays compression.snappy.ffi
+kernel sequences ;
+IN: compression.snappy
+
+ERROR: snappy-error error ;
+
+<PRIVATE
+
+: check-snappy ( ret -- )
+    dup SNAPPY_OK = [ drop ] [ snappy-error ] if ;
+
+: n>outs ( n -- byte-array size_t* )
+    [ <byte-array> ] [ size_t <ref> ] bi ;
+
+PRIVATE>
+
+: snappy-compress ( byte-array -- compressed )
+    dup length
+    dup snappy_max_compressed_length
+    n>outs
+    [ snappy_compress check-snappy ] 2keep size_t deref head ;
+
+: snappy-uncompress ( compressed -- byte-array )
+    dup length
+    over
+    dup length 0 size_t <ref>
+    [ snappy_uncompressed_length check-snappy ] keep
+    size_t deref 
+    n>outs
+    [ snappy_uncompress check-snappy ] 2keep drop >byte-array ;
+