-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathpack.go
235 lines (216 loc) · 7.19 KB
/
pack.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gitfs
import (
"bytes"
"compress/zlib"
"crypto/sha1"
"encoding/binary"
"fmt"
"io"
)
// unpack parses data, which is a Git pack-formatted archive,
// writing every object it contains to the store s.
//
// See https://git-scm.com/docs/pack-format for format documentation.
func unpack(s *store, data []byte) error {
// If the store is empty, pre-allocate the length of data.
// This should be about the right order of magnitude for the eventual data,
// avoiding many growing steps during append.
if len(s.data) == 0 {
s.data = make([]byte, 0, len(data))
}
// Pack data starts with 12-byte header: "PACK" version[4] nobj[4].
if len(data) < 12+20 {
return fmt.Errorf("malformed git pack: too short")
}
hdr := data[:12]
vers := binary.BigEndian.Uint32(hdr[4:8])
nobj := binary.BigEndian.Uint32(hdr[8:12])
if string(hdr[:4]) != "PACK" || vers != 2 && vers != 3 || len(data) < 12+20 || int64(nobj) >= int64(len(data)) {
return fmt.Errorf("malformed git pack")
}
if vers == 3 {
return fmt.Errorf("cannot read git pack v3")
}
// Pack data ends with SHA1 of the entire pack.
sum := sha1.Sum(data[:len(data)-20])
if !bytes.Equal(sum[:], data[len(data)-20:]) {
return fmt.Errorf("malformed git pack: bad checksum")
}
// Object data is everything between hdr and ending SHA1.
// Unpack every object into the store.
objs := data[12 : len(data)-20]
off := 0
for i := 0; i < int(nobj); i++ {
_, _, _, encSize, err := unpackObject(s, objs, off)
if err != nil {
return fmt.Errorf("unpack: malformed git pack: %v", err)
}
off += encSize
}
if off != len(objs) {
return fmt.Errorf("malformed git pack: junk after objects")
}
return nil
}
// unpackObject unpacks the object at objs[off:] and writes it to the store s.
// It returns the type, hash, and content of the object, as well as the encoded size,
// meaning the number of bytes at the start of objs[off:] that this record occupies.
func unpackObject(s *store, objs []byte, off int) (typ objType, h Hash, content []byte, encSize int, err error) {
fail := func(err error) (objType, Hash, []byte, int, error) {
return 0, Hash{}, nil, 0, err
}
if off < 0 || off >= len(objs) {
return fail(fmt.Errorf("invalid object offset"))
}
// Object starts with varint-encoded type and length n.
// (The length n is the length of the compressed data that follows,
// not the length of the actual object.)
u, size := binary.Uvarint(objs[off:])
if size <= 0 {
return fail(fmt.Errorf("invalid object: bad varint header"))
}
typ = objType((u >> 4) & 7)
n := int(u&15 | u>>7<<4)
// Git often stores objects that differ very little (different revs of a file).
// It can save space by encoding one as "start with this other object and apply these diffs".
// There are two ways to specify "this other object": an object ref (20-byte SHA1)
// or as a relative offset to an earlier position in the objs slice.
// For either of these, we need to fetch the other object's type and data (deltaTyp and deltaBase).
// The Git docs call this the "deltified representation".
var deltaTyp objType
var deltaBase []byte
switch typ {
case objRefDelta:
if len(objs)-(off+size) < 20 {
return fail(fmt.Errorf("invalid object: bad delta ref"))
}
// Base block identified by SHA1 of an already unpacked hash.
var h Hash
copy(h[:], objs[off+size:])
size += 20
deltaTyp, deltaBase = s.object(h)
if deltaTyp == 0 {
return fail(fmt.Errorf("invalid object: unknown delta ref %v", h))
}
case objOfsDelta:
i := off + size
if len(objs)-i < 20 {
return fail(fmt.Errorf("invalid object: too short"))
}
// Base block identified by relative offset to earlier position in objs,
// using a varint-like but not-quite-varint encoding.
// Look for "offset encoding:" in https://git-scm.com/docs/pack-format.
d := int64(objs[i] & 0x7f)
for objs[i]&0x80 != 0 {
i++
if i-(off+size) > 10 {
return fail(fmt.Errorf("invalid object: malformed delta offset"))
}
d = d<<7 | int64(objs[i]&0x7f)
d += 1 << 7
}
i++
size = i - off
// Re-unpack the object at the earlier offset to find its type and content.
if d == 0 || d > int64(off) {
return fail(fmt.Errorf("invalid object: bad delta offset"))
}
var err error
deltaTyp, _, deltaBase, _, err = unpackObject(s, objs, off-int(d))
if err != nil {
return fail(fmt.Errorf("invalid object: bad delta offset"))
}
}
// The main encoded data is a zlib-compressed stream.
br := bytes.NewReader(objs[off+size:])
zr, err := zlib.NewReader(br)
if err != nil {
return fail(fmt.Errorf("invalid object deflate: %v", err))
}
data, err := io.ReadAll(zr)
if err != nil {
return fail(fmt.Errorf("invalid object: bad deflate: %v", err))
}
if len(data) != n {
return fail(fmt.Errorf("invalid object: deflate size %d != %d", len(data), n))
}
encSize = len(objs[off:]) - br.Len()
// If we fetched a base object above, the stream is an encoded delta.
// Otherwise it is the raw data.
switch typ {
default:
return fail(fmt.Errorf("invalid object: unknown object type"))
case objCommit, objTree, objBlob, objTag:
// ok
case objRefDelta, objOfsDelta:
// Actual object type is the type of the base object.
typ = deltaTyp
// Delta encoding starts with size of base object and size of new object.
baseSize, s := binary.Uvarint(data)
data = data[s:]
if baseSize != uint64(len(deltaBase)) {
return fail(fmt.Errorf("invalid object: mismatched delta src size"))
}
targSize, s := binary.Uvarint(data)
data = data[s:]
// Apply delta to base object, producing new object.
targ := make([]byte, targSize)
if err := applyDelta(targ, deltaBase, data); err != nil {
return fail(fmt.Errorf("invalid object: %v", err))
}
data = targ
}
h, data = s.add(typ, data)
return typ, h, data, encSize, nil
}
// applyDelta applies the delta encoding to src, producing dst,
// which has already been allocated to the expected final size.
// See https://git-scm.com/docs/pack-format#_deltified_representation for docs.
func applyDelta(dst, src, delta []byte) error {
for len(delta) > 0 {
// Command byte says what comes next.
cmd := delta[0]
delta = delta[1:]
switch {
case cmd == 0:
// cmd == 0 is reserved.
return fmt.Errorf("invalid delta cmd")
case cmd&0x80 != 0:
// Copy from base object, 4-byte offset, 3-byte size.
// But any zero byte in the offset or size can be omitted.
// The bottom 7 bits of cmd say which offset/size bytes are present.
var off, size int64
for i := uint(0); i < 4; i++ {
if cmd&(1<<i) != 0 {
off |= int64(delta[0]) << (8 * i)
delta = delta[1:]
}
}
for i := uint(0); i < 3; i++ {
if cmd&(0x10<<i) != 0 {
size |= int64(delta[0]) << (8 * i)
delta = delta[1:]
}
}
// Size 0 means size 0x10000 for some reason. (!)
if size == 0 {
size = 0x10000
}
copy(dst[:size], src[off:off+size])
dst = dst[size:]
default:
// Up to 0x7F bytes of literal data, length in bottom 7 bits of cmd.
n := int(cmd)
copy(dst[:n], delta[:n])
dst = dst[n:]
delta = delta[n:]
}
}
if len(dst) != 0 {
return fmt.Errorf("delta encoding too short")
}
return nil
}