Skip to content

Commit a322920

Browse files
hanwengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Han-Wen Nienhuys <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e303bf2 commit a322920

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ all::
256256
#
257257
# Define NO_DEFLATE_BOUND if your zlib does not have deflateBound.
258258
#
259+
# Define NO_UNCOMPRESS2 if your zlib does not have uncompress2.
260+
#
259261
# Define NO_NORETURN if using buggy versions of gcc 4.6+ and profile feedback,
260262
# as the compiler can crash (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49299)
261263
#
@@ -1738,6 +1740,11 @@ ifdef NO_DEFLATE_BOUND
17381740
BASIC_CFLAGS += -DNO_DEFLATE_BOUND
17391741
endif
17401742

1743+
ifdef NO_UNCOMPRESS2
1744+
BASIC_CFLAGS += -DNO_UNCOMPRESS2
1745+
REFTABLE_OBJS += compat/zlib-uncompress2.o
1746+
endif
1747+
17411748
ifdef NO_POSIX_GOODIES
17421749
BASIC_CFLAGS += -DNO_POSIX_GOODIES
17431750
endif

ci/lib.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ linux-gcc-default)
224224
;;
225225
Linux32)
226226
CC=gcc
227+
MAKEFLAGS="$MAKEFLAGS NO_UNCOMPRESS2=1"
227228
;;
228229
linux-musl)
229230
CC=gcc

compat/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/zlib-uncompress2.c whitespace=-indent-with-non-tab,-trailing-space

compat/zlib-uncompress2.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* taken from zlib's uncompr.c
2+
3+
commit cacf7f1d4e3d44d871b605da3b647f07d718623f
4+
Author: Mark Adler <[email protected]>
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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ ifeq ($(uname_S),FreeBSD)
258258
FILENO_IS_A_MACRO = UnfortunatelyYes
259259
endif
260260
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
261265
NO_STRCASESTR = YesPlease
262266
NO_MEMMEM = YesPlease
263267
USE_ST_TIMESPEC = YesPlease
@@ -513,6 +517,7 @@ ifeq ($(uname_S),Interix)
513517
endif
514518
endif
515519
ifeq ($(uname_S),Minix)
520+
NO_UNCOMPRESS2 = YesPlease
516521
NO_IPV6 = YesPlease
517522
NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
518523
NO_NSEC = YesPlease

configure.ac

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,22 @@ AC_LINK_IFELSE([ZLIBTEST_SRC],
672672
NO_DEFLATE_BOUND=yes])
673673
LIBS="$old_LIBS"
674674

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+
675687
GIT_UNSTASH_FLAGS($ZLIB_PATH)
676688

677689
GIT_CONF_SUBST([NO_DEFLATE_BOUND])
690+
GIT_CONF_SUBST([NO_UNCOMPRESS2])
678691

679692
#
680693
# Define NEEDS_SOCKET if linking with libc is not enough (SunOS,

0 commit comments

Comments
 (0)