// 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 (
	
	
)

type byteBuffer interface {
	// Read up to 8 bytes.
	// Returns io.ErrUnexpectedEOF if this cannot be satisfied.
	readSmall(n int) ([]byte, error)

	// 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.
	skipN(n int64) error
}

// in-memory buffer
type byteBuf []byte

func ( *byteBuf) ( int) ([]byte, error) {
	if debugAsserts &&  > 8 {
		panic(fmt.Errorf("small read > 8 (%d). use readBig", ))
	}
	 := *
	if len() <  {
		return nil, io.ErrUnexpectedEOF
	}
	 := [:]
	* = [:]
	return , nil
}

func ( *byteBuf) ( int,  []byte) ([]byte, error) {
	 := *
	if len() <  {
		return nil, io.ErrUnexpectedEOF
	}
	 := [:]
	* = [:]
	return , nil
}

func ( *byteBuf) () (byte, error) {
	 := *
	if len() < 1 {
		return 0, io.ErrUnexpectedEOF
	}
	 := [0]
	* = [1:]
	return , nil
}

func ( *byteBuf) ( int64) error {
	 := *
	if  < 0 {
		return fmt.Errorf("negative skip (%d) requested", )
	}
	if int64(len()) <  {
		return io.ErrUnexpectedEOF
	}
	* = [:]
	return nil
}

// wrapper around a reader.
type readerWrapper struct {
	r   io.Reader
	tmp [8]byte
}

func ( *readerWrapper) ( int) ([]byte, error) {
	if debugAsserts &&  > 8 {
		panic(fmt.Errorf("small read > 8 (%d). use readBig", ))
	}
	,  := io.ReadFull(.r, .tmp[:])
	// We only really care about the actual bytes read.
	if  != nil {
		if  == io.EOF {
			return nil, io.ErrUnexpectedEOF
		}
		if debugDecoder {
			println("readSmall: got", , "want", , "err", )
		}
		return nil, 
	}
	return .tmp[:], nil
}

func ( *readerWrapper) ( int,  []byte) ([]byte, error) {
	if cap() <  {
		 = make([]byte, )
	}
	,  := io.ReadFull(.r, [:])
	if  == io.EOF &&  > 0 {
		 = io.ErrUnexpectedEOF
	}
	return [:], 
}

func ( *readerWrapper) () (byte, error) {
	,  := io.ReadFull(.r, .tmp[:1])
	if  != nil {
		if  == io.EOF {
			 = io.ErrUnexpectedEOF
		}
		return 0, 
	}
	if  != 1 {
		return 0, io.ErrUnexpectedEOF
	}
	return .tmp[0], nil
}

func ( *readerWrapper) ( int64) error {
	,  := io.CopyN(io.Discard, .r, )
	if  !=  {
		 = io.ErrUnexpectedEOF
	}
	return 
}