decoder_options.go 3.6 KB

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