|
@@ -0,0 +1,134 @@
|
|
|
+package czlib
|
|
|
+
|
|
|
+// #cgo CFLAGS: -Werror=implicit
|
|
|
+// #cgo pkg-config: zlib
|
|
|
+// #include "zstream.h"
|
|
|
+import "C"
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "unsafe"
|
|
|
+)
|
|
|
+
|
|
|
+type zstream C.z_stream
|
|
|
+
|
|
|
+const (
|
|
|
+ // Allowed flush values
|
|
|
+ Z_NO_FLUSH = C.Z_NO_FLUSH
|
|
|
+ Z_PARTIAL_FLUSH = C.Z_PARTIAL_FLUSH
|
|
|
+ Z_SYNC_FLUSH = C.Z_SYNC_FLUSH
|
|
|
+ Z_FULL_FLUSH = C.Z_FULL_FLUSH
|
|
|
+ Z_FINISH = C.Z_FINISH
|
|
|
+ Z_BLOCK = C.Z_BLOCK
|
|
|
+ Z_TREES = C.Z_TREES
|
|
|
+
|
|
|
+ // Return codes for the compression/decompression functions
|
|
|
+ Z_OK = C.Z_OK
|
|
|
+ Z_STREAM_END = C.Z_STREAM_END
|
|
|
+ Z_NEED_DICT = C.Z_NEED_DICT
|
|
|
+ Z_ERRNO = C.Z_ERRNO
|
|
|
+ Z_STREAM_ERROR = C.Z_STREAM_ERROR
|
|
|
+ Z_DATA_ERROR = C.Z_DATA_ERROR
|
|
|
+ Z_MEM_ERROR = C.Z_MEM_ERROR
|
|
|
+ Z_BUF_ERROR = C.Z_BUF_ERROR
|
|
|
+ Z_VERSION_ERROR = C.Z_VERSION_ERROR
|
|
|
+
|
|
|
+ // Compression levels
|
|
|
+ Z_NO_COMPRESSION = C.Z_NO_COMPRESSION
|
|
|
+ Z_BEST_SPEED = C.Z_BEST_SPEED
|
|
|
+ Z_BEST_COMPRESSION = C.Z_BEST_COMPRESSION
|
|
|
+ Z_DEFAULT_COMPRESSION = C.Z_DEFAULT_COMPRESSION
|
|
|
+
|
|
|
+ // Compression strategy
|
|
|
+ Z_FILTERED = C.Z_FILTERED
|
|
|
+ Z_HUFFMAN_ONLY = C.Z_HUFFMAN_ONLY
|
|
|
+ Z_RLE = C.Z_RLE
|
|
|
+ Z_FIXED = C.Z_FIXED
|
|
|
+ Z_DEFAULT_STRATEGY = C.Z_DEFAULT_STRATEGY
|
|
|
+
|
|
|
+ // Deflate compression method
|
|
|
+ Z_DEFLATED = C.Z_DEFLATED
|
|
|
+
|
|
|
+ // Various zlib constants
|
|
|
+ MAX_WBITS = C.MAX_WBITS
|
|
|
+ MAX_MEM_LEVEL = C.MAX_MEM_LEVEL
|
|
|
+)
|
|
|
+
|
|
|
+func (strm *zstream) deflateInit(level int, method int, windowBits int, memLevel int, strategy int) error {
|
|
|
+ result := C.zstream_deflate_init((*C.z_stream)(strm), C.int(level), C.int(method), C.int(windowBits), C.int(memLevel), C.int(strategy))
|
|
|
+ if result != Z_OK {
|
|
|
+ return fmt.Errorf("czlib: deflate init failed (%v) :%v", result, strm.message())
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (strm *zstream) deflate(flag int) {
|
|
|
+ ret := C.zstream_deflate((*C.z_stream)(strm), C.int(flag))
|
|
|
+ if ret == Z_STREAM_ERROR {
|
|
|
+ panic(fmt.Errorf("czlib: unexpected error (1)"))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (strm *zstream) deflateEnd() {
|
|
|
+ C.zstream_deflate_end((*C.z_stream)(strm))
|
|
|
+}
|
|
|
+
|
|
|
+func (strm *zstream) setInBuf(buf []byte, size int) {
|
|
|
+ if buf == nil {
|
|
|
+ C.zstream_set_in_buf((*C.z_stream)(strm), nil, C.uint(size))
|
|
|
+ } else {
|
|
|
+ C.zstream_set_in_buf((*C.z_stream)(strm), unsafe.Pointer(&buf[0]), C.uint(size))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (strm *zstream) setOutBuf(buf []byte, size int) {
|
|
|
+ if buf == nil {
|
|
|
+ C.zstream_set_out_buf((*C.z_stream)(strm), nil, C.uint(size))
|
|
|
+ } else {
|
|
|
+ C.zstream_set_out_buf((*C.z_stream)(strm), unsafe.Pointer(&buf[0]), C.uint(size))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (strm *zstream) availIn() int {
|
|
|
+ return int(C.zstream_avail_in((*C.z_stream)(strm)))
|
|
|
+}
|
|
|
+
|
|
|
+func (strm *zstream) availOut() int {
|
|
|
+ return int(C.zstream_avail_out((*C.z_stream)(strm)))
|
|
|
+}
|
|
|
+
|
|
|
+func (strm *zstream) inflateInit(windowBits int) error {
|
|
|
+ result := C.zstream_inflate_init((*C.z_stream)(strm), C.int(windowBits))
|
|
|
+ if result != Z_OK {
|
|
|
+ return fmt.Errorf("czlib: inflate init failed (%v): %v", result, strm.message())
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (strm *zstream) inflate(flush int) (int, error) {
|
|
|
+ ret := C.zstream_inflate((*C.z_stream)(strm), C.int(flush))
|
|
|
+ switch ret {
|
|
|
+ case Z_NEED_DICT:
|
|
|
+ ret = Z_DATA_ERROR
|
|
|
+ fallthrough
|
|
|
+ case Z_DATA_ERROR, Z_MEM_ERROR:
|
|
|
+ return int(ret), fmt.Errorf("czlib: inflate failed (%v): %v", ret, strm.message())
|
|
|
+ }
|
|
|
+ return int(ret), nil
|
|
|
+}
|
|
|
+
|
|
|
+func (strm *zstream) inflateEnd() {
|
|
|
+ C.zstream_inflate_end((*C.z_stream)(strm))
|
|
|
+}
|
|
|
+
|
|
|
+func zstreamAlloc() *zstream {
|
|
|
+ return (*zstream)(C.zstream_alloc())
|
|
|
+}
|
|
|
+
|
|
|
+func zstreamFree(strm *zstream) {
|
|
|
+ C.zstream_free((*C.z_stream)(strm))
|
|
|
+}
|
|
|
+
|
|
|
+func (strm *zstream) message() string {
|
|
|
+ return C.GoString(strm.msg)
|
|
|
+}
|