Skip to content

Commit

Permalink
Add a ./config --solo option to make zlib subset with no libary use
Browse files Browse the repository at this point in the history
A common request has been the ability to compile zlib to require no
other libraries.  This --solo option provides that ability.  The price
is that the gz*, compress*, and uncompress functions are eliminated,
and that the user must provide memory allocation and free routines to
deflate and inflate when initializing.
  • Loading branch information
madler committed Oct 7, 2011
1 parent 518ad01 commit f442c1e
Show file tree
Hide file tree
Showing 13 changed files with 537 additions and 195 deletions.
10 changes: 6 additions & 4 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ man3dir = ${mandir}/man3
pkgconfigdir = ${libdir}/pkgconfig
tempfile := $(shell mktemp -u __XXXXXX)

OBJC = adler32.o compress.o crc32.o deflate.o gzclose.o gzlib.o gzread.o \
gzwrite.o infback.o inffast.o inflate.o inftrees.o trees.o uncompr.o zutil.o
OBJZ = adler32.o crc32.o deflate.o infback.o inffast.o inflate.o inftrees.o trees.o zutil.o
OBJG = compress.o uncompr.o gzclose.o gzlib.o gzread.o gzwrite.o
OBJC = $(OBJZ) $(OBJG)

PIC_OBJC = adler32.lo compress.lo crc32.lo deflate.lo gzclose.lo gzlib.lo gzread.lo \
gzwrite.lo infback.lo inffast.lo inflate.lo inftrees.lo trees.lo uncompr.lo zutil.lo
PIC_OBJZ = adler32.lo crc32.lo deflate.lo infback.lo inffast.lo inflate.lo inftrees.lo trees.lo zutil.lo
PIC_OBJG = compress.lo uncompr.lo gzclose.lo gzlib.lo gzread.lo gzwrite.lo
PIC_OBJC = $(PIC_OBJZ) $(PIC_OBJG)

# to use the asm code: make OBJA=match.o, PIC_OBJA=match.lo
OBJA =
Expand Down
16 changes: 16 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ includedir=${includedir-'${prefix}/include'}
mandir=${mandir-'${prefix}/share/man'}
shared_ext='.so'
shared=1
solo=0
zprefix=0
build64=0
gcc=0
old_cc="$CC"
old_cflags="$CFLAGS"
OBJC='$(OBJZ) $(OBJG)'
PIC_OBJC='$(PIC_OBJZ) $(PIC_OBJG)'

while test $# -ge 1
do
Expand All @@ -81,6 +84,7 @@ case "$1" in
-i* | --includedir) includedir="$2"; shift; shift ;;
-s* | --shared | --enable-shared) shared=1; shift ;;
-t | --static) shared=0; shift ;;
--solo) solo=1; shift ;;
-z* | --zprefix) zprefix=1; shift ;;
-6* | --64) build64=1; shift ;;
-a*=* | --archs=*) ARCHS=`echo $1 | sed 's/.*=//'`; shift ;;
Expand Down Expand Up @@ -331,6 +335,16 @@ if test $zprefix -eq 1; then
echo "Using z_ prefix on all symbols."
fi

if test $solo -eq 1; then
sed '/#define ZCONF_H/a\
#define Z_SOLO
' < zconf.h > zconf.temp.h
mv zconf.temp.h zconf.h
OBJC='$(OBJZ)'
PIC_OBJC='$(PIC_OBJZ)'
fi

cat > $test.c <<EOF
#include <stdio.h>
#include <stdarg.h>
Expand Down Expand Up @@ -589,6 +603,8 @@ sed < Makefile.in "
/^sharedlibdir *=/s#=.*#=$sharedlibdir#
/^includedir *=/s#=.*#=$includedir#
/^mandir *=/s#=.*#=$mandir#
/^OBJC *=/s#=.*#= $OBJC#
/^PIC_OBJC *=/s#=.*#= $PIC_OBJC#
/^all: */s#:.*#: $ALL#
/^test: */s#:.*#: $TEST#
" > Makefile
Expand Down
19 changes: 14 additions & 5 deletions deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,19 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,

strm->msg = Z_NULL;
if (strm->zalloc == (alloc_func)0) {
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
strm->zalloc = zcalloc;
strm->opaque = (voidpf)0;
#endif
}
if (strm->zfree == (free_func)0) strm->zfree = zcfree;
if (strm->zfree == (free_func)0)
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
strm->zfree = zcfree;
#endif

#ifdef FASTEST
if (level != 0) level = 1;
Expand Down Expand Up @@ -957,12 +966,12 @@ int ZEXPORT deflateCopy (dest, source)

ss = source->state;

zmemcpy(dest, source, sizeof(z_stream));
zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));

ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
if (ds == Z_NULL) return Z_MEM_ERROR;
dest->state = (struct internal_state FAR *) ds;
zmemcpy(ds, ss, sizeof(deflate_state));
zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
ds->strm = dest;

ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
Expand All @@ -978,8 +987,8 @@ int ZEXPORT deflateCopy (dest, source)
}
/* following zmemcpy do not work for 16-bit MSDOS */
zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);

ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
Expand Down
76 changes: 56 additions & 20 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ const char hello[] = "hello, hello!";
const char dictionary[] = "hello";
uLong dictId; /* Adler32 value of the dictionary */

void test_compress OF((Byte *compr, uLong comprLen,
Byte *uncompr, uLong uncomprLen));
void test_gzio OF((const char *fname,
Byte *uncompr, uLong uncomprLen));
void test_deflate OF((Byte *compr, uLong comprLen));
void test_inflate OF((Byte *compr, uLong comprLen,
Byte *uncompr, uLong uncomprLen));
Expand All @@ -53,6 +49,39 @@ void test_dict_inflate OF((Byte *compr, uLong comprLen,
Byte *uncompr, uLong uncomprLen));
int main OF((int argc, char *argv[]));


#ifdef Z_SOLO

void *myalloc OF((void *, unsigned, unsigned));
void myfree OF((void *, void *));

void *myalloc(q, n, m)
void *q;
unsigned n, m;
{
q = Z_NULL;
return calloc(n, m);
}

void myfree(void *q, void *p)
{
q = Z_NULL;
free(p);
}

static alloc_func zalloc = myalloc;
static free_func zfree = myfree;

#else /* !Z_SOLO */

static alloc_func zalloc = (alloc_func)0;
static free_func zfree = (free_func)0;

void test_compress OF((Byte *compr, uLong comprLen,
Byte *uncompr, uLong uncomprLen));
void test_gzio OF((const char *fname,
Byte *uncompr, uLong uncomprLen));

/* ===========================================================================
* Test compress() and uncompress()
*/
Expand Down Expand Up @@ -163,6 +192,8 @@ void test_gzio(fname, uncompr, uncomprLen)
#endif
}

#endif /* Z_SOLO */

/* ===========================================================================
* Test deflate() with small buffers
*/
Expand All @@ -174,8 +205,8 @@ void test_deflate(compr, comprLen)
int err;
uLong len = (uLong)strlen(hello)+1;

c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (voidpf)0;

err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
Expand Down Expand Up @@ -213,8 +244,8 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen)

strcpy((char*)uncompr, "garbage");

d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.zalloc = zalloc;
d_stream.zfree = zfree;
d_stream.opaque = (voidpf)0;

d_stream.next_in = compr;
Expand Down Expand Up @@ -252,8 +283,8 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
z_stream c_stream; /* compression stream */
int err;

c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (voidpf)0;

err = deflateInit(&c_stream, Z_BEST_SPEED);
Expand Down Expand Up @@ -309,8 +340,8 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen)

strcpy((char*)uncompr, "garbage");

d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.zalloc = zalloc;
d_stream.zfree = zfree;
d_stream.opaque = (voidpf)0;

d_stream.next_in = compr;
Expand Down Expand Up @@ -349,8 +380,8 @@ void test_flush(compr, comprLen)
int err;
uInt len = (uInt)strlen(hello)+1;

c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (voidpf)0;

err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
Expand Down Expand Up @@ -388,8 +419,8 @@ void test_sync(compr, comprLen, uncompr, uncomprLen)

strcpy((char*)uncompr, "garbage");

d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.zalloc = zalloc;
d_stream.zfree = zfree;
d_stream.opaque = (voidpf)0;

d_stream.next_in = compr;
Expand Down Expand Up @@ -430,8 +461,8 @@ void test_dict_deflate(compr, comprLen)
z_stream c_stream; /* compression stream */
int err;

c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (voidpf)0;

err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
Expand Down Expand Up @@ -469,8 +500,8 @@ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)

strcpy((char*)uncompr, "garbage");

d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.zalloc = zalloc;
d_stream.zfree = zfree;
d_stream.opaque = (voidpf)0;

d_stream.next_in = compr;
Expand Down Expand Up @@ -540,10 +571,15 @@ int main(argc, argv)
printf("out of memory\n");
exit(1);
}

#ifdef Z_SOLO
argc = strlen(argv[0]);
#else
test_compress(compr, comprLen, uncompr, uncomprLen);

test_gzio((argc > 1 ? argv[1] : TESTFILE),
uncompr, uncomprLen);
#endif

test_deflate(compr, comprLen);
test_inflate(compr, comprLen, uncompr, uncomprLen);
Expand Down
11 changes: 10 additions & 1 deletion infback.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,19 @@ int stream_size;
return Z_STREAM_ERROR;
strm->msg = Z_NULL; /* in case we return an error */
if (strm->zalloc == (alloc_func)0) {
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
strm->zalloc = zcalloc;
strm->opaque = (voidpf)0;
#endif
}
if (strm->zfree == (free_func)0) strm->zfree = zcfree;
if (strm->zfree == (free_func)0)
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
strm->zfree = zcfree;
#endif
state = (struct inflate_state FAR *)ZALLOC(strm, 1,
sizeof(struct inflate_state));
if (state == Z_NULL) return Z_MEM_ERROR;
Expand Down
15 changes: 12 additions & 3 deletions inflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,19 @@ int stream_size;
if (strm == Z_NULL) return Z_STREAM_ERROR;
strm->msg = Z_NULL; /* in case we return an error */
if (strm->zalloc == (alloc_func)0) {
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
strm->zalloc = zcalloc;
strm->opaque = (voidpf)0;
#endif
}
if (strm->zfree == (free_func)0) strm->zfree = zcfree;
if (strm->zfree == (free_func)0)
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
strm->zfree = zcfree;
#endif
state = (struct inflate_state FAR *)
ZALLOC(strm, 1, sizeof(struct inflate_state));
if (state == Z_NULL) return Z_MEM_ERROR;
Expand Down Expand Up @@ -1433,8 +1442,8 @@ z_streamp source;
}

/* copy state */
zmemcpy(dest, source, sizeof(z_stream));
zmemcpy(copy, state, sizeof(struct inflate_state));
zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
if (state->lencode >= state->codes &&
state->lencode <= state->codes + ENOUGH - 1) {
copy->lencode = copy->codes + (state->lencode - state->codes);
Expand Down
Loading

0 comments on commit f442c1e

Please sign in to comment.