-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
109 lines (90 loc) · 2.13 KB
/
decode.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
package compressor
import (
"encoding/binary"
"io"
"log"
"github.com/dgryski/go-bitstream"
)
type decoder struct {
root *node
w io.Writer
r *bitstream.BitReader
numCharsEncoded uint16
numCharsWritten uint16
numChars uint8
numCharsDecoded uint8
}
// decode the meta data and tree in the header
func (d *decoder) decodeHeader() {
// first byte is the number of leaf nodes
d.numChars = uint8(readByte(d.r))
// read in the total number of characters in the encoded data
buf := make([]byte, 2)
buf[0] = readByte(d.r)
buf[1] = readByte(d.r)
d.numCharsEncoded = binary.LittleEndian.Uint16(buf)
// deserialize the tree
d.root = d.createTree()
}
func (d *decoder) decodeData() {
p := d.root
// loop over all the bits in the data
for {
// read a bit
one, done := readBit(d.r)
if done {
break
}
// traverse right for 1
if one {
if p.right != nil {
p = p.right
// mapping found from code to character
if !p.internal {
d.w.Write([]byte{p.char})
p = d.root
d.numCharsWritten++
}
} else {
log.Fatal("Problem decoding data")
}
} else { // traverse left
if p.left != nil {
p = p.left
if !p.internal {
d.w.Write([]byte{p.char})
p = d.root
d.numCharsWritten++
}
} else {
log.Fatal("Problem decoding data")
}
}
// stop once all characters have been decoded to avoid padding problems
if d.numCharsEncoded == d.numCharsWritten {
break
}
}
}
// read the bit and create the node, zero indicates an internal node, one indicates a leaf character node
func (d *decoder) createTree() *node {
if val, _ := readBit(d.r); val {
return &node{readByte(d.r), -1, false, nil, nil}
} else if d.numChars != d.numCharsDecoded {
left := d.createTree()
right := d.createTree()
return &node{0, -1, true, left, right}
}
return nil
}
func (d *decoder) decode() {
d.decodeHeader()
d.decodeData()
}
//Decode takes a compressed reader, decodes the data and outputs the decompressed data to the writer
func Decode(r io.Reader, w io.Writer) {
d := decoder{}
d.w = w
d.r = bitstream.NewReader(r)
d.decode()
}