-
Notifications
You must be signed in to change notification settings - Fork 0
/
zx0.go
351 lines (312 loc) · 8.03 KB
/
zx0.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package zx0
import (
"fmt"
)
const (
InitialOffset = 1
MaxScale = 50
QtyBlocks = 1000
MaxOffsetZx0 = 32640
MaxOffsetZx7 = 2176
)
var (
//emptyBlock = Zx0Block{}
)
type Zx0 struct {
outputData []byte
outputIndex int
inputIndex int
bitIndex int
bitMask int
diff int
backtrack bool
ghostRoot *Zx0Block
deadArray []Zx0Block
deadArraySize int
}
type Zx0Block struct {
chain *Zx0Block
ghostChain *Zx0Block
bits int
index int
offset int
length int
references int
}
func NewZx0() *Zx0 {
return &Zx0{}
}
func (z *Zx0) Compress(optimal *Zx0Block, inputData []byte, input_size, skip, backwards_mode int, output_size *int, delta *int) []byte {
var (
next *Zx0Block
prev *Zx0Block
last_offset int = InitialOffset
first bool = true
i int
)
/* calculate and allocate output buffer */
*output_size = (optimal.bits + 18 + 7) / 8
z.outputData = make([]byte, *output_size)
/* initialize delta */
z.diff = *output_size - input_size + skip
*delta = 0
/* un-reverse optimal sequence */
next = nil
for optimal != nil {
prev = optimal.chain
optimal.chain = next
next = optimal
optimal = prev
}
z.inputIndex = skip
z.outputIndex = 0
z.bitMask = 0
for optimal = next.chain; optimal != nil; optimal = optimal.chain {
if optimal.offset == 0 {
/* copy literals indicator */
if first {
first = false
} else {
z.writeBit(0)
}
/* copy literals length */
z.writeInterlacedEliasGamma(optimal.length, backwards_mode)
/* copy literals values */
for i = 0; i < optimal.length; i++ {
z.writeByte(int(inputData[z.inputIndex]))
z.readBytes(1, delta)
}
} else {
if optimal.offset == last_offset {
/* copy from last offset indicator */
z.writeBit(0)
/* copy from last offset length */
z.writeInterlacedEliasGamma(optimal.length, backwards_mode)
z.readBytes(optimal.length, delta)
} else {
/* copy from new offset indicator */
z.writeBit(1)
/* copy from new offset MSB */
z.writeInterlacedEliasGamma((optimal.offset-1)/128+1, backwards_mode)
/* copy from new offset LSB */
if backwards_mode != 0 {
z.writeByte(((optimal.offset - 1) % 128) << 1)
} else {
z.writeByte((255 - ((optimal.offset - 1) % 128)) << 1)
}
z.backtrack = true
/* copy from new offset length */
z.writeInterlacedEliasGamma(optimal.length-1, backwards_mode)
z.readBytes(optimal.length, delta)
last_offset = optimal.offset
}
}
}
/* end marker */
z.writeBit(1)
z.writeInterlacedEliasGamma(256, backwards_mode)
return z.outputData
}
func (z *Zx0) readBytes(n int, delta *int) {
z.inputIndex += n
z.diff += n
if z.diff > *delta {
*delta = z.diff
}
}
func (z *Zx0) writeByte(value int) {
z.outputData[z.outputIndex] = byte(value)
z.outputIndex++
z.diff--
}
func (z *Zx0) writeBit(value int) {
if z.backtrack {
if value != 0 {
z.outputData[z.outputIndex-1] |= 1
}
z.backtrack = false
} else {
if z.bitMask == 0 {
z.bitMask = 128
z.bitIndex = z.outputIndex
z.writeByte(0)
}
if value != 0 {
z.outputData[z.bitIndex] |= byte(z.bitMask)
}
z.bitMask >>= 1
}
}
func (z *Zx0) writeInterlacedEliasGamma(value int, backwardsMode int) {
var i int
for i = 2; i <= value; i <<= 1 {
}
i >>= 1
i >>= 1
for i > 0 {
z.writeBit(backwardsMode)
z.writeBit(value & i)
i >>= 1
}
if backwardsMode == 0 {
z.writeBit(1)
} else {
z.writeBit(0)
}
// z.writeBit(!backwardsMode)
}
func (z *Zx0) allocate(bits, index, offset, length int, chain *Zx0Block) *Zx0Block {
var ptr *Zx0Block
if z.ghostRoot != nil {
ptr = z.ghostRoot
z.ghostRoot = ptr.ghostChain
if ptr.chain != nil {
ptr.chain.references--
if ptr.chain.references == 0 {
ptr.chain.ghostChain = z.ghostRoot
z.ghostRoot = ptr.chain
}
}
} else {
if z.deadArraySize == 0 {
z.deadArray = make([]Zx0Block, QtyBlocks)
z.deadArraySize = QtyBlocks
}
z.deadArraySize--
ptr = &z.deadArray[z.deadArraySize]
}
ptr.bits = bits
ptr.index = index
ptr.offset = offset
ptr.length = length
if chain != nil {
chain.references++
}
ptr.chain = chain
ptr.references = 0
return ptr
}
func (z *Zx0) Optimize(inputData []byte, inputSize, skip, offsetLimit int) *Zx0Block {
var (
lastLiteral []*Zx0Block
lastMatch []*Zx0Block
optimal []*Zx0Block
matchLength []int
bestLength []int
bestLengthSize int
bits int
index int
offset int
length int
bits2 int
dots int = 2
maxOffset int = offsetCeiling(inputSize-1, offsetLimit)
)
/* allocate all main data structures at once */
lastLiteral = make([]*Zx0Block, maxOffset+1)
lastMatch = make([]*Zx0Block, maxOffset+1)
optimal = make([]*Zx0Block, inputSize+1)
matchLength = make([]int, maxOffset+1)
bestLength = make([]int, inputSize+1)
bestLength[2] = 2
/* start with fake block */
lastMatch[InitialOffset] = z.assign(lastMatch[InitialOffset], z.allocate(-1, skip-1, InitialOffset, 0, nil))
fmt.Printf("[")
/* process remaining bytes */
for index = skip; index < inputSize; index++ {
bestLengthSize = 2
maxOffset = offsetCeiling(index, offsetLimit)
for offset = 1; offset <= maxOffset; offset++ {
if index != skip && index >= offset && inputData[index] == inputData[index-offset] {
/* copy from last offset */
if lastLiteral[offset] != nil {
length = index - lastLiteral[offset].index
bits = lastLiteral[offset].bits + 1 + eliasGammaBits(length)
lastMatch[offset] = z.assign(lastMatch[offset],
z.allocate(bits, index, offset, length, lastLiteral[offset]))
if optimal[index] == nil || optimal[index].bits > bits {
optimal[index] = z.assign(optimal[index], lastMatch[offset])
}
}
/* copy from new offset */
matchLength[offset]++
if matchLength[offset] > 1 {
if bestLengthSize < matchLength[offset] {
bits = optimal[index-bestLength[bestLengthSize]].bits +
eliasGammaBits(bestLength[bestLengthSize]-1)
for {
bestLengthSize++
bits2 = optimal[index-bestLengthSize].bits + eliasGammaBits(bestLengthSize-1)
if bits2 <= bits {
bestLength[bestLengthSize] = bestLengthSize
bits = bits2
} else {
bestLength[bestLengthSize] = bestLength[bestLengthSize-1]
}
if bestLengthSize >= matchLength[offset] {
break
}
}
}
length = bestLength[matchLength[offset]]
bits = optimal[index-length].bits + 8 + eliasGammaBits((offset-1)/128+1) +
eliasGammaBits(length-1)
if lastMatch[offset] == nil || lastMatch[offset].index != index || lastMatch[offset].bits > bits {
lastMatch[offset] = z.assign(lastMatch[offset],
z.allocate(bits, index, offset, length, optimal[index-length]))
if optimal[index] == nil || optimal[index].bits > bits {
optimal[index] = z.assign(optimal[index], lastMatch[offset])
}
}
}
} else {
/* copy literals */
matchLength[offset] = 0
if lastMatch[offset] != nil {
length = index - lastMatch[offset].index
bits = lastMatch[offset].bits + 1 + eliasGammaBits(length) + length*8
lastLiteral[offset] = z.assign(lastLiteral[offset],
z.allocate(bits, index, 0, length, lastMatch[offset]))
if optimal[index] == nil || optimal[index].bits > bits {
optimal[index] = z.assign(optimal[index], lastLiteral[offset])
}
}
}
}
if index*MaxScale/inputSize > dots {
fmt.Printf(".")
dots++
}
}
fmt.Printf("]\n")
return optimal[inputSize-1]
}
func offsetCeiling(index, offsetLimit int) int {
if index > offsetLimit {
return offsetLimit
}
if index < InitialOffset {
return InitialOffset
}
return index
}
func eliasGammaBits(value int) int {
var bits int = 1
for value > 1 {
bits += 2
value >>= 1
}
return bits
}
func (z *Zx0) assign(ptr *Zx0Block, chain *Zx0Block) *Zx0Block {
(*chain).references++
if ptr != nil {
(*ptr).references--
if (*ptr).references == 0 {
(*ptr).ghostChain = z.ghostRoot
z.ghostRoot = ptr
}
}
ptr = chain
return ptr
}