forked from dolthub/vitess
/
zstream.go
163 lines (134 loc) · 4.23 KB
/
zstream.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
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cgzip
// See http://www.zlib.net/zlib_how.html for more information on this
/*
#cgo CFLAGS: -Werror=implicit
#cgo pkg-config: zlib
#include "zlib.h"
// inflateInit2 is a macro, so using a wrapper function
int zstream_inflate_init(char *strm) {
((z_stream*)strm)->zalloc = Z_NULL;
((z_stream*)strm)->zfree = Z_NULL;
((z_stream*)strm)->opaque = Z_NULL;
((z_stream*)strm)->avail_in = 0;
((z_stream*)strm)->next_in = Z_NULL;
return inflateInit2((z_stream*)strm,
16+15); // 16 makes it understand only gzip files
}
// deflateInit2 is a macro, so using a wrapper function
// using deflateInit2 instead of deflateInit to be able to specify gzip format
int zstream_deflate_init(char *strm, int level) {
((z_stream*)strm)->zalloc = Z_NULL;
((z_stream*)strm)->zfree = Z_NULL;
((z_stream*)strm)->opaque = Z_NULL;
return deflateInit2((z_stream*)strm, level, Z_DEFLATED,
16+15, // 16 makes it a gzip file, 15 is default
8, Z_DEFAULT_STRATEGY); // default values
}
unsigned int zstream_avail_in(char *strm) {
return ((z_stream*)strm)->avail_in;
}
unsigned int zstream_avail_out(char *strm) {
return ((z_stream*)strm)->avail_out;
}
char* zstream_msg(char *strm) {
return ((z_stream*)strm)->msg;
}
void zstream_set_in_buf(char *strm, void *buf, unsigned int len) {
((z_stream*)strm)->next_in = (Bytef*)buf;
((z_stream*)strm)->avail_in = len;
}
void zstream_set_out_buf(char *strm, void *buf, unsigned int len) {
((z_stream*)strm)->next_out = (Bytef*)buf;
((z_stream*)strm)->avail_out = len;
}
int zstream_inflate(char *strm, int flag) {
return inflate((z_stream*)strm, flag);
}
int zstream_deflate(char *strm, int flag) {
return deflate((z_stream*)strm, flag);
}
void zstream_inflate_end(char *strm) {
inflateEnd((z_stream*)strm);
}
void zstream_deflate_end(char *strm) {
deflateEnd((z_stream*)strm);
}
*/
import "C"
import (
"fmt"
"unsafe"
)
const (
zNoFlush = C.Z_NO_FLUSH
)
// z_stream is a buffer that's big enough to fit a C.z_stream.
// This lets us allocate a C.z_stream within Go, while keeping the contents
// opaque to the Go GC. Otherwise, the GC would look inside and complain that
// the pointers are invalid, since they point to objects allocated by C code.
type zstream [unsafe.Sizeof(C.z_stream{})]C.char
func (strm *zstream) inflateInit() error {
result := C.zstream_inflate_init(&strm[0])
if result != Z_OK {
return fmt.Errorf("cgzip: failed to initialize inflate (%v): %v", result, strm.msg())
}
return nil
}
func (strm *zstream) deflateInit(level int) error {
result := C.zstream_deflate_init(&strm[0], C.int(level))
if result != Z_OK {
return fmt.Errorf("cgzip: failed to initialize deflate (%v): %v", result, strm.msg())
}
return nil
}
func (strm *zstream) inflateEnd() {
C.zstream_inflate_end(&strm[0])
}
func (strm *zstream) deflateEnd() {
C.zstream_deflate_end(&strm[0])
}
func (strm *zstream) availIn() int {
return int(C.zstream_avail_in(&strm[0]))
}
func (strm *zstream) availOut() int {
return int(C.zstream_avail_out(&strm[0]))
}
func (strm *zstream) msg() string {
return C.GoString(C.zstream_msg(&strm[0]))
}
func (strm *zstream) setInBuf(buf []byte, size int) {
if buf == nil {
C.zstream_set_in_buf(&strm[0], nil, C.uint(size))
} else {
C.zstream_set_in_buf(&strm[0], unsafe.Pointer(&buf[0]), C.uint(size))
}
}
func (strm *zstream) setOutBuf(buf []byte, size int) {
if buf == nil {
C.zstream_set_out_buf(&strm[0], nil, C.uint(size))
} else {
C.zstream_set_out_buf(&strm[0], unsafe.Pointer(&buf[0]), C.uint(size))
}
}
func (strm *zstream) inflate(flag int) (int, error) {
ret := C.zstream_inflate(&strm[0], C.int(flag))
switch ret {
case Z_NEED_DICT:
ret = Z_DATA_ERROR
fallthrough
case Z_DATA_ERROR, Z_MEM_ERROR:
return int(ret), fmt.Errorf("cgzip: failed to inflate (%v): %v", ret, strm.msg())
}
return int(ret), nil
}
func (strm *zstream) deflate(flag int) {
ret := C.zstream_deflate(&strm[0], C.int(flag))
if ret == Z_STREAM_ERROR {
// all the other error cases are normal,
// and this should never happen
panic(fmt.Errorf("cgzip: Unexpected error (1)"))
}
}