deflate-ruby 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/CLAUDE.md +95 -92
  3. data/GEM_VERIFICATION_REPORT.md +140 -0
  4. data/LICENSE.txt +6 -6
  5. data/README.md +87 -65
  6. data/Rakefile +23 -0
  7. data/ext/deflate_ruby/{libdeflate/lib/x86/adler32_impl.h → adler32_impl.h} +8 -7
  8. data/ext/deflate_ruby/common_defs.h +748 -0
  9. data/ext/deflate_ruby/{libdeflate/lib/x86/cpu_features.c → cpu_features.c} +46 -16
  10. data/ext/deflate_ruby/{libdeflate/lib/x86/cpu_features.h → cpu_features.h} +2 -1
  11. data/ext/deflate_ruby/{libdeflate/lib/x86/crc32_impl.h → crc32_impl.h} +22 -23
  12. data/ext/deflate_ruby/{libdeflate/lib/crc32_multipliers.h → crc32_multipliers.h} +2 -4
  13. data/ext/deflate_ruby/{libdeflate/lib/x86/crc32_pclmul_template.h → crc32_pclmul_template.h} +23 -94
  14. data/ext/deflate_ruby/{libdeflate/lib/crc32_tables.h → crc32_tables.h} +1 -1
  15. data/ext/deflate_ruby/{libdeflate/lib/deflate_compress.c → deflate_compress.c} +59 -60
  16. data/ext/deflate_ruby/deflate_ruby.c +392 -218
  17. data/ext/deflate_ruby/deflate_ruby.h +6 -0
  18. data/ext/deflate_ruby/extconf.rb +35 -25
  19. data/ext/deflate_ruby/libdeflate/adler32.c +162 -0
  20. data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/adler32_impl.h +14 -7
  21. data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/crc32_impl.h +25 -31
  22. data/ext/deflate_ruby/libdeflate/arm/crc32_pmull_helpers.h +156 -0
  23. data/ext/deflate_ruby/libdeflate/arm/crc32_pmull_wide.h +226 -0
  24. data/ext/deflate_ruby/libdeflate/bt_matchfinder.h +342 -0
  25. data/ext/deflate_ruby/libdeflate/common_defs.h +2 -1
  26. data/ext/deflate_ruby/libdeflate/cpu_features_common.h +93 -0
  27. data/ext/deflate_ruby/libdeflate/crc32.c +262 -0
  28. data/ext/deflate_ruby/libdeflate/crc32_multipliers.h +375 -0
  29. data/ext/deflate_ruby/libdeflate/crc32_tables.h +587 -0
  30. data/ext/deflate_ruby/libdeflate/decompress_template.h +777 -0
  31. data/ext/deflate_ruby/libdeflate/deflate_compress.c +4128 -0
  32. data/ext/deflate_ruby/libdeflate/deflate_compress.h +15 -0
  33. data/ext/deflate_ruby/libdeflate/deflate_constants.h +56 -0
  34. data/ext/deflate_ruby/libdeflate/deflate_decompress.c +1208 -0
  35. data/ext/deflate_ruby/libdeflate/gzip_compress.c +90 -0
  36. data/ext/deflate_ruby/libdeflate/gzip_constants.h +45 -0
  37. data/ext/deflate_ruby/libdeflate/gzip_decompress.c +144 -0
  38. data/ext/deflate_ruby/libdeflate/hc_matchfinder.h +401 -0
  39. data/ext/deflate_ruby/libdeflate/ht_matchfinder.h +234 -0
  40. data/ext/deflate_ruby/libdeflate/lib_common.h +106 -0
  41. data/ext/deflate_ruby/libdeflate/libdeflate.h +2 -2
  42. data/ext/deflate_ruby/libdeflate/{lib/matchfinder_common.h → matchfinder_common.h} +3 -3
  43. data/ext/deflate_ruby/libdeflate/x86/adler32_impl.h +135 -0
  44. data/ext/deflate_ruby/libdeflate/x86/adler32_template.h +518 -0
  45. data/ext/deflate_ruby/libdeflate/x86/cpu_features.c +213 -0
  46. data/ext/deflate_ruby/libdeflate/x86/cpu_features.h +170 -0
  47. data/ext/deflate_ruby/libdeflate/x86/crc32_impl.h +159 -0
  48. data/ext/deflate_ruby/libdeflate/x86/crc32_pclmul_template.h +424 -0
  49. data/ext/deflate_ruby/libdeflate/x86/decompress_impl.h +57 -0
  50. data/ext/deflate_ruby/libdeflate.h +411 -0
  51. data/ext/deflate_ruby/matchfinder_common.h +224 -0
  52. data/ext/deflate_ruby/matchfinder_impl.h +122 -0
  53. data/ext/deflate_ruby/utils.c +141 -0
  54. data/ext/deflate_ruby/zlib_compress.c +82 -0
  55. data/ext/deflate_ruby/zlib_constants.h +21 -0
  56. data/ext/deflate_ruby/zlib_decompress.c +104 -0
  57. data/lib/deflate_ruby/version.rb +1 -1
  58. data/lib/deflate_ruby.rb +1 -63
  59. data/sig/deflate_ruby.rbs +4 -0
  60. data/test/test_deflate_ruby.rb +220 -0
  61. data/test/test_helper.rb +6 -0
  62. metadata +90 -144
  63. data/ext/deflate_ruby/libdeflate/CMakeLists.txt +0 -270
  64. data/ext/deflate_ruby/libdeflate/NEWS.md +0 -494
  65. data/ext/deflate_ruby/libdeflate/README.md +0 -228
  66. data/ext/deflate_ruby/libdeflate/libdeflate-config.cmake.in +0 -3
  67. data/ext/deflate_ruby/libdeflate/libdeflate.pc.in +0 -18
  68. data/ext/deflate_ruby/libdeflate/programs/CMakeLists.txt +0 -105
  69. data/ext/deflate_ruby/libdeflate/programs/benchmark.c +0 -696
  70. data/ext/deflate_ruby/libdeflate/programs/checksum.c +0 -218
  71. data/ext/deflate_ruby/libdeflate/programs/config.h.in +0 -19
  72. data/ext/deflate_ruby/libdeflate/programs/gzip.c +0 -688
  73. data/ext/deflate_ruby/libdeflate/programs/prog_util.c +0 -521
  74. data/ext/deflate_ruby/libdeflate/programs/prog_util.h +0 -225
  75. data/ext/deflate_ruby/libdeflate/programs/test_checksums.c +0 -200
  76. data/ext/deflate_ruby/libdeflate/programs/test_custom_malloc.c +0 -155
  77. data/ext/deflate_ruby/libdeflate/programs/test_incomplete_codes.c +0 -385
  78. data/ext/deflate_ruby/libdeflate/programs/test_invalid_streams.c +0 -130
  79. data/ext/deflate_ruby/libdeflate/programs/test_litrunlen_overflow.c +0 -72
  80. data/ext/deflate_ruby/libdeflate/programs/test_overread.c +0 -95
  81. data/ext/deflate_ruby/libdeflate/programs/test_slow_decompression.c +0 -472
  82. data/ext/deflate_ruby/libdeflate/programs/test_trailing_bytes.c +0 -151
  83. data/ext/deflate_ruby/libdeflate/programs/test_util.c +0 -237
  84. data/ext/deflate_ruby/libdeflate/programs/test_util.h +0 -61
  85. data/ext/deflate_ruby/libdeflate/programs/tgetopt.c +0 -118
  86. data/ext/deflate_ruby/libdeflate/scripts/android_build.sh +0 -118
  87. data/ext/deflate_ruby/libdeflate/scripts/android_tests.sh +0 -69
  88. data/ext/deflate_ruby/libdeflate/scripts/benchmark.sh +0 -10
  89. data/ext/deflate_ruby/libdeflate/scripts/checksum.sh +0 -10
  90. data/ext/deflate_ruby/libdeflate/scripts/checksum_benchmarks.sh +0 -253
  91. data/ext/deflate_ruby/libdeflate/scripts/cmake-helper.sh +0 -17
  92. data/ext/deflate_ruby/libdeflate/scripts/deflate_benchmarks.sh +0 -119
  93. data/ext/deflate_ruby/libdeflate/scripts/exec_tests.sh +0 -38
  94. data/ext/deflate_ruby/libdeflate/scripts/gen-release-archives.sh +0 -37
  95. data/ext/deflate_ruby/libdeflate/scripts/gen_bitreverse_tab.py +0 -19
  96. data/ext/deflate_ruby/libdeflate/scripts/gen_crc32_multipliers.c +0 -199
  97. data/ext/deflate_ruby/libdeflate/scripts/gen_crc32_tables.c +0 -105
  98. data/ext/deflate_ruby/libdeflate/scripts/gen_default_litlen_costs.py +0 -44
  99. data/ext/deflate_ruby/libdeflate/scripts/gen_offset_slot_map.py +0 -29
  100. data/ext/deflate_ruby/libdeflate/scripts/gzip_tests.sh +0 -523
  101. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_compress/corpus/0 +0 -0
  102. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_compress/fuzz.c +0 -95
  103. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_decompress/corpus/0 +0 -3
  104. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_decompress/fuzz.c +0 -62
  105. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/fuzz.sh +0 -108
  106. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/gzip_decompress/corpus/0 +0 -0
  107. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/gzip_decompress/fuzz.c +0 -19
  108. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/zlib_decompress/corpus/0 +0 -3
  109. data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/zlib_decompress/fuzz.c +0 -19
  110. data/ext/deflate_ruby/libdeflate/scripts/run_tests.sh +0 -416
  111. data/ext/deflate_ruby/libdeflate/scripts/toolchain-i686-w64-mingw32.cmake +0 -8
  112. data/ext/deflate_ruby/libdeflate/scripts/toolchain-x86_64-w64-mingw32.cmake +0 -8
  113. /data/ext/deflate_ruby/{libdeflate/lib/adler32.c → adler32.c} +0 -0
  114. /data/ext/deflate_ruby/{libdeflate/lib/x86/adler32_template.h → adler32_template.h} +0 -0
  115. /data/ext/deflate_ruby/{libdeflate/lib/bt_matchfinder.h → bt_matchfinder.h} +0 -0
  116. /data/ext/deflate_ruby/{libdeflate/lib/cpu_features_common.h → cpu_features_common.h} +0 -0
  117. /data/ext/deflate_ruby/{libdeflate/lib/crc32.c → crc32.c} +0 -0
  118. /data/ext/deflate_ruby/{libdeflate/lib/arm/crc32_pmull_helpers.h → crc32_pmull_helpers.h} +0 -0
  119. /data/ext/deflate_ruby/{libdeflate/lib/arm/crc32_pmull_wide.h → crc32_pmull_wide.h} +0 -0
  120. /data/ext/deflate_ruby/{libdeflate/lib/x86/decompress_impl.h → decompress_impl.h} +0 -0
  121. /data/ext/deflate_ruby/{libdeflate/lib/decompress_template.h → decompress_template.h} +0 -0
  122. /data/ext/deflate_ruby/{libdeflate/lib/deflate_compress.h → deflate_compress.h} +0 -0
  123. /data/ext/deflate_ruby/{libdeflate/lib/deflate_constants.h → deflate_constants.h} +0 -0
  124. /data/ext/deflate_ruby/{libdeflate/lib/deflate_decompress.c → deflate_decompress.c} +0 -0
  125. /data/ext/deflate_ruby/{libdeflate/lib/gzip_compress.c → gzip_compress.c} +0 -0
  126. /data/ext/deflate_ruby/{libdeflate/lib/gzip_constants.h → gzip_constants.h} +0 -0
  127. /data/ext/deflate_ruby/{libdeflate/lib/gzip_decompress.c → gzip_decompress.c} +0 -0
  128. /data/ext/deflate_ruby/{libdeflate/lib/hc_matchfinder.h → hc_matchfinder.h} +0 -0
  129. /data/ext/deflate_ruby/{libdeflate/lib/ht_matchfinder.h → ht_matchfinder.h} +0 -0
  130. /data/ext/deflate_ruby/{libdeflate/lib/lib_common.h → lib_common.h} +0 -0
  131. /data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/cpu_features.c +0 -0
  132. /data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/cpu_features.h +0 -0
  133. /data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/matchfinder_impl.h +0 -0
  134. /data/ext/deflate_ruby/libdeflate/{lib/riscv → riscv}/matchfinder_impl.h +0 -0
  135. /data/ext/deflate_ruby/libdeflate/{lib/utils.c → utils.c} +0 -0
  136. /data/ext/deflate_ruby/libdeflate/{lib/x86 → x86}/matchfinder_impl.h +0 -0
  137. /data/ext/deflate_ruby/libdeflate/{lib/zlib_compress.c → zlib_compress.c} +0 -0
  138. /data/ext/deflate_ruby/libdeflate/{lib/zlib_constants.h → zlib_constants.h} +0 -0
  139. /data/ext/deflate_ruby/libdeflate/{lib/zlib_decompress.c → zlib_decompress.c} +0 -0
@@ -0,0 +1,411 @@
1
+ /*
2
+ * libdeflate.h - public header for libdeflate
3
+ */
4
+
5
+ #ifndef LIBDEFLATE_H
6
+ #define LIBDEFLATE_H
7
+
8
+ #include <stddef.h>
9
+ #include <stdint.h>
10
+
11
+ #ifdef __cplusplus
12
+ extern "C" {
13
+ #endif
14
+
15
+ #define LIBDEFLATE_VERSION_MAJOR 1
16
+ #define LIBDEFLATE_VERSION_MINOR 25
17
+ #define LIBDEFLATE_VERSION_STRING "1.25"
18
+
19
+ /*
20
+ * Users of libdeflate.dll on Windows can define LIBDEFLATE_DLL to cause
21
+ * __declspec(dllimport) to be used. This should be done when it's easy to do.
22
+ * Otherwise it's fine to skip it, since it is a very minor performance
23
+ * optimization that is irrelevant for most use cases of libdeflate.
24
+ */
25
+ #ifndef LIBDEFLATEAPI
26
+ # if defined(LIBDEFLATE_DLL) && (defined(_WIN32) || defined(__CYGWIN__))
27
+ # define LIBDEFLATEAPI __declspec(dllimport)
28
+ # else
29
+ # define LIBDEFLATEAPI
30
+ # endif
31
+ #endif
32
+
33
+ /* ========================================================================== */
34
+ /* Compression */
35
+ /* ========================================================================== */
36
+
37
+ struct libdeflate_compressor;
38
+ struct libdeflate_options;
39
+
40
+ /*
41
+ * libdeflate_alloc_compressor() allocates a new compressor that supports
42
+ * DEFLATE, zlib, and gzip compression. 'compression_level' is the compression
43
+ * level on a zlib-like scale but with a higher maximum value (1 = fastest, 6 =
44
+ * medium/default, 9 = slow, 12 = slowest). Level 0 is also supported and means
45
+ * "no compression", specifically "create a valid stream, but only emit
46
+ * uncompressed blocks" (this will expand the data slightly).
47
+ *
48
+ * The return value is a pointer to the new compressor, or NULL if out of memory
49
+ * or if the compression level is invalid (i.e. outside the range [0, 12]).
50
+ *
51
+ * Note: for compression, the sliding window size is defined at compilation time
52
+ * to 32768, the largest size permissible in the DEFLATE format. It cannot be
53
+ * changed at runtime.
54
+ *
55
+ * A single compressor is not safe to use by multiple threads concurrently.
56
+ * However, different threads may use different compressors concurrently.
57
+ */
58
+ LIBDEFLATEAPI struct libdeflate_compressor *
59
+ libdeflate_alloc_compressor(int compression_level);
60
+
61
+ /*
62
+ * Like libdeflate_alloc_compressor(), but adds the 'options' argument.
63
+ */
64
+ LIBDEFLATEAPI struct libdeflate_compressor *
65
+ libdeflate_alloc_compressor_ex(int compression_level,
66
+ const struct libdeflate_options *options);
67
+
68
+ /*
69
+ * libdeflate_deflate_compress() performs raw DEFLATE compression on a buffer of
70
+ * data. It attempts to compress 'in_nbytes' bytes of data located at 'in' and
71
+ * write the result to 'out', which has space for 'out_nbytes_avail' bytes. The
72
+ * return value is the compressed size in bytes, or 0 if the data could not be
73
+ * compressed to 'out_nbytes_avail' bytes or fewer.
74
+ *
75
+ * If compression is successful, then the output data is guaranteed to be a
76
+ * valid DEFLATE stream that decompresses to the input data. No other
77
+ * guarantees are made about the output data. Notably, different versions of
78
+ * libdeflate can produce different compressed data for the same uncompressed
79
+ * data, even at the same compression level. Do ***NOT*** do things like
80
+ * writing tests that compare compressed data to a golden output, as this can
81
+ * break when libdeflate is updated. (This property isn't specific to
82
+ * libdeflate; the same is true for zlib and other compression libraries too.)
83
+ */
84
+ LIBDEFLATEAPI size_t
85
+ libdeflate_deflate_compress(struct libdeflate_compressor *compressor,
86
+ const void *in, size_t in_nbytes,
87
+ void *out, size_t out_nbytes_avail);
88
+
89
+ /*
90
+ * libdeflate_deflate_compress_bound() returns a worst-case upper bound on the
91
+ * number of bytes of compressed data that may be produced by compressing any
92
+ * buffer of length less than or equal to 'in_nbytes' using
93
+ * libdeflate_deflate_compress() with the specified compressor. This bound will
94
+ * necessarily be a number greater than or equal to 'in_nbytes'. It may be an
95
+ * overestimate of the true upper bound. The return value is guaranteed to be
96
+ * the same for all invocations with the same compressor and same 'in_nbytes'.
97
+ *
98
+ * As a special case, 'compressor' may be NULL. This causes the bound to be
99
+ * taken across *any* libdeflate_compressor that could ever be allocated with
100
+ * this build of the library, with any options.
101
+ *
102
+ * Note that this function is not necessary in many applications. With
103
+ * block-based compression, it is usually preferable to separately store the
104
+ * uncompressed size of each block and to store any blocks that did not compress
105
+ * to less than their original size uncompressed. In that scenario, there is no
106
+ * need to know the worst-case compressed size, since the maximum number of
107
+ * bytes of compressed data that may be used would always be one less than the
108
+ * input length. You can just pass a buffer of that size to
109
+ * libdeflate_deflate_compress() and store the data uncompressed if
110
+ * libdeflate_deflate_compress() returns 0, indicating that the compressed data
111
+ * did not fit into the provided output buffer.
112
+ */
113
+ LIBDEFLATEAPI size_t
114
+ libdeflate_deflate_compress_bound(struct libdeflate_compressor *compressor,
115
+ size_t in_nbytes);
116
+
117
+ /*
118
+ * Like libdeflate_deflate_compress(), but uses the zlib wrapper format instead
119
+ * of raw DEFLATE.
120
+ */
121
+ LIBDEFLATEAPI size_t
122
+ libdeflate_zlib_compress(struct libdeflate_compressor *compressor,
123
+ const void *in, size_t in_nbytes,
124
+ void *out, size_t out_nbytes_avail);
125
+
126
+ /*
127
+ * Like libdeflate_deflate_compress_bound(), but assumes the data will be
128
+ * compressed with libdeflate_zlib_compress() rather than with
129
+ * libdeflate_deflate_compress().
130
+ */
131
+ LIBDEFLATEAPI size_t
132
+ libdeflate_zlib_compress_bound(struct libdeflate_compressor *compressor,
133
+ size_t in_nbytes);
134
+
135
+ /*
136
+ * Like libdeflate_deflate_compress(), but uses the gzip wrapper format instead
137
+ * of raw DEFLATE.
138
+ */
139
+ LIBDEFLATEAPI size_t
140
+ libdeflate_gzip_compress(struct libdeflate_compressor *compressor,
141
+ const void *in, size_t in_nbytes,
142
+ void *out, size_t out_nbytes_avail);
143
+
144
+ /*
145
+ * Like libdeflate_deflate_compress_bound(), but assumes the data will be
146
+ * compressed with libdeflate_gzip_compress() rather than with
147
+ * libdeflate_deflate_compress().
148
+ */
149
+ LIBDEFLATEAPI size_t
150
+ libdeflate_gzip_compress_bound(struct libdeflate_compressor *compressor,
151
+ size_t in_nbytes);
152
+
153
+ /*
154
+ * libdeflate_free_compressor() frees a compressor that was allocated with
155
+ * libdeflate_alloc_compressor(). If a NULL pointer is passed in, no action is
156
+ * taken.
157
+ */
158
+ LIBDEFLATEAPI void
159
+ libdeflate_free_compressor(struct libdeflate_compressor *compressor);
160
+
161
+ /* ========================================================================== */
162
+ /* Decompression */
163
+ /* ========================================================================== */
164
+
165
+ struct libdeflate_decompressor;
166
+ struct libdeflate_options;
167
+
168
+ /*
169
+ * libdeflate_alloc_decompressor() allocates a new decompressor that can be used
170
+ * for DEFLATE, zlib, and gzip decompression. The return value is a pointer to
171
+ * the new decompressor, or NULL if out of memory.
172
+ *
173
+ * This function takes no parameters, and the returned decompressor is valid for
174
+ * decompressing data that was compressed at any compression level and with any
175
+ * sliding window size.
176
+ *
177
+ * A single decompressor is not safe to use by multiple threads concurrently.
178
+ * However, different threads may use different decompressors concurrently.
179
+ */
180
+ LIBDEFLATEAPI struct libdeflate_decompressor *
181
+ libdeflate_alloc_decompressor(void);
182
+
183
+ /*
184
+ * Like libdeflate_alloc_decompressor(), but adds the 'options' argument.
185
+ */
186
+ LIBDEFLATEAPI struct libdeflate_decompressor *
187
+ libdeflate_alloc_decompressor_ex(const struct libdeflate_options *options);
188
+
189
+ /*
190
+ * Result of a call to libdeflate_deflate_decompress(),
191
+ * libdeflate_zlib_decompress(), or libdeflate_gzip_decompress().
192
+ */
193
+ enum libdeflate_result {
194
+ /* Decompression was successful. */
195
+ LIBDEFLATE_SUCCESS = 0,
196
+
197
+ /* Decompression failed because the compressed data was invalid,
198
+ * corrupt, or otherwise unsupported. */
199
+ LIBDEFLATE_BAD_DATA = 1,
200
+
201
+ /* A NULL 'actual_out_nbytes_ret' was provided, but the data would have
202
+ * decompressed to fewer than 'out_nbytes_avail' bytes. */
203
+ LIBDEFLATE_SHORT_OUTPUT = 2,
204
+
205
+ /* The data would have decompressed to more than 'out_nbytes_avail'
206
+ * bytes. */
207
+ LIBDEFLATE_INSUFFICIENT_SPACE = 3,
208
+ };
209
+
210
+ /*
211
+ * libdeflate_deflate_decompress() decompresses a DEFLATE stream from the buffer
212
+ * 'in' with compressed size up to 'in_nbytes' bytes. The uncompressed data is
213
+ * written to 'out', a buffer with size 'out_nbytes_avail' bytes. If
214
+ * decompression succeeds, then 0 (LIBDEFLATE_SUCCESS) is returned. Otherwise,
215
+ * a nonzero result code such as LIBDEFLATE_BAD_DATA is returned, and the
216
+ * contents of the output buffer are undefined.
217
+ *
218
+ * Decompression stops at the end of the DEFLATE stream (as indicated by the
219
+ * BFINAL flag), even if it is actually shorter than 'in_nbytes' bytes.
220
+ *
221
+ * libdeflate_deflate_decompress() can be used in cases where the actual
222
+ * uncompressed size is known (recommended) or unknown (not recommended):
223
+ *
224
+ * - If the actual uncompressed size is known, then pass the actual
225
+ * uncompressed size as 'out_nbytes_avail' and pass NULL for
226
+ * 'actual_out_nbytes_ret'. This makes libdeflate_deflate_decompress() fail
227
+ * with LIBDEFLATE_SHORT_OUTPUT if the data decompressed to fewer than the
228
+ * specified number of bytes.
229
+ *
230
+ * - If the actual uncompressed size is unknown, then provide a non-NULL
231
+ * 'actual_out_nbytes_ret' and provide a buffer with some size
232
+ * 'out_nbytes_avail' that you think is large enough to hold all the
233
+ * uncompressed data. In this case, if the data decompresses to less than
234
+ * or equal to 'out_nbytes_avail' bytes, then
235
+ * libdeflate_deflate_decompress() will write the actual uncompressed size
236
+ * to *actual_out_nbytes_ret and return 0 (LIBDEFLATE_SUCCESS). Otherwise,
237
+ * it will return LIBDEFLATE_INSUFFICIENT_SPACE if the provided buffer was
238
+ * not large enough but no other problems were encountered, or another
239
+ * nonzero result code if decompression failed for another reason.
240
+ */
241
+ LIBDEFLATEAPI enum libdeflate_result
242
+ libdeflate_deflate_decompress(struct libdeflate_decompressor *decompressor,
243
+ const void *in, size_t in_nbytes,
244
+ void *out, size_t out_nbytes_avail,
245
+ size_t *actual_out_nbytes_ret);
246
+
247
+ /*
248
+ * Like libdeflate_deflate_decompress(), but adds the 'actual_in_nbytes_ret'
249
+ * argument. If decompression succeeds and 'actual_in_nbytes_ret' is not NULL,
250
+ * then the actual compressed size of the DEFLATE stream (aligned to the next
251
+ * byte boundary) is written to *actual_in_nbytes_ret.
252
+ */
253
+ LIBDEFLATEAPI enum libdeflate_result
254
+ libdeflate_deflate_decompress_ex(struct libdeflate_decompressor *decompressor,
255
+ const void *in, size_t in_nbytes,
256
+ void *out, size_t out_nbytes_avail,
257
+ size_t *actual_in_nbytes_ret,
258
+ size_t *actual_out_nbytes_ret);
259
+
260
+ /*
261
+ * Like libdeflate_deflate_decompress(), but assumes the zlib wrapper format
262
+ * instead of raw DEFLATE.
263
+ *
264
+ * Decompression will stop at the end of the zlib stream, even if it is shorter
265
+ * than 'in_nbytes'. If you need to know exactly where the zlib stream ended,
266
+ * use libdeflate_zlib_decompress_ex().
267
+ */
268
+ LIBDEFLATEAPI enum libdeflate_result
269
+ libdeflate_zlib_decompress(struct libdeflate_decompressor *decompressor,
270
+ const void *in, size_t in_nbytes,
271
+ void *out, size_t out_nbytes_avail,
272
+ size_t *actual_out_nbytes_ret);
273
+
274
+ /*
275
+ * Like libdeflate_zlib_decompress(), but adds the 'actual_in_nbytes_ret'
276
+ * argument. If 'actual_in_nbytes_ret' is not NULL and the decompression
277
+ * succeeds (indicating that the first zlib-compressed stream in the input
278
+ * buffer was decompressed), then the actual number of input bytes consumed is
279
+ * written to *actual_in_nbytes_ret.
280
+ */
281
+ LIBDEFLATEAPI enum libdeflate_result
282
+ libdeflate_zlib_decompress_ex(struct libdeflate_decompressor *decompressor,
283
+ const void *in, size_t in_nbytes,
284
+ void *out, size_t out_nbytes_avail,
285
+ size_t *actual_in_nbytes_ret,
286
+ size_t *actual_out_nbytes_ret);
287
+
288
+ /*
289
+ * Like libdeflate_deflate_decompress(), but assumes the gzip wrapper format
290
+ * instead of raw DEFLATE.
291
+ *
292
+ * If multiple gzip-compressed members are concatenated, then only the first
293
+ * will be decompressed. Use libdeflate_gzip_decompress_ex() if you need
294
+ * multi-member support.
295
+ */
296
+ LIBDEFLATEAPI enum libdeflate_result
297
+ libdeflate_gzip_decompress(struct libdeflate_decompressor *decompressor,
298
+ const void *in, size_t in_nbytes,
299
+ void *out, size_t out_nbytes_avail,
300
+ size_t *actual_out_nbytes_ret);
301
+
302
+ /*
303
+ * Like libdeflate_gzip_decompress(), but adds the 'actual_in_nbytes_ret'
304
+ * argument. If 'actual_in_nbytes_ret' is not NULL and the decompression
305
+ * succeeds (indicating that the first gzip-compressed member in the input
306
+ * buffer was decompressed), then the actual number of input bytes consumed is
307
+ * written to *actual_in_nbytes_ret.
308
+ */
309
+ LIBDEFLATEAPI enum libdeflate_result
310
+ libdeflate_gzip_decompress_ex(struct libdeflate_decompressor *decompressor,
311
+ const void *in, size_t in_nbytes,
312
+ void *out, size_t out_nbytes_avail,
313
+ size_t *actual_in_nbytes_ret,
314
+ size_t *actual_out_nbytes_ret);
315
+
316
+ /*
317
+ * libdeflate_free_decompressor() frees a decompressor that was allocated with
318
+ * libdeflate_alloc_decompressor(). If a NULL pointer is passed in, no action
319
+ * is taken.
320
+ */
321
+ LIBDEFLATEAPI void
322
+ libdeflate_free_decompressor(struct libdeflate_decompressor *decompressor);
323
+
324
+ /* ========================================================================== */
325
+ /* Checksums */
326
+ /* ========================================================================== */
327
+
328
+ /*
329
+ * libdeflate_adler32() updates a running Adler-32 checksum with 'len' bytes of
330
+ * data and returns the updated checksum. When starting a new checksum, the
331
+ * required initial value for 'adler' is 1. This value is also returned when
332
+ * 'buffer' is specified as NULL.
333
+ */
334
+ LIBDEFLATEAPI uint32_t
335
+ libdeflate_adler32(uint32_t adler, const void *buffer, size_t len);
336
+
337
+
338
+ /*
339
+ * libdeflate_crc32() updates a running CRC-32 checksum with 'len' bytes of data
340
+ * and returns the updated checksum. When starting a new checksum, the required
341
+ * initial value for 'crc' is 0. This value is also returned when 'buffer' is
342
+ * specified as NULL.
343
+ */
344
+ LIBDEFLATEAPI uint32_t
345
+ libdeflate_crc32(uint32_t crc, const void *buffer, size_t len);
346
+
347
+ /* ========================================================================== */
348
+ /* Custom memory allocator */
349
+ /* ========================================================================== */
350
+
351
+ /*
352
+ * Install a custom memory allocator which libdeflate will use for all memory
353
+ * allocations by default. 'malloc_func' is a function that must behave like
354
+ * malloc(), and 'free_func' is a function that must behave like free().
355
+ *
356
+ * The per-(de)compressor custom memory allocator that can be specified in
357
+ * 'struct libdeflate_options' takes priority over this.
358
+ *
359
+ * This doesn't affect the free() function that will be used to free
360
+ * (de)compressors that were already in existence when this is called.
361
+ */
362
+ LIBDEFLATEAPI void
363
+ libdeflate_set_memory_allocator(void *(*malloc_func)(size_t),
364
+ void (*free_func)(void *));
365
+
366
+ /*
367
+ * Advanced options. This is the options structure that
368
+ * libdeflate_alloc_compressor_ex() and libdeflate_alloc_decompressor_ex()
369
+ * require. Most users won't need this and should just use the non-"_ex"
370
+ * functions instead. If you do need this, it should be initialized like this:
371
+ *
372
+ * struct libdeflate_options options;
373
+ *
374
+ * memset(&options, 0, sizeof(options));
375
+ * options.sizeof_options = sizeof(options);
376
+ * // Then set the fields that you need to override the defaults for.
377
+ */
378
+ struct libdeflate_options {
379
+
380
+ /*
381
+ * This field must be set to the struct size. This field exists for
382
+ * extensibility, so that fields can be appended to this struct in
383
+ * future versions of libdeflate while still supporting old binaries.
384
+ */
385
+ size_t sizeof_options;
386
+
387
+ /*
388
+ * An optional custom memory allocator to use for this (de)compressor.
389
+ * 'malloc_func' must be a function that behaves like malloc(), and
390
+ * 'free_func' must be a function that behaves like free().
391
+ *
392
+ * This is useful in cases where a process might have multiple users of
393
+ * libdeflate who want to use different memory allocators. For example,
394
+ * a library might want to use libdeflate with a custom memory allocator
395
+ * without interfering with user code that might use libdeflate too.
396
+ *
397
+ * This takes priority over the "global" memory allocator (which by
398
+ * default is malloc() and free(), but can be changed by
399
+ * libdeflate_set_memory_allocator()). Moreover, libdeflate will never
400
+ * call the "global" memory allocator if a per-(de)compressor custom
401
+ * allocator is always given.
402
+ */
403
+ void *(*malloc_func)(size_t);
404
+ void (*free_func)(void *);
405
+ };
406
+
407
+ #ifdef __cplusplus
408
+ }
409
+ #endif
410
+
411
+ #endif /* LIBDEFLATE_H */
@@ -0,0 +1,224 @@
1
+ /*
2
+ * matchfinder_common.h - common code for Lempel-Ziv matchfinding
3
+ */
4
+
5
+ #ifndef LIB_MATCHFINDER_COMMON_H
6
+ #define LIB_MATCHFINDER_COMMON_H
7
+
8
+ #include "lib_common.h"
9
+
10
+ #ifndef MATCHFINDER_WINDOW_ORDER
11
+ # error "MATCHFINDER_WINDOW_ORDER must be defined!"
12
+ #endif
13
+
14
+ /*
15
+ * Given a 32-bit value that was loaded with the platform's native endianness,
16
+ * return a 32-bit value whose high-order 8 bits are 0 and whose low-order 24
17
+ * bits contain the first 3 bytes, arranged in octets in a platform-dependent
18
+ * order, at the memory location from which the input 32-bit value was loaded.
19
+ */
20
+ static forceinline u32
21
+ loaded_u32_to_u24(u32 v)
22
+ {
23
+ if (CPU_IS_LITTLE_ENDIAN())
24
+ return v & 0xFFFFFF;
25
+ else
26
+ return v >> 8;
27
+ }
28
+
29
+ /*
30
+ * Load the next 3 bytes from @p into the 24 low-order bits of a 32-bit value.
31
+ * The order in which the 3 bytes will be arranged as octets in the 24 bits is
32
+ * platform-dependent. At least 4 bytes (not 3) must be available at @p.
33
+ */
34
+ static forceinline u32
35
+ load_u24_unaligned(const u8 *p)
36
+ {
37
+ #if UNALIGNED_ACCESS_IS_FAST
38
+ return loaded_u32_to_u24(load_u32_unaligned(p));
39
+ #else
40
+ if (CPU_IS_LITTLE_ENDIAN())
41
+ return ((u32)p[0] << 0) | ((u32)p[1] << 8) | ((u32)p[2] << 16);
42
+ else
43
+ return ((u32)p[2] << 0) | ((u32)p[1] << 8) | ((u32)p[0] << 16);
44
+ #endif
45
+ }
46
+
47
+ #define MATCHFINDER_WINDOW_SIZE (1UL << MATCHFINDER_WINDOW_ORDER)
48
+
49
+ typedef s16 mf_pos_t;
50
+
51
+ #define MATCHFINDER_INITVAL ((mf_pos_t)-MATCHFINDER_WINDOW_SIZE)
52
+
53
+ /*
54
+ * This is the memory address alignment, in bytes, required for the matchfinder
55
+ * buffers by the architecture-specific implementations of matchfinder_init()
56
+ * and matchfinder_rebase(). "Matchfinder buffer" means an entire struct
57
+ * hc_matchfinder, bt_matchfinder, or ht_matchfinder; the next_tab field of
58
+ * struct hc_matchfinder; or the child_tab field of struct bt_matchfinder.
59
+ *
60
+ * This affects how the entire 'struct deflate_compressor' is allocated, since
61
+ * the matchfinder structures are embedded inside it.
62
+ *
63
+ * Currently the maximum memory address alignment required is 32 bytes, needed
64
+ * by the AVX-2 matchfinder functions.
65
+ */
66
+ #define MATCHFINDER_MEM_ALIGNMENT 32
67
+
68
+ /*
69
+ * This declares a size, in bytes, that is guaranteed to divide the sizes of the
70
+ * matchfinder buffers (where "matchfinder buffers" is as defined for
71
+ * MATCHFINDER_MEM_ALIGNMENT). The architecture-specific implementations of
72
+ * matchfinder_init() and matchfinder_rebase() take advantage of this value.
73
+ *
74
+ * Currently the maximum size alignment required is 128 bytes, needed by
75
+ * the AVX-2 matchfinder functions. However, the RISC-V Vector Extension
76
+ * matchfinder functions can, in principle, take advantage of a larger size
77
+ * alignment. Therefore, we set this to 1024, which still easily divides the
78
+ * actual sizes that result from the current matchfinder struct definitions.
79
+ * This value can safely be changed to any power of two that is >= 128.
80
+ */
81
+ #define MATCHFINDER_SIZE_ALIGNMENT 1024
82
+
83
+ #undef matchfinder_init
84
+ #undef matchfinder_rebase
85
+ #ifdef _aligned_attribute
86
+ # define MATCHFINDER_ALIGNED _aligned_attribute(MATCHFINDER_MEM_ALIGNMENT)
87
+ # if defined(ARCH_ARM32) || defined(ARCH_ARM64)
88
+ # include "arm/matchfinder_impl.h"
89
+ # elif defined(ARCH_RISCV)
90
+ # include "riscv/matchfinder_impl.h"
91
+ # elif defined(ARCH_X86_32) || defined(ARCH_X86_64)
92
+ # include "x86/matchfinder_impl.h"
93
+ # endif
94
+ #else
95
+ # define MATCHFINDER_ALIGNED
96
+ #endif
97
+
98
+ /*
99
+ * Initialize the hash table portion of the matchfinder.
100
+ *
101
+ * Essentially, this is an optimized memset().
102
+ *
103
+ * 'data' must be aligned to a MATCHFINDER_MEM_ALIGNMENT boundary, and
104
+ * 'size' must be a multiple of MATCHFINDER_SIZE_ALIGNMENT.
105
+ */
106
+ #ifndef matchfinder_init
107
+ static forceinline void
108
+ matchfinder_init(mf_pos_t *data, size_t size)
109
+ {
110
+ size_t num_entries = size / sizeof(*data);
111
+ size_t i;
112
+
113
+ for (i = 0; i < num_entries; i++)
114
+ data[i] = MATCHFINDER_INITVAL;
115
+ }
116
+ #endif
117
+
118
+ /*
119
+ * Slide the matchfinder by MATCHFINDER_WINDOW_SIZE bytes.
120
+ *
121
+ * This must be called just after each MATCHFINDER_WINDOW_SIZE bytes have been
122
+ * run through the matchfinder.
123
+ *
124
+ * This subtracts MATCHFINDER_WINDOW_SIZE bytes from each entry in the given
125
+ * array, making the entries be relative to the current position rather than the
126
+ * position MATCHFINDER_WINDOW_SIZE bytes prior. To avoid integer underflows,
127
+ * entries that would become less than -MATCHFINDER_WINDOW_SIZE stay at
128
+ * -MATCHFINDER_WINDOW_SIZE, keeping them permanently out of bounds.
129
+ *
130
+ * The given array must contain all matchfinder data that is position-relative:
131
+ * the hash table(s) as well as any hash chain or binary tree links. Its
132
+ * address must be aligned to a MATCHFINDER_MEM_ALIGNMENT boundary, and its size
133
+ * must be a multiple of MATCHFINDER_SIZE_ALIGNMENT.
134
+ */
135
+ #ifndef matchfinder_rebase
136
+ static forceinline void
137
+ matchfinder_rebase(mf_pos_t *data, size_t size)
138
+ {
139
+ size_t num_entries = size / sizeof(*data);
140
+ size_t i;
141
+
142
+ if (MATCHFINDER_WINDOW_SIZE == 32768) {
143
+ /*
144
+ * Branchless version for 32768-byte windows. Clear all bits if
145
+ * the value was already negative, then set the sign bit. This
146
+ * is equivalent to subtracting 32768 with signed saturation.
147
+ */
148
+ for (i = 0; i < num_entries; i++)
149
+ data[i] = 0x8000 | (data[i] & ~(data[i] >> 15));
150
+ } else {
151
+ for (i = 0; i < num_entries; i++) {
152
+ if (data[i] >= 0)
153
+ data[i] -= (mf_pos_t)-MATCHFINDER_WINDOW_SIZE;
154
+ else
155
+ data[i] = (mf_pos_t)-MATCHFINDER_WINDOW_SIZE;
156
+ }
157
+ }
158
+ }
159
+ #endif
160
+
161
+ /*
162
+ * The hash function: given a sequence prefix held in the low-order bits of a
163
+ * 32-bit value, multiply by a carefully-chosen large constant. Discard any
164
+ * bits of the product that don't fit in a 32-bit value, but take the
165
+ * next-highest @num_bits bits of the product as the hash value, as those have
166
+ * the most randomness.
167
+ */
168
+ static forceinline u32
169
+ lz_hash(u32 seq, unsigned num_bits)
170
+ {
171
+ return (u32)(seq * 0x1E35A7BD) >> (32 - num_bits);
172
+ }
173
+
174
+ /*
175
+ * Return the number of bytes at @matchptr that match the bytes at @strptr, up
176
+ * to a maximum of @max_len. Initially, @start_len bytes are matched.
177
+ */
178
+ static forceinline u32
179
+ lz_extend(const u8 * const strptr, const u8 * const matchptr,
180
+ const u32 start_len, const u32 max_len)
181
+ {
182
+ u32 len = start_len;
183
+ machine_word_t v_word;
184
+
185
+ if (UNALIGNED_ACCESS_IS_FAST) {
186
+
187
+ if (likely(max_len - len >= 4 * WORDBYTES)) {
188
+
189
+ #define COMPARE_WORD_STEP \
190
+ v_word = load_word_unaligned(&matchptr[len]) ^ \
191
+ load_word_unaligned(&strptr[len]); \
192
+ if (v_word != 0) \
193
+ goto word_differs; \
194
+ len += WORDBYTES; \
195
+
196
+ COMPARE_WORD_STEP
197
+ COMPARE_WORD_STEP
198
+ COMPARE_WORD_STEP
199
+ COMPARE_WORD_STEP
200
+ #undef COMPARE_WORD_STEP
201
+ }
202
+
203
+ while (len + WORDBYTES <= max_len) {
204
+ v_word = load_word_unaligned(&matchptr[len]) ^
205
+ load_word_unaligned(&strptr[len]);
206
+ if (v_word != 0)
207
+ goto word_differs;
208
+ len += WORDBYTES;
209
+ }
210
+ }
211
+
212
+ while (len < max_len && matchptr[len] == strptr[len])
213
+ len++;
214
+ return len;
215
+
216
+ word_differs:
217
+ if (CPU_IS_LITTLE_ENDIAN())
218
+ len += (bsfw(v_word) >> 3);
219
+ else
220
+ len += (WORDBITS - 1 - bsrw(v_word)) >> 3;
221
+ return len;
222
+ }
223
+
224
+ #endif /* LIB_MATCHFINDER_COMMON_H */