Provide zlib's uncompress2 from compat/zlib-compat.c

This will be needed for reading reflog blocks in reftable. Helped-by: Carlo Marcelo Arenas Belón <carenas@gmail.com> Signed-off-by: Han-Wen Nienhuys <hanwen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Han-Wen Nienhuys committed Oct 7, 2021 at 20:25 UTC a322920d0bb8a930c17dd824990f7e5fd026bb3f
6 files changed +122
Makefile
+7
@@ -256,6 +256,8 @@ all::
256 #
257 # Define NO_DEFLATE_BOUND if your zlib does not have deflateBound.
258 #
259 +# Define NO_UNCOMPRESS2 if your zlib does not have uncompress2.
260 +#
261 # Define NO_NORETURN if using buggy versions of gcc 4.6+ and profile feedback,
262 # as the compiler can crash (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49299)
263 #
@@ -1738,6 +1740,11 @@ ifdef NO_DEFLATE_BOUND
1740 BASIC_CFLAGS += -DNO_DEFLATE_BOUND
1741 endif
1742
1743 +ifdef NO_UNCOMPRESS2
1744 + BASIC_CFLAGS += -DNO_UNCOMPRESS2
1745 + REFTABLE_OBJS += compat/zlib-uncompress2.o
1746 +endif
1747 +
1748 ifdef NO_POSIX_GOODIES
1749 BASIC_CFLAGS += -DNO_POSIX_GOODIES
1750 endif
ci/lib.sh
+1
@@ -224,6 +224,7 @@ linux-gcc-default)
224 ;;
225 Linux32)
226 CC=gcc
227 + MAKEFLAGS="$MAKEFLAGS NO_UNCOMPRESS2=1"
228 ;;
229 linux-musl)
230 CC=gcc
compat/.gitattributes new
+1
@@ -0,0 +1 @@
1 +/zlib-uncompress2.c whitespace=-indent-with-non-tab,-trailing-space
compat/zlib-uncompress2.c new
+95
@@ -0,0 +1,95 @@
1 +/* taken from zlib's uncompr.c
2 +
3 + commit cacf7f1d4e3d44d871b605da3b647f07d718623f
4 + Author: Mark Adler <madler@alumni.caltech.edu>
5 + Date: Sun Jan 15 09:18:46 2017 -0800
6 +
7 + zlib 1.2.11
8 +
9 +*/
10 +
11 +#include "../reftable/system.h"
12 +#define z_const
13 +
14 +/*
15 + * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
16 + * For conditions of distribution and use, see copyright notice in zlib.h
17 + */
18 +
19 +#include <zlib.h>
20 +
21 +/* clang-format off */
22 +
23 +/* ===========================================================================
24 + Decompresses the source buffer into the destination buffer. *sourceLen is
25 + the byte length of the source buffer. Upon entry, *destLen is the total size
26 + of the destination buffer, which must be large enough to hold the entire
27 + uncompressed data. (The size of the uncompressed data must have been saved
28 + previously by the compressor and transmitted to the decompressor by some
29 + mechanism outside the scope of this compression library.) Upon exit,
30 + *destLen is the size of the decompressed data and *sourceLen is the number
31 + of source bytes consumed. Upon return, source + *sourceLen points to the
32 + first unused input byte.
33 +
34 + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
35 + memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
36 + Z_DATA_ERROR if the input data was corrupted, including if the input data is
37 + an incomplete zlib stream.
38 +*/
39 +int ZEXPORT uncompress2 (
40 + Bytef *dest,
41 + uLongf *destLen,
42 + const Bytef *source,
43 + uLong *sourceLen) {
44 + z_stream stream;
45 + int err;
46 + const uInt max = (uInt)-1;
47 + uLong len, left;
48 + Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */
49 +
50 + len = *sourceLen;
51 + if (*destLen) {
52 + left = *destLen;
53 + *destLen = 0;
54 + }
55 + else {
56 + left = 1;
57 + dest = buf;
58 + }
59 +
60 + stream.next_in = (z_const Bytef *)source;
61 + stream.avail_in = 0;
62 + stream.zalloc = (alloc_func)0;
63 + stream.zfree = (free_func)0;
64 + stream.opaque = (voidpf)0;
65 +
66 + err = inflateInit(&stream);
67 + if (err != Z_OK) return err;
68 +
69 + stream.next_out = dest;
70 + stream.avail_out = 0;
71 +
72 + do {
73 + if (stream.avail_out == 0) {
74 + stream.avail_out = left > (uLong)max ? max : (uInt)left;
75 + left -= stream.avail_out;
76 + }
77 + if (stream.avail_in == 0) {
78 + stream.avail_in = len > (uLong)max ? max : (uInt)len;
79 + len -= stream.avail_in;
80 + }
81 + err = inflate(&stream, Z_NO_FLUSH);
82 + } while (err == Z_OK);
83 +
84 + *sourceLen -= len + stream.avail_in;
85 + if (dest != buf)
86 + *destLen = stream.total_out;
87 + else if (stream.total_out && err == Z_BUF_ERROR)
88 + left = 1;
89 +
90 + inflateEnd(&stream);
91 + return err == Z_STREAM_END ? Z_OK :
92 + err == Z_NEED_DICT ? Z_DATA_ERROR :
93 + err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
94 + err;
95 +}
config.mak.uname
+5
@@ -258,6 +258,10 @@ ifeq ($(uname_S),FreeBSD)
258 FILENO_IS_A_MACRO = UnfortunatelyYes
259 endif
260 ifeq ($(uname_S),OpenBSD)
261 + # Versions < 7.0 need compatibility layer
262 + ifeq ($(shell expr "$(uname_R)" : "[1-6]\."),2)
263 + NO_UNCOMPRESS2 = UnfortunatelyYes
264 + endif
265 NO_STRCASESTR = YesPlease
266 NO_MEMMEM = YesPlease
267 USE_ST_TIMESPEC = YesPlease
@@ -513,6 +517,7 @@ ifeq ($(uname_S),Interix)
517 endif
518 endif
519 ifeq ($(uname_S),Minix)
520 + NO_UNCOMPRESS2 = YesPlease
521 NO_IPV6 = YesPlease
522 NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
523 NO_NSEC = YesPlease
configure.ac
+13
@@ -672,9 +672,22 @@ AC_LINK_IFELSE([ZLIBTEST_SRC],
672 NO_DEFLATE_BOUND=yes])
673 LIBS="$old_LIBS"
674
675 +AC_DEFUN([ZLIBTEST_UNCOMPRESS2_SRC], [
676 +AC_LANG_PROGRAM([#include <zlib.h>],
677 + [uncompress2(NULL,NULL,NULL,NULL);])])
678 +AC_MSG_CHECKING([for uncompress2 in -lz])
679 +old_LIBS="$LIBS"
680 +LIBS="$LIBS -lz"
681 +AC_LINK_IFELSE([ZLIBTEST_UNCOMPRESS2_SRC],
682 + [AC_MSG_RESULT([yes])],
683 + [AC_MSG_RESULT([no])
684 + NO_UNCOMPRESS2=yes])
685 +LIBS="$old_LIBS"
686 +
687 GIT_UNSTASH_FLAGS($ZLIB_PATH)
688
689 GIT_CONF_SUBST([NO_DEFLATE_BOUND])
690 +GIT_CONF_SUBST([NO_UNCOMPRESS2])
691
692 #
693 # Define NEEDS_SOCKET if linking with libc is not enough (SunOS,