forked from valyala/gozstd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
237 lines (201 loc) · 5.16 KB
/
reader.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
package gozstd
/*
#define ZSTD_STATIC_LINKING_ONLY
#include "zstd.h"
#include "zstd_errors.h"
#include <stdlib.h> // for malloc/free
*/
import "C"
import (
"fmt"
"io"
"runtime"
"unsafe"
)
var (
dstreamInBufSize = C.ZSTD_DStreamInSize()
dstreamOutBufSize = C.ZSTD_DStreamOutSize()
)
// Reader implements zstd reader.
type Reader struct {
r io.Reader
ds *C.ZSTD_DStream
dd *DDict
inBuf *C.ZSTD_inBuffer
outBuf *C.ZSTD_outBuffer
inBufGo cMemPtr
outBufGo cMemPtr
}
// NewReader returns new zstd reader reading compressed data from r.
//
// Call Release when the Reader is no longer needed.
func NewReader(r io.Reader) *Reader {
return NewReaderDict(r, nil)
}
// NewReaderDict returns new zstd reader reading compressed data from r
// using the given DDict.
//
// Call Release when the Reader is no longer needed.
func NewReaderDict(r io.Reader, dd *DDict) *Reader {
ds := C.ZSTD_createDStream()
initDStream(ds, dd)
inBuf := (*C.ZSTD_inBuffer)(C.malloc(C.sizeof_ZSTD_inBuffer))
inBuf.src = C.malloc(dstreamInBufSize)
inBuf.size = 0
inBuf.pos = 0
outBuf := (*C.ZSTD_outBuffer)(C.malloc(C.sizeof_ZSTD_outBuffer))
outBuf.dst = C.malloc(dstreamOutBufSize)
outBuf.size = 0
outBuf.pos = 0
zr := &Reader{
r: r,
ds: ds,
dd: dd,
inBuf: inBuf,
outBuf: outBuf,
}
zr.inBufGo = cMemPtr(zr.inBuf.src)
zr.outBufGo = cMemPtr(zr.outBuf.dst)
runtime.SetFinalizer(zr, freeDStream)
return zr
}
// Reset resets zr to read from r using the given dictionary dd.
func (zr *Reader) Reset(r io.Reader, dd *DDict) {
zr.inBuf.size = 0
zr.inBuf.pos = 0
zr.outBuf.size = 0
zr.outBuf.pos = 0
zr.dd = dd
initDStream(zr.ds, zr.dd)
zr.r = r
}
func initDStream(ds *C.ZSTD_DStream, dd *DDict) {
var ddict *C.ZSTD_DDict
if dd != nil {
ddict = dd.p
}
result := C.ZSTD_initDStream_usingDDict(ds, ddict)
ensureNoError("ZSTD_initDStream_usingDDict", result)
}
func freeDStream(v interface{}) {
v.(*Reader).Release()
}
// Release releases all the resources occupied by zr.
//
// zr cannot be used after the release.
func (zr *Reader) Release() {
if zr.ds == nil {
return
}
result := C.ZSTD_freeDStream(zr.ds)
ensureNoError("ZSTD_freeDStream", result)
zr.ds = nil
C.free(zr.inBuf.src)
C.free(unsafe.Pointer(zr.inBuf))
zr.inBuf = nil
C.free(zr.outBuf.dst)
C.free(unsafe.Pointer(zr.outBuf))
zr.outBuf = nil
zr.r = nil
zr.dd = nil
}
// WriteTo writes all the data from zr to w.
//
// It returns the number of bytes written to w.
func (zr *Reader) WriteTo(w io.Writer) (int64, error) {
nn := int64(0)
for {
if zr.outBuf.pos == zr.outBuf.size {
if err := zr.fillOutBuf(); err != nil {
if err == io.EOF {
return nn, nil
}
return nn, err
}
}
n, err := w.Write(zr.outBufGo[zr.outBuf.pos:zr.outBuf.size])
zr.outBuf.pos += C.size_t(n)
nn += int64(n)
if err != nil {
return nn, err
}
}
}
// Read reads up to len(p) bytes from zr to p.
func (zr *Reader) Read(p []byte) (int, error) {
if len(p) == 0 {
return 0, nil
}
if zr.outBuf.pos == zr.outBuf.size {
if err := zr.fillOutBuf(); err != nil {
return 0, err
}
}
n := copy(p, zr.outBufGo[zr.outBuf.pos:zr.outBuf.size])
zr.outBuf.pos += C.size_t(n)
return n, nil
}
func (zr *Reader) fillOutBuf() error {
if zr.inBuf.pos == zr.inBuf.size && zr.outBuf.size < dstreamOutBufSize {
// inBuf is empty and the previously decompressed data size
// is smaller than the maximum possible zr.outBuf.size.
// This means that the internal buffer in zr.ds doesn't contain
// more data to decompress, so read new data into inBuf.
if err := zr.fillInBuf(); err != nil {
return err
}
}
tryDecompressAgain:
// Try decompressing inBuf into outBuf.
zr.outBuf.size = dstreamOutBufSize
zr.outBuf.pos = 0
prevInBufPos := zr.inBuf.pos
result := C.ZSTD_decompressStream(zr.ds, zr.outBuf, zr.inBuf)
zr.outBuf.size = zr.outBuf.pos
zr.outBuf.pos = 0
if C.ZSTD_getErrorCode(result) != 0 {
return fmt.Errorf("cannot decompress data: %s", errStr(result))
}
if zr.outBuf.size > 0 {
// Something has been decompressed to outBuf. Return it.
return nil
}
// Nothing has been decompressed from inBuf.
if zr.inBuf.pos != prevInBufPos && zr.inBuf.pos < zr.inBuf.size {
// Data has been consumed from inBuf, but decompressed
// into nothing. There is more data in inBuf, so try
// decompressing it again.
goto tryDecompressAgain
}
// Either nothing has been consumed from inBuf or it has been
// decompressed into nothing and inBuf became empty.
// Read more data into inBuf and try decompressing again.
if err := zr.fillInBuf(); err != nil {
return err
}
goto tryDecompressAgain
}
func (zr *Reader) fillInBuf() error {
// Copy the remaining data to the start of inBuf.
copy(zr.inBufGo[:dstreamInBufSize], zr.inBufGo[zr.inBuf.pos:zr.inBuf.size])
zr.inBuf.size -= zr.inBuf.pos
zr.inBuf.pos = 0
readAgain:
// Read more data into inBuf.
n, err := zr.r.Read(zr.inBufGo[zr.inBuf.size:dstreamInBufSize])
zr.inBuf.size += C.size_t(n)
if err == nil {
if n == 0 {
// Nothing has been read. Try reading data again.
goto readAgain
}
return nil
}
if err != io.EOF {
return fmt.Errorf("cannot read data from the underlying reader: %s", err)
}
if n == 0 {
return io.EOF
}
return nil
}