@@ -1,110 +0,0 @@
1−{-# OPTIONS_GHC -Wall #-}
14−import Prelude hiding (appendFile)
15−import Control.Monad (foldM)
16−import qualified Control.Monad.State as State
17−import qualified Data.Binary.Get as Binary
18−import qualified Data.ByteString as BS
19−import qualified Data.ByteString.Builder as BS
20−import qualified Data.ByteString.Builder.Extra as BS (defaultChunkSize)
21−import qualified Data.ByteString.Lazy as LBS
22−import qualified Data.Digest.Pure.SHA as SHA
23−import qualified System.IO as IO
30−type Hasher = State.StateT State IO ()
33−type Digest = SHA.Digest SHA.SHA1State
36−put :: FilePath -> Hasher -> IO Digest
38− IO.withBinaryFile path IO.WriteMode $ \handle ->
39− do let state = State handle 0 SHA.sha1Incremental
40− (State _ len decoder) <- State.execStateT hasher state
41− return (SHA.completeSha1Incremental decoder len)
44−toString :: Digest -> String
55− { _handle :: !IO.Handle
57− , _decoder :: !(Binary.Decoder SHA.SHA1State)
65−putByteString :: BS.ByteString -> Hasher
67− do state <- State.get
68− State.put =<< State.liftIO (appendByteString state chunk)
71−putBuilder :: BS.Builder -> Hasher
73− do state <- State.get
74− State.put =<< State.liftIO (appendBuilder builder state)
77−putFile :: FilePath -> Hasher
79− do state <- State.get
80− State.put =<< State.liftIO (appendFile path state)
87−appendByteString :: State -> BS.ByteString -> IO State
88−appendByteString (State handle len decoder) chunk =
89− do BS.hPut handle chunk
90− return $ State handle (len + BS.length chunk) (Binary.pushChunk decoder chunk)
93−appendBuilder :: BS.Builder -> State -> IO State
94−appendBuilder builder state =
95− foldM appendByteString state $
96− LBS.toChunks (BS.toLazyByteString builder)
99−appendFile :: FilePath -> State -> IO State
100−appendFile path state =
101− IO.withBinaryFile path IO.ReadMode $ \handle ->
102− appendHelp handle state
105−appendHelp :: IO.Handle -> State -> IO State
106−appendHelp handle state =
107− do chunk <- BS.hGet handle BS.defaultChunkSize
110− else appendHelp handle =<< appendByteString state chunk