-
Notifications
You must be signed in to change notification settings - Fork 0
/
decompress.go
337 lines (299 loc) · 6.08 KB
/
decompress.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//
// decompress.go
//
// Copyright (c) 2018 Markku Rossi
//
// All rights reserved.
//
// Streaming Lossless Data Compression Algorithm - (SLDC)
// Standard ECMA-321 June 2001
package sldc
import (
"encoding/hex"
"errors"
"fmt"
"io"
)
// ErrTruncatedInput is an error that is returned when the input
// stream is truncated.
var ErrTruncatedInput = errors.New("Truncated input")
// Scheme specifies compression scheme.
type Scheme int
// Known compression schemes.
const (
Scheme1 Scheme = iota + 1
Scheme2
)
// Ctrl specifies control words.
type Ctrl int
// Known control words.
const (
CtrlFlush Ctrl = iota
CtrlScheme1
CtrlScheme2
CtrlFileMark
CtrlEOR
CtrlReset1
CtrlReset2
CtrlEndMarker = 0xf
)
var ctrlNames = map[Ctrl]string{
CtrlFlush: "Flush",
CtrlScheme1: "Scheme 1",
CtrlScheme2: "Scheme 2",
CtrlFileMark: "File Mark",
CtrlEOR: "EOR",
CtrlReset1: "Reset 1",
CtrlReset2: "Reset 2",
CtrlEndMarker: "End Marker",
}
func (c Ctrl) String() string {
name, ok := ctrlNames[c]
if ok {
return name
}
return fmt.Sprintf("{ctrl %d}", c)
}
// Input implements an input stream.
type Input struct {
data []byte
ofs int
bits int
}
// NewInput creates a new input object.
func NewInput(data []byte) *Input {
return &Input{
data: data,
bits: 8,
}
}
// Avail returns the number of bits available.
func (in *Input) Avail() int {
return (len(in.data)-in.ofs-1)*8 + in.bits
}
// Get gets the specified number of bits from the input.
func (in *Input) Get(bits int) (val uint32, err error) {
if in.bits > bits {
left := in.bits - bits
val = uint32(in.data[in.ofs] >> uint(left))
val &= (0xff >> uint(8-bits))
in.bits = left
return
}
val = uint32(in.data[in.ofs])
val &= (0xff >> uint(8-in.bits))
bits -= in.bits
in.ofs++
in.bits = 8
for bits >= 8 {
if in.ofs >= len(in.data) {
err = ErrTruncatedInput
return
}
val <<= 8
val |= uint32(in.data[in.ofs])
in.ofs++
bits -= 8
}
if bits > 0 {
if in.ofs >= len(in.data) {
err = ErrTruncatedInput
return
}
val <<= uint(bits)
in.bits = 8 - bits
b := uint32(in.data[in.ofs]) >> uint(in.bits)
b &= (0xff >> uint(8-bits))
val |= b
}
return
}
// Peek returns the specified number of bits from the input without
// updating the input position.
func (in *Input) Peek(bits int) (val uint32, err error) {
savedOfs := in.ofs
savedBits := in.bits
val, err = in.Get(bits)
in.ofs = savedOfs
in.bits = savedBits
return
}
// IsCtrl tests if the next input is a control word.
func (in *Input) IsCtrl() bool {
val, err := in.Peek(9)
return err == nil && val == 0x1ff
}
// Ctrl gets the next control word.
func (in *Input) Ctrl() (Ctrl, error) {
val, err := in.Get(9)
if err != nil || val != 0x1ff {
return CtrlEOR, err
}
val, err = in.Get(4)
return Ctrl(val), err
}
// Align aligns input to the next four byte boundary.
func (in *Input) Align() error {
for (in.ofs%4) != 0 && in.ofs < len(in.data) {
in.ofs++
}
if (in.ofs % 4) != 0 {
return ErrTruncatedInput
}
return nil
}
// History implements an input history.
type History struct {
data []byte
pos int
}
// NewHistory creates a new history object.
func NewHistory() *History {
return &History{
data: make([]byte, 1024),
}
}
// Add adds a byte to the history.
func (h *History) Add(b byte) {
h.data[h.pos] = b
h.pos++
if h.pos >= len(h.data) {
h.pos = 0
}
}
// Get gets the byte from the specified offset of the history.
func (h *History) Get(ofs int) (byte, int) {
if ofs < 0 || ofs >= len(h.data) {
panic(fmt.Sprintf("Invalid displacement %d", ofs))
}
b := h.data[ofs]
ofs++
if ofs >= len(h.data) {
ofs = 0
}
return b, ofs
}
// Reset resets the history.
func (h *History) Reset() {
h.pos = 0
}
// Decompress decompresses the data.
func Decompress(data []byte) ([]byte, error) {
input := NewInput(data)
scheme := Scheme1
history := NewHistory()
var result []byte
for {
if input.IsCtrl() {
ctrl, err := input.Ctrl()
if err != nil {
return nil, err
}
switch ctrl {
case CtrlFlush:
err = input.Align()
if err != nil {
return nil, err
}
case CtrlScheme1:
scheme = Scheme1
case CtrlScheme2:
scheme = Scheme2
case CtrlEOR:
err = input.Align()
if err != nil {
return nil, err
}
return result, nil
case CtrlReset1:
scheme = Scheme1
history.Reset()
case CtrlReset2:
scheme = Scheme2
history.Reset()
case CtrlEndMarker:
if len(result) == 0 {
// End marker at the beginning of the data.
return nil, io.EOF
}
default:
fmt.Printf("Unknown Control %s, result so far:\n%s",
ctrl, hex.Dump(result))
return nil, fmt.Errorf("Invalid control symbol %s", ctrl)
}
} else if scheme == Scheme1 {
val, err := input.Get(1)
if err != nil {
return nil, err
}
if val == 0 {
// Literal 1 Data Symbols
val, err = input.Get(8)
if err != nil {
return nil, err
}
history.Add(byte(val))
result = append(result, byte(val))
} else {
// Copy Pointer Data Symbols
var ones int
for ones = 0; ones < 4; ones++ {
val, err := input.Get(1)
if err != nil {
return nil, err
}
if val == 0 {
break
}
}
var base int
var bits int
switch ones {
case 0:
// 0 x
base = 2
bits = 1
case 1:
// 10 xx
base = 4
bits = 2
case 2:
// 110 xxx
base = 8
bits = 3
case 3:
// 1110 xxxx
base = 16
bits = 4
case 4:
// 1111 xxxxxxxx
base = 32
bits = 8
}
val, err = input.Get(bits)
if err != nil {
return nil, err
}
matchCount := base + int(val)
if matchCount > 271 {
return nil, fmt.Errorf("Invalid match count %d", matchCount)
}
val, err := input.Get(10)
if err != nil {
return nil, err
}
displacement := int(val)
var b byte
for matchCount > 0 {
b, displacement = history.Get(displacement)
history.Add(b)
result = append(result, b)
matchCount--
}
}
} else {
fmt.Printf("- Scheme %d rules\n", scheme)
return nil, fmt.Errorf("Scheme 2 rules not implemented yet")
}
}
}