1 /*
2 * Copyright © 2021 Valve Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifdef HAVE_COMPRESSION
25
26 #include <assert.h>
27
28 /* Ensure that zlib uses 'const' in 'z_const' declarations. */
29 #ifndef ZLIB_CONST
30 #define ZLIB_CONST
31 #endif
32
33 #ifdef HAVE_ZLIB
34 #include "zlib.h"
35 #endif
36
37 #ifdef HAVE_ZSTD
38 #include "zstd.h"
39 #endif
40
41 #include "util/compress.h"
42 #include "macros.h"
43
44 /* 3 is the recomended level, with 22 as the absolute maximum */
45 #define ZSTD_COMPRESSION_LEVEL 3
46
47 size_t
util_compress_max_compressed_len(size_t in_data_size)48 util_compress_max_compressed_len(size_t in_data_size)
49 {
50 #ifdef HAVE_ZSTD
51 /* from the zstd docs (https://facebook.github.io/zstd/zstd_manual.html):
52 * compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
53 */
54 return ZSTD_compressBound(in_data_size);
55 #elif defined(HAVE_ZLIB)
56 /* From https://zlib.net/zlib_tech.html:
57 *
58 * "In the worst possible case, where the other block types would expand
59 * the data, deflation falls back to stored (uncompressed) blocks. Thus
60 * for the default settings used by deflateInit(), compress(), and
61 * compress2(), the only expansion is an overhead of five bytes per 16 KB
62 * block (about 0.03%), plus a one-time overhead of six bytes for the
63 * entire stream."
64 */
65 size_t num_blocks = (in_data_size + 16383) / 16384; /* round up blocks */
66 return in_data_size + 6 + (num_blocks * 5);
67 #else
68 STATIC_ASSERT(false);
69 #endif
70 }
71
72 /* Compress data and return the size of the compressed data */
73 size_t
util_compress_deflate(const uint8_t * in_data,size_t in_data_size,uint8_t * out_data,size_t out_buff_size)74 util_compress_deflate(const uint8_t *in_data, size_t in_data_size,
75 uint8_t *out_data, size_t out_buff_size)
76 {
77 #ifdef HAVE_ZSTD
78 size_t ret = ZSTD_compress(out_data, out_buff_size, in_data, in_data_size,
79 ZSTD_COMPRESSION_LEVEL);
80 if (ZSTD_isError(ret))
81 return 0;
82
83 return ret;
84 #elif defined(HAVE_ZLIB)
85 size_t compressed_size = 0;
86
87 /* allocate deflate state */
88 z_stream strm;
89 strm.zalloc = Z_NULL;
90 strm.zfree = Z_NULL;
91 strm.opaque = Z_NULL;
92 strm.next_in = in_data;
93 strm.next_out = out_data;
94 strm.avail_in = in_data_size;
95 strm.avail_out = out_buff_size;
96
97 int ret = deflateInit(&strm, Z_BEST_COMPRESSION);
98 if (ret != Z_OK) {
99 (void) deflateEnd(&strm);
100 return 0;
101 }
102
103 /* compress until end of in_data */
104 ret = deflate(&strm, Z_FINISH);
105
106 /* stream should be complete */
107 assert(ret == Z_STREAM_END);
108 if (ret == Z_STREAM_END) {
109 compressed_size = strm.total_out;
110 }
111
112 /* clean up and return */
113 (void) deflateEnd(&strm);
114 return compressed_size;
115 #else
116 STATIC_ASSERT(false);
117 # endif
118 }
119
120 /**
121 * Decompresses data, returns true if successful.
122 */
123 bool
util_compress_inflate(const uint8_t * in_data,size_t in_data_size,uint8_t * out_data,size_t out_data_size)124 util_compress_inflate(const uint8_t *in_data, size_t in_data_size,
125 uint8_t *out_data, size_t out_data_size)
126 {
127 #ifdef HAVE_ZSTD
128 size_t ret = ZSTD_decompress(out_data, out_data_size, in_data, in_data_size);
129 return !ZSTD_isError(ret);
130 #elif defined(HAVE_ZLIB)
131 z_stream strm;
132
133 /* allocate inflate state */
134 strm.zalloc = Z_NULL;
135 strm.zfree = Z_NULL;
136 strm.opaque = Z_NULL;
137 strm.next_in = in_data;
138 strm.avail_in = in_data_size;
139 strm.next_out = out_data;
140 strm.avail_out = out_data_size;
141
142 int ret = inflateInit(&strm);
143 if (ret != Z_OK)
144 return false;
145
146 ret = inflate(&strm, Z_NO_FLUSH);
147 assert(ret != Z_STREAM_ERROR); /* state not clobbered */
148
149 /* Unless there was an error we should have decompressed everything in one
150 * go as we know the uncompressed file size.
151 */
152 if (ret != Z_STREAM_END) {
153 (void)inflateEnd(&strm);
154 return false;
155 }
156 assert(strm.avail_out == 0);
157
158 /* clean up and return */
159 (void)inflateEnd(&strm);
160 return true;
161 #else
162 STATIC_ASSERT(false);
163 #endif
164 }
165
166 #endif
167