forked from haproxy/haproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslz.c
1421 lines (1198 loc) · 45.4 KB
/
slz.c
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 (C) 2013-2015 Willy Tarreau <w@1wt.eu>
*
* 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.
*/
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <import/slz.h>
#include <import/slz-tables.h>
/* First, RFC1951-specific declarations and extracts from the RFC.
*
* RFC1951 - deflate stream format
* Data elements are packed into bytes in order of
increasing bit number within the byte, i.e., starting
with the least-significant bit of the byte.
* Data elements other than Huffman codes are packed
starting with the least-significant bit of the data
element.
* Huffman codes are packed starting with the most-
significant bit of the code.
3.2.3. Details of block format
Each block of compressed data begins with 3 header bits
containing the following data:
first bit BFINAL
next 2 bits BTYPE
Note that the header bits do not necessarily begin on a byte
boundary, since a block does not necessarily occupy an integral
number of bytes.
BFINAL is set if and only if this is the last block of the data
set.
BTYPE specifies how the data are compressed, as follows:
00 - no compression
01 - compressed with fixed Huffman codes
10 - compressed with dynamic Huffman codes
11 - reserved (error)
3.2.4. Non-compressed blocks (BTYPE=00)
Any bits of input up to the next byte boundary are ignored.
The rest of the block consists of the following information:
0 1 2 3 4...
+---+---+---+---+================================+
| LEN | NLEN |... LEN bytes of literal data...|
+---+---+---+---+================================+
LEN is the number of data bytes in the block. NLEN is the
one's complement of LEN.
3.2.5. Compressed blocks (length and distance codes)
As noted above, encoded data blocks in the "deflate" format
consist of sequences of symbols drawn from three conceptually
distinct alphabets: either literal bytes, from the alphabet of
byte values (0..255), or <length, backward distance> pairs,
where the length is drawn from (3..258) and the distance is
drawn from (1..32,768). In fact, the literal and length
alphabets are merged into a single alphabet (0..285), where
values 0..255 represent literal bytes, the value 256 indicates
end-of-block, and values 257..285 represent length codes
(possibly in conjunction with extra bits following the symbol
code) as follows:
Length encoding :
Extra Extra Extra
Code Bits Length(s) Code Bits Lengths Code Bits Length(s)
---- ---- ------ ---- ---- ------- ---- ---- -------
257 0 3 267 1 15,16 277 4 67-82
258 0 4 268 1 17,18 278 4 83-98
259 0 5 269 2 19-22 279 4 99-114
260 0 6 270 2 23-26 280 4 115-130
261 0 7 271 2 27-30 281 5 131-162
262 0 8 272 2 31-34 282 5 163-194
263 0 9 273 3 35-42 283 5 195-226
264 0 10 274 3 43-50 284 5 227-257
265 1 11,12 275 3 51-58 285 0 258
266 1 13,14 276 3 59-66
Distance encoding :
Extra Extra Extra
Code Bits Dist Code Bits Dist Code Bits Distance
---- ---- ---- ---- ---- ------ ---- ---- --------
0 0 1 10 4 33-48 20 9 1025-1536
1 0 2 11 4 49-64 21 9 1537-2048
2 0 3 12 5 65-96 22 10 2049-3072
3 0 4 13 5 97-128 23 10 3073-4096
4 1 5,6 14 6 129-192 24 11 4097-6144
5 1 7,8 15 6 193-256 25 11 6145-8192
6 2 9-12 16 7 257-384 26 12 8193-12288
7 2 13-16 17 7 385-512 27 12 12289-16384
8 3 17-24 18 8 513-768 28 13 16385-24576
9 3 25-32 19 8 769-1024 29 13 24577-32768
3.2.6. Compression with fixed Huffman codes (BTYPE=01)
The Huffman codes for the two alphabets are fixed, and are not
represented explicitly in the data. The Huffman code lengths
for the literal/length alphabet are:
Lit Value Bits Codes
--------- ---- -----
0 - 143 8 00110000 through
10111111
144 - 255 9 110010000 through
111111111
256 - 279 7 0000000 through
0010111
280 - 287 8 11000000 through
11000111
The code lengths are sufficient to generate the actual codes,
as described above; we show the codes in the table for added
clarity. Literal/length values 286-287 will never actually
occur in the compressed data, but participate in the code
construction.
Distance codes 0-31 are represented by (fixed-length) 5-bit
codes, with possible additional bits as shown in the table
shown in Paragraph 3.2.5, above. Note that distance codes 30-
31 will never actually occur in the compressed data.
*/
/* back references, built in a way that is optimal for 32/64 bits */
union ref {
struct {
uint32_t pos;
uint32_t word;
} by32;
uint64_t by64;
};
#if defined(USE_64BIT_QUEUE) && defined(UNALIGNED_LE_OK)
/* enqueue code x of <xbits> bits (LSB aligned, at most 24) and copy complete
* 32-bit words into output buffer. X must not contain non-zero bits above
* xbits.
*/
static inline void enqueue24(struct slz_stream *strm, uint32_t x, uint32_t xbits)
{
uint64_t queue = strm->queue + ((uint64_t)x << strm->qbits);
uint32_t qbits = strm->qbits + xbits;
if (__builtin_expect(qbits >= 32, 1)) {
*(uint32_t *)strm->outbuf = queue;
queue >>= 32;
qbits -= 32;
strm->outbuf += 4;
}
strm->queue = queue;
strm->qbits = qbits;
}
#define enqueue8 enqueue24
/* flush the queue and align to next byte */
static inline void flush_bits(struct slz_stream *strm)
{
if (strm->qbits > 0)
*strm->outbuf++ = strm->queue;
if (strm->qbits > 8)
*strm->outbuf++ = strm->queue >> 8;
if (strm->qbits > 16)
*strm->outbuf++ = strm->queue >> 16;
if (strm->qbits > 24)
*strm->outbuf++ = strm->queue >> 24;
strm->queue = 0;
strm->qbits = 0;
}
#else /* non-64 bit or aligned or big endian */
/* enqueue code x of <xbits> bits (LSB aligned, at most 24) and copy complete
* bytes into out buf. X must not contain non-zero bits above xbits. Prefer
* enqueue8() when xbits is known for being 8 or less.
*/
static void enqueue24(struct slz_stream *strm, uint32_t x, uint32_t xbits)
{
uint32_t queue = strm->queue + (x << strm->qbits);
uint32_t qbits = strm->qbits + xbits;
if (qbits >= 16) {
#ifndef UNALIGNED_LE_OK
strm->outbuf[0] = queue;
strm->outbuf[1] = queue >> 8;
#else
*(uint16_t *)strm->outbuf = queue;
#endif
strm->outbuf += 2;
queue >>= 16;
qbits -= 16;
}
if (qbits >= 8) {
qbits -= 8;
*strm->outbuf++ = queue;
queue >>= 8;
}
strm->qbits = qbits;
strm->queue = queue;
return;
}
/* enqueue code x of <xbits> bits (at most 8) and copy complete bytes into
* out buf. X must not contain non-zero bits above xbits.
*/
static inline void enqueue8(struct slz_stream *strm, uint32_t x, uint32_t xbits)
{
uint32_t queue = strm->queue + (x << strm->qbits);
uint32_t qbits = strm->qbits + xbits;
if (__builtin_expect((signed)(qbits - 8) >= 0, 1)) {
qbits -= 8;
*strm->outbuf++ = queue;
queue >>= 8;
}
strm->qbits = qbits;
strm->queue = queue;
}
/* align to next byte */
static inline void flush_bits(struct slz_stream *strm)
{
if (strm->qbits > 0)
*strm->outbuf++ = strm->queue;
if (strm->qbits > 8)
*strm->outbuf++ = strm->queue >> 8;
strm->queue = 0;
strm->qbits = 0;
}
#endif
/* only valid if buffer is already aligned */
static inline void copy_8b(struct slz_stream *strm, uint32_t x)
{
*strm->outbuf++ = x;
}
/* only valid if buffer is already aligned */
static inline void copy_16b(struct slz_stream *strm, uint32_t x)
{
strm->outbuf[0] = x;
strm->outbuf[1] = x >> 8;
strm->outbuf += 2;
}
/* only valid if buffer is already aligned */
static inline void copy_32b(struct slz_stream *strm, uint32_t x)
{
strm->outbuf[0] = x;
strm->outbuf[1] = x >> 8;
strm->outbuf[2] = x >> 16;
strm->outbuf[3] = x >> 24;
strm->outbuf += 4;
}
static inline void send_huff(struct slz_stream *strm, uint32_t code)
{
uint32_t bits;
code = fixed_huff[code];
bits = code & 15;
code >>= 4;
enqueue24(strm, code, bits);
}
static inline void send_eob(struct slz_stream *strm)
{
enqueue8(strm, 0, 7); // direct encoding of 256 = EOB (cf RFC1951)
}
/* copies <len> literals from <buf>. <more> indicates that there are data past
* buf + <len>. <len> must not be null.
*/
static void copy_lit(struct slz_stream *strm, const void *buf, uint32_t len, int more)
{
uint32_t len2;
do {
len2 = len;
if (__builtin_expect(len2 > 65535, 0))
len2 = 65535;
len -= len2;
if (strm->state != SLZ_ST_EOB)
send_eob(strm);
strm->state = (more || len) ? SLZ_ST_EOB : SLZ_ST_DONE;
enqueue8(strm, !(more || len), 3); // BFINAL = !more ; BTYPE = 00
flush_bits(strm);
copy_16b(strm, len2); // len2
copy_16b(strm, ~len2); // nlen2
memcpy(strm->outbuf, buf, len2);
buf += len2;
strm->outbuf += len2;
} while (len);
}
/* copies <len> literals from <buf>. <more> indicates that there are data past
* buf + <len>. <len> must not be null.
*/
static void copy_lit_huff(struct slz_stream *strm, const unsigned char *buf, uint32_t len, int more)
{
uint32_t pos;
/* This ugly construct limits the mount of tests and optimizes for the
* most common case (more > 0).
*/
if (strm->state == SLZ_ST_EOB) {
eob:
strm->state = more ? SLZ_ST_FIXED : SLZ_ST_LAST;
enqueue8(strm, 2 + !more, 3); // BFINAL = !more ; BTYPE = 01
}
else if (!more) {
send_eob(strm);
goto eob;
}
pos = 0;
do {
send_huff(strm, buf[pos++]);
} while (pos < len);
}
/* format:
* bit0..31 = word
* bit32..63 = last position in buffer of similar content
*/
/* This hash provides good average results on HTML contents, and is among the
* few which provide almost optimal results on various different pages.
*/
static inline uint32_t slz_hash(uint32_t a)
{
#if defined(__ARM_FEATURE_CRC32)
# if defined(__ARM_ARCH_ISA_A64)
// 64 bit mode
__asm__ volatile("crc32w %w0,%w0,%w1" : "+r"(a) : "r"(0));
# else
// 32 bit mode (e.g. armv7 compiler building for armv8
__asm__ volatile("crc32w %0,%0,%1" : "+r"(a) : "r"(0));
# endif
return a >> (32 - HASH_BITS);
#else
return ((a << 19) + (a << 6) - a) >> (32 - HASH_BITS);
#endif
}
/* This function compares buffers <a> and <b> and reads 32 or 64 bits at a time
* during the approach. It makes us of unaligned little endian memory accesses
* on capable architectures. <max> is the maximum number of bytes that can be
* read, so both <a> and <b> must have at least <max> bytes ahead. <max> may
* safely be null or negative if that simplifies computations in the caller.
*/
static inline long memmatch(const unsigned char *a, const unsigned char *b, long max)
{
long len = 0;
#ifdef UNALIGNED_LE_OK
unsigned long xor;
while (1) {
if ((long)(len + 2 * sizeof(long)) > max) {
while (len < max) {
if (a[len] != b[len])
break;
len++;
}
return len;
}
xor = *(long *)&a[len] ^ *(long *)&b[len];
if (xor)
break;
len += sizeof(long);
xor = *(long *)&a[len] ^ *(long *)&b[len];
if (xor)
break;
len += sizeof(long);
}
#if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
/* x86 has bsf. We know that xor is non-null here */
asm("bsf %1,%0\n" : "=r"(xor) : "0" (xor));
return len + xor / 8;
#else
if (sizeof(long) > 4 && !(xor & 0xffffffff)) {
/* This code is optimized out on 32-bit archs, but we still
* need to shift in two passes to avoid a warning. It is
* properly optimized out as a single shift.
*/
xor >>= 16; xor >>= 16;
if (xor & 0xffff) {
if (xor & 0xff)
return len + 4;
return len + 5;
}
if (xor & 0xffffff)
return len + 6;
return len + 7;
}
if (xor & 0xffff) {
if (xor & 0xff)
return len;
return len + 1;
}
if (xor & 0xffffff)
return len + 2;
return len + 3;
#endif // x86
#else // UNALIGNED_LE_OK
/* This is the generic version for big endian or unaligned-incompatible
* architectures.
*/
while (len < max) {
if (a[len] != b[len])
break;
len++;
}
return len;
#endif
}
/* sets <count> BYTES to -32769 in <refs> so that any uninitialized entry will
* verify (pos-last-1 >= 32768) and be ignored. <count> must be a multiple of
* 128 bytes and <refs> must be at least one count in length. It's supposed to
* be applied to 64-bit aligned data exclusively, which makes it slightly
* faster than the regular memset() since no alignment check is performed.
*/
static void reset_refs(union ref *refs, long count)
{
/* avoid a shift/mask by casting to void* */
union ref *end = (void *)refs + count;
do {
refs[ 0].by64 = -32769;
refs[ 1].by64 = -32769;
refs[ 2].by64 = -32769;
refs[ 3].by64 = -32769;
refs[ 4].by64 = -32769;
refs[ 5].by64 = -32769;
refs[ 6].by64 = -32769;
refs[ 7].by64 = -32769;
refs[ 8].by64 = -32769;
refs[ 9].by64 = -32769;
refs[10].by64 = -32769;
refs[11].by64 = -32769;
refs[12].by64 = -32769;
refs[13].by64 = -32769;
refs[14].by64 = -32769;
refs[15].by64 = -32769;
refs += 16;
} while (refs < end);
}
/* Compresses <ilen> bytes from <in> into <out> according to RFC1951. The
* output result may be up to 5 bytes larger than the input, to which 2 extra
* bytes may be added to send the last chunk due to BFINAL+EOB encoding (10
* bits) when <more> is not set. The caller is responsible for ensuring there
* is enough room in the output buffer for this. The amount of output bytes is
* returned, and no CRC is computed.
*/
long slz_rfc1951_encode(struct slz_stream *strm, unsigned char *out, const unsigned char *in, long ilen, int more)
{
long rem = ilen;
unsigned long pos = 0;
unsigned long last;
uint32_t word = 0;
long mlen;
uint32_t h;
uint64_t ent;
uint32_t plit = 0;
uint32_t bit9 = 0;
uint32_t dist, code;
union ref refs[1 << HASH_BITS];
if (!strm->level) {
/* force to send as literals (eg to preserve CPU) */
strm->outbuf = out;
plit = pos = ilen;
bit9 = 52; /* force literal dump */
goto final_lit_dump;
}
reset_refs(refs, sizeof(refs));
strm->outbuf = out;
#ifndef UNALIGNED_FASTER
word = ((unsigned char)in[pos] << 8) + ((unsigned char)in[pos + 1] << 16) + ((unsigned char)in[pos + 2] << 24);
#endif
while (rem >= 4) {
#ifndef UNALIGNED_FASTER
word = ((unsigned char)in[pos + 3] << 24) + (word >> 8);
#else
word = *(uint32_t *)&in[pos];
#endif
h = slz_hash(word);
asm volatile ("" ::); // prevent gcc from trying to be smart with the prefetch
if (sizeof(long) >= 8) {
ent = refs[h].by64;
last = (uint32_t)ent;
ent >>= 32;
refs[h].by64 = ((uint64_t)pos) + ((uint64_t)word << 32);
} else {
ent = refs[h].by32.word;
last = refs[h].by32.pos;
refs[h].by32.pos = pos;
refs[h].by32.word = word;
}
#ifdef FIND_OPTIMAL_MATCH
/* Experimental code to see what could be saved with an ideal
* longest match lookup algorithm. This one is very slow but
* scans the whole window. In short, here are the savings :
* file orig fast(ratio) optimal(ratio)
* README 5185 3419 (65.9%) 3165 (61.0%) -7.5%
* index.html 76799 35662 (46.4%) 29875 (38.9%) -16.3%
* rfc1952.c 29383 13442 (45.7%) 11793 (40.1%) -12.3%
*
* Thus the savings to expect for large files is at best 16%.
*
* A non-colliding hash gives 33025 instead of 35662 (-7.4%),
* and keeping the last two entries gives 31724 (-11.0%).
*/
unsigned long scan;
int saved = 0;
int bestpos = 0;
int bestlen = 0;
int firstlen = 0;
int max_lookup = 2; // 0 = no limit
for (scan = pos - 1; scan < pos && (unsigned long)(pos - scan - 1) < 32768; scan--) {
int len;
if (*(uint32_t *)(in + scan) != word)
continue;
len = memmatch(in + pos, in + scan, rem);
if (!bestlen)
firstlen = len;
if (len > bestlen) {
bestlen = len;
bestpos = scan;
}
if (!--max_lookup)
break;
}
if (bestlen) {
//printf("pos=%d last=%d bestpos=%d word=%08x ent=%08x len=%d\n",
// (int)pos, (int)last, (int)bestpos, (int)word, (int)ent, bestlen);
last = bestpos;
ent = word;
saved += bestlen - firstlen;
}
//fprintf(stderr, "first=%d best=%d saved_total=%d\n", firstlen, bestlen, saved);
#endif
if ((uint32_t)ent != word) {
send_as_lit:
rem--;
plit++;
bit9 += ((unsigned char)word >= 144);
pos++;
continue;
}
/* We reject pos = last and pos > last+32768 */
if ((unsigned long)(pos - last - 1) >= 32768)
goto send_as_lit;
/* Note: cannot encode a length larger than 258 bytes */
mlen = memmatch(in + pos + 4, in + last + 4, (rem > 258 ? 258 : rem) - 4) + 4;
/* found a matching entry */
if (bit9 >= 52 && mlen < 6)
goto send_as_lit;
/* compute the output code, its size and the length's size in
* bits to know if the reference is cheaper than literals.
*/
code = len_fh[mlen];
/* direct mapping of dist->huffman code */
dist = fh_dist_table[pos - last - 1];
/* if encoding the dist+length is more expensive than sending
* the equivalent as bytes, lets keep the literals.
*/
if ((dist & 0x1f) + (code >> 16) + 8 >= 8 * mlen + bit9)
goto send_as_lit;
/* first, copy pending literals */
if (plit) {
/* Huffman encoding requires 9 bits for octets 144..255, so this
* is a waste of space for binary data. Switching between Huffman
* and no-comp then huffman consumes 52 bits (7 for EOB + 3 for
* block type + 7 for alignment + 32 for LEN+NLEN + 3 for next
* block. Only use plain literals if there are more than 52 bits
* to save then.
*/
if (bit9 >= 52)
copy_lit(strm, in + pos - plit, plit, 1);
else
copy_lit_huff(strm, in + pos - plit, plit, 1);
plit = 0;
}
/* use mode 01 - fixed huffman */
if (strm->state == SLZ_ST_EOB) {
strm->state = SLZ_ST_FIXED;
enqueue8(strm, 0x02, 3); // BTYPE = 01, BFINAL = 0
}
/* copy the length first */
enqueue24(strm, code & 0xFFFF, code >> 16);
/* in fixed huffman mode, dist is fixed 5 bits */
enqueue24(strm, dist >> 5, dist & 0x1f);
bit9 = 0;
rem -= mlen;
pos += mlen;
#ifndef UNALIGNED_FASTER
#ifdef UNALIGNED_LE_OK
word = *(uint32_t *)&in[pos - 1];
#else
word = ((unsigned char)in[pos] << 8) + ((unsigned char)in[pos + 1] << 16) + ((unsigned char)in[pos + 2] << 24);
#endif
#endif
}
if (__builtin_expect(rem, 0)) {
/* we're reading the 1..3 last bytes */
plit += rem;
do {
bit9 += ((unsigned char)in[pos++] >= 144);
} while (--rem);
}
final_lit_dump:
/* now copy remaining literals or mark the end */
if (plit) {
if (bit9 >= 52)
copy_lit(strm, in + pos - plit, plit, more);
else
copy_lit_huff(strm, in + pos - plit, plit, more);
plit = 0;
}
strm->ilen += ilen;
return strm->outbuf - out;
}
/* Initializes stream <strm> for use with raw deflate (rfc1951). The CRC is
* unused but set to zero. The compression level passed in <level> is set. This
* value can only be 0 (no compression) or 1 (compression) and other values
* will lead to unpredictable behaviour. The function always returns 0.
*/
int slz_rfc1951_init(struct slz_stream *strm, int level)
{
strm->state = SLZ_ST_EOB; // no header
strm->level = level;
strm->format = SLZ_FMT_DEFLATE;
strm->crc32 = 0;
strm->ilen = 0;
strm->qbits = 0;
strm->queue = 0;
return 0;
}
/* Flushes any pending data for stream <strm> into buffer <buf>, then emits an
* empty literal block to byte-align the output, allowing to completely flush
* the queue. This requires that the output buffer still has the size of the
* queue available (up to 4 bytes), plus one byte for (BFINAL,BTYPE), plus 4
* bytes for LEN+NLEN, or a total of 9 bytes in the worst case. The number of
* bytes emitted is returned. It is guaranteed that the queue is empty on
* return. This may cause some overhead by adding needless 5-byte blocks if
* called to often.
*/
int slz_rfc1951_flush(struct slz_stream *strm, unsigned char *buf)
{
strm->outbuf = buf;
/* The queue is always empty on INIT, DONE, and END */
if (!strm->qbits)
return 0;
/* we may need to terminate a huffman output. Lit is always in EOB state */
if (strm->state != SLZ_ST_EOB) {
strm->state = (strm->state == SLZ_ST_LAST) ? SLZ_ST_DONE : SLZ_ST_EOB;
send_eob(strm);
}
/* send BFINAL according to state, and BTYPE=00 (lit) */
enqueue8(strm, (strm->state == SLZ_ST_DONE) ? 1 : 0, 3);
flush_bits(strm); // emit pending bits
copy_32b(strm, 0xFFFF0000U); // len=0, nlen=~0
/* Now the queue is empty, EOB was sent, BFINAL might have been sent if
* we completed the last block, and a zero-byte block was sent to byte-
* align the output. The last state reflects all this. Let's just
* return the number of bytes added to the output buffer.
*/
return strm->outbuf - buf;
}
/* Flushes any pending for stream <strm> into buffer <buf>, then sends BTYPE=1
* and BFINAL=1 if needed. The stream ends in SLZ_ST_DONE. It returns the number
* of bytes emitted. The trailer consists in flushing the possibly pending bits
* from the queue (up to 7 bits), then possibly EOB (7 bits), then 3 bits, EOB,
* a rounding to the next byte, which amounts to a total of 4 bytes max, that
* the caller must ensure are available before calling the function.
*/
int slz_rfc1951_finish(struct slz_stream *strm, unsigned char *buf)
{
strm->outbuf = buf;
if (strm->state == SLZ_ST_FIXED || strm->state == SLZ_ST_LAST) {
strm->state = (strm->state == SLZ_ST_LAST) ? SLZ_ST_DONE : SLZ_ST_EOB;
send_eob(strm);
}
if (strm->state != SLZ_ST_DONE) {
/* send BTYPE=1, BFINAL=1 */
enqueue8(strm, 3, 3);
send_eob(strm);
strm->state = SLZ_ST_DONE;
}
flush_bits(strm);
return strm->outbuf - buf;
}
/* Now RFC1952-specific declarations and extracts from RFC.
* From RFC1952 about the GZIP file format :
A gzip file consists of a series of "members" ...
2.3. Member format
Each member has the following structure:
+---+---+---+---+---+---+---+---+---+---+
|ID1|ID2|CM |FLG| MTIME |XFL|OS | (more-->)
+---+---+---+---+---+---+---+---+---+---+
(if FLG.FEXTRA set)
+---+---+=================================+
| XLEN |...XLEN bytes of "extra field"...| (more-->)
+---+---+=================================+
(if FLG.FNAME set)
+=========================================+
|...original file name, zero-terminated...| (more-->)
+=========================================+
(if FLG.FCOMMENT set)
+===================================+
|...file comment, zero-terminated...| (more-->)
+===================================+
(if FLG.FHCRC set)
+---+---+
| CRC16 |
+---+---+
+=======================+
|...compressed blocks...| (more-->)
+=======================+
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| CRC32 | ISIZE |
+---+---+---+---+---+---+---+---+
2.3.1. Member header and trailer
ID1 (IDentification 1)
ID2 (IDentification 2)
These have the fixed values ID1 = 31 (0x1f, \037), ID2 = 139
(0x8b, \213), to identify the file as being in gzip format.
CM (Compression Method)
This identifies the compression method used in the file. CM
= 0-7 are reserved. CM = 8 denotes the "deflate"
compression method, which is the one customarily used by
gzip and which is documented elsewhere.
FLG (FLaGs)
This flag byte is divided into individual bits as follows:
bit 0 FTEXT
bit 1 FHCRC
bit 2 FEXTRA
bit 3 FNAME
bit 4 FCOMMENT
bit 5 reserved
bit 6 reserved
bit 7 reserved
Reserved FLG bits must be zero.
MTIME (Modification TIME)
This gives the most recent modification time of the original
file being compressed. The time is in Unix format, i.e.,
seconds since 00:00:00 GMT, Jan. 1, 1970. (Note that this
may cause problems for MS-DOS and other systems that use
local rather than Universal time.) If the compressed data
did not come from a file, MTIME is set to the time at which
compression started. MTIME = 0 means no time stamp is
available.
XFL (eXtra FLags)
These flags are available for use by specific compression
methods. The "deflate" method (CM = 8) sets these flags as
follows:
XFL = 2 - compressor used maximum compression,
slowest algorithm
XFL = 4 - compressor used fastest algorithm
OS (Operating System)
This identifies the type of file system on which compression
took place. This may be useful in determining end-of-line
convention for text files. The currently defined values are
as follows:
0 - FAT filesystem (MS-DOS, OS/2, NT/Win32)
1 - Amiga
2 - VMS (or OpenVMS)
3 - Unix
4 - VM/CMS
5 - Atari TOS
6 - HPFS filesystem (OS/2, NT)
7 - Macintosh
8 - Z-System
9 - CP/M
10 - TOPS-20
11 - NTFS filesystem (NT)
12 - QDOS
13 - Acorn RISCOS
255 - unknown
==> A file compressed using "gzip -1" on Unix-like systems can be :
1F 8B 08 00 00 00 00 00 04 03
<deflate-compressed stream>
crc32 size32
*/
static const unsigned char gzip_hdr[] = { 0x1F, 0x8B, // ID1, ID2
0x08, 0x00, // Deflate, flags (none)
0x00, 0x00, 0x00, 0x00, // mtime: none
0x04, 0x03 }; // fastest comp, OS=Unix
static inline uint32_t crc32_char(uint32_t crc, uint8_t x)
{
#if defined(__ARM_FEATURE_CRC32)
crc = ~crc;
# if defined(__ARM_ARCH_ISA_A64)
// 64 bit mode
__asm__ volatile("crc32b %w0,%w0,%w1" : "+r"(crc) : "r"(x));
# else
// 32 bit mode (e.g. armv7 compiler building for armv8
__asm__ volatile("crc32b %0,%0,%1" : "+r"(crc) : "r"(x));
# endif
crc = ~crc;
#else
crc = crc32_fast[0][(crc ^ x) & 0xff] ^ (crc >> 8);
#endif
return crc;
}
static inline uint32_t crc32_uint32(uint32_t data)
{
#if defined(__ARM_FEATURE_CRC32)
# if defined(__ARM_ARCH_ISA_A64)
// 64 bit mode
__asm__ volatile("crc32w %w0,%w0,%w1" : "+r"(data) : "r"(~0UL));
# else
// 32 bit mode (e.g. armv7 compiler building for armv8
__asm__ volatile("crc32w %0,%0,%1" : "+r"(data) : "r"(~0UL));
# endif
data = ~data;
#else
data = crc32_fast[3][(data >> 0) & 0xff] ^
crc32_fast[2][(data >> 8) & 0xff] ^
crc32_fast[1][(data >> 16) & 0xff] ^
crc32_fast[0][(data >> 24) & 0xff];
#endif
return data;
}
/* Modified version originally from RFC1952, working with non-inverting CRCs */
uint32_t slz_crc32_by1(uint32_t crc, const unsigned char *buf, int len)
{
int n;
for (n = 0; n < len; n++)
crc = crc32_char(crc, buf[n]);
return crc;
}
/* This version computes the crc32 of <buf> over <len> bytes, doing most of it
* in 32-bit chunks.
*/
uint32_t slz_crc32_by4(uint32_t crc, const unsigned char *buf, int len)
{
const unsigned char *end = buf + len;
while (buf <= end - 16) {
#ifdef UNALIGNED_LE_OK
#if defined(__ARM_FEATURE_CRC32)
crc = ~crc;
# if defined(__ARM_ARCH_ISA_A64)
// 64 bit mode
__asm__ volatile("crc32w %w0,%w0,%w1" : "+r"(crc) : "r"(*(uint32_t*)(buf)));
__asm__ volatile("crc32w %w0,%w0,%w1" : "+r"(crc) : "r"(*(uint32_t*)(buf + 4)));
__asm__ volatile("crc32w %w0,%w0,%w1" : "+r"(crc) : "r"(*(uint32_t*)(buf + 8)));
__asm__ volatile("crc32w %w0,%w0,%w1" : "+r"(crc) : "r"(*(uint32_t*)(buf + 12)));
# else
// 32 bit mode (e.g. armv7 compiler building for armv8
__asm__ volatile("crc32w %0,%0,%1" : "+r"(crc) : "r"(*(uint32_t*)(buf)));
__asm__ volatile("crc32w %0,%0,%1" : "+r"(crc) : "r"(*(uint32_t*)(buf + 4)));
__asm__ volatile("crc32w %0,%0,%1" : "+r"(crc) : "r"(*(uint32_t*)(buf + 8)));
__asm__ volatile("crc32w %0,%0,%1" : "+r"(crc) : "r"(*(uint32_t*)(buf + 12)));
# endif
crc = ~crc;
#else
crc ^= *(uint32_t *)buf;
crc = crc32_uint32(crc);
crc ^= *(uint32_t *)(buf + 4);
crc = crc32_uint32(crc);
crc ^= *(uint32_t *)(buf + 8);
crc = crc32_uint32(crc);
crc ^= *(uint32_t *)(buf + 12);
crc = crc32_uint32(crc);
#endif
#else
crc = crc32_fast[3][(buf[0] ^ (crc >> 0)) & 0xff] ^