history.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2019+ Klaus Post. All rights reserved.
  2. // License information can be found in the LICENSE file.
  3. // Based on work by Yann Collet, released under BSD License.
  4. package zstd
  5. import (
  6. "github.com/klauspost/compress/huff0"
  7. )
  8. // history contains the information transferred between blocks.
  9. type history struct {
  10. // Literal decompression
  11. huffTree *huff0.Scratch
  12. // Sequence decompression
  13. decoders sequenceDecs
  14. recentOffsets [3]int
  15. // History buffer...
  16. b []byte
  17. // ignoreBuffer is meant to ignore a number of bytes
  18. // when checking for matches in history
  19. ignoreBuffer int
  20. windowSize int
  21. allocFrameBuffer int // needed?
  22. error bool
  23. dict *dict
  24. }
  25. // reset will reset the history to initial state of a frame.
  26. // The history must already have been initialized to the desired size.
  27. func (h *history) reset() {
  28. h.b = h.b[:0]
  29. h.ignoreBuffer = 0
  30. h.error = false
  31. h.recentOffsets = [3]int{1, 4, 8}
  32. h.decoders.freeDecoders()
  33. h.decoders = sequenceDecs{br: h.decoders.br}
  34. h.freeHuffDecoder()
  35. h.huffTree = nil
  36. h.dict = nil
  37. //printf("history created: %+v (l: %d, c: %d)", *h, len(h.b), cap(h.b))
  38. }
  39. func (h *history) freeHuffDecoder() {
  40. if h.huffTree != nil {
  41. if h.dict == nil || h.dict.litEnc != h.huffTree {
  42. huffDecoderPool.Put(h.huffTree)
  43. h.huffTree = nil
  44. }
  45. }
  46. }
  47. func (h *history) setDict(dict *dict) {
  48. if dict == nil {
  49. return
  50. }
  51. h.dict = dict
  52. h.decoders.litLengths = dict.llDec
  53. h.decoders.offsets = dict.ofDec
  54. h.decoders.matchLengths = dict.mlDec
  55. h.decoders.dict = dict.content
  56. h.recentOffsets = dict.offsets
  57. h.huffTree = dict.litEnc
  58. }
  59. // append bytes to history.
  60. // This function will make sure there is space for it,
  61. // if the buffer has been allocated with enough extra space.
  62. func (h *history) append(b []byte) {
  63. if len(b) >= h.windowSize {
  64. // Discard all history by simply overwriting
  65. h.b = h.b[:h.windowSize]
  66. copy(h.b, b[len(b)-h.windowSize:])
  67. return
  68. }
  69. // If there is space, append it.
  70. if len(b) < cap(h.b)-len(h.b) {
  71. h.b = append(h.b, b...)
  72. return
  73. }
  74. // Move data down so we only have window size left.
  75. // We know we have less than window size in b at this point.
  76. discard := len(b) + len(h.b) - h.windowSize
  77. copy(h.b, h.b[discard:])
  78. h.b = h.b[:h.windowSize]
  79. copy(h.b[h.windowSize-len(b):], b)
  80. }
  81. // ensureBlock will ensure there is space for at least one block...
  82. func (h *history) ensureBlock() {
  83. if cap(h.b) < h.allocFrameBuffer {
  84. h.b = make([]byte, 0, h.allocFrameBuffer)
  85. return
  86. }
  87. avail := cap(h.b) - len(h.b)
  88. if avail >= h.windowSize || avail > maxCompressedBlockSize {
  89. return
  90. }
  91. // Move data down so we only have window size left.
  92. // We know we have less than window size in b at this point.
  93. discard := len(h.b) - h.windowSize
  94. copy(h.b, h.b[discard:])
  95. h.b = h.b[:h.windowSize]
  96. }
  97. // append bytes to history without ever discarding anything.
  98. func (h *history) appendKeep(b []byte) {
  99. h.b = append(h.b, b...)
  100. }