forked from Sydsvenskan/xferspdy
/
diff.go
136 lines (116 loc) · 3.77 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2015 Monmohan Singh. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xferspdy
import (
"crypto/sha256"
"github.com/golang/glog"
"io"
"os"
)
// NewDiff computes a diff between a given file and Fingerprint created from some other file
// The diff is represented as a slice of Blocks. Matching Blocks are represented just by their hashes, start and end byte position
// Non-matching blocks are raw binary arrays.
func NewDiff(filename string, sign Fingerprint) []Block {
file, err := os.Open(filename)
defer file.Close()
if err != nil {
glog.Fatal(err)
}
finfo, _ := file.Stat()
var delta []Block
processBlock(file, 0, finfo.Size(), sign, &delta)
glog.V(3).Infof("Delta created %v\n", delta)
return delta
}
func processBlock(r io.Reader, rptr int64, filesz int64, s Fingerprint, delta *[]Block) {
blksz := int64(s.Blocksz)
brem := int64(blksz)
if (rptr + blksz) > filesz {
brem = filesz - rptr
}
glog.V(2).Infof("Process Block :rptr %d filesz %d blocksz %d brem %d \n", rptr, filesz, s.Blocksz, brem)
glog.V(4).Infof("Delta %v \n", *delta)
if brem == 0 {
glog.V(2).Infof("All read\n ")
return
}
buf := make([]byte, brem)
n, err := io.ReadFull(r, buf)
if err != nil || int64(n) != brem {
glog.Fatalf("Error %v read %d bytes", err, n)
}
checksum, state := Checksum(buf)
matchblock, matched := matchBlock(checksum, sha256.Sum256(buf), s)
if matched {
glog.V(3).Infof("Matched block %v \n", matchblock)
*delta = append(*delta, matchblock)
rptr += int64(brem)
processBlock(r, rptr, filesz, s, delta)
} else {
glog.V(3).Infof("Block not matched\n")
*delta = append(*delta, Block{HasData: true, Start: rptr})
processRolling(r, state, rptr, filesz, s, delta)
}
}
func processRolling(r io.Reader, st *State, rptr int64, filesz int64, s Fingerprint, delta *[]Block) {
diff := *delta
db := &diff[len(diff)-1]
glog.V(4).Infof("db.RawBytes %v \n", db)
brem := filesz - (rptr + int64(len(st.window)))
glog.V(4).Infof("Rolling State: State %v \n", *st)
glog.V(3).Infof("Rolling Info: rptr %d filesz %d blocksz %d brem %d \n", rptr, filesz, s.Blocksz, brem)
glog.V(4).Infof("Delta %v \n", *delta)
if brem == 0 {
db.RawBytes = append(db.RawBytes, st.window...)
*delta = diff
glog.V(4).Infof("db.RawBytes %v \n", db.RawBytes)
return
}
fb := st.window[0]
db.RawBytes = append(db.RawBytes, fb)
b := make([]byte, 1)
_, e := io.ReadFull(r, b)
if e != nil {
glog.Fatal(e)
}
rptr++
checksum := st.UpdateWindow(b[0])
matchblock, matched := matchBlock(checksum, sha256.Sum256(st.window), s)
if matched {
*delta = append(diff, matchblock)
rptr += int64(len(st.window))
processBlock(r, rptr, filesz, s, delta)
} else {
processRolling(r, st, rptr, filesz, s, delta)
}
}
func matchBlock(checksum uint32, sha256 [sha256.Size]byte, s Fingerprint) (mblock Block, matched bool) {
glog.V(3).Infof("comparing input checksum %d ", checksum)
if sha2blk, ok := s.BlockMap[checksum]; ok {
if block, m := sha2blk[sha256]; m {
glog.V(3).Infof("found match ")
return block, true
}
}
return Block{}, false
}
func (f *Fingerprint) DeepEqual(other *Fingerprint) bool {
eq := (f.Blocksz == other.Blocksz) && len(f.BlockMap) == len(other.BlockMap)
if !eq {
glog.Errorf("Fingerprints don't match, 1) source =%s, blocksz=%d, block map size=%d\n 2)source =%s, blocksz=%d, block map size=%d\n",
f.Source, f.Blocksz, len(f.BlockMap), other.Source, other.Blocksz, len(other.BlockMap))
}
if eq {
for _, shablkmap := range f.BlockMap {
for _, blk := range shablkmap {
var matched Block
matched, eq = matchBlock(blk.Checksum32, blk.Sha256hash, *other)
if !(eq && (matched.Start == blk.Start) && (matched.End == blk.End)) {
return false
}
}
}
}
return eq
}