Debian Codegraph @

pack/chain_delta.go

raw
1 package pack
2
3 import "fmt"
4
5 // ChainDelta represents a "delta" component of a delta-base chain.
6 type ChainDelta struct {
7 // Base is the base delta-base chain that this delta should be applied
8 // to. It can be a ChainBase in the simple case, or it can itself be a
9 // ChainDelta, which resolves against another ChainBase, when the
10 // delta-base chain is of length greater than 2.
11 base Chain
12 // delta is the set of copy/add instructions to apply on top of the
13 // base.
14 delta []byte
15 }
16
17 // Unpack applies the delta operation to the previous delta-base chain, "base".
18 //
19 // If any of the delta-base instructions were invalid, an error will be
20 // returned.
21 func (d *ChainDelta) Unpack() ([]byte, error) {
22 base, err := d.base.Unpack()
23 if err != nil {
24 return nil, err
25 }
26
27 return patch(base, d.delta)
28 }
29
30 // Type returns the type of the base of the delta-base chain.
31 func (d *ChainDelta) Type() PackedObjectType {
32 return d.base.Type()
33 }
34
35 // patch applies the delta instructions in "delta" to the base given as "base".
36 // It returns the result of applying those patch instructions to base, but does
37 // not modify base itself.
38 //
39 // If any of the delta instructions were malformed, or otherwise could not be
40 // applied to the given base, an error will returned, along with an empty set of
41 // data.
42 func patch(base, delta []byte) ([]byte, error) {
43 srcSize, pos := patchDeltaHeader(delta, 0)
44 if srcSize != int64(len(base)) {
45 // The header of the delta gives the size of the source contents
46 // that it is a patch over.
47 //
48 // If this does not match with the srcSize, return an error
49 // early so as to avoid a possible bounds error below.
50 return nil, fmt.Errorf("gitobj/pack: invalid delta data")
51 }
52
53 // The remainder of the delta header contains the destination size, and
54 // moves the "pos" offset to the correct position to begin the set of
55 // delta instructions.
56 destSize, pos := patchDeltaHeader(delta, pos)
57
58 dest := make([]byte, 0, destSize)
59
60 for pos < len(delta) {
61 c := int(delta[pos])
62 pos += 1
63
64 if c&0x80 != 0 {
65 // If the most significant bit (MSB, at position 0x80)
66 // is set, this is a copy instruction. Advance the
67 // position one byte backwards, and initialize variables
68 // for the copy offset and size instructions.
69 pos -= 1
70
71 var co, cs int
72
73 // The lower-half of "c" (0000 1111) defines a "bitmask"
74 // for the copy offset.
75 if c&0x1 != 0 {
76 pos += 1
77 co = int(delta[pos])
78 }
79 if c&0x2 != 0 {
80 pos += 1
81 co |= (int(delta[pos]) << 8)
82 }
83 if c&0x4 != 0 {
84 pos += 1
85 co |= (int(delta[pos]) << 16)
86 }
87 if c&0x8 != 0 {
88 pos += 1
89 co |= (int(delta[pos]) << 24)
90 }
91
92 // The upper-half of "c" (1111 0000) defines a "bitmask"
93 // for the size of the copy instruction.
94 if c&0x10 != 0 {
95 pos += 1
96 cs = int(delta[pos])
97 }
98 if c&0x20 != 0 {
99 pos += 1
100 cs |= (int(delta[pos]) << 8)
101 }
102 if c&0x40 != 0 {
103 pos += 1
104 cs |= (int(delta[pos]) << 16)
105 }
106
107 if cs == 0 {
108 // If the copy size is zero, we assume that it
109 // is the next whole number after the max uint32
110 // value.
111 cs = 0x10000
112 }
113 pos += 1
114
115 // Once we have the copy offset and length defined, copy
116 // that number of bytes from the base into the
117 // destination. Since we are copying from the base and
118 // not the delta, the position into the delta ("pos")
119 // need not be updated.
120 dest = append(dest, base[co:co+cs]...)
121 } else if c != 0 {
122 // If the most significant bit (MSB) is _not_ set, we
123 // instead process a copy instruction, where "c" is the
124 // number of successive bytes in the delta patch to add
125 // to the output.
126 //
127 // Copy the bytes and increment the read pointer
128 // forward.
129 dest = append(dest, delta[pos:int(pos)+c]...)
130
131 pos += int(c)
132 } else {
133 // Otherwise, "c" is 0, and is an invalid delta
134 // instruction.
135 //
136 // Return immediately.
137 return nil, fmt.Errorf(
138 "gitobj/pack: invalid delta data")
139 }
140 }
141
142 if destSize != int64(len(dest)) {
143 // If after patching the delta against the base, the destination
144 // size is different than the expected destination size, we have
145 // an invalid set of patch instructions.
146 //
147 // Return immediately.
148 return nil, fmt.Errorf("gitobj/pack: invalid delta data")
149 }
150 return dest, nil
151 }
152
153 // patchDeltaHeader examines the header within delta at the given offset, and
154 // returns the size encoded within it, as well as the ending offset where begins
155 // the next header, or the patch instructions.
156 func patchDeltaHeader(delta []byte, pos int) (size int64, end int) {
157 var shift uint
158 var c int64
159
160 for shift == 0 || c&0x80 != 0 {
161 if len(delta) <= pos {
162 panic("gitobj/pack: invalid delta header")
163 }
164
165 c = int64(delta[pos])
166
167 pos++
168 size |= (c & 0x7f) << shift
169 shift += 7
170 }
171
172 return size, pos
173 }