| 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 |
"errors" |
| 9 |
"fmt" |
| 10 |
"math/bits" |
| 11 |
"runtime" |
| 12 |
) |
| 13 |
|
| 14 |
// DOption is an option for creating a decoder. |
| 15 |
type DOption func(*decoderOptions) error |
| 16 |
|
| 17 |
// options retains accumulated state of multiple options. |
| 18 |
type decoderOptions struct { |
| 19 |
lowMem bool |
| 20 |
concurrent int |
| 21 |
maxDecodedSize uint64 |
| 22 |
maxWindowSize uint64 |
| 23 |
dicts map[uint32]*dict |
| 24 |
ignoreChecksum bool |
| 25 |
limitToCap bool |
| 26 |
decodeBufsBelow int |
| 27 |
resetOpt bool |
| 28 |
} |
| 29 |
|
| 30 |
func (o *decoderOptions) setDefault() { |
| 31 |
*o = decoderOptions{ |
| 32 |
// use less ram: true for now, but may change. |
| 33 |
lowMem: true, |
| 34 |
concurrent: runtime.GOMAXPROCS(0), |
| 35 |
maxWindowSize: MaxWindowSize, |
| 36 |
decodeBufsBelow: 128 << 10, |
| 37 |
} |
| 38 |
if o.concurrent > 4 { |
| 39 |
o.concurrent = 4 |
| 40 |
} |
| 41 |
o.maxDecodedSize = 64 << 30 |
| 42 |
} |
| 43 |
|
| 44 |
// WithDecoderLowmem will set whether to use a lower amount of memory, |
| 45 |
// but possibly have to allocate more while running. |
| 46 |
// Cannot be changed with ResetWithOptions. |
| 47 |
func WithDecoderLowmem(b bool) DOption { |
| 48 |
return func(o *decoderOptions) error { |
| 49 |
if o.resetOpt && b != o.lowMem { |
| 50 |
return errors.New("WithDecoderLowmem cannot be changed on Reset") |
| 51 |
} |
| 52 |
o.lowMem = b |
| 53 |
return nil |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// WithDecoderConcurrency sets the number of created decoders. |
| 58 |
// When decoding block with DecodeAll, this will limit the number |
| 59 |
// of possible concurrently running decodes. |
| 60 |
// When decoding streams, this will limit the number of |
| 61 |
// inflight blocks. |
| 62 |
// When decoding streams and setting maximum to 1, |
| 63 |
// no async decoding will be done. |
| 64 |
// The value supplied must be at least 0. |
| 65 |
// When a value of 0 is provided GOMAXPROCS will be used. |
| 66 |
// By default this will be set to 4 or GOMAXPROCS, whatever is lower. |
| 67 |
// Cannot be changed with ResetWithOptions. |
| 68 |
func WithDecoderConcurrency(n int) DOption { |
| 69 |
return func(o *decoderOptions) error { |
| 70 |
if n < 0 { |
| 71 |
return errors.New("concurrency must be at least 0") |
| 72 |
} |
| 73 |
newVal := n |
| 74 |
if n == 0 { |
| 75 |
newVal = runtime.GOMAXPROCS(0) |
| 76 |
} |
| 77 |
if o.resetOpt && newVal != o.concurrent { |
| 78 |
return errors.New("WithDecoderConcurrency cannot be changed on Reset") |
| 79 |
} |
| 80 |
o.concurrent = newVal |
| 81 |
return nil |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// WithDecoderMaxMemory allows to set a maximum decoded size for in-memory |
| 86 |
// non-streaming operations or maximum window size for streaming operations. |
| 87 |
// This can be used to control memory usage of potentially hostile content. |
| 88 |
// Maximum is 1 << 63 bytes. Default is 64GiB. |
| 89 |
// Can be changed with ResetWithOptions. |
| 90 |
func WithDecoderMaxMemory(n uint64) DOption { |
| 91 |
return func(o *decoderOptions) error { |
| 92 |
if n == 0 { |
| 93 |
return errors.New("WithDecoderMaxMemory must be at least 1") |
| 94 |
} |
| 95 |
if n > 1<<63 { |
| 96 |
return errors.New("WithDecoderMaxmemory must be less than 1 << 63") |
| 97 |
} |
| 98 |
o.maxDecodedSize = n |
| 99 |
return nil |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// WithDecoderDicts allows to register one or more dictionaries for the decoder. |
| 104 |
// |
| 105 |
// Each slice in dict must be in the [dictionary format] produced by |
| 106 |
// "zstd --train" from the Zstandard reference implementation. |
| 107 |
// |
| 108 |
// If several dictionaries with the same ID are provided, the last one will be used. |
| 109 |
// Can be changed with ResetWithOptions. |
| 110 |
// |
| 111 |
// [dictionary format]: https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#dictionary-format |
| 112 |
func WithDecoderDicts(dicts ...[]byte) DOption { |
| 113 |
return func(o *decoderOptions) error { |
| 114 |
if o.dicts == nil { |
| 115 |
o.dicts = make(map[uint32]*dict) |
| 116 |
} |
| 117 |
for _, b := range dicts { |
| 118 |
d, err := loadDict(b) |
| 119 |
if err != nil { |
| 120 |
return err |
| 121 |
} |
| 122 |
o.dicts[d.id] = d |
| 123 |
} |
| 124 |
return nil |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// WithDecoderDictRaw registers a dictionary that may be used by the decoder. |
| 129 |
// The slice content can be arbitrary data. |
| 130 |
// Can be changed with ResetWithOptions. |
| 131 |
func WithDecoderDictRaw(id uint32, content []byte) DOption { |
| 132 |
return func(o *decoderOptions) error { |
| 133 |
if bits.UintSize > 32 && uint(len(content)) > dictMaxLength { |
| 134 |
return fmt.Errorf("dictionary of size %d > 2GiB too large", len(content)) |
| 135 |
} |
| 136 |
if o.dicts == nil { |
| 137 |
o.dicts = make(map[uint32]*dict) |
| 138 |
} |
| 139 |
o.dicts[id] = &dict{id: id, content: content, offsets: [3]int{1, 4, 8}} |
| 140 |
return nil |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
// WithDecoderMaxWindow allows to set a maximum window size for decodes. |
| 145 |
// This allows rejecting packets that will cause big memory usage. |
| 146 |
// The Decoder will likely allocate more memory based on the WithDecoderLowmem setting. |
| 147 |
// If WithDecoderMaxMemory is set to a lower value, that will be used. |
| 148 |
// Default is 512MB, Maximum is ~3.75 TB as per zstandard spec. |
| 149 |
// Can be changed with ResetWithOptions. |
| 150 |
func WithDecoderMaxWindow(size uint64) DOption { |
| 151 |
return func(o *decoderOptions) error { |
| 152 |
if size < MinWindowSize { |
| 153 |
return errors.New("WithMaxWindowSize must be at least 1KB, 1024 bytes") |
| 154 |
} |
| 155 |
if size > (1<<41)+7*(1<<38) { |
| 156 |
return errors.New("WithMaxWindowSize must be less than (1<<41) + 7*(1<<38) ~ 3.75TB") |
| 157 |
} |
| 158 |
o.maxWindowSize = size |
| 159 |
return nil |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// WithDecodeAllCapLimit will limit DecodeAll to decoding cap(dst)-len(dst) bytes, |
| 164 |
// or any size set in WithDecoderMaxMemory. |
| 165 |
// This can be used to limit decoding to a specific maximum output size. |
| 166 |
// Disabled by default. |
| 167 |
// Can be changed with ResetWithOptions. |
| 168 |
func WithDecodeAllCapLimit(b bool) DOption { |
| 169 |
return func(o *decoderOptions) error { |
| 170 |
o.limitToCap = b |
| 171 |
return nil |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
// WithDecodeBuffersBelow will fully decode readers that have a |
| 176 |
// `Bytes() []byte` and `Len() int` interface similar to bytes.Buffer. |
| 177 |
// This typically uses less allocations but will have the full decompressed object in memory. |
| 178 |
// Note that DecodeAllCapLimit will disable this, as well as giving a size of 0 or less. |
| 179 |
// Default is 128KiB. |
| 180 |
// Cannot be changed with ResetWithOptions. |
| 181 |
func WithDecodeBuffersBelow(size int) DOption { |
| 182 |
return func(o *decoderOptions) error { |
| 183 |
if o.resetOpt && size != o.decodeBufsBelow { |
| 184 |
return errors.New("WithDecodeBuffersBelow cannot be changed on Reset") |
| 185 |
} |
| 186 |
o.decodeBufsBelow = size |
| 187 |
return nil |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// IgnoreChecksum allows to forcibly ignore checksum checking. |
| 192 |
// Can be changed with ResetWithOptions. |
| 193 |
func IgnoreChecksum(b bool) DOption { |
| 194 |
return func(o *decoderOptions) error { |
| 195 |
o.ignoreChecksum = b |
| 196 |
return nil |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// WithDecoderDictDelete removes dictionaries by ID. |
| 201 |
// If no ids are passed, all dictionaries are deleted. |
| 202 |
// Should be used with ResetWithOptions. |
| 203 |
func WithDecoderDictDelete(ids ...uint32) DOption { |
| 204 |
return func(o *decoderOptions) error { |
| 205 |
if len(ids) == 0 { |
| 206 |
clear(o.dicts) |
| 207 |
} |
| 208 |
for _, id := range ids { |
| 209 |
delete(o.dicts, id) |
| 210 |
} |
| 211 |
return nil |
| 212 |
} |
| 213 |
} |