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
@@ -1,521 +0,0 @@
1
- /*
2
- * prog_util.c - utility functions for programs
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 "prog_util.h"
29
-
30
- #include <errno.h>
31
- #include <fcntl.h>
32
- #include <stdarg.h>
33
- #ifdef _WIN32
34
- # include <windows.h>
35
- #else
36
- # include <unistd.h>
37
- # include <sys/mman.h>
38
- #endif
39
-
40
- #ifndef O_BINARY
41
- # define O_BINARY 0
42
- #endif
43
- #ifndef O_SEQUENTIAL
44
- # define O_SEQUENTIAL 0
45
- #endif
46
- #ifndef O_NOFOLLOW
47
- # define O_NOFOLLOW 0
48
- #endif
49
- #ifndef O_NONBLOCK
50
- # define O_NONBLOCK 0
51
- #endif
52
- #ifndef O_NOCTTY
53
- # define O_NOCTTY 0
54
- #endif
55
-
56
- /* The invocation name of the program (filename component only) */
57
- const tchar *prog_invocation_name;
58
-
59
- /* Whether to suppress warning messages or not */
60
- bool suppress_warnings;
61
-
62
- static void
63
- do_msg(const char *format, bool with_errno, va_list va)
64
- {
65
- int saved_errno = errno;
66
-
67
- fprintf(stderr, "%"TS": ", prog_invocation_name);
68
- vfprintf(stderr, format, va);
69
- if (with_errno)
70
- fprintf(stderr, ": %s\n", strerror(saved_errno));
71
- else
72
- fprintf(stderr, "\n");
73
-
74
- errno = saved_errno;
75
- }
76
-
77
- /* Print a message to standard error */
78
- void
79
- msg(const char *format, ...)
80
- {
81
- va_list va;
82
-
83
- va_start(va, format);
84
- do_msg(format, false, va);
85
- va_end(va);
86
- }
87
-
88
- /* Print a message to standard error, including a description of errno */
89
- void
90
- msg_errno(const char *format, ...)
91
- {
92
- va_list va;
93
-
94
- va_start(va, format);
95
- do_msg(format, true, va);
96
- va_end(va);
97
- }
98
-
99
-
100
- /* Same as msg(), but do nothing if 'suppress_warnings' has been set. */
101
- void
102
- warn(const char *format, ...)
103
- {
104
- if (!suppress_warnings) {
105
- va_list va;
106
-
107
- va_start(va, format);
108
- do_msg(format, false, va);
109
- va_end(va);
110
- }
111
- }
112
-
113
- /* malloc() wrapper */
114
- void *
115
- xmalloc(size_t size)
116
- {
117
- void *p = malloc(size);
118
- if (p == NULL && size == 0)
119
- p = malloc(1);
120
- if (p == NULL)
121
- msg("Out of memory");
122
- return p;
123
- }
124
-
125
- /*
126
- * Retrieve a pointer to the filename component of the specified path.
127
- *
128
- * Note: this does not modify the path. Therefore, it is not guaranteed to work
129
- * properly for directories, since a path to a directory might have trailing
130
- * slashes.
131
- */
132
- static const tchar *
133
- get_filename(const tchar *path)
134
- {
135
- const tchar *slash = tstrrchr(path, '/');
136
- #ifdef _WIN32
137
- const tchar *backslash = tstrrchr(path, '\\');
138
- if (backslash != NULL && (slash == NULL || backslash > slash))
139
- slash = backslash;
140
- #endif
141
- if (slash != NULL)
142
- return slash + 1;
143
- return path;
144
- }
145
-
146
- void
147
- begin_program(tchar *argv[])
148
- {
149
- prog_invocation_name = get_filename(argv[0]);
150
-
151
- #ifdef FREESTANDING
152
- /* This allows testing freestanding library builds. */
153
- libdeflate_set_memory_allocator(malloc, free);
154
- #endif
155
- }
156
-
157
- /* Create a copy of 'path' surrounded by double quotes */
158
- static tchar *
159
- quote_path(const tchar *path)
160
- {
161
- size_t len = tstrlen(path);
162
- tchar *result;
163
-
164
- result = xmalloc((1 + len + 1 + 1) * sizeof(tchar));
165
- if (result == NULL)
166
- return NULL;
167
- result[0] = '"';
168
- tmemcpy(&result[1], path, len);
169
- result[1 + len] = '"';
170
- result[1 + len + 1] = '\0';
171
- return result;
172
- }
173
-
174
- /* Open a file for reading, or set up standard input for reading */
175
- int
176
- xopen_for_read(const tchar *path, bool symlink_ok, struct file_stream *strm)
177
- {
178
- strm->mmap_token = NULL;
179
- strm->mmap_mem = NULL;
180
-
181
- if (path == NULL) {
182
- strm->is_standard_stream = true;
183
- strm->name = T("standard input");
184
- strm->fd = STDIN_FILENO;
185
- #ifdef _WIN32
186
- _setmode(strm->fd, O_BINARY);
187
- #endif
188
- return 0;
189
- }
190
-
191
- strm->is_standard_stream = false;
192
-
193
- strm->name = quote_path(path);
194
- if (strm->name == NULL)
195
- return -1;
196
-
197
- strm->fd = topen(path, O_RDONLY | O_BINARY | O_NONBLOCK | O_NOCTTY |
198
- (symlink_ok ? 0 : O_NOFOLLOW) | O_SEQUENTIAL);
199
- if (strm->fd < 0) {
200
- msg_errno("Can't open %"TS" for reading", strm->name);
201
- free(strm->name);
202
- return -1;
203
- }
204
-
205
- #if O_SEQUENTIAL == 0 && \
206
- (defined(HAVE_POSIX_FADVISE) || \
207
- /* fallback detection method for direct compilation */ \
208
- (!defined(HAVE_CONFIG_H) && defined(POSIX_FADV_SEQUENTIAL)))
209
- (void)posix_fadvise(strm->fd, 0, 0, POSIX_FADV_SEQUENTIAL);
210
- #endif
211
-
212
- return 0;
213
- }
214
-
215
- /* Open a file for writing, or set up standard output for writing */
216
- int
217
- xopen_for_write(const tchar *path, bool overwrite, struct file_stream *strm)
218
- {
219
- int ret = -1;
220
-
221
- strm->mmap_token = NULL;
222
- strm->mmap_mem = NULL;
223
-
224
- if (path == NULL) {
225
- strm->is_standard_stream = true;
226
- strm->name = T("standard output");
227
- strm->fd = STDOUT_FILENO;
228
- #ifdef _WIN32
229
- _setmode(strm->fd, O_BINARY);
230
- #endif
231
- return 0;
232
- }
233
-
234
- strm->is_standard_stream = false;
235
-
236
- strm->name = quote_path(path);
237
- if (strm->name == NULL)
238
- goto err;
239
- retry:
240
- strm->fd = topen(path, O_WRONLY | O_BINARY | O_NOFOLLOW |
241
- O_CREAT | O_EXCL, 0644);
242
- if (strm->fd < 0) {
243
- if (errno != EEXIST) {
244
- msg_errno("Can't open %"TS" for writing", strm->name);
245
- goto err;
246
- }
247
- if (!overwrite) {
248
- if (!isatty(STDERR_FILENO) || !isatty(STDIN_FILENO)) {
249
- warn("%"TS" already exists; use -f to overwrite",
250
- strm->name);
251
- ret = -2; /* warning only */
252
- goto err;
253
- }
254
- fprintf(stderr, "%"TS": %"TS" already exists; "
255
- "overwrite? (y/n) ",
256
- prog_invocation_name, strm->name);
257
- if (getchar() != 'y') {
258
- msg("Not overwriting.");
259
- goto err;
260
- }
261
- }
262
- if (tunlink(path) != 0) {
263
- msg_errno("Unable to delete %"TS, strm->name);
264
- goto err;
265
- }
266
- goto retry;
267
- }
268
-
269
- return 0;
270
-
271
- err:
272
- free(strm->name);
273
- return ret;
274
- }
275
-
276
- /* Read the full contents of a file into memory */
277
- static int
278
- read_full_contents(struct file_stream *strm)
279
- {
280
- size_t filled = 0;
281
- size_t capacity = 4096;
282
- char *buf;
283
- int ret;
284
-
285
- buf = xmalloc(capacity);
286
- if (buf == NULL)
287
- return -1;
288
- do {
289
- if (filled == capacity) {
290
- char *newbuf;
291
-
292
- if (capacity == SIZE_MAX)
293
- goto oom;
294
- capacity += MIN(SIZE_MAX - capacity, capacity);
295
- newbuf = realloc(buf, capacity);
296
- if (newbuf == NULL)
297
- goto oom;
298
- buf = newbuf;
299
- }
300
- ret = xread(strm, &buf[filled], capacity - filled);
301
- if (ret < 0)
302
- goto err;
303
- filled += ret;
304
- } while (ret != 0);
305
-
306
- strm->mmap_mem = buf;
307
- strm->mmap_size = filled;
308
- return 0;
309
-
310
- err:
311
- free(buf);
312
- return ret;
313
- oom:
314
- msg("Out of memory! %"TS" is too large to be processed by "
315
- "this program as currently implemented.", strm->name);
316
- ret = -1;
317
- goto err;
318
- }
319
-
320
- /* Map the contents of a file into memory */
321
- int
322
- map_file_contents(struct file_stream *strm, u64 size)
323
- {
324
- if (size == 0) /* mmap isn't supported on empty files */
325
- return read_full_contents(strm);
326
-
327
- if (size > SIZE_MAX) {
328
- msg("%"TS" is too large to be processed by this program",
329
- strm->name);
330
- return -1;
331
- }
332
- #ifdef _WIN32
333
- strm->mmap_token = CreateFileMapping(
334
- (HANDLE)(intptr_t)_get_osfhandle(strm->fd),
335
- NULL, PAGE_READONLY, 0, 0, NULL);
336
- if (strm->mmap_token == NULL) {
337
- DWORD err = GetLastError();
338
- if (err == ERROR_BAD_EXE_FORMAT) /* mmap unsupported */
339
- return read_full_contents(strm);
340
- msg("Unable create file mapping for %"TS": Windows error %u",
341
- strm->name, (unsigned int)err);
342
- return -1;
343
- }
344
-
345
- strm->mmap_mem = MapViewOfFile((HANDLE)strm->mmap_token,
346
- FILE_MAP_READ, 0, 0, size);
347
- if (strm->mmap_mem == NULL) {
348
- msg("Unable to map %"TS" into memory: Windows error %u",
349
- strm->name, (unsigned int)GetLastError());
350
- CloseHandle((HANDLE)strm->mmap_token);
351
- return -1;
352
- }
353
- #else /* _WIN32 */
354
- strm->mmap_mem = mmap(NULL, size, PROT_READ, MAP_SHARED, strm->fd, 0);
355
- if (strm->mmap_mem == MAP_FAILED) {
356
- strm->mmap_mem = NULL;
357
- if (errno == ENODEV /* standard */ ||
358
- errno == EINVAL /* macOS */) {
359
- /* mmap isn't supported on this file */
360
- return read_full_contents(strm);
361
- }
362
- if (errno == ENOMEM) {
363
- msg("%"TS" is too large to be processed by this "
364
- "program", strm->name);
365
- } else {
366
- msg_errno("Unable to map %"TS" into memory",
367
- strm->name);
368
- }
369
- return -1;
370
- }
371
-
372
- #if defined(HAVE_POSIX_MADVISE) || \
373
- /* fallback detection method for direct compilation */ \
374
- (!defined(HAVE_CONFIG_H) && defined(POSIX_MADV_SEQUENTIAL))
375
- (void)posix_madvise(strm->mmap_mem, size, POSIX_MADV_SEQUENTIAL);
376
- #endif
377
- strm->mmap_token = strm; /* anything that's not NULL */
378
-
379
- #endif /* !_WIN32 */
380
- strm->mmap_size = size;
381
- return 0;
382
- }
383
-
384
- /*
385
- * Read from a file, returning the full count to indicate all bytes were read, a
386
- * short count (possibly 0) to indicate EOF, or -1 to indicate error.
387
- */
388
- ssize_t
389
- xread(struct file_stream *strm, void *buf, size_t count)
390
- {
391
- char *p = buf;
392
- size_t orig_count = count;
393
-
394
- while (count != 0) {
395
- ssize_t res = read(strm->fd, p, MIN(count, INT_MAX));
396
- if (res == 0)
397
- break;
398
- if (res < 0) {
399
- if (errno == EAGAIN || errno == EINTR)
400
- continue;
401
- msg_errno("Error reading from %"TS, strm->name);
402
- return -1;
403
- }
404
- p += res;
405
- count -= res;
406
- }
407
- return orig_count - count;
408
- }
409
-
410
- /* Write to a file, returning 0 if all bytes were written or -1 on error */
411
- int
412
- full_write(struct file_stream *strm, const void *buf, size_t count)
413
- {
414
- const char *p = buf;
415
-
416
- while (count != 0) {
417
- ssize_t res = write(strm->fd, p, MIN(count, INT_MAX));
418
- if (res <= 0) {
419
- msg_errno("Error writing to %"TS, strm->name);
420
- return -1;
421
- }
422
- p += res;
423
- count -= res;
424
- }
425
- return 0;
426
- }
427
-
428
- /* Close a file, returning 0 on success or -1 on error */
429
- int
430
- xclose(struct file_stream *strm)
431
- {
432
- int ret = 0;
433
-
434
- if (!strm->is_standard_stream) {
435
- if (close(strm->fd) != 0) {
436
- msg_errno("Error closing %"TS, strm->name);
437
- ret = -1;
438
- }
439
- free(strm->name);
440
- }
441
-
442
- if (strm->mmap_token != NULL) {
443
- #ifdef _WIN32
444
- UnmapViewOfFile(strm->mmap_mem);
445
- CloseHandle((HANDLE)strm->mmap_token);
446
- #else
447
- munmap(strm->mmap_mem, strm->mmap_size);
448
- #endif
449
- strm->mmap_token = NULL;
450
- } else {
451
- free(strm->mmap_mem);
452
- }
453
- strm->mmap_mem = NULL;
454
- strm->fd = -1;
455
- strm->name = NULL;
456
- return ret;
457
- }
458
-
459
- /*
460
- * Parse the compression level given on the command line, returning the
461
- * compression level on success or -1 on error
462
- */
463
- int
464
- parse_compression_level(tchar opt_char, const tchar *arg)
465
- {
466
- int level;
467
-
468
- if (arg == NULL)
469
- arg = T("");
470
-
471
- if (opt_char < '0' || opt_char > '9')
472
- goto invalid;
473
- level = opt_char - '0';
474
-
475
- if (arg[0] != '\0') {
476
- if (arg[0] < '0' || arg[0] > '9')
477
- goto invalid;
478
- if (arg[1] != '\0') /* Levels are at most 2 digits */
479
- goto invalid;
480
- if (level == 0) /* Don't allow arguments like "-01" */
481
- goto invalid;
482
- level = (level * 10) + (arg[0] - '0');
483
- }
484
-
485
- if (level < 0 || level > 12)
486
- goto invalid;
487
-
488
- return level;
489
-
490
- invalid:
491
- msg("Invalid compression level: \"%"TC"%"TS"\". "
492
- "Must be an integer in the range [0, 12].", opt_char, arg);
493
- return -1;
494
- }
495
-
496
- /* Allocate a new DEFLATE compressor */
497
- struct libdeflate_compressor *
498
- alloc_compressor(int level)
499
- {
500
- struct libdeflate_compressor *c;
501
-
502
- c = libdeflate_alloc_compressor(level);
503
- if (c == NULL) {
504
- msg_errno("Unable to allocate compressor with "
505
- "compression level %d", level);
506
- }
507
- return c;
508
- }
509
-
510
- /* Allocate a new DEFLATE decompressor */
511
- struct libdeflate_decompressor *
512
- alloc_decompressor(void)
513
- {
514
- struct libdeflate_decompressor *d;
515
-
516
- d = libdeflate_alloc_decompressor();
517
- if (d == NULL)
518
- msg_errno("Unable to allocate decompressor");
519
-
520
- return d;
521
- }