This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 391
/
zlib.go
73 lines (58 loc) · 1.85 KB
/
zlib.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package archiver
import (
"context"
"io"
"strings"
"github.com/klauspost/compress/zlib"
)
func init() {
RegisterFormat(Zlib{})
}
// Zlib facilitates zlib compression.
type Zlib struct {
CompressionLevel int
}
func (Zlib) Extension() string { return ".zz" }
func (zz Zlib) Match(_ context.Context, filename string, stream io.Reader) (MatchResult, error) {
var mr MatchResult
// match filename
if strings.Contains(strings.ToLower(filename), zz.Extension()) {
mr.ByName = true
}
// match file header
buf, err := readAtMost(stream, 2)
// If an error occurred or buf is not 2 bytes we can't check the header
if err != nil || len(buf) < 2 {
return mr, err
}
mr.ByStream = isValidZlibHeader(buf[0], buf[1])
return mr, nil
}
func (zz Zlib) OpenWriter(w io.Writer) (io.WriteCloser, error) {
level := zz.CompressionLevel
if level == 0 {
level = zlib.DefaultCompression
}
return zlib.NewWriterLevel(w, level)
}
func (Zlib) OpenReader(r io.Reader) (io.ReadCloser, error) {
return zlib.NewReader(r)
}
func isValidZlibHeader(first, second byte) bool {
// Define all 32 valid zlib headers, see https://stackoverflow.com/questions/9050260/what-does-a-zlib-header-look-like/54915442#54915442
validHeaders := map[uint16]struct{}{
0x081D: {}, 0x085B: {}, 0x0899: {}, 0x08D7: {},
0x1819: {}, 0x1857: {}, 0x1895: {}, 0x18D3: {},
0x2815: {}, 0x2853: {}, 0x2891: {}, 0x28CF: {},
0x3811: {}, 0x384F: {}, 0x388D: {}, 0x38CB: {},
0x480D: {}, 0x484B: {}, 0x4889: {}, 0x48C7: {},
0x5809: {}, 0x5847: {}, 0x5885: {}, 0x58C3: {},
0x6805: {}, 0x6843: {}, 0x6881: {}, 0x68DE: {},
0x7801: {}, 0x785E: {}, 0x789C: {}, 0x78DA: {},
}
// Combine the first and second bytes into a single 16-bit, big-endian value
header := uint16(first)<<8 | uint16(second)
// Check if the header is in the map of valid headers
_, isValid := validHeaders[header]
return isValid
}