lz4 (empty) → 0.1.0.0
raw patch · 5 files changed
+114/−0 lines, 5 filesdep +QuickCheckdep +basedep +bytestringsetup-changed
Dependencies added: QuickCheck, base, bytestring, hspec, hspec-discover, hspec-shouldbe, lz4
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- lz4.cabal +33/−0
- src/Codec/Compression/LZ4.hs +47/−0
- test/Spec.hs +2/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2012, Mark Wotton++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Mark Wotton nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ lz4.cabal view
@@ -0,0 +1,33 @@+name: lz4+version: 0.1.0.0+synopsis: Haskell bindings to the lz4 compression library+description: lz4 is a fast compression library, and can be found at+ http://code.google.com/p/lz4/+ a version that adds a C library can be found at+ https://github.com/mwotton/lz4+homepage: http://github.com/mwotton/lz4hs+license: BSD3+license-file: LICENSE+author: Mark Wotton+maintainer: mwotton@gmail.com++category: Codec+build-type: Simple+cabal-version: >=1.8+source-repository head+ type: git+ location: git://github.com/mwotton/lz4hs.git++library+ exposed-modules: Codec.Compression.LZ4+ hs-source-dirs: src+ extra-libraries: lz4+ build-depends: base ==4.5.*, bytestring++test-suite spec+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ hs-source-dirs: test+ extra-libraries: lz4+ build-depends: base, hspec, hspec-discover, lz4, QuickCheck, hspec-shouldbe,+ bytestring
+ src/Codec/Compression/LZ4.hs view
@@ -0,0 +1,47 @@+module Codec.Compression.LZ4 (compress, compressHC, uncompress) where+import Codec.Compression.LZ4.Foreign+import Foreign+import Data.ByteString+import Debug.Trace+import Data.ByteString.Unsafe++-- Following zlib's lead, we pretend to be pure on Bytestrings+-- errors may be thrown for bad formats. I'm not entirely comfortable with this.++-- we use unsafeUseAsCStringLen because the underlying files may be large, and+-- we trust lz4 not to tamper with the input buffer.++-- TODO: use a standard chunk size & support lazy Bytestrings+-- TODO: load files from disk: we don't currently support the full file format with+-- magic archive numbers etc.++-- compressHC = compressWorker c_LZ4_compressHC+-- compress = compressWorker c_LZ4_compress++-- for some reason, when I factor this out in the obvious way, I get link errors. Very confusing. FIXME+compressHC x = unsafePerformIO $ unsafeUseAsCStringLen x $ \(input,len) ->+ let outLen = c_LZ4_compressBound len in+ allocaBytes (1+outLen) $ \out -> do + written <- c_LZ4_compressHC input out (fromIntegral len)+ if written == 0+ then error "compression failed"+ else packCStringLen (out, written) -- `doTrace` ("compress", x, len, outLen, written)++compress x = unsafePerformIO $ unsafeUseAsCStringLen x $ \(input,len) ->+ let outLen = c_LZ4_compressBound len in+ allocaBytes (1+outLen) $ \out -> do + written <- c_LZ4_compress input out (fromIntegral len)+ if written == 0+ then error "compression failed"+ else packCStringLen (out, written) -- `doTrace` ("compress", x, len, outLen, written)++uncompress :: ByteString -> ByteString+uncompress x = unsafePerformIO $ unsafeUseAsCStringLen x $ \(input,len) ->+ -- this is a complete punt. let's say we use 5*len for the moment,+ -- and extend to lazy bytestrings later.+ let outLen = 5 * len in+ allocaBytes (1+outLen) $ \out -> do+ written <- c_LZ4_uncompress_unknownOutputSize input out (fromIntegral len) (fromIntegral outLen)+ if written < 0+ then error "decompression failed!"+ else packCStringLen (out, written)
+ test/Spec.hs view
@@ -0,0 +1,2 @@+-- file test/Spec.hs+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}