MCPcopy Create free account
hub / github.com/antonmedv/gitmal / applyBinaryDeltaCopy

Function applyBinaryDeltaCopy

pkg/gitdiff/apply_binary.go:160–195  ·  view source on GitHub ↗

applyBinaryDeltaCopy applies a copy opcode in a delta-encoded binary fragment, returing the amount of data written and the unused part of the fragment. A copy operation takes the form: [1xxxxxxx][offset1][offset2][offset3][offset4][size1][size2][size3] where the lower seven bits of the opcode det

(w io.Writer, op byte, delta []byte, src io.ReaderAt)

Source from the content-addressed store, hash-verified

158// present, etc. If no offset or size bytes are present, offset is 0 and size
159// is 0x10000. See also pack-format.txt in the Git source.
160func applyBinaryDeltaCopy(w io.Writer, op byte, delta []byte, src io.ReaderAt) (n int64, rest []byte, err error) {
161 const defaultSize = 0x10000
162
163 unpack := func(start, bits uint) (v int64) {
164 for i := uint(0); i < bits; i++ {
165 mask := byte(1 << (i + start))
166 if op&mask > 0 {
167 if len(delta) == 0 {
168 err = errors.New("corrupt binary delta: incomplete copy")
169 return
170 }
171 v |= int64(delta[0]) << (8 * i)
172 delta = delta[1:]
173 }
174 }
175 return
176 }
177
178 offset := unpack(0, 4)
179 size := unpack(4, 3)
180 if err != nil {
181 return 0, delta, err
182 }
183 if size == 0 {
184 size = defaultSize
185 }
186
187 // TODO(bkeyes): consider pooling these buffers
188 b := make([]byte, size)
189 if _, err := src.ReadAt(b, offset); err != nil {
190 return 0, delta, err
191 }
192
193 _, err = w.Write(b)
194 return size, delta, err
195}
196
197func checkBinarySrcSize(r io.ReaderAt, size int64) error {
198 ok, err := isLen(r, size)

Callers 1

applyBinaryDeltaFragmentFunction · 0.85

Calls 2

NewMethod · 0.80
WriteMethod · 0.80

Tested by

no test coverage detected