-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
222 lines (181 loc) · 4.18 KB
/
main.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"bufio"
"container/list"
"encoding/binary"
"flag"
"fmt"
"io"
"os"
)
// Encodes a rune and a position for decompression
type Pair struct {
R rune
P uint8
}
var chatty bool
// Naive, but revised, utf-8 text compression/decompression program
func main() {
c := flag.Bool("c", false, "Explicit compress mode")
d := flag.Bool("d", false, "De-compress mode")
flag.BoolVar(&chatty, "D", false, "Chatty debug mode")
flag.Parse()
// Compress by default
if (!*c) && (!*d) {
*c = true
}
if *c == *d {
fatal("err: choose one explicit mode")
}
// TODO - allow calling like `runez2 -c foo.txt` ;; use flag.Args
inf := os.Stdin
outf := os.Stdout
args := flag.Args()
switch len(args) {
case 0:
case 1:
f, err := os.OpenFile(args[0], os.O_RDONLY, 0755)
if err != nil {
fatal("err: could not read input file - ", err)
}
inf = f
case 2:
fin, err := os.OpenFile(args[0], os.O_RDONLY, 0755)
if err != nil {
fatal("err: could not read input file - ", err)
}
fout, err := os.OpenFile(args[1], os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
fatal("err: could not read input file - ", err)
}
inf = fin
outf = fout
default:
fatal("err: invalid argument count; use stdin/stdout or specify in/out files")
}
in := bufio.NewReader(inf)
out := bufio.NewWriter(outf)
// Choose mode operation
switch {
case *c:
Compress(in, out)
case *d:
Decompress(in, out)
}
}
// Compress text to the archive format
func Compress(r *bufio.Reader, w *bufio.Writer) {
dict := make(map[rune]uint8)
runes := list.New()
var first rune
// Build table
// i is the index position of unique runes
for i := 0; ; {
ru, _, err := r.ReadRune()
if err != nil {
if err == io.EOF {
break
}
fatal("err: could not read rune - ", err)
}
// Check for character count overflow
if max := int(^uint8(0)); i > max {
fatal("err: too many runes to index, stopped at: ", max)
}
if i <= 0 {
// Handle the base case explicitly
dict[ru] = uint8(i)
i++
first = ru
} else {
// 0 means the rune isn't indexed - kind of a hack
if dict[ru] <= 0 && ru != first {
dict[ru] = uint8(i)
i++
}
}
// Push the i-value for the rune
runes.PushBack(dict[ru])
}
table := make([]rune, len(dict))
// Iterate the dict to build the table
for ru, i := range dict {
if chatty {
fmt.Fprintf(os.Stderr, "%q has index %v\n", ru, i)
}
table[i] = ru
}
// Iterate table to emit output file format
for i := 0; i < len(table); i++ {
if chatty {
fmt.Fprintf(os.Stderr, "%q emitted from table\n", table[i])
}
// Rune
err := binary.Write(w, binary.LittleEndian, table[i])
if err != nil {
fatal("err: could not write rune - ", err)
}
}
// Null Rune
err := binary.Write(w, binary.LittleEndian, rune(0))
if err != nil {
fatal("err: could not write rune - ", err)
}
// Indices
for p := runes.Front(); p != nil; p = p.Next() {
err := binary.Write(w, binary.LittleEndian, byte(p.Value.(uint8)))
if err != nil {
fatal("err: could not write position - ", err)
}
}
w.Flush()
}
// Decompress the archive format to text
func Decompress(r *bufio.Reader, w *bufio.Writer) {
var table []rune // Store final table of runes
table = make([]rune, 0, int(^uint8(0)))
// Read out runes
for {
var ru rune
err := binary.Read(r, binary.LittleEndian, &ru)
if err != nil {
fatal("err: could not read rune - ", err)
}
if ru == 0 {
// \0 rune
if chatty {
fmt.Fprintln(os.Stderr, ">>> Hit \\0 rune")
}
break
}
table = append(table, ru)
}
if chatty {
fmt.Fprintf(os.Stderr, "Table = {\n")
for _, ru := range table {
fmt.Fprintf(os.Stderr, "%q,\n", ru)
}
fmt.Fprintf(os.Stderr, "}\n")
}
// Read in indices and emit ouput rune
for {
var i uint8
err := binary.Read(r, binary.LittleEndian, &i)
if err != nil {
if err == io.EOF {
break
}
fatal("err: could not read index - ", err)
}
if int(i) > len(table) || int(i) < 0 {
fatal("err: bad archive, out of bounds index number: ", i)
}
w.Write([]byte(string(table[i])))
}
w.Flush()
}
// Fatal - end program with an error message and newline
func fatal(s ...interface{}) {
fmt.Fprintln(os.Stderr, s...)
os.Exit(1)
}