/
cmd.go
191 lines (177 loc) · 4.29 KB
/
cmd.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
package ctw
import (
"bufio"
"bytes"
"encoding/binary"
"io"
"os"
"github.com/fumin/ctw/ac/witten"
)
// Compress compresses the named file using arithmetic coding supplied with a Context Tree Weighting probabilistic model of depth depth.
// The compressed result is written to w.
func Compress(w io.Writer, name string, depth int) error {
// Write file size
fi, err := os.Stat(name)
if err != nil {
return err
}
buf := new(bytes.Buffer)
err = binary.Write(buf, binary.BigEndian, fi.Size())
if err != nil {
return err
}
if _, err = w.Write(buf.Bytes()); err != nil {
return err
}
// Send file contents to encoder through the src channel.
f, err := os.Open(name)
if err != nil {
return err
}
defer f.Close()
src := make(chan int)
errc := make(chan error, 1)
// We allow the reader to terminate early via a stopReader channel,
// in case for example, a downstream error occured when writing to w.
stopReader := make(chan struct{}, 1)
go func() {
defer close(src)
errc <- func() error {
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanBytes)
for scanner.Scan() {
var bt byte = scanner.Bytes()[0]
for i := uint(0); i < 8; i++ {
select {
case src <- ((int(bt) & (1 << i)) >> i):
case <-stopReader:
}
}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}()
}()
// Collect encoded bits into bytes and write to w
dst := make(chan int)
dsterrc := make(chan error, 1)
go func() {
dsterrc <- func() error {
defer func() { stopReader <- struct{}{} }()
buf := []byte{0}
var bt *byte = &buf[0]
var i uint = 0
for b := range dst {
if b == 1 {
*bt |= (1 << i)
}
i += 1
if i == 8 {
if _, err := w.Write(buf); err != nil {
return err
}
*bt = 0
i = 0
}
}
if i > 0 {
if _, err := w.Write(buf); err != nil {
return err
}
}
return nil
}()
}()
model := NewCTW(make([]int, depth))
witten.Encode(dst, src, model)
if err := <-errc; err != nil {
return err
}
if err := <-dsterrc; err != nil {
return err
}
return nil
}
// Decompress decompress a compressed stream of bytes generated by Compress.
// Decompress reads the compressed bytes from r, and writes the decompressed result to w.
// Decompress expects the same Context Tree Weighting depth used in Compress.
func Decompress(w io.Writer, r io.Reader, depth int) error {
var numBytes int64
err := binary.Read(r, binary.BigEndian, &numBytes)
if err != nil {
return err
}
// Read bits from r and send them to the decoder through the src channel.
// Errors during reading are surfaced through the errc channel.
// Since the last byte might contain superfluous bits not belonging to the encoded result (there is no reason why the number of encoded bits should be multipies of 8),
// we need a stopReader channel to avoid the reader goroutine from blocking indefinitely..
src := make(chan int)
srcErrc := make(chan error, 1)
stopReader := make(chan struct{}, 1)
go func() {
defer close(src)
srcErrc <- func() error {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanBytes)
for scanner.Scan() {
var bt byte = scanner.Bytes()[0]
for i := uint(0); i < 8; i++ {
select {
case src <- ((int(bt) & (1 << i)) >> i):
case <-stopReader:
return nil
}
}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}()
}()
// Collect decoded bits and write them out as bytes.
// Upon completion or error, signal the reader goroutine to terminate via the stopReader channel.
dst := make(chan int)
dstErrc := make(chan error, 1)
go func() {
dstErrc <- func() error {
defer func() { stopReader <- struct{}{} }()
buf := []byte{0}
var bt *byte = &buf[0]
var i uint = 0
for b := range dst {
if b == 1 {
*bt |= (1 << i)
}
i += 1
if i == 8 {
if _, err := w.Write(buf); err != nil {
return err
}
*bt = 0
i = 0
}
}
if i > 0 {
if _, err := w.Write(buf); err != nil {
return err
}
}
return nil
}()
}()
model := NewCTW(make([]int, depth))
decodeErr := witten.Decode(dst, src, model, numBytes*8)
if err := <-srcErrc; err != nil {
return err
}
if err := <-dstErrc; err != nil {
return err
}
if decodeErr != nil {
return decodeErr
}
return nil
}