@@ -3,21 +3,38 @@ package skanda
33import "errors"
44
55var (
6- ErrCorrupt = errors .New ("skanda: corrupt input" )
6+ // ErrCorrupt reports malformed input or an output-size mismatch.
7+ ErrCorrupt = errors .New ("skanda: corrupt input" )
8+ // ErrUnsupportedEntropy reports a recognized stream that uses an unsupported entropy mode.
79 ErrUnsupportedEntropy = errors .New ("skanda: unsupported entropy stream" )
10+ // ErrInterrupted reports compression stopped because a progress callback returned true.
11+ ErrInterrupted = errors .New ("skanda: interrupted" )
812)
913
14+ // ProgressFunc observes compression progress.
15+ //
16+ // processedBytes is the number of source bytes consumed, and compressedBytes
17+ // is the number of bytes appended to the destination. Returning true stops
18+ // compression early and causes Encode or Compress to return ErrInterrupted.
1019type ProgressFunc func (processedBytes , compressedBytes int ) bool
1120
21+ // Options contains compression settings.
1222type Options struct {
1323 // Level follows Skanda v1.0's public range and is clamped to 0..10.
14- Level int
24+ Level int
25+ // DecSpeedBias trades compression ratio for decoder speed and is clamped to 0..1.
1526 DecSpeedBias float64
16- Progress ProgressFunc
27+ // Progress observes compression progress and can interrupt long encodes.
28+ Progress ProgressFunc
1729}
1830
31+ // Option configures compression.
1932type Option func (* Options )
2033
34+ // Encoder reuses compression scratch memory across calls.
35+ //
36+ // An Encoder is not safe for concurrent use. Call Close when the encoder is no
37+ // longer needed to release pooled scratch buffers.
2138type Encoder struct {
2239 state compressState
2340 levelOptions compressorLevelOptions
@@ -27,22 +44,29 @@ type Encoder struct {
2744 splitter * blockSplitter
2845}
2946
47+ // Decoder reuses decompression scratch memory across calls.
48+ //
49+ // A Decoder is not safe for concurrent use. Call Close when the decoder is no
50+ // longer needed to release pooled scratch buffers.
3051type Decoder struct {
3152 state decodeState
3253}
3354
55+ // WithLevel sets the compression level. Values outside 0..10 are clamped.
3456func WithLevel (level int ) Option {
3557 return func (o * Options ) {
3658 o .Level = level
3759 }
3860}
3961
62+ // WithDecSpeedBias sets the decoder-speed bias. Values outside 0..1 are clamped.
4063func WithDecSpeedBias (decSpeedBias float64 ) Option {
4164 return func (o * Options ) {
4265 o .DecSpeedBias = decSpeedBias
4366 }
4467}
4568
69+ // WithProgress installs a compression progress callback.
4670func WithProgress (progress ProgressFunc ) Option {
4771 return func (o * Options ) {
4872 o .Progress = progress
@@ -76,13 +100,15 @@ func normalizeOptions(options []Option) Options {
76100 return opts
77101}
78102
103+ // CompressBound returns a conservative upper bound for compressed output size.
79104func CompressBound (size int ) int {
80105 if size < 0 {
81106 return 0
82107 }
83108 return size + size / 1024 + 128
84109}
85110
111+ // IsUnsupported reports whether err indicates an unsupported encoded feature.
86112func IsUnsupported (err error ) bool {
87113 return errors .Is (err , ErrUnsupportedEntropy )
88114}
0 commit comments