-
Notifications
You must be signed in to change notification settings - Fork 1
/
header.go
357 lines (321 loc) · 8.14 KB
/
header.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package lha
import (
"errors"
"os"
"time"
)
// Header is header of file in LHA archive.
type Header struct {
Size uint16
Sum uint8 // for level 0, 1
Method string
PackedSize uint64
OriginalSize uint64
Time time.Time
Attribute uint8
Level uint8
CRC uint16
OSID uint8
Name string
HeaderCRC *uint16
ExtendType ExtendType
MinorVersion uint8
Dir string
DOS HeaderDOS
UNIX HeaderUNIX
}
// HeaderDOS is exntended header for DOS.
type HeaderDOS struct {
Attr uint16
Time uint64
}
// HeaderUNIX is exntended header for UNIX.
type HeaderUNIX struct {
Perm uint16
GID uint16
UID uint16
Group string
User string
Time time.Time
}
// ExtendType is type of exntend part.
type ExtendType uint8
const (
// ExtendGeneric is generic extend.
ExtendGeneric ExtendType = 0
// ExtendUNIX is extend for UNIX.
ExtendUNIX ExtendType = 'U'
// ExtendMSDOS is extend for MS-DOS.
ExtendMSDOS ExtendType = 'M'
// ExtendMACOS is extend for MacOS
ExtendMACOS ExtendType = 'm'
// ExtendOS9 is extend for OS/9
ExtendOS9 ExtendType = '9'
// ExtendOS2 is extend for OS/2
ExtendOS2 ExtendType = '2'
// ExtendOS68K is extend for OS68K
ExtendOS68K ExtendType = 'K'
// ExtendOS386 is extend for OS386
ExtendOS386 ExtendType = '3'
// ExtendHuman is extend for Human
ExtendHuman ExtendType = 'H'
// ExtendCPM is extend for CP/M
ExtendCPM ExtendType = 'C'
// ExtendFLEX is extend for FLEX
ExtendFLEX ExtendType = 'F'
// ExtendRUNSER is extend for RUNSER
ExtendRUNSER ExtendType = 'R'
)
type headerReader func(r *Reader) (*Header, error)
var headerReaders = map[byte]headerReader{
0: readHeaderLv0,
1: readHeaderLv1,
2: readHeaderLv2,
3: readHeaderLv3,
}
func readHeaderLv0(r *Reader) (*Header, error) {
h := new(Header)
headerSize, _ := r.readUint8()
h.Size = uint16(headerSize)
h.Sum, _ = r.readUint8()
// FIXME: verify check sum
h.Method, _ = r.readStringN(5)
packedSize, _ := r.readUint32()
h.PackedSize = uint64(packedSize)
originalSize, _ := r.readUint32()
h.OriginalSize = uint64(originalSize)
dt, _ := r.readUint32()
h.Time = fromDOSTimestamp(dt)
h.Attribute, _ = r.readUint8()
h.Level, _ = r.readUint8()
nameLen, _ := r.readUint8()
h.Name, _ = r.readStringN(int(nameLen))
extendSize := int(headerSize) + 2 - int(nameLen) - 24
if extendSize < 0 {
if extendSize == -2 {
h.HeaderCRC = nil
return h, nil
}
return nil, errors.New("unknown header")
}
*(*uint16)(&h.CRC), _ = r.readUint16()
if extendSize == 0 {
return h, nil
}
extendType, _ := r.readUint8()
h.ExtendType = ExtendType(extendType)
extendSize--
if ExtendType(extendType) == ExtendUNIX {
if extendSize >= 11 {
h.MinorVersion, _ = r.readUint8()
h.UNIX.Time, _ = r.readTime()
h.UNIX.Perm, _ = r.readUint16()
h.UNIX.UID, _ = r.readUint16()
h.UNIX.GID, _ = r.readUint16()
} else {
h.ExtendType = ExtendGeneric
}
}
if remain := int(h.Size) - int(r.cnt); remain > 0 {
r.skip(remain)
}
if r.err != nil {
return nil, r.err
}
return h, nil
}
func readHeaderLv1(r *Reader) (*Header, error) {
h := new(Header)
headerSize, _ := r.readUint8()
h.Size = uint16(headerSize)
h.Sum, _ = r.readUint8()
// FIXME: verify check sum
h.Method, _ = r.readStringN(5)
packedSize, _ := r.readUint32()
h.PackedSize = uint64(packedSize)
originalSize, _ := r.readUint32()
h.OriginalSize = uint64(originalSize)
dt, _ := r.readUint32()
h.Time = fromDOSTimestamp(dt)
h.Attribute, _ = r.readUint8() // 0x20 fixed
h.Level, _ = r.readUint8()
nameLen, _ := r.readUint8()
h.Name, _ = r.readStringN(int(nameLen))
*(*uint16)(&h.CRC), _ = r.readUint16()
h.OSID, _ = r.readUint8()
// FIXME: consider 64bit length.
if remain := int(h.Size) - int(nameLen) - 25; remain > 0 {
r.skip(remain)
}
nextSize, _ := r.readUint16()
readAllExtendedHeaders(r, h, nextSize)
if r.err != nil {
return nil, r.err
}
return h, nil
}
func readHeaderLv2(r *Reader) (*Header, error) {
if r.err != nil {
return nil, r.err
}
h := new(Header)
h.Size, _ = r.readUint16()
h.Method, _ = r.readStringN(5)
packedSize, _ := r.readUint32()
h.PackedSize = uint64(packedSize)
originalSize, _ := r.readUint32()
h.OriginalSize = uint64(originalSize)
h.Time, _ = r.readTime()
h.Attribute, _ = r.readUint8()
h.Level, _ = r.readUint8()
*(*uint16)(&h.CRC), _ = r.readUint16()
h.OSID, _ = r.readUint8()
nextSize, _ := r.readUint16()
readAllExtendedHeaders(r, h, nextSize)
// FIXME: consider 64bit length.
if remain := int(h.Size) - int(r.cnt); remain > 0 {
r.skip(remain)
}
if r.err != nil {
return nil, r.err
}
return h, nil
}
func readHeaderLv3(r *Reader) (*Header, error) {
h := new(Header)
_, _ = r.readUint16()
h.Method, _ = r.readStringN(5)
packedSize, _ := r.readUint32()
h.PackedSize = uint64(packedSize)
originalSize, _ := r.readUint32()
h.OriginalSize = uint64(originalSize)
h.Time, _ = r.readTime()
h.Attribute, _ = r.readUint8()
h.Level, _ = r.readUint8()
*(*uint16)(&h.CRC), _ = r.readUint16()
h.OSID, _ = r.readUint8()
headerSize, _ := r.readUint32()
nextSize, _ := r.readUint32()
readAllExtendedHeaders(r, h, uint16(nextSize))
// FIXME: consider 64bit length.
if remain := int(headerSize) - int(r.cnt); remain >= 0 {
r.skip(remain)
}
if r.err != nil {
return nil, r.err
}
return nil, nil
}
type exHeaderReader func(r *Reader, h *Header, size int) (remain int, err error)
var exHeaderReaders = map[uint8]exHeaderReader{
0x00: readHeaderCRC,
0x01: readFilename,
0x02: readDirectory,
0x40: readDOSAttr,
0x41: readWinTime,
0x42: readWinSize,
0x50: readUNIXPerm,
0x51: readUNIXGIDUID,
0x52: readUNIXGroup,
0x53: readUNIXUser,
0x54: readUNIXTime,
}
func readAllExtendedHeaders(r *Reader, h *Header, size uint16) error {
if r.err != nil {
return r.err
}
for size > 0 {
if size < 3 {
r.err = errTooShortExtendedHeader
return r.err
}
size, r.err = readExtendedHeader(r, h, size)
if r.err != nil {
return r.err
}
}
return nil
}
func readExtendedHeader(r *Reader, h *Header, size uint16) (uint16, error) {
t, err := r.readUint8()
if err != nil {
return 0, err
}
proc, ok := exHeaderReaders[t]
remain := int(size) - 3
if ok {
remain, err = proc(r, h, remain)
if err != nil {
return 0, err
}
}
if remain > 0 {
r.skip(remain)
}
return r.readUint16()
}
func readHeaderCRC(r *Reader, h *Header, size int) (remain int, err error) {
crc := new(uint16)
*crc, err = r.readUint16NoCRC()
if err == nil {
h.HeaderCRC = crc
}
return remain - 2, err
}
func readFilename(r *Reader, h *Header, size int) (remain int, err error) {
h.Name, err = r.readStringN(size)
return 0, err
}
func readDirectory(r *Reader, h *Header, size int) (remain int, err error) {
d, err := r.readBytes(size)
for i, b := range d {
if b == 0xff {
d[i] = os.PathSeparator
}
}
h.Dir = string(d)
return 0, err
}
func readDOSAttr(r *Reader, h *Header, size int) (remain int, err error) {
h.DOS.Attr, err = r.readUint16()
return size - 2, err
}
func readWinTime(r *Reader, h *Header, size int) (remain int, err error) {
// FIXME: parse Windows time.
return size, r.err
}
func readWinSize(r *Reader, h *Header, size int) (remain int, err error) {
h.PackedSize, _ = r.readUint64()
h.OriginalSize, err = r.readUint64()
return size - 16, err
}
func readUNIXPerm(r *Reader, h *Header, size int) (remain int, err error) {
h.UNIX.Perm, err = r.readUint16()
return size - 2, err
}
func readUNIXGIDUID(r *Reader, h *Header, size int) (remain int, err error) {
h.UNIX.GID, _ = r.readUint16()
h.UNIX.UID, err = r.readUint16()
return size - 4, err
}
func readUNIXGroup(r *Reader, h *Header, size int) (remain int, err error) {
h.UNIX.Group, err = r.readStringN(size)
return 0, err
}
func readUNIXUser(r *Reader, h *Header, size int) (remain int, err error) {
h.UNIX.User, err = r.readStringN(size)
return 0, err
}
func readUNIXTime(r *Reader, h *Header, size int) (remain int, err error) {
h.UNIX.Time, err = r.readTime()
return size - 4, err
}
func fromDOSTimestamp(v uint32) time.Time {
y := int(1980 + (v>>25)&0x7f)
m := int((v >> 21) & 0x0f)
d := int((v >> 16) & 0x1f)
h := int((v >> 11) & 0x1f)
mi := int((v >> 5) & 0x3f)
s := int(v&0x1f) * 2
return time.Date(y, time.Month(m), d, h, mi, s, 0, time.Local)
}