|
|
840239 |
From c9de6c53dcf3eb1071c7999c8accc43ef2b3f458 Mon Sep 17 00:00:00 2001
|
|
|
840239 |
From: Mark Adler <madler@alumni.caltech.edu>
|
|
|
840239 |
Date: Sun, 24 Jan 2021 22:00:00 -0800
|
|
|
840239 |
Subject: [PATCH] Portability improvements.
|
|
|
840239 |
|
|
|
840239 |
Avoid many bogus warnings across versions of gcc. Work around
|
|
|
840239 |
missing definitions on some systems. Fix some printf format types.
|
|
|
840239 |
---
|
|
|
840239 |
pigz.c | 77 ++++++++++++++++++++-----------------
|
|
|
840239 |
try.h | 6 +--
|
|
|
840239 |
zopfli/src/zopfli/cache.c | 2 +-
|
|
|
840239 |
zopfli/src/zopfli/deflate.c | 13 ++++---
|
|
|
840239 |
4 files changed, 53 insertions(+), 45 deletions(-)
|
|
|
840239 |
|
|
|
840239 |
diff --git a/pigz.c b/pigz.c
|
|
|
840239 |
index 7430e1e..6ec3a82 100644
|
|
|
840239 |
--- a/pigz.c
|
|
|
840239 |
+++ b/pigz.c
|
|
|
840239 |
@@ -333,7 +333,7 @@
|
|
|
840239 |
// Portability defines.
|
|
|
840239 |
#define _FILE_OFFSET_BITS 64 // Use large file functions
|
|
|
840239 |
#define _LARGE_FILES // Same thing for AIX
|
|
|
840239 |
-#define _POSIX_C_SOURCE 200809L // For MinGW
|
|
|
840239 |
+#define _XOPEN_SOURCE 700 // For POSIX 2008
|
|
|
840239 |
|
|
|
840239 |
// Included headers and what is expected from each.
|
|
|
840239 |
#include <stdio.h> // fflush(), fprintf(), fputs(), getchar(), putc(),
|
|
|
840239 |
@@ -874,10 +874,10 @@ local void log_dump(void) {
|
|
|
840239 |
;
|
|
|
840239 |
log_free();
|
|
|
840239 |
if (mem_track.num || mem_track.size)
|
|
|
840239 |
- complain("memory leak: %lu allocs of %lu bytes total",
|
|
|
840239 |
+ complain("memory leak: %zu allocs of %zu bytes total",
|
|
|
840239 |
mem_track.num, mem_track.size);
|
|
|
840239 |
if (mem_track.max)
|
|
|
840239 |
- fprintf(stderr, "%lu bytes of memory used in %lu allocs\n",
|
|
|
840239 |
+ fprintf(stderr, "%zu bytes of memory used in %zu allocs\n",
|
|
|
840239 |
mem_track.max, mem_track.tot);
|
|
|
840239 |
}
|
|
|
840239 |
|
|
|
840239 |
@@ -993,7 +993,7 @@ local size_t writen(int desc, void const *buf, size_t len) {
|
|
|
840239 |
size_t left = len;
|
|
|
840239 |
|
|
|
840239 |
while (left) {
|
|
|
840239 |
- size_t const max = SIZE_MAX >> 1; // max ssize_t
|
|
|
840239 |
+ size_t const max = SSIZE_MAX;
|
|
|
840239 |
ssize_t ret = write(desc, next, left > max ? max : left);
|
|
|
840239 |
if (ret < 1)
|
|
|
840239 |
throw(errno, "write error on %s (%s)", g.outf, strerror(errno));
|
|
|
840239 |
@@ -1668,26 +1668,32 @@ local void compress_thread(void *dummy) {
|
|
|
840239 |
size_t len; // remaining bytes to compress/check
|
|
|
840239 |
#if ZLIB_VERNUM >= 0x1260
|
|
|
840239 |
int bits; // deflate pending bits
|
|
|
840239 |
-#endif
|
|
|
840239 |
-#ifndef NOZOPFLI
|
|
|
840239 |
- struct space *temp = NULL; // temporary space for zopfli input
|
|
|
840239 |
#endif
|
|
|
840239 |
int ret; // zlib return code
|
|
|
840239 |
- z_stream strm; // deflate stream
|
|
|
840239 |
ball_t err; // error information from throw()
|
|
|
840239 |
|
|
|
840239 |
(void)dummy;
|
|
|
840239 |
|
|
|
840239 |
try {
|
|
|
840239 |
- // initialize the deflate stream for this thread
|
|
|
840239 |
- strm.zfree = ZFREE;
|
|
|
840239 |
- strm.zalloc = ZALLOC;
|
|
|
840239 |
- strm.opaque = OPAQUE;
|
|
|
840239 |
- ret = deflateInit2(&strm, 6, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
|
|
|
840239 |
- if (ret == Z_MEM_ERROR)
|
|
|
840239 |
- throw(ENOMEM, "not enough memory");
|
|
|
840239 |
- if (ret != Z_OK)
|
|
|
840239 |
- throw(EINVAL, "internal error");
|
|
|
840239 |
+ z_stream strm; // deflate stream
|
|
|
840239 |
+#ifndef NOZOPFLI
|
|
|
840239 |
+ struct space *temp = NULL;
|
|
|
840239 |
+ // get temporary space for zopfli input
|
|
|
840239 |
+ if (g.level > 9)
|
|
|
840239 |
+ temp = get_space(&out_pool);
|
|
|
840239 |
+ else
|
|
|
840239 |
+#endif
|
|
|
840239 |
+ {
|
|
|
840239 |
+ // initialize the deflate stream for this thread
|
|
|
840239 |
+ strm.zfree = ZFREE;
|
|
|
840239 |
+ strm.zalloc = ZALLOC;
|
|
|
840239 |
+ strm.opaque = OPAQUE;
|
|
|
840239 |
+ ret = deflateInit2(&strm, 6, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
|
|
|
840239 |
+ if (ret == Z_MEM_ERROR)
|
|
|
840239 |
+ throw(ENOMEM, "not enough memory");
|
|
|
840239 |
+ if (ret != Z_OK)
|
|
|
840239 |
+ throw(EINVAL, "internal error");
|
|
|
840239 |
+ }
|
|
|
840239 |
|
|
|
840239 |
// keep looking for work
|
|
|
840239 |
for (;;) {
|
|
|
840239 |
@@ -1714,11 +1720,8 @@ local void compress_thread(void *dummy) {
|
|
|
840239 |
(void)deflateParams(&strm, g.level, Z_DEFAULT_STRATEGY);
|
|
|
840239 |
#ifndef NOZOPFLI
|
|
|
840239 |
}
|
|
|
840239 |
- else {
|
|
|
840239 |
- if (temp == NULL)
|
|
|
840239 |
- temp = get_space(&out_pool);
|
|
|
840239 |
+ else
|
|
|
840239 |
temp->len = 0;
|
|
|
840239 |
- }
|
|
|
840239 |
#endif
|
|
|
840239 |
|
|
|
840239 |
// set dictionary if provided, release that input or dictionary
|
|
|
840239 |
@@ -1912,11 +1915,15 @@ local void compress_thread(void *dummy) {
|
|
|
840239 |
}
|
|
|
840239 |
|
|
|
840239 |
// found job with seq == -1 -- return to join
|
|
|
840239 |
+ release(compress_have);
|
|
|
840239 |
#ifndef NOZOPFLI
|
|
|
840239 |
- drop_space(temp);
|
|
|
840239 |
+ if (g.level > 9)
|
|
|
840239 |
+ drop_space(temp);
|
|
|
840239 |
+ else
|
|
|
840239 |
#endif
|
|
|
840239 |
- release(compress_have);
|
|
|
840239 |
- (void)deflateEnd(&strm;;
|
|
|
840239 |
+ {
|
|
|
840239 |
+ (void)deflateEnd(&strm;;
|
|
|
840239 |
+ }
|
|
|
840239 |
}
|
|
|
840239 |
catch (err) {
|
|
|
840239 |
THREADABORT(err);
|
|
|
840239 |
@@ -3078,7 +3085,7 @@ local void show_info(int method, unsigned long check, length_t len, int cont) {
|
|
|
840239 |
strncpy(tag, "<...>", max + 1);
|
|
|
840239 |
else if (g.hname == NULL) {
|
|
|
840239 |
n = strlen(g.inf) - compressed_suffix(g.inf);
|
|
|
840239 |
- strncpy(tag, g.inf, n > max + 1 ? max + 1 : n);
|
|
|
840239 |
+ memcpy(tag, g.inf, n > max + 1 ? max + 1 : n);
|
|
|
840239 |
if (strcmp(g.inf + n, ".tgz") == 0 && n < max + 1)
|
|
|
840239 |
strncpy(tag + n, ".tar", max + 1 - n);
|
|
|
840239 |
}
|
|
|
840239 |
@@ -3802,37 +3809,33 @@ local char *justname(char *path) {
|
|
|
840239 |
return p == NULL ? path : p + 1;
|
|
|
840239 |
}
|
|
|
840239 |
|
|
|
840239 |
-#pragma GCC diagnostic push
|
|
|
840239 |
-#pragma GCC diagnostic ignored "-Wunused-result"
|
|
|
840239 |
-
|
|
|
840239 |
// Copy file attributes, from -> to, as best we can. This is best effort, so no
|
|
|
840239 |
// errors are reported. The mode bits, including suid, sgid, and the sticky bit
|
|
|
840239 |
// are copied (if allowed), the owner's user id and group id are copied (again
|
|
|
840239 |
// if allowed), and the access and modify times are copied.
|
|
|
840239 |
-local void copymeta(char *from, char *to) {
|
|
|
840239 |
+local int copymeta(char *from, char *to) {
|
|
|
840239 |
struct stat st;
|
|
|
840239 |
struct timeval times[2];
|
|
|
840239 |
|
|
|
840239 |
// get all of from's Unix meta data, return if not a regular file
|
|
|
840239 |
if (stat(from, &st) != 0 || (st.st_mode & S_IFMT) != S_IFREG)
|
|
|
840239 |
- return;
|
|
|
840239 |
+ return -4;
|
|
|
840239 |
|
|
|
840239 |
// set to's mode bits, ignore errors
|
|
|
840239 |
- (void)chmod(to, st.st_mode & 07777);
|
|
|
840239 |
+ int ret = chmod(to, st.st_mode & 07777);
|
|
|
840239 |
|
|
|
840239 |
// copy owner's user and group, ignore errors
|
|
|
840239 |
- (void)chown(to, st.st_uid, st.st_gid);
|
|
|
840239 |
+ ret += chown(to, st.st_uid, st.st_gid);
|
|
|
840239 |
|
|
|
840239 |
// copy access and modify times, ignore errors
|
|
|
840239 |
times[0].tv_sec = st.st_atime;
|
|
|
840239 |
times[0].tv_usec = 0;
|
|
|
840239 |
times[1].tv_sec = st.st_mtime;
|
|
|
840239 |
times[1].tv_usec = 0;
|
|
|
840239 |
- (void)utimes(to, times);
|
|
|
840239 |
+ ret += utimes(to, times);
|
|
|
840239 |
+ return ret;
|
|
|
840239 |
}
|
|
|
840239 |
|
|
|
840239 |
-#pragma GCC diagnostic pop
|
|
|
840239 |
-
|
|
|
840239 |
// Set the access and modify times of fd to t.
|
|
|
840239 |
local void touch(char *path, time_t t) {
|
|
|
840239 |
struct timeval times[2];
|
|
|
840239 |
@@ -4430,6 +4433,7 @@ local int option(char *arg) {
|
|
|
840239 |
puts("Subject to the terms of the zlib license.");
|
|
|
840239 |
puts("No warranty is provided or implied.");
|
|
|
840239 |
exit(0);
|
|
|
840239 |
+ break; // avoid warning
|
|
|
840239 |
case 'M': g.headis |= 0xa; break;
|
|
|
840239 |
case 'N': g.headis = 0xf; break;
|
|
|
840239 |
#ifndef NOZOPFLI
|
|
|
840239 |
@@ -4443,13 +4447,16 @@ local int option(char *arg) {
|
|
|
840239 |
if (g.verbosity > 1)
|
|
|
840239 |
printf("zlib %s\n", zlibVersion());
|
|
|
840239 |
exit(0);
|
|
|
840239 |
+ break; // avoid warning
|
|
|
840239 |
case 'Y': g.sync = 1; break;
|
|
|
840239 |
case 'Z':
|
|
|
840239 |
throw(EINVAL, "invalid option: LZW output not supported: %s",
|
|
|
840239 |
bad);
|
|
|
840239 |
+ break; // avoid warning
|
|
|
840239 |
case 'a':
|
|
|
840239 |
throw(EINVAL, "invalid option: no ascii conversion: %s",
|
|
|
840239 |
bad);
|
|
|
840239 |
+ break; // avoid warning
|
|
|
840239 |
case 'b': get = 1; break;
|
|
|
840239 |
case 'c': g.pipeout = 1; break;
|
|
|
840239 |
case 'd': if (!g.decode) g.headis >>= 2; g.decode = 1; break;
|
|
|
840239 |
diff --git a/try.h b/try.h
|
|
|
840239 |
index 03289dd..3009f7d 100644
|
|
|
840239 |
--- a/try.h
|
|
|
840239 |
+++ b/try.h
|
|
|
840239 |
@@ -304,8 +304,8 @@ struct try_s_ {
|
|
|
840239 |
# define try_stack_ ((try_t_ *)pthread_getspecific(try_key_))
|
|
|
840239 |
# define try_stack_set_(next) \
|
|
|
840239 |
do { \
|
|
|
840239 |
- int try_ret_ = pthread_setspecific(try_key_, next); \
|
|
|
840239 |
- assert(try_ret_ == 0 && "try: pthread_setspecific() failed"); \
|
|
|
840239 |
+ assert(pthread_setspecific(try_key_, next) == 0 && \
|
|
|
840239 |
+ "try: pthread_setspecific() failed"); \
|
|
|
840239 |
} while (0)
|
|
|
840239 |
#else /* !PTHREAD_ONCE_INIT */
|
|
|
840239 |
extern try_t_ *try_stack_;
|
|
|
840239 |
@@ -320,7 +320,7 @@ struct try_s_ {
|
|
|
840239 |
#define TRY_TRY_ \
|
|
|
840239 |
do { \
|
|
|
840239 |
try_t_ try_this_; \
|
|
|
840239 |
- int try_pushed_ = 1; \
|
|
|
840239 |
+ volatile int try_pushed_ = 1; \
|
|
|
840239 |
try_this_.ball.code = 0; \
|
|
|
840239 |
try_this_.ball.free = 0; \
|
|
|
840239 |
try_this_.ball.why = NULL; \
|
|
|
840239 |
diff --git a/zopfli/src/zopfli/cache.c b/zopfli/src/zopfli/cache.c
|
|
|
840239 |
index f5559c3..e5934df 100644
|
|
|
840239 |
--- a/zopfli/src/zopfli/cache.c
|
|
|
840239 |
+++ b/zopfli/src/zopfli/cache.c
|
|
|
840239 |
@@ -33,7 +33,7 @@ void ZopfliInitCache(size_t blocksize, ZopfliLongestMatchCache* lmc) {
|
|
|
840239 |
lmc->sublen = (unsigned char*)malloc(ZOPFLI_CACHE_LENGTH * 3 * blocksize);
|
|
|
840239 |
if(lmc->sublen == NULL) {
|
|
|
840239 |
fprintf(stderr,
|
|
|
840239 |
- "Error: Out of memory. Tried allocating %lu bytes of memory.\n",
|
|
|
840239 |
+ "Error: Out of memory. Tried allocating %zu bytes of memory.\n",
|
|
|
840239 |
ZOPFLI_CACHE_LENGTH * 3 * blocksize);
|
|
|
840239 |
exit (EXIT_FAILURE);
|
|
|
840239 |
}
|
|
|
840239 |
diff --git a/zopfli/src/zopfli/deflate.c b/zopfli/src/zopfli/deflate.c
|
|
|
840239 |
index abe7360..f7b62e4 100644
|
|
|
840239 |
--- a/zopfli/src/zopfli/deflate.c
|
|
|
840239 |
+++ b/zopfli/src/zopfli/deflate.c
|
|
|
840239 |
@@ -431,22 +431,23 @@ Changes the population counts in a way that the consequent Huffman tree
|
|
|
840239 |
compression, especially its rle-part, will be more likely to compress this data
|
|
|
840239 |
more efficiently. length contains the size of the histogram.
|
|
|
840239 |
*/
|
|
|
840239 |
-void OptimizeHuffmanForRle(int length, size_t* counts) {
|
|
|
840239 |
- int i, k, stride;
|
|
|
840239 |
+void OptimizeHuffmanForRle(unsigned length, size_t* counts) {
|
|
|
840239 |
+ unsigned i;
|
|
|
840239 |
+ int k, stride;
|
|
|
840239 |
size_t symbol, sum, limit;
|
|
|
840239 |
int* good_for_rle;
|
|
|
840239 |
|
|
|
840239 |
/* 1) We don't want to touch the trailing zeros. We may break the
|
|
|
840239 |
rules of the format by adding more data in the distance codes. */
|
|
|
840239 |
- for (; length >= 0; --length) {
|
|
|
840239 |
- if (length == 0) {
|
|
|
840239 |
- return;
|
|
|
840239 |
- }
|
|
|
840239 |
+ for (; length > 0; --length) {
|
|
|
840239 |
if (counts[length - 1] != 0) {
|
|
|
840239 |
/* Now counts[0..length - 1] does not have trailing zeros. */
|
|
|
840239 |
break;
|
|
|
840239 |
}
|
|
|
840239 |
}
|
|
|
840239 |
+ if (length == 0) {
|
|
|
840239 |
+ return;
|
|
|
840239 |
+ }
|
|
|
840239 |
/* 2) Let's mark all population counts that already can be encoded
|
|
|
840239 |
with an rle code.*/
|
|
|
840239 |
good_for_rle = (int*)malloc(length * sizeof(int));
|