| 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 "io" |
| 22 |
|
| 23 |
// err starts out as nil |
| 24 |
// we will call inflateEnd when we set err to a value: |
| 25 |
// - whatever error is returned by the underlying reader |
| 26 |
// - io.EOF if Close was called |
| 27 |
type reader struct { |
| 28 |
r io.Reader |
| 29 |
in []byte |
| 30 |
strm zstream |
| 31 |
err error |
| 32 |
skipIn bool |
| 33 |
} |
| 34 |
|
| 35 |
// NewReader returns a new cgzip.reader for reading gzip files with the C gzip |
| 36 |
// library. |
| 37 |
func NewReader(r io.Reader) (io.ReadCloser, error) { |
| 38 |
return NewReaderBuffer(r, DEFAULT_COMPRESSED_BUFFER_SIZE) |
| 39 |
} |
| 40 |
|
| 41 |
// NewReaderBuffer returns a new cgzip.reader with a given buffer size for |
| 42 |
// reading gzip files with the C gzip library. |
| 43 |
func NewReaderBuffer(r io.Reader, bufferSize int) (io.ReadCloser, error) { |
| 44 |
z := &reader{r: r, in: make([]byte, bufferSize)} |
| 45 |
if err := z.strm.inflateInit(); err != nil { |
| 46 |
return nil, err |
| 47 |
} |
| 48 |
return z, nil |
| 49 |
} |
| 50 |
|
| 51 |
// Read reads from the gz stream. |
| 52 |
func (z *reader) Read(p []byte) (int, error) { |
| 53 |
if z.err != nil { |
| 54 |
return 0, z.err |
| 55 |
} |
| 56 |
|
| 57 |
if len(p) == 0 { |
| 58 |
return 0, nil |
| 59 |
} |
| 60 |
|
| 61 |
// read and deflate until the output buffer is full |
| 62 |
z.strm.setOutBuf(p, len(p)) |
| 63 |
|
| 64 |
for { |
| 65 |
// if we have no data to inflate, read more |
| 66 |
if !z.skipIn && z.strm.availIn() == 0 { |
| 67 |
var n int |
| 68 |
n, z.err = z.r.Read(z.in) |
| 69 |
// If we got data and EOF, pretend we didn't get the |
| 70 |
// EOF. That way we will return the right values |
| 71 |
// upstream. Note this will trigger another read |
| 72 |
// later on, that should return (0, EOF). |
| 73 |
if n > 0 && z.err == io.EOF { |
| 74 |
z.err = nil |
| 75 |
} |
| 76 |
|
| 77 |
// FIXME(alainjobart) this code is not compliant with |
| 78 |
// the Reader interface. We should process all the |
| 79 |
// data we got from the reader, and then return the |
| 80 |
// error, whatever it is. |
| 81 |
if (z.err != nil && z.err != io.EOF) || (n == 0 && z.err == io.EOF) { |
| 82 |
z.strm.inflateEnd() |
| 83 |
return 0, z.err |
| 84 |
} |
| 85 |
|
| 86 |
z.strm.setInBuf(z.in, n) |
| 87 |
} else { |
| 88 |
z.skipIn = false |
| 89 |
} |
| 90 |
|
| 91 |
// inflate some |
| 92 |
ret, err := z.strm.inflate(zNoFlush) |
| 93 |
if err != nil { |
| 94 |
z.err = err |
| 95 |
z.strm.inflateEnd() |
| 96 |
return 0, z.err |
| 97 |
} |
| 98 |
|
| 99 |
// if we read something, we're good |
| 100 |
have := len(p) - z.strm.availOut() |
| 101 |
if have > 0 { |
| 102 |
z.skipIn = ret == Z_OK && z.strm.availOut() == 0 |
| 103 |
return have, z.err |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// Close closes the Reader. It does not close the underlying io.Reader. |
| 109 |
func (z *reader) Close() error { |
| 110 |
if z.err != nil { |
| 111 |
if z.err != io.EOF { |
| 112 |
return z.err |
| 113 |
} |
| 114 |
return nil |
| 115 |
} |
| 116 |
z.strm.inflateEnd() |
| 117 |
z.err = io.EOF |
| 118 |
return nil |
| 119 |
} |