forked from balacode/go-delta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delta_load.go
81 lines (77 loc) · 1.97 KB
/
delta_load.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
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
// -----------------------------------------------------------------------------
// github.com/balacode/go-delta go-delta/[delta_load.go]
// (c) [email protected] License: MIT
// -----------------------------------------------------------------------------
package delta
import (
"bytes"
"encoding/binary"
)
// Load fills a new Delta structure from a byte
// array previously returned by Delta.Bytes().
func Load(data []byte) (Delta, error) {
//
// uncompress the delta
if DebugInfo {
PL("Load: compressed delta length:", len(data))
}
data = uncompressBytes(data)
if DebugInfo {
PL("Load: uncompressed delta length:", len(data))
}
buf := bytes.NewBuffer(data)
readInt := func() int {
var i int32
err := binary.Read(buf, binary.BigEndian, &i)
if err != nil {
mod.Error("readInt() failed:", err)
return -1
}
return int(i)
}
readBytes := func() []byte {
var size int32
err := binary.Read(buf, binary.BigEndian, &size)
if err != nil {
mod.Error("readBytes() failed @1:", err)
}
ar := make([]byte, size)
var nread int
nread, err = buf.Read(ar)
if err != nil {
mod.Error("readBytes() failed @2:", err)
}
if nread != int(size) {
mod.Error("readBytes() failed @3: size:", size, "nread:", nread)
}
return ar
}
// read the header
ret := Delta{
sourceSize: readInt(),
sourceHash: readBytes(),
targetSize: readInt(),
targetHash: readBytes(),
newCount: readInt(),
oldCount: readInt(),
}
// read the parts
count := readInt()
if count < 1 {
return Delta{},
mod.Error("readBytes() failed @4: invalid number of parts:", count)
}
ret.parts = make([]deltaPart, count)
for i := range ret.parts {
pt := &ret.parts[i]
pt.sourceLoc = readInt()
if pt.sourceLoc == -1 {
pt.data = readBytes()
pt.size = len(pt.data)
continue
}
pt.size = readInt()
}
return ret, nil
} // Load
// end