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.
8
"github.com/klauspost/compress/huff0"
11
// history contains the information transferred between blocks.
13
// Literal decompression
14
huffTree *huff0.Scratch
16
// Sequence decompression
23
// ignoreBuffer is meant to ignore a number of bytes
24
// when checking for matches in history
28
allocFrameBuffer int // needed?
33
// reset will reset the history to initial state of a frame.
34
// The history must already have been initialized to the desired size.
35
func (h *history) reset() {
39
h.recentOffsets = [3]int{1, 4, 8}
40
if f := h.decoders.litLengths.fse; f != nil && !f.preDefined {
43
if f := h.decoders.offsets.fse; f != nil && !f.preDefined {
46
if f := h.decoders.matchLengths.fse; f != nil && !f.preDefined {
49
h.decoders = sequenceDecs{br: h.decoders.br}
50
if h.huffTree != nil {
51
if h.dict == nil || h.dict.litEnc != h.huffTree {
52
huffDecoderPool.Put(h.huffTree)
57
//printf("history created: %+v (l: %d, c: %d)", *h, len(h.b), cap(h.b))
60
func (h *history) setDict(dict *dict) {
65
h.decoders.litLengths = dict.llDec
66
h.decoders.offsets = dict.ofDec
67
h.decoders.matchLengths = dict.mlDec
68
h.decoders.dict = dict.content
69
h.recentOffsets = dict.offsets
70
h.huffTree = dict.litEnc
73
// append bytes to history.
74
// This function will make sure there is space for it,
75
// if the buffer has been allocated with enough extra space.
76
func (h *history) append(b []byte) {
77
if len(b) >= h.windowSize {
78
// Discard all history by simply overwriting
79
h.b = h.b[:h.windowSize]
80
copy(h.b, b[len(b)-h.windowSize:])
84
// If there is space, append it.
85
if len(b) < cap(h.b)-len(h.b) {
86
h.b = append(h.b, b...)
90
// Move data down so we only have window size left.
91
// We know we have less than window size in b at this point.
92
discard := len(b) + len(h.b) - h.windowSize
93
copy(h.b, h.b[discard:])
94
h.b = h.b[:h.windowSize]
95
copy(h.b[h.windowSize-len(b):], b)
98
// ensureBlock will ensure there is space for at least one block...
99
func (h *history) ensureBlock() {
100
if cap(h.b) < h.allocFrameBuffer {
101
h.b = make([]byte, 0, h.allocFrameBuffer)
105
avail := cap(h.b) - len(h.b)
106
if avail >= h.windowSize || avail > maxCompressedBlockSize {
109
// Move data down so we only have window size left.
110
// We know we have less than window size in b at this point.
111
discard := len(h.b) - h.windowSize
112
copy(h.b, h.b[discard:])
113
h.b = h.b[:h.windowSize]
116
// append bytes to history without ever discarding anything.
117
func (h *history) appendKeep(b []byte) {
118
h.b = append(h.b, b...)