package brotli

import 

/* Copyright 2013 Google Inc. All Rights Reserved.

   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/

/* Bit reading helpers */

const shortFillBitWindowRead = (8 >> 1)

var kBitMask = [33]uint32{
	0x00000000,
	0x00000001,
	0x00000003,
	0x00000007,
	0x0000000F,
	0x0000001F,
	0x0000003F,
	0x0000007F,
	0x000000FF,
	0x000001FF,
	0x000003FF,
	0x000007FF,
	0x00000FFF,
	0x00001FFF,
	0x00003FFF,
	0x00007FFF,
	0x0000FFFF,
	0x0001FFFF,
	0x0003FFFF,
	0x0007FFFF,
	0x000FFFFF,
	0x001FFFFF,
	0x003FFFFF,
	0x007FFFFF,
	0x00FFFFFF,
	0x01FFFFFF,
	0x03FFFFFF,
	0x07FFFFFF,
	0x0FFFFFFF,
	0x1FFFFFFF,
	0x3FFFFFFF,
	0x7FFFFFFF,
	0xFFFFFFFF,
}

func bitMask( uint32) uint32 {
	return kBitMask[]
}

type bitReader struct {
	val_      uint64
	bit_pos_  uint32
	input     []byte
	input_len uint
	byte_pos  uint
}

type bitReaderState struct {
	val_      uint64
	bit_pos_  uint32
	input     []byte
	input_len uint
	byte_pos  uint
}

/* Initializes the BrotliBitReader fields. */

/* Ensures that accumulator is not empty.
   May consume up to sizeof(brotli_reg_t) - 1 bytes of input.
   Returns false if data is required but there is no input available.
   For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned
   reading. */
func bitReaderSaveState( *bitReader,  *bitReaderState) {
	.val_ = .val_
	.bit_pos_ = .bit_pos_
	.input = .input
	.input_len = .input_len
	.byte_pos = .byte_pos
}

func bitReaderRestoreState( *bitReader,  *bitReaderState) {
	.val_ = .val_
	.bit_pos_ = .bit_pos_
	.input = .input
	.input_len = .input_len
	.byte_pos = .byte_pos
}

func getAvailableBits( *bitReader) uint32 {
	return 64 - .bit_pos_
}

/* Returns amount of unread bytes the bit reader still has buffered from the
   BrotliInput, including whole bytes in br->val_. */
func getRemainingBytes( *bitReader) uint {
	return uint(uint32(.input_len-.byte_pos) + (getAvailableBits() >> 3))
}

/* Checks if there is at least |num| bytes left in the input ring-buffer
   (excluding the bits remaining in br->val_). */
func checkInputAmount( *bitReader,  uint) bool {
	return .input_len-.byte_pos >= 
}

/* Guarantees that there are at least |n_bits| + 1 bits in accumulator.
   Precondition: accumulator contains at least 1 bit.
   |n_bits| should be in the range [1..24] for regular build. For portable
   non-64-bit little-endian build only 16 bits are safe to request. */
func fillBitWindow( *bitReader,  uint32) {
	if .bit_pos_ >= 32 {
		.val_ >>= 32
		.bit_pos_ ^= 32 /* here same as -= 32 because of the if condition */
		.val_ |= (uint64(binary.LittleEndian.Uint32(.input[.byte_pos:]))) << 32
		.byte_pos += 4
	}
}

/* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no
   more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */
func fillBitWindow16( *bitReader) {
	fillBitWindow(, 17)
}

/* Tries to pull one byte of input to accumulator.
   Returns false if there is no input available. */
func pullByte( *bitReader) bool {
	if .byte_pos == .input_len {
		return false
	}

	.val_ >>= 8
	.val_ |= (uint64(.input[.byte_pos])) << 56
	.bit_pos_ -= 8
	.byte_pos++
	return true
}

/* Returns currently available bits.
   The number of valid bits could be calculated by BrotliGetAvailableBits. */
func getBitsUnmasked( *bitReader) uint64 {
	return .val_ >> .bit_pos_
}

/* Like BrotliGetBits, but does not mask the result.
   The result contains at least 16 valid bits. */
func get16BitsUnmasked( *bitReader) uint32 {
	fillBitWindow(, 16)
	return uint32(getBitsUnmasked())
}

/* Returns the specified number of bits from |br| without advancing bit
   position. */
func getBits( *bitReader,  uint32) uint32 {
	fillBitWindow(, )
	return uint32(getBitsUnmasked()) & bitMask()
}

/* Tries to peek the specified amount of bits. Returns false, if there
   is not enough input. */
func safeGetBits( *bitReader,  uint32,  *uint32) bool {
	for getAvailableBits() <  {
		if !pullByte() {
			return false
		}
	}

	* = uint32(getBitsUnmasked()) & bitMask()
	return true
}

/* Advances the bit pos by |n_bits|. */
func dropBits( *bitReader,  uint32) {
	.bit_pos_ += 
}

func bitReaderUnload( *bitReader) {
	var  uint32 = getAvailableBits() >> 3
	var  uint32 =  << 3
	.byte_pos -= uint()
	if  == 64 {
		.val_ = 0
	} else {
		.val_ <<= 
	}

	.bit_pos_ += 
}

/* Reads the specified number of bits from |br| and advances the bit pos.
   Precondition: accumulator MUST contain at least |n_bits|. */
func takeBits( *bitReader,  uint32,  *uint32) {
	* = uint32(getBitsUnmasked()) & bitMask()
	dropBits(, )
}

/* Reads the specified number of bits from |br| and advances the bit pos.
   Assumes that there is enough input to perform BrotliFillBitWindow. */
func readBits( *bitReader,  uint32) uint32 {
	var  uint32
	fillBitWindow(, )
	takeBits(, , &)
	return 
}

/* Tries to read the specified amount of bits. Returns false, if there
   is not enough input. |n_bits| MUST be positive. */
func safeReadBits( *bitReader,  uint32,  *uint32) bool {
	for getAvailableBits() <  {
		if !pullByte() {
			return false
		}
	}

	takeBits(, , )
	return true
}

/* Advances the bit reader position to the next byte boundary and verifies
   that any skipped bits are set to zero. */
func bitReaderJumpToByteBoundary( *bitReader) bool {
	var  uint32 = getAvailableBits() & 0x7
	var  uint32 = 0
	if  != 0 {
		takeBits(, , &)
	}

	return  == 0
}

/* Copies remaining input bytes stored in the bit reader to the output. Value
   |num| may not be larger than BrotliGetRemainingBytes. The bit reader must be
   warmed up again after this. */
func copyBytes( []byte,  *bitReader,  uint) {
	for getAvailableBits() >= 8 &&  > 0 {
		[0] = byte(getBitsUnmasked())
		dropBits(, 8)
		 = [1:]
		--
	}

	copy(, .input[.byte_pos:][:])
	.byte_pos += 
}

func initBitReader( *bitReader) {
	.val_ = 0
	.bit_pos_ = 64
}

func warmupBitReader( *bitReader) bool {
	/* Fixing alignment after unaligned BrotliFillWindow would result accumulator
	   overflow. If unalignment is caused by BrotliSafeReadBits, then there is
	   enough space in accumulator to fix alignment. */
	if getAvailableBits() == 0 {
		if !pullByte() {
			return false
		}
	}

	return true
}