-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader.go
266 lines (244 loc) · 4.8 KB
/
reader.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package lha
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"io"
"time"
"github.com/koron-go/lha/crc16"
)
const (
commonHeaderSize = 21
)
var (
errTooShortExtendedHeader = errors.New("too short extended header")
errHeaderCRCMismatch = errors.New("header CRC mismatch")
errBodyCRCMismatch = errors.New("body CRC mismatch")
errNilHeader = errors.New("no header prepared: try NextHeader() first")
)
// Reader is LHA archive reader.
type Reader struct {
raw io.Reader
br *bufio.Reader
err error
cnt uint64
crc crc16.Hash16
curr *Header
}
// NewReader creates LHA archive reader.
func NewReader(r io.Reader) *Reader {
return &Reader{
raw: r,
br: bufio.NewReader(r),
crc: crc16.NewIBM(),
}
}
// CRC16 returns current CRC16 value.
func (r *Reader) CRC16() uint16 {
return r.crc.Sum16()
}
// NextHeader reads a next file header.
func (r *Reader) NextHeader() (h *Header, err error) {
if r.err != nil {
return nil, r.err
}
if err := r.seekNext(); err != nil {
return nil, err
}
lv, err := r.peekHeaderLevel()
if err == io.EOF {
return nil, nil
} else if err != nil {
return nil, err
}
proc, ok := headerReaders[lv]
if !ok {
return nil, fmt.Errorf("unknown header level: %d", lv)
}
r.cnt = 0
r.crc.Reset()
h, err = proc(r)
if err != nil {
return nil, err
}
if h.HeaderCRC != nil && *h.HeaderCRC != r.crc.Sum16() {
return nil, errHeaderCRCMismatch
}
r.cnt = 0
r.curr = new(Header)
*r.curr = *h
return h, nil
}
func (r *Reader) seekNext() error {
if r.curr != nil && r.curr.PackedSize > r.cnt {
remain := r.curr.PackedSize - r.cnt
r.curr = nil
// FIXME: consider 64bit length.
_, r.err = r.br.Discard(int(remain))
if r.err != nil {
return r.err
}
}
return nil
}
func (r *Reader) peekHeaderLevel() (lv byte, err error) {
if r.err != nil {
return 0, r.err
}
var d []byte
d, r.err = r.br.Peek(1)
if r.err != nil {
return 0, r.err
}
if d[0] == 0 {
r.err = io.EOF
return 0, io.EOF
}
d, r.err = r.br.Peek(commonHeaderSize)
if r.err != nil {
return 0, r.err
}
return d[20], nil
}
func (r *Reader) skip(n int) (int, error) {
if r.err != nil {
return 0, r.err
}
var d []byte
d, r.err = r.readBytes(n)
if r.err != nil {
return 0, r.err
}
return len(d), nil
}
func (r *Reader) readBytes(n int) ([]byte, error) {
if r.err != nil {
return nil, r.err
}
d := make([]byte, n)
_, r.err = io.ReadFull(r.br, d)
if r.err != nil {
return nil, r.err
}
r.cnt += uint64(len(d))
r.crc.Write(d)
return d, nil
}
func (r *Reader) readStringN(n int) (string, error) {
d, err := r.readBytes(n)
if err != nil {
return "", err
}
return string(d), nil
}
func (r *Reader) readUint8() (uint8, error) {
if r.err != nil {
return 0, r.err
}
b0, err := r.br.ReadByte()
if err != nil {
r.err = err
return 0, r.err
}
r.cnt++
r.crc.Write([]byte{b0})
return uint8(b0), nil
}
func (r *Reader) readUint16() (uint16, error) {
if r.err != nil {
return 0, r.err
}
b0, _ := r.br.ReadByte()
b1, err := r.br.ReadByte()
if err != nil {
r.err = err
return 0, r.err
}
r.cnt += 2
r.crc.Write([]byte{b0, b1})
return uint16(b1)<<8 + uint16(b0), nil
}
func (r *Reader) readUint16NoCRC() (uint16, error) {
if r.err != nil {
return 0, r.err
}
b0, _ := r.br.ReadByte()
b1, err := r.br.ReadByte()
if err != nil {
r.err = err
return 0, r.err
}
r.cnt += 2
r.crc.Write([]byte{0, 0})
return uint16(b1)<<8 + uint16(b0), nil
}
func (r *Reader) readUint32() (uint32, error) {
if r.err != nil {
return 0, r.err
}
var (
b0, b1, b2, b3 byte
)
b0, _ = r.br.ReadByte()
b1, _ = r.br.ReadByte()
b2, _ = r.br.ReadByte()
b3, r.err = r.br.ReadByte()
if r.err != nil {
return 0, r.err
}
r.cnt += 4
r.crc.Write([]byte{b0, b1, b2, b3})
return uint32(b3)<<24 + uint32(b2)<<16 + uint32(b1)<<8 + uint32(b0), nil
}
func (r *Reader) readUint64() (uint64, error) {
if r.err != nil {
return 0, r.err
}
d, err := r.readBytes(8)
if err != nil {
return 0, err
}
r.cnt += uint64(len(d))
r.crc.Write(d)
return binary.LittleEndian.Uint64(d), nil
}
func (r *Reader) readTime() (time.Time, error) {
if r.err != nil {
return time.Time{}, r.err
}
var n uint32
n, r.err = r.readUint32()
if r.err != nil {
return time.Time{}, r.err
}
return time.Unix(int64(n), 0), nil
}
// Decode decodes a file to w. It returns decoded size and error.
func (r *Reader) Decode(w io.Writer) (decoded int, err error) {
if r.curr == nil {
return 0, errNilHeader
}
m, err := getMethod(r.curr.Method)
if err != nil {
return 0, err
}
lr := &io.LimitedReader{
R: r.br,
N: int64(r.curr.PackedSize),
}
h := r.curr
defer func() {
// count read length.
r.cnt += h.PackedSize - uint64(lr.N)
}()
n, crc, err := m.decode(lr, w, int(r.curr.OriginalSize))
if err != nil {
return 0, err
}
if crc != r.curr.CRC {
return 0, errBodyCRCMismatch
}
r.curr = nil
return n, nil
}