/
lzo.go
111 lines (92 loc) · 1.7 KB
/
lzo.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
package extract
import (
"encoding/binary"
"github.com/rasky/go-lzo"
"io"
)
type RaskyReader struct {
R io.Reader
}
func (r *RaskyReader) Read(p []byte) (int, error) {
return io.ReadFull(r.R, p)
}
var Uncompressed uint32
var Compressed uint32
func Decompress(w io.Writer, s io.Reader) {
var skip int = 33
sk := make([]byte, skip)
n, err := s.Read(sk)
if n != len(sk) {
panic("Did not fill skip")
}
if err != nil {
panic(err)
}
var fileNameLen uint8
binary.Read(s, binary.BigEndian, &fileNameLen)
fileName := make([]byte, fileNameLen)
n, err = s.Read(fileName)
if n != len(fileName) {
panic("Did not fill filename")
}
if err != nil {
panic(err)
}
fileComment := make([]byte, 4)
n, err = s.Read(fileComment)
if n != len(fileComment) {
panic("Did not fill fileComment")
}
if err != nil {
panic(err)
}
var uncom uint32
var com uint32
var check uint32
for {
err = binary.Read(s, binary.BigEndian, &uncom)
if uncom == 0 {
break
}
if err != nil {
panic(err)
}
err = binary.Read(s, binary.BigEndian, &com)
if err != nil {
panic(err)
}
Uncompressed += uncom
Compressed += com
err = binary.Read(s, binary.BigEndian, &check)
if err != nil {
panic(err)
}
if uncom <= com {
n, err := io.CopyN(w, s, int64(com))
if n != int64(com) {
panic("uncom <= com")
}
if err != nil {
panic(err)
}
} else {
ras := &RaskyReader{
R: s,
}
out, err := lzo.Decompress1X(ras, int(com), int(uncom))
if len(out) != int(uncom) {
panic("Decompress1X")
}
if err != nil {
panic(err)
}
n, err = w.Write(out)
if n != len(out) {
panic("Write to pipe failed")
}
if err != nil {
panic(err)
}
}
}
}