-
Notifications
You must be signed in to change notification settings - Fork 5
/
reader.go
86 lines (67 loc) · 1.63 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
package azcs
import (
"bytes"
"compress/zlib"
"encoding/binary"
"fmt"
"github.com/new-world-tools/new-world-tools/reader"
"io"
)
const signature = "AZCS"
type header struct {
signature string
compressorId uint32
uncompressedSize uint64
}
type zLibHeader struct {
numSeekPoints uint32
}
type zLibSeekPoint struct {
compressedOffset uint64
uncompressedOffset uint64
}
func NewReader(r io.Reader) (io.Reader, error) {
headerData := &header{}
data, err := reader.ReadBytes(r, len([]byte(signature)))
if err != nil {
return nil, err
}
if string(data) != signature {
return nil, fmt.Errorf("wrong signature - %q. Must be %q", string(data), signature)
}
headerData.signature = signature
compressorId, err := reader.ReadUint32(r, binary.BigEndian)
if err != nil {
return nil, err
}
headerData.compressorId = compressorId
uncompressedSize, err := reader.ReadUint64(r, binary.BigEndian)
if err != nil {
return nil, err
}
headerData.uncompressedSize = uncompressedSize
switch headerData.compressorId {
case 0x73887d3a:
return handleZlib(r)
case 0x72fd505e:
return nil, fmt.Errorf("zstd is not implemented")
}
return nil, fmt.Errorf("unsupported commpressorId: 0x%08x", headerData.compressorId)
}
func handleZlib(r io.Reader) (io.Reader, error) {
numSeekPoints, err := reader.ReadUint32(r, binary.BigEndian)
if err != nil {
return nil, err
}
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
numSeekPointsSize := numSeekPoints * 16
buf := bytes.NewBuffer(data[:len(data)-int(numSeekPointsSize)])
zr, err := zlib.NewReader(buf)
if err != nil {
return nil, err
}
return zr, nil
}