blob: d01599ca442a008bdf2de7937c0aff313f5dbeb4 [file] [log] [blame]
Willy Tarreauab2b7822021-04-22 14:09:44 +02001/*
2 * Copyright (C) 2013-2015 Willy Tarreau <w@1wt.eu>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef _SLZ_H
26#define _SLZ_H
27
Willy Tarreau388fc252021-05-14 08:44:52 +020028#include <inttypes.h>
Willy Tarreauab2b7822021-04-22 14:09:44 +020029
30/* We have two macros UNALIGNED_LE_OK and UNALIGNED_FASTER. The latter indicates
31 * that using unaligned data is faster than a simple shift. On x86 32-bit at
32 * least it is not the case as the per-byte access is 30% faster. A core2-duo on
33 * x86_64 is 7% faster to read one byte + shifting by 8 than to read one word,
34 * but a core i5 is 7% faster doing the unaligned read, so we privilege more
35 * recent implementations here.
36 */
37#if defined(__x86_64__)
38#define UNALIGNED_LE_OK
39#define UNALIGNED_FASTER
40#define USE_64BIT_QUEUE
41#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
42#define UNALIGNED_LE_OK
43//#define UNALIGNED_FASTER
44#elif defined(__ARMEL__) && defined(__ARM_ARCH_7A__)
45#define UNALIGNED_LE_OK
46#define UNALIGNED_FASTER
47#elif defined(__ARM_ARCH_8A) || defined(__ARM_FEATURE_UNALIGNED)
48#define UNALIGNED_LE_OK
49#define UNALIGNED_FASTER
50#endif
51
52/* Log2 of the size of the hash table used for the references table. */
53#define HASH_BITS 13
54
55enum slz_state {
56 SLZ_ST_INIT, /* stream initialized */
57 SLZ_ST_EOB, /* header or end of block already sent */
58 SLZ_ST_FIXED, /* inside a fixed huffman sequence */
59 SLZ_ST_LAST, /* last block, BFINAL sent */
60 SLZ_ST_DONE, /* BFINAL+EOB sent BFINAL */
61 SLZ_ST_END /* end sent (BFINAL, EOB, CRC + len) */
62};
63
64enum {
65 SLZ_FMT_GZIP, /* RFC1952: gzip envelope and crc32 for CRC */
66 SLZ_FMT_ZLIB, /* RFC1950: zlib envelope and adler-32 for CRC */
67 SLZ_FMT_DEFLATE, /* RFC1951: raw deflate, and no crc */
68};
69
70struct slz_stream {
71#ifdef USE_64BIT_QUEUE
72 uint64_t queue; /* last pending bits, LSB first */
73#else
74 uint32_t queue; /* last pending bits, LSB first */
75#endif
76 uint32_t qbits; /* number of bits in queue, < 8 on 32-bit, < 32 on 64-bit */
77 unsigned char *outbuf; /* set by encode() */
78 uint16_t state; /* one of slz_state */
79 uint8_t level:1; /* 0 = no compression, 1 = compression */
80 uint8_t format:2; /* SLZ_FMT_* */
81 uint8_t unused1; /* unused for now */
82 uint32_t crc32;
83 uint32_t ilen;
84};
85
86/* Functions specific to rfc1951 (deflate) */
Willy Tarreauab2b7822021-04-22 14:09:44 +020087long slz_rfc1951_encode(struct slz_stream *strm, unsigned char *out, const unsigned char *in, long ilen, int more);
88int slz_rfc1951_init(struct slz_stream *strm, int level);
89int slz_rfc1951_finish(struct slz_stream *strm, unsigned char *buf);
90
91/* Functions specific to rfc1952 (gzip) */
Willy Tarreauab2b7822021-04-22 14:09:44 +020092uint32_t slz_crc32_by1(uint32_t crc, const unsigned char *buf, int len);
93uint32_t slz_crc32_by4(uint32_t crc, const unsigned char *buf, int len);
94long slz_rfc1952_encode(struct slz_stream *strm, unsigned char *out, const unsigned char *in, long ilen, int more);
95int slz_rfc1952_send_header(struct slz_stream *strm, unsigned char *buf);
96int slz_rfc1952_init(struct slz_stream *strm, int level);
97int slz_rfc1952_finish(struct slz_stream *strm, unsigned char *buf);
98
99/* Functions specific to rfc1950 (zlib) */
100uint32_t slz_adler32_by1(uint32_t crc, const unsigned char *buf, int len);
101uint32_t slz_adler32_block(uint32_t crc, const unsigned char *buf, long len);
102long slz_rfc1950_encode(struct slz_stream *strm, unsigned char *out, const unsigned char *in, long ilen, int more);
103int slz_rfc1950_send_header(struct slz_stream *strm, unsigned char *buf);
104int slz_rfc1950_init(struct slz_stream *strm, int level);
105int slz_rfc1950_finish(struct slz_stream *strm, unsigned char *buf);
106
107/* generic functions */
108
109/* Initializes stream <strm>. It will configure the stream to use format
110 * <format> for the data, which must be one of SLZ_FMT_*. The compression level
111 * passed in <level> is set. This value can only be 0 (no compression) or 1
112 * (compression) and other values will lead to unpredictable behaviour. The
113 * function should always return 0.
114 */
115static inline int slz_init(struct slz_stream *strm, int level, int format)
116{
117 int ret;
118
119 if (format == SLZ_FMT_GZIP)
120 ret = slz_rfc1952_init(strm, level);
121 else if (format == SLZ_FMT_ZLIB)
122 ret = slz_rfc1950_init(strm, level);
123 else { /* deflate for anything else */
124 ret = slz_rfc1951_init(strm, level);
125 strm->format = format;
126 }
127 return ret;
128}
129
130/* Encodes the block according to the format used by the stream. This means
131 * that the CRC of the input block may be computed according to the CRC32 or
132 * adler-32 algorithms. The number of output bytes is returned.
133 */
134static inline long slz_encode(struct slz_stream *strm, void *out,
135 const void *in, long ilen, int more)
136{
137 long ret;
138
139 if (strm->format == SLZ_FMT_GZIP)
140 ret = slz_rfc1952_encode(strm, (unsigned char *) out, (const unsigned char *) in, ilen, more);
141 else if (strm->format == SLZ_FMT_ZLIB)
142 ret = slz_rfc1950_encode(strm, (unsigned char *) out, (const unsigned char *) in, ilen, more);
143 else /* deflate for other ones */
144 ret = slz_rfc1951_encode(strm, (unsigned char *) out, (const unsigned char *) in, ilen, more);
145
146 return ret;
147}
148
149/* Flushes pending bits and sends the trailer for stream <strm> into buffer
150 * <buf> if needed. When it's done, the stream state is updated to SLZ_ST_END.
151 * It returns the number of bytes emitted. The trailer consists in flushing the
152 * possibly pending bits from the queue (up to 24 bits), rounding to the next
153 * byte, then 4 bytes for the CRC when doing zlib/gzip, then another 4 bytes
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +0500154 * for the input length for gzip. That may about to 4+4+4 = 12 bytes, that the
Dridi Boukelmoune4bd53c32022-03-30 07:58:23 +0200155 * caller must ensure are available before calling the function. Note that if
156 * the initial header was never sent, it will be sent first as well (up to 10
157 * extra bytes).
Willy Tarreauab2b7822021-04-22 14:09:44 +0200158 */
159static inline int slz_finish(struct slz_stream *strm, void *buf)
160{
161 int ret;
162
163 if (strm->format == SLZ_FMT_GZIP)
164 ret = slz_rfc1952_finish(strm, (unsigned char *) buf);
165 else if (strm->format == SLZ_FMT_ZLIB)
166 ret = slz_rfc1950_finish(strm, (unsigned char *) buf);
167 else /* deflate for other ones */
168 ret = slz_rfc1951_finish(strm, (unsigned char *) buf);
169
170 return ret;
171}
172
173#endif