literalcodec.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2014-2022 Ulrich Kunitz. 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. package lzma
  5. // literalCodec supports the encoding of literal. It provides 768 probability
  6. // values per literal state. The upper 512 probabilities are used with the
  7. // context of a match bit.
  8. type literalCodec struct {
  9. probs []prob
  10. }
  11. // deepcopy initializes literal codec c as a deep copy of the source.
  12. func (c *literalCodec) deepcopy(src *literalCodec) {
  13. if c == src {
  14. return
  15. }
  16. c.probs = make([]prob, len(src.probs))
  17. copy(c.probs, src.probs)
  18. }
  19. // init initializes the literal codec.
  20. func (c *literalCodec) init(lc, lp int) {
  21. switch {
  22. case !(minLC <= lc && lc <= maxLC):
  23. panic("lc out of range")
  24. case !(minLP <= lp && lp <= maxLP):
  25. panic("lp out of range")
  26. }
  27. c.probs = make([]prob, 0x300<<uint(lc+lp))
  28. for i := range c.probs {
  29. c.probs[i] = probInit
  30. }
  31. }
  32. // Encode encodes the byte s using a range encoder as well as the current LZMA
  33. // encoder state, a match byte and the literal state.
  34. func (c *literalCodec) Encode(e *rangeEncoder, s byte,
  35. state uint32, match byte, litState uint32,
  36. ) (err error) {
  37. k := litState * 0x300
  38. probs := c.probs[k : k+0x300]
  39. symbol := uint32(1)
  40. r := uint32(s)
  41. if state >= 7 {
  42. m := uint32(match)
  43. for {
  44. matchBit := (m >> 7) & 1
  45. m <<= 1
  46. bit := (r >> 7) & 1
  47. r <<= 1
  48. i := ((1 + matchBit) << 8) | symbol
  49. if err = probs[i].Encode(e, bit); err != nil {
  50. return
  51. }
  52. symbol = (symbol << 1) | bit
  53. if matchBit != bit {
  54. break
  55. }
  56. if symbol >= 0x100 {
  57. break
  58. }
  59. }
  60. }
  61. for symbol < 0x100 {
  62. bit := (r >> 7) & 1
  63. r <<= 1
  64. if err = probs[symbol].Encode(e, bit); err != nil {
  65. return
  66. }
  67. symbol = (symbol << 1) | bit
  68. }
  69. return nil
  70. }
  71. // Decode decodes a literal byte using the range decoder as well as the LZMA
  72. // state, a match byte, and the literal state.
  73. func (c *literalCodec) Decode(d *rangeDecoder,
  74. state uint32, match byte, litState uint32,
  75. ) (s byte, err error) {
  76. k := litState * 0x300
  77. probs := c.probs[k : k+0x300]
  78. symbol := uint32(1)
  79. if state >= 7 {
  80. m := uint32(match)
  81. for {
  82. matchBit := (m >> 7) & 1
  83. m <<= 1
  84. i := ((1 + matchBit) << 8) | symbol
  85. bit, err := d.DecodeBit(&probs[i])
  86. if err != nil {
  87. return 0, err
  88. }
  89. symbol = (symbol << 1) | bit
  90. if matchBit != bit {
  91. break
  92. }
  93. if symbol >= 0x100 {
  94. break
  95. }
  96. }
  97. }
  98. for symbol < 0x100 {
  99. bit, err := d.DecodeBit(&probs[symbol])
  100. if err != nil {
  101. return 0, err
  102. }
  103. symbol = (symbol << 1) | bit
  104. }
  105. s = byte(symbol - 0x100)
  106. return s, nil
  107. }
  108. // minLC and maxLC define the range for LC values.
  109. const (
  110. minLC = 0
  111. maxLC = 8
  112. )
  113. // minLC and maxLC define the range for LP values.
  114. const (
  115. minLP = 0
  116. maxLP = 4
  117. )