lengthcodec.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. import "errors"
  6. // maxPosBits defines the number of bits of the position value that are used to
  7. // to compute the posState value. The value is used to select the tree codec
  8. // for length encoding and decoding.
  9. const maxPosBits = 4
  10. // minMatchLen and maxMatchLen give the minimum and maximum values for
  11. // encoding and decoding length values. minMatchLen is also used as base
  12. // for the encoded length values.
  13. const (
  14. minMatchLen = 2
  15. maxMatchLen = minMatchLen + 16 + 256 - 1
  16. )
  17. // lengthCodec support the encoding of the length value.
  18. type lengthCodec struct {
  19. choice [2]prob
  20. low [1 << maxPosBits]treeCodec
  21. mid [1 << maxPosBits]treeCodec
  22. high treeCodec
  23. }
  24. // deepcopy initializes the lc value as deep copy of the source value.
  25. func (lc *lengthCodec) deepcopy(src *lengthCodec) {
  26. if lc == src {
  27. return
  28. }
  29. lc.choice = src.choice
  30. for i := range lc.low {
  31. lc.low[i].deepcopy(&src.low[i])
  32. }
  33. for i := range lc.mid {
  34. lc.mid[i].deepcopy(&src.mid[i])
  35. }
  36. lc.high.deepcopy(&src.high)
  37. }
  38. // init initializes a new length codec.
  39. func (lc *lengthCodec) init() {
  40. for i := range lc.choice {
  41. lc.choice[i] = probInit
  42. }
  43. for i := range lc.low {
  44. lc.low[i] = makeTreeCodec(3)
  45. }
  46. for i := range lc.mid {
  47. lc.mid[i] = makeTreeCodec(3)
  48. }
  49. lc.high = makeTreeCodec(8)
  50. }
  51. // Encode encodes the length offset. The length offset l can be compute by
  52. // subtracting minMatchLen (2) from the actual length.
  53. //
  54. // l = length - minMatchLen
  55. func (lc *lengthCodec) Encode(e *rangeEncoder, l uint32, posState uint32,
  56. ) (err error) {
  57. if l > maxMatchLen-minMatchLen {
  58. return errors.New("lengthCodec.Encode: l out of range")
  59. }
  60. if l < 8 {
  61. if err = lc.choice[0].Encode(e, 0); err != nil {
  62. return
  63. }
  64. return lc.low[posState].Encode(e, l)
  65. }
  66. if err = lc.choice[0].Encode(e, 1); err != nil {
  67. return
  68. }
  69. if l < 16 {
  70. if err = lc.choice[1].Encode(e, 0); err != nil {
  71. return
  72. }
  73. return lc.mid[posState].Encode(e, l-8)
  74. }
  75. if err = lc.choice[1].Encode(e, 1); err != nil {
  76. return
  77. }
  78. if err = lc.high.Encode(e, l-16); err != nil {
  79. return
  80. }
  81. return nil
  82. }
  83. // Decode reads the length offset. Add minMatchLen to compute the actual length
  84. // to the length offset l.
  85. func (lc *lengthCodec) Decode(d *rangeDecoder, posState uint32,
  86. ) (l uint32, err error) {
  87. var b uint32
  88. if b, err = lc.choice[0].Decode(d); err != nil {
  89. return
  90. }
  91. if b == 0 {
  92. l, err = lc.low[posState].Decode(d)
  93. return
  94. }
  95. if b, err = lc.choice[1].Decode(d); err != nil {
  96. return
  97. }
  98. if b == 0 {
  99. l, err = lc.mid[posState].Decode(d)
  100. l += 8
  101. return
  102. }
  103. l, err = lc.high.Decode(d)
  104. l += 16
  105. return
  106. }