-
Notifications
You must be signed in to change notification settings - Fork 15
/
LZCodec.go
1273 lines (1034 loc) · 26.7 KB
/
LZCodec.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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2011-2024 Frederic Langlet
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
you may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package transform
import (
"encoding/binary"
"errors"
"fmt"
"math/bits"
kanzi "github.com/flanglet/kanzi-go/v2"
internal "github.com/flanglet/kanzi-go/v2/internal"
)
const (
_LZX_HASH_SEED = 0x1E35A7BD
_LZX_HASH_LOG1 = 17
_LZX_HASH_SHIFT1 = 40 - _LZX_HASH_LOG1
_LZX_HASH_MASK1 = (1 << _LZX_HASH_LOG1) - 1
_LZX_HASH_LOG2 = 21
_LZX_HASH_SHIFT2 = 48 - _LZX_HASH_LOG2
_LZX_HASH_MASK2 = (1 << _LZX_HASH_LOG2) - 1
_LZX_MAX_DISTANCE1 = (1 << 16) - 2
_LZX_MAX_DISTANCE2 = (1 << 24) - 2
_LZX_MIN_MATCH4 = 4
_LZX_MIN_MATCH9 = 9
_LZX_MAX_MATCH = 65535 + 254 + 15 + _LZX_MIN_MATCH4
_LZX_MIN_BLOCK_LENGTH = 24
_LZP_HASH_SEED = 0x7FEB352D
_LZP_HASH_LOG = 16
_LZP_HASH_SHIFT = 32 - _LZP_HASH_LOG
_LZP_MIN_MATCH96 = 96
_LZP_MIN_MATCH64 = 64
_LZP_MATCH_FLAG = 0xFC
_LZP_MIN_BLOCK_LENGTH = 128
)
// LZCodec encapsulates an implementation of a Lempel-Ziv codec
type LZCodec struct {
delegate kanzi.ByteTransform
}
// NewLZCodec creates a new instance of LZCodec
func NewLZCodec() (*LZCodec, error) {
this := &LZCodec{}
d, err := NewLZXCodec()
this.delegate = d
return this, err
}
// MaxEncodedLen returns the max size required for the encoding output mBuf
func (this *LZCodec) MaxEncodedLen(srcLen int) int {
return this.delegate.MaxEncodedLen(srcLen)
}
// NewLZCodecWithCtx creates a new instance of LZCodec using a
// configuration map as parameter.
func NewLZCodecWithCtx(ctx *map[string]any) (*LZCodec, error) {
this := &LZCodec{}
var err error
var d kanzi.ByteTransform
if val, containsKey := (*ctx)["lz"]; containsKey {
lzType := val.(uint64)
if lzType == LZP_TYPE {
d, err = NewLZPCodecWithCtx(ctx)
this.delegate = d
}
}
if this.delegate == nil && err == nil {
d, err = NewLZXCodecWithCtx(ctx)
this.delegate = d
}
return this, err
}
// Forward applies the function to the src and writes the result
// to the destination. Returns number of bytes read, number of bytes
// written and possibly an error.
func (this *LZCodec) Forward(src, dst []byte) (uint, uint, error) {
if len(src) == 0 {
return 0, 0, nil
}
if &src[0] == &dst[0] {
return 0, 0, errors.New("Input and output buffers cannot be equal")
}
return this.delegate.Forward(src, dst)
}
// Inverse applies the reverse function to the src and writes the result
// to the destination. Returns number of bytes read, number of bytes
// written and possibly an error.
func (this *LZCodec) Inverse(src, dst []byte) (uint, uint, error) {
if len(src) == 0 {
return 0, 0, nil
}
if &src[0] == &dst[0] {
return 0, 0, errors.New("Input and output buffers cannot be equal")
}
return this.delegate.Inverse(src, dst)
}
// LZXCodec Simple byte oriented LZ77 implementation.
// It is a based on a heavily modified LZ4 with a bigger window, a bigger
// hash map, 3+n*8 bit literal lengths and 17 or 24 bit match lengths.
type LZXCodec struct {
hashes []int32
mLenBuf []byte
mBuf []byte
tkBuf []byte
extra bool
ctx *map[string]any
bsVersion uint
}
// NewLZXCodec creates a new instance of LZXCodec
func NewLZXCodec() (*LZXCodec, error) {
this := &LZXCodec{}
this.hashes = make([]int32, 0)
this.mLenBuf = make([]byte, 0)
this.mBuf = make([]byte, 0)
this.tkBuf = make([]byte, 0)
this.extra = false
this.bsVersion = 4
return this, nil
}
// NewLZXCodecWithCtx creates a new instance of LZXCodec using a
// configuration map as parameter.
func NewLZXCodecWithCtx(ctx *map[string]any) (*LZXCodec, error) {
this := &LZXCodec{}
this.hashes = make([]int32, 0)
this.mLenBuf = make([]byte, 0)
this.mBuf = make([]byte, 0)
this.tkBuf = make([]byte, 0)
this.extra = false
this.ctx = ctx
bsVersion := uint(3)
if ctx != nil {
if val, containsKey := (*ctx)["lz"]; containsKey {
lzType := val.(uint64)
this.extra = lzType == LZX_TYPE
}
if val, containsKey := (*ctx)["bsVersion"]; containsKey {
bsVersion = val.(uint)
}
}
this.bsVersion = bsVersion
return this, nil
}
func emitLengthLZ(block []byte, length int) int {
if length < 254 {
block[0] = byte(length)
return 1
}
if length < 65536+254 {
length -= 254
block[0] = byte(254)
block[1] = byte(length >> 8)
block[2] = byte(length)
return 3
}
length -= 255
block[0] = byte(255)
block[1] = byte(length >> 16)
block[2] = byte(length >> 8)
block[3] = byte(length)
return 4
}
func readLengthLZ(block []byte) (int, int) {
res := int(block[0])
if res < 254 {
return res, 1
}
if res == 254 {
res += (int(block[1]) << 8)
res += int(block[2])
return res, 3
}
res += (int(block[1]) << 16)
res += (int(block[2]) << 8)
res += int(block[3])
return res, 4
}
func emitLiteralsLZ(src, dst []byte) {
for i := 0; i < len(src); i += 8 {
copy(dst[i:], src[i:i+8])
}
}
func (this *LZXCodec) hash(p []byte) uint32 {
if this.extra == true {
return uint32((binary.LittleEndian.Uint64(p)*_LZX_HASH_SEED)>>_LZX_HASH_SHIFT2) & _LZX_HASH_MASK2
}
return uint32((binary.LittleEndian.Uint64(p)*_LZX_HASH_SEED)>>_LZX_HASH_SHIFT1) & _LZX_HASH_MASK1
}
// Forward applies the function to the src and writes the result
// to the destination. Returns number of bytes read, number of bytes
// written and possibly an error.
func (this *LZXCodec) Forward(src, dst []byte) (uint, uint, error) {
if len(src) == 0 {
return 0, 0, nil
}
count := len(src)
if n := this.MaxEncodedLen(count); len(dst) < n {
return 0, 0, fmt.Errorf("Output mBuf is too small - size: %d, required %d", len(dst), n)
}
// If too small, skip
if count < _LZX_MIN_BLOCK_LENGTH {
return 0, 0, errors.New("Block too small, skip")
}
if len(this.hashes) == 0 {
if this.extra == true {
this.hashes = make([]int32, 1<<_LZX_HASH_LOG2)
} else {
this.hashes = make([]int32, 1<<_LZX_HASH_LOG1)
}
} else {
for i := range this.hashes {
this.hashes[i] = 0
}
}
minBufSize := max(count/5, 256)
if len(this.mLenBuf) < minBufSize {
this.mLenBuf = make([]byte, minBufSize)
}
if len(this.mBuf) < minBufSize {
this.mBuf = make([]byte, minBufSize)
}
if len(this.tkBuf) < minBufSize {
this.tkBuf = make([]byte, minBufSize)
}
srcEnd := count - 16 - 1
maxDist := _LZX_MAX_DISTANCE2
dThreshold := 1 << 16
dst[12] = 1
if srcEnd < 4*_LZX_MAX_DISTANCE1 {
maxDist = _LZX_MAX_DISTANCE1
dThreshold = 1 << 8
dst[12] = 0
}
minMatch := _LZX_MIN_MATCH4
if this.ctx != nil {
if val, containsKey := (*this.ctx)["dataType"]; containsKey {
dt := val.(internal.DataType)
if dt == internal.DT_DNA {
// Longer min match for DNA input
minMatch = _LZX_MIN_MATCH9
dst[12] |= 2
} else if dt == internal.DT_SMALL_ALPHABET {
return 0, 0, errors.New("Small alphabet, skip")
}
}
}
srcIdx := 0
dstIdx := 13
anchor := 0
mLenIdx := 0
mIdx := 0
tkIdx := 0
var repd = []int{count, count}
repdIdx := 0
srcInc := 0
for srcIdx < srcEnd {
var minRef int
if srcIdx < maxDist {
minRef = 0
} else {
minRef = srcIdx - maxDist
}
ref := srcIdx + 1 - repd[repdIdx]
bestLen := 0
maxMatch := min(srcEnd-srcIdx-1, _LZX_MAX_MATCH)
p := binary.LittleEndian.Uint64(src[srcIdx:])
// Check repd first
if ref > minRef && uint32(p>>8) == binary.LittleEndian.Uint32(src[ref:]) {
if bestLen = findMatchLZX(src, srcIdx+1, ref, maxMatch); bestLen < minMatch {
ref = srcIdx + 1 - repd[1-repdIdx]
if ref > minRef && uint32(p>>8) == binary.LittleEndian.Uint32(src[ref:]) {
bestLen = findMatchLZX(src, srcIdx+1, ref, maxMatch)
}
}
}
if bestLen < minMatch {
h0 := this.hash(src[srcIdx:])
ref = int(this.hashes[h0])
this.hashes[h0] = int32(srcIdx)
if ref > minRef && uint32(p) == binary.LittleEndian.Uint32(src[ref:]) {
bestLen = findMatchLZX(src, srcIdx, ref, min(srcEnd-srcIdx, _LZX_MAX_MATCH))
}
// No good match ?
if bestLen < minMatch {
srcIdx++
srcIdx += (srcInc >> 6)
srcInc++
repdIdx = 0
continue
}
if ref != srcIdx-repd[0] && ref != srcIdx-repd[1] {
// Check if better match at next position
srcIdx1 := srcIdx + 1
h1 := this.hash(src[srcIdx1:])
ref1 := int(this.hashes[h1])
this.hashes[h1] = int32(srcIdx1)
// Find a match
if ref1 > minRef+1 && binary.LittleEndian.Uint32(src[srcIdx1+bestLen-3:]) == binary.LittleEndian.Uint32(src[ref1+bestLen-3:]) {
bestLen1 := findMatchLZX(src, srcIdx1, ref1, min(srcEnd-srcIdx1, _LZX_MAX_MATCH))
// Select best match
if (bestLen1 > bestLen) || ((bestLen1 == bestLen) && (ref1 > ref)) {
if src[srcIdx] == src[ref1-1] && bestLen1 < _LZX_MAX_MATCH {
ref = ref1 - 1
bestLen = bestLen1 + 1
} else {
ref = ref1
bestLen = bestLen1
srcIdx++
}
}
}
}
} else {
h0 := this.hash(src[srcIdx:])
this.hashes[h0] = int32(srcIdx)
if src[srcIdx] == src[ref-1] && bestLen < _LZX_MAX_MATCH {
bestLen++
ref--
} else {
srcIdx++
h1 := this.hash(src[srcIdx:])
this.hashes[h1] = int32(srcIdx)
}
}
// Emit match
srcInc = 0
// Token: 3 bits litLen + 1 bit flag + 4 bits mLen (LLLFMMMM)
// LLL : <= 7 --> LLL == literal length (if 7, remainder encoded outside of token)
// MMMM : <= 14 --> MMMM == match length (if 14, remainder encoded outside of token)
// == 15 if dist == repd0 or repd1 && matchLen fully encoded outside of token
// F : if MMMM == 15, flag = 0 if dist == repd0 and 1 if dist == repd1
// else flag = 1 if dist >= dThreshold and 0 otherwise
dist := srcIdx - ref
litLen := srcIdx - anchor
var token int
if dist == repd[0] {
token = 0x0F
mLenIdx += emitLengthLZ(this.mLenBuf[mLenIdx:], bestLen-minMatch)
} else if dist == repd[1] {
token = 0x1F
mLenIdx += emitLengthLZ(this.mLenBuf[mLenIdx:], bestLen-minMatch)
} else {
// Emit distance since not a repeat
if maxDist == _LZX_MAX_DISTANCE2 {
if dist >= 65536 {
this.mBuf[mIdx] = byte(dist >> 16)
mIdx++
}
this.mBuf[mIdx] = byte(dist >> 8)
mIdx++
} else {
if dist >= 256 {
this.mBuf[mIdx] = byte(dist >> 8)
mIdx++
}
}
this.mBuf[mIdx] = byte(dist)
mIdx++
mLen := bestLen - minMatch
// Emit match length
if mLen >= 14 {
if mLen == 14 {
// Avoid the penalty of one extra byte to encode match length
token = 0x0D
bestLen--
} else {
token = 0x0E
mLenIdx += emitLengthLZ(this.mLenBuf[mLenIdx:], mLen-14)
}
} else {
token = mLen
}
if dist >= dThreshold {
token |= 0x10
}
}
repd[1] = repd[0]
repd[0] = dist
repdIdx = 1
// Emit token
// Literals to process ?
if litLen == 0 {
this.tkBuf[tkIdx] = byte(token)
tkIdx++
} else {
// Emit literal length
if litLen >= 7 {
if litLen >= 1<<24 {
return 0, 0, errors.New("Too many literals, skip")
}
this.tkBuf[tkIdx] = byte((7 << 5) | token)
tkIdx++
dstIdx += emitLengthLZ(dst[dstIdx:], litLen-7)
} else {
this.tkBuf[tkIdx] = byte((litLen << 5) | token)
tkIdx++
}
// Emit literals
emitLiteralsLZ(src[anchor:anchor+litLen], dst[dstIdx:])
dstIdx += litLen
}
if mIdx >= len(this.mBuf)-8 {
extraBuf1 := make([]byte, len(this.mBuf)/2)
this.mBuf = append(this.mBuf, extraBuf1...)
if mLenIdx >= len(this.mLenBuf)-8 {
extraBuf2 := make([]byte, len(this.mLenBuf)/2)
this.mLenBuf = append(this.mLenBuf, extraBuf2...)
}
}
// Fill this.hashes and update positions
anchor = srcIdx + bestLen
srcIdx++
for srcIdx < anchor {
this.hashes[this.hash(src[srcIdx:])] = int32(srcIdx)
srcIdx++
}
}
// Emit last literals
litLen := count - anchor
if dstIdx+litLen+tkIdx+mIdx >= count {
return uint(count), uint(dstIdx), errors.New("No compression")
}
if litLen >= 7 {
this.tkBuf[tkIdx] = byte(7 << 5)
tkIdx++
dstIdx += emitLengthLZ(dst[dstIdx:], litLen-7)
} else {
this.tkBuf[tkIdx] = byte(litLen << 5)
tkIdx++
}
copy(dst[dstIdx:], src[anchor:anchor+litLen])
dstIdx += litLen
// Emit buffers: literals + tokens + matches
binary.LittleEndian.PutUint32(dst[0:], uint32(dstIdx))
binary.LittleEndian.PutUint32(dst[4:], uint32(tkIdx))
binary.LittleEndian.PutUint32(dst[8:], uint32(mIdx))
copy(dst[dstIdx:], this.tkBuf[0:tkIdx])
dstIdx += tkIdx
copy(dst[dstIdx:], this.mBuf[0:mIdx])
dstIdx += mIdx
copy(dst[dstIdx:], this.mLenBuf[0:mLenIdx])
dstIdx += mLenIdx
return uint(count), uint(dstIdx), nil
}
func findMatchLZX(src []byte, srcIdx, ref, maxMatch int) int {
bestLen := 0
for bestLen <= maxMatch-4 {
diff := binary.LittleEndian.Uint32(src[srcIdx+bestLen:]) ^ binary.LittleEndian.Uint32(src[ref+bestLen:])
if diff != 0 {
bestLen += (bits.TrailingZeros32(diff) >> 3)
break
}
bestLen += 4
}
return bestLen
}
// Inverse applies the reverse function to the src and writes the result
// to the destination. Returns number of bytes read, number of bytes
// written and possibly an error.
func (this *LZXCodec) Inverse(src, dst []byte) (uint, uint, error) {
if this.bsVersion == 2 {
return this.inverseV2(src, dst)
}
if this.bsVersion == 3 {
return this.inverseV3(src, dst)
}
return this.inverseV4(src, dst)
}
func (this *LZXCodec) inverseV4(src, dst []byte) (uint, uint, error) {
if len(src) == 0 {
return 0, 0, nil
}
count := len(src)
if count < 13 {
return 0, 0, errors.New("LZCodec: inverse transform failed, invalid data")
}
tkIdx := int(binary.LittleEndian.Uint32(src[0:]))
mIdx := int(binary.LittleEndian.Uint32(src[4:]))
mLenIdx := int(binary.LittleEndian.Uint32(src[8:]))
if (tkIdx < 0) || (mIdx < 0) || (mLenIdx < 0) {
return 0, 0, errors.New("LZCodec: inverse transform failed, invalid data")
}
mIdx += tkIdx
mLenIdx += mIdx
if (tkIdx > count) || (mIdx > count) || (mLenIdx > count) {
return 0, 0, errors.New("LZCodec: inverse transform failed, invalid data")
}
srcEnd := tkIdx - 13
mFlag := int(src[12]) & 1
dstEnd := len(dst) - 16
maxDist := _LZX_MAX_DISTANCE2
if mFlag == 0 {
maxDist = _LZX_MAX_DISTANCE1
}
minMatch := _LZX_MIN_MATCH9
if src[12]&2 == 0 {
minMatch = _LZX_MIN_MATCH4
}
srcIdx := 13
dstIdx := 0
repd0 := 0
repd1 := 0
for {
token := int(src[tkIdx])
tkIdx++
if token >= 32 {
// Get literal length
var litLen int
if token >= 0xE0 {
ll, delta := readLengthLZ(src[srcIdx:])
litLen = 7 + ll
srcIdx += delta
} else {
litLen = token >> 5
}
// Emit literals
if dstIdx+litLen >= dstEnd {
copy(dst[dstIdx:], src[srcIdx:srcIdx+litLen])
} else {
emitLiteralsLZ(src[srcIdx:srcIdx+litLen], dst[dstIdx:])
}
srcIdx += litLen
dstIdx += litLen
if srcIdx >= srcEnd {
break
}
}
// Get match length and distance
mLen := token & 0x0F
var dist int
if mLen == 15 {
// Repetition distance, read mLen fully outside of token
ll, delta := readLengthLZ(src[mLenIdx:])
mLen = minMatch + ll
mLenIdx += delta
if token&0x10 == 0 {
dist = repd0
} else {
dist = repd1
}
} else {
// Read mLen remainder (if any) outside of token
if mLen == 14 {
ll, delta := readLengthLZ(src[mLenIdx:])
mLen = 14 + minMatch + ll
mLenIdx += delta
} else {
mLen += minMatch
}
dist = int(src[mIdx])
mIdx++
if mFlag != 0 {
dist = (dist << 8) | int(src[mIdx])
mIdx++
}
if token&0x10 != 0 {
dist = (dist << 8) | int(src[mIdx])
mIdx++
}
}
repd1 = repd0
repd0 = dist
mEnd := dstIdx + mLen
ref := dstIdx - dist
// Sanity check
if ref < 0 || dist > maxDist || mEnd > dstEnd {
return uint(srcIdx), uint(dstIdx), fmt.Errorf("LZCodec: invalid distance decoded: %d", dist)
}
// Copy match
if dist >= 16 {
for {
// No overlap
copy(dst[dstIdx:], dst[ref:ref+16])
ref += 16
dstIdx += 16
if dstIdx >= mEnd {
break
}
}
} else {
for i := 0; i < mLen; i++ {
dst[dstIdx+i] = dst[ref+i]
}
}
dstIdx = mEnd
}
var err error
if srcIdx != srcEnd+13 {
err = errors.New("LZCodec: inverse transform failed")
}
return uint(mIdx), uint(dstIdx), err
}
func (this *LZXCodec) inverseV3(src, dst []byte) (uint, uint, error) {
if len(src) == 0 {
return 0, 0, nil
}
count := len(src)
if count < 13 {
return 0, 0, errors.New("LZCodec: inverse transform failed, invalid data")
}
tkIdx := int(binary.LittleEndian.Uint32(src[0:]))
mIdx := int(binary.LittleEndian.Uint32(src[4:]))
mLenIdx := int(binary.LittleEndian.Uint32(src[8:]))
if (tkIdx < 0) || (mIdx < 0) || (mLenIdx < 0) {
return 0, 0, errors.New("LZCodec: inverse transform failed, invalid data")
}
mIdx += tkIdx
mLenIdx += mIdx
if (tkIdx > count) || (mIdx > count) || (mLenIdx > count) {
return 0, 0, errors.New("LZCodec: inverse transform failed, invalid data")
}
srcEnd := tkIdx - 13
dstEnd := len(dst) - 16
maxDist := _LZX_MAX_DISTANCE2
if src[12]&1 == 0 {
maxDist = _LZX_MAX_DISTANCE1
}
minMatch := _LZX_MIN_MATCH4
if src[12]&2 != 0 {
minMatch = _LZX_MIN_MATCH9
}
srcIdx := 13
dstIdx := 0
repd0 := 0
repd1 := 0
for {
token := int(src[tkIdx])
tkIdx++
if token >= 32 {
// Get literal length
litLen := token >> 5
if litLen == 7 {
ll, delta := readLengthLZ(src[srcIdx:])
litLen += ll
srcIdx += delta
}
// Emit literals
if dstIdx+litLen >= dstEnd {
copy(dst[dstIdx:], src[srcIdx:srcIdx+litLen])
} else {
emitLiteralsLZ(src[srcIdx:srcIdx+litLen], dst[dstIdx:])
}
srcIdx += litLen
dstIdx += litLen
if srcIdx >= srcEnd {
break
}
}
// Get match length
mLen := token & 0x0F
if mLen == 15 {
ll, delta := readLengthLZ(src[mLenIdx:])
mLen += ll
mLenIdx += delta
}
mLen += minMatch
mEnd := dstIdx + mLen
// Get distance
dist := (int(src[mIdx]) << 8) | int(src[mIdx+1])
mIdx += 2
if (token & 0x10) != 0 {
if maxDist == _LZX_MAX_DISTANCE1 {
dist += 65536
} else {
dist = (dist << 8) | int(src[mIdx])
mIdx++
}
}
if dist == 0 {
dist = repd0
} else {
if dist == 1 {
dist = repd1
} else {
dist--
}
repd1 = repd0
repd0 = dist
}
// Sanity check
if dstIdx < dist || dist > maxDist || mEnd > dstEnd+16 {
return uint(srcIdx), uint(dstIdx), fmt.Errorf("LZCodec: invalid distance decoded: %d", dist)
}
ref := dstIdx - dist
// Copy match
if dist >= 16 {
for {
// No overlap
copy(dst[dstIdx:], dst[ref:ref+16])
ref += 16
dstIdx += 16
if dstIdx >= mEnd {
break
}
}
} else {
for i := 0; i < mLen; i++ {
dst[dstIdx+i] = dst[ref+i]
}
}
dstIdx = mEnd
}
var err error
if srcIdx != srcEnd+13 {
err = errors.New("LZCodec: inverse transform failed")
}
return uint(mIdx), uint(dstIdx), err
}
func (this *LZXCodec) inverseV2(src, dst []byte) (uint, uint, error) {
if len(src) == 0 {
return 0, 0, nil
}
count := len(src)
tkIdx := int(binary.LittleEndian.Uint32(src[0:]))
mIdx := int(binary.LittleEndian.Uint32(src[4:]))
if (tkIdx < 0) || (mIdx < 0) {
return 0, 0, errors.New("LZCodec: inverse transform failed, invalid data")
}
mIdx += tkIdx
if (tkIdx > count) || (mIdx > count) {
return 0, 0, errors.New("LZCodec: inverse transform failed, invalid data")
}
srcEnd := tkIdx - 9
dstEnd := len(dst) - 16
maxDist := _LZX_MAX_DISTANCE2
if src[8] == 0 {
maxDist = _LZX_MAX_DISTANCE1
}
srcIdx := 9
dstIdx := 0
repd := 0
for {
token := int(src[tkIdx])
tkIdx++
if token >= 32 {
// Get literal length
litLen := token >> 5
if litLen == 7 {
ll, delta := readLengthLZ(src[srcIdx:])
litLen += ll
srcIdx += delta
}
// Emit literals
if dstIdx+litLen >= dstEnd {
copy(dst[dstIdx:], src[srcIdx:srcIdx+litLen])
} else {
emitLiteralsLZ(src[srcIdx:srcIdx+litLen], dst[dstIdx:])
}
srcIdx += litLen
dstIdx += litLen
if srcIdx >= srcEnd {
break
}
}
// Get match length
mLen := token & 0x0F
if mLen == 15 {
ll, delta := readLengthLZ(src[mIdx:])
mLen += ll
mIdx += delta
}
mLen += 5
mEnd := dstIdx + mLen
// Get distance
d := (int(src[mIdx]) << 8) | int(src[mIdx+1])
mIdx += 2
if (token & 0x10) != 0 {
if maxDist == _LZX_MAX_DISTANCE1 {
d += 65536
} else {
d = (d << 8) | int(src[mIdx])
mIdx++
}
}
var dist int
if d == 0 {
dist = repd
} else {
dist = d - 1
repd = dist
}
// Sanity check
if dstIdx < dist || dist > maxDist || mEnd > dstEnd+16 {
return uint(srcIdx), uint(dstIdx), fmt.Errorf("LZCodec: invalid distance decoded: %d", dist)
}
ref := dstIdx - dist
// Copy match
if dist >= 16 {
for {
// No overlap
copy(dst[dstIdx:], dst[ref:ref+16])
ref += 16
dstIdx += 16
if dstIdx >= mEnd {
break
}
}
} else {
for i := 0; i < mLen; i++ {
dst[dstIdx+i] = dst[ref+i]
}
}
dstIdx = mEnd
}
var err error
if srcIdx != srcEnd+9 {
err = errors.New("LZCodec: inverse transform failed")
}
return uint(mIdx), uint(dstIdx), err
}