| 1 |
// +build cgo |
| 2 |
|
| 3 |
/* |
| 4 |
Copyright 2017 Google Inc. |
| 5 |
|
| 6 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
you may not use this file except in compliance with the License. |
| 8 |
You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package cgzip |
| 20 |
|
| 21 |
// See http://www.zlib.net/zlib_how.html for more information on this |
| 22 |
|
| 23 |
/* |
| 24 |
#cgo CFLAGS: -Werror=implicit |
| 25 |
#cgo pkg-config: zlib |
| 26 |
|
| 27 |
#include "zlib.h" |
| 28 |
|
| 29 |
// inflateInit2 is a macro, so using a wrapper function |
| 30 |
int zstream_inflate_init(char *strm) { |
| 31 |
((z_stream*)strm)->zalloc = Z_NULL; |
| 32 |
((z_stream*)strm)->zfree = Z_NULL; |
| 33 |
((z_stream*)strm)->opaque = Z_NULL; |
| 34 |
((z_stream*)strm)->avail_in = 0; |
| 35 |
((z_stream*)strm)->next_in = Z_NULL; |
| 36 |
return inflateInit2((z_stream*)strm, |
| 37 |
16+15); // 16 makes it understand only gzip files |
| 38 |
} |
| 39 |
|
| 40 |
// deflateInit2 is a macro, so using a wrapper function |
| 41 |
// using deflateInit2 instead of deflateInit to be able to specify gzip format |
| 42 |
int zstream_deflate_init(char *strm, int level) { |
| 43 |
((z_stream*)strm)->zalloc = Z_NULL; |
| 44 |
((z_stream*)strm)->zfree = Z_NULL; |
| 45 |
((z_stream*)strm)->opaque = Z_NULL; |
| 46 |
return deflateInit2((z_stream*)strm, level, Z_DEFLATED, |
| 47 |
16+15, // 16 makes it a gzip file, 15 is default |
| 48 |
8, Z_DEFAULT_STRATEGY); // default values |
| 49 |
} |
| 50 |
|
| 51 |
unsigned int zstream_avail_in(char *strm) { |
| 52 |
return ((z_stream*)strm)->avail_in; |
| 53 |
} |
| 54 |
|
| 55 |
unsigned int zstream_avail_out(char *strm) { |
| 56 |
return ((z_stream*)strm)->avail_out; |
| 57 |
} |
| 58 |
|
| 59 |
char* zstream_msg(char *strm) { |
| 60 |
return ((z_stream*)strm)->msg; |
| 61 |
} |
| 62 |
|
| 63 |
void zstream_set_in_buf(char *strm, void *buf, unsigned int len) { |
| 64 |
((z_stream*)strm)->next_in = (Bytef*)buf; |
| 65 |
((z_stream*)strm)->avail_in = len; |
| 66 |
} |
| 67 |
|
| 68 |
void zstream_set_out_buf(char *strm, void *buf, unsigned int len) { |
| 69 |
((z_stream*)strm)->next_out = (Bytef*)buf; |
| 70 |
((z_stream*)strm)->avail_out = len; |
| 71 |
} |
| 72 |
|
| 73 |
int zstream_inflate(char *strm, int flag) { |
| 74 |
return inflate((z_stream*)strm, flag); |
| 75 |
} |
| 76 |
|
| 77 |
int zstream_deflate(char *strm, int flag) { |
| 78 |
return deflate((z_stream*)strm, flag); |
| 79 |
} |
| 80 |
|
| 81 |
void zstream_inflate_end(char *strm) { |
| 82 |
inflateEnd((z_stream*)strm); |
| 83 |
} |
| 84 |
|
| 85 |
void zstream_deflate_end(char *strm) { |
| 86 |
deflateEnd((z_stream*)strm); |
| 87 |
} |
| 88 |
*/ |
| 89 |
import "C" |
| 90 |
|
| 91 |
import ( |
| 92 |
"fmt" |
| 93 |
"unsafe" |
| 94 |
) |
| 95 |
|
| 96 |
const ( |
| 97 |
zNoFlush = C.Z_NO_FLUSH |
| 98 |
) |
| 99 |
|
| 100 |
// z_stream is a buffer that's big enough to fit a C.z_stream. |
| 101 |
// This lets us allocate a C.z_stream within Go, while keeping the contents |
| 102 |
// opaque to the Go GC. Otherwise, the GC would look inside and complain that |
| 103 |
// the pointers are invalid, since they point to objects allocated by C code. |
| 104 |
type zstream [unsafe.Sizeof(C.z_stream{})]C.char |
| 105 |
|
| 106 |
func (strm *zstream) inflateInit() error { |
| 107 |
result := C.zstream_inflate_init(&strm[0]) |
| 108 |
if result != Z_OK { |
| 109 |
return fmt.Errorf("cgzip: failed to initialize inflate (%v): %v", result, strm.msg()) |
| 110 |
} |
| 111 |
return nil |
| 112 |
} |
| 113 |
|
| 114 |
func (strm *zstream) deflateInit(level int) error { |
| 115 |
result := C.zstream_deflate_init(&strm[0], C.int(level)) |
| 116 |
if result != Z_OK { |
| 117 |
return fmt.Errorf("cgzip: failed to initialize deflate (%v): %v", result, strm.msg()) |
| 118 |
} |
| 119 |
return nil |
| 120 |
} |
| 121 |
|
| 122 |
func (strm *zstream) inflateEnd() { |
| 123 |
C.zstream_inflate_end(&strm[0]) |
| 124 |
} |
| 125 |
|
| 126 |
func (strm *zstream) deflateEnd() { |
| 127 |
C.zstream_deflate_end(&strm[0]) |
| 128 |
} |
| 129 |
|
| 130 |
func (strm *zstream) availIn() int { |
| 131 |
return int(C.zstream_avail_in(&strm[0])) |
| 132 |
} |
| 133 |
|
| 134 |
func (strm *zstream) availOut() int { |
| 135 |
return int(C.zstream_avail_out(&strm[0])) |
| 136 |
} |
| 137 |
|
| 138 |
func (strm *zstream) msg() string { |
| 139 |
return C.GoString(C.zstream_msg(&strm[0])) |
| 140 |
} |
| 141 |
|
| 142 |
func (strm *zstream) setInBuf(buf []byte, size int) { |
| 143 |
if buf == nil { |
| 144 |
C.zstream_set_in_buf(&strm[0], nil, C.uint(size)) |
| 145 |
} else { |
| 146 |
C.zstream_set_in_buf(&strm[0], unsafe.Pointer(&buf[0]), C.uint(size)) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
func (strm *zstream) setOutBuf(buf []byte, size int) { |
| 151 |
if buf == nil { |
| 152 |
C.zstream_set_out_buf(&strm[0], nil, C.uint(size)) |
| 153 |
} else { |
| 154 |
C.zstream_set_out_buf(&strm[0], unsafe.Pointer(&buf[0]), C.uint(size)) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func (strm *zstream) inflate(flag int) (int, error) { |
| 159 |
ret := C.zstream_inflate(&strm[0], C.int(flag)) |
| 160 |
switch ret { |
| 161 |
case Z_NEED_DICT: |
| 162 |
ret = Z_DATA_ERROR |
| 163 |
fallthrough |
| 164 |
case Z_DATA_ERROR, Z_MEM_ERROR: |
| 165 |
return int(ret), fmt.Errorf("cgzip: failed to inflate (%v): %v", ret, strm.msg()) |
| 166 |
} |
| 167 |
return int(ret), nil |
| 168 |
} |
| 169 |
|
| 170 |
func (strm *zstream) deflate(flag int) { |
| 171 |
ret := C.zstream_deflate(&strm[0], C.int(flag)) |
| 172 |
if ret == Z_STREAM_ERROR { |
| 173 |
// all the other error cases are normal, |
| 174 |
// and this should never happen |
| 175 |
panic(fmt.Errorf("cgzip: Unexpected error (1)")) |
| 176 |
} |
| 177 |
} |