Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implement overlapping decompression function

  • Loading branch information...
commit 7c0682b468f887a62b117a95192c8cc402d60889 1 parent 081690a
austin seipp authored
Showing with 16 additions and 1 deletion.
  1. +16 −1 Codec/Compression/QuickLZ.hsc
View
17 Codec/Compression/QuickLZ.hsc
@@ -1,5 +1,4 @@
{-# LANGUAGE CPP, ForeignFunctionInterface #-}
-
-- |
-- Module : Codec.Compression.QuickLZ
-- Copyright : (c) Austin Seipp 2011
@@ -40,6 +39,7 @@ module Codec.Compression.QuickLZ
( -- * Compressing and decompressing strict 'ByteString's
compress -- :: S.ByteString -> S.ByteString
, decompress -- :: S.ByteString -> S.ByteString
+, decompress' -- :: S.ByteString -> S.ByteString
) where
import Foreign
@@ -81,6 +81,21 @@ decompress xs
return $ c_ - 4 -- hack: remove 4 ending bytes off of output string
{-# INLINEABLE decompress #-}
+-- | Decompress the input 'ByteString' and save memory via overlapping decompression.
+decompress' :: S.ByteString -> S.ByteString
+decompress' xs
+ | S.null xs = S.empty
+ | otherwise =
+ unsafePerformIO . allocaBytes qlz_state_decompress_sz $ \decompress_state -> do
+ d <- U.unsafeUseAsCString xs c_qlz_size_decompressed
+ let sz = (d + (d `shiftR` 3) + 400)
+ SI.createAndTrim sz $ \output -> do
+ U.unsafeUseAsCStringLen xs $ \(cstr,clen) -> do
+ let dest = output `plusPtr` (sz - clen)
+ SI.memcpy dest (castPtr cstr) (fromIntegral clen)
+ c_ <- c_qlz_decompress dest output decompress_state
+ return $ c_ - 4 -- hack: remove 4 ending bytes off of output string
+
--
-- Simple bindings to some constants
--

0 comments on commit 7c0682b

Please sign in to comment.
Something went wrong with that request. Please try again.