-
Notifications
You must be signed in to change notification settings - Fork 2
/
lzvn.go
274 lines (229 loc) · 8.7 KB
/
lzvn.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
package lzfse
import (
"bytes"
"encoding/binary"
"io"
)
type lzvnDecoder struct {
r io.Reader
w *cachedWriter
buffer *bytes.Buffer
header struct {
N_raw_bytes uint32
N_payload_bytes uint32
}
}
// at the beginning of each lzvn block
func newLzvnDecoder(r io.Reader, w *cachedWriter) (*lzvnDecoder, error) {
decoder := &lzvnDecoder{
r: r,
w: w,
}
if err := binary.Read(r, binary.LittleEndian, &decoder.header); err != nil {
return nil, err
}
b := make([]byte, 0, decoder.header.N_payload_bytes)
decoder.buffer = bytes.NewBuffer(b)
return decoder, nil
}
type lzvnOpCode byte
const (
nop lzvnOpCode = iota
end_of_stream
undefined
small_literal
large_literal
small_match
large_match
small_distance
large_distance
medium_distance
previous_distance
)
var opcode_table = [256]lzvnOpCode{
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, end_of_stream, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, nop, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, nop, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, undefined, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, undefined, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, undefined, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, undefined, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, undefined, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, previous_distance, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, previous_distance, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, previous_distance, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, previous_distance, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, previous_distance, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, previous_distance, large_distance,
undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined,
undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, previous_distance, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, previous_distance, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, previous_distance, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, previous_distance, large_distance,
medium_distance, medium_distance, medium_distance, medium_distance, medium_distance, medium_distance, medium_distance, medium_distance,
medium_distance, medium_distance, medium_distance, medium_distance, medium_distance, medium_distance, medium_distance, medium_distance,
medium_distance, medium_distance, medium_distance, medium_distance, medium_distance, medium_distance, medium_distance, medium_distance,
medium_distance, medium_distance, medium_distance, medium_distance, medium_distance, medium_distance, medium_distance, medium_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, previous_distance, large_distance,
small_distance, small_distance, small_distance, small_distance, small_distance, small_distance, previous_distance, large_distance,
undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined,
undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined,
large_literal, small_literal, small_literal, small_literal, small_literal, small_literal, small_literal, small_literal,
small_literal, small_literal, small_literal, small_literal, small_literal, small_literal, small_literal, small_literal,
large_match, small_match, small_match, small_match, small_match, small_match, small_match, small_match,
small_match, small_match, small_match, small_match, small_match, small_match, small_match, small_match,
}
type lzvnOp struct {
code lzvnOpCode
l byte
m byte
d uint16
}
func newLzvnOp(r io.Reader, op *lzvnOp) (*lzvnOp, error) {
if nil == op {
op = &lzvnOp{}
}
var first_byte byte
if err := binary.Read(r, binary.LittleEndian, &first_byte); err != nil {
return nil, err
}
op.code = opcode_table[first_byte]
switch op.code {
case small_distance:
var second_byte byte
if err := binary.Read(r, binary.LittleEndian, &second_byte); err != nil {
return nil, err
}
op.l = extractByte(first_byte, 6, 2)
op.m = extractByte(first_byte, 3, 3) + 3
op.d = (extractUint16(uint16(first_byte), 0, 3) << 8) | uint16(second_byte)
case medium_distance:
var bytes2and3 uint16
if err := binary.Read(r, binary.LittleEndian, &bytes2and3); err != nil {
return nil, err
}
op.l = extractByte(first_byte, 3, 2)
op.m = ((extractByte(first_byte, 0, 3) << 2) | byte(extractUint16(bytes2and3, 0, 2)) + 3)
op.d = extractUint16(bytes2and3, 2, 14)
case large_distance:
var bytes2and3 uint16
if err := binary.Read(r, binary.LittleEndian, &bytes2and3); err != nil {
return nil, err
}
op.l = extractByte(first_byte, 6, 2)
op.m = extractByte(first_byte, 3, 3) + 3
op.d = bytes2and3
case previous_distance:
op.l = extractByte(first_byte, 6, 2)
op.m = extractByte(first_byte, 3, 3) + 3
case small_match:
op.m = extractByte(first_byte, 0, 4)
case large_match:
var second_byte byte
if err := binary.Read(r, binary.LittleEndian, &second_byte); err != nil {
return nil, err
}
op.m = second_byte + 16
case small_literal:
op.l = extractByte(first_byte, 0, 4)
case large_literal:
var second_byte byte
if err := binary.Read(r, binary.LittleEndian, &second_byte); err != nil {
return nil, err
}
op.l = second_byte + 16
case end_of_stream:
var b [7]byte
if n, err := r.Read(b[:]); n != 7 {
return nil, err
}
}
return op, nil
}
func (dec *lzvnDecoder) Decode() error {
var op *lzvnOp
var opErr error
loop:
for {
// Get the operation
if op, opErr = newLzvnOp(dec.r, op); opErr != nil {
return opErr
}
switch op.code {
// Distance operations
case small_distance:
fallthrough
case medium_distance:
fallthrough
case large_distance:
fallthrough
case previous_distance:
dec.copyLiteralAndMatch(op.l, op.m, op.d)
// Match operations
case small_match:
fallthrough
case large_match:
dec.copyMatch(op.m, op.d)
// Literal operations
case small_literal:
fallthrough
case large_literal:
dec.copyLiteral(op.l)
// Other operations
case nop:
case end_of_stream:
break loop
case undefined:
panic("Bad LZVN opcode")
}
}
return nil
}
func decodeLZVNBlock(r combinedReader, cw *cachedWriter) error {
if decoder, err := newLzvnDecoder(r, cw); err != nil {
return err
} else {
return decoder.Decode()
}
}
// copyMatch copies M bytes from output - D to output
func (dec *lzvnDecoder) copyMatch(m byte, d uint16) error {
b := make([]byte, m)
n, err := dec.w.ReadRelativeToEnd(b, int64(d))
if err == nil && n != len(b) {
// There weren't enough bytes in the buffer, so we should repeat them until we fill b.
// (this is what would happen if there was an overlapped copy)
for i := 0; i < len(b)-n; i++ {
b[n+i] = b[i]
}
}
_, err = dec.w.Write(b)
return err
}
// copyLiteral copies L bytes from src to dst
func (dec *lzvnDecoder) copyLiteral(l byte) error {
if _, err := io.CopyN(dec.w, dec.r, int64(l)); err != nil {
return err
}
return nil
}
// copyLiteralAndMatch copies a literal and then a match from output - d to output
func (dec *lzvnDecoder) copyLiteralAndMatch(l, m byte, d uint16) error {
if err := dec.copyLiteral(l); err != nil {
return err
}
return dec.copyMatch(m, d)
}
func extract64(container uint64, lsb, width int) uint64 {
if width == 64 {
return container
}
return (container >> lsb) & ((1 << width) - 1)
}
func extractByte(container byte, lsb, width int) byte {
return byte(extract64(uint64(container), lsb, width))
}
func extractUint16(container uint16, lsb, width int) uint16 {
return uint16(extract64(uint64(container), lsb, width))
}