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.
- checksums.yaml +4 -4
- data/CLAUDE.md +95 -92
- data/GEM_VERIFICATION_REPORT.md +140 -0
- data/LICENSE.txt +6 -6
- data/README.md +87 -65
- data/Rakefile +23 -0
- data/ext/deflate_ruby/{libdeflate/lib/x86/adler32_impl.h → adler32_impl.h} +8 -7
- data/ext/deflate_ruby/common_defs.h +748 -0
- data/ext/deflate_ruby/{libdeflate/lib/x86/cpu_features.c → cpu_features.c} +46 -16
- data/ext/deflate_ruby/{libdeflate/lib/x86/cpu_features.h → cpu_features.h} +2 -1
- data/ext/deflate_ruby/{libdeflate/lib/x86/crc32_impl.h → crc32_impl.h} +22 -23
- data/ext/deflate_ruby/{libdeflate/lib/crc32_multipliers.h → crc32_multipliers.h} +2 -4
- data/ext/deflate_ruby/{libdeflate/lib/x86/crc32_pclmul_template.h → crc32_pclmul_template.h} +23 -94
- data/ext/deflate_ruby/{libdeflate/lib/crc32_tables.h → crc32_tables.h} +1 -1
- data/ext/deflate_ruby/{libdeflate/lib/deflate_compress.c → deflate_compress.c} +59 -60
- data/ext/deflate_ruby/deflate_ruby.c +392 -218
- data/ext/deflate_ruby/deflate_ruby.h +6 -0
- data/ext/deflate_ruby/extconf.rb +35 -25
- data/ext/deflate_ruby/libdeflate/adler32.c +162 -0
- data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/adler32_impl.h +14 -7
- data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/crc32_impl.h +25 -31
- data/ext/deflate_ruby/libdeflate/arm/crc32_pmull_helpers.h +156 -0
- data/ext/deflate_ruby/libdeflate/arm/crc32_pmull_wide.h +226 -0
- data/ext/deflate_ruby/libdeflate/bt_matchfinder.h +342 -0
- data/ext/deflate_ruby/libdeflate/common_defs.h +2 -1
- data/ext/deflate_ruby/libdeflate/cpu_features_common.h +93 -0
- data/ext/deflate_ruby/libdeflate/crc32.c +262 -0
- data/ext/deflate_ruby/libdeflate/crc32_multipliers.h +375 -0
- data/ext/deflate_ruby/libdeflate/crc32_tables.h +587 -0
- data/ext/deflate_ruby/libdeflate/decompress_template.h +777 -0
- data/ext/deflate_ruby/libdeflate/deflate_compress.c +4128 -0
- data/ext/deflate_ruby/libdeflate/deflate_compress.h +15 -0
- data/ext/deflate_ruby/libdeflate/deflate_constants.h +56 -0
- data/ext/deflate_ruby/libdeflate/deflate_decompress.c +1208 -0
- data/ext/deflate_ruby/libdeflate/gzip_compress.c +90 -0
- data/ext/deflate_ruby/libdeflate/gzip_constants.h +45 -0
- data/ext/deflate_ruby/libdeflate/gzip_decompress.c +144 -0
- data/ext/deflate_ruby/libdeflate/hc_matchfinder.h +401 -0
- data/ext/deflate_ruby/libdeflate/ht_matchfinder.h +234 -0
- data/ext/deflate_ruby/libdeflate/lib_common.h +106 -0
- data/ext/deflate_ruby/libdeflate/libdeflate.h +2 -2
- data/ext/deflate_ruby/libdeflate/{lib/matchfinder_common.h → matchfinder_common.h} +3 -3
- data/ext/deflate_ruby/libdeflate/x86/adler32_impl.h +135 -0
- data/ext/deflate_ruby/libdeflate/x86/adler32_template.h +518 -0
- data/ext/deflate_ruby/libdeflate/x86/cpu_features.c +213 -0
- data/ext/deflate_ruby/libdeflate/x86/cpu_features.h +170 -0
- data/ext/deflate_ruby/libdeflate/x86/crc32_impl.h +159 -0
- data/ext/deflate_ruby/libdeflate/x86/crc32_pclmul_template.h +424 -0
- data/ext/deflate_ruby/libdeflate/x86/decompress_impl.h +57 -0
- data/ext/deflate_ruby/libdeflate.h +411 -0
- data/ext/deflate_ruby/matchfinder_common.h +224 -0
- data/ext/deflate_ruby/matchfinder_impl.h +122 -0
- data/ext/deflate_ruby/utils.c +141 -0
- data/ext/deflate_ruby/zlib_compress.c +82 -0
- data/ext/deflate_ruby/zlib_constants.h +21 -0
- data/ext/deflate_ruby/zlib_decompress.c +104 -0
- data/lib/deflate_ruby/version.rb +1 -1
- data/lib/deflate_ruby.rb +1 -63
- data/sig/deflate_ruby.rbs +4 -0
- data/test/test_deflate_ruby.rb +220 -0
- data/test/test_helper.rb +6 -0
- metadata +90 -144
- data/ext/deflate_ruby/libdeflate/CMakeLists.txt +0 -270
- data/ext/deflate_ruby/libdeflate/NEWS.md +0 -494
- data/ext/deflate_ruby/libdeflate/README.md +0 -228
- data/ext/deflate_ruby/libdeflate/libdeflate-config.cmake.in +0 -3
- data/ext/deflate_ruby/libdeflate/libdeflate.pc.in +0 -18
- data/ext/deflate_ruby/libdeflate/programs/CMakeLists.txt +0 -105
- data/ext/deflate_ruby/libdeflate/programs/benchmark.c +0 -696
- data/ext/deflate_ruby/libdeflate/programs/checksum.c +0 -218
- data/ext/deflate_ruby/libdeflate/programs/config.h.in +0 -19
- data/ext/deflate_ruby/libdeflate/programs/gzip.c +0 -688
- data/ext/deflate_ruby/libdeflate/programs/prog_util.c +0 -521
- data/ext/deflate_ruby/libdeflate/programs/prog_util.h +0 -225
- data/ext/deflate_ruby/libdeflate/programs/test_checksums.c +0 -200
- data/ext/deflate_ruby/libdeflate/programs/test_custom_malloc.c +0 -155
- data/ext/deflate_ruby/libdeflate/programs/test_incomplete_codes.c +0 -385
- data/ext/deflate_ruby/libdeflate/programs/test_invalid_streams.c +0 -130
- data/ext/deflate_ruby/libdeflate/programs/test_litrunlen_overflow.c +0 -72
- data/ext/deflate_ruby/libdeflate/programs/test_overread.c +0 -95
- data/ext/deflate_ruby/libdeflate/programs/test_slow_decompression.c +0 -472
- data/ext/deflate_ruby/libdeflate/programs/test_trailing_bytes.c +0 -151
- data/ext/deflate_ruby/libdeflate/programs/test_util.c +0 -237
- data/ext/deflate_ruby/libdeflate/programs/test_util.h +0 -61
- data/ext/deflate_ruby/libdeflate/programs/tgetopt.c +0 -118
- data/ext/deflate_ruby/libdeflate/scripts/android_build.sh +0 -118
- data/ext/deflate_ruby/libdeflate/scripts/android_tests.sh +0 -69
- data/ext/deflate_ruby/libdeflate/scripts/benchmark.sh +0 -10
- data/ext/deflate_ruby/libdeflate/scripts/checksum.sh +0 -10
- data/ext/deflate_ruby/libdeflate/scripts/checksum_benchmarks.sh +0 -253
- data/ext/deflate_ruby/libdeflate/scripts/cmake-helper.sh +0 -17
- data/ext/deflate_ruby/libdeflate/scripts/deflate_benchmarks.sh +0 -119
- data/ext/deflate_ruby/libdeflate/scripts/exec_tests.sh +0 -38
- data/ext/deflate_ruby/libdeflate/scripts/gen-release-archives.sh +0 -37
- data/ext/deflate_ruby/libdeflate/scripts/gen_bitreverse_tab.py +0 -19
- data/ext/deflate_ruby/libdeflate/scripts/gen_crc32_multipliers.c +0 -199
- data/ext/deflate_ruby/libdeflate/scripts/gen_crc32_tables.c +0 -105
- data/ext/deflate_ruby/libdeflate/scripts/gen_default_litlen_costs.py +0 -44
- data/ext/deflate_ruby/libdeflate/scripts/gen_offset_slot_map.py +0 -29
- data/ext/deflate_ruby/libdeflate/scripts/gzip_tests.sh +0 -523
- data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_compress/corpus/0 +0 -0
- data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_compress/fuzz.c +0 -95
- data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_decompress/corpus/0 +0 -3
- data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/deflate_decompress/fuzz.c +0 -62
- data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/fuzz.sh +0 -108
- data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/gzip_decompress/corpus/0 +0 -0
- data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/gzip_decompress/fuzz.c +0 -19
- data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/zlib_decompress/corpus/0 +0 -3
- data/ext/deflate_ruby/libdeflate/scripts/libFuzzer/zlib_decompress/fuzz.c +0 -19
- data/ext/deflate_ruby/libdeflate/scripts/run_tests.sh +0 -416
- data/ext/deflate_ruby/libdeflate/scripts/toolchain-i686-w64-mingw32.cmake +0 -8
- data/ext/deflate_ruby/libdeflate/scripts/toolchain-x86_64-w64-mingw32.cmake +0 -8
- /data/ext/deflate_ruby/{libdeflate/lib/adler32.c → adler32.c} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/x86/adler32_template.h → adler32_template.h} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/bt_matchfinder.h → bt_matchfinder.h} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/cpu_features_common.h → cpu_features_common.h} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/crc32.c → crc32.c} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/arm/crc32_pmull_helpers.h → crc32_pmull_helpers.h} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/arm/crc32_pmull_wide.h → crc32_pmull_wide.h} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/x86/decompress_impl.h → decompress_impl.h} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/decompress_template.h → decompress_template.h} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/deflate_compress.h → deflate_compress.h} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/deflate_constants.h → deflate_constants.h} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/deflate_decompress.c → deflate_decompress.c} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/gzip_compress.c → gzip_compress.c} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/gzip_constants.h → gzip_constants.h} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/gzip_decompress.c → gzip_decompress.c} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/hc_matchfinder.h → hc_matchfinder.h} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/ht_matchfinder.h → ht_matchfinder.h} +0 -0
- /data/ext/deflate_ruby/{libdeflate/lib/lib_common.h → lib_common.h} +0 -0
- /data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/cpu_features.c +0 -0
- /data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/cpu_features.h +0 -0
- /data/ext/deflate_ruby/libdeflate/{lib/arm → arm}/matchfinder_impl.h +0 -0
- /data/ext/deflate_ruby/libdeflate/{lib/riscv → riscv}/matchfinder_impl.h +0 -0
- /data/ext/deflate_ruby/libdeflate/{lib/utils.c → utils.c} +0 -0
- /data/ext/deflate_ruby/libdeflate/{lib/x86 → x86}/matchfinder_impl.h +0 -0
- /data/ext/deflate_ruby/libdeflate/{lib/zlib_compress.c → zlib_compress.c} +0 -0
- /data/ext/deflate_ruby/libdeflate/{lib/zlib_constants.h → zlib_constants.h} +0 -0
- /data/ext/deflate_ruby/libdeflate/{lib/zlib_decompress.c → zlib_decompress.c} +0 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* x86/matchfinder_impl.h - x86 implementations of matchfinder functions
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2016 Eric Biggers
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person
|
|
7
|
+
* obtaining a copy of this software and associated documentation
|
|
8
|
+
* files (the "Software"), to deal in the Software without
|
|
9
|
+
* restriction, including without limitation the rights to use,
|
|
10
|
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the
|
|
12
|
+
* Software is furnished to do so, subject to the following
|
|
13
|
+
* conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be
|
|
16
|
+
* included in all copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
20
|
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
21
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
23
|
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
24
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
25
|
+
* OTHER DEALINGS IN THE SOFTWARE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
#ifndef LIB_X86_MATCHFINDER_IMPL_H
|
|
29
|
+
#define LIB_X86_MATCHFINDER_IMPL_H
|
|
30
|
+
|
|
31
|
+
#include "cpu_features.h"
|
|
32
|
+
|
|
33
|
+
#ifdef __AVX2__
|
|
34
|
+
static forceinline void
|
|
35
|
+
matchfinder_init_avx2(mf_pos_t *data, size_t size)
|
|
36
|
+
{
|
|
37
|
+
__m256i *p = (__m256i *)data;
|
|
38
|
+
__m256i v = _mm256_set1_epi16(MATCHFINDER_INITVAL);
|
|
39
|
+
|
|
40
|
+
STATIC_ASSERT(MATCHFINDER_MEM_ALIGNMENT % sizeof(*p) == 0);
|
|
41
|
+
STATIC_ASSERT(MATCHFINDER_SIZE_ALIGNMENT % (4 * sizeof(*p)) == 0);
|
|
42
|
+
STATIC_ASSERT(sizeof(mf_pos_t) == 2);
|
|
43
|
+
|
|
44
|
+
do {
|
|
45
|
+
p[0] = v;
|
|
46
|
+
p[1] = v;
|
|
47
|
+
p[2] = v;
|
|
48
|
+
p[3] = v;
|
|
49
|
+
p += 4;
|
|
50
|
+
size -= 4 * sizeof(*p);
|
|
51
|
+
} while (size != 0);
|
|
52
|
+
}
|
|
53
|
+
#define matchfinder_init matchfinder_init_avx2
|
|
54
|
+
|
|
55
|
+
static forceinline void
|
|
56
|
+
matchfinder_rebase_avx2(mf_pos_t *data, size_t size)
|
|
57
|
+
{
|
|
58
|
+
__m256i *p = (__m256i *)data;
|
|
59
|
+
__m256i v = _mm256_set1_epi16((u16)-MATCHFINDER_WINDOW_SIZE);
|
|
60
|
+
|
|
61
|
+
STATIC_ASSERT(MATCHFINDER_MEM_ALIGNMENT % sizeof(*p) == 0);
|
|
62
|
+
STATIC_ASSERT(MATCHFINDER_SIZE_ALIGNMENT % (4 * sizeof(*p)) == 0);
|
|
63
|
+
STATIC_ASSERT(sizeof(mf_pos_t) == 2);
|
|
64
|
+
|
|
65
|
+
do {
|
|
66
|
+
/* PADDSW: Add Packed Signed Integers With Signed Saturation */
|
|
67
|
+
p[0] = _mm256_adds_epi16(p[0], v);
|
|
68
|
+
p[1] = _mm256_adds_epi16(p[1], v);
|
|
69
|
+
p[2] = _mm256_adds_epi16(p[2], v);
|
|
70
|
+
p[3] = _mm256_adds_epi16(p[3], v);
|
|
71
|
+
p += 4;
|
|
72
|
+
size -= 4 * sizeof(*p);
|
|
73
|
+
} while (size != 0);
|
|
74
|
+
}
|
|
75
|
+
#define matchfinder_rebase matchfinder_rebase_avx2
|
|
76
|
+
|
|
77
|
+
#elif HAVE_SSE2_NATIVE
|
|
78
|
+
static forceinline void
|
|
79
|
+
matchfinder_init_sse2(mf_pos_t *data, size_t size)
|
|
80
|
+
{
|
|
81
|
+
__m128i *p = (__m128i *)data;
|
|
82
|
+
__m128i v = _mm_set1_epi16(MATCHFINDER_INITVAL);
|
|
83
|
+
|
|
84
|
+
STATIC_ASSERT(MATCHFINDER_MEM_ALIGNMENT % sizeof(*p) == 0);
|
|
85
|
+
STATIC_ASSERT(MATCHFINDER_SIZE_ALIGNMENT % (4 * sizeof(*p)) == 0);
|
|
86
|
+
STATIC_ASSERT(sizeof(mf_pos_t) == 2);
|
|
87
|
+
|
|
88
|
+
do {
|
|
89
|
+
p[0] = v;
|
|
90
|
+
p[1] = v;
|
|
91
|
+
p[2] = v;
|
|
92
|
+
p[3] = v;
|
|
93
|
+
p += 4;
|
|
94
|
+
size -= 4 * sizeof(*p);
|
|
95
|
+
} while (size != 0);
|
|
96
|
+
}
|
|
97
|
+
#define matchfinder_init matchfinder_init_sse2
|
|
98
|
+
|
|
99
|
+
static forceinline void
|
|
100
|
+
matchfinder_rebase_sse2(mf_pos_t *data, size_t size)
|
|
101
|
+
{
|
|
102
|
+
__m128i *p = (__m128i *)data;
|
|
103
|
+
__m128i v = _mm_set1_epi16((u16)-MATCHFINDER_WINDOW_SIZE);
|
|
104
|
+
|
|
105
|
+
STATIC_ASSERT(MATCHFINDER_MEM_ALIGNMENT % sizeof(*p) == 0);
|
|
106
|
+
STATIC_ASSERT(MATCHFINDER_SIZE_ALIGNMENT % (4 * sizeof(*p)) == 0);
|
|
107
|
+
STATIC_ASSERT(sizeof(mf_pos_t) == 2);
|
|
108
|
+
|
|
109
|
+
do {
|
|
110
|
+
/* PADDSW: Add Packed Signed Integers With Signed Saturation */
|
|
111
|
+
p[0] = _mm_adds_epi16(p[0], v);
|
|
112
|
+
p[1] = _mm_adds_epi16(p[1], v);
|
|
113
|
+
p[2] = _mm_adds_epi16(p[2], v);
|
|
114
|
+
p[3] = _mm_adds_epi16(p[3], v);
|
|
115
|
+
p += 4;
|
|
116
|
+
size -= 4 * sizeof(*p);
|
|
117
|
+
} while (size != 0);
|
|
118
|
+
}
|
|
119
|
+
#define matchfinder_rebase matchfinder_rebase_sse2
|
|
120
|
+
#endif /* HAVE_SSE2_NATIVE */
|
|
121
|
+
|
|
122
|
+
#endif /* LIB_X86_MATCHFINDER_IMPL_H */
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* utils.c - utility functions for libdeflate
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2016 Eric Biggers
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person
|
|
7
|
+
* obtaining a copy of this software and associated documentation
|
|
8
|
+
* files (the "Software"), to deal in the Software without
|
|
9
|
+
* restriction, including without limitation the rights to use,
|
|
10
|
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the
|
|
12
|
+
* Software is furnished to do so, subject to the following
|
|
13
|
+
* conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be
|
|
16
|
+
* included in all copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
20
|
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
21
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
23
|
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
24
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
25
|
+
* OTHER DEALINGS IN THE SOFTWARE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
#include "lib_common.h"
|
|
29
|
+
|
|
30
|
+
#ifdef FREESTANDING
|
|
31
|
+
# define malloc NULL
|
|
32
|
+
# define free NULL
|
|
33
|
+
#else
|
|
34
|
+
# include <stdlib.h>
|
|
35
|
+
#endif
|
|
36
|
+
|
|
37
|
+
malloc_func_t libdeflate_default_malloc_func = malloc;
|
|
38
|
+
free_func_t libdeflate_default_free_func = free;
|
|
39
|
+
|
|
40
|
+
void *
|
|
41
|
+
libdeflate_aligned_malloc(malloc_func_t malloc_func,
|
|
42
|
+
size_t alignment, size_t size)
|
|
43
|
+
{
|
|
44
|
+
void *ptr = (*malloc_func)(sizeof(void *) + alignment - 1 + size);
|
|
45
|
+
|
|
46
|
+
if (ptr) {
|
|
47
|
+
void *orig_ptr = ptr;
|
|
48
|
+
|
|
49
|
+
ptr = (void *)ALIGN((uintptr_t)ptr + sizeof(void *), alignment);
|
|
50
|
+
((void **)ptr)[-1] = orig_ptr;
|
|
51
|
+
}
|
|
52
|
+
return ptr;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
void
|
|
56
|
+
libdeflate_aligned_free(free_func_t free_func, void *ptr)
|
|
57
|
+
{
|
|
58
|
+
(*free_func)(((void **)ptr)[-1]);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
LIBDEFLATEAPI void
|
|
62
|
+
libdeflate_set_memory_allocator(malloc_func_t malloc_func,
|
|
63
|
+
free_func_t free_func)
|
|
64
|
+
{
|
|
65
|
+
libdeflate_default_malloc_func = malloc_func;
|
|
66
|
+
libdeflate_default_free_func = free_func;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/*
|
|
70
|
+
* Implementations of libc functions for freestanding library builds.
|
|
71
|
+
* Normal library builds don't use these. Not optimized yet; usually the
|
|
72
|
+
* compiler expands these functions and doesn't actually call them anyway.
|
|
73
|
+
*/
|
|
74
|
+
#ifdef FREESTANDING
|
|
75
|
+
#undef memset
|
|
76
|
+
void * __attribute__((weak))
|
|
77
|
+
memset(void *s, int c, size_t n)
|
|
78
|
+
{
|
|
79
|
+
u8 *p = s;
|
|
80
|
+
size_t i;
|
|
81
|
+
|
|
82
|
+
for (i = 0; i < n; i++)
|
|
83
|
+
p[i] = c;
|
|
84
|
+
return s;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
#undef memcpy
|
|
88
|
+
void * __attribute__((weak))
|
|
89
|
+
memcpy(void *dest, const void *src, size_t n)
|
|
90
|
+
{
|
|
91
|
+
u8 *d = dest;
|
|
92
|
+
const u8 *s = src;
|
|
93
|
+
size_t i;
|
|
94
|
+
|
|
95
|
+
for (i = 0; i < n; i++)
|
|
96
|
+
d[i] = s[i];
|
|
97
|
+
return dest;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
#undef memmove
|
|
101
|
+
void * __attribute__((weak))
|
|
102
|
+
memmove(void *dest, const void *src, size_t n)
|
|
103
|
+
{
|
|
104
|
+
u8 *d = dest;
|
|
105
|
+
const u8 *s = src;
|
|
106
|
+
size_t i;
|
|
107
|
+
|
|
108
|
+
if (d <= s)
|
|
109
|
+
return memcpy(d, s, n);
|
|
110
|
+
|
|
111
|
+
for (i = n; i > 0; i--)
|
|
112
|
+
d[i - 1] = s[i - 1];
|
|
113
|
+
return dest;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
#undef memcmp
|
|
117
|
+
int __attribute__((weak))
|
|
118
|
+
memcmp(const void *s1, const void *s2, size_t n)
|
|
119
|
+
{
|
|
120
|
+
const u8 *p1 = s1;
|
|
121
|
+
const u8 *p2 = s2;
|
|
122
|
+
size_t i;
|
|
123
|
+
|
|
124
|
+
for (i = 0; i < n; i++) {
|
|
125
|
+
if (p1[i] != p2[i])
|
|
126
|
+
return (int)p1[i] - (int)p2[i];
|
|
127
|
+
}
|
|
128
|
+
return 0;
|
|
129
|
+
}
|
|
130
|
+
#endif /* FREESTANDING */
|
|
131
|
+
|
|
132
|
+
#ifdef LIBDEFLATE_ENABLE_ASSERTIONS
|
|
133
|
+
#include <stdio.h>
|
|
134
|
+
#include <stdlib.h>
|
|
135
|
+
NORETURN void
|
|
136
|
+
libdeflate_assertion_failed(const char *expr, const char *file, int line)
|
|
137
|
+
{
|
|
138
|
+
fprintf(stderr, "Assertion failed: %s at %s:%d\n", expr, file, line);
|
|
139
|
+
abort();
|
|
140
|
+
}
|
|
141
|
+
#endif /* LIBDEFLATE_ENABLE_ASSERTIONS */
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* zlib_compress.c - compress with a zlib wrapper
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2016 Eric Biggers
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person
|
|
7
|
+
* obtaining a copy of this software and associated documentation
|
|
8
|
+
* files (the "Software"), to deal in the Software without
|
|
9
|
+
* restriction, including without limitation the rights to use,
|
|
10
|
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the
|
|
12
|
+
* Software is furnished to do so, subject to the following
|
|
13
|
+
* conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be
|
|
16
|
+
* included in all copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
20
|
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
21
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
23
|
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
24
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
25
|
+
* OTHER DEALINGS IN THE SOFTWARE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
#include "deflate_compress.h"
|
|
29
|
+
#include "zlib_constants.h"
|
|
30
|
+
|
|
31
|
+
LIBDEFLATEAPI size_t
|
|
32
|
+
libdeflate_zlib_compress(struct libdeflate_compressor *c,
|
|
33
|
+
const void *in, size_t in_nbytes,
|
|
34
|
+
void *out, size_t out_nbytes_avail)
|
|
35
|
+
{
|
|
36
|
+
u8 *out_next = out;
|
|
37
|
+
u16 hdr;
|
|
38
|
+
unsigned compression_level;
|
|
39
|
+
unsigned level_hint;
|
|
40
|
+
size_t deflate_size;
|
|
41
|
+
|
|
42
|
+
if (out_nbytes_avail <= ZLIB_MIN_OVERHEAD)
|
|
43
|
+
return 0;
|
|
44
|
+
|
|
45
|
+
/* 2 byte header: CMF and FLG */
|
|
46
|
+
hdr = (ZLIB_CM_DEFLATE << 8) | (ZLIB_CINFO_32K_WINDOW << 12);
|
|
47
|
+
compression_level = libdeflate_get_compression_level(c);
|
|
48
|
+
if (compression_level < 2)
|
|
49
|
+
level_hint = ZLIB_FASTEST_COMPRESSION;
|
|
50
|
+
else if (compression_level < 6)
|
|
51
|
+
level_hint = ZLIB_FAST_COMPRESSION;
|
|
52
|
+
else if (compression_level < 8)
|
|
53
|
+
level_hint = ZLIB_DEFAULT_COMPRESSION;
|
|
54
|
+
else
|
|
55
|
+
level_hint = ZLIB_SLOWEST_COMPRESSION;
|
|
56
|
+
hdr |= level_hint << 6;
|
|
57
|
+
hdr |= 31 - (hdr % 31);
|
|
58
|
+
|
|
59
|
+
put_unaligned_be16(hdr, out_next);
|
|
60
|
+
out_next += 2;
|
|
61
|
+
|
|
62
|
+
/* Compressed data */
|
|
63
|
+
deflate_size = libdeflate_deflate_compress(c, in, in_nbytes, out_next,
|
|
64
|
+
out_nbytes_avail - ZLIB_MIN_OVERHEAD);
|
|
65
|
+
if (deflate_size == 0)
|
|
66
|
+
return 0;
|
|
67
|
+
out_next += deflate_size;
|
|
68
|
+
|
|
69
|
+
/* ADLER32 */
|
|
70
|
+
put_unaligned_be32(libdeflate_adler32(1, in, in_nbytes), out_next);
|
|
71
|
+
out_next += 4;
|
|
72
|
+
|
|
73
|
+
return out_next - (u8 *)out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
LIBDEFLATEAPI size_t
|
|
77
|
+
libdeflate_zlib_compress_bound(struct libdeflate_compressor *c,
|
|
78
|
+
size_t in_nbytes)
|
|
79
|
+
{
|
|
80
|
+
return ZLIB_MIN_OVERHEAD +
|
|
81
|
+
libdeflate_deflate_compress_bound(c, in_nbytes);
|
|
82
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* zlib_constants.h - constants for the zlib wrapper format
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#ifndef LIB_ZLIB_CONSTANTS_H
|
|
6
|
+
#define LIB_ZLIB_CONSTANTS_H
|
|
7
|
+
|
|
8
|
+
#define ZLIB_MIN_HEADER_SIZE 2
|
|
9
|
+
#define ZLIB_FOOTER_SIZE 4
|
|
10
|
+
#define ZLIB_MIN_OVERHEAD (ZLIB_MIN_HEADER_SIZE + ZLIB_FOOTER_SIZE)
|
|
11
|
+
|
|
12
|
+
#define ZLIB_CM_DEFLATE 8
|
|
13
|
+
|
|
14
|
+
#define ZLIB_CINFO_32K_WINDOW 7
|
|
15
|
+
|
|
16
|
+
#define ZLIB_FASTEST_COMPRESSION 0
|
|
17
|
+
#define ZLIB_FAST_COMPRESSION 1
|
|
18
|
+
#define ZLIB_DEFAULT_COMPRESSION 2
|
|
19
|
+
#define ZLIB_SLOWEST_COMPRESSION 3
|
|
20
|
+
|
|
21
|
+
#endif /* LIB_ZLIB_CONSTANTS_H */
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* zlib_decompress.c - decompress with a zlib wrapper
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2016 Eric Biggers
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person
|
|
7
|
+
* obtaining a copy of this software and associated documentation
|
|
8
|
+
* files (the "Software"), to deal in the Software without
|
|
9
|
+
* restriction, including without limitation the rights to use,
|
|
10
|
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the
|
|
12
|
+
* Software is furnished to do so, subject to the following
|
|
13
|
+
* conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be
|
|
16
|
+
* included in all copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
20
|
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
21
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
23
|
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
24
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
25
|
+
* OTHER DEALINGS IN THE SOFTWARE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
#include "lib_common.h"
|
|
29
|
+
#include "zlib_constants.h"
|
|
30
|
+
|
|
31
|
+
LIBDEFLATEAPI enum libdeflate_result
|
|
32
|
+
libdeflate_zlib_decompress_ex(struct libdeflate_decompressor *d,
|
|
33
|
+
const void *in, size_t in_nbytes,
|
|
34
|
+
void *out, size_t out_nbytes_avail,
|
|
35
|
+
size_t *actual_in_nbytes_ret,
|
|
36
|
+
size_t *actual_out_nbytes_ret)
|
|
37
|
+
{
|
|
38
|
+
const u8 *in_next = in;
|
|
39
|
+
const u8 * const in_end = in_next + in_nbytes;
|
|
40
|
+
u16 hdr;
|
|
41
|
+
size_t actual_in_nbytes;
|
|
42
|
+
size_t actual_out_nbytes;
|
|
43
|
+
enum libdeflate_result result;
|
|
44
|
+
|
|
45
|
+
if (in_nbytes < ZLIB_MIN_OVERHEAD)
|
|
46
|
+
return LIBDEFLATE_BAD_DATA;
|
|
47
|
+
|
|
48
|
+
/* 2 byte header: CMF and FLG */
|
|
49
|
+
hdr = get_unaligned_be16(in_next);
|
|
50
|
+
in_next += 2;
|
|
51
|
+
|
|
52
|
+
/* FCHECK */
|
|
53
|
+
if ((hdr % 31) != 0)
|
|
54
|
+
return LIBDEFLATE_BAD_DATA;
|
|
55
|
+
|
|
56
|
+
/* CM */
|
|
57
|
+
if (((hdr >> 8) & 0xF) != ZLIB_CM_DEFLATE)
|
|
58
|
+
return LIBDEFLATE_BAD_DATA;
|
|
59
|
+
|
|
60
|
+
/* CINFO */
|
|
61
|
+
if ((hdr >> 12) > ZLIB_CINFO_32K_WINDOW)
|
|
62
|
+
return LIBDEFLATE_BAD_DATA;
|
|
63
|
+
|
|
64
|
+
/* FDICT */
|
|
65
|
+
if ((hdr >> 5) & 1)
|
|
66
|
+
return LIBDEFLATE_BAD_DATA;
|
|
67
|
+
|
|
68
|
+
/* Compressed data */
|
|
69
|
+
result = libdeflate_deflate_decompress_ex(d, in_next,
|
|
70
|
+
in_end - ZLIB_FOOTER_SIZE - in_next,
|
|
71
|
+
out, out_nbytes_avail,
|
|
72
|
+
&actual_in_nbytes, actual_out_nbytes_ret);
|
|
73
|
+
if (result != LIBDEFLATE_SUCCESS)
|
|
74
|
+
return result;
|
|
75
|
+
|
|
76
|
+
if (actual_out_nbytes_ret)
|
|
77
|
+
actual_out_nbytes = *actual_out_nbytes_ret;
|
|
78
|
+
else
|
|
79
|
+
actual_out_nbytes = out_nbytes_avail;
|
|
80
|
+
|
|
81
|
+
in_next += actual_in_nbytes;
|
|
82
|
+
|
|
83
|
+
/* ADLER32 */
|
|
84
|
+
if (libdeflate_adler32(1, out, actual_out_nbytes) !=
|
|
85
|
+
get_unaligned_be32(in_next))
|
|
86
|
+
return LIBDEFLATE_BAD_DATA;
|
|
87
|
+
in_next += 4;
|
|
88
|
+
|
|
89
|
+
if (actual_in_nbytes_ret)
|
|
90
|
+
*actual_in_nbytes_ret = in_next - (u8 *)in;
|
|
91
|
+
|
|
92
|
+
return LIBDEFLATE_SUCCESS;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
LIBDEFLATEAPI enum libdeflate_result
|
|
96
|
+
libdeflate_zlib_decompress(struct libdeflate_decompressor *d,
|
|
97
|
+
const void *in, size_t in_nbytes,
|
|
98
|
+
void *out, size_t out_nbytes_avail,
|
|
99
|
+
size_t *actual_out_nbytes_ret)
|
|
100
|
+
{
|
|
101
|
+
return libdeflate_zlib_decompress_ex(d, in, in_nbytes,
|
|
102
|
+
out, out_nbytes_avail,
|
|
103
|
+
NULL, actual_out_nbytes_ret);
|
|
104
|
+
}
|
data/lib/deflate_ruby/version.rb
CHANGED
data/lib/deflate_ruby.rb
CHANGED
|
@@ -4,68 +4,6 @@ require_relative "deflate_ruby/version"
|
|
|
4
4
|
require_relative "deflate_ruby/deflate_ruby"
|
|
5
5
|
|
|
6
6
|
module DeflateRuby
|
|
7
|
+
# Base error class for all DeflateRuby errors
|
|
7
8
|
class Error < StandardError; end
|
|
8
|
-
|
|
9
|
-
# Compress data from a file using DEFLATE format
|
|
10
|
-
#
|
|
11
|
-
# @param input_file [String] Path to input file
|
|
12
|
-
# @param output_file [String] Path to output file
|
|
13
|
-
# @param level [Integer] Compression level (1-12, default: 6)
|
|
14
|
-
def self.deflate_compress_file(input_file, output_file, level: 6)
|
|
15
|
-
data = File.binread(input_file)
|
|
16
|
-
compressed = deflate_compress(data, level)
|
|
17
|
-
File.binwrite(output_file, compressed)
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
# Decompress DEFLATE data from a file
|
|
21
|
-
#
|
|
22
|
-
# @param input_file [String] Path to input file
|
|
23
|
-
# @param output_file [String] Path to output file
|
|
24
|
-
def self.deflate_decompress_file(input_file, output_file)
|
|
25
|
-
data = File.binread(input_file)
|
|
26
|
-
decompressed = deflate_decompress(data)
|
|
27
|
-
File.binwrite(output_file, decompressed)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
# Compress data from a file using zlib format
|
|
31
|
-
#
|
|
32
|
-
# @param input_file [String] Path to input file
|
|
33
|
-
# @param output_file [String] Path to output file
|
|
34
|
-
# @param level [Integer] Compression level (1-12, default: 6)
|
|
35
|
-
def self.zlib_compress_file(input_file, output_file, level: 6)
|
|
36
|
-
data = File.binread(input_file)
|
|
37
|
-
compressed = zlib_compress(data, level)
|
|
38
|
-
File.binwrite(output_file, compressed)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
# Decompress zlib data from a file
|
|
42
|
-
#
|
|
43
|
-
# @param input_file [String] Path to input file
|
|
44
|
-
# @param output_file [String] Path to output file
|
|
45
|
-
def self.zlib_decompress_file(input_file, output_file)
|
|
46
|
-
data = File.binread(input_file)
|
|
47
|
-
decompressed = zlib_decompress(data)
|
|
48
|
-
File.binwrite(output_file, decompressed)
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
# Compress data from a file using gzip format
|
|
52
|
-
#
|
|
53
|
-
# @param input_file [String] Path to input file
|
|
54
|
-
# @param output_file [String] Path to output file
|
|
55
|
-
# @param level [Integer] Compression level (1-12, default: 6)
|
|
56
|
-
def self.gzip_compress_file(input_file, output_file, level: 6)
|
|
57
|
-
data = File.binread(input_file)
|
|
58
|
-
compressed = gzip_compress(data, level)
|
|
59
|
-
File.binwrite(output_file, compressed)
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
# Decompress gzip data from a file
|
|
63
|
-
#
|
|
64
|
-
# @param input_file [String] Path to input file
|
|
65
|
-
# @param output_file [String] Path to output file
|
|
66
|
-
def self.gzip_decompress_file(input_file, output_file)
|
|
67
|
-
data = File.binread(input_file)
|
|
68
|
-
decompressed = gzip_decompress(data)
|
|
69
|
-
File.binwrite(output_file, decompressed)
|
|
70
|
-
end
|
|
71
9
|
end
|