Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

120 řádky
3.0 KiB

  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. if f := h.decoders.litLengths.fse; f != nil && !f.preDefined {
  33. fseDecoderPool.Put(f)
  34. }
  35. if f := h.decoders.offsets.fse; f != nil && !f.preDefined {
  36. fseDecoderPool.Put(f)
  37. }
  38. if f := h.decoders.matchLengths.fse; f != nil && !f.preDefined {
  39. fseDecoderPool.Put(f)
  40. }
  41. h.decoders = sequenceDecs{br: h.decoders.br}
  42. if h.huffTree != nil {
  43. if h.dict == nil || h.dict.litEnc != h.huffTree {
  44. huffDecoderPool.Put(h.huffTree)
  45. }
  46. }
  47. h.huffTree = nil
  48. h.dict = nil
  49. //printf("history created: %+v (l: %d, c: %d)", *h, len(h.b), cap(h.b))
  50. }
  51. func (h *history) setDict(dict *dict) {
  52. if dict == nil {
  53. return
  54. }
  55. h.dict = dict
  56. h.decoders.litLengths = dict.llDec
  57. h.decoders.offsets = dict.ofDec
  58. h.decoders.matchLengths = dict.mlDec
  59. h.decoders.dict = dict.content
  60. h.recentOffsets = dict.offsets
  61. h.huffTree = dict.litEnc
  62. }
  63. // append bytes to history.
  64. // This function will make sure there is space for it,
  65. // if the buffer has been allocated with enough extra space.
  66. func (h *history) append(b []byte) {
  67. if len(b) >= h.windowSize {
  68. // Discard all history by simply overwriting
  69. h.b = h.b[:h.windowSize]
  70. copy(h.b, b[len(b)-h.windowSize:])
  71. return
  72. }
  73. // If there is space, append it.
  74. if len(b) < cap(h.b)-len(h.b) {
  75. h.b = append(h.b, b...)
  76. return
  77. }
  78. // Move data down so we only have window size left.
  79. // We know we have less than window size in b at this point.
  80. discard := len(b) + len(h.b) - h.windowSize
  81. copy(h.b, h.b[discard:])
  82. h.b = h.b[:h.windowSize]
  83. copy(h.b[h.windowSize-len(b):], b)
  84. }
  85. // ensureBlock will ensure there is space for at least one block...
  86. func (h *history) ensureBlock() {
  87. if cap(h.b) < h.allocFrameBuffer {
  88. h.b = make([]byte, 0, h.allocFrameBuffer)
  89. return
  90. }
  91. avail := cap(h.b) - len(h.b)
  92. if avail >= h.windowSize || avail > maxCompressedBlockSize {
  93. return
  94. }
  95. // Move data down so we only have window size left.
  96. // We know we have less than window size in b at this point.
  97. discard := len(h.b) - h.windowSize
  98. copy(h.b, h.b[discard:])
  99. h.b = h.b[:h.windowSize]
  100. }
  101. // append bytes to history without ever discarding anything.
  102. func (h *history) appendKeep(b []byte) {
  103. h.b = append(h.b, b...)
  104. }