forked from rameshvarun/ups
/
apply.go
43 lines (34 loc) · 989 Bytes
/
apply.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
package operations
import (
"errors"
"hash/crc32"
"github.com/rameshvarun/ups/common"
)
// Apply applies the patch data to the given base.
func Apply(base []byte, patch *common.PatchData, skipCRC bool) ([]byte, error) {
if uint64(len(base)) != patch.InputFileSize {
return nil, errors.New("Base file did not have expected size.")
}
if !skipCRC && crc32.ChecksumIEEE(base) != patch.InputChecksum {
return nil, errors.New("Base file did not have expected checksum")
}
output := make([]byte, patch.OutputFileSize)
copy(output, base)
pointer := 0
for _, block := range patch.PatchBlocks {
pointer += int(block.RelativeOffset)
for _, b := range block.Data {
if pointer >= len(base) {
output[pointer] = b
} else {
output[pointer] = base[pointer] ^ b
}
pointer++
}
pointer++
}
if !skipCRC && crc32.ChecksumIEEE(output) != patch.OutputChecksum {
return nil, errors.New("Patch result did not have expected checksum")
}
return output, nil
}