package brotli

import (
	
	
)

type decodeError int

func ( decodeError) () string {
	return "brotli: " + string(decoderErrorString(int()))
}

var errExcessiveInput = errors.New("brotli: excessive input")
var errInvalidState = errors.New("brotli: invalid state")

// readBufSize is a "good" buffer size that avoids excessive round-trips
// between C and Go but doesn't waste too much memory on buffering.
// It is arbitrarily chosen to be equal to the constant used in io.Copy.
const readBufSize = 32 * 1024

// ReaderOptions controls optional Brotli decoding features.
type ReaderOptions struct {
	// LargeWindow accepts the large-window extension defined by RFC 9841.
	// It is disabled by default. Applications such as PDF readers may require it.
	LargeWindow bool
}

// NewReader creates a new Reader reading the given reader.
func ( io.Reader) *Reader {
	return NewReaderOptions(, ReaderOptions{})
}

// NewReaderOptions creates a Reader with the supplied decoding options.
func ( io.Reader,  ReaderOptions) *Reader {
	 := &Reader{largeWindow: .LargeWindow}
	.Reset()
	return 
}

// Reset discards the Reader's state and makes it equivalent to the result of
// its original state from NewReader, but reading from src instead.
// This permits reusing a Reader rather than allocating a new one.
// Error is always nil
func ( *Reader) ( io.Reader) error {
	if .error_code < 0 {
		// There was an unrecoverable error, leaving the Reader's state
		// undefined. Clear out everything but the buffers.
		* = Reader{
			largeWindow:      .largeWindow,
			buf:              .buf,
			block_type_trees: .block_type_trees,
			literal_hgroup: huffmanTreeGroup{
				htrees: .literal_hgroup.htrees,
				codes:  .literal_hgroup.codes,
			},
			distance_hgroup: huffmanTreeGroup{
				htrees: .distance_hgroup.htrees,
				codes:  .distance_hgroup.codes,
			},
			insert_copy_hgroup: huffmanTreeGroup{
				htrees: .insert_copy_hgroup.htrees,
				codes:  .insert_copy_hgroup.codes,
			},
		}
	}

	decoderStateInit()
	.large_window = .largeWindow
	.src = 
	if .buf == nil {
		.buf = make([]byte, readBufSize)
	}
	.in = .buf[:0]
	return nil
}

func ( *Reader) ( []byte) ( int,  error) {
	if !decoderHasMoreOutput() && len(.in) == 0 {
		,  := .src.Read(.buf)
		if  == 0 {
			if  == io.EOF && .state != stateDone {
				 = io.ErrUnexpectedEOF
			}
			// If readErr is `nil`, we just proxy underlying stream behavior.
			return 0, 
		}
		.in = .buf[:]
	}

	if len() == 0 {
		return 0, nil
	}

	for {
		var  uint
		 := uint(len(.in))
		 := uint(len())
		 := 
		 := 
		 := decoderDecompressStream(, &, &.in, &, &)
		 =  - 
		 = int()

		switch  {
		case decoderResultSuccess:
			if len(.in) > 0 || .buffer_length > 0 {
				return , errExcessiveInput
			}
			return , nil
		case decoderResultError:
			return , decodeError(decoderGetErrorCode())
		case decoderResultNeedsMoreOutput:
			if  == 0 {
				return 0, io.ErrShortBuffer
			}
			return , nil
		case decoderNeedsMoreInput:
		}

		if len(.in) != 0 {
			return 0, errInvalidState
		}

		// Calling r.src.Read may block. Don't block if we have data to return.
		if  > 0 {
			return , nil
		}

		// Top off the buffer.
		,  := .src.Read(.buf)
		if  == 0 {
			// Not enough data to complete decoding.
			if  == io.EOF {
				return 0, io.ErrUnexpectedEOF
			}
			return 0, 
		}
		.in = .buf[:]
	}
}