forked from avaneev/lzav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlzav.h
1053 lines (874 loc) · 25.6 KB
/
lzav.h
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
/**
* lzav.h version 1.0
*
* The inclusion file for the "LZAV" in-memory data compression and
* decompression algorithm.
*
* Description is available at https://github.com/avaneev/lzav
* E-mail: [email protected]
*
* License
*
* Copyright (c) 2023 Aleksey Vaneev
*
* 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.
*/
#ifndef LZAV_INCLUDED
#define LZAV_INCLUDED
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#define LZAV_API_VER 0x100 // API version, unrelated to code version.
// Decompression error codes:
#define LZAV_E_PARAMS -1 // Incorrect function parameters.
#define LZAV_E_SRCOOB -2 // Source buffer OOB.
#define LZAV_E_DSTOOB -3 // Destination buffer OOB.
#define LZAV_E_REFOOB -4 // Back-reference OOB.
#define LZAV_E_DSTLEN -5 // Uncompressed length mismatch.
#define LZAV_E_UNKFMT -6 // Unknown stream format.
// NOTE: all macros defined below are for internal use, do not change.
#define LZAV_WIN_LEN ( 1 << 24 ) // LZ77 window length, in bytes.
#define LZAV_LIT_LEN ( 1 + 15 + 255 + 255 ) // Max literal length, in bytes.
#define LZAV_REF_MIN 6 // Min reference length, in bytes.
#define LZAV_REF_LEN ( LZAV_REF_MIN + 15 + 255 ) // Max reference length.
#define LZAV_LIT_FIN 5 // The number of literals required at finish.
// Endianness-definition macro.
#if defined( _WIN32 ) || defined( __LITTLE_ENDIAN__ ) || \
( defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
#define LZAV_LITTLE_ENDIAN 1
#elif defined( __BIG_ENDIAN__ ) || \
( defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
#define LZAV_LITTLE_ENDIAN 0
#else // defined( __BIG_ENDIAN__ )
#warning LZAV: cannot determine endianness, assuming little-endian.
#define LZAV_LITTLE_ENDIAN 1
#endif // defined( __BIG_ENDIAN__ )
// In-place endianness-correction macros, for singular variables of matching
// bit-size.
#if LZAV_LITTLE_ENDIAN
#define LZAV_IEC32( x )
#else // LZAV_LITTLE_ENDIAN
#if defined( __GNUC__ ) || defined( __clang__ )
#define LZAV_IEC32( x ) x = __builtin_bswap32( x )
#elif defined( _MSC_VER )
#define LZAV_IEC32( x ) x = _byteswap_ulong( x )
#else // defined( _MSC_VER )
#define LZAV_IEC32( x ) x = ( \
( x & 0xFF000000 ) >> 24 | \
( x & 0x00FF0000 ) >> 8 | \
( x & 0x0000FF00 ) << 8 | \
( x & 0x000000FF ) << 24 )
#endif // defined( _MSC_VER )
#endif // LZAV_LITTLE_ENDIAN
// Likelihood macros that are used for manually-guided micro-optimization.
#if defined( __GNUC__ ) || defined( __clang__ )
#define LZAV_LIKELY( x ) __builtin_expect( x, 1 )
#define LZAV_UNLIKELY( x ) __builtin_expect( x, 0 )
#else // likelihood macros
#define LZAV_LIKELY( x ) ( x )
#define LZAV_UNLIKELY( x ) ( x )
#endif // likelihood macros
/**
* Function finds the number of continuously-matching leading bytes between
* two buffers.
*
* @param p1 Pointer to buffer 1.
* @param p2 Pointer to buffer 2.
* @param ml Maximal number of bytes to match.
* @return The number of matching leading bytes.
*/
static inline size_t lzav_match_len( const uint8_t* p1, const uint8_t* p2,
const size_t ml )
{
#if defined( __GNUC__ ) || defined( __clang__ )
const uint8_t* const p1s = p1;
const uint8_t* const p1e = p1 + ml;
#if defined( __x86_64__ ) || defined( __ppc64__ ) || \
defined( __aarch64__ ) || defined( __arm64__ )
while( LZAV_LIKELY( p1 + 7 < p1e ))
{
uint64_t v1, v2, vd;
memcpy( &v1, p1, 8 );
memcpy( &v2, p2, 8 );
vd = v1 ^ v2;
if( vd != 0 )
{
#if LZAV_LITTLE_ENDIAN
return( p1 - p1s + ( __builtin_ctzll( vd ) >> 3 ));
#else // LZAV_LITTLE_ENDIAN
return( p1 - p1s + ( __builtin_clzll( vd ) >> 3 ));
#endif // LZAV_LITTLE_ENDIAN
}
p1 += 8;
p2 += 8;
}
#endif // 64-bit check
while( p1 + 3 < p1e )
{
uint32_t v1, v2, vd;
memcpy( &v1, p1, 4 );
memcpy( &v2, p2, 4 );
vd = v1 ^ v2;
if( vd != 0 )
{
#if LZAV_LITTLE_ENDIAN
return( p1 - p1s + ( __builtin_ctz( vd ) >> 3 ));
#else // LZAV_LITTLE_ENDIAN
return( p1 - p1s + ( __builtin_clz( vd ) >> 3 ));
#endif // LZAV_LITTLE_ENDIAN
}
p1 += 4;
p2 += 4;
}
while( p1 < p1e )
{
if( *p1 != *p2 )
{
return( p1 - p1s );
}
p1++;
p2++;
}
return( ml );
#else // defined( __GNUC__ ) || defined( __clang__ )
size_t l = 0;
while( 1 )
{
if( ml > 7 )
{
if( p1[ 0 ] != p2[ 0 ]) break; l++;
if( p1[ 1 ] != p2[ 1 ]) break; l++;
if( p1[ 2 ] != p2[ 2 ]) break; l++;
if( p1[ 3 ] != p2[ 3 ]) break; l++;
if( p1[ 4 ] != p2[ 4 ]) break; l++;
if( p1[ 5 ] != p2[ 5 ]) break; l++;
if( p1[ 6 ] != p2[ 6 ]) break; l++;
if( p1[ 7 ] != p2[ 7 ]) break; l++;
}
const uint8_t* const p1s = p1;
const uint8_t* const p1e = p1 + ml;
p1 += l;
p2 += l;
while( p1 < p1e )
{
if( *p1 != *p2 )
{
return( p1 - p1s );
}
p1++;
p2++;
}
return( ml );
}
return( l );
#endif // defined( __GNUC__ ) || defined( __clang__ )
}
/**
* Internal function writes a block to the output buffer. This function can be
* used in custom compression algorithms.
*
* Block starts with a header byte, followed by several optional bytes. The
* lowest 2 bits of the header specify block's type.
*
* CCLLLL00: literal block (1-2 bytes). LLLL is literal length.
* OORRRR01: 10-bit offset block (2-3 bytes). RRRR is reference length.
* OORRRR10: 18-bit offset block (3-4 bytes).
* CCRRRR11: 24-bit offset block (4-5 bytes).
*
* If LLLL==15 or RRRR==15, an additional length byte follows. If, in literal
* block, this additional byte is equal to 255, one more length byte follows.
* CC is a reference offset carry value (additional 2 lowest bits of offset of
* the next reference block).
*
* The overall compressed data is prefixed with a byte whose lower 4 bits
* contain minimal reference length (mref), and the highest 4 bits contain
* stream format identifier.
*
* @param op Output buffer pointer.
* @param lc Literal length, in bytes.
* @param rc Reference length, in bytes, not lesser than mref.
* @param d Reference offset, in bytes.
* @param ipa Literals anchor pointer.
* @param cbpp Pointer to the pointer to the latest offset carry block header.
* Cannot be 0, but the contained pointer can be 0.
* @param mref Minimal reference length, in bytes, used by the compression
* algorithm.
* @return Incremented output buffer pointer.
*/
static inline uint8_t* lzav_write_blk( uint8_t* op, size_t lc, size_t rc,
size_t d, const uint8_t* ipa, uint8_t** const cbpp, const size_t mref )
{
uint8_t* cbp = *cbpp;
while( lc >= LZAV_LIT_LEN )
{
// Write literals due to literal length overflow.
cbp = op;
*cbpp = op;
op[ 0 ] = 15 << 2 | 0;
op[ 1 ] = 255;
op[ 2 ] = 255;
op += 3;
memcpy( op, ipa, LZAV_LIT_LEN );
op += LZAV_LIT_LEN;
ipa += LZAV_LIT_LEN;
lc -= LZAV_LIT_LEN;
}
if( lc != 0 )
{
// Write literal block.
cbp = op;
*cbpp = op;
if( lc < 9 )
{
*op = (uint8_t) (( lc - 1 ) << 2 | 0 );
op++;
memcpy( op, ipa, 8 );
op += lc;
}
else
if( lc < 1 + 15 )
{
*op = (uint8_t) (( lc - 1 ) << 2 | 0 );
op++;
memcpy( op, ipa, 16 );
op += lc;
}
else
if( lc < 1 + 15 + 255 )
{
#if LZAV_LITTLE_ENDIAN
uint16_t ov = (uint16_t) (( lc - 1 - 15 ) << 8 | 15 << 2 | 0 );
#else // LZAV_LITTLE_ENDIAN
uint16_t ov = (uint16_t) (( lc - 1 - 15 ) | 15 << ( 2 + 8 ));
#endif // LZAV_LITTLE_ENDIAN
memcpy( op, &ov, 2 );
op += 2;
memcpy( op, ipa, 16 );
if( lc < 17 )
{
op += lc;
}
else
{
ipa += 16;
op += 16;
lc -= 16;
do
{
*op = *ipa;
ipa++;
op++;
} while( --lc != 0 );
}
}
else
{
op[ 0 ] = 15 << 2 | 0;
op[ 1 ] = 255;
op[ 2 ] = (uint8_t) ( lc - 1 - 15 - 255 );
op += 3;
memcpy( op, ipa, lc );
op += lc;
}
}
// Write reference block.
if( cbp != 0 )
{
// Perform offset carry to a previous block header.
*cbp |= (uint8_t) ( d << 6 );
d >>= 2;
*cbpp = 0;
}
rc -= mref;
if( d < ( 1 << 10 ))
{
if( rc < 15 )
{
#if LZAV_LITTLE_ENDIAN
uint16_t ov = (uint16_t) ( d << 6 | rc << 2 | 1 );
#else // LZAV_LITTLE_ENDIAN
uint16_t ov = (uint16_t) ( d >> 2 | d << 14 | rc << 10 | 1 << 8 );
#endif // LZAV_LITTLE_ENDIAN
memcpy( op, &ov, 2 );
return( op + 2 );
}
*op = (uint8_t) ( d << 6 | 15 << 2 | 1 );
#if LZAV_LITTLE_ENDIAN
uint16_t ov = (uint16_t) (( d & ~3 ) << 6 | ( rc - 15 ));
#else // LZAV_LITTLE_ENDIAN
uint16_t ov = (uint16_t) ( d >> 2 | ( rc - 15 ) << 8 );
#endif // LZAV_LITTLE_ENDIAN
memcpy( op + 1, &ov, 2 );
return( op + 3 );
}
if( d < ( 1 << 18 ))
{
if( rc < 15 )
{
*op = (uint8_t) ( d << 6 | rc << 2 | 2 );
#if LZAV_LITTLE_ENDIAN
uint16_t ov = (uint16_t) ( d >> 2 );
#else // LZAV_LITTLE_ENDIAN
uint16_t ov = (uint16_t) ( d >> 10 | ( d & ~3 ) << 6 );
#endif // LZAV_LITTLE_ENDIAN
memcpy( op + 1, &ov, 2 );
return( op + 3 );
}
uint32_t ov = (uint32_t) (( d & ~3 ) << 14 | ( rc - 15 ) << 8 |
( d & 3 ) << 6 | 15 << 2 | 2 );
LZAV_IEC32( ov );
memcpy( op, &ov, 4 );
return( op + 4 );
}
*cbpp = op;
if( rc < 15 )
{
uint32_t ov = (uint32_t) ( d << 8 | rc << 2 | 3 );
LZAV_IEC32( ov );
memcpy( op, &ov, 4 );
return( op + 4 );
}
*op = (uint8_t) ( 15 << 2 | 3 );
uint32_t ov = (uint32_t) ( d << 8 | ( rc - 15 ));
LZAV_IEC32( ov );
memcpy( op + 1, &ov, 4 );
return( op + 5 );
}
/**
* Internal function writes finishing literal block(s) to the output buffer.
* This function can be used in custom compression algorithms.
*
* @param op Output buffer pointer.
* @param lc Literal length, in bytes. Not less than LZAV_LIT_FIN.
* @param ipa Literals anchor pointer.
* @return Incremented output buffer pointer.
*/
static inline uint8_t* lzav_write_fin( uint8_t* op, size_t lc,
const uint8_t* ipa )
{
while( lc > 15 )
{
size_t wc = lc - LZAV_LIT_FIN; // Leave literals for the final block.
if( wc < 16 )
{
*op = (uint8_t) (( wc - 1 ) << 2 | 0 );
op++;
}
else
{
if( wc > LZAV_LIT_LEN )
{
wc = LZAV_LIT_LEN;
}
if( wc < 1 + 15 + 255 )
{
op[ 0 ] = 15 << 2 | 0;
op[ 1 ] = (uint8_t) ( wc - 1 - 15 );
op += 2;
}
else
{
op[ 0 ] = 15 << 2 | 0;
op[ 1 ] = 255;
op[ 2 ] = (uint8_t) ( wc - 1 - 15 - 255 );
op += 3;
}
}
memcpy( op, ipa, wc );
op += wc;
ipa += wc;
lc -= wc;
}
*op = (uint8_t) (( lc - 1 ) << 2 | 0 );
op++;
do
{
*op = *ipa;
ipa++;
op++;
} while( --lc != 0 );
return( op );
}
/**
* @param srcl The length of the source data to be compressed.
* @return The required allocation size for destination compression buffer.
*/
static inline int lzav_compress_bound( const int srcl )
{
if( srcl < 0 )
{
return( 8 );
}
return( srcl + srcl * 3 / LZAV_LIT_LEN + 8 );
}
/**
* Function performs in-memory data compression using the LZAV compression
* algorithm and stream format. The function produces a "raw" compressed data,
* without any header containing data length nor identifier nor checksum.
*
* @param[in] src Source (uncompressed) data pointer, can be 0 if srcl==0.
* Address alignment is unimportant.
* @param[out] dst Destination (compressed data) buffer pointer. The allocated
* size should be at least lzav_compress_bound() bytes large. Address
* alignment is unimportant.
* @param srcl Source data length, in bytes, can be 0: in this case the
* compressed length is assumed to be 0 as well.
* @param dstl Destination buffer's capacity, in bytes.
* @param ext_buf External buffer to use for hash-table, set to 0 for the
* function to manage memory itself (via std malloc). Supplying a
* pre-allocated buffer is useful if compression is performed during
* application's operation often: this reduces memory allocation overhead and
* fragmentation. Note that the access to the supplied buffer is not
* implicitly thread-safe.
* @param ext_bufl The capacity of the ext_buf, in bytes, should be a
* power-of-2 value. Set to 0 if ext_buf is 0. The capacity should not be
* lesser than srcl, not lesser than 256, but not greater than 512 KiB. Same
* ext_bufl value can be used for any smaller sources.
* @return The length of compressed data, in bytes. Returns 0 if srcl<=0, or
* if dstl is too small, or if not enough memory.
*/
static inline int lzav_compress( const void* const src, void* const dst,
const int srcl, const int dstl, void* const ext_buf,
const int ext_bufl )
{
if( srcl <= 0 || src == 0 || dst == 0 ||
dstl < lzav_compress_bound( srcl ))
{
return( 0 );
}
if( srcl <= LZAV_LIT_FIN )
{
// Handle very short source data.
*(uint8_t*) dst = LZAV_REF_MIN;
*( (uint8_t*) dst + 1 ) = (uint8_t) (( srcl - 1 ) << 2 | 0 );
memset( (uint8_t*) dst + 2, 0, LZAV_LIT_FIN );
memcpy( (uint8_t*) dst + 2, src, srcl );
return( 2 + LZAV_LIT_FIN );
}
uint32_t stack_buf[ 2048 ]; // On-stack hash-table.
void* alloc_buf = 0; // Hash-table allocated on heap.
uint8_t* ht = (uint8_t*) stack_buf; // The actual hash-table pointer.
size_t hcap = 1 << 8; // Hash-table's capacity (power-of-2).
while( hcap != ( 1 << 17 ) && hcap * 5 < (size_t) srcl )
{
hcap <<= 1;
}
const size_t htsize = hcap * sizeof( uint32_t );
if( htsize > sizeof( stack_buf ))
{
if( ext_buf != 0 && (size_t) ext_bufl >= htsize )
{
ht = (uint8_t*) ext_buf;
}
else
{
alloc_buf = malloc( htsize );
if( alloc_buf == 0 )
{
return( 0 );
}
ht = (uint8_t*) alloc_buf;
}
}
memset( ht, 0, htsize );
const uint32_t hmask = (uint32_t) (( htsize - 1 ) ^ 3 ); // Hash mask.
const uint8_t* ip = (const uint8_t*) src; // Source data pointer.
const uint8_t* const ipe = ip + srcl - LZAV_LIT_FIN; // End pointer.
const uint8_t* const ipet = ipe - ( LZAV_REF_MIN - 1 ); // Hash threshold.
const uint8_t* ipa = ip; // Literals anchor pointer.
uint8_t* op = (uint8_t*) dst; // Destination data pointer.
uint8_t* cbp = 0; // Pointer to the latest offset carry block header.
int mavg = 500000; // Running average of match success percentage (*10000).
int rndb = 0; // PRNG bit derived from the non-matching offset.
*op = LZAV_REF_MIN;
op++;
while( LZAV_LIKELY( ip < ipet ))
{
// Hash source data (endianness is unimportant for compression
// efficiency). Hash is based on the "komihash" math construct, see
// https://github.com/avaneev/komihash for details.
uint32_t iw1, ww1, hval, io;
uint16_t iw2, ww2;
memcpy( &iw1, ip, 4 );
const uint64_t Seed1 = 0x243F6A88 ^ iw1;
memcpy( &iw2, ip + 4, 2 );
const uint64_t hm64 = Seed1 * (uint32_t) ( 0x85A308D3 ^ iw2 );
hval = (uint32_t) hm64 ^ ( 0x85A308D3 + (uint32_t) ( hm64 >> 32 ));
// Hash-table access.
uint32_t* const hp = (uint32_t*) ( ht + ( hval & hmask ));
const size_t wo = *hp; // Window back-offset.
if( LZAV_LIKELY( wo != 0 ))
{
const uint8_t* const wp = (const uint8_t*) src + wo;
size_t d = ip - wp; // Reference offset.
if( LZAV_UNLIKELY( d < 8 ))
{
ip++;
continue;
}
if( LZAV_LIKELY( d < LZAV_WIN_LEN ))
{
memcpy( &ww1, wp, 4 );
memcpy( &ww2, wp + 4, 2 );
if( iw1 == ww1 && iw2 == ww2 )
{
// Source data and hash-table entry match.
if( d > 127 ) // Do not update close matching entries.
{
*hp = (uint32_t) ( ip - (const uint8_t*) src );
}
// Disallow overlapped reference copy.
size_t ml = ( d > LZAV_REF_LEN ? LZAV_REF_LEN : d );
if( ip + ml > ipe )
{
// Make sure LZAV_LIT_FIN literals remain on finish.
ml = ipe - ip;
}
size_t lc = ip - ipa;
size_t rc = 0;
if( lc != 0 && lc < LZAV_REF_MIN && lc < wo )
{
// Try to skip literals by finding a local match.
const uint8_t* const ipl = ip - lc;
const uint8_t* const wpl = wp - lc;
if( *ipl == *wpl )
{
rc = 1 + lzav_match_len( ipl + 1, wpl + 1,
ml - 1 );
if( rc < LZAV_REF_MIN )
{
rc = 0;
}
else
{
ip -= lc;
lc = 0;
}
}
}
if( rc == 0 )
{
rc = LZAV_REF_MIN + lzav_match_len( ip + LZAV_REF_MIN,
wp + LZAV_REF_MIN, ml - LZAV_REF_MIN );
}
op = lzav_write_blk( op, lc, rc, d, ipa, &cbp,
LZAV_REF_MIN );
ip += rc;
ipa = ip;
mavg += ( 1000000 - mavg ) >> 8;
continue;
}
mavg -= mavg >> 8;
if( mavg < 120000 && ip != ipa ) // 12% match rate threshold.
{
// Compression speed-up technique that keeps the number of
// hash evaluations around 45% of compressed data length.
// In some cases reduces the number of blocks by several
// percent.
io = (uint32_t) ( ip - (const uint8_t*) src );
ip += 2 | rndb; // Use PRNG bit to dither match positions.
rndb = io & 1; // Delay to decorrelate from current match.
ip += ( mavg < 80000 ) + ( mavg < 40000 ); // More speed.
*hp = io;
continue;
}
}
}
*hp = (uint32_t) ( ip - (const uint8_t*) src );
ip++;
}
if( alloc_buf != 0 )
{
free( alloc_buf );
}
return( (int) ( lzav_write_fin( op, ipe - ipa + LZAV_LIT_FIN, ipa ) -
(uint8_t*) dst ));
}
/**
* Function performs in-memory data compression using the LZAV compression
* algorithm, with the default settings.
*
* See the lzav_compress() function for more detailed description.
*
* @param[in] src Source (uncompressed) data pointer.
* @param[out] dst Destination (compressed data) buffer pointer. The allocated
* size should be at least lzav_compress_bound() bytes large.
* @param srcl Source data length, in bytes.
* @param dstl Destination buffer's capacity, in bytes.
* @return The length of compressed data, in bytes. Returns 0 if srcl<=0, or
* if dstl is too small, or if not enough memory.
*/
static inline int lzav_compress_default( const void* const src,
void* const dst, const int srcl, const int dstl )
{
return( lzav_compress( src, dst, srcl, dstl, 0, 0 ));
}
/**
* Function decompresses "raw" data previously compressed in the LZAV stream
* format.
*
* Note that while the function does perform checks to avoid OOB memory
* accesses, and checks for decompressed data length equality, this is not a
* strict guarantee of a valid decompression. In cases when the compressed
* data is stored in a long-term storage without embedded data integrity
* mechanisms (e.g., a database without RAID 0 guarantee, a binary container
* without digital signature nor CRC), a checksum (hash) of the original
* uncompressed data should be stored, and then evaluated against that of the
* decompressed data. Also, a separate checksum (hash) of application-defined
* header, which contains uncompressed and compressed data lengths, should be
* checked before decompression. A high-performance "komihash" hash function
* can be used to obtain a hash value of the data.
*
* @param[in] src Source (compressed) data pointer, can be 0 if srcl==0.
* Address alignment is unimportant.
* @param[out] dst Destination (uncompressed data) buffer pointer. Address
* alignment is unimportant.
* @param srcl Source data length, in bytes, can be 0.
* @param dstl Expected destination data length, in bytes, can be 0.
* @return The length of decompressed data, in bytes, or any negative value if
* some error happened, including buffer overrun. Always returns a negative
* value if the resulting decompressed data length differs from dstl. This
* means that error result handling requires just a check for a negative
* return value (see the LZAV_E_x macros for possible values).
*/
static inline int lzav_decompress( const void* const src, void* const dst,
const int srcl, const int dstl )
{
if( srcl < 0 )
{
return( LZAV_E_PARAMS );
}
if( srcl == 0 )
{
return( dstl == 0 ? 0 : LZAV_E_PARAMS );
}
if( src == 0 || dst == 0 || dstl <= 0 )
{
return( LZAV_E_PARAMS );
}
if(( *(const uint8_t*) src & 0xF0 ) != 0 )
{
return( LZAV_E_UNKFMT );
}
const uint8_t* ip = (const uint8_t*) src;
const uint8_t* const ipe = ip + srcl;
const uint8_t* const ipet = ipe - LZAV_LIT_FIN;
uint8_t* op = (uint8_t*) dst;
uint8_t* const ope = op + dstl;
uint8_t* const opet = ope - 63; // Threshold for fast copy.
const int mref = *ip & 15; // Minimal reference length in use.
size_t cv = 0; // Reference offset carry value.
int csh = 0; // Reference offset carry shift.
int bh = 0; // Current block header, updated in each branch.
ip++;
if( LZAV_LIKELY( ip < ipet ))
{
bh = *ip;
}
#define LZAV_SET_IPD( x ) \
const size_t d = ( x ) << csh | cv; \
ipd = op - d; \
if( LZAV_UNLIKELY( (uint8_t*) dst + d > op )) return( LZAV_E_REFOOB ); \
cv = 0; \
csh = 0;
while( LZAV_LIKELY( ip < ipet ))
{
const uint8_t* ipd;
int cc = ( bh >> 2 ) & 15;
if( LZAV_LIKELY( cc != 15 ))
{
if(( bh & 2 ) != 0 )
{
cc += mref;
if( LZAV_LIKELY(( bh & 1 ) == 0 ))
{
LZAV_SET_IPD( bh >> 6 | ip[ 1 ] << 2 | ip[ 2 ] << 10 );
bh = ip[ 3 ];
ip += 3;
if( LZAV_LIKELY( op < opet ))
{
memcpy( op, ipd, 16 );
memcpy( op + 16, ipd + 16, 4 );
op += cc;
continue;
}
}
else
{
LZAV_SET_IPD( ip[ 1 ] | ip[ 2 ] << 8 | ip[ 3 ] << 16 );
cv = bh >> 6;
csh = 2;
bh = ip[ 4 ];
ip += 4;
if( LZAV_LIKELY( op < opet ))
{
memcpy( op, ipd, 16 );
memcpy( op + 16, ipd + 16, 4 );
op += cc;
continue;
}
}
}
else
{
if( LZAV_LIKELY(( bh & 1 ) == 0 ))
{
cv = bh >> 6;
csh = 2;
cc++;
ip++;
ipd = ip;
ip += cc;
if( LZAV_LIKELY( ip < ipe ))
{
bh = *ip;
}
else
if( ip > ipe )
{
return( LZAV_E_SRCOOB );
}
if( LZAV_LIKELY(( op < opet ) & ( ipe - ipd >= 20 )))
{
memcpy( op, ipd, 16 );
memcpy( op + 16, ipd + 16, 4 );
op += cc;
continue;
}
}
else
{
LZAV_SET_IPD( bh >> 6 | ip[ 1 ] << 2 );
bh = ip[ 2 ];
ip += 2;
cc += mref;
if( LZAV_LIKELY( op < opet ))
{
memcpy( op, ipd, 16 );
memcpy( op + 16, ipd + 16, 4 );
op += cc;
continue;
}
}
}
}
else
{
int bt;
if( LZAV_LIKELY(( bt = bh & 3 ) != 0 ))
{
cc += mref;
if( LZAV_LIKELY( bt == 2 ))
{
cc += ip[ 1 ];
LZAV_SET_IPD( bh >> 6 | ip[ 2 ] << 2 | ip[ 3 ] << 10 );
bh = ip[ 4 ];
ip += 4;
}
else
if( LZAV_LIKELY( bt == 1 ))
{
cc += ip[ 1 ];
LZAV_SET_IPD( bh >> 6 | ip[ 2 ] << 2 );
bh = ip[ 3 ];
ip += 3;
}
else
{
uint32_t bv;
memcpy( &bv, ip + 1, 4 );
LZAV_IEC32( bv );
cc += bv & 0xFF;
LZAV_SET_IPD( bv >> 8 );
cv = bh >> 6;
csh = 2;
bh = ip[ 5 ];
ip += 5;
}
if( LZAV_LIKELY( op < opet ))
{
memcpy( op, ipd, 16 );
memcpy( op + 16, ipd + 16, 16 );
memcpy( op + 32, ipd + 32, 16 );
memcpy( op + 48, ipd + 48, 16 );
if( LZAV_LIKELY( cc <= 64 ))
{
op += cc;
continue;
}
ipd += 64;
op += 64;
cc -= 64;
}
}
else
{
cv = bh >> 6;
csh = 2;
const int l2 = ip[ 1 ];
const int lb = ( l2 == 255 );
cc += 1 + l2 + ( ip[ 2 ] & ( 0x100 - lb ));
ip += 2 + lb;
ipd = ip;
ip += cc;
if( LZAV_LIKELY( ip < ipe ))