2019-07-23 16:26:39 +00:00
|
|
|
// Copyright 2019+ Klaus Post. All rights reserved.
|
|
|
|
// License information can be found in the LICENSE file.
|
|
|
|
// Based on work by Yann Collet, released under BSD License.
|
|
|
|
|
|
|
|
package zstd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
type byteBuffer interface {
|
|
|
|
// Read up to 8 bytes.
|
2021-05-28 10:18:26 +00:00
|
|
|
// Returns io.ErrUnexpectedEOF if this cannot be satisfied.
|
|
|
|
readSmall(n int) ([]byte, error)
|
2019-07-23 16:26:39 +00:00
|
|
|
|
|
|
|
// Read >8 bytes.
|
|
|
|
// MAY use the destination slice.
|
|
|
|
readBig(n int, dst []byte) ([]byte, error)
|
|
|
|
|
|
|
|
// Read a single byte.
|
|
|
|
readByte() (byte, error)
|
|
|
|
|
|
|
|
// Skip n bytes.
|
2022-07-21 18:10:25 +00:00
|
|
|
skipN(n int64) error
|
2019-07-23 16:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// in-memory buffer
|
|
|
|
type byteBuf []byte
|
|
|
|
|
2021-05-28 10:18:26 +00:00
|
|
|
func (b *byteBuf) readSmall(n int) ([]byte, error) {
|
2020-02-26 18:45:19 +00:00
|
|
|
if debugAsserts && n > 8 {
|
2019-07-23 16:26:39 +00:00
|
|
|
panic(fmt.Errorf("small read > 8 (%d). use readBig", n))
|
|
|
|
}
|
|
|
|
bb := *b
|
|
|
|
if len(bb) < n {
|
2021-05-28 10:18:26 +00:00
|
|
|
return nil, io.ErrUnexpectedEOF
|
2019-07-23 16:26:39 +00:00
|
|
|
}
|
|
|
|
r := bb[:n]
|
|
|
|
*b = bb[n:]
|
2021-05-28 10:18:26 +00:00
|
|
|
return r, nil
|
2019-07-23 16:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *byteBuf) readBig(n int, dst []byte) ([]byte, error) {
|
|
|
|
bb := *b
|
|
|
|
if len(bb) < n {
|
|
|
|
return nil, io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
r := bb[:n]
|
|
|
|
*b = bb[n:]
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *byteBuf) readByte() (byte, error) {
|
|
|
|
bb := *b
|
|
|
|
if len(bb) < 1 {
|
2023-03-25 05:28:55 +00:00
|
|
|
return 0, io.ErrUnexpectedEOF
|
2019-07-23 16:26:39 +00:00
|
|
|
}
|
|
|
|
r := bb[0]
|
|
|
|
*b = bb[1:]
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:10:25 +00:00
|
|
|
func (b *byteBuf) skipN(n int64) error {
|
2019-07-23 16:26:39 +00:00
|
|
|
bb := *b
|
2022-07-21 18:10:25 +00:00
|
|
|
if n < 0 {
|
|
|
|
return fmt.Errorf("negative skip (%d) requested", n)
|
|
|
|
}
|
|
|
|
if int64(len(bb)) < n {
|
2019-07-23 16:26:39 +00:00
|
|
|
return io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
*b = bb[n:]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrapper around a reader.
|
|
|
|
type readerWrapper struct {
|
|
|
|
r io.Reader
|
|
|
|
tmp [8]byte
|
|
|
|
}
|
|
|
|
|
2021-05-28 10:18:26 +00:00
|
|
|
func (r *readerWrapper) readSmall(n int) ([]byte, error) {
|
2020-02-26 18:45:19 +00:00
|
|
|
if debugAsserts && n > 8 {
|
2019-07-23 16:26:39 +00:00
|
|
|
panic(fmt.Errorf("small read > 8 (%d). use readBig", n))
|
|
|
|
}
|
|
|
|
n2, err := io.ReadFull(r.r, r.tmp[:n])
|
|
|
|
// We only really care about the actual bytes read.
|
2021-05-28 10:18:26 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil, io.ErrUnexpectedEOF
|
|
|
|
}
|
2021-06-08 12:42:57 +00:00
|
|
|
if debugDecoder {
|
2019-07-23 16:26:39 +00:00
|
|
|
println("readSmall: got", n2, "want", n, "err", err)
|
|
|
|
}
|
2021-05-28 10:18:26 +00:00
|
|
|
return nil, err
|
2019-07-23 16:26:39 +00:00
|
|
|
}
|
2021-05-28 10:18:26 +00:00
|
|
|
return r.tmp[:n], nil
|
2019-07-23 16:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *readerWrapper) readBig(n int, dst []byte) ([]byte, error) {
|
|
|
|
if cap(dst) < n {
|
|
|
|
dst = make([]byte, n)
|
|
|
|
}
|
|
|
|
n2, err := io.ReadFull(r.r, dst[:n])
|
2019-11-19 19:05:37 +00:00
|
|
|
if err == io.EOF && n > 0 {
|
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
2019-07-23 16:26:39 +00:00
|
|
|
return dst[:n2], err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *readerWrapper) readByte() (byte, error) {
|
|
|
|
n2, err := r.r.Read(r.tmp[:1])
|
|
|
|
if err != nil {
|
2022-03-16 11:55:03 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
2019-07-23 16:26:39 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if n2 != 1 {
|
|
|
|
return 0, io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
return r.tmp[0], nil
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:10:25 +00:00
|
|
|
func (r *readerWrapper) skipN(n int64) error {
|
2022-09-19 12:12:22 +00:00
|
|
|
n2, err := io.CopyN(io.Discard, r.r, n)
|
2022-07-21 18:10:25 +00:00
|
|
|
if n2 != n {
|
2019-08-23 07:01:56 +00:00
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
2019-07-23 16:26:39 +00:00
|
|
|
return err
|
|
|
|
}
|