cubefs

Форк
0
119 строк · 3.0 Кб
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

5
package zstd
6

7
import (
8
	"github.com/klauspost/compress/huff0"
9
)
10

11
// history contains the information transferred between blocks.
12
type history struct {
13
	// Literal decompression
14
	huffTree *huff0.Scratch
15

16
	// Sequence decompression
17
	decoders      sequenceDecs
18
	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
31
}
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.
35
func (h *history) reset() {
36
	h.b = h.b[:0]
37
	h.ignoreBuffer = 0
38
	h.error = false
39
	h.recentOffsets = [3]int{1, 4, 8}
40
	if f := h.decoders.litLengths.fse; f != nil && !f.preDefined {
41
		fseDecoderPool.Put(f)
42
	}
43
	if f := h.decoders.offsets.fse; f != nil && !f.preDefined {
44
		fseDecoderPool.Put(f)
45
	}
46
	if f := h.decoders.matchLengths.fse; f != nil && !f.preDefined {
47
		fseDecoderPool.Put(f)
48
	}
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)
53
		}
54
	}
55
	h.huffTree = nil
56
	h.dict = nil
57
	//printf("history created: %+v (l: %d, c: %d)", *h, len(h.b), cap(h.b))
58
}
59

60
func (h *history) setDict(dict *dict) {
61
	if dict == nil {
62
		return
63
	}
64
	h.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
71
}
72

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:])
81
		return
82
	}
83

84
	// If there is space, append it.
85
	if len(b) < cap(h.b)-len(h.b) {
86
		h.b = append(h.b, b...)
87
		return
88
	}
89

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)
96
}
97

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)
102
		return
103
	}
104

105
	avail := cap(h.b) - len(h.b)
106
	if avail >= h.windowSize || avail > maxCompressedBlockSize {
107
		return
108
	}
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]
114
}
115

116
// append bytes to history without ever discarding anything.
117
func (h *history) appendKeep(b []byte) {
118
	h.b = append(h.b, b...)
119
}
120

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.