This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
isal_cgo.go
464 lines (381 loc) · 10.7 KB
/
isal_cgo.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package isal
//#cgo LDFLAGS: -ldl
//#include "igzip_lib.h"
//#include <isal_native.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include <stdint.h>
import "C"
import (
"errors"
"fmt"
"io"
"runtime"
"sync"
"unsafe"
)
var errReaderClosed = errors.New("Reader is closed")
var errCouldNotLoadLib = errors.New("could not load isal library")
const (
D_BUF_SIZE = 640 * 1024
C_BUF_SIZE = 128 * 1024
HAS_GZIP_HEADER = 1
DEFAULT_LEVEL = 0
)
// Variable to check if library is loaded
var LIB_LOADED = 0
// cPool is a pool of buffers for use in reader.compressionBuffer. Buffers are
// taken from the pool in NewReaderDict, returned in reader.Close(). Returns a
// pointer to a slice to avoid the extra allocation of returning the slice as a
// value.
var cPool = sync.Pool{
New: func() interface{} {
buff := make([]byte, D_BUF_SIZE)
return &buff
},
}
// dPool is a pool of buffers for use in reader.decompressionBuffer. Buffers are
// taken from the pool in NewReaderDict, returned in reader.Close(). Returns a
// pointer to a slice to avoid the extra allocation of returning the slice as a
// value.
var dPool = sync.Pool{
New: func() interface{} {
buff := make([]byte, 12*D_BUF_SIZE)
return &buff
},
}
func resize(in []byte, newSize int) []byte {
if in == nil {
return make([]byte, newSize)
}
if newSize <= cap(in) {
return in[:newSize]
}
toAdd := newSize - len(in)
return append(in, make([]byte, toAdd)...)
}
type zstream [unsafe.Sizeof(C.isal_zstream{})]C.char
type inf_state [unsafe.Sizeof(C.inflate_state{})]C.char
type isalgzheader [unsafe.Sizeof(C.isal_gzip_header{})]C.char
// Reader is a gzip/zlib/flate reader. It implements io.ReadCloser. Calling
// Close is optional, though strongly recommended. NewReader() also installs a
// GC finalizer that closes the Reader, in case the application forgets to call
// Close.
type Reader struct {
underlyingReader io.Reader
zs inf_state
inEOF bool // true if in reaches io.EOF
firstError error
gzHeader isalgzheader
hasGzHeader bool // true if gzHeader was successfully set.
compressionBuffer []byte
decompressionBuffer []byte
tempDecompressBuffer []byte
decompOff int
compressionLeft int
remaining int
previous int
err error
}
// NewReader creates a gzip/flate reader. There can be at most one options arg.
func NewReader(in io.Reader) (*Reader, error) {
var err error
var ready bool
// load the isal library if not loaded by Ready()
if LIB_LOADED == 0 {
ready = Ready()
if !ready {
return nil, errCouldNotLoadLib
}
}
compressionBufferP := cPool.Get().(*[]byte)
z := &Reader{
underlyingReader: in,
compressionBuffer: *compressionBufferP,
firstError: err,
compressionLeft: 0,
previous: 0,
remaining: 0,
}
if ec := C.ig_isal_inflate_init(&z.zs[0]); ec != 0 {
return nil, isalReturnCodeToError(ec)
}
return z, nil
}
// Writer is the gzip/flate writer. It implements io.WriterCloser.
type Writer struct {
out io.Writer
zs zstream // underlying zlib implementation.
gzHeader isalgzheader
outBuf []byte
level int
err error
}
//NewWriter returns a new Writer.
// Writes to the returned writer are compressed and written to w.
//
// It is the caller's responsibility to call Close on the Writer when done.
// Writes may be buffered and not flushed until Close.
//
// Callers that wish to set the fields in Writer.Header must do so before
// the first call to Write, Flush, or Close.
func NewWriter(w io.Writer) (*Writer, error) {
z, err := NewWriterLevel(w, DEFAULT_LEVEL)
return z, err
}
// NewWriterLevel is like NewWriter but specifies the compression level instead
// of assuming DefaultCompression.
//
// The compression level can be DefaultCompression, NoCompression, HuffmanOnly
// or any integer value between BestSpeed and BestCompression inclusive.
// The error returned will be nil if the level is valid.
func NewWriterLevel(w io.Writer, level int) (*Writer, error) {
var ready bool
if LIB_LOADED == 0 {
ready = Ready()
if !ready {
return nil, errCouldNotLoadLib
}
}
z := &Writer{
out: w,
outBuf: make([]byte, C_BUF_SIZE),
level: level,
}
if level < 0 || level > 3 {
return z, errors.New("isal.gzip: invalid compression level")
}
ec := C.ig_isal_deflate_init(&z.zs[0], C.int(level))
if HAS_GZIP_HEADER == 1 {
C.ig_isal_gzip_header_init(&z.gzHeader[0])
}
if ec != 0 {
return nil, isalReturnCodeToError(ec)
}
return z, nil
}
//Adds a Ready() API that will check if the ISAL library is loadable and will load it
//If NewWriter/NewReader or NewWriterLevel is called before Ready, initialize the dynamic loads using Ready()
func Ready() bool {
if LIB_LOADED == 1 {
return true
}
// load the isal library and the symbols
ret := C.isal_dload_functions()
if ret != 0 {
fmt.Printf("isal: library could not be dynamically loaded %d", 1)
return false
} else {
LIB_LOADED = 1
}
return true
}
// Write implements io.Writer.
func (z *Writer) Write(in []byte) (int, error) {
if len(in) == 0 {
return 0, nil
}
var (
outLen = C.int(len(z.outBuf))
inConsumed C.int
length int
)
if len(z.outBuf) > len(in) {
ret := C.ig_isal_deflate_stateless(&z.zs[0], (*C.uint8_t)(unsafe.Pointer(&in[0])), C.int(len(in)), (*C.uint8_t)(unsafe.Pointer(&z.outBuf[0])), &outLen, &inConsumed, HAS_GZIP_HEADER, &z.gzHeader[0])
if ret != 0 {
return 0, isalReturnCodeToError(ret)
}
nOut := len(z.outBuf) - int(outLen)
if err := z.flush(z.outBuf[:nOut]); err != nil {
return 0, err
}
return len(in), nil
} else {
ec := C.ig_isal_deflate_init(&z.zs[0], C.int(z.level))
if ec != 0 {
return 0, isalReturnCodeToError(ec)
}
if HAS_GZIP_HEADER == 1 {
C.ig_isal_gzip_header_init(&z.gzHeader[0])
}
outLen = C.int(len(z.outBuf))
infile_size := C.int(len(in))
end_of_stream := C.int(0)
state := C.int(0)
for {
avail_in := C.int(C_BUF_SIZE)
if infile_size < avail_in {
end_of_stream = 1
avail_in = infile_size
}
for {
avail_out := C.int(C_BUF_SIZE)
C.ig_isal_deflate(&z.zs[0], (*C.uint8_t)(unsafe.Pointer(&in[0])), (*C.uint8_t)(unsafe.Pointer(&z.outBuf[0])), &avail_out, &end_of_stream, &state, &avail_in, HAS_GZIP_HEADER, &z.gzHeader[0])
nOut := C_BUF_SIZE - avail_out
if nOut != 0 {
length = length + int(nOut)
if err := z.flush(z.outBuf[:nOut]); err != nil {
return 0, err
}
}
if avail_out != 0 {
break
}
}
infile_size = infile_size - C_BUF_SIZE
if state != 0 {
break
}
}
return len(in), nil
}
}
// Read implements io.Reader, reading uncompressed bytes from its underlying Reader.
func (z *Reader) Read(p []byte) (n int, err error) {
if z.err != nil {
return 0, z.err
}
if z.remaining > 0 {
n = copy(p, z.tempDecompressBuffer[z.decompOff:z.decompOff+z.remaining])
z.decompOff += n
z.remaining -= n
if z.remaining <= 0 {
return n, io.EOF
} else {
return n, nil
}
}
avail_out := C.int(0)
if len(p) < 1024 {
if len(z.compressionBuffer) == 0 {
compressionBufferP := cPool.Get().(*[]byte)
z.compressionBuffer = *compressionBufferP
}
decompressionBufferP := dPool.Get().(*[]byte)
tempDecompressBufferP := dPool.Get().(*[]byte)
z.decompressionBuffer = *decompressionBufferP
z.tempDecompressBuffer = *tempDecompressBufferP
avail_out = C.int(len(z.decompressionBuffer))
} else {
runtime.GC()
avail_out = C.int(len(p))
}
inbytes, err := z.underlyingReader.Read(z.compressionBuffer)
state := C.int(0)
totalOut := C.int(0)
avail_in := C.int(inbytes)
ret := C.int(0)
prevOut := 0
for {
avail_in = C.int(inbytes)
if len(p) > 1024 {
ret = C.ig_isal_inflate(&z.zs[0], (*C.uint8_t)(unsafe.Pointer(&z.compressionBuffer[0])), C.int(inbytes),
(*C.uint8_t)(unsafe.Pointer(&p[0])), &avail_out, &totalOut, &state, &avail_in, HAS_GZIP_HEADER, &z.gzHeader[0])
} else {
ret = C.ig_isal_inflate_buffered(&z.zs[0], (*C.uint8_t)(unsafe.Pointer(&z.compressionBuffer[0])), C.int(inbytes),
(*C.uint8_t)(unsafe.Pointer(&z.decompressionBuffer[0])), &avail_out, &totalOut, &state, &avail_in, HAS_GZIP_HEADER, &z.gzHeader[0])
}
if ret != 0 {
z.err = isalReturnCodeToError(ret)
return 0, z.err
}
if len(p) < 1024 {
n = copy(z.tempDecompressBuffer[prevOut:], z.decompressionBuffer[:int(totalOut)-prevOut])
prevOut = prevOut + n
avail_out = C.int(len(z.decompressionBuffer))
}
if err == io.EOF {
break
}
if len(p) < 1024 {
if len(z.tempDecompressBuffer) < (len(z.decompressionBuffer) + int(totalOut)) {
z.tempDecompressBuffer = append(z.tempDecompressBuffer, make([]byte, 12*D_BUF_SIZE)...)
}
}
inbytes, err = z.underlyingReader.Read(z.compressionBuffer)
if inbytes == 0 {
break
}
}
if len(p) < 1024 {
z.decompOff = copy(p, z.tempDecompressBuffer[:int(totalOut)])
if z.decompOff < int(totalOut) {
z.remaining = int(totalOut) - z.decompOff
}
if z.decompOff < int(totalOut) {
return z.decompOff, nil
} else {
return z.decompOff, io.EOF
}
} else {
return int(totalOut), err
}
}
// Close implements io.Closer
func (z *Reader) Close() error {
if z.firstError != nil {
return z.firstError
}
cb := z.compressionBuffer
db := z.decompressionBuffer
// Ensure that we won't resuse buffer
z.firstError = errReaderClosed
z.compressionBuffer = nil
z.decompressionBuffer = nil
cPool.Put(&cb)
dPool.Put(&db)
return nil
}
// Flush writes the data to the output.
func (z *Writer) flush(data []byte) error {
n, err := z.out.Write(data)
if err != nil {
return err
}
if n < len(data) { // shouldn't happen in practice
return fmt.Errorf("zlib: n=%d, outLen=%d", n, len(data))
}
return nil
}
// Close implements io.Closer
func (z *Writer) Close() error {
if z.err != nil {
return z.err
}
return nil
}
// Reset discards the Writer z's state and makes it equivalent to the
// result of its original state from NewWriter or NewWriterLevel, but
// writing to w instead. This permits reusing a Writer rather than
// allocating a new one.
func (z *Writer) Reset(w io.Writer) error {
z = &Writer{
out: w,
level: z.level,
}
C.ig_isal_deflate_reset(&z.zs[0])
return z.err
}
func (z *Reader) Reset(r io.Reader) error {
z.underlyingReader = r
C.ig_isal_inflate_init(&z.zs[0])
return z.err
}
func isalReturnCodeToError(r C.int) error {
if r == 0 {
return nil
}
if r == C.ISAL_END_INPUT {
return fmt.Errorf("isal: End of input reached%d", r)
}
if r == C.ISAL_INVALID_LEVEL {
return fmt.Errorf("isal: invalid level passed %d", r)
}
if r == C.ISAL_INVALID_LEVEL_BUF {
return fmt.Errorf("isal: isal_invalid_level_buf %d", r)
}
if r == C.STATELESS_OVERFLOW {
return fmt.Errorf("isal: stateless overflow %d", r)
}
return fmt.Errorf("isal: unknown error %d", r)
}