-
Notifications
You must be signed in to change notification settings - Fork 1
/
method.go
125 lines (116 loc) · 2.34 KB
/
method.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
package lha
import (
"fmt"
"io"
"github.com/koron-go/lha/crc16"
"github.com/koron-go/lha/lzhuff"
)
type huffDecoderFactory func(r io.Reader) lzhuff.Decoder
type method struct {
dictBits uint
adjust uint
decoderFactory huffDecoderFactory
}
var methods = map[string]*method{
"-lh0-": {
dictBits: 0,
adjust: 253,
decoderFactory: func(r io.Reader) lzhuff.Decoder {
// use copyRaw() to extract raw data.
return nil
},
},
"-lh4-": {
dictBits: 12,
adjust: 253,
decoderFactory: func(r io.Reader) lzhuff.Decoder {
return lzhuff.NewStaticDecoder(r, 4, 14)
},
},
"-lh5-": {
dictBits: 13,
adjust: 253,
decoderFactory: func(r io.Reader) lzhuff.Decoder {
return lzhuff.NewStaticDecoder(r, 4, 14)
},
},
"-lh6-": {
dictBits: 15,
adjust: 253,
decoderFactory: func(r io.Reader) lzhuff.Decoder {
return lzhuff.NewStaticDecoder(r, 5, 16)
},
},
"-lh7-": {
dictBits: 16,
adjust: 253,
decoderFactory: func(r io.Reader) lzhuff.Decoder {
return lzhuff.NewStaticDecoder(r, 5, 17)
},
},
}
// TODO: implement these methods.
//var unsupportedMethods = map[string]*method{
// "-lh1-": {
// dictBits: 12,
// adjust: 253,
// },
// "-lh2-": {
// dictBits: 13,
// adjust: 253,
// },
// "-lh3-": {
// dictBits: 13,
// adjust: 253,
// },
// "-lzs-": {
// dictBits: 11,
// adjust: 254,
// },
// "-lz5-": {
// dictBits: 12,
// adjust: 253,
// },
// "-lz4-": {
// dictBits: 0,
// adjust: 253,
// },
// "-pm0-": {
// dictBits: 0,
// adjust: 253,
// },
// "-pm2-": {
// dictBits: 13,
// adjust: 254,
// },
// // FIXME: need somehthing special.
// "-lhd-": {
// adjust: 253,
// },
//}
func getMethod(s string) (*method, error) {
m, ok := methods[s]
if !ok {
return nil, fmt.Errorf("unsupported method: %s", s)
}
return m, nil
}
func (m *method) decode(r io.Reader, w io.Writer, size int) (n int, crc uint16, err error) {
hd := m.decoderFactory(r)
if hd == nil {
return m.copyRaw(r, w, size)
}
n, crc, err = lzhuff.Decode(hd, w, m.dictBits, m.adjust, size)
if err != nil {
return n, 0, err
}
return n, crc, nil
}
func (m *method) copyRaw(r io.Reader, w io.Writer, size int) (n int, crc uint16, err error) {
hash := crc16.NewIBM()
n64, err := io.CopyN(io.MultiWriter(hash, w), r, int64(size))
if err != nil {
return int(n64), 0, err
}
return int(n64), hash.Sum16(), nil
}