-
Notifications
You must be signed in to change notification settings - Fork 53
/
patcher.go
47 lines (38 loc) · 1.1 KB
/
patcher.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
package patch
import (
"bytes"
"io"
"github.com/gabstv/go-bsdiff/pkg/bsdiff"
"github.com/gabstv/go-bsdiff/pkg/bspatch"
)
type BinaryPatcher interface {
GeneratePatch(old io.Reader, new io.Reader, patchOut io.Writer) error
ApplyPatch(old io.Reader, patch io.Reader, newOut io.Writer) error
CheckFormat(reader io.ReaderAt) bool
}
type BsdiffPatcher struct{}
func (BsdiffPatcher) GeneratePatch(old io.Reader, new io.Reader, patchOut io.Writer) (err error) {
return bsdiff.Reader(old, new, patchOut)
}
func (BsdiffPatcher) ApplyPatch(old io.Reader, patch io.Reader, newOut io.Writer) (err error) {
return bspatch.Reader(old, newOut, patch)
}
func (BsdiffPatcher) CheckFormat(reader io.ReaderAt) bool {
header := []byte("BSDIFF40")
buf := make([]byte, len(header))
if _, err := reader.ReadAt(buf, 0); err == nil {
return bytes.Equal(buf, header)
}
return false
}
var allPatchEngines = []BinaryPatcher{
BsdiffPatcher{},
}
func NewPatcherFromFormat(reader io.ReaderAt) (BinaryPatcher, bool) {
for _, p := range allPatchEngines {
if p.CheckFormat(reader) {
return p, true
}
}
return nil, false
}