-
Notifications
You must be signed in to change notification settings - Fork 0
/
delta.go
131 lines (122 loc) · 2.95 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
// Copyright 2014 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package ggit
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
)
type compressedDeltaReader struct {
baseCompressed []byte
deltasCompressed [][]byte
r io.Reader // Lazily set on first access
}
func readAllBytes(compressed []byte) ([]byte, error) {
r, err := getZlibReader(bytes.NewReader(compressed))
if err != nil {
return nil, err
}
defer returnZlibReader(r)
return ioutil.ReadAll(r)
}
func (d *compressedDeltaReader) Read(b []byte) (int, error) {
if d.r == nil {
base, err := readAllBytes(d.baseCompressed)
if err != nil {
return 0, fmt.Errorf("error decompressing base: %v", err)
}
patched := base
for i := range d.deltasCompressed {
delta, err := readAllBytes(d.deltasCompressed[len(d.deltasCompressed)-i-1])
if err != nil {
return 0, fmt.Errorf("error decompressing delta: %v", err)
}
patched, err = patchDelta(patched, delta)
if err != nil {
return 0, fmt.Errorf("error applying delta: %v", err)
}
}
d.r = bytes.NewReader(patched)
}
return d.r.Read(b)
}
func deltaHeaderSize(delta []byte) (uint32, int) {
used := 0
size := uint32(0)
shift := uint32(0)
for {
c := delta[used]
used++
size |= uint32(c&0x7f) << shift
shift += 7
if (c&0x80) == 0 || used == len(delta) {
return size, used
}
}
}
func patchDelta(base, delta []byte) ([]byte, error) {
if len(delta) < 4 {
return nil, fmt.Errorf("delta too small: %d", delta)
}
expectedSourceSize, used := deltaHeaderSize(delta)
if expectedSourceSize != uint32(len(base)) {
return nil, fmt.Errorf("source size %d but delta header says %d",
len(base), expectedSourceSize)
}
resultSize, moreUsed := deltaHeaderSize(delta[used:])
used += moreUsed
result := make([]byte, resultSize)
resultOff := 0
for i := used; i < len(delta); {
cmd := delta[i]
i++
if (cmd & 0x80) != 0 {
copyOffset, copySize := 0, 0
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 {
copySize = 0x10000
}
copy(result[resultOff:resultOff+copySize], base[copyOffset:copyOffset+copySize])
resultOff += copySize
} else if cmd != 0 {
copySize := int(cmd)
copy(result[resultOff:resultOff+copySize], delta[i:i+copySize])
resultOff += copySize
i += copySize
} else {
return nil, errors.New("unexpected delta opcode 0")
}
}
return result, nil
}