-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastlz.go
909 lines (803 loc) · 17.8 KB
/
fastlz.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
/*
FastLZ - lightning-fast lossless compression library
Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package fastlz
import (
"math"
"unsafe"
)
/*
* Always check for bound when decompressing.
* Generally it is best to leave it defined.
*/
// FASTLZ_SAFE - will leave it
/*
* Give hints to the compiler for branch prediction optimization.
*/
// FASTLZ_EXPECT_CONDITIONAL - golang does not support
/*
* Use inlined functions for supported systems.
*/
// FASTLZ_INLINE - golang does not support
/*
* Prevent accessing more than 8-bit at once, except on x86 architectures.
*/
// FASTLZ_STRICT_ALIGN - will ONLY support x64 for golang !!!
/*
* FIXME: use preprocessor magic to set this on different platforms!
*/
//type flzuint8 uint8
//type flzuint16 uint16
//type flzuint32 uint32
/* prototypes */
//int fastlz_compress(const void* input, int length, void* output);
//int fastlz_compress_level(int level, const void* input, int length, void* output);
//int fastlz_decompress(const void* input, int length, void* output, int maxout);
const MAX_COPY = 32
const MAX_LEN = 264 /* 256 + 8 */
const MAX_DISTANCE1 = 8192 // MAX_DISTANCE
// FASTLZ_READU16 - will expand into the code
const HASH_LOG = 13
const HASH_SIZE = (1 << HASH_LOG)
const HASH_MASK = (HASH_SIZE - 1)
// HASH_FUNCTION - will expand into the code
const MAX_DISTANCE2 = 8191 // MAX_DISTANCE
const MAX_FARDISTANCE = (65535 + MAX_DISTANCE2 - 1)
/**
Compress a block of data in the input buffer and returns the size of
compressed block. The size of input buffer is specified by length. The
minimum input buffer size is 16.
The output buffer must be at least 5% larger than the input buffer
and can not be smaller than 66 bytes.
If the input is not compressible, the return value might be larger than
length (input buffer size).
The input buffer and the output buffer can not overlap.
*/
func Fastlz_compress(input []byte, length int, output []byte) int {
/* for short block, choose fastlz1 */
if length < 65536 {
return fastlz1_compress(input, length, output)
}
/* else... */
return fastlz2_compress(input, length, output)
}
/**
Decompress a block of compressed data and returns the size of the
decompressed block. If error occurs, e.g. the compressed data is
corrupted or the output buffer is not large enough, then 0 (zero)
will be returned instead.
The input buffer and the output buffer can not overlap.
Decompression is memory safe and guaranteed not to write the output buffer
more than what is specified in maxout.
*/
func Fastlz_decompress(input []byte, length int, output []byte, maxout int) int {
/* magic identifier for compression level */
level := ((*(*uint8)(unsafe.Pointer(&input[0]))) >> 5) + 1
if level == 1 {
return fastlz1_decompress(input, length, output, maxout)
}
if level == 2 {
return fastlz2_decompress(input, length, output, maxout)
}
/* unknown level, trigger error */
return 0
}
/**
Compress a block of data in the input buffer and returns the size of
compressed block. The size of input buffer is specified by length. The
minimum input buffer size is 16.
The output buffer must be at least 5% larger than the input buffer
and can not be smaller than 66 bytes.
If the input is not compressible, the return value might be larger than
length (input buffer size).
The input buffer and the output buffer can not overlap.
Compression level can be specified in parameter level. At the moment,
only level 1 and level 2 are supported.
Level 1 is the fastest compression and generally useful for short data.
Level 2 is slightly slower but it gives better compression ratio.
Note that the compressed data, regardless of the level, can always be
decompressed using the function fastlz_decompress above.
*/
func Fastlz_compress_level(level int, input []byte, length int, output []byte) int {
if len(output) < int(math.Max(66, float64(length)*1.05)) {
return 0
}
if level == 1 {
return fastlz1_compress(input, length, output)
}
if level == 2 {
return fastlz2_compress(input, length, output)
}
return 0
}
func fastlz1_compress(input []byte, length int, output []byte) int {
var ip uint = 0
var ip_bound uint = uint(length - 2)
var ip_limit uint = uint(length - 12)
var op uint = 0
var htab [HASH_SIZE]uint
var hslot uint
var hval uint
var copy uint
/* sanity check */
if length < 4 {
if length != 0 {
/* create literal copy only */
output[op] = byte(length - 1)
op++
ip_bound++
for ip <= ip_bound {
output[op] = input[ip]
op++
ip++
}
return length + 1
} else {
return 0
}
}
/* initializes hash table */
// do nothing
/* we start with literal copy */
copy = 2
output[op] = MAX_COPY - 1
op++
output[op] = input[ip]
op++
ip++
output[op] = input[ip]
op++
ip++
/* main loop */
for ip < ip_limit {
var ref uint
var distance uint
/* minimum match length */
var len uint = 3
/* comparison starting-point */
anchor := ip
/* check for a run */
// do nothing
/* find potential match */
hval = (uint(input[ip]) | uint(input[ip+1])<<8)
hval ^= (uint(input[ip+1]) | uint(input[ip+2])<<8) ^ (hval >> (16 - HASH_LOG))
hval &= HASH_MASK
hslot = hval
ref = htab[hval]
/* calculate distance to the match */
distance = anchor - ref
/* update hash table */
htab[hslot] = anchor
/* is this a match? check the first 3 bytes */
if distance == 0 ||
(distance >= MAX_DISTANCE1) ||
input[ref] != input[ip] || input[ref+1] != input[ip+1] || input[ref+2] != input[ip+2] {
goto literal
}
/* last matched byte */
ref += len
ip = anchor + len
/* distance is biased */
distance--
if distance == 0 {
/* zero distance means a run */
x := input[ip-1]
for ip < ip_bound {
if input[ref] != x {
break
} else {
ip++
}
ref++
}
} else {
for {
/* safe because the outer check against ip limit */
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
for ip < ip_bound {
if input[ref] != input[ip] {
break
}
ref++
ip++
}
break
}
ref++
ip++
}
/* if we have copied something, adjust the copy count */
if copy != 0 {
/* copy is biased, '0' means 1 byte copy */
output[op-copy-1] = byte(copy - 1)
} else {
/* back, to overwrite the copy count */
op--
}
/* reset literal counter */
copy = 0
/* length is biased, '1' means a match of 3 bytes */
ip -= 3
len = ip - anchor
/* encode the match */
for len > MAX_LEN-2 {
output[op] = byte((7 << 5) + (distance >> 8))
op++
output[op] = MAX_LEN - 2 - 7 - 2
op++
output[op] = byte(distance & 255)
op++
len -= MAX_LEN - 2
}
if len < 7 {
output[op] = byte((len << 5) + (distance >> 8))
op++
output[op] = byte(distance & 255)
op++
} else {
output[op] = byte((7 << 5) + (distance >> 8))
op++
output[op] = byte(len - 7)
op++
output[op] = byte(distance & 255)
op++
}
/* update the hash at match boundary */
hval = (uint(input[ip]) | uint(input[ip+1])<<8)
hval ^= (uint(input[ip+1]) | uint(input[ip+2])<<8) ^ (hval >> (16 - HASH_LOG))
hval &= HASH_MASK
htab[hval] = ip
ip++
hval = (uint(input[ip]) | uint(input[ip+1])<<8)
hval ^= (uint(input[ip+1]) | uint(input[ip+2])<<8) ^ (hval >> (16 - HASH_LOG))
hval &= HASH_MASK
htab[hval] = ip
ip++
/* assuming literal copy */
output[op] = MAX_COPY - 1
op++
continue
literal:
output[op] = input[anchor]
op++
anchor++
ip = anchor
copy++
if copy == MAX_COPY {
copy = 0
output[op] = MAX_COPY - 1
op++
}
}
/* left-over as literal copy */
ip_bound++
for ip <= ip_bound {
output[op] = input[ip]
op++
ip++
copy++
if copy == MAX_COPY {
copy = 0
output[op] = MAX_COPY - 1
op++
}
}
/* if we have copied something, adjust the copy length */
if copy != 0 {
output[op-copy-1] = byte(copy - 1)
} else {
op--
}
return int(op)
}
func fastlz2_compress(input []byte, length int, output []byte) int {
var ip uint = 0
var ip_bound uint = uint(length - 2)
var ip_limit uint = uint(length - 12)
var op uint = 0
var htab [HASH_SIZE]uint
var hslot uint
var hval uint
var copy uint
/* sanity check */
if length < 4 {
if length != 0 {
/* create literal copy only */
output[op] = byte(length - 1)
op++
ip_bound++
for ip <= ip_bound {
output[op] = input[ip]
op++
ip++
}
return length + 1
} else {
return 0
}
}
/* initializes hash table */
// do nothing
/* we start with literal copy */
copy = 2
output[op] = MAX_COPY - 1
op++
output[op] = input[ip]
op++
ip++
output[op] = input[ip]
op++
ip++
/* main loop */
for ip < ip_limit {
var ref uint
var distance uint
/* minimum match length */
var len uint = 3
/* comparison starting-point */
anchor := ip
/* check for a run */
if input[ip] == input[ip-1] && (uint(input[ip-1])|uint(input[ip])<<8) == (uint(input[ip+1])|uint(input[ip+2])<<8) {
distance = 1
ip += 3
ref = anchor - 1 + 3
goto match
}
/* find potential match */
hval = (uint(input[ip]) | uint(input[ip+1])<<8)
hval ^= (uint(input[ip+1]) | uint(input[ip+2])<<8) ^ (hval >> (16 - HASH_LOG))
hval &= HASH_MASK
hslot = hval
ref = htab[hval]
/* calculate distance to the match */
distance = anchor - ref
/* update hash table */
htab[hslot] = anchor
/* is this a match? check the first 3 bytes */
if distance == 0 ||
(distance >= MAX_FARDISTANCE) ||
input[ref] != input[ip] || input[ref+1] != input[ip+1] || input[ref+2] != input[ip+2] {
goto literal
}
/* far, needs at least 5-byte match */
if distance >= MAX_DISTANCE2 {
if input[ref+3] != input[ip+3] || input[ref+4] != input[ip+4] {
goto literal
}
len += 2
}
ref += len
match:
/* last matched byte */
ip = anchor + len
/* distance is biased */
distance--
if distance == 0 {
/* zero distance means a run */
x := input[ip-1]
for ip < ip_bound {
if input[ref] != x {
break
} else {
ip++
}
ref++
}
} else {
for {
/* safe because the outer check against ip limit */
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
if input[ref] != input[ip] {
break
}
ref++
ip++
for ip < ip_bound {
if input[ref] != input[ip] {
break
}
ref++
ip++
}
break
}
ref++
ip++
}
/* if we have copied something, adjust the copy count */
if copy != 0 {
/* copy is biased, '0' means 1 byte copy */
output[op-copy-1] = byte(copy - 1)
} else {
/* back, to overwrite the copy count */
op--
}
/* reset literal counter */
copy = 0
/* length is biased, '1' means a match of 3 bytes */
ip -= 3
len = ip - anchor
/* encode the match */
if distance < MAX_DISTANCE2 {
if len < 7 {
output[op] = byte((len << 5) + (distance >> 8))
op++
output[op] = byte(distance & 255)
op++
} else {
output[op] = byte((7 << 5) + (distance >> 8))
op++
for len -= 7; len >= 255; len -= 255 {
output[op] = 255
op++
}
output[op] = byte(len)
op++
output[op] = byte(distance & 255)
op++
}
} else {
/* far away, but not yet in the another galaxy... */
if len < 7 {
distance -= MAX_DISTANCE2
output[op] = byte((len << 5) + 31)
op++
output[op] = 255
op++
output[op] = byte(distance >> 8)
op++
output[op] = byte(distance & 255)
op++
} else {
distance -= MAX_DISTANCE2
output[op] = (7 << 5) + 31
op++
for len -= 7; len >= 255; len -= 255 {
output[op] = 255
op++
}
output[op] = byte(len)
op++
output[op] = 255
op++
output[op] = byte(distance >> 8)
op++
output[op] = byte(distance & 255)
op++
}
}
/* update the hash at match boundary */
hval = (uint(input[ip]) | uint(input[ip+1])<<8)
hval ^= (uint(input[ip+1]) | uint(input[ip+2])<<8) ^ (hval >> (16 - HASH_LOG))
hval &= HASH_MASK
htab[hval] = ip
ip++
hval = (uint(input[ip]) | uint(input[ip+1])<<8)
hval ^= (uint(input[ip+1]) | uint(input[ip+2])<<8) ^ (hval >> (16 - HASH_LOG))
hval &= HASH_MASK
htab[hval] = ip
ip++
/* assuming literal copy */
output[op] = MAX_COPY - 1
op++
continue
literal:
output[op] = input[anchor]
op++
anchor++
ip = anchor
copy++
if copy == MAX_COPY {
copy = 0
output[op] = MAX_COPY - 1
op++
}
}
/* left-over as literal copy */
ip_bound++
for ip <= ip_bound {
output[op] = input[ip]
op++
ip++
copy++
if copy == MAX_COPY {
copy = 0
output[op] = MAX_COPY - 1
op++
}
}
/* if we have copied something, adjust the copy length */
if copy != 0 {
output[op-copy-1] = byte(copy - 1)
} else {
op--
}
/* marker for fastlz2 */
output[0] |= (1 << 5)
return int(op)
}
func fastlz1_decompress(input []byte, length int, output []byte, maxout int) int {
var ip uint = 0
var ip_limit uint = uint(length)
var op uint = 0
var op_limit uint = uint(maxout)
var ctrl uint = uint(input[ip] & 31)
ip++
loop := true
for loop {
ref := op
len := ctrl >> 5
ofs := (ctrl & 31) << 8
if ctrl >= 32 {
len--
ref -= ofs
if len == 7-1 {
len += uint(input[ip])
ip++
}
ref -= uint(input[ip])
ip++
if op+len+3 > op_limit {
return 0
}
if int(ref-1) < 0 {
return 0
}
if ip < ip_limit {
ctrl = uint(input[ip])
ip++
} else {
loop = false
}
if ref == op {
/* optimize copy for a run */
b := output[ref-1]
output[op] = b
op++
output[op] = b
op++
output[op] = b
op++
for ; len != 0; len-- {
output[op] = b
op++
}
} else {
/* copy from reference */
ref--
output[op] = output[ref]
op++
ref++
output[op] = output[ref]
op++
ref++
output[op] = output[ref]
op++
ref++
for ; len != 0; len-- {
output[op] = output[ref]
op++
ref++
}
}
} else {
ctrl++
if op+ctrl > op_limit {
return 0
}
if ip+ctrl > ip_limit {
return 0
}
output[op] = input[ip]
op++
ip++
for ctrl--; ctrl != 0; ctrl-- {
output[op] = input[ip]
op++
ip++
}
loop = (ip < ip_limit)
if loop {
ctrl = uint(input[ip])
ip++
}
}
}
return int(op)
}
func fastlz2_decompress(input []byte, length int, output []byte, maxout int) int {
var ip uint = 0
var ip_limit uint = uint(length)
var op uint = 0
var op_limit uint = uint(maxout)
var ctrl uint = uint(input[ip] & 31)
ip++
loop := true
for loop {
ref := op
len := ctrl >> 5
ofs := (ctrl & 31) << 8
if ctrl >= 32 {
var code byte
len--
ref -= ofs
if len == 7-1 {
code = 255
for code == 255 {
code = input[ip]
ip++
len += uint(code)
}
}
code = input[ip]
ip++
ref -= uint(code)
/* match from 16-bit distance */
if code == 255 {
if ofs == (31 << 8) {
ofs = uint(input[ip]) << 8
ip++
ofs += uint(input[ip])
ip++
ref = op - ofs - MAX_DISTANCE2
}
}
if op+len+3 > op_limit {
return 0
}
if int(ref-1) < 0 {
return 0
}
if ip < ip_limit {
ctrl = uint(input[ip])
ip++
} else {
loop = false
}
if ref == op {
/* optimize copy for a run */
b := output[ref-1]
output[op] = b
op++
output[op] = b
op++
output[op] = b
op++
for ; len != 0; len-- {
output[op] = b
op++
}
} else {
/* copy from reference */
ref--
output[op] = output[ref]
op++
ref++
output[op] = output[ref]
op++
ref++
output[op] = output[ref]
op++
ref++
for ; len != 0; len-- {
output[op] = output[ref]
op++
ref++
}
}
} else {
ctrl++
if op+ctrl > op_limit {
return 0
}
if ip+ctrl > ip_limit {
return 0
}
output[op] = input[ip]
op++
ip++
for ctrl--; ctrl != 0; ctrl-- {
output[op] = input[ip]
op++
ip++
}
loop = (ip < ip_limit)
if loop {
ctrl = uint(input[ip])
ip++
}
}
}
return int(op)
}