blob: 09164856d22247e17a9f0fe7040b81b81aebcb55 [file] [log] [blame]
khenaidood948f772021-08-11 17:49:24 -04001// 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
5package zstd
6
7import (
8 "github.com/klauspost/compress/huff0"
9)
10
11// history contains the information transferred between blocks.
12type history struct {
Akash Reddy Kankanalacf045372025-06-10 14:11:24 +053013 // Literal decompression
14 huffTree *huff0.Scratch
15
16 // Sequence decompression
khenaidood948f772021-08-11 17:49:24 -040017 decoders sequenceDecs
Akash Reddy Kankanalacf045372025-06-10 14:11:24 +053018 recentOffsets [3]int
19
20 // History buffer...
21 b []byte
22
23 // ignoreBuffer is meant to ignore a number of bytes
24 // when checking for matches in history
25 ignoreBuffer int
26
27 windowSize int
28 allocFrameBuffer int // needed?
29 error bool
30 dict *dict
khenaidood948f772021-08-11 17:49:24 -040031}
32
33// reset will reset the history to initial state of a frame.
34// The history must already have been initialized to the desired size.
35func (h *history) reset() {
36 h.b = h.b[:0]
Akash Reddy Kankanalacf045372025-06-10 14:11:24 +053037 h.ignoreBuffer = 0
khenaidood948f772021-08-11 17:49:24 -040038 h.error = false
39 h.recentOffsets = [3]int{1, 4, 8}
Abhay Kumara2ae5992025-11-10 14:02:24 +000040 h.decoders.freeDecoders()
Akash Reddy Kankanalacf045372025-06-10 14:11:24 +053041 h.decoders = sequenceDecs{br: h.decoders.br}
Abhay Kumara2ae5992025-11-10 14:02:24 +000042 h.freeHuffDecoder()
khenaidood948f772021-08-11 17:49:24 -040043 h.huffTree = nil
44 h.dict = nil
45 //printf("history created: %+v (l: %d, c: %d)", *h, len(h.b), cap(h.b))
46}
47
Abhay Kumara2ae5992025-11-10 14:02:24 +000048func (h *history) freeHuffDecoder() {
49 if h.huffTree != nil {
50 if h.dict == nil || h.dict.litEnc != h.huffTree {
51 huffDecoderPool.Put(h.huffTree)
52 h.huffTree = nil
53 }
54 }
55}
56
khenaidood948f772021-08-11 17:49:24 -040057func (h *history) setDict(dict *dict) {
58 if dict == nil {
59 return
60 }
61 h.dict = dict
62 h.decoders.litLengths = dict.llDec
63 h.decoders.offsets = dict.ofDec
64 h.decoders.matchLengths = dict.mlDec
Akash Reddy Kankanalacf045372025-06-10 14:11:24 +053065 h.decoders.dict = dict.content
khenaidood948f772021-08-11 17:49:24 -040066 h.recentOffsets = dict.offsets
67 h.huffTree = dict.litEnc
68}
69
70// append bytes to history.
71// This function will make sure there is space for it,
72// if the buffer has been allocated with enough extra space.
73func (h *history) append(b []byte) {
74 if len(b) >= h.windowSize {
75 // Discard all history by simply overwriting
76 h.b = h.b[:h.windowSize]
77 copy(h.b, b[len(b)-h.windowSize:])
78 return
79 }
80
81 // If there is space, append it.
82 if len(b) < cap(h.b)-len(h.b) {
83 h.b = append(h.b, b...)
84 return
85 }
86
87 // Move data down so we only have window size left.
88 // We know we have less than window size in b at this point.
89 discard := len(b) + len(h.b) - h.windowSize
90 copy(h.b, h.b[discard:])
91 h.b = h.b[:h.windowSize]
92 copy(h.b[h.windowSize-len(b):], b)
93}
94
Akash Reddy Kankanalacf045372025-06-10 14:11:24 +053095// ensureBlock will ensure there is space for at least one block...
96func (h *history) ensureBlock() {
97 if cap(h.b) < h.allocFrameBuffer {
98 h.b = make([]byte, 0, h.allocFrameBuffer)
99 return
100 }
101
102 avail := cap(h.b) - len(h.b)
103 if avail >= h.windowSize || avail > maxCompressedBlockSize {
104 return
105 }
106 // Move data down so we only have window size left.
107 // We know we have less than window size in b at this point.
108 discard := len(h.b) - h.windowSize
109 copy(h.b, h.b[discard:])
110 h.b = h.b[:h.windowSize]
111}
112
khenaidood948f772021-08-11 17:49:24 -0400113// append bytes to history without ever discarding anything.
114func (h *history) appendKeep(b []byte) {
115 h.b = append(h.b, b...)
116}