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
|
// QuickLZ data compression library
// Copyright (C) 2006-2007 Lasse Mikkel Reinhold
// lar@quicklz.com
//
// QuickLZ can be used for free under the GPL-1 or GPL-2 license (where anything
// released into public must be open source) or under a commercial license if such
// has been acquired (see http://www.quicklz.com/order.html). The commercial license
// does not cover derived or ported versions created by third parties under GPL.
#include <string.h>
#include "yquicklz.h"
// Version 1.31 final
#define QLZ_VERSION_MAJOR 1
#define QLZ_VERSION_MINOR 3
#define QLZ_VERSION_REVISION 1
#define test_rle
#define speedup_incompressible
//#define memory_safe
#ifndef _WIN32
# define __inline static inline
#endif
#if (defined(__GNUC__) && (__GNUC__ > 2))
# define EXPECT_FALSE(Cond) __builtin_expect(!!(Cond), 0)
#else
# define EXPECT_FALSE(Cond) (Cond)
#endif
// Public functions of QuickLZ
size_t qlz_decompress(const char *source, void *destination, char *scratch);
size_t qlz_compress(const void *source, char *destination, size_t size, char *scratch);
size_t qlz_size_decompressed(const char *source);
size_t qlz_size_compressed(const char *source);
int qlz_get_setting(int setting);
#if (defined(__X86__) || defined(__i386__) || defined(i386) || defined(_M_IX86) || defined(__386__) || defined(__x86_64__) || defined(_M_X64))
#define X86X64
#endif
// Compute SCRATCH_COMPRESS, SCRATCH_DECOMPRESS and constants used internally
#if COMPRESSION_LEVEL == 0 && defined(memory_safe)
#error memory_safe flag cannot be used with COMPRESSION_LEVEL 0
#endif
#define HASH_ENTRIES 4096
#if (COMPRESSION_LEVEL == 0 || COMPRESSION_LEVEL == 1 || COMPRESSION_LEVEL == 2)
#define AND 1
#elif (COMPRESSION_LEVEL == 3)
#define AND 0x7
#else
#error COMPRESSION_LEVEL must be 0, 1, 2 or 3
#endif
#define HASH_SIZE (AND + 1)*HASH_ENTRIES*sizeof(unsigned char *)
#ifdef STREAMING_MODE
#define STREAMING_MODE_VALUE STREAMING_MODE
#else
#define STREAMING_MODE_VALUE 0
#endif
#define STREAMING_MODE_ROUNDED ((STREAMING_MODE_VALUE >> 3) << 3)
#if (COMPRESSION_LEVEL > 1)
#define SCRATCH_COMPRESS HASH_SIZE + STREAMING_MODE_VALUE + 16 + HASH_ENTRIES
#else
#define SCRATCH_COMPRESS HASH_SIZE + STREAMING_MODE_VALUE + 16
#endif
#if (COMPRESSION_LEVEL == 0)
#define SCRATCH_DECOMPRESS HASH_ENTRIES*sizeof(unsigned char *) + 16 + STREAMING_MODE_VALUE
#else
#define SCRATCH_DECOMPRESS 16 + STREAMING_MODE_VALUE
#endif
int qlz_get_setting(int setting)
{
switch (setting)
{
case 0: return COMPRESSION_LEVEL;
case 1: return SCRATCH_COMPRESS;
case 2: return SCRATCH_DECOMPRESS;
case 3: return STREAMING_MODE_VALUE;
#ifdef test_rle
case 4: return 1;
#else
case 4: return 0;
#endif
#ifdef speedup_incompressible
case 5: return 1;
#else
case 5: return 0;
#endif
#ifdef memory_safe
case 6: return 1;
#else
case 6: return 0;
#endif
case 7: return QLZ_VERSION_MAJOR;
case 8: return QLZ_VERSION_MINOR;
case 9: return QLZ_VERSION_REVISION;
}
return -1;
}
__inline unsigned int hash_func(unsigned int i)
{
return ((i >> 12) ^ i) & 0x0fff;
}
__inline unsigned int fast_read(void const *src, unsigned int bytes)
{
#ifndef X86X64
unsigned char *p = (unsigned char*)src;
switch (bytes)
{
case 4:
return(*p | *(p + 1) << 8 | *(p + 2) << 16 | *(p + 3) << 24);
case 3:
return(*p | *(p + 1) << 8 | *(p + 2) << 16);
case 2:
return(*p | *(p + 1) << 8);
case 1:
return(*p);
}
return 0;
#else
if (bytes >= 1 && bytes <= 4)
return *((unsigned int*)src);
else
return 0;
#endif
}
__inline void fast_write(unsigned int f, void *dst, unsigned int bytes)
{
#ifndef X86X64
unsigned char *p = (unsigned char*)dst;
switch (bytes)
{
case 4:
*p = (unsigned char)f;
*(p + 1) = (unsigned char)(f >> 8);
*(p + 2) = (unsigned char)(f >> 16);
*(p + 3) = (unsigned char)(f >> 24);
return;
case 3:
*p = (unsigned char)f;
*(p + 1) = (unsigned char)(f >> 8);
*(p + 2) = (unsigned char)(f >> 16);
return;
case 2:
*p = (unsigned char)f;
*(p + 1) = (unsigned char)(f >> 8);
return;
case 1:
*p = (unsigned char)f;
return;
}
#else
switch (bytes)
{
case 4:
*((unsigned int*)dst) = f;
return;
case 3:
*((unsigned int*)dst) = f;
return;
case 2:
#if COMPRESSION_LEVEL == 0
// 2 byte writes are common in level 0
*((unsigned short int*)dst) = (unsigned short int)f;
#else
*((unsigned int*)dst) = f;
#endif
return;
case 1:
*((unsigned char*)dst) = (unsigned char)f;
return;
}
#endif
}
__inline void memcpy_up(unsigned char *dst, const unsigned char *src, unsigned int n)
{
// cannot be replaced by overlap handling of memmove() due to LZSS algorithm
#ifndef X86X64
if(n > 8 && src + n < dst)
memcpy(dst, src, n);
else
{
unsigned char *end = dst + n;
while(dst < end)
{
*dst = *src;
dst++;
src++;
}
}
#else
if (n < 5)
*((unsigned int*)dst) = *((unsigned int*)src);
else
{
unsigned char *end = dst + n;
while(dst < end)
{
*((unsigned int*)dst) = *((unsigned int*)src);
dst = dst + 4;
src = src + 4;
}
}
#endif
}
__inline unsigned int fast_read_safe(void const *src, unsigned int bytes, const unsigned char *invalid)
{
#ifdef memory_safe
if ((const unsigned char *)src + 4 > (const unsigned char *)invalid)
return 0xffffffff;
#endif
invalid = invalid;
return fast_read(src, bytes);
}
__inline unsigned int qlz_compress_core(const void *source, unsigned char *destination, unsigned int size, const unsigned char *hashtable[][AND + 1], const unsigned char *first_valid, unsigned char *hash_counter)
{
const unsigned char *source_c = (const unsigned char*)source;
unsigned char *destination_c = (unsigned char*)destination;
const unsigned char *last_byte = source_c + size - 1;
const unsigned char *src = source_c;
unsigned char *cword_ptr = destination_c;
unsigned char *dst = destination_c + 4;
unsigned int cword_val = 1U << 31;
const unsigned char *guarantee_uncompressed = last_byte - 8;
#ifdef speedup_incompressible
unsigned char *prev_dst = dst;
const unsigned char *prev_src = src;
#endif
hash_counter = hash_counter;
first_valid = first_valid;
// save first 4 bytes uncompressed
while(src < source_c + 4 && src < guarantee_uncompressed)
{
cword_val = (cword_val >> 1);
*dst = *src;
dst++;
src++;
}
while(src < guarantee_uncompressed)
{
unsigned int fetch;
if ((cword_val & 1) == 1)
{
// check if destinationc pointer could exceed destination buffer
if (dst > destination_c + size)
return 0;
// store control word
fast_write((cword_val >> 1) | (1U << 31), cword_ptr, 4);
cword_ptr = dst;
dst += 4;
cword_val = 1U << 31;
#ifdef speedup_incompressible
// check if source chunk is compressible
if (dst - prev_dst > src - prev_src && src > source_c + 1000)
{
int q;
for(q = 0; q < 30 && src + 31 < guarantee_uncompressed && dst + 35 < destination_c + size; q++)
{
#if(COMPRESSION_LEVEL == 0)
int w;
for(w = 0; w < 31; w++)
{
fetch = fast_read(src + w, 4);
*(unsigned int*)&hashtable[hash_func(fetch)][0] = fast_read(src + w, 4);
hashtable[hash_func(fetch)][1] = src + w;
}
#endif
fast_write((1U << 31), dst - 4, 4);
memcpy(dst, src, 31);
dst += 4*8 - 1 + 4;
src += 4*8 - 1;
prev_src = src;
prev_dst = dst;
cword_ptr = dst - 4;
}
}
#endif
}
#ifdef test_rle
// check for rle sequence
if (fast_read(src, 4) == fast_read(src + 1, 4))
{
const unsigned char *orig_src;
fetch = fast_read(src, 4);
orig_src = src;
do src = src + 4; while (src <= guarantee_uncompressed - 4 && fetch == fast_read(src, 4));
if((src - orig_src) <= 2047)
{
fast_write(((fetch & 0xff) << 16) | (unsigned int)((src - orig_src) << 4) | 15, dst, 4);
dst = dst + 3;
}
else
{
fast_write(((fetch & 0xff) << 16) | 15, dst, 4);
fast_write((unsigned int)(src - orig_src), dst + 3, 4);
dst = dst + 7;
}
cword_val = (cword_val >> 1) | (1 << 31);
}
else
#endif
{
const unsigned char *o;
unsigned int hash, matchlen;
#if(COMPRESSION_LEVEL < 2)
unsigned int cached;
fetch = fast_read(src, 4);
hash = hash_func(fetch);
cached = fetch ^ *(unsigned int*)&hashtable[hash][0];
*(unsigned int*)&hashtable[hash][0] = fetch;
o = hashtable[hash][1];
hashtable[hash][1] = src;
#else
unsigned char c;
unsigned int k, m;
const unsigned char *offset2 = 0;
fetch = fast_read(src, 4);
hash = hash_func(fetch);
matchlen = 0;
c = hash_counter[hash];
for(k = 0; k < AND + 1; k++)
{
o = hashtable[hash][(c - k) & AND];
if(o > first_valid && o < src - 3 && *(src + matchlen) == *(o + matchlen) && (fast_read(o, 3) & 0xffffff) == (fetch & 0xffffff) && src - o < 131071)
{
size_t remaining;
remaining = guarantee_uncompressed - src;
m = 3;
if (fast_read(o, 4) == fetch)
{
while(*(o + m) == *(src + m) && m < remaining)
m++;
}
if (m > matchlen)
{
matchlen = m;
offset2 = o;
}
}
}
o = offset2;
c = (hash_counter[hash] + 1) & AND;
hash_counter[hash] = c;
hashtable[hash][c] = src;
#endif
#if(COMPRESSION_LEVEL == 0)
if (o != 0 && (cached & 0xffffff) == 0 && src - o > 3)
#elif(COMPRESSION_LEVEL == 1)
if ((cached & 0xffffff) == 0 && o > first_valid && o < src - 3 && ((fast_read(o, 3) ^ fast_read(src, 3)) & 0xffffff) == 0 && src - o < 131071)
#elif(COMPRESSION_LEVEL > 1)
if(matchlen == 3)
#endif
{
unsigned int offset;
offset = (unsigned int)(src - o);
#if(COMPRESSION_LEVEL < 2)
if (cached & 0xffffffff)
#endif
{
#if (COMPRESSION_LEVEL > 2)
unsigned int u;
for(u = 1; u < 3; u++)
{
hash = hash_func(fast_read(src + u, 4));
c = (hash_counter[hash] + 1) & AND;
hash_counter[hash] = c;
hashtable[hash][c] = src + u;
}
#endif
#if (COMPRESSION_LEVEL == 0)
cword_val = (cword_val >> 1) | (1U << 31);
fast_write(3 | (hash << 4), dst, 2);
src += 3;
dst += 2;
#else
if(offset <= 63)
{
// encode lz match
*dst = (unsigned char)(offset << 2);
cword_val = (cword_val >> 1) | (1U << 31);
src += 3;
dst++;
}
else if (offset <= 16383)
{
// encode lz match
unsigned int f = (offset << 2) | 1;
fast_write(f, dst, 2);
cword_val = (cword_val >> 1) | (1U << 31);
src += 3;
dst += 2;
}
else
{
// encode literal
*dst = *src;
src++;
dst++;
cword_val = (cword_val >> 1);
}
#endif
}
#if(COMPRESSION_LEVEL > 1)
}
else if(matchlen > 3)
{
#elif(COMPRESSION_LEVEL < 2)
else
#endif
{
// encode lz match
unsigned int offset;
#if(COMPRESSION_LEVEL < 2)
const unsigned char *old_src = src;
offset = (unsigned int)(src - o);
cword_val = (cword_val >> 1) | (1U << 31);
src += 3;
while(*(o + (src - old_src)) == *src && src < guarantee_uncompressed)
src++;
matchlen = (unsigned int)(src - old_src);
#else
unsigned int u;
offset = (unsigned int)(src - o);
cword_val = (cword_val >> 1) | (1U << 31);
#if (COMPRESSION_LEVEL > 2)
for(u = 1; u < matchlen; u++)
#else
for(u = 1; u < matchlen && u < 5; u++)
#endif
{
hash = hash_func(fast_read(src + u, 4));
c = (hash_counter[hash] + 1) & AND;
hash_counter[hash] = c;
hashtable[hash][c] = src + u;
}
src += matchlen;
#endif
#if (COMPRESSION_LEVEL == 0)
if (matchlen < 15)
{
fast_write(matchlen | (hash << 4), dst, 2);
dst += 2;
}
else if (matchlen < 255)
{
fast_write(hash << 4, dst, 2);
*(dst + 2) = (unsigned char)matchlen;
dst += 3;
}
else
{
fast_write(hash << 4, dst, 2);
*(dst + 2) = 0;
fast_write(matchlen, dst + 3, 4);
dst += 7;
}
#else
if (matchlen <= 18 && offset <= 1023)
{
unsigned int f = ((matchlen - 3) << 2) | (offset << 6) | 2;
fast_write(f, dst, 2);
dst += 2;
}
else if(matchlen <= 34 && offset <= 65535)
{
unsigned int f = ((matchlen - 3) << 3) | (offset << 8) | 3;
fast_write(f, dst, 3);
dst += 3;
}
else if (matchlen >= 3)
{
if (matchlen <= 2050)
{
unsigned int f = ((matchlen - 3) << 4) | (offset << 15) | 7;
fast_write(f, dst, 4);
dst += 4;
}
else
{
fast_write(7, dst, 4);
fast_write(matchlen, dst + 4, 4);
fast_write(offset, dst + 8, 4);
dst += 12;
}
}
#endif
}
}
else
{
// encode literal
*dst = *src;
src++;
dst++;
cword_val = (cword_val >> 1);
}
}
}
// save last source bytes as literals
while (src <= last_byte)
{
if ((cword_val & 1) == 1)
{
fast_write((cword_val >> 1) | (1U << 31), cword_ptr, 4);
cword_ptr = dst;
dst += 4;
cword_val = 1U << 31;
}
if (src < last_byte - 2 && src > source_c + 3)
{
hashtable[hash_func(fast_read(src, 4))][1] = src;
*(unsigned int*)&hashtable[hash_func(fast_read(src, 4))][0] = fast_read(src, 4);
}
*dst = *src;
src++;
dst++;
cword_val = (cword_val >> 1);
}
while((cword_val & 1) != 1)
cword_val = (cword_val >> 1);
fast_write((cword_val >> 1) | (1U << 31), cword_ptr, 4);
// min. size must be 9 bytes so that the qlz_size functions can take 9 bytes as argument
if (dst - destination_c < 9)
return 9;
else
return (unsigned int)(dst - destination_c);
}
#if !defined(DECOMPRESSION_ERROR_H)
#define DECOMPRESSION_ERROR_H
// We can't use 0 as error return code, because at 815 line it is used as a success code.
static const size_t DECOMPRESSION_ERROR = (size_t)(-1);
#endif
__inline size_t qlz_decompress_core(const unsigned char *source, void *destination, size_t size, size_t source_size, unsigned char *first_valid, const unsigned char *hashtable[])
{
const unsigned char *source_c = (const unsigned char*)source;
unsigned char *destination_c = (unsigned char*)destination;
const unsigned char *src = source_c;
unsigned char *dst = destination_c;
const unsigned char* last_byte_successor = destination_c + size;
unsigned int cword_val = 1;
const unsigned int bitlut[16] = {4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0};
const unsigned char *guaranteed_uncompressed = last_byte_successor - 4;
unsigned char *last_hashed = destination_c + 3;
first_valid = first_valid;
last_hashed = last_hashed;
hashtable = hashtable;
// prevent spurious memory read on a source with size < 4
if (dst >= guaranteed_uncompressed)
{
src += 4;
while(dst < last_byte_successor)
{
*dst = *src;
dst++;
src++;
}
return (unsigned int)(dst - destination_c);
}
for(;;)
{
unsigned int fetch;
if (cword_val == 1)
{
// fetch control word
cword_val = fast_read_safe(src, 4, source_c + source_size) | (1U << 31);
src += 4;
}
fetch = fast_read_safe(src, 4, source_c + source_size);
// check if we must decode lz match
if ((cword_val & 1) == 1)
{
unsigned int matchlen;
#if(COMPRESSION_LEVEL == 0)
unsigned int hash;
const unsigned char *offset2;
cword_val = cword_val >> 1;
if((fetch & 0xf) != 15)
{
hash = (fetch >> 4) & 0xfff;
offset2 = hashtable[hash];
if((fetch & 0xf) != 0)
{
matchlen = (fetch & 0xf);
src += 2;
}
else if((fetch & 0x00ff0000) != 0)
{
matchlen = *(src + 2);
src += 3;
}
else
{
matchlen = fast_read(src + 3, 4);
src += 7;
}
// This check prevents segfault for some cases with invalid data.
// not sure about guaranteed_decompressed so assuming at most size bytes (decompressed size)
if (EXPECT_FALSE(dst + matchlen > destination_c + size || offset2 == NULL)) {
return DECOMPRESSION_ERROR;
}
memcpy_up(dst, offset2, matchlen);
while(last_hashed < dst)
{
last_hashed++;
hashtable[hash_func(fast_read(last_hashed, 4))] = last_hashed;
}
dst += matchlen;
last_hashed = dst - 1;
}
#else
unsigned int offset;
cword_val = cword_val >> 1;
if ((fetch & 3) == 0)
{
offset = (fetch & 0xff) >> 2;
#ifdef memory_safe
if (3 > (unsigned int)(guaranteed_uncompressed - dst) || offset > (unsigned int)(dst - first_valid))
return 0;
#endif
memcpy_up(dst, dst - offset, 3);
dst += 3;
src++;
}
else if ((fetch & 2) == 0)
{
offset = (fetch & 0xffff) >> 2;
#ifdef memory_safe
if (3 > (unsigned int)(guaranteed_uncompressed - dst) || offset > (unsigned int)(dst - first_valid))
return 0;
#endif
memcpy_up(dst, dst - offset, 3);
dst += 3;
src += 2;
}
else if ((fetch & 1) == 0)
{
offset = (fetch & 0xffff) >> 6;
matchlen = ((fetch >> 2) & 15) + 3;
#ifdef memory_safe
if (matchlen > (unsigned int)(guaranteed_uncompressed - dst) || offset > (unsigned int)(dst - first_valid))
return 0;
#endif
memcpy_up(dst, dst - offset, matchlen);
src += 2;
dst += matchlen;
}
else if ((fetch & 4) == 0)
{
offset = (fetch & 0xffffff) >> 8;
matchlen = ((fetch >> 3) & 31) + 3;
#ifdef memory_safe
if (matchlen > (unsigned int)(guaranteed_uncompressed - dst) || offset > (unsigned int)(dst - first_valid))
return 0;
#endif
memcpy_up(dst, dst - offset, matchlen);
src += 3;
dst += matchlen;
}
else if ((fetch & 8) == 0)
{
offset = (fetch >> 15);
if (offset != 0)
{
matchlen = ((fetch >> 4) & 2047) + 3;
src += 4;
}
else
{
matchlen = fast_read_safe(src + 4, 4, source_c + source_size);
offset = fast_read_safe(src + 8, 4, source_c + source_size);
src += 12;
}
#ifdef memory_safe
if (matchlen > (unsigned int)(guaranteed_uncompressed - dst) || offset > (unsigned int)(dst - first_valid))
return 0;
#endif
memcpy_up(dst, dst - offset, matchlen);
dst += matchlen;
}
#endif
else
{
// decode rle sequence
unsigned char rle_char;
rle_char = (unsigned char)(fetch >> 16);
matchlen = ((fetch >> 4) & 0xfff);
if(matchlen != 0)
src += 3;
else
{
matchlen = fast_read_safe(src + 3, 4, source_c + source_size);
src += 7;
}
#ifdef memory_safe
if(matchlen > (unsigned int)(guaranteed_uncompressed - dst))
return 0;
#endif
memset(dst, rle_char, matchlen);
#if(COMPRESSION_LEVEL == 0)
while(last_hashed < dst - 1)
{
last_hashed++;
hashtable[hash_func(fast_read(last_hashed, 4))] = last_hashed;
}
last_hashed = dst - 1 + matchlen;
#endif
dst += matchlen;
}
}
else
{
// decode literal
#ifdef memory_safe
if (4 > destination_c + size - dst || src > source_c + source_size + 4)
return 0;
#endif
memcpy_up(dst, src, 4);
dst += bitlut[cword_val & 0xf];
src += bitlut[cword_val & 0xf];
cword_val = cword_val >> (bitlut[cword_val & 0xf]);
#if(COMPRESSION_LEVEL == 0)
while(last_hashed < dst - 3)
{
last_hashed++;
hashtable[hash_func(fast_read(last_hashed, 4))] = last_hashed;
}
#endif
if (dst >= guaranteed_uncompressed)
{
// decode last literals and exit
while(dst < last_byte_successor)
{
if (cword_val == 1)
{
src += 4;
cword_val = 1U << 31;
}
if (1 > destination_c + size - dst)
return 0;
*dst = *src;
dst++;
src++;
cword_val = cword_val >> 1;
}
#if(COMPRESSION_LEVEL == 0)
while(last_hashed < last_byte_successor - 4)
{
last_hashed++;
hashtable[hash_func(fast_read(last_hashed, 4))] = last_hashed;
}
#endif
if((src - 1) - source_c > 8) // 8 bytes comp. size excessive len is ok
return 0;
else if(dst - destination_c - size == 0)
return size;
else
return 0;
}
}
}
}
size_t qlz_size_decompressed(const char *source)
{
unsigned int n, r;
n = (((*source) & 2) == 2) ? 4 : 1;
r = fast_read(source + 1 + n, n);
r = r & (0xffffffff >> ((4 - n)*8));
return r;
}
size_t qlz_size_compressed(const char *source)
{
unsigned int n, r;
n = (((*source) & 2) == 2) ? 4 : 1;
r = fast_read(source + 1, n);
r = r & (0xffffffff >> ((4 - n)*8));
return r;
}
size_t qlz_compress(const void *source, char *destination, size_t size, char *scratch)
{
// 1-8 bytes for aligning (not 0-7!); 8 bytes for buffersize (padds on 32 bit cpu); HASH_SIZE hash table; STREAMING_MODE_ROUNDED bytes streambuffer; optional HASH_ENTRIES byte hash counter
unsigned char *buffer_aligned = (unsigned char *)scratch + 8 - (((size_t)scratch) % 8);
const unsigned char *(*hashtable)[AND + 1] = (const unsigned char *(*)[AND + 1])(buffer_aligned + 8);
size_t *buffersize = (size_t *)buffer_aligned;
unsigned char *streambuffer = buffer_aligned + 8 + HASH_SIZE;
unsigned int r;
unsigned int compressed, base;
unsigned char *hash_counter = streambuffer + STREAMING_MODE_ROUNDED;
if(size == 0 || size > 0xffffffff)
return 0;
#if (COMPRESSION_LEVEL == 0 && STREAMING_MODE_ROUNDED == 0)
memset((void *)hashtable, 0, HASH_SIZE);
#endif
if(size < 216)
base = 3;
else
base = 9;
// if not STREAMING_MODE, then STREAMING_MODE_ROUNDED == 0 and first case (streaming buffer full) is executed unconditionally, functioning as block comp.
if (*buffersize + size - 1 >= STREAMING_MODE_ROUNDED)
{
#if (COMPRESSION_LEVEL == 0 && STREAMING_MODE_ROUNDED != 0)
memset((void *)hashtable, 0, HASH_SIZE);
#endif
r = base + qlz_compress_core(source, (unsigned char*)destination + base, (unsigned int)size, hashtable, (const unsigned char*)source, hash_counter);
#if (COMPRESSION_LEVEL == 0 && STREAMING_MODE_ROUNDED != 0)
memset((void *)hashtable, 0, HASH_SIZE);
#endif
if(r == base)
{
memcpy(destination + base, source, size);
r = (unsigned int)size + base;
compressed = 0;
}
else
compressed = 1;
*buffersize = 0;
}
else
{
memcpy(streambuffer + *buffersize, source, size);
r = base + qlz_compress_core(streambuffer + *buffersize, (unsigned char*)destination + base, (unsigned int)size, hashtable, streambuffer, hash_counter);
if(r == base)
{
memcpy(destination + base, streambuffer + *buffersize, size);
r = (unsigned int)size + base;
compressed = 0;
memset((void*)hashtable, 0, HASH_SIZE);
}
else
compressed = 1;
*buffersize += size;
}
if(base == 3)
{
*destination = (unsigned char)(0 | compressed);
*(destination + 1) = (unsigned char)r;
*(destination + 2) = (unsigned char)size;
}
else
{
*destination = (unsigned char)(2 | compressed);
fast_write(r, destination + 1, 4);
fast_write((unsigned int)size, destination + 5, 4);
}
#if (COMPRESSION_LEVEL == 0)
*destination = (*destination) | 4;
#endif
return (size_t)r;
}
// returns DECOMPRESSION_ERROR in case of an error, or decompressed size on success
size_t qlz_decompress(const char *source, void *destination, char *scratch)
{
// 1-8 bytes for aligning (not 0-7!); 8 bytes for buffersize (padds on 32bit cpu); STREAMING_MODE_ROUNDED streambuffer; HASH_SIZE hash table
unsigned char *buffer_aligned = (unsigned char *)scratch + 8 - (((size_t)scratch) % 8);
size_t *buffersize = (size_t *)buffer_aligned;
unsigned int headerlen = 2*((((*source) & 2) == 2) ? 4 : 1) + 1; // get header len
unsigned char *streambuffer = buffer_aligned + 8;
const unsigned char **hashtable = (const unsigned char **)(streambuffer + STREAMING_MODE_ROUNDED);
size_t dsiz = qlz_size_decompressed((char *)source);
size_t csiz = qlz_size_compressed((char *)source);
if (*buffersize + qlz_size_decompressed((char *)source) - 1 >= STREAMING_MODE_ROUNDED)
{
if((*source & 1) == 1) {
if (qlz_decompress_core((const unsigned char *)source + headerlen, destination, dsiz, csiz, (unsigned char*)destination, hashtable) == DECOMPRESSION_ERROR) {
return DECOMPRESSION_ERROR;
}
} else {
memcpy(destination, source + headerlen, dsiz);
}
*buffersize = 0;
}
else
{
if((*source & 1) == 1) {
if (qlz_decompress_core((const unsigned char *)source + headerlen, streambuffer + *buffersize, dsiz, csiz, streambuffer, hashtable) == DECOMPRESSION_ERROR) {
return DECOMPRESSION_ERROR;
}
} else {
memcpy(streambuffer + *buffersize, source + headerlen, dsiz);
}
memcpy(destination, streambuffer + *buffersize, dsiz);
*buffersize += dsiz;
}
return dsiz;
}
#undef __inline
|