Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

96 rader
2.8 KiB

  1. // Copyright 2018 Klaus Post. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Based on work Copyright (c) 2013, Yann Collet, released under BSD License.
  5. package huff0
  6. // bitWriter will write bits.
  7. // First bit will be LSB of the first byte of output.
  8. type bitWriter struct {
  9. bitContainer uint64
  10. nBits uint8
  11. out []byte
  12. }
  13. // bitMask16 is bitmasks. Has extra to avoid bounds check.
  14. var bitMask16 = [32]uint16{
  15. 0, 1, 3, 7, 0xF, 0x1F,
  16. 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF,
  17. 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0xFFFF,
  18. 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
  19. 0xFFFF, 0xFFFF} /* up to 16 bits */
  20. // addBits16Clean will add up to 16 bits. value may not contain more set bits than indicated.
  21. // It will not check if there is space for them, so the caller must ensure that it has flushed recently.
  22. func (b *bitWriter) addBits16Clean(value uint16, bits uint8) {
  23. b.bitContainer |= uint64(value) << (b.nBits & 63)
  24. b.nBits += bits
  25. }
  26. // encSymbol will add up to 16 bits. value may not contain more set bits than indicated.
  27. // It will not check if there is space for them, so the caller must ensure that it has flushed recently.
  28. func (b *bitWriter) encSymbol(ct cTable, symbol byte) {
  29. enc := ct[symbol]
  30. b.bitContainer |= uint64(enc.val) << (b.nBits & 63)
  31. if false {
  32. if enc.nBits == 0 {
  33. panic("nbits 0")
  34. }
  35. }
  36. b.nBits += enc.nBits
  37. }
  38. // encTwoSymbols will add up to 32 bits. value may not contain more set bits than indicated.
  39. // It will not check if there is space for them, so the caller must ensure that it has flushed recently.
  40. func (b *bitWriter) encTwoSymbols(ct cTable, av, bv byte) {
  41. encA := ct[av]
  42. encB := ct[bv]
  43. sh := b.nBits & 63
  44. combined := uint64(encA.val) | (uint64(encB.val) << (encA.nBits & 63))
  45. b.bitContainer |= combined << sh
  46. if false {
  47. if encA.nBits == 0 {
  48. panic("nbitsA 0")
  49. }
  50. if encB.nBits == 0 {
  51. panic("nbitsB 0")
  52. }
  53. }
  54. b.nBits += encA.nBits + encB.nBits
  55. }
  56. // flush32 will flush out, so there are at least 32 bits available for writing.
  57. func (b *bitWriter) flush32() {
  58. if b.nBits < 32 {
  59. return
  60. }
  61. b.out = append(b.out,
  62. byte(b.bitContainer),
  63. byte(b.bitContainer>>8),
  64. byte(b.bitContainer>>16),
  65. byte(b.bitContainer>>24))
  66. b.nBits -= 32
  67. b.bitContainer >>= 32
  68. }
  69. // flushAlign will flush remaining full bytes and align to next byte boundary.
  70. func (b *bitWriter) flushAlign() {
  71. nbBytes := (b.nBits + 7) >> 3
  72. for i := uint8(0); i < nbBytes; i++ {
  73. b.out = append(b.out, byte(b.bitContainer>>(i*8)))
  74. }
  75. b.nBits = 0
  76. b.bitContainer = 0
  77. }
  78. // close will write the alignment bit and write the final byte(s)
  79. // to the output.
  80. func (b *bitWriter) close() error {
  81. // End mark
  82. b.addBits16Clean(1, 1)
  83. // flush until next byte.
  84. b.flushAlign()
  85. return nil
  86. }