func (o *objectReader) inflate() ([]byte, error) { zr, err := zlib.NewReader(o.pr.r) if err != nil { if err == zlib.ErrHeader { return nil, zlib.ErrHeader } else { return nil, NewError("error opening packfile's object zlib: %v", err) } } defer zr.Close() if o.size > SIZE_LIMIT { return nil, NewError("the object size exceeed the allowed limit: %d", o.size) } var buf bytes.Buffer io.Copy(&buf, zr) // also: io.CopyN(&buf, zr, int64(o.size)) var bufLen = buf.Len() if bufLen != int(o.size) { return nil, NewError("inflated size mismatch, expected %d, got %d", o.size, bufLen) } return buf.Bytes(), nil }
func (r *reader) readCompressed(in io.Reader, events []interface{}) ([]interface{}, error) { var hdr [4]byte if err := readFull(in, hdr[:]); err != nil { return nil, err } payloadSz := binary.BigEndian.Uint32(hdr[:]) limit := io.LimitReader(in, int64(payloadSz)) reader, err := zlib.NewReader(limit) if err != nil { log.Printf("Failed to initialized zlib reader %v\n", err) return nil, err } events, err = r.readEvents(reader, events) if err != nil { _ = reader.Close() return nil, err } if err := reader.Close(); err != nil { return nil, err } // consume final bytes from limit reader for { var tmp [16]byte if _, err := limit.Read(tmp[:]); err != nil { if err != io.EOF { return nil, err } break } } return events, nil }
func acquireFlateReader(r io.Reader) (io.ReadCloser, error) { v := flateReaderPool.Get() if v == nil { zr, err := zlib.NewReader(r) if err != nil { return nil, err } return zr, nil } zr := v.(io.ReadCloser) if err := resetFlateReader(zr, r); err != nil { return nil, err } return zr, nil }
func (r *Reader) inflate(w io.Writer) error { zr, err := zlib.NewReader(r.r) if err != nil { if err == zlib.ErrHeader { return zlib.ErrHeader } return ZLibErr.n("%s", err) } defer zr.Close() _, err = io.Copy(w, zr) return err }