forked from dotabuff/manta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lzss.go
47 lines (37 loc) · 905 Bytes
/
lzss.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
package manta
// Decompress a Valve LZSS compressed buffer
func unlzss(buf []byte) ([]byte, error) {
r := newReader(buf)
if s := r.readStringN(4); s != "LZSS" {
return nil, _errorf("expected LZSS header, got %s", s)
}
size := int(r.readLeUint32())
out := make([]byte, 0)
var cmdByte, getCmdByte byte
for {
if getCmdByte == 0 {
cmdByte = r.readByte()
}
getCmdByte = (getCmdByte + 1) & 0x07
if (cmdByte & 0x01) != 0 {
a := r.readByte()
b := r.readByte()
position := (int(a) << 4) | (int(b) >> 4)
count := int((b & 0x0F) + 1)
if count == 1 {
break
}
source := len(out) - int(position) - 1
for i := 0; i < count; i++ {
out = append(out, out[source+i])
}
} else {
out = append(out, r.readByte())
}
cmdByte = cmdByte >> 1
}
if len(out) != size {
return nil, _errorf("expected %d bytes, got %d", size, len(out))
}
return out, nil
}