diff --git a/lib/libz/Makefile b/lib/libz/Makefile index 81526179e2d0..ef3ec038f6f5 100644 --- a/lib/libz/Makefile +++ b/lib/libz/Makefile @@ -1,80 +1,81 @@ # # $FreeBSD$ # PACKAGE=lib${LIB} LIB= z SHLIBDIR?= /lib SHLIB_MAJOR= 6 MAN= zlib.3 zopen.3 +MLINKS+= zopen.3 zdopen.3 ZLIBSRC= ${SRCTOP}/sys/contrib/zlib .PATH: ${ZLIBSRC} #CFLAGS=-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7 #CFLAGS=-g -DDEBUG #CFLAGS=-O3 -Wall -Wwrite-strings -Wpointer-arith -Wconversion \ # -Wstrict-prototypes -Wmissing-prototypes CFLAGS+= -DHAS_snprintf -DHAS_vsnprintf -I${.CURDIR} WARNS?= 3 CLEANFILES+= example.o example foo.gz minigzip.o minigzip SRCS+= adler32.c SRCS+= compress.c SRCS+= crc32.c SRCS+= deflate.c SRCS+= gzclose.c SRCS+= gzlib.c SRCS+= gzread.c SRCS+= gzwrite.c SRCS+= infback.c SRCS+= inffast.c SRCS+= inflate.c SRCS+= inftrees.c SRCS+= trees.c SRCS+= uncompr.c SRCS+= zopen.c SRCS+= zutil.c .if ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "amd64" CFLAGS+= -DUNALIGNED_OK .endif VERSION_DEF= ${.CURDIR}/Versions.def SYMBOL_MAPS= ${.CURDIR}/Symbol.map CFLAGS+= -DSYMBOL_VERSIONING INCS= zconf.h zlib.h .PATH: ${ZLIBSRC}/test ZLIB_VERSION!= sed -n '/define.*ZLIB_VERSION/{s,[^0-9.],,gp;q;}' ${ZLIBSRC}/zlib.h zlib.pc: zlib.pc.in sed -e 's,@prefix@,/usr,g ; \ s,@exec_prefix@,$${prefix},g ; \ s,@libdir@,$${exec_prefix}/lib,g ; \ s,@sharedlibdir@,$${libdir},g ; \ s,@includedir@,$${prefix}/include,g ; \ s,@VERSION@,${ZLIB_VERSION},g ;' \ ${.ALLSRC} > ${.TARGET} minigzip: all minigzip.o $(CC) -o minigzip minigzip.o -L. -lz example: all example.o $(CC) -o example example.o -L. -lz test: example minigzip (export LD_LIBRARY_PATH=. ; ./example ) (export LD_LIBRARY_PATH=. ; \ echo hello world | ./minigzip | ./minigzip -d ) FILES= zlib.pc FILESDIR= ${LIBDATADIR}/pkgconfig .include diff --git a/lib/libz/Symbol.map b/lib/libz/Symbol.map index 92b02b56d0eb..0a91537272c1 100644 --- a/lib/libz/Symbol.map +++ b/lib/libz/Symbol.map @@ -1,119 +1,123 @@ /* * $FreeBSD$ */ ZLIB_1.2.9 { inflateCodesUsed; inflateValidate; uncompress2; gzfread; gzfwrite; deflateGetDictionary; adler32_z; crc32_z; }; ZLIB_1.2.7.1 { inflateGetDictionary; gzvprintf; }; ZLIB_1.2.7.0 { deflatePending; deflateResetKeep; gzgetc_; inflateResetKeep; }; ZLIB_1.2.4.0 { adler32; adler32_combine; adler32_combine64; compress; compress2; compressBound; crc32; crc32_combine; crc32_combine64; deflate; deflateBound; deflateCopy; deflateEnd; deflateInit2_; deflateInit_; deflateParams; deflatePrime; deflateReset; deflateSetDictionary; deflateSetHeader; deflateTune; get_crc_table; gzbuffer; gzclearerr; gzclose; gzclose_r; gzclose_w; gzdirect; gzdopen; gzeof; gzerror; gzflush; gzgetc; gzgets; gzoffset; gzoffset64; gzopen; gzopen64; gzprintf; gzputc; gzputs; gzread; gzrewind; gzseek; gzseek64; gzsetparams; gztell; gztell64; gzungetc; gzwrite; inflate; inflateBack; inflateBackEnd; inflateBackInit_; inflateCopy; inflateEnd; inflateGetHeader; inflateInit2_; inflateInit_; inflateMark; inflatePrime; inflateReset; inflateReset2; inflateSetDictionary; inflateSync; inflateSyncPoint; inflateUndermine; uncompress; zError; zlibCompileFlags; zlibVersion; }; FBSD_1.2 { zopen; }; +FBSD_1.6 { + zdopen; +}; + ZLIBprivate_1.0 { _tr_align; _tr_flush_block; _tr_init; _tr_stored_block; _tr_tally; gz_error; inflate_fast; inflate_table; longest_match; match_init; zcalloc; zcfree; }; diff --git a/lib/libz/Versions.def b/lib/libz/Versions.def index d5f1816b2be8..9a67602185b8 100644 --- a/lib/libz/Versions.def +++ b/lib/libz/Versions.def @@ -1,20 +1,23 @@ # $FreeBSD$ ZLIB_1.2.4.0 { }; ZLIB_1.2.7.0 { } ZLIB_1.2.4.0; ZLIB_1.2.7.1 { } ZLIB_1.2.7.0; ZLIB_1.2.9 { } ZLIB_1.2.7.1; FBSD_1.2 { } ZLIB_1.2.4.0; +FBSD_1.6 { +} FBSD_1.2; + ZLIBprivate_1.0 { } ZLIB_1.2.4.0; diff --git a/lib/libz/zopen.3 b/lib/libz/zopen.3 index b56fb447a345..7677f300a8ae 100644 --- a/lib/libz/zopen.3 +++ b/lib/libz/zopen.3 @@ -1,95 +1,108 @@ .\" Copyright (c) 2014 Xin LI .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD$ .\" -.Dd March 5, 2014 +.Dd December 6, 2018 .Dt ZOPEN 3 .Os .Sh NAME .Nm zopen .Nd open a gzip compressed stream .Sh LIBRARY .Lb libz .Sh SYNOPSIS .Ft FILE * .Fn zopen "const char *path" "const char *mode" +.Ft FILE * +.Fn zdopen "int fd" "const char *mode" .Sh DESCRIPTION The .Fn zopen -opens a gzip file whose name is the string pointed to by +function opens a gzip file whose name is the string pointed to by .Fa path -and associates a stream with it. -It is a wrapper around +and returns a stream which can be used to access the uncompressed contents +of the file. +The +.Fn zdopen +variant takes a gzip file referenced by the file descriptor +.Fa fd , +analogous to +.Xr fdopen 3 . +They are wrappers around .Xr zlib 3 -and standard stream I/O APIs. +and the standard stream I/O APIs. .Pp The argument .Fa mode -have the same meaning as it does in +has the same meaning as it does in .Xr fopen 3 . .Pp The -.Nm -function will associate read, write, seek and close +.Fn zopen +and +.Fn zdopen +functions will associate the read, write, seek and close functions of .Xr zlib 3 -after successfully opened a file with -.Xr funopen 3 -so that they will be used to read or write the new stream. +with the returned stream. .Sh RETURN VALUES Upon successful completion -.Nm -returns a +.Fn zopen +and +.Fn zdopen +return a .Tn FILE pointer. Otherwise, .Dv NULL is returned and the global variable .Va errno is set to indicate the error. .Sh ERRORS In addition to the errors documented for -.Xr fopen 3 , -the -.Nm -function may also fail for: +.Xr fopen 3 +and +.Xr fdopen 3 , +the functions may also fail for: .Bl -tag -width Er .It Bq Er ENOMEM Insufficient memory is available. .El .Sh COMPATIBILITY -This implementation of -.Nm +The implementation of +.Fn zopen function first appeared in .Nx 1.6 and .Fx 4.5 . -The -.Nm -function may not be portable to systems other than +.Fn zdopen +first appeared in +.Fx 13.0 . +These functions may not be portable to systems other than .Fx . .Sh SEE ALSO +.Xr fdopen 3 , .Xr fopen 3 , .Xr funopen 3 , .Xr zlib 3 diff --git a/lib/libz/zopen.c b/lib/libz/zopen.c index cde022dcf0e6..a6ac99166128 100644 --- a/lib/libz/zopen.c +++ b/lib/libz/zopen.c @@ -1,49 +1,65 @@ /* * Public domain stdio wrapper for libz, written by Johan Danielsson. */ #include __FBSDID("$FreeBSD$"); #include #include FILE *zopen(const char *fname, const char *mode); +FILE *zdopen(int fd, const char *mode); /* convert arguments */ static int xgzread(void *cookie, char *data, int size) { return gzread(cookie, data, size); } static int xgzwrite(void *cookie, const char *data, int size) { return gzwrite(cookie, (void*)data, size); } static int xgzclose(void *cookie) { return gzclose(cookie); } static fpos_t xgzseek(void *cookie, fpos_t offset, int whence) { return gzseek(cookie, (z_off_t)offset, whence); } FILE * zopen(const char *fname, const char *mode) { gzFile gz = gzopen(fname, mode); if(gz == NULL) return NULL; if(*mode == 'r') return (funopen(gz, xgzread, NULL, xgzseek, xgzclose)); else return (funopen(gz, NULL, xgzwrite, xgzseek, xgzclose)); } + +FILE * +zdopen(int fd, const char *mode) +{ + gzFile gz; + + gz = gzdopen(fd, mode); + if (gz == NULL) + return (NULL); + + if (*mode == 'r') + return (funopen(gz, xgzread, NULL, xgzseek, xgzclose)); + else + return (funopen(gz, NULL, xgzwrite, xgzseek, xgzclose)); +}