-
Notifications
You must be signed in to change notification settings - Fork 12
/
register.go
129 lines (106 loc) · 3.5 KB
/
register.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package go7z
import (
"bytes"
"compress/bzip2"
"compress/flate"
"encoding/binary"
"io"
"sync"
"github.com/saracen/go7z/filters"
"github.com/ulikunitz/xz/lzma"
)
// Decompressor is a handler function called when a registered decompressor is
// initialized.
type Decompressor func(r []io.Reader, options []byte, unpackSize uint64, ro *ReaderOptions) (io.Reader, error)
var (
decompressors sync.Map // map[uint32]Decompressor
)
func init() {
// copy
RegisterDecompressor(0x00, Decompressor(func(r []io.Reader, options []byte, unpackSize uint64, ro *ReaderOptions) (io.Reader, error) {
if len(r) != 1 {
return nil, ErrNotSupported
}
return r[0], nil
}))
// delta
RegisterDecompressor(0x03, Decompressor(func(r []io.Reader, options []byte, unpackSize uint64, ro *ReaderOptions) (io.Reader, error) {
if len(r) != 1 || len(options) == 0 || len(options) > 1 {
return nil, ErrNotSupported
}
return filters.NewDeltaDecoder(r[0], uint(options[0])+1, int64(unpackSize))
}))
// lzma
RegisterDecompressor(0x030101, Decompressor(func(r []io.Reader, options []byte, unpackSize uint64, ro *ReaderOptions) (io.Reader, error) {
if len(r) != 1 {
return nil, ErrNotSupported
}
// We can't set options in the lzma decoder library, so instead we add
// a fake header
header := bytes.NewBuffer(options)
binary.Write(header, binary.LittleEndian, unpackSize)
return lzma.NewReader(io.MultiReader(header, r[0]))
}))
// lzma2
RegisterDecompressor(0x21, Decompressor(func(r []io.Reader, options []byte, unpackSize uint64, ro *ReaderOptions) (io.Reader, error) {
if len(r) != 1 {
return nil, ErrNotSupported
}
config := lzma.Reader2Config{}
if len(options) > 0 {
config.DictCap = int(2 | (options[0] & 1))
config.DictCap <<= (options[0] >> 1) + 11
}
return config.NewReader2(r[0])
}))
// bcj2
RegisterDecompressor(0x303011b, Decompressor(func(r []io.Reader, options []byte, unpackSize uint64, ro *ReaderOptions) (io.Reader, error) {
if len(r) != 4 {
return nil, ErrNotSupported
}
return filters.NewBCJ2Decoder(r[0], r[1], r[2], r[3], int64(unpackSize))
}))
// deflate
RegisterDecompressor(0x40108, Decompressor(func(r []io.Reader, options []byte, unpackSize uint64, ro *ReaderOptions) (io.Reader, error) {
if len(r) != 1 {
return nil, ErrNotSupported
}
return flate.NewReader(r[0]), nil
}))
// bzip2
RegisterDecompressor(0x40202, Decompressor(func(r []io.Reader, options []byte, unpackSize uint64, ro *ReaderOptions) (io.Reader, error) {
if len(r) != 1 {
return nil, ErrNotSupported
}
return bzip2.NewReader(r[0]), nil
}))
// AES
RegisterDecompressor(0x6f10701, Decompressor(func(r []io.Reader, options []byte, unpackSize uint64, ro *ReaderOptions) (io.Reader, error) {
if len(r) != 1 {
return nil, ErrNotSupported
}
if len(options) < 2 {
return nil, ErrNotSupported
}
saltSize := ((options[0] >> 7) & 1) + (options[1] >> 4)
ivSize := ((options[0] >> 6) & 1) + (options[1] & 0x0F)
power := int(options[0]) & 0x3f
options = options[2:]
salt := options[:saltSize]
iv := options[saltSize : saltSize+ivSize]
return filters.NewAESDecrypter(r[0], power, salt, iv, ro.Password())
}))
}
// RegisterDecompressor registers a decompressor.
func RegisterDecompressor(method uint32, dcomp Decompressor) {
if _, dup := decompressors.LoadOrStore(method, dcomp); dup {
panic("decompressor already registered")
}
}
func decompressor(method uint32) Decompressor {
di, ok := decompressors.Load(method)
if !ok {
return nil
}
return di.(Decompressor)
}