history.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. b []byte
  11. huffTree *huff0.Scratch
  12. recentOffsets [3]int
  13. decoders sequenceDecs
  14. windowSize int
  15. maxSize int
  16. error bool
  17. dict *dict
  18. }
  19. // reset will reset the history to initial state of a frame.
  20. // The history must already have been initialized to the desired size.
  21. func (h *history) reset() {
  22. h.b = h.b[:0]
  23. h.error = false
  24. h.recentOffsets = [3]int{1, 4, 8}
  25. if f := h.decoders.litLengths.fse; f != nil && !f.preDefined {
  26. fseDecoderPool.Put(f)
  27. }
  28. if f := h.decoders.offsets.fse; f != nil && !f.preDefined {
  29. fseDecoderPool.Put(f)
  30. }
  31. if f := h.decoders.matchLengths.fse; f != nil && !f.preDefined {
  32. fseDecoderPool.Put(f)
  33. }
  34. h.decoders = sequenceDecs{}
  35. if h.huffTree != nil {
  36. if h.dict == nil || h.dict.litEnc != h.huffTree {
  37. huffDecoderPool.Put(h.huffTree)
  38. }
  39. }
  40. h.huffTree = nil
  41. h.dict = nil
  42. //printf("history created: %+v (l: %d, c: %d)", *h, len(h.b), cap(h.b))
  43. }
  44. func (h *history) setDict(dict *dict) {
  45. if dict == nil {
  46. return
  47. }
  48. h.dict = dict
  49. h.decoders.litLengths = dict.llDec
  50. h.decoders.offsets = dict.ofDec
  51. h.decoders.matchLengths = dict.mlDec
  52. h.recentOffsets = dict.offsets
  53. h.huffTree = dict.litEnc
  54. }
  55. // append bytes to history.
  56. // This function will make sure there is space for it,
  57. // if the buffer has been allocated with enough extra space.
  58. func (h *history) append(b []byte) {
  59. if len(b) >= h.windowSize {
  60. // Discard all history by simply overwriting
  61. h.b = h.b[:h.windowSize]
  62. copy(h.b, b[len(b)-h.windowSize:])
  63. return
  64. }
  65. // If there is space, append it.
  66. if len(b) < cap(h.b)-len(h.b) {
  67. h.b = append(h.b, b...)
  68. return
  69. }
  70. // Move data down so we only have window size left.
  71. // We know we have less than window size in b at this point.
  72. discard := len(b) + len(h.b) - h.windowSize
  73. copy(h.b, h.b[discard:])
  74. h.b = h.b[:h.windowSize]
  75. copy(h.b[h.windowSize-len(b):], b)
  76. }
  77. // append bytes to history without ever discarding anything.
  78. func (h *history) appendKeep(b []byte) {
  79. h.b = append(h.b, b...)
  80. }