| 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 |
import ( |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
) |
| 25 |
|
| 26 |
const ( |
| 27 |
// Allowed flush values |
| 28 |
Z_NO_FLUSH = 0 |
| 29 |
Z_PARTIAL_FLUSH = 1 |
| 30 |
Z_SYNC_FLUSH = 2 |
| 31 |
Z_FULL_FLUSH = 3 |
| 32 |
Z_FINISH = 4 |
| 33 |
Z_BLOCK = 5 |
| 34 |
Z_TREES = 6 |
| 35 |
|
| 36 |
// Return codes |
| 37 |
Z_OK = 0 |
| 38 |
Z_STREAM_END = 1 |
| 39 |
Z_NEED_DICT = 2 |
| 40 |
Z_ERRNO = -1 |
| 41 |
Z_STREAM_ERROR = -2 |
| 42 |
Z_DATA_ERROR = -3 |
| 43 |
Z_MEM_ERROR = -4 |
| 44 |
Z_BUF_ERROR = -5 |
| 45 |
Z_VERSION_ERROR = -6 |
| 46 |
|
| 47 |
// compression levels |
| 48 |
Z_NO_COMPRESSION = 0 |
| 49 |
Z_BEST_SPEED = 1 |
| 50 |
Z_BEST_COMPRESSION = 9 |
| 51 |
Z_DEFAULT_COMPRESSION = -1 |
| 52 |
|
| 53 |
// our default buffer size |
| 54 |
// most go io functions use 32KB as buffer size, so 32KB |
| 55 |
// works well here for compressed data buffer |
| 56 |
DEFAULT_COMPRESSED_BUFFER_SIZE = 32 * 1024 |
| 57 |
) |
| 58 |
|
| 59 |
// err starts out as nil |
| 60 |
// we will call deflateEnd when we set err to a value: |
| 61 |
// - whatever error is returned by the underlying writer |
| 62 |
// - io.EOF if Close was called |
| 63 |
type Writer struct { |
| 64 |
w io.Writer |
| 65 |
out []byte |
| 66 |
strm zstream |
| 67 |
err error |
| 68 |
} |
| 69 |
|
| 70 |
func NewWriter(w io.Writer) *Writer { |
| 71 |
z, _ := NewWriterLevelBuffer(w, Z_DEFAULT_COMPRESSION, DEFAULT_COMPRESSED_BUFFER_SIZE) |
| 72 |
return z |
| 73 |
} |
| 74 |
|
| 75 |
func NewWriterLevel(w io.Writer, level int) (*Writer, error) { |
| 76 |
return NewWriterLevelBuffer(w, level, DEFAULT_COMPRESSED_BUFFER_SIZE) |
| 77 |
} |
| 78 |
|
| 79 |
func NewWriterLevelBuffer(w io.Writer, level, bufferSize int) (*Writer, error) { |
| 80 |
z := &Writer{w: w, out: make([]byte, bufferSize)} |
| 81 |
if err := z.strm.deflateInit(level); err != nil { |
| 82 |
return nil, err |
| 83 |
} |
| 84 |
return z, nil |
| 85 |
} |
| 86 |
|
| 87 |
// this is the main function: it advances the write with either |
| 88 |
// new data or something else to do, like a flush |
| 89 |
func (z *Writer) write(p []byte, flush int) int { |
| 90 |
if len(p) == 0 { |
| 91 |
z.strm.setInBuf(nil, 0) |
| 92 |
} else { |
| 93 |
z.strm.setInBuf(p, len(p)) |
| 94 |
} |
| 95 |
// we loop until we don't get a full output buffer |
| 96 |
// each loop completely writes the output buffer to the underlying |
| 97 |
// writer |
| 98 |
for { |
| 99 |
// deflate one buffer |
| 100 |
z.strm.setOutBuf(z.out, len(z.out)) |
| 101 |
z.strm.deflate(flush) |
| 102 |
|
| 103 |
// write everything |
| 104 |
from := 0 |
| 105 |
have := len(z.out) - int(z.strm.availOut()) |
| 106 |
for have > 0 { |
| 107 |
var n int |
| 108 |
n, z.err = z.w.Write(z.out[from:have]) |
| 109 |
if z.err != nil { |
| 110 |
z.strm.deflateEnd() |
| 111 |
return 0 |
| 112 |
} |
| 113 |
from += n |
| 114 |
have -= n |
| 115 |
} |
| 116 |
|
| 117 |
// we stop trying if we get a partial response |
| 118 |
if z.strm.availOut() != 0 { |
| 119 |
break |
| 120 |
} |
| 121 |
} |
| 122 |
// the library guarantees this |
| 123 |
if z.strm.availIn() != 0 { |
| 124 |
panic(fmt.Errorf("cgzip: Unexpected error (2)")) |
| 125 |
} |
| 126 |
return len(p) |
| 127 |
} |
| 128 |
|
| 129 |
func (z *Writer) Write(p []byte) (n int, err error) { |
| 130 |
if z.err != nil { |
| 131 |
return 0, z.err |
| 132 |
} |
| 133 |
n = z.write(p, Z_NO_FLUSH) |
| 134 |
return n, z.err |
| 135 |
} |
| 136 |
|
| 137 |
func (z *Writer) Flush() error { |
| 138 |
if z.err != nil { |
| 139 |
return z.err |
| 140 |
} |
| 141 |
z.write(nil, Z_SYNC_FLUSH) |
| 142 |
return z.err |
| 143 |
} |
| 144 |
|
| 145 |
// Calling Close does not close the wrapped io.Writer originally |
| 146 |
// passed to NewWriterX. |
| 147 |
func (z *Writer) Close() error { |
| 148 |
if z.err != nil { |
| 149 |
return z.err |
| 150 |
} |
| 151 |
z.write(nil, Z_FINISH) |
| 152 |
if z.err != nil { |
| 153 |
return z.err |
| 154 |
} |
| 155 |
z.strm.deflateEnd() |
| 156 |
z.err = io.EOF |
| 157 |
return nil |
| 158 |
} |