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
|
// Package apply applies Git delta instruction streams.
package apply
import "fmt"
// Apply applies one Git delta instruction stream to base.
func Apply(base, delta []byte) ([]byte, error) {
pos := 0
srcSize, err := readVarint(delta, &pos)
if err != nil {
return nil, err
}
dstSize, err := readVarint(delta, &pos)
if err != nil {
return nil, err
}
if srcSize != len(base) {
return nil, fmt.Errorf("format/delta/apply: delta source size mismatch: got %d want %d", srcSize, len(base))
}
out := make([]byte, dstSize)
outPos := 0
for pos < len(delta) {
op := delta[pos]
pos++
if op&0x80 != 0 {
off := 0
if op&0x01 != 0 {
if pos >= len(delta) {
return nil, fmt.Errorf("format/delta/apply: malformed delta copy offset")
}
off |= int(delta[pos])
pos++
}
if op&0x02 != 0 {
if pos >= len(delta) {
return nil, fmt.Errorf("format/delta/apply: malformed delta copy offset")
}
off |= int(delta[pos]) << 8
pos++
}
if op&0x04 != 0 {
if pos >= len(delta) {
return nil, fmt.Errorf("format/delta/apply: malformed delta copy offset")
}
off |= int(delta[pos]) << 16
pos++
}
if op&0x08 != 0 {
if pos >= len(delta) {
return nil, fmt.Errorf("format/delta/apply: malformed delta copy offset")
}
off |= int(delta[pos]) << 24
pos++
}
n := 0
if op&0x10 != 0 {
if pos >= len(delta) {
return nil, fmt.Errorf("format/delta/apply: malformed delta copy size")
}
n |= int(delta[pos])
pos++
}
if op&0x20 != 0 {
if pos >= len(delta) {
return nil, fmt.Errorf("format/delta/apply: malformed delta copy size")
}
n |= int(delta[pos]) << 8
pos++
}
if op&0x40 != 0 {
if pos >= len(delta) {
return nil, fmt.Errorf("format/delta/apply: malformed delta copy size")
}
n |= int(delta[pos]) << 16
pos++
}
if n == 0 {
n = 0x10000
}
if off < 0 || n < 0 || off+n > len(base) || outPos+n > len(out) {
return nil, fmt.Errorf("format/delta/apply: delta copy out of bounds")
}
copy(out[outPos:outPos+n], base[off:off+n])
outPos += n
continue
}
if op == 0 {
return nil, fmt.Errorf("format/delta/apply: invalid delta opcode 0")
}
n := int(op)
if pos+n > len(delta) || outPos+n > len(out) {
return nil, fmt.Errorf("format/delta/apply: delta insert out of bounds")
}
copy(out[outPos:outPos+n], delta[pos:pos+n])
outPos += n
pos += n
}
if outPos != len(out) {
return nil, fmt.Errorf("format/delta/apply: delta output size mismatch: got %d want %d", outPos, len(out))
}
return out, nil
}
// readVarint parses one Git delta varint and advances pos.
func readVarint(buf []byte, pos *int) (int, error) {
value := 0
shift := uint(0)
for {
if *pos >= len(buf) {
return 0, fmt.Errorf("format/delta/apply: malformed delta varint")
}
b := buf[*pos]
*pos++
value |= int(b&0x7f) << shift
if b&0x80 == 0 {
break
}
shift += 7
if shift > 63 {
return 0, fmt.Errorf("format/delta/apply: delta varint overflow")
}
}
return value, nil
}
|