aboutsummaryrefslogtreecommitdiff
path: root/internal/format/packfile/delta/apply.go
blob: 4210d1b31f0301414d41a8cf085c8711f6c86661 (about) (plain) (blame)
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
package delta

import (
	"fmt"

	"lindenii.org/go/lgo/intconv"
)

// MaxChainDepth is the maximum supported delta chain length.
// Resolvers reject chains deeper than this
// to bound recursion depth and reconstruction work.
const MaxChainDepth = 1 << 12

// Apply applies one inflated delta payload to base
// and returns the reconstructed result.
//
// delta must include the leading base/result size header.
func Apply(base, delta []byte) ([]byte, error) {
	baseSize, resultSize, pos, err := ParseHeaderSizes(delta)
	if err != nil {
		return nil, err
	}

	if baseSize != uint64(len(base)) {
		return nil, fmt.Errorf("%w: base size mismatch", ErrMalformedDelta)
	}

	outLen, err := intconv.Uint64ToInt(resultSize)
	if err != nil {
		return nil, fmt.Errorf("%w: result size overflows int", ErrMalformedDelta)
	}

	out := make([]byte, outLen)
	outPos := 0

	for pos < len(delta) {
		op := delta[pos]
		pos++

		switch {
		case op&0x80 != 0:
			outPos, err = applyCopy(out, outPos, base, delta, &pos, op)
		case op != 0:
			outPos, err = applyInsert(out, outPos, delta, &pos, int(op))
		default:
			err = fmt.Errorf("%w: invalid opcode 0", ErrMalformedDelta)
		}

		if err != nil {
			return nil, err
		}
	}

	if outPos != len(out) {
		return nil, fmt.Errorf("%w: result size mismatch", ErrMalformedDelta)
	}

	return out, nil
}

// applyCopy executes one copy instruction,
// copying a base range into out,
// and returns the new output position.
func applyCopy(out []byte, outPos int, base, delta []byte, pos *int, op byte) (int, error) {
	off, err := parseCopyOperand(delta, pos, op, 0, 4)
	if err != nil {
		return 0, err
	}

	n, err := parseCopyOperand(delta, pos, op, 4, 3)
	if err != nil {
		return 0, err
	}

	if n == 0 {
		n = 0x10000
	}

	if off+n > len(base) || outPos+n > len(out) {
		return 0, fmt.Errorf("%w: copy out of bounds", ErrMalformedDelta)
	}

	copy(out[outPos:outPos+n], base[off:off+n])

	return outPos + n, nil
}

// applyInsert executes one insert instruction,
// copying n literal delta bytes into out,
// and returns the new output position.
func applyInsert(out []byte, outPos int, delta []byte, pos *int, n int) (int, error) {
	if *pos+n > len(delta) || outPos+n > len(out) {
		return 0, fmt.Errorf("%w: insert out of bounds", ErrMalformedDelta)
	}

	copy(out[outPos:outPos+n], delta[*pos:*pos+n])
	*pos += n

	return outPos + n, nil
}

// parseCopyOperand assembles one little-endian copy instruction operand
// from the operand bytes selected by op's flag bits
// firstBit through firstBit+count-1.
func parseCopyOperand(delta []byte, pos *int, op byte, firstBit uint, count int) (int, error) {
	value := 0

	for i := range count {
		if op&(1<<(firstBit+uint(i))) == 0 {
			continue
		}

		if *pos >= len(delta) {
			return 0, fmt.Errorf("%w: truncated copy operand", ErrMalformedDelta)
		}

		value |= int(delta[*pos]) << (8 * i)
		*pos++
	}

	return value, nil
}