@@ -92,6 +92,7 @@ import Data.Word (Word8)
92
92
import GHC.IO (noDuplicate )
93
93
94
94
import qualified Codec.Compression.Zlib.Stream as Stream
95
+ import Codec.Compression.Zlib.ByteStringCompat (mkBS , withBS )
95
96
import Codec.Compression.Zlib.Stream (Stream )
96
97
97
98
-- | The full set of parameters for compression. The defaults are
@@ -471,13 +472,12 @@ compressStream format (CompressParams compLevel method bits memLevel
471
472
\ chunk -> do
472
473
Stream. deflateInit format compLevel method bits memLevel strategy
473
474
setDictionary mdict
474
- case chunk of
475
- _ | S. null chunk ->
476
- fillBuffers 20 -- gzip header is 20 bytes, others even smaller
477
-
478
- S. PS inFPtr offset length -> do
479
- Stream. pushInputBuffer inFPtr offset length
480
- fillBuffers initChunkSize
475
+ withBS chunk $ \ inFPtr length ->
476
+ if length == 0
477
+ then fillBuffers 20 -- gzip header is 20 bytes, others even smaller
478
+ else do
479
+ Stream. pushInputBuffer inFPtr 0 length
480
+ fillBuffers initChunkSize
481
481
482
482
where
483
483
-- we flick between two states:
@@ -507,11 +507,11 @@ compressStream format (CompressParams compLevel method bits memLevel
507
507
Stream. pushOutputBuffer outFPtr 0 outChunkSize
508
508
509
509
if inputBufferEmpty
510
- then return $ CompressInputRequired $ \ chunk ->
511
- case chunk of
512
- _ | S. null chunk -> drainBuffers True
513
- S. PS inFPtr offset length -> do
514
- Stream. pushInputBuffer inFPtr offset length
510
+ then return $ CompressInputRequired $ flip withBS $ \ inFPtr length ->
511
+ if length == 0
512
+ then drainBuffers True
513
+ else do
514
+ Stream. pushInputBuffer inFPtr 0 length
515
515
drainBuffers False
516
516
else drainBuffers False
517
517
@@ -534,7 +534,7 @@ compressStream format (CompressParams compLevel method bits memLevel
534
534
outputBufferFull <- Stream. outputBufferFull
535
535
if outputBufferFull
536
536
then do (outFPtr, offset, length ) <- Stream. popOutputBuffer
537
- let chunk = S. PS outFPtr offset length
537
+ let chunk = mkBS outFPtr offset length
538
538
return $ CompressOutputAvailable chunk $ do
539
539
fillBuffers defaultCompressBufferSize
540
540
else do fillBuffers defaultCompressBufferSize
@@ -545,7 +545,7 @@ compressStream format (CompressParams compLevel method bits memLevel
545
545
outputBufferBytesAvailable <- Stream. outputBufferBytesAvailable
546
546
if outputBufferBytesAvailable > 0
547
547
then do (outFPtr, offset, length ) <- Stream. popOutputBuffer
548
- let chunk = S. PS outFPtr offset length
548
+ let chunk = mkBS outFPtr offset length
549
549
Stream. finalise
550
550
return $ CompressOutputAvailable chunk (return CompressStreamEnd )
551
551
else do Stream. finalise
@@ -607,25 +607,25 @@ decompressStream format (DecompressParams bits initChunkSize mdict allMembers)
607
607
Stream. inflateReset
608
608
else assert outputBufferFull $
609
609
Stream. inflateInit format bits
610
- case chunk of
611
- _ | S. null chunk -> do
612
- -- special case to avoid demanding more input again
613
- -- always an error anyway
614
- when outputBufferFull $ do
615
- let outChunkSize = 1
616
- outFPtr <- Stream. unsafeLiftIO ( S. mallocByteString outChunkSize)
617
- Stream. pushOutputBuffer outFPtr 0 outChunkSize
618
- drainBuffers True
619
-
620
- S. PS inFPtr offset length -> do
621
- Stream. pushInputBuffer inFPtr offset length
622
- -- Normally we start with no output buffer (so counts as full) but
623
- -- if we're resuming then we'll usually still have output buffer
624
- -- space available
625
- assert (if not resume then outputBufferFull else True ) $ return ()
626
- if outputBufferFull
627
- then fillBuffers initChunkSize
628
- else drainBuffers False
610
+ withBS chunk $ \ inFPtr length ->
611
+ if length == 0
612
+ then do
613
+ -- special case to avoid demanding more input again
614
+ -- always an error anyway
615
+ when outputBufferFull $ do
616
+ let outChunkSize = 1
617
+ outFPtr <- Stream. unsafeLiftIO ( S. mallocByteString outChunkSize)
618
+ Stream. pushOutputBuffer outFPtr 0 outChunkSize
619
+ drainBuffers True
620
+ else do
621
+ Stream. pushInputBuffer inFPtr 0 length
622
+ -- Normally we start with no output buffer (so counts as full) but
623
+ -- if we're resuming then we'll usually still have output buffer
624
+ -- space available
625
+ assert (if not resume then outputBufferFull else True ) $ return ()
626
+ if outputBufferFull
627
+ then fillBuffers initChunkSize
628
+ else drainBuffers False
629
629
630
630
where
631
631
-- we flick between two states:
@@ -657,11 +657,12 @@ decompressStream format (DecompressParams bits initChunkSize mdict allMembers)
657
657
658
658
if inputBufferEmpty
659
659
then return $ DecompressInputRequired $ \ chunk ->
660
- case chunk of
661
- _ | S. null chunk -> drainBuffers True
662
- S. PS inFPtr offset length -> do
663
- Stream. pushInputBuffer inFPtr offset length
664
- drainBuffers False
660
+ withBS chunk $ \ inFPtr length ->
661
+ if length == 0
662
+ then drainBuffers True
663
+ else do
664
+ Stream. pushInputBuffer inFPtr 0 length
665
+ drainBuffers False
665
666
else drainBuffers False
666
667
667
668
@@ -682,7 +683,7 @@ decompressStream format (DecompressParams bits initChunkSize mdict allMembers)
682
683
outputBufferFull <- Stream. outputBufferFull
683
684
if outputBufferFull
684
685
then do (outFPtr, offset, length ) <- Stream. popOutputBuffer
685
- let chunk = S. PS outFPtr offset length
686
+ let chunk = mkBS outFPtr offset length
686
687
return $ DecompressOutputAvailable chunk $ do
687
688
fillBuffers defaultDecompressBufferSize
688
689
else do fillBuffers defaultDecompressBufferSize
@@ -695,7 +696,7 @@ decompressStream format (DecompressParams bits initChunkSize mdict allMembers)
695
696
if inputBufferEmpty
696
697
then do finish (DecompressStreamEnd S. empty)
697
698
else do (inFPtr, offset, length ) <- Stream. popRemainingInputBuffer
698
- let inchunk = S. PS inFPtr offset length
699
+ let inchunk = mkBS inFPtr offset length
699
700
finish (DecompressStreamEnd inchunk)
700
701
701
702
Stream. Error code msg -> case code of
@@ -714,7 +715,7 @@ decompressStream format (DecompressParams bits initChunkSize mdict allMembers)
714
715
outputBufferBytesAvailable <- Stream. outputBufferBytesAvailable
715
716
if outputBufferBytesAvailable > 0
716
717
then do (outFPtr, offset, length ) <- Stream. popOutputBuffer
717
- return (DecompressOutputAvailable (S. PS outFPtr offset length ) (return end))
718
+ return (DecompressOutputAvailable (mkBS outFPtr offset length ) (return end))
718
719
else return end
719
720
720
721
setDictionary :: Stream. DictionaryHash -> Maybe S. ByteString
@@ -902,7 +903,7 @@ decompressStreamST format params =
902
903
903
904
904
905
tryFollowingStream :: S. ByteString -> Stream. State s -> ST s (DecompressStream (ST s ))
905
- tryFollowingStream chunk zstate =
906
+ tryFollowingStream chunk zstate =
906
907
case S. length chunk of
907
908
0 -> return $ DecompressInputRequired $ \ chunk' -> case S. length chunk' of
908
909
0 -> finaliseStreamEnd S. empty zstate
0 commit comments