-
Notifications
You must be signed in to change notification settings - Fork 1
/
delta.go
136 lines (117 loc) · 3 KB
/
delta.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
package git
import (
"crypto/sha1"
"errors"
"fmt"
)
func deltaHdrSize(src []byte) (int, []byte) {
//log.Info("deltaHdrSize(%x)", src[:5])
// pop quiz: how many variable-length size encodings are there in git?
var size int
shift := uint(0)
for {
cmd := src[0]
src = src[1:]
size = size | (int(cmd&0x7f) << shift)
shift += 7
if cmd&0x80 == 0 || len(src) == 0 {
return size, src
}
}
}
var ErrBadDelta = errors.New("corrupt delta")
func patchDelta(mode ObjType, base, delta []byte) ([]byte, *Ptr, error) {
baseSize, delta := deltaHdrSize(delta)
if baseSize != len(base) {
log.Error("%s baseSize=%d len(base)=%d", mode, baseSize, len(base))
return nil, nil, ErrBadDelta
}
resultSize, delta := deltaHdrSize(delta)
//log.Info("%s baseSize=%d len(base)=%d resultSize=%d len(delta)=%d", mode, baseSize, len(base), resultSize, len(delta))
result := make([]byte, resultSize)
check := sha1.New()
//fmt.Printf(" %d byte base, %d byte result, type %s\n", baseSize, resultSize, mode)
//log.Info("%s %d", mode, resultSize)
fmt.Fprintf(check, "%s %d", mode, resultSize)
check.Write([]byte{0})
j := 0
i := 0
remain := resultSize
for i < len(delta) {
cmd := delta[i]
i++
if cmd&0x80 != 0 {
// "copy from base"
// an interesting and unique length encoding; it
// supports (don't know if they use) interior and right
// 00 compression!
var copyOffset, copySize int
if (cmd & 0x01) != 0 {
copyOffset = int(delta[i])
i++
}
if (cmd & 0x02) != 0 {
copyOffset |= int(delta[i]) << 8
i++
}
if (cmd & 0x04) != 0 {
copyOffset |= int(delta[i]) << 16
i++
}
if (cmd & 0x08) != 0 {
copyOffset |= int(delta[i]) << 24
i++
}
if (cmd & 0x10) != 0 {
copySize = int(delta[i])
i++
}
if (cmd & 0x20) != 0 {
copySize |= int(delta[i]) << 8
i++
}
if (cmd & 0x40) != 0 {
copySize |= int(delta[i]) << 16
i++
}
if copySize == 0 {
// default to a 64K chunk
copySize = 0x10000
}
/*fmt.Printf(" copy from base: %d bytes from offset %d\n%x\n",
copySize,
copyOffset,
base[copyOffset:copyOffset+copySize])*/
copy(result[j:j+copySize], base[copyOffset:])
check.Write(base[copyOffset : copyOffset+copySize])
j += copySize
remain -= copySize
} else if cmd != 0 {
// copy from delta, up to 127 bytes
copySize := int(cmd)
if copySize > remain {
break
}
copy(result[j:j+copySize], delta[i:])
check.Write(delta[i : i+copySize])
/*fmt.Printf(" copy from delta: %d bytes from offset %d\n%x\n",
cmd, i, delta[i:i+copySize])*/
i += copySize
j += copySize
remain -= copySize
} else {
return nil, nil, ErrUnexpectedDeltaOpcode
}
}
if j != resultSize {
panic("didn't seem to write it all")
}
var p Ptr
copy(p.hash[:], check.Sum(nil))
/*f := "/tmp/x-" + ((&p).String())
log.Info("Writing %d bytes to %s", len(result), f)
ioutil.WriteFile(f, result, 0666)
*/
return result, &p, nil
}
var ErrUnexpectedDeltaOpcode = errors.New("unexpected delta opcode 0")