package brotli

import 

/* Copyright 2015 Google Inc. All Rights Reserved.

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

/* Function for fast encoding of an input fragment, independently from the input
   history. This function uses one-pass processing: when we find a backward
   match, we immediately emit the corresponding command and literal codes to
   the bit stream.

   Adapted from the CompressFragment() function in
   https://github.com/google/snappy/blob/master/snappy.cc */

const maxDistance_compress_fragment = 262128

func hash5( []byte,  uint) uint32 {
	var  uint64 = (binary.LittleEndian.Uint64() << 24) * uint64(kHashMul32)
	return uint32( >> )
}

func hashBytesAtOffset5( uint64,  int,  uint) uint32 {
	assert( >= 0)
	assert( <= 3)
	{
		var  uint64 = (( >> uint(8*)) << 24) * uint64(kHashMul32)
		return uint32( >> )
	}
}

func isMatch5( []byte,  []byte) bool {
	return binary.LittleEndian.Uint32() == binary.LittleEndian.Uint32() &&
		[4] == [4]
}

/* Builds a literal prefix code into "depths" and "bits" based on the statistics
   of the "input" string and stores it into the bit stream.
   Note that the prefix code here is built from the pre-LZ77 input, therefore
   we can only approximate the statistics of the actual literal stream.
   Moreover, for long inputs we build a histogram from a sample of the input
   and thus have to assign a non-zero depth for each literal.
   Returns estimated compression ratio millibytes/char for encoding given input
   with generated code. */
func buildAndStoreLiteralPrefixCode( []byte,  uint,  []byte,  []uint16,  *uint,  []byte) uint {
	var  = [256]uint32{0}
	var  uint
	var  uint
	if  < 1<<15 {
		for  = 0;  < ; ++ {
			[[]]++
		}

		 = 
		for  = 0;  < 256; ++ {
			/* We weigh the first 11 samples with weight 3 to account for the
			   balancing effect of the LZ77 phase on the histogram. */
			var  uint32 = 2 * brotli_min_uint32_t([], 11)
			[] += 
			 += uint()
		}
	} else {
		const  uint = 29
		for  = 0;  < ;  +=  {
			[[]]++
		}

		 = ( +  - 1) / 
		for  = 0;  < 256; ++ {
			/* We add 1 to each population count to avoid 0 bit depths (since this is
			   only a sample and we don't know if the symbol appears or not), and we
			   weigh the first 11 samples with weight 3 to account for the balancing
			   effect of the LZ77 phase on the histogram (more frequent symbols are
			   more likely to be in backward references instead as literals). */
			var  uint32 = 1 + 2*brotli_min_uint32_t([], 11)
			[] += 
			 += uint()
		}
	}

	buildAndStoreHuffmanTreeFast([:], , /* max_bits = */
		8, , , , )
	{
		var  uint = 0
		for  = 0;  < 256; ++ {
			if [] != 0 {
				 += uint([] * uint32([]))
			}
		}

		/* Estimated encoding ratio, millibytes per symbol. */
		return ( * 125) / 
	}
}

/* Builds a command and distance prefix code (each 64 symbols) into "depth" and
   "bits" based on "histogram" and stores it into the bit stream. */
func buildAndStoreCommandPrefixCode1( []uint32,  []byte,  []uint16,  *uint,  []byte) {
	var  [129]huffmanTree
	var  = [numCommandSymbols]byte{0}
	/* Tree size for building a tree over 64 symbols is 2 * 64 + 1. */

	var  [64]uint16

	createHuffmanTree(, 64, 15, [:], )
	createHuffmanTree([64:], 64, 14, [:], [64:])

	/* We have to jump through a few hoops here in order to compute
	   the command bits because the symbols are in a different order than in
	   the full alphabet. This looks complicated, but having the symbols
	   in this order in the command bits saves a few branches in the Emit*
	   functions. */
	copy([:], [:24])

	copy([24:][:], [40:][:8])
	copy([32:][:], [24:][:8])
	copy([40:][:], [48:][:8])
	copy([48:][:], [32:][:8])
	copy([56:][:], [56:][:8])
	convertBitDepthsToSymbols([:], 64, [:])
	copy(, [:24])
	copy([24:], [32:][:8])
	copy([32:], [48:][:8])
	copy([40:], [24:][:8])
	copy([48:], [40:][:8])
	copy([56:], [56:][:8])
	convertBitDepthsToSymbols([64:], 64, [64:])
	{
		/* Create the bit length array for the full command alphabet. */
		var  uint
		for  := 0;  < int(64); ++ {
			[] = 0
		} /* only 64 first values were used */
		copy([:], [:8])
		copy([64:][:], [8:][:8])
		copy([128:][:], [16:][:8])
		copy([192:][:], [24:][:8])
		copy([384:][:], [32:][:8])
		for  = 0;  < 8; ++ {
			[128+8*] = [40+]
			[256+8*] = [48+]
			[448+8*] = [56+]
		}

		storeHuffmanTree([:], numCommandSymbols, [:], , )
	}

	storeHuffmanTree([64:], 64, [:], , )
}

/* REQUIRES: insertlen < 6210 */
func emitInsertLen1( uint,  []byte,  []uint16,  []uint32,  *uint,  []byte) {
	if  < 6 {
		var  uint =  + 40
		writeBits(uint([]), uint64([]), , )
		[]++
	} else if  < 130 {
		var  uint =  - 2
		var  uint32 = log2FloorNonZero() - 1
		var  uint =  >> 
		var  uint = uint(( << 1) + uint32() + 42)
		writeBits(uint([]), uint64([]), , )
		writeBits(uint(), uint64()-(uint64()<<), , )
		[]++
	} else if  < 2114 {
		var  uint =  - 66
		var  uint32 = log2FloorNonZero()
		var  uint = uint( + 50)
		writeBits(uint([]), uint64([]), , )
		writeBits(uint(), uint64()-(uint64(uint(1))<<), , )
		[]++
	} else {
		writeBits(uint([61]), uint64([61]), , )
		writeBits(12, uint64()-2114, , )
		[61]++
	}
}

func emitLongInsertLen( uint,  []byte,  []uint16,  []uint32,  *uint,  []byte) {
	if  < 22594 {
		writeBits(uint([62]), uint64([62]), , )
		writeBits(14, uint64()-6210, , )
		[62]++
	} else {
		writeBits(uint([63]), uint64([63]), , )
		writeBits(24, uint64()-22594, , )
		[63]++
	}
}

func emitCopyLen1( uint,  []byte,  []uint16,  []uint32,  *uint,  []byte) {
	if  < 10 {
		writeBits(uint([+14]), uint64([+14]), , )
		[+14]++
	} else if  < 134 {
		var  uint =  - 6
		var  uint32 = log2FloorNonZero() - 1
		var  uint =  >> 
		var  uint = uint(( << 1) + uint32() + 20)
		writeBits(uint([]), uint64([]), , )
		writeBits(uint(), uint64()-(uint64()<<), , )
		[]++
	} else if  < 2118 {
		var  uint =  - 70
		var  uint32 = log2FloorNonZero()
		var  uint = uint( + 28)
		writeBits(uint([]), uint64([]), , )
		writeBits(uint(), uint64()-(uint64(uint(1))<<), , )
		[]++
	} else {
		writeBits(uint([39]), uint64([39]), , )
		writeBits(24, uint64()-2118, , )
		[39]++
	}
}

func emitCopyLenLastDistance1( uint,  []byte,  []uint16,  []uint32,  *uint,  []byte) {
	if  < 12 {
		writeBits(uint([-4]), uint64([-4]), , )
		[-4]++
	} else if  < 72 {
		var  uint =  - 8
		var  uint32 = log2FloorNonZero() - 1
		var  uint =  >> 
		var  uint = uint(( << 1) + uint32() + 4)
		writeBits(uint([]), uint64([]), , )
		writeBits(uint(), uint64()-(uint64()<<), , )
		[]++
	} else if  < 136 {
		var  uint =  - 8
		var  uint = ( >> 5) + 30
		writeBits(uint([]), uint64([]), , )
		writeBits(5, uint64()&31, , )
		writeBits(uint([64]), uint64([64]), , )
		[]++
		[64]++
	} else if  < 2120 {
		var  uint =  - 72
		var  uint32 = log2FloorNonZero()
		var  uint = uint( + 28)
		writeBits(uint([]), uint64([]), , )
		writeBits(uint(), uint64()-(uint64(uint(1))<<), , )
		writeBits(uint([64]), uint64([64]), , )
		[]++
		[64]++
	} else {
		writeBits(uint([39]), uint64([39]), , )
		writeBits(24, uint64()-2120, , )
		writeBits(uint([64]), uint64([64]), , )
		[39]++
		[64]++
	}
}

func emitDistance1( uint,  []byte,  []uint16,  []uint32,  *uint,  []byte) {
	var  uint =  + 3
	var  uint32 = log2FloorNonZero() - 1
	var  uint = ( >> ) & 1
	var  uint = (2 + ) << 
	var  uint = uint(2*(-1) + uint32() + 80)
	writeBits(uint([]), uint64([]), , )
	writeBits(uint(), uint64()-uint64(), , )
	[]++
}

func emitLiterals( []byte,  uint,  []byte,  []uint16,  *uint,  []byte) {
	var  uint
	for  = 0;  < ; ++ {
		var  byte = []
		writeBits(uint([]), uint64([]), , )
	}
}

/* REQUIRES: len <= 1 << 24. */
func storeMetaBlockHeader1( uint,  bool,  *uint,  []byte) {
	var  uint = 6

	/* ISLAST */
	writeBits(1, 0, , )

	if  <= 1<<16 {
		 = 4
	} else if  <= 1<<20 {
		 = 5
	}

	writeBits(2, uint64()-4, , )
	writeBits(*4, uint64()-1, , )

	/* ISUNCOMPRESSED */
	writeSingleBit(, , )
}

func updateBits( uint,  uint32,  uint,  []byte) {
	for  > 0 {
		var  uint =  >> 3
		var  uint =  & 7
		var  uint = brotli_min_size_t(, 8-)
		var  uint =  + 
		var  uint32 = (^((1 << ) - 1)) | ((1 << ) - 1)
		var  uint32 = uint32([]) & 
		var  uint32 =  & ((1 << ) - 1)
		[] = byte(<< | )
		 -= 
		 >>= 
		 += 
	}
}

func rewindBitPosition1( uint,  *uint,  []byte) {
	var  uint =  & 7
	var  uint = (1 << ) - 1
	[>>3] &= byte()
	* = 
}

var shouldMergeBlock_kSampleRate uint = 43

func shouldMergeBlock( []byte,  uint,  []byte) bool {
	var  = [256]uint{0}
	var  uint
	for  = 0;  < ;  += shouldMergeBlock_kSampleRate {
		[[]]++
	}
	{
		var  uint = ( + shouldMergeBlock_kSampleRate - 1) / shouldMergeBlock_kSampleRate
		var  float64 = (fastLog2()+0.5)*float64() + 200
		for  = 0;  < 256; ++ {
			 -= float64([]) * (float64([]) + fastLog2([]))
		}

		return  >= 0.0
	}
}

func shouldUseUncompressedMode( []byte,  []byte,  uint,  uint) bool {
	var  uint = uint(-cap() + cap())
	if *50 >  {
		return false
	} else {
		return  > 980
	}
}

func emitUncompressedMetaBlock1( []byte,  []byte,  uint,  *uint,  []byte) {
	var  uint = uint(-cap() + cap())
	rewindBitPosition1(, , )
	storeMetaBlockHeader1(uint(), true, , )
	* = (* + 7) &^ 7
	copy([*>>3:], [:])
	* += uint( << 3)
	[*>>3] = 0
}

var kCmdHistoSeed = [128]uint32{
	0,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	0,
	0,
	0,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	0,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	1,
	0,
	0,
	0,
	0,
}

var compressFragmentFastImpl_kFirstBlockSize uint = 3 << 15
var compressFragmentFastImpl_kMergeBlockSize uint = 1 << 16

func compressFragmentFastImpl( []byte,  uint,  bool,  []int,  uint,  []byte,  []uint16,  *uint,  []byte,  *uint,  []byte) {
	var  [128]uint32
	var  int
	var  int = 0
	var  int = 0
	var  int = 0
	const  uint = windowGap
	const  uint = 5
	var  int = 
	var  uint = brotli_min_size_t(, compressFragmentFastImpl_kFirstBlockSize)
	var  uint = 
	var  uint = * + 3
	var  [256]byte
	var  [256]uint16
	var  uint
	var  int
	var  int
	var  uint = 64 - 

	/* "next_emit" is a pointer to the first byte that is not covered by a
	   previous copy. Bytes between "next_emit" and the start of the next copy or
	   the end of the input will be emitted as literal bytes. */

	/* Save the start of the first block for position and distance computations.
	 */

	/* Save the bit position of the MLEN field of the meta-block header, so that
	   we can update it later if we decide to extend this meta-block. */
	storeMetaBlockHeader1(, false, , )

	/* No block splits, no contexts. */
	writeBits(13, 0, , )

	 = buildAndStoreLiteralPrefixCode([:], , [:], [:], , )
	{
		/* Store the pre-compressed command and distance prefix codes. */
		var  uint
		for  = 0; +7 < *;  += 8 {
			writeBits(8, uint64([>>3]), , )
		}
	}

	writeBits(*&7, uint64([*>>3]), , )

	/* Initialize the command and distance histograms. We will gather
	   statistics of command and distance codes during the processing
	   of this block and use it to update the command and distance
	   prefix codes for the next block. */
:
	copy([:], kCmdHistoSeed[:])

	/* "ip" is the input pointer. */
	 = 

	 = -1
	 = int(uint() + )

	if  >=  {
		var  uint = brotli_min_size_t(-, -)
		var  int = int(uint() + )
		/* For the last block, we need to keep a 16 bytes margin so that we can be
		   sure that all distances are at most window size - 16.
		   For all other blocks, we only need to keep a margin of 5 bytes so that
		   we don't go over the block size with a copy. */

		var  uint32
		++
		for  = hash5([:], ); ; {
			var  uint32 = 32
			var  int = 
			/* Step 1: Scan forward in the input looking for a 5-byte-long match.
			   If we get close to exhausting the input then goto emit_remainder.

			   Heuristic match skipping: If 32 bytes are scanned with no matches
			   found, start looking only at every other byte. If 32 more bytes are
			   scanned, look at every third byte, etc.. When a match is found,
			   immediately go back to looking at every byte. This is a small loss
			   (~5% performance, ~0.1% density) for compressible data due to more
			   bookkeeping, but for non-compressible data (such as JPEG) it's a huge
			   win since the compressor quickly "realizes" the data is incompressible
			   and doesn't bother looking for matches everywhere.

			   The "skip" variable keeps track of how many bytes there are since the
			   last match; dividing it by 32 (i.e. right-shifting by five) gives the
			   number of bytes to move ahead for each iteration. */

			var  int
			assert( < )

		:
			for {
				var  uint32 = 
				var  uint32 =  >> 5
				++
				assert( == hash5([:], ))
				 = 
				 = int(uint32() + )
				if  >  {
					goto 
				}

				 = hash5([:], )
				 =  - 
				if isMatch5([:], [:]) {
					if  <  {
						[] = int( - )
						break
					}
				}

				 =  + []
				assert( >= )
				assert( < )

				[] = int( - )
				if isMatch5([:], [:]) {
					break
				}
			}

			/* Check copy distance. If candidate is not feasible, continue search.
			   Checking is done outside of hot loop to reduce overhead. */
			if - > maxDistance_compress_fragment {
				goto 
			}

			/* Step 2: Emit the found match together with the literal bytes from
			   "next_emit" to the bit stream, and then see if we can find a next match
			   immediately afterwards. Repeat until we find no match for the input
			   without emitting some literal bytes. */
			{
				var  int = 
				/* > 0 */
				var  uint = 5 + findMatchLengthWithLimit([+5:], [+5:], uint(-)-5)
				var  int = int( - )
				/* We have a 5-byte match at ip, and we need to emit bytes in
				   [next_emit, ip). */

				var  uint = uint( - )
				 += int()
				if  < 6210 {
					emitInsertLen1(, , , [:], , )
				} else if shouldUseUncompressedMode([:], [:], , ) {
					emitUncompressedMetaBlock1([:], [:], -3, , )
					 -= uint( - )
					 = 
					 = 
					goto 
				} else {
					emitLongInsertLen(, , , [:], , )
				}

				emitLiterals([:], , [:], [:], , )
				if  ==  {
					writeBits(uint([64]), uint64([64]), , )
					[64]++
				} else {
					emitDistance1(uint(), , , [:], , )
					 = 
				}

				emitCopyLenLastDistance1(, , , [:], , )

				 = 
				if  >=  {
					goto 
				}

				/* We could immediately start working at ip now, but to improve
				   compression we first update "table" with the hashes of some positions
				   within the last copy. */
				{
					var  uint64 = binary.LittleEndian.Uint64([-3:])
					var  uint32 = hashBytesAtOffset5(, 0, )
					var  uint32 = hashBytesAtOffset5(, 3, )
					[] = int( -  - 3)
					 = hashBytesAtOffset5(, 1, )
					[] = int( -  - 2)
					 = hashBytesAtOffset5(, 2, )
					[] = int( -  - 1)

					 =  + []
					[] = int( - )
				}
			}

			for isMatch5([:], [:]) {
				var  int = 
				/* We have a 5-byte match at ip, and no need to emit any literal bytes
				   prior to ip. */

				var  uint = 5 + findMatchLengthWithLimit([+5:], [+5:], uint(-)-5)
				if - > maxDistance_compress_fragment {
					break
				}
				 += int()
				 = int( - ) /* > 0 */
				emitCopyLen1(, , , [:], , )
				emitDistance1(uint(), , , [:], , )

				 = 
				if  >=  {
					goto 
				}

				/* We could immediately start working at ip now, but to improve
				   compression we first update "table" with the hashes of some positions
				   within the last copy. */
				{
					var  uint64 = binary.LittleEndian.Uint64([-3:])
					var  uint32 = hashBytesAtOffset5(, 0, )
					var  uint32 = hashBytesAtOffset5(, 3, )
					[] = int( -  - 3)
					 = hashBytesAtOffset5(, 1, )
					[] = int( -  - 2)
					 = hashBytesAtOffset5(, 2, )
					[] = int( -  - 1)

					 =  + []
					[] = int( - )
				}
			}

			++
			 = hash5([:], )
		}
	}

:
	assert( <= )
	 += int()
	 -= 
	 = brotli_min_size_t(, compressFragmentFastImpl_kMergeBlockSize)

	/* Decide if we want to continue this meta-block instead of emitting the
	   last insert-only command. */
	if  > 0 && + <= 1<<20 && shouldMergeBlock([:], , [:]) {
		assert( > 1<<16)

		/* Update the size of the current meta-block and continue emitting commands.
		   We can do this because the current size and the new size both have 5
		   nibbles. */
		 += 

		updateBits(20, uint32(-1), , )
		goto 
	}

	/* Emit the remaining bytes as literals. */
	if  <  {
		var  uint = uint( - )
		if  < 6210 {
			emitInsertLen1(, , , [:], , )
			emitLiterals([:], , [:], [:], , )
		} else if shouldUseUncompressedMode([:], [:], , ) {
			emitUncompressedMetaBlock1([:], [:], -3, , )
		} else {
			emitLongInsertLen(, , , [:], , )
			emitLiterals([:], , [:], [:], , )
		}
	}

	 = 

	/* If we have more data, write a new meta-block header and prefix codes and
	   then continue emitting commands. */
:
	if  > 0 {
		 = 
		 = brotli_min_size_t(, compressFragmentFastImpl_kFirstBlockSize)
		 = 

		/* Save the bit position of the MLEN field of the meta-block header, so that
		   we can update it later if we decide to extend this meta-block. */
		 = * + 3

		storeMetaBlockHeader1(, false, , )

		/* No block splits, no contexts. */
		writeBits(13, 0, , )

		 = buildAndStoreLiteralPrefixCode([:], , [:], [:], , )
		buildAndStoreCommandPrefixCode1([:], , , , )
		goto 
	}

	if ! {
		/* If this is not the last block, update the command and distance prefix
		   codes for the next block and store the compressed forms. */
		[0] = 0

		* = 0
		buildAndStoreCommandPrefixCode1([:], , , , )
	}
}

/* Compresses "input" string to the "*storage" buffer as one or more complete
   meta-blocks, and updates the "*storage_ix" bit position.

   If "is_last" is 1, emits an additional empty last meta-block.

   "cmd_depth" and "cmd_bits" contain the command and distance prefix codes
   (see comment in encode.h) used for the encoding of this input fragment.
   If "is_last" is 0, they are updated to reflect the statistics
   of this input fragment, to be used for the encoding of the next fragment.

   "*cmd_code_numbits" is the number of bits of the compressed representation
   of the command and distance prefix codes, and "cmd_code" is an array of
   at least "(*cmd_code_numbits + 7) >> 3" size that contains the compressed
   command and distance prefix codes. If "is_last" is 0, these are also
   updated to represent the updated "cmd_depth" and "cmd_bits".

   REQUIRES: "input_size" is greater than zero, or "is_last" is 1.
   REQUIRES: "input_size" is less or equal to maximal metablock size (1 << 24).
   REQUIRES: All elements in "table[0..table_size-1]" are initialized to zero.
   REQUIRES: "table_size" is an odd (9, 11, 13, 15) power of two
   OUTPUT: maximal copy distance <= |input_size|
   OUTPUT: maximal copy distance <= BROTLI_MAX_BACKWARD_LIMIT(18) */
func compressFragmentFast( []byte,  uint,  bool,  []int,  uint,  []byte,  []uint16,  *uint,  []byte,  *uint,  []byte) {
	var  uint = *
	var  uint = uint(log2FloorNonZero())

	if  == 0 {
		assert()
		writeBits(1, 1, , ) /* islast */
		writeBits(1, 1, , ) /* isempty */
		* = (* + 7) &^ 7
		return
	}

	compressFragmentFastImpl(, , , , , , , , , , )

	/* If output is larger than single uncompressed block, rewrite it. */
	if *- > 31+(<<3) {
		emitUncompressedMetaBlock1(, [:], , , )
	}

	if  {
		writeBits(1, 1, , ) /* islast */
		writeBits(1, 1, , ) /* isempty */
		* = (* + 7) &^ 7
	}
}