kesavand | c71914f | 2022-03-25 11:19:03 +0530 | [diff] [blame^] | 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 | "runtime" |
| 10 | ) |
| 11 | |
| 12 | // DOption is an option for creating a decoder. |
| 13 | type DOption func(*decoderOptions) error |
| 14 | |
| 15 | // options retains accumulated state of multiple options. |
| 16 | type decoderOptions struct { |
| 17 | lowMem bool |
| 18 | concurrent int |
| 19 | maxDecodedSize uint64 |
| 20 | maxWindowSize uint64 |
| 21 | dicts []dict |
| 22 | } |
| 23 | |
| 24 | func (o *decoderOptions) setDefault() { |
| 25 | *o = decoderOptions{ |
| 26 | // use less ram: true for now, but may change. |
| 27 | lowMem: true, |
| 28 | concurrent: runtime.GOMAXPROCS(0), |
| 29 | maxWindowSize: MaxWindowSize, |
| 30 | } |
| 31 | o.maxDecodedSize = 1 << 63 |
| 32 | } |
| 33 | |
| 34 | // WithDecoderLowmem will set whether to use a lower amount of memory, |
| 35 | // but possibly have to allocate more while running. |
| 36 | func WithDecoderLowmem(b bool) DOption { |
| 37 | return func(o *decoderOptions) error { o.lowMem = b; return nil } |
| 38 | } |
| 39 | |
| 40 | // WithDecoderConcurrency will set the concurrency, |
| 41 | // meaning the maximum number of decoders to run concurrently. |
| 42 | // The value supplied must be at least 1. |
| 43 | // By default this will be set to GOMAXPROCS. |
| 44 | func WithDecoderConcurrency(n int) DOption { |
| 45 | return func(o *decoderOptions) error { |
| 46 | if n <= 0 { |
| 47 | return errors.New("concurrency must be at least 1") |
| 48 | } |
| 49 | o.concurrent = n |
| 50 | return nil |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // WithDecoderMaxMemory allows to set a maximum decoded size for in-memory |
| 55 | // non-streaming operations or maximum window size for streaming operations. |
| 56 | // This can be used to control memory usage of potentially hostile content. |
| 57 | // Maximum and default is 1 << 63 bytes. |
| 58 | func WithDecoderMaxMemory(n uint64) DOption { |
| 59 | return func(o *decoderOptions) error { |
| 60 | if n == 0 { |
| 61 | return errors.New("WithDecoderMaxMemory must be at least 1") |
| 62 | } |
| 63 | if n > 1<<63 { |
| 64 | return errors.New("WithDecoderMaxmemory must be less than 1 << 63") |
| 65 | } |
| 66 | o.maxDecodedSize = n |
| 67 | return nil |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // WithDecoderDicts allows to register one or more dictionaries for the decoder. |
| 72 | // If several dictionaries with the same ID is provided the last one will be used. |
| 73 | func WithDecoderDicts(dicts ...[]byte) DOption { |
| 74 | return func(o *decoderOptions) error { |
| 75 | for _, b := range dicts { |
| 76 | d, err := loadDict(b) |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | o.dicts = append(o.dicts, *d) |
| 81 | } |
| 82 | return nil |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // WithDecoderMaxWindow allows to set a maximum window size for decodes. |
| 87 | // This allows rejecting packets that will cause big memory usage. |
| 88 | // The Decoder will likely allocate more memory based on the WithDecoderLowmem setting. |
| 89 | // If WithDecoderMaxMemory is set to a lower value, that will be used. |
| 90 | // Default is 512MB, Maximum is ~3.75 TB as per zstandard spec. |
| 91 | func WithDecoderMaxWindow(size uint64) DOption { |
| 92 | return func(o *decoderOptions) error { |
| 93 | if size < MinWindowSize { |
| 94 | return errors.New("WithMaxWindowSize must be at least 1KB, 1024 bytes") |
| 95 | } |
| 96 | if size > (1<<41)+7*(1<<38) { |
| 97 | return errors.New("WithMaxWindowSize must be less than (1<<41) + 7*(1<<38) ~ 3.75TB") |
| 98 | } |
| 99 | o.maxWindowSize = size |
| 100 | return nil |
| 101 | } |
| 102 | } |