Skip to content
This repository has been archived by the owner on Sep 22, 2019. It is now read-only.

Commit

Permalink
implement overlapping decompression function
Browse files Browse the repository at this point in the history
  • Loading branch information
austin seipp committed Mar 14, 2011
1 parent 081690a commit 7c0682b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Codec/Compression/QuickLZ.hsc
@@ -1,5 +1,4 @@
{-# LANGUAGE CPP, ForeignFunctionInterface #-}

-- |
-- Module : Codec.Compression.QuickLZ
-- Copyright : (c) Austin Seipp 2011
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
--
Expand Down

0 comments on commit 7c0682b

Please sign in to comment.