-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathflate.go
More file actions
45 lines (42 loc) · 1023 Bytes
/
Copy pathflate.go
File metadata and controls
45 lines (42 loc) · 1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package msgpackzip
import (
"bytes"
"compress/flate"
"io"
"math"
)
func flateCompress(b []byte) ([]byte, error) {
var buf bytes.Buffer
zw, err := flate.NewWriter(&buf, flate.DefaultCompression)
if err != nil {
return nil, err
}
_, err = zw.Write(b)
if err != nil {
return nil, err
}
err = zw.Close()
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// flateInflateWithLimit decompresses b, returning ErrOutputTooBig if the
// result exceeds maxSize bytes. maxSize must be positive.
func flateInflateWithLimit(b []byte, maxSize int64) ([]byte, error) {
zr := flate.NewReader(bytes.NewBuffer(b))
defer zr.Close() //nolint:errcheck // reader Close only returns decompressor to pool
// Read one byte past maxSize to detect oversize; guard against overflow.
limit := maxSize
if limit < math.MaxInt64 {
limit++
}
out, err := io.ReadAll(io.LimitReader(zr, limit))
if err != nil {
return nil, err
}
if int64(len(out)) > maxSize {
return nil, ErrOutputTooBig
}
return out, nil
}