Example #1
0
func testReadPostClose(ibs kanzi.InputBitStream) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Printf("Error: %v\n", r.(error).Error())
		}
	}()

	fmt.Printf("\nTrying to read from closed stream\n")
	ibs.ReadBit()
}
Example #2
0
func DecodeAlphabet(ibs kanzi.InputBitStream, alphabet []byte) (int, error) {
	// Read encoding mode from bitstream
	aphabetType := ibs.ReadBit()

	if aphabetType == FULL_ALPHABET {
		mode := ibs.ReadBit()
		var alphabetSize int

		if mode == EIGHT_BIT_ALPHABET {
			alphabetSize = 256
		} else {
			alphabetSize = 128
		}

		// Full alphabet
		for i := 0; i < alphabetSize; i++ {
			alphabet[i] = byte(i)
		}

		return alphabetSize, nil
	}

	alphabetSize := 0
	mode := ibs.ReadBit()

	if mode == BIT_ENCODED_ALPHABET {
		// Decode presence flags
		for i := 0; i < 256; i += 64 {
			val := ibs.ReadBits(64)

			for j := 0; j < 64; j++ {
				if val&(uint64(1)<<uint(j)) != 0 {
					alphabet[alphabetSize] = byte(i + j)
					alphabetSize++
				}
			}
		}
	} else { // DELTA_ENCODED_ALPHABET
		val := int(ibs.ReadBits(6))
		log := uint(1 + ibs.ReadBits(3)) // log(max(diff))
		alphabetSize = val >> 1
		n := 0
		symbol := uint8(0)

		if val&1 == ABSENT_SYMBOLS_MASK {
			for i := 0; i < alphabetSize; i++ {
				next := symbol + byte(ibs.ReadBits(log))

				for symbol < next {
					alphabet[n] = symbol
					symbol++
					n++
				}

				symbol++
			}

			alphabetSize = 256 - alphabetSize

			for n < alphabetSize {
				alphabet[n] = symbol
				n++
				symbol++
			}

		} else {
			for i := 0; i < alphabetSize; i++ {
				symbol += uint8(ibs.ReadBits(log))
				alphabet[i] = symbol
				symbol++
			}
		}
	}

	return alphabetSize, nil
}