forked from nine-lives-later/go-xdelta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
238 lines (194 loc) · 5.94 KB
/
decoder.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package xdelta
import (
"context"
"fmt"
"io"
"runtime"
"unsafe"
"github.com/konsorten/go-xdelta/xdelta-lib"
)
type Decoder struct {
io.Closer
handle uintptr
inputFile io.Reader
sourceFile io.ReadSeeker
outputFile io.Writer
inputBuffer []byte
sourceBuffer []byte
outputBuffer []byte
Header chan<- []byte
}
type DecoderOptions struct {
BlockSizeKB int
FileID string
FromFile io.ReadSeeker
ToFile io.Writer
PatchFile io.Reader
}
func NewDecoder(options DecoderOptions) (*Decoder, error) {
// create the new decoder
var handle uintptr
err := lib.CallToError(lib.NewDecoder.Call(uintptr(unsafe.Pointer(&handle))))
if err != nil {
return nil, err
}
// initialize
if options.BlockSizeKB <= 0 {
options.BlockSizeKB = (8 * 1024) // 8 MB
}
var hasSource uintptr
if options.FromFile != nil {
hasSource = 1
}
err = lib.CallToError(lib.DecoderInit.Call(handle, uintptr(options.BlockSizeKB), lib.FromString(options.FileID), hasSource))
if err != nil {
lib.FreeDecoder.Call(uintptr(unsafe.Pointer(&handle)))
return nil, err
}
// setup decoder object
ret := &Decoder{
handle: handle,
inputFile: options.PatchFile,
sourceFile: options.FromFile,
outputFile: options.ToFile,
inputBuffer: make([]byte, options.BlockSizeKB*1024),
outputBuffer: make([]byte, options.BlockSizeKB*1024),
}
if options.FromFile != nil {
ret.sourceBuffer = make([]byte, options.BlockSizeKB*1024)
}
// ensure shutdown
runtime.SetFinalizer(ret, freeDecoder)
return ret, nil
}
func (enc *Decoder) GetStreamError() error {
var s uintptr
err := lib.CallToError(lib.DecoderGetStreamError.Call(enc.handle, uintptr(unsafe.Pointer(&s))))
if err != nil {
return err
}
if s == 0 {
return nil
}
return fmt.Errorf("%v", lib.ToString(s, true))
}
func (enc *Decoder) Process(ctx context.Context) error {
var isFinal uintptr
for {
// retrieve the current state
var state lib.XdeltaState
err := lib.CallToError(lib.DecoderProcess.Call(enc.handle, uintptr(unsafe.Pointer(&state))))
if err != nil {
return err
}
switch state {
case lib.XdeltaState_INPUT:
// done?
if isFinal != 0 {
return nil
}
// try read input data
n, err := enc.inputFile.Read(enc.inputBuffer)
if err != nil && err != io.EOF {
return fmt.Errorf("Failed to read from TO/input file: %v", err)
}
if n <= 0 { // no more data?
isFinal = 1
}
err = lib.CallToError(lib.DecoderProvideInputData.Call(enc.handle, uintptr(unsafe.Pointer(&enc.inputBuffer[0])), uintptr(n), isFinal))
if err != nil {
return fmt.Errorf("Failed to provide data from TO/input file: %v", err)
}
break
case lib.XdeltaState_OUTPUT:
var length int
err := lib.CallToError(lib.DecoderGetOutputRequest.Call(enc.handle, uintptr(unsafe.Pointer(&length))))
if err != nil {
return fmt.Errorf("Failed to request data for PATCH/output file: %v", err)
}
if length <= 0 { // nothing to write?
break
}
if length > len(enc.outputBuffer) {
return fmt.Errorf("Failed to consume data for PATCH/output file: output buffer overflow")
}
err = lib.CallToError(lib.DecoderCopyOutputData.Call(enc.handle, uintptr(unsafe.Pointer(&enc.outputBuffer[0]))))
if err != nil {
return fmt.Errorf("Failed to consume data for PATCH/output file: %v", err)
}
written, err := enc.outputFile.Write(enc.outputBuffer[:length])
if err != nil {
return fmt.Errorf("Failed to write data to PATCH/output file: %v", err)
}
if written < length {
return fmt.Errorf("Failed to write data to PATCH/output file: not enough data written (%v < %v)", written, length)
}
break
case lib.XdeltaState_GOTHEADER:
if enc.Header == nil { // nobody there to receive
break
}
var length int
err := lib.CallToError(lib.DecoderGetHeaderRequest.Call(enc.handle, uintptr(unsafe.Pointer(&length))))
if err != nil {
return fmt.Errorf("Failed to request header from PATCH/output file: %v", err)
}
if length <= 0 { // nothing to write?
break
}
headerData := make([]byte, length)
err = lib.CallToError(lib.DecoderCopyHeaderData.Call(enc.handle, uintptr(unsafe.Pointer(&headerData[0]))))
if err != nil {
return fmt.Errorf("Failed to consume header from PATCH/output file: %v", err)
}
enc.Header <- headerData
break
case lib.XdeltaState_GETSRCBLK:
if enc.sourceFile == nil || enc.sourceBuffer == nil {
return fmt.Errorf("Failed to request data for FROM/source file: not available")
}
var blockno, blocksize int
err := lib.CallToError(lib.DecoderGetSourceRequest.Call(enc.handle, uintptr(unsafe.Pointer(&blockno)), uintptr(unsafe.Pointer(&blocksize))))
if err != nil {
return fmt.Errorf("Failed to request data for FROM/source file: %v", err)
}
if blocksize > len(enc.sourceBuffer) {
return fmt.Errorf("Failed to request data for FROM/source file: source buffer overflow (%v > %v)", blocksize, len(enc.sourceBuffer))
}
_, err = enc.sourceFile.Seek(int64(blockno)*int64(blocksize), io.SeekStart)
if err != nil {
return fmt.Errorf("Failed to seek FROM/source file: %v", err)
}
n, err := enc.sourceFile.Read(enc.sourceBuffer)
if err != nil {
return fmt.Errorf("Failed to read from FROM/source file: %v", err)
}
err = lib.CallToError(lib.DecoderProvideSourceData.Call(enc.handle, uintptr(unsafe.Pointer(&enc.sourceBuffer[0])), uintptr(n)))
if err != nil {
return fmt.Errorf("Failed to provide data from FROM/source file: %v", err)
}
break
case lib.XdeltaState_WINSTART:
case lib.XdeltaState_WINFINISH:
break
default:
return fmt.Errorf("Unknown state: %v", state)
}
// check if cancelled
err = ctx.Err()
if err != nil {
return err
}
}
}
func (enc *Decoder) Close() error {
return freeDecoder(enc)
}
func freeDecoder(enc *Decoder) error {
// nothing to do?
if enc == nil || enc.handle == 0 {
return nil
}
// create the new decoder
return lib.CallToError(lib.FreeDecoder.Call(uintptr(unsafe.Pointer(&enc.handle))))
}