]> git.rkrishnan.org Git - hs-rsync.git/blob - src/Lib.hs
01eb180471788a9a7645f9c32204df78c6bae021
[hs-rsync.git] / src / Lib.hs
1 {-# LANGUAGE OverloadedStrings #-}
2 module Lib
3     ( fileSignatures
4     , genInstructions
5     , recreate
6     ) where
7
8 import Control.Monad.State
9 import Data.Bits (shiftL, (.&.), (.|.))
10 import qualified Data.ByteString.Char8 as BS
11 import qualified Data.ByteString.Lazy as BL
12 import Data.Char (ord)
13 import Data.Digest.Adler32 (adler32)
14 import qualified Data.Map as M
15 import Data.Word (Word8, Word32)
16 import qualified Crypto.Hash.MD4 as MD4
17
18 type Md4digest       = BS.ByteString
19 type Adler32checksum = Word32
20
21 type Signature     = (Md4digest, Adler32checksum, Int)
22
23 fileSignatures :: BL.ByteString -> Integer -> [Signature]
24 fileSignatures bs blockSize = zip3 strongsigs weaksigs [0..]
25   where strongsigs = map blockSig (splitBS bs blockSize)
26         weaksigs   = map adler32 (splitBS bs blockSize)
27
28 splitBS :: BL.ByteString -> Integer -> [BL.ByteString]
29 splitBS bs blockSize | fromIntegral (BL.length bs) < blockSize = [bs]
30 splitBS bs blockSize = BL.take (fromIntegral blockSize) bs :
31   splitBS (BL.drop (fromIntegral blockSize) bs) blockSize
32
33 -- compute md4 digest (128 bits)
34 blockSig :: BL.ByteString -> BS.ByteString
35 blockSig = MD4.hash . BL.toStrict
36
37 weakSig :: BL.ByteString -> Adler32checksum
38 weakSig = adler32
39
40 data Instruction = RChar Word8
41                  | RBlk  Int
42                  deriving Show
43
44 genInstructions :: [Signature] -> Integer -> BL.ByteString -> [Instruction]
45 genInstructions f0sigs blockSize fnew =
46   evalState (go fnew) 0
47   where
48     go :: BL.ByteString -> State Word32 [Instruction]
49     go fnew | fnew == BL.empty = return []
50             | otherwise =
51                 let (blk, blks) = BL.splitAt (fromIntegral blockSize) fnew
52                     adlerSum    = weakSig blk
53                     matches     = M.lookup adlerSum f0AdlerTable >>
54                       M.lookup (blockSig blk) f0MD4Table
55                 in
56                   case matches of
57                     Just idxs -> do
58                       is <- go blks
59                       return $ RBlk (head idxs) : is
60                     Nothing -> do
61                       put adlerSum
62                       is <- go (BL.tail (blk `mappend` blks))
63                       return $ RChar (BL.head blk) : is
64     f0AdlerTable = toAdlerMap f0sigs
65     f0MD4Table   = toMD4Map f0sigs
66
67 toAdlerMap :: [Signature] -> M.Map Adler32checksum [Int]
68 toAdlerMap = foldr f M.empty
69   where f sig m = let (_, aSig, idx) = sig in M.insertWith (++) aSig [idx] m
70
71 toMD4Map :: [Signature] -> M.Map Md4digest [Int]
72 toMD4Map = foldr f M.empty
73   where f sig m = let (mSig, _, idx) = sig in M.insertWith (++) mSig [idx] m
74
75 recreate :: BL.ByteString -> Integer -> [Instruction] -> BL.ByteString
76 recreate f0 blockSize ins =
77   let f0blocks = splitBS f0 blockSize
78   in
79     go f0blocks ins
80   where go f0blocks [] = mempty
81         go f0blocks (inst:insts) =
82           case inst of
83             RBlk i  -> (f0blocks !! i) `mappend` go f0blocks insts
84             RChar w -> BL.singleton w `mappend` go f0blocks insts
85