libdeflate 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/test.yml +34 -0
- data/README.md +1 -6
- data/ext/libdeflate/extconf.rb +18 -7
- data/ext/libdeflate/libdeflate_ext.c +17 -17
- data/lib/libdeflate/version.rb +1 -1
- data/libdeflate.gemspec +2 -1
- metadata +13 -84
- data/.gitmodules +0 -3
- data/.travis.yml +0 -5
- data/ext/libdeflate/libdeflate/.gitignore +0 -19
- data/ext/libdeflate/libdeflate/COPYING +0 -21
- data/ext/libdeflate/libdeflate/Makefile +0 -231
- data/ext/libdeflate/libdeflate/Makefile.msc +0 -64
- data/ext/libdeflate/libdeflate/NEWS +0 -57
- data/ext/libdeflate/libdeflate/README.md +0 -170
- data/ext/libdeflate/libdeflate/common/common_defs.h +0 -351
- data/ext/libdeflate/libdeflate/common/compiler_gcc.h +0 -134
- data/ext/libdeflate/libdeflate/common/compiler_msc.h +0 -95
- data/ext/libdeflate/libdeflate/lib/adler32.c +0 -213
- data/ext/libdeflate/libdeflate/lib/adler32_impl.h +0 -281
- data/ext/libdeflate/libdeflate/lib/aligned_malloc.c +0 -57
- data/ext/libdeflate/libdeflate/lib/aligned_malloc.h +0 -13
- data/ext/libdeflate/libdeflate/lib/bt_matchfinder.h +0 -357
- data/ext/libdeflate/libdeflate/lib/crc32.c +0 -368
- data/ext/libdeflate/libdeflate/lib/crc32_impl.h +0 -286
- data/ext/libdeflate/libdeflate/lib/crc32_table.h +0 -526
- data/ext/libdeflate/libdeflate/lib/decompress_impl.h +0 -404
- data/ext/libdeflate/libdeflate/lib/deflate_compress.c +0 -2817
- data/ext/libdeflate/libdeflate/lib/deflate_compress.h +0 -14
- data/ext/libdeflate/libdeflate/lib/deflate_constants.h +0 -66
- data/ext/libdeflate/libdeflate/lib/deflate_decompress.c +0 -889
- data/ext/libdeflate/libdeflate/lib/gzip_compress.c +0 -95
- data/ext/libdeflate/libdeflate/lib/gzip_constants.h +0 -45
- data/ext/libdeflate/libdeflate/lib/gzip_decompress.c +0 -130
- data/ext/libdeflate/libdeflate/lib/hc_matchfinder.h +0 -405
- data/ext/libdeflate/libdeflate/lib/lib_common.h +0 -35
- data/ext/libdeflate/libdeflate/lib/matchfinder_avx2.h +0 -53
- data/ext/libdeflate/libdeflate/lib/matchfinder_common.h +0 -205
- data/ext/libdeflate/libdeflate/lib/matchfinder_neon.h +0 -61
- data/ext/libdeflate/libdeflate/lib/matchfinder_sse2.h +0 -53
- data/ext/libdeflate/libdeflate/lib/unaligned.h +0 -202
- data/ext/libdeflate/libdeflate/lib/x86_cpu_features.c +0 -169
- data/ext/libdeflate/libdeflate/lib/x86_cpu_features.h +0 -48
- data/ext/libdeflate/libdeflate/lib/zlib_compress.c +0 -87
- data/ext/libdeflate/libdeflate/lib/zlib_constants.h +0 -21
- data/ext/libdeflate/libdeflate/lib/zlib_decompress.c +0 -91
- data/ext/libdeflate/libdeflate/libdeflate.h +0 -274
- data/ext/libdeflate/libdeflate/programs/benchmark.c +0 -558
- data/ext/libdeflate/libdeflate/programs/checksum.c +0 -197
- data/ext/libdeflate/libdeflate/programs/detect.sh +0 -62
- data/ext/libdeflate/libdeflate/programs/gzip.c +0 -603
- data/ext/libdeflate/libdeflate/programs/prog_util.c +0 -530
- data/ext/libdeflate/libdeflate/programs/prog_util.h +0 -162
- data/ext/libdeflate/libdeflate/programs/test_checksums.c +0 -135
- data/ext/libdeflate/libdeflate/programs/tgetopt.c +0 -118
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/Makefile +0 -12
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/deflate_compress/fuzz.c +0 -40
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/deflate_compress/inputs/0 +0 -0
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/deflate_decompress/fuzz.c +0 -28
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/deflate_decompress/inputs/0 +0 -3
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/gzip_decompress/fuzz.c +0 -28
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/gzip_decompress/inputs/0 +0 -0
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/prepare_for_fuzz.sh +0 -14
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/zlib_decompress/fuzz.c +0 -28
- data/ext/libdeflate/libdeflate/tools/afl-fuzz/zlib_decompress/inputs/0 +0 -3
- data/ext/libdeflate/libdeflate/tools/android_build.sh +0 -104
- data/ext/libdeflate/libdeflate/tools/checksum_benchmarks.sh +0 -76
- data/ext/libdeflate/libdeflate/tools/exec_tests.sh +0 -30
- data/ext/libdeflate/libdeflate/tools/gen_crc32_multipliers.c +0 -108
- data/ext/libdeflate/libdeflate/tools/gen_crc32_table.c +0 -100
- data/ext/libdeflate/libdeflate/tools/gzip_tests.sh +0 -412
- data/ext/libdeflate/libdeflate/tools/make-windows-releases +0 -21
- data/ext/libdeflate/libdeflate/tools/mips_build.sh +0 -9
- data/ext/libdeflate/libdeflate/tools/msc_test.bat +0 -3
- data/ext/libdeflate/libdeflate/tools/pgo_build.sh +0 -23
- data/ext/libdeflate/libdeflate/tools/produce_gzip_benchmark_table.sh +0 -37
- data/ext/libdeflate/libdeflate/tools/run_tests.sh +0 -305
- data/ext/libdeflate/libdeflate/tools/windows_build.sh +0 -10
@@ -1,603 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* gzip.c - a file compression and decompression program
|
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 <sys/types.h>
|
32
|
-
#include <sys/stat.h>
|
33
|
-
#ifdef _WIN32
|
34
|
-
# include <sys/utime.h>
|
35
|
-
#else
|
36
|
-
# include <sys/time.h>
|
37
|
-
# include <unistd.h>
|
38
|
-
# include <utime.h>
|
39
|
-
#endif
|
40
|
-
|
41
|
-
struct options {
|
42
|
-
bool to_stdout;
|
43
|
-
bool decompress;
|
44
|
-
bool force;
|
45
|
-
bool keep;
|
46
|
-
int compression_level;
|
47
|
-
const tchar *suffix;
|
48
|
-
};
|
49
|
-
|
50
|
-
static const tchar *const optstring = T("1::2::3::4::5::6::7::8::9::cdfhknS:V");
|
51
|
-
|
52
|
-
static void
|
53
|
-
show_usage(FILE *fp)
|
54
|
-
{
|
55
|
-
fprintf(fp,
|
56
|
-
"Usage: %"TS" [-LEVEL] [-cdfhkV] [-S SUF] FILE...\n"
|
57
|
-
"Compress or decompress the specified FILEs.\n"
|
58
|
-
"\n"
|
59
|
-
"Options:\n"
|
60
|
-
" -1 fastest (worst) compression\n"
|
61
|
-
" -6 medium compression (default)\n"
|
62
|
-
" -12 slowest (best) compression\n"
|
63
|
-
" -c write to standard output\n"
|
64
|
-
" -d decompress\n"
|
65
|
-
" -f overwrite existing output files\n"
|
66
|
-
" -h print this help\n"
|
67
|
-
" -k don't delete input files\n"
|
68
|
-
" -S SUF use suffix SUF instead of .gz\n"
|
69
|
-
" -V show version and legal information\n",
|
70
|
-
program_invocation_name);
|
71
|
-
}
|
72
|
-
|
73
|
-
static void
|
74
|
-
show_version(void)
|
75
|
-
{
|
76
|
-
printf(
|
77
|
-
"gzip compression program v" LIBDEFLATE_VERSION_STRING "\n"
|
78
|
-
"Copyright 2016 Eric Biggers\n"
|
79
|
-
"\n"
|
80
|
-
"This program is free software which may be modified and/or redistributed\n"
|
81
|
-
"under the terms of the MIT license. There is NO WARRANTY, to the extent\n"
|
82
|
-
"permitted by law. See the COPYING file for details.\n"
|
83
|
-
);
|
84
|
-
}
|
85
|
-
|
86
|
-
/* Was the program invoked in decompression mode? */
|
87
|
-
static bool
|
88
|
-
is_gunzip(void)
|
89
|
-
{
|
90
|
-
if (tstrxcmp(program_invocation_name, T("gunzip")) == 0)
|
91
|
-
return true;
|
92
|
-
if (tstrxcmp(program_invocation_name, T("libdeflate-gunzip")) == 0)
|
93
|
-
return true;
|
94
|
-
#ifdef _WIN32
|
95
|
-
if (tstrxcmp(program_invocation_name, T("gunzip.exe")) == 0)
|
96
|
-
return true;
|
97
|
-
if (tstrxcmp(program_invocation_name, T("libdeflate-gunzip.exe")) == 0)
|
98
|
-
return true;
|
99
|
-
#endif
|
100
|
-
return false;
|
101
|
-
}
|
102
|
-
|
103
|
-
static const tchar *
|
104
|
-
get_suffix(const tchar *path, const tchar *suffix)
|
105
|
-
{
|
106
|
-
size_t path_len = tstrlen(path);
|
107
|
-
size_t suffix_len = tstrlen(suffix);
|
108
|
-
const tchar *p;
|
109
|
-
|
110
|
-
if (path_len <= suffix_len)
|
111
|
-
return NULL;
|
112
|
-
p = &path[path_len - suffix_len];
|
113
|
-
if (tstrxcmp(p, suffix) == 0)
|
114
|
-
return p;
|
115
|
-
return NULL;
|
116
|
-
}
|
117
|
-
|
118
|
-
static bool
|
119
|
-
has_suffix(const tchar *path, const tchar *suffix)
|
120
|
-
{
|
121
|
-
return get_suffix(path, suffix) != NULL;
|
122
|
-
}
|
123
|
-
|
124
|
-
static tchar *
|
125
|
-
append_suffix(const tchar *path, const tchar *suffix)
|
126
|
-
{
|
127
|
-
size_t path_len = tstrlen(path);
|
128
|
-
size_t suffix_len = tstrlen(suffix);
|
129
|
-
tchar *suffixed_path;
|
130
|
-
|
131
|
-
suffixed_path = xmalloc((path_len + suffix_len + 1) * sizeof(tchar));
|
132
|
-
if (suffixed_path == NULL)
|
133
|
-
return NULL;
|
134
|
-
tmemcpy(suffixed_path, path, path_len);
|
135
|
-
tmemcpy(&suffixed_path[path_len], suffix, suffix_len + 1);
|
136
|
-
return suffixed_path;
|
137
|
-
}
|
138
|
-
|
139
|
-
static int
|
140
|
-
do_compress(struct libdeflate_compressor *compressor,
|
141
|
-
struct file_stream *in, struct file_stream *out)
|
142
|
-
{
|
143
|
-
const void *uncompressed_data = in->mmap_mem;
|
144
|
-
size_t uncompressed_size = in->mmap_size;
|
145
|
-
void *compressed_data;
|
146
|
-
size_t actual_compressed_size;
|
147
|
-
size_t max_compressed_size;
|
148
|
-
int ret;
|
149
|
-
|
150
|
-
max_compressed_size = libdeflate_gzip_compress_bound(compressor,
|
151
|
-
uncompressed_size);
|
152
|
-
compressed_data = xmalloc(max_compressed_size);
|
153
|
-
if (compressed_data == NULL) {
|
154
|
-
msg("%"TS": file is probably too large to be processed by this "
|
155
|
-
"program", in->name);
|
156
|
-
ret = -1;
|
157
|
-
goto out;
|
158
|
-
}
|
159
|
-
|
160
|
-
actual_compressed_size = libdeflate_gzip_compress(compressor,
|
161
|
-
uncompressed_data,
|
162
|
-
uncompressed_size,
|
163
|
-
compressed_data,
|
164
|
-
max_compressed_size);
|
165
|
-
if (actual_compressed_size == 0) {
|
166
|
-
msg("Bug in libdeflate_gzip_compress_bound()!");
|
167
|
-
ret = -1;
|
168
|
-
goto out;
|
169
|
-
}
|
170
|
-
|
171
|
-
ret = full_write(out, compressed_data, actual_compressed_size);
|
172
|
-
out:
|
173
|
-
free(compressed_data);
|
174
|
-
return ret;
|
175
|
-
}
|
176
|
-
|
177
|
-
static u32
|
178
|
-
load_u32_gzip(const u8 *p)
|
179
|
-
{
|
180
|
-
return ((u32)p[0] << 0) | ((u32)p[1] << 8) |
|
181
|
-
((u32)p[2] << 16) | ((u32)p[3] << 24);
|
182
|
-
}
|
183
|
-
|
184
|
-
static int
|
185
|
-
do_decompress(struct libdeflate_decompressor *decompressor,
|
186
|
-
struct file_stream *in, struct file_stream *out)
|
187
|
-
{
|
188
|
-
const u8 *compressed_data = in->mmap_mem;
|
189
|
-
size_t compressed_size = in->mmap_size;
|
190
|
-
void *uncompressed_data = NULL;
|
191
|
-
size_t uncompressed_size;
|
192
|
-
enum libdeflate_result result;
|
193
|
-
int ret;
|
194
|
-
|
195
|
-
if (compressed_size < sizeof(u32)) {
|
196
|
-
msg("%"TS": not in gzip format", in->name);
|
197
|
-
ret = -1;
|
198
|
-
goto out;
|
199
|
-
}
|
200
|
-
|
201
|
-
uncompressed_size = load_u32_gzip(&compressed_data[compressed_size - 4]);
|
202
|
-
|
203
|
-
uncompressed_data = xmalloc(uncompressed_size);
|
204
|
-
if (uncompressed_data == NULL) {
|
205
|
-
msg("%"TS": file is probably too large to be processed by this "
|
206
|
-
"program", in->name);
|
207
|
-
ret = -1;
|
208
|
-
goto out;
|
209
|
-
}
|
210
|
-
|
211
|
-
result = libdeflate_gzip_decompress(decompressor,
|
212
|
-
compressed_data,
|
213
|
-
compressed_size,
|
214
|
-
uncompressed_data,
|
215
|
-
uncompressed_size, NULL);
|
216
|
-
|
217
|
-
if (result == LIBDEFLATE_INSUFFICIENT_SPACE) {
|
218
|
-
msg("%"TS": file corrupt or too large to be processed by this "
|
219
|
-
"program", in->name);
|
220
|
-
ret = -1;
|
221
|
-
goto out;
|
222
|
-
}
|
223
|
-
|
224
|
-
if (result != LIBDEFLATE_SUCCESS) {
|
225
|
-
msg("%"TS": file corrupt or not in gzip format", in->name);
|
226
|
-
ret = -1;
|
227
|
-
goto out;
|
228
|
-
}
|
229
|
-
|
230
|
-
ret = full_write(out, uncompressed_data, uncompressed_size);
|
231
|
-
out:
|
232
|
-
free(uncompressed_data);
|
233
|
-
return ret;
|
234
|
-
}
|
235
|
-
|
236
|
-
static int
|
237
|
-
stat_file(struct file_stream *in, stat_t *stbuf, bool allow_hard_links)
|
238
|
-
{
|
239
|
-
if (tfstat(in->fd, stbuf) != 0) {
|
240
|
-
msg("%"TS": unable to stat file", in->name);
|
241
|
-
return -1;
|
242
|
-
}
|
243
|
-
|
244
|
-
if (!S_ISREG(stbuf->st_mode) && !in->is_standard_stream) {
|
245
|
-
msg("%"TS" is %s -- skipping",
|
246
|
-
in->name, S_ISDIR(stbuf->st_mode) ? "a directory" :
|
247
|
-
"not a regular file");
|
248
|
-
return -2;
|
249
|
-
}
|
250
|
-
|
251
|
-
if (stbuf->st_nlink > 1 && !allow_hard_links) {
|
252
|
-
msg("%"TS" has multiple hard links -- skipping "
|
253
|
-
"(use -f to process anyway)", in->name);
|
254
|
-
return -2;
|
255
|
-
}
|
256
|
-
|
257
|
-
return 0;
|
258
|
-
}
|
259
|
-
|
260
|
-
static void
|
261
|
-
restore_mode(struct file_stream *out, const stat_t *stbuf)
|
262
|
-
{
|
263
|
-
#ifndef _WIN32
|
264
|
-
if (fchmod(out->fd, stbuf->st_mode) != 0)
|
265
|
-
msg_errno("%"TS": unable to preserve mode", out->name);
|
266
|
-
#endif
|
267
|
-
}
|
268
|
-
|
269
|
-
static void
|
270
|
-
restore_owner_and_group(struct file_stream *out, const stat_t *stbuf)
|
271
|
-
{
|
272
|
-
#ifndef _WIN32
|
273
|
-
if (fchown(out->fd, stbuf->st_uid, stbuf->st_gid) != 0) {
|
274
|
-
msg_errno("%"TS": unable to preserve owner and group",
|
275
|
-
out->name);
|
276
|
-
}
|
277
|
-
#endif
|
278
|
-
}
|
279
|
-
|
280
|
-
static void
|
281
|
-
restore_timestamps(struct file_stream *out, const tchar *newpath,
|
282
|
-
const stat_t *stbuf)
|
283
|
-
{
|
284
|
-
int ret;
|
285
|
-
#if defined(HAVE_FUTIMENS) && defined(HAVE_STAT_NANOSECOND_PRECISION)
|
286
|
-
struct timespec times[2] = {
|
287
|
-
stbuf->st_atim, stbuf->st_mtim,
|
288
|
-
};
|
289
|
-
ret = futimens(out->fd, times);
|
290
|
-
#elif defined(HAVE_FUTIMES) && defined(HAVE_STAT_NANOSECOND_PRECISION)
|
291
|
-
struct timeval times[2] = {
|
292
|
-
{ stbuf->st_atim.tv_sec, stbuf->st_atim.tv_nsec / 1000, },
|
293
|
-
{ stbuf->st_mtim.tv_sec, stbuf->st_mtim.tv_nsec / 1000, },
|
294
|
-
};
|
295
|
-
ret = futimes(out->fd, times);
|
296
|
-
#else
|
297
|
-
struct tutimbuf times = {
|
298
|
-
stbuf->st_atime, stbuf->st_mtime,
|
299
|
-
};
|
300
|
-
ret = tutime(newpath, ×);
|
301
|
-
#endif
|
302
|
-
if (ret != 0)
|
303
|
-
msg_errno("%"TS": unable to preserve timestamps", out->name);
|
304
|
-
}
|
305
|
-
|
306
|
-
static void
|
307
|
-
restore_metadata(struct file_stream *out, const tchar *newpath,
|
308
|
-
const stat_t *stbuf)
|
309
|
-
{
|
310
|
-
restore_mode(out, stbuf);
|
311
|
-
restore_owner_and_group(out, stbuf);
|
312
|
-
restore_timestamps(out, newpath, stbuf);
|
313
|
-
}
|
314
|
-
|
315
|
-
static int
|
316
|
-
decompress_file(struct libdeflate_decompressor *decompressor, const tchar *path,
|
317
|
-
const struct options *options)
|
318
|
-
{
|
319
|
-
tchar *oldpath = (tchar *)path;
|
320
|
-
tchar *newpath = NULL;
|
321
|
-
struct file_stream in;
|
322
|
-
struct file_stream out;
|
323
|
-
stat_t stbuf;
|
324
|
-
int ret;
|
325
|
-
int ret2;
|
326
|
-
|
327
|
-
if (path != NULL) {
|
328
|
-
const tchar *suffix = get_suffix(path, options->suffix);
|
329
|
-
if (suffix == NULL) {
|
330
|
-
/*
|
331
|
-
* Input file is unsuffixed. If the file doesn't exist,
|
332
|
-
* then try it suffixed. Otherwise, if we're not
|
333
|
-
* writing to stdout, skip the file with warning status.
|
334
|
-
* Otherwise, go ahead and try to open the file anyway
|
335
|
-
* (which will very likely fail).
|
336
|
-
*/
|
337
|
-
if (tstat(path, &stbuf) != 0 && errno == ENOENT) {
|
338
|
-
oldpath = append_suffix(path, options->suffix);
|
339
|
-
if (oldpath == NULL)
|
340
|
-
return -1;
|
341
|
-
if (!options->to_stdout)
|
342
|
-
newpath = (tchar *)path;
|
343
|
-
} else if (!options->to_stdout) {
|
344
|
-
msg("\"%"TS"\" does not end with the %"TS" "
|
345
|
-
"suffix -- skipping",
|
346
|
-
path, options->suffix);
|
347
|
-
return -2;
|
348
|
-
}
|
349
|
-
} else if (!options->to_stdout) {
|
350
|
-
/*
|
351
|
-
* Input file is suffixed, and we're not writing to
|
352
|
-
* stdout. Strip the suffix to get the path to the
|
353
|
-
* output file.
|
354
|
-
*/
|
355
|
-
newpath = xmalloc((suffix - oldpath + 1) *
|
356
|
-
sizeof(tchar));
|
357
|
-
if (newpath == NULL)
|
358
|
-
return -1;
|
359
|
-
tmemcpy(newpath, oldpath, suffix - oldpath);
|
360
|
-
newpath[suffix - oldpath] = '\0';
|
361
|
-
}
|
362
|
-
}
|
363
|
-
|
364
|
-
ret = xopen_for_read(oldpath, options->force || options->to_stdout,
|
365
|
-
&in);
|
366
|
-
if (ret != 0)
|
367
|
-
goto out_free_paths;
|
368
|
-
|
369
|
-
if (!options->force && isatty(in.fd)) {
|
370
|
-
msg("Refusing to read compressed data from terminal. "
|
371
|
-
"Use -f to override.\nFor help, use -h.");
|
372
|
-
ret = -1;
|
373
|
-
goto out_close_in;
|
374
|
-
}
|
375
|
-
|
376
|
-
ret = stat_file(&in, &stbuf, options->force || options->keep ||
|
377
|
-
oldpath == NULL || newpath == NULL);
|
378
|
-
if (ret != 0)
|
379
|
-
goto out_close_in;
|
380
|
-
|
381
|
-
ret = xopen_for_write(newpath, options->force, &out);
|
382
|
-
if (ret != 0)
|
383
|
-
goto out_close_in;
|
384
|
-
|
385
|
-
/* TODO: need a streaming-friendly solution */
|
386
|
-
ret = map_file_contents(&in, stbuf.st_size);
|
387
|
-
if (ret != 0)
|
388
|
-
goto out_close_out;
|
389
|
-
|
390
|
-
ret = do_decompress(decompressor, &in, &out);
|
391
|
-
if (ret != 0)
|
392
|
-
goto out_close_out;
|
393
|
-
|
394
|
-
if (oldpath != NULL && newpath != NULL)
|
395
|
-
restore_metadata(&out, newpath, &stbuf);
|
396
|
-
ret = 0;
|
397
|
-
out_close_out:
|
398
|
-
ret2 = xclose(&out);
|
399
|
-
if (ret == 0)
|
400
|
-
ret = ret2;
|
401
|
-
if (ret != 0 && newpath != NULL)
|
402
|
-
tunlink(newpath);
|
403
|
-
out_close_in:
|
404
|
-
xclose(&in);
|
405
|
-
if (ret == 0 && oldpath != NULL && newpath != NULL && !options->keep)
|
406
|
-
tunlink(oldpath);
|
407
|
-
out_free_paths:
|
408
|
-
if (newpath != path)
|
409
|
-
free(newpath);
|
410
|
-
if (oldpath != path)
|
411
|
-
free(oldpath);
|
412
|
-
return ret;
|
413
|
-
}
|
414
|
-
|
415
|
-
static int
|
416
|
-
compress_file(struct libdeflate_compressor *compressor, const tchar *path,
|
417
|
-
const struct options *options)
|
418
|
-
{
|
419
|
-
tchar *newpath = NULL;
|
420
|
-
struct file_stream in;
|
421
|
-
struct file_stream out;
|
422
|
-
stat_t stbuf;
|
423
|
-
int ret;
|
424
|
-
int ret2;
|
425
|
-
|
426
|
-
if (path != NULL && !options->to_stdout) {
|
427
|
-
if (!options->force && has_suffix(path, options->suffix)) {
|
428
|
-
msg("%"TS": already has %"TS" suffix -- skipping",
|
429
|
-
path, options->suffix);
|
430
|
-
return 0;
|
431
|
-
}
|
432
|
-
newpath = append_suffix(path, options->suffix);
|
433
|
-
if (newpath == NULL)
|
434
|
-
return -1;
|
435
|
-
}
|
436
|
-
|
437
|
-
ret = xopen_for_read(path, options->force || options->to_stdout, &in);
|
438
|
-
if (ret != 0)
|
439
|
-
goto out_free_newpath;
|
440
|
-
|
441
|
-
ret = stat_file(&in, &stbuf, options->force || options->keep ||
|
442
|
-
path == NULL || newpath == NULL);
|
443
|
-
if (ret != 0)
|
444
|
-
goto out_close_in;
|
445
|
-
|
446
|
-
ret = xopen_for_write(newpath, options->force, &out);
|
447
|
-
if (ret != 0)
|
448
|
-
goto out_close_in;
|
449
|
-
|
450
|
-
if (!options->force && isatty(out.fd)) {
|
451
|
-
msg("Refusing to write compressed data to terminal. "
|
452
|
-
"Use -f to override.\nFor help, use -h.");
|
453
|
-
ret = -1;
|
454
|
-
goto out_close_out;
|
455
|
-
}
|
456
|
-
|
457
|
-
/* TODO: need a streaming-friendly solution */
|
458
|
-
ret = map_file_contents(&in, stbuf.st_size);
|
459
|
-
if (ret != 0)
|
460
|
-
goto out_close_out;
|
461
|
-
|
462
|
-
ret = do_compress(compressor, &in, &out);
|
463
|
-
if (ret != 0)
|
464
|
-
goto out_close_out;
|
465
|
-
|
466
|
-
if (path != NULL && newpath != NULL)
|
467
|
-
restore_metadata(&out, newpath, &stbuf);
|
468
|
-
ret = 0;
|
469
|
-
out_close_out:
|
470
|
-
ret2 = xclose(&out);
|
471
|
-
if (ret == 0)
|
472
|
-
ret = ret2;
|
473
|
-
if (ret != 0 && newpath != NULL)
|
474
|
-
tunlink(newpath);
|
475
|
-
out_close_in:
|
476
|
-
xclose(&in);
|
477
|
-
if (ret == 0 && path != NULL && newpath != NULL && !options->keep)
|
478
|
-
tunlink(path);
|
479
|
-
out_free_newpath:
|
480
|
-
free(newpath);
|
481
|
-
return ret;
|
482
|
-
}
|
483
|
-
|
484
|
-
int
|
485
|
-
tmain(int argc, tchar *argv[])
|
486
|
-
{
|
487
|
-
tchar *default_file_list[] = { NULL };
|
488
|
-
struct options options;
|
489
|
-
int opt_char;
|
490
|
-
int i;
|
491
|
-
int ret;
|
492
|
-
|
493
|
-
program_invocation_name = get_filename(argv[0]);
|
494
|
-
|
495
|
-
options.to_stdout = false;
|
496
|
-
options.decompress = is_gunzip();
|
497
|
-
options.force = false;
|
498
|
-
options.keep = false;
|
499
|
-
options.compression_level = 6;
|
500
|
-
options.suffix = T(".gz");
|
501
|
-
|
502
|
-
while ((opt_char = tgetopt(argc, argv, optstring)) != -1) {
|
503
|
-
switch (opt_char) {
|
504
|
-
case '1':
|
505
|
-
case '2':
|
506
|
-
case '3':
|
507
|
-
case '4':
|
508
|
-
case '5':
|
509
|
-
case '6':
|
510
|
-
case '7':
|
511
|
-
case '8':
|
512
|
-
case '9':
|
513
|
-
options.compression_level =
|
514
|
-
parse_compression_level(opt_char, toptarg);
|
515
|
-
if (options.compression_level == 0)
|
516
|
-
return 1;
|
517
|
-
break;
|
518
|
-
case 'c':
|
519
|
-
options.to_stdout = true;
|
520
|
-
break;
|
521
|
-
case 'd':
|
522
|
-
options.decompress = true;
|
523
|
-
break;
|
524
|
-
case 'f':
|
525
|
-
options.force = true;
|
526
|
-
break;
|
527
|
-
case 'h':
|
528
|
-
show_usage(stdout);
|
529
|
-
return 0;
|
530
|
-
case 'k':
|
531
|
-
options.keep = true;
|
532
|
-
break;
|
533
|
-
case 'n':
|
534
|
-
/*
|
535
|
-
* -n means don't save or restore the original filename
|
536
|
-
* in the gzip header. Currently this implementation
|
537
|
-
* already behaves this way by default, so accept the
|
538
|
-
* option as a no-op.
|
539
|
-
*/
|
540
|
-
break;
|
541
|
-
case 'S':
|
542
|
-
options.suffix = toptarg;
|
543
|
-
if (options.suffix[0] == T('\0')) {
|
544
|
-
msg("invalid suffix");
|
545
|
-
return 1;
|
546
|
-
}
|
547
|
-
break;
|
548
|
-
case 'V':
|
549
|
-
show_version();
|
550
|
-
return 0;
|
551
|
-
default:
|
552
|
-
show_usage(stderr);
|
553
|
-
return 1;
|
554
|
-
}
|
555
|
-
}
|
556
|
-
|
557
|
-
argv += toptind;
|
558
|
-
argc -= toptind;
|
559
|
-
|
560
|
-
if (argc == 0) {
|
561
|
-
argv = default_file_list;
|
562
|
-
argc = ARRAY_LEN(default_file_list);
|
563
|
-
} else {
|
564
|
-
for (i = 0; i < argc; i++)
|
565
|
-
if (argv[i][0] == '-' && argv[i][1] == '\0')
|
566
|
-
argv[i] = NULL;
|
567
|
-
}
|
568
|
-
|
569
|
-
ret = 0;
|
570
|
-
if (options.decompress) {
|
571
|
-
struct libdeflate_decompressor *d;
|
572
|
-
|
573
|
-
d = alloc_decompressor();
|
574
|
-
if (d == NULL)
|
575
|
-
return 1;
|
576
|
-
|
577
|
-
for (i = 0; i < argc; i++)
|
578
|
-
ret |= -decompress_file(d, argv[i], &options);
|
579
|
-
|
580
|
-
libdeflate_free_decompressor(d);
|
581
|
-
} else {
|
582
|
-
struct libdeflate_compressor *c;
|
583
|
-
|
584
|
-
c = alloc_compressor(options.compression_level);
|
585
|
-
if (c == NULL)
|
586
|
-
return 1;
|
587
|
-
|
588
|
-
for (i = 0; i < argc; i++)
|
589
|
-
ret |= -compress_file(c, argv[i], &options);
|
590
|
-
|
591
|
-
libdeflate_free_compressor(c);
|
592
|
-
}
|
593
|
-
|
594
|
-
/*
|
595
|
-
* If ret=0, there were no warnings or errors. Exit with status 0.
|
596
|
-
* If ret=2, there was at least one warning. Exit with status 2.
|
597
|
-
* Else, there was at least one error. Exit with status 1.
|
598
|
-
*/
|
599
|
-
if (ret != 0 && ret != 2)
|
600
|
-
ret = 1;
|
601
|
-
|
602
|
-
return ret;
|
603
|
-
}
|