mirror of https://github.com/madler/zlib.git
parent
b97ec631c6
commit
a2506218cd
31 changed files with 1530 additions and 119 deletions
@ -0,0 +1,132 @@ |
||||
STRPGMEXP PGMLVL(*CURRENT) SIGNATURE('ZLIB') |
||||
|
||||
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ |
||||
/* Version 1.1.3 entry points. */ |
||||
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE ADLER32 ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("adler32") |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE COMPRESS ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("compress") |
||||
EXPORT SYMBOL("compress2") |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE CRC32 ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("crc32") |
||||
EXPORT SYMBOL("get_crc_table") |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE DEFLATE ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("deflate") |
||||
EXPORT SYMBOL("deflateEnd") |
||||
EXPORT SYMBOL("deflateSetDictionary") |
||||
EXPORT SYMBOL("deflateCopy") |
||||
EXPORT SYMBOL("deflateReset") |
||||
EXPORT SYMBOL("deflateParams") |
||||
EXPORT SYMBOL("deflatePrime") |
||||
EXPORT SYMBOL("deflateInit_") |
||||
EXPORT SYMBOL("deflateInit2_") |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE GZIO ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("gzopen") |
||||
EXPORT SYMBOL("gzdopen") |
||||
EXPORT SYMBOL("gzsetparams") |
||||
EXPORT SYMBOL("gzread") |
||||
EXPORT SYMBOL("gzwrite") |
||||
EXPORT SYMBOL("gzprintf") |
||||
EXPORT SYMBOL("gzputs") |
||||
EXPORT SYMBOL("gzgets") |
||||
EXPORT SYMBOL("gzputc") |
||||
EXPORT SYMBOL("gzgetc") |
||||
EXPORT SYMBOL("gzflush") |
||||
EXPORT SYMBOL("gzseek") |
||||
EXPORT SYMBOL("gzrewind") |
||||
EXPORT SYMBOL("gztell") |
||||
EXPORT SYMBOL("gzeof") |
||||
EXPORT SYMBOL("gzclose") |
||||
EXPORT SYMBOL("gzerror") |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE INFLATE ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("inflate") |
||||
EXPORT SYMBOL("inflateEnd") |
||||
EXPORT SYMBOL("inflateSetDictionary") |
||||
EXPORT SYMBOL("inflateSync") |
||||
EXPORT SYMBOL("inflateReset") |
||||
EXPORT SYMBOL("inflateInit_") |
||||
EXPORT SYMBOL("inflateInit2_") |
||||
EXPORT SYMBOL("inflateSyncPoint") |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE UNCOMPR ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("uncompress") |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE ZUTIL ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("zlibVersion") |
||||
EXPORT SYMBOL("zError") |
||||
|
||||
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ |
||||
/* Version 1.2.0.7 additional entry points. */ |
||||
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE COMPRESS ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("compressBound") |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE DEFLATE ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("deflateBound") |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE GZIO ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("gzungetc") |
||||
EXPORT SYMBOL("gzclearerr") |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE INFBACK ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("inflateBack") |
||||
EXPORT SYMBOL("inflateBackEnd") |
||||
EXPORT SYMBOL("inflateBackInit_") |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE INFLATE ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("inflateCopy") |
||||
|
||||
/********************************************************************/ |
||||
/* *MODULE ZUTIL ZLIB 01/02/01 00:15:09 */ |
||||
/********************************************************************/ |
||||
|
||||
EXPORT SYMBOL("zlibCompileFlags") |
||||
|
||||
ENDPGMEXP |
@ -0,0 +1,123 @@ |
||||
/******************************************************************************/ |
||||
/* */ |
||||
/* ZLIB */ |
||||
/* */ |
||||
/* Compile sources into modules and link them into a service program. */ |
||||
/* */ |
||||
/******************************************************************************/ |
||||
|
||||
PGM |
||||
|
||||
/* Configuration adjustable parameters. */ |
||||
|
||||
DCL VAR(&SRCLIB) TYPE(*CHAR) LEN(10) + |
||||
VALUE('ZLIB') /* Source library. */ |
||||
DCL VAR(&SRCFILE) TYPE(*CHAR) LEN(10) + |
||||
VALUE('SOURCES') /* Source member file. */ |
||||
DCL VAR(&CTLFILE) TYPE(*CHAR) LEN(10) + |
||||
VALUE('TOOLS') /* Control member file. */ |
||||
|
||||
DCL VAR(&MODLIB) TYPE(*CHAR) LEN(10) + |
||||
VALUE('ZLIB') /* Module library. */ |
||||
|
||||
DCL VAR(&SRVLIB) TYPE(*CHAR) LEN(10) + |
||||
VALUE('LGPL') /* Service program library. */ |
||||
|
||||
DCL VAR(&CFLAGS) TYPE(*CHAR) + |
||||
VALUE('OPTIMIZE(40)') /* Compile options. */ |
||||
|
||||
|
||||
/* Working storage. */ |
||||
|
||||
DCL VAR(&CMDLEN) TYPE(*DEC) LEN(15 5) VALUE(300) /* Command length. */ |
||||
DCL VAR(&CMD) TYPE(*CHAR) LEN(512) |
||||
|
||||
|
||||
/* Compile sources into modules. */ |
||||
|
||||
CHGVAR VAR(&CMD) VALUE('CRTCMOD MODULE(' *TCAT &MODLIB *TCAT + |
||||
'/ADLER32) SRCFILE(' *TCAT + |
||||
&SRCLIB *TCAT '/' *TCAT &SRCFILE *TCAT + |
||||
') SYSIFCOPT(*IFSIO)' *BCAT &CFLAGS) |
||||
CALL PGM(QCMDEXC) PARM(&CMD &CMDLEN) |
||||
|
||||
CHGVAR VAR(&CMD) VALUE('CRTCMOD MODULE(' *TCAT &MODLIB *TCAT + |
||||
'/COMPRESS) SRCFILE(' *TCAT + |
||||
&SRCLIB *TCAT '/' *TCAT &SRCFILE *TCAT + |
||||
') SYSIFCOPT(*IFSIO)' *BCAT &CFLAGS) |
||||
CALL PGM(QCMDEXC) PARM(&CMD &CMDLEN) |
||||
|
||||
CHGVAR VAR(&CMD) VALUE('CRTCMOD MODULE(' *TCAT &MODLIB *TCAT + |
||||
'/CRC32) SRCFILE(' *TCAT + |
||||
&SRCLIB *TCAT '/' *TCAT &SRCFILE *TCAT + |
||||
') SYSIFCOPT(*IFSIO)' *BCAT &CFLAGS) |
||||
CALL PGM(QCMDEXC) PARM(&CMD &CMDLEN) |
||||
|
||||
CHGVAR VAR(&CMD) VALUE('CRTCMOD MODULE(' *TCAT &MODLIB *TCAT + |
||||
'/DEFLATE) SRCFILE(' *TCAT + |
||||
&SRCLIB *TCAT '/' *TCAT &SRCFILE *TCAT + |
||||
') SYSIFCOPT(*IFSIO)' *BCAT &CFLAGS) |
||||
CALL PGM(QCMDEXC) PARM(&CMD &CMDLEN) |
||||
|
||||
CHGVAR VAR(&CMD) VALUE('CRTCMOD MODULE(' *TCAT &MODLIB *TCAT + |
||||
'/GZIO) SRCFILE(' *TCAT + |
||||
&SRCLIB *TCAT '/' *TCAT &SRCFILE *TCAT + |
||||
') SYSIFCOPT(*IFSIO)' *BCAT &CFLAGS) |
||||
CALL PGM(QCMDEXC) PARM(&CMD &CMDLEN) |
||||
|
||||
CHGVAR VAR(&CMD) VALUE('CRTCMOD MODULE(' *TCAT &MODLIB *TCAT + |
||||
'/INFBACK) SRCFILE(' *TCAT + |
||||
&SRCLIB *TCAT '/' *TCAT &SRCFILE *TCAT + |
||||
') SYSIFCOPT(*IFSIO)' *BCAT &CFLAGS) |
||||
CALL PGM(QCMDEXC) PARM(&CMD &CMDLEN) |
||||
|
||||
CHGVAR VAR(&CMD) VALUE('CRTCMOD MODULE(' *TCAT &MODLIB *TCAT + |
||||
'/INFFAST) SRCFILE(' *TCAT + |
||||
&SRCLIB *TCAT '/' *TCAT &SRCFILE *TCAT + |
||||
') SYSIFCOPT(*IFSIO)' *BCAT &CFLAGS) |
||||
CALL PGM(QCMDEXC) PARM(&CMD &CMDLEN) |
||||
|
||||
CHGVAR VAR(&CMD) VALUE('CRTCMOD MODULE(' *TCAT &MODLIB *TCAT + |
||||
'/INFLATE) SRCFILE(' *TCAT + |
||||
&SRCLIB *TCAT '/' *TCAT &SRCFILE *TCAT + |
||||
') SYSIFCOPT(*IFSIO)' *BCAT &CFLAGS) |
||||
CALL PGM(QCMDEXC) PARM(&CMD &CMDLEN) |
||||
|
||||
CHGVAR VAR(&CMD) VALUE('CRTCMOD MODULE(' *TCAT &MODLIB *TCAT + |
||||
'/INFTREES) SRCFILE(' *TCAT + |
||||
&SRCLIB *TCAT '/' *TCAT &SRCFILE *TCAT + |
||||
') SYSIFCOPT(*IFSIO)' *BCAT &CFLAGS) |
||||
CALL PGM(QCMDEXC) PARM(&CMD &CMDLEN) |
||||
|
||||
CHGVAR VAR(&CMD) VALUE('CRTCMOD MODULE(' *TCAT &MODLIB *TCAT + |
||||
'/TREES) SRCFILE(' *TCAT + |
||||
&SRCLIB *TCAT '/' *TCAT &SRCFILE *TCAT + |
||||
') SYSIFCOPT(*IFSIO)' *BCAT &CFLAGS) |
||||
CALL PGM(QCMDEXC) PARM(&CMD &CMDLEN) |
||||
|
||||
CHGVAR VAR(&CMD) VALUE('CRTCMOD MODULE(' *TCAT &MODLIB *TCAT + |
||||
'/UNCOMPR) SRCFILE(' *TCAT + |
||||
&SRCLIB *TCAT '/' *TCAT &SRCFILE *TCAT + |
||||
') SYSIFCOPT(*IFSIO)' *BCAT &CFLAGS) |
||||
CALL PGM(QCMDEXC) PARM(&CMD &CMDLEN) |
||||
|
||||
CHGVAR VAR(&CMD) VALUE('CRTCMOD MODULE(' *TCAT &MODLIB *TCAT + |
||||
'/ZUTIL) SRCFILE(' *TCAT + |
||||
&SRCLIB *TCAT '/' *TCAT &SRCFILE *TCAT + |
||||
') SYSIFCOPT(*IFSIO)' *BCAT &CFLAGS) |
||||
CALL PGM(QCMDEXC) PARM(&CMD &CMDLEN) |
||||
|
||||
|
||||
/* Link modules into a service program. */ |
||||
|
||||
CRTSRVPGM SRVPGM(&SRVLIB/ZLIB) + |
||||
MODULE(&MODLIB/ADLER32 &MODLIB/COMPRESS + |
||||
&MODLIB/CRC32 &MODLIB/DEFLATE + |
||||
&MODLIB/GZIO &MODLIB/INFBACK + |
||||
&MODLIB/INFFAST &MODLIB/INFLATE + |
||||
&MODLIB/INFTREES &MODLIB/TREES + |
||||
&MODLIB/UNCOMPR &MODLIB/ZUTIL) + |
||||
SRCFILE(&SRCLIB/&CTLFILE) SRCMBR(BNDSRC) + |
||||
TEXT('ZLIB 1.2.0.7') TGTRLS(V4R4M0) |
||||
|
||||
ENDPGM |
@ -0,0 +1,111 @@ |
||||
ZLIB version 1.2.0.7 for AS400 installation instructions |
||||
|
||||
I) From an AS400 *SAVF file: |
||||
|
||||
1) Unpacking archive to an AS400 save file |
||||
|
||||
On the AS400: |
||||
|
||||
_ Create the ZLIB AS400 library: |
||||
|
||||
CRTLIB LIB(ZLIB) TYPE(PROD) TEXT('ZLIB compression API library') |
||||
|
||||
_ Create a work save file, for example: |
||||
|
||||
CRTSAVF FILE(ZLIB/ZLIBSAVF) |
||||
|
||||
On a PC connected to the target AS400: |
||||
|
||||
_ Unpack the save file image to a PC file "ZLIBSAVF" |
||||
_ Upload this file into the save file on the AS400, for example |
||||
using ftp in BINARY mode. |
||||
|
||||
|
||||
2) Populating the ZLIB AS400 source library |
||||
|
||||
On the AS400: |
||||
|
||||
_ Extract the saved objects into the ZLIB AS400 library using: |
||||
|
||||
RSTOBJ OBJ(*ALL) SAVLIB(ZLIB) DEV(*SAVF) SAVF(ZLIB/ZLIBSAVF) RSTLIB(ZLIB) |
||||
|
||||
|
||||
3) Customize installation: |
||||
|
||||
_ Edit CL member ZLIB/TOOLS(COMPILE) and change parameters if needed, |
||||
according to the comments. |
||||
|
||||
_ Compile this member with: |
||||
|
||||
CRTCLPGM PGM(ZLIB/COMPILE) SRCFILE(ZLIB/TOOLS) SRCMBR(COMPILE) |
||||
|
||||
|
||||
4) Compile and generate the service program: |
||||
|
||||
_ This can now be done by executing: |
||||
|
||||
CALL PGM(ZLIB/COMPILE) |
||||
|
||||
|
||||
|
||||
II) From the original source distribution: |
||||
|
||||
1) On the AS400, create the source library: |
||||
|
||||
CRTLIB LIB(ZLIB) TYPE(PROD) TEXT('ZLIB compression API library') |
||||
|
||||
2) Create the source files: |
||||
|
||||
CRTSRCPF FILE(ZLIB/SOURCES) RCDLEN(112) TEXT('ZLIB library modules') |
||||
CRTSRCPF FILE(ZLIB/H) RCDLEN(112) TEXT('ZLIB library includes') |
||||
CRTSRCPF FILE(ZLIB/TOOLS) RCDLEN(112) TEXT('ZLIB library control utilities') |
||||
|
||||
3) From the machine hosting the distribution files, upload them (with |
||||
FTP in text mode, for example) according to the following table: |
||||
|
||||
Original AS400 AS400 AS400 AS400 |
||||
file file member type description |
||||
SOURCES Original ZLIB C subprogram sources |
||||
adler32.c ADLER32 C ZLIB - Compute the Adler-32 checksum of a dta strm |
||||
compress.c COMPRESS C ZLIB - Compress a memory buffer |
||||
crc32.c CRC32 C ZLIB - Compute the CRC-32 of a data stream |
||||
deflate.c DEFLATE C ZLIB - Compress data using the deflation algorithm |
||||
gzio.c GZIO C ZLIB - IO on .gz files |
||||
infback.c INFBACK C ZLIB - Inflate using a callback interface |
||||
inffast.c INFFAST C ZLIB - Fast proc. literals & length/distance pairs |
||||
inflate.c INFLATE C ZLIB - Interface to inflate modules |
||||
inftrees.c INFTREES C ZLIB - Generate Huffman trees for efficient decode |
||||
trees.c TREES C ZLIB - Output deflated data using Huffman coding |
||||
uncompr.c UNCOMPR C ZLIB - Decompress a memory buffer |
||||
zutil.c ZUTIL C ZLIB - Target dependent utility functions |
||||
H Original ZLIB C and ILE/RPG include files |
||||
crc32.h CRC32 C ZLIB - CRC32 tables |
||||
deflate.h DEFLATE C ZLIB - Internal compression state |
||||
inffast.h INFFAST C ZLIB - Header to use inffast.c |
||||
inffixed.h INFFIXED C ZLIB - Table for decoding fixed codes |
||||
inflate.h INFLATE C ZLIB - Internal inflate state definitions |
||||
inftrees.h INFTREES C ZLIB - Header to use inftrees.c |
||||
trees.h TREES C ZLIB - Created automatically with -DGEN_TREES_H |
||||
zconf.h ZCONF C ZLIB - Compression library configuration |
||||
zlib.h ZLIB C ZLIB - Compression library C user interface |
||||
as400/zlib.inc ZLIB.INC RPGLE ZLIB - Compression library ILE RPG user interface |
||||
zutil.h ZUTIL C ZLIB - Internal interface and configuration |
||||
TOOLS Building source software & AS/400 README |
||||
as400/bndsrc BNDSRC Entry point exportation list |
||||
as400/compile.clp COMPILE CLP Compile sources & generate service program |
||||
as400/readme.txt README TXT Installation instructions |
||||
|
||||
4) Continue as in I)3). |
||||
|
||||
|
||||
|
||||
|
||||
Notes: For AS400 ILE RPG programmers, a /copy member defining the ZLIB |
||||
API prototypes for ILE RPG can be found in ZLIB/H(ZLIB.INC). |
||||
Please read comments in this member for more information. |
||||
|
||||
Remember that most foreign textual data are ASCII coded: this |
||||
implementation does not handle conversion from/to ASCII, so |
||||
text data code conversions must be done explicitely. |
||||
|
||||
Always open zipped files in binary mode. |
@ -0,0 +1,327 @@ |
||||
* ZLIB.INC - Interface to the general purpose compression library |
||||
* |
||||
* ILE RPG400 version by Patrick Monnerat, DATASPHERE. |
||||
* Version 1.2.0.7 |
||||
* |
||||
* |
||||
* WARNING: |
||||
* Procedures inflateInit(), inflateInit2(), deflateInit(), |
||||
* deflateInit2() and inflateBackInit() need to be called with |
||||
* two additional arguments: |
||||
* the package version string and the stream control structure. |
||||
* size. This is needed because RPG lacks some macro feature. |
||||
* Call these procedures as: |
||||
* inflateInit(...: ZLIB_VERSION: %size(z_stream)) |
||||
* |
||||
/if not defined(ZLIB_H_) |
||||
/define ZLIB_H_ |
||||
* |
||||
************************************************************************** |
||||
* Constants |
||||
************************************************************************** |
||||
* |
||||
D ZLIB_VERSION C '1.2.0.8' Header's version |
||||
D ZLIB_VERNUM C X'1208' |
||||
* |
||||
D Z_NO_FLUSH C 0 |
||||
D Z_SYNC_FLUSH C 2 |
||||
D Z_FULL_FLUSH C 3 |
||||
D Z_FINISH C 4 |
||||
D Z_BLOCK C 5 |
||||
* |
||||
D Z_OK C 0 |
||||
D Z_STREAM_END C 1 |
||||
D Z_NEED_DICT C 2 |
||||
D Z_ERRNO C -1 |
||||
D Z_STREAM_ERROR C -2 |
||||
D Z_DATA_ERROR C -3 |
||||
D Z_MEM_ERROR C -4 |
||||
D Z_BUF_ERROR C -5 |
||||
DZ_VERSION_ERROR C -6 |
||||
* |
||||
D Z_NO_COMPRESSION... |
||||
D C 0 |
||||
D Z_BEST_SPEED C 1 |
||||
D Z_BEST_COMPRESSION... |
||||
D C 9 |
||||
D Z_DEFAULT_COMPRESSION... |
||||
D C -1 |
||||
* |
||||
D Z_FILTERED C 1 |
||||
D Z_HUFFMAN_ONLY C 2 |
||||
D Z_RLE C 3 |
||||
D Z_DEFAULT_STRATEGY... |
||||
D C 0 |
||||
* |
||||
D Z_BINARY C 0 |
||||
D Z_ASCII C 1 |
||||
D Z_UNKNOWN C 2 |
||||
* |
||||
D Z_DEFLATED C 8 |
||||
* |
||||
D Z_NULL C 0 |
||||
* |
||||
************************************************************************** |
||||
* Types |
||||
************************************************************************** |
||||
* |
||||
D z_streamp S * Stream struct ptr |
||||
D gzFile S * File pointer |
||||
D z_off_t S 10i 0 Stream offsets |
||||
* |
||||
************************************************************************** |
||||
* Structures |
||||
************************************************************************** |
||||
* |
||||
* The GZIP encode/decode stream support structure. |
||||
* |
||||
D z_stream DS align based(z_streamp) |
||||
D zs_next_in * Next input byte |
||||
D zs_avail_in 10U 0 Byte cnt at next_in |
||||
D zs_total_in 10U 0 Total bytes read |
||||
D zs_next_out * Output buffer ptr |
||||
D zs_avail_out 10U 0 Room left @ next_out |
||||
D zs_total_out 10U 0 Total bytes written |
||||
D zs_msg * Last errmsg or null |
||||
D zs_state * Internal state |
||||
D zs_zalloc * procptr Int. state allocator |
||||
D zs_free * procptr Int. state dealloc. |
||||
D zs_opaque * Private alloc. data |
||||
D zs_data_type 10i 0 ASC/BIN best guess |
||||
D zs_adler 10u 0 Uncompr. adler32 val |
||||
D 10U 0 Reserved |
||||
D 10U 0 Ptr. alignment |
||||
* |
||||
************************************************************************** |
||||
* Utility function prototypes |
||||
************************************************************************** |
||||
* |
||||
D compress PR 10I 0 extproc('compress') |
||||
D dest 32767 options(*varsize) Destination buffer |
||||
D destLen 10U 0 Destination length |
||||
D source 32767 const options(*varsize) Source buffer |
||||
D sourceLen 10u 0 value Source length |
||||
* |
||||
D compress2 PR 10I 0 extproc('compress2') |
||||
D dest 32767 options(*varsize) Destination buffer |
||||
D destLen 10U 0 Destination length |
||||
D source 32767 const options(*varsize) Source buffer |
||||
D sourceLen 10U 0 value Source length |
||||
D level 10I 0 value Compression level |
||||
* |
||||
D compressBound PR 10U 0 extproc('compressBound') |
||||
D sourceLen 10U 0 value |
||||
* |
||||
D uncompress PR 10I 0 extproc('uncompress') |
||||
D dest 32767 options(*varsize) Destination buffer |
||||
D destLen 10U 0 Destination length |
||||
D source 32767 const options(*varsize) Source buffer |
||||
D sourceLen 10U 0 value Source length |
||||
* |
||||
D gzopen PR extproc('gzopen') |
||||
D like(gzFile) |
||||
D path * value options(*string) File pathname |
||||
D mode * value options(*string) Open mode |
||||
* |
||||
D gzdopen PR extproc('gzdopen') |
||||
D like(gzFile) |
||||
D fd 10i 0 value File descriptor |
||||
D mode * value options(*string) Open mode |
||||
* |
||||
D gzsetparams PR 10I 0 extproc('gzsetparams') |
||||
D file value like(gzFile) File pointer |
||||
D level 10I 0 value |
||||
D strategy 10i 0 value |
||||
* |
||||
D gzread PR 10I 0 extproc('gzread') |
||||
D file value like(gzFile) File pointer |
||||
D buf 32767 options(*varsize) Buffer |
||||
D len 10u 0 value Buffer length |
||||
* |
||||
D gzwrite PR 10I 0 extproc('gzwrite') |
||||
D file value like(gzFile) File pointer |
||||
D buf 32767 const options(*varsize) Buffer |
||||
D len 10u 0 value Buffer length |
||||
* |
||||
D gzputs PR 10I 0 extproc('gzputs') |
||||
D file value like(gzFile) File pointer |
||||
D s * value options(*string) String to output |
||||
* |
||||
D gzgets PR * extproc('gzgets') |
||||
D file value like(gzFile) File pointer |
||||
D buf 32767 options(*varsize) Read buffer |
||||
D len 10i 0 value Buffer length |
||||
* |
||||
D gzflush PR 10i 0 extproc('gzflush') |
||||
D file value like(gzFile) File pointer |
||||
D flush 10I 0 value Type of flush |
||||
* |
||||
D gzseek PR extproc('gzseek') |
||||
D like(z_off_t) |
||||
D file value like(gzFile) File pointer |
||||
D offset value like(z_off_t) Offset |
||||
D whence 10i 0 value Origin |
||||
* |
||||
D gzrewind PR 10i 0 extproc('gzrewind') |
||||
D file value like(gzFile) File pointer |
||||
* |
||||
D gztell PR extproc('gztell') |
||||
D like(z_off_t) |
||||
D file value like(gzFile) File pointer |
||||
* |
||||
D gzeof PR 10i 0 extproc('gzeof') |
||||
D file value like(gzFile) File pointer |
||||
* |
||||
D gzclose PR 10i 0 extproc('gzclose') |
||||
D file value like(gzFile) File pointer |
||||
* |
||||
D gzerror PR * extproc('gzerror') Error string |
||||
D file value like(gzFile) File pointer |
||||
D errnum 10I 0 Error code |
||||
* |
||||
D gzclearerr PR extproc('gzclearerr') |
||||
D file value like(gzFile) File pointer |
||||
* |
||||
************************************************************************** |
||||
* Basic function prototypes |
||||
************************************************************************** |
||||
* |
||||
D zlibVersion PR * extproc('zlibVersion') Version string |
||||
* |
||||
D deflateInit PR 10I 0 extproc('deflateInit_') Init. compression |
||||
D strm like(z_stream) Compression stream |
||||
D level 10I 0 value Compression level |
||||
D version * value options(*string) Version string |
||||
D stream_size 10i 0 value Stream struct. size |
||||
* |
||||
D deflate PR 10I 0 extproc('deflate') Compress data |
||||
D strm like(z_stream) Compression stream |
||||
D flush 10I 0 value Flush type required |
||||
* |
||||
D deflateEnd PR 10I 0 extproc('deflateEnd') Termin. compression |
||||
D strm like(z_stream) Compression stream |
||||
* |
||||
D inflateInit PR 10I 0 extproc('inflateInit_') Init. expansion |
||||
D strm like(z_stream) Expansion stream |
||||
D version * value options(*string) Version string |
||||
D stream_size 10i 0 value Stream struct. size |
||||
* |
||||
D inflate PR 10I 0 extproc('inflate') Expand data |
||||
D strm like(z_stream) Expansion stream |
||||
D flush 10I 0 value Flush type required |
||||
* |
||||
D inflateEnd PR 10I 0 extproc('inflateEnd') Termin. expansion |
||||
D strm like(z_stream) Expansion stream |
||||
* |
||||
************************************************************************** |
||||
* Advanced function prototypes |
||||
************************************************************************** |
||||
* |
||||
D deflateInit2 PR 10I 0 extproc('deflateInit2_') Init. compression |
||||
D strm like(z_stream) Compression stream |
||||
D level 10I 0 value Compression level |
||||
D method 10I 0 value Compression method |
||||
D windowBits 10I 0 value log2(window size) |
||||
D memLevel 10I 0 value Mem/cmpress tradeoff |
||||
D strategy 10I 0 value Compression stategy |
||||
D version * value options(*string) Version string |
||||
D stream_size 10i 0 value Stream struct. size |
||||
* |
||||
D deflateSetDictionary... |
||||
D PR 10I 0 extproc('deflateSetDictionary') Init. dictionary |
||||
D strm like(z_stream) Compression stream |
||||
D dictionary 32767 const options(*varsize) Dictionary bytes |
||||
D dictLength 10U 0 value Dictionary length |
||||
* |
||||
D deflateCopy PR 10I 0 extproc('deflateCopy') Compress strm 2 strm |
||||
D dest like(z_stream) Destination stream |
||||
D source like(z_stream) Source stream |
||||
* |
||||
D deflateReset PR 10I 0 extproc('deflateReset') End and init. stream |
||||
D strm like(z_stream) Compression stream |
||||
* |
||||
D deflateParams PR 10I 0 extproc('deflateParams') Change level & strat |
||||
D strm like(z_stream) Compression stream |
||||
D level 10I 0 value Compression level |
||||
D strategy 10I 0 value Compression stategy |
||||
* |
||||
D deflateBound PR 10U 0 extproc('deflateBound') Change level & strat |
||||
D strm like(z_stream) Compression stream |
||||
D sourcelen 10U 0 value Compression level |
||||
* |
||||
D deflatePrime PR 10I 0 extproc('deflatePrime') Change level & strat |
||||
D strm like(z_stream) Compression stream |
||||
D bits 10I 0 value Number of bits to insert |
||||
D value 10I 0 value Bits to insert |
||||
* |
||||
D inflateInit2 PR 10I 0 extproc('inflateInit2_') Init. expansion |
||||
D strm like(z_stream) Expansion stream |
||||
D windowBits 10I 0 value log2(window size) |
||||
D version * value options(*string) Version string |
||||
D stream_size 10i 0 value Stream struct. size |
||||
* |
||||
D inflateSetDictionary... |
||||
D PR 10I 0 extproc('inflateSetDictionary') Init. dictionary |
||||
D strm like(z_stream) Expansion stream |
||||
D dictionary 32767 const options(*varsize) Dictionary bytes |
||||
D dictLength 10U 0 value Dictionary length |
||||
* |
||||
D inflateSync PR 10I 0 extproc('inflateSync') Sync. expansion |
||||
D strm like(z_stream) Expansion stream |
||||
* |
||||
D inflateCopy PR 10I 0 extproc('inflateCopy') |
||||
D dest like(z_stream) Destination stream |
||||
D source like(z_stream) Source stream |
||||
* |
||||
D inflateReset PR 10I 0 extproc('inflateReset') End and init. stream |
||||
D strm like(z_stream) Expansion stream |
||||
* |
||||
D inflateBackInit... |
||||
D PR 10I 0 extproc('inflateBackInit_') |
||||
D strm like(z_stream) Expansion stream |
||||
D windowBits 10I 0 value Log2(buffer size) |
||||
D window 32767 options(*varsize) Buffer |
||||
D version * value options(*string) Version string |
||||
D stream_size 10i 0 value Stream struct. size |
||||
* |
||||
D inflateBack PR 10I 0 extproc('inflateBack') |
||||
D strm like(z_stream) Expansion stream |
||||
D in * value procptr Input function |
||||
D in_desc * value Input descriptor |
||||
D out * value procptr Output function |
||||
D out_desc * value Output descriptor |
||||
* |
||||
D inflateBackEnd PR 10I 0 extproc('inflateBackEnd') |
||||
D strm like(z_stream) Expansion stream |
||||
* |
||||
D zlibCompileFlags... |
||||
D PR 10U 0 extproc('zlibCompileFlags') |
||||
* |
||||
************************************************************************** |
||||
* Checksum function prototypes |
||||
************************************************************************** |
||||
* |
||||
D adler32 PR 10U 0 extproc('adler32') New checksum |
||||
D adler 10U 0 value Old checksum |
||||
D buf 32767 const options(*varsize) Bytes to accumulate |
||||
D len 10U 0 value Buffer length |
||||
* |
||||
D crc32 PR 10U 0 extproc('crc32') New checksum |
||||
D crc 10U 0 value Old checksum |
||||
D buf 32767 const options(*varsize) Bytes to accumulate |
||||
D len 10U 0 value Buffer length |
||||
* |
||||
************************************************************************** |
||||
* Miscellaneous function prototypes |
||||
************************************************************************** |
||||
* |
||||
D zError PR * extproc('zError') Error string |
||||
D err 10I 0 value Error code |
||||
* |
||||
D inflateSyncPoint... |
||||
D PR 10I 0 extproc('inflateSyncPoint') |
||||
D strm like(z_stream) Expansion stream |
||||
* |
||||
D get_crc_table PR * extproc('get_crc_table') Ptr to ulongs |
||||
* |
||||
/endif |
@ -0,0 +1,500 @@ |
||||
/* gzappend -- command to append to a gzip file
|
||||
|
||||
Copyright (C) 2003 Mark Adler, all rights reserved |
||||
version 1.1, 4 Nov 2003 |
||||
|
||||
This software is provided 'as-is', without any express or implied |
||||
warranty. In no event will the author be held liable for any damages |
||||
arising from the use of this software. |
||||
|
||||
Permission is granted to anyone to use this software for any purpose, |
||||
including commercial applications, and to alter it and redistribute it |
||||
freely, subject to the following restrictions: |
||||
|
||||
1. The origin of this software must not be misrepresented; you must not |
||||
claim that you wrote the original software. If you use this software |
||||
in a product, an acknowledgment in the product documentation would be |
||||
appreciated but is not required. |
||||
2. Altered source versions must be plainly marked as such, and must not be |
||||
misrepresented as being the original software. |
||||
3. This notice may not be removed or altered from any source distribution. |
||||
|
||||
Mark Adler madler@alumni.caltech.edu |
||||
*/ |
||||
|
||||
/*
|
||||
* Change history: |
||||
* |
||||
* 1.0 19 Oct 2003 - First version |
||||
* 1.1 4 Nov 2003 - Expand and clarify some comments and notes |
||||
* - Add version and copyright to help |
||||
* - Send help to stdout instead of stderr |
||||
* - Add some preemptive typecasts |
||||
* - Add L to constants in lseek() calls |
||||
* - Remove some debugging information in error messages |
||||
* - Use new data_type definition for zlib 1.2.1 |
||||
* - Simplfy and unify file operations |
||||
* - Finish off gzip file in gztack() |
||||
* - Use deflatePrime() instead of adding empty blocks |
||||
* - Keep gzip file clean on appended file read errors |
||||
* - Use in-place rotate instead of auxiliary buffer |
||||
* (Why you ask? Because it was fun to write!) |
||||
*/ |
||||
|
||||
/*
|
||||
gzappend takes a gzip file and appends to it, compressing files from the |
||||
command line or data from stdin. The gzip file is written to directly, to |
||||
avoid copying that file, in case it's large. Note that this results in the |
||||
unfriendly behavior that if gzappend fails, the gzip file is corrupted. |
||||
|
||||
This program was written to illustrate the use of the new Z_BLOCK option of |
||||
zlib 1.2.1's inflate() function. This option returns from inflate() at each |
||||
block boundary to facilitate locating and modifying the last block bit at |
||||
the start of the final deflate block. Also whether using Z_BLOCK or not, |
||||
another required feature of zlib 1.2.1 is that inflate() now provides the |
||||
number of unusued bits in the last input byte used. gzappend will not work |
||||
with versions of zlib earlier than 1.2.1. |
||||
|
||||
gzappend first decompresses the gzip file internally, discarding all but |
||||
the last 32K of uncompressed data, and noting the location of the last block |
||||
bit and the number of unused bits in the last byte of the compressed data. |
||||
The gzip trailer containing the CRC-32 and length of the uncompressed data |
||||
is verified. This trailer will be later overwritten. |
||||
|
||||
Then the last block bit is cleared by seeking back in the file and rewriting |
||||
the byte that contains it. Seeking forward, the last byte of the compressed |
||||
data is saved along with the number of unused bits to initialize deflate. |
||||
|
||||
A deflate process is initialized, using the last 32K of the uncompressed |
||||
data from the gzip file to initialize the dictionary. If the total |
||||
uncompressed data was less than 32K, then all of it is used to initialize |
||||
the dictionary. The deflate output bit buffer is also initialized with the |
||||
last bits from the original deflate stream. From here on, the data to |
||||
append is simply compressed using deflate, and written to the gzip file. |
||||
When that is complete, the new CRC-32 and uncompressed length are written |
||||
as the trailer of the gzip file. |
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <fcntl.h> |
||||
#include <unistd.h> |
||||
#include "zlib.h" |
||||
|
||||
#define local static |
||||
#define LGCHUNK 14 |
||||
#define CHUNK (1U << LGCHUNK) |
||||
#define DSIZE 32768U |
||||
|
||||
/* print an error message and terminate with extreme prejudice */ |
||||
local void bye(char *msg1, char *msg2) |
||||
{ |
||||
fprintf(stderr, "gzappend error: %s%s\n", msg1, msg2); |
||||
exit(1); |
||||
} |
||||
|
||||
/* return the greatest common divisor of a and b using Euclid's algorithm,
|
||||
modified to be fast when one argument much greater than the other, and |
||||
coded to avoid unnecessary swapping */ |
||||
local unsigned gcd(unsigned a, unsigned b) |
||||
{ |
||||
unsigned c; |
||||
|
||||
while (a && b) |
||||
if (a > b) { |
||||
c = b; |
||||
while (a - c >= c) |
||||
c <<= 1; |
||||
a -= c; |
||||
} |
||||
else { |
||||
c = a; |
||||
while (b - c >= c) |
||||
c <<= 1; |
||||
b -= c; |
||||
} |
||||
return a + b; |
||||
} |
||||
|
||||
/* rotate list[0..len-1] left by rot positions, in place */ |
||||
local void rotate(unsigned char *list, unsigned len, unsigned rot) |
||||
{ |
||||
unsigned char tmp; |
||||
unsigned cycles; |
||||
unsigned char *start, *last, *to, *from; |
||||
|
||||
/* normalize rot and handle degenerate cases */ |
||||
if (len < 2) return; |
||||
if (rot >= len) rot %= len; |
||||
if (rot == 0) return; |
||||
|
||||
/* pointer to last entry in list */ |
||||
last = list + (len - 1); |
||||
|
||||
/* do simple left shift by one */ |
||||
if (rot == 1) { |
||||
tmp = *list; |
||||
memcpy(list, list + 1, len - 1); |
||||
*last = tmp; |
||||
return; |
||||
} |
||||
|
||||
/* do simple right shift by one */ |
||||
if (rot == len - 1) { |
||||
tmp = *last; |
||||
memmove(list + 1, list, len - 1); |
||||
*list = tmp; |
||||
return; |
||||
} |
||||
|
||||
/* otherwise do rotate as a set of cycles in place */ |
||||
cycles = gcd(len, rot); /* number of cycles */ |
||||
do { |
||||
start = from = list + cycles; /* start index is arbitrary */ |
||||
tmp = *from; /* save entry to be overwritten */ |
||||
for (;;) { |
||||
to = from; /* next step in cycle */ |
||||
from += rot; /* go right rot positions */ |
||||
if (from > last) from -= len; /* (pointer better not wrap) */ |
||||
if (from == start) break; /* all but one shifted */ |
||||
*to = *from; /* shift left */ |
||||
} |
||||
*to = tmp; /* complete the circle */ |
||||
} while (--cycles); |
||||
} |
||||
|
||||
/* structure for gzip file read operations */ |
||||
typedef struct { |
||||
int fd; /* file descriptor */ |
||||
int size; /* 1 << size is bytes in buf */ |
||||
unsigned left; /* bytes available at next */ |
||||
unsigned char *buf; /* buffer */ |
||||
unsigned char *next; /* next byte in buffer */ |
||||
char *name; /* file name for error messages */ |
||||
} file; |
||||
|
||||
/* reload buffer */ |
||||
local int readin(file *in) |
||||
{ |
||||
int len; |
||||
|
||||
len = read(in->fd, in->buf, 1 << in->size); |
||||
if (len == -1) bye("error reading ", in->name); |
||||
in->left = (unsigned)len; |
||||
in->next = in->buf; |
||||
return len; |
||||
} |
||||
|
||||
/* read from file in, exit if end-of-file */ |
||||
local int readmore(file *in) |
||||
{ |
||||
if (readin(in) == 0) bye("unexpected end of ", in->name); |
||||
return 0; |
||||
} |
||||
|
||||
#define read1(in) (in->left == 0 ? readmore(in) : 0, \ |
||||
in->left--, *(in->next)++) |
||||
|
||||
/* skip over n bytes of in */ |
||||
local void skip(file *in, unsigned n) |
||||
{ |
||||
unsigned bypass; |
||||
|
||||
if (n > in->left) { |
||||
n -= in->left; |
||||
bypass = n & ~((1U << in->size) - 1); |
||||
if (bypass) { |
||||
if (lseek(in->fd, (off_t)bypass, SEEK_CUR) == -1) |
||||
bye("seeking ", in->name); |
||||
n -= bypass; |
||||
} |
||||
readmore(in); |
||||
if (n > in->left) |
||||
bye("unexpected end of ", in->name); |
||||
} |
||||
in->left -= n; |
||||
in->next += n; |
||||
} |
||||
|
||||
/* read a four-byte unsigned integer, little-endian, from in */ |
||||
unsigned long read4(file *in) |
||||
{ |
||||
unsigned long val; |
||||
|
||||
val = read1(in); |
||||
val += (unsigned)read1(in) << 8; |
||||
val += (unsigned long)read1(in) << 16; |
||||
val += (unsigned long)read1(in) << 24; |
||||
return val; |
||||
} |
||||
|
||||
/* skip over gzip header */ |
||||
local void gzheader(file *in) |
||||
{ |
||||
int flags; |
||||
unsigned n; |
||||
|
||||
if (read1(in) != 31 || read1(in) != 139) bye(in->name, " not a gzip file"); |
||||
if (read1(in) != 8) bye("unknown compression method in", in->name); |
||||
flags = read1(in); |
||||
if (flags & 0xe0) bye("unknown header flags set in", in->name); |
||||
skip(in, 6); |
||||
if (flags & 4) { |
||||
n = read1(in); |
||||
n += (unsigned)(read1(in)) << 8; |
||||
skip(in, n); |
||||
} |
||||
if (flags & 8) while (read1(in) != 0) ; |
||||
if (flags & 16) while (read1(in) != 0) ; |
||||
if (flags & 2) skip(in, 2); |
||||
} |
||||
|
||||
/* decompress gzip file "name", return strm with a deflate stream ready to
|
||||
continue compression of the data in the gzip file, and return a file |
||||
descriptor pointing to where to write the compressed data -- the deflate |
||||
stream is initialized to compress using level "level" */ |
||||
local int gzscan(char *name, z_stream *strm, int level) |
||||
{ |
||||
int ret, lastbit, left, full; |
||||
unsigned have; |
||||
unsigned long crc, tot; |
||||
unsigned char *window; |
||||
off_t lastoff, end; |
||||
file gz; |
||||
|
||||
/* open gzip file */ |
||||
gz.name = name; |
||||
gz.fd = open(name, O_RDWR, 0); |
||||
if (gz.fd == -1) bye("cannot open ", name); |
||||
gz.buf = malloc(CHUNK); |
||||
if (gz.buf == NULL) bye("out of memory", ""); |
||||
gz.size = LGCHUNK; |
||||
gz.left = 0; |
||||
|
||||
/* skip gzip header */ |
||||
gzheader(&gz); |
||||
|
||||
/* prepare to decompress */ |
||||
window = malloc(DSIZE); |
||||
if (window == NULL) bye("out of memory", ""); |
||||
strm->zalloc = Z_NULL; |
||||
strm->zfree = Z_NULL; |
||||
strm->opaque = Z_NULL; |
||||
ret = inflateInit2(strm, -15); |
||||
if (ret != Z_OK) bye("out of memory", " or library mismatch"); |
||||
|
||||
/* decompress the deflate stream, saving append information */ |
||||
lastbit = 0; |
||||
lastoff = lseek(gz.fd, 0L, SEEK_CUR) - gz.left; |
||||
left = 0; |
||||
strm->avail_in = gz.left; |
||||
strm->next_in = gz.next; |
||||
crc = crc32(0L, Z_NULL, 0); |
||||
have = full = 0; |
||||
do { |
||||
/* if needed, get more input */ |
||||
if (strm->avail_in == 0) { |
||||
readmore(&gz); |
||||
strm->avail_in = gz.left; |
||||
strm->next_in = gz.next; |
||||
} |
||||
|
||||
/* set up output to next available section of sliding window */ |
||||
strm->avail_out = DSIZE - have; |
||||
strm->next_out = window + have; |
||||
|
||||
/* inflate and check for errors */ |
||||
ret = inflate(strm, Z_BLOCK); |
||||
if (ret == Z_STREAM_ERROR) bye("internal stream error!", ""); |
||||
if (ret == Z_MEM_ERROR) bye("out of memory", ""); |
||||
if (ret == Z_DATA_ERROR) |
||||
bye("invalid compressed data--format violated in", name); |
||||
|
||||
/* update crc and sliding window pointer */ |
||||
crc = crc32(crc, window + have, DSIZE - have - strm->avail_out); |
||||
if (strm->avail_out) |
||||
have = DSIZE - strm->avail_out; |
||||
else { |
||||
have = 0; |
||||
full = 1; |
||||
} |
||||
|
||||
/* process end of block */ |
||||
if (strm->data_type & 128) { |
||||
if (strm->data_type & 64) |
||||
left = strm->data_type & 0x1f; |
||||
else { |
||||
lastbit = strm->data_type & 0x1f; |
||||
lastoff = lseek(gz.fd, 0L, SEEK_CUR) - strm->avail_in; |
||||
} |
||||
} |
||||
} while (ret != Z_STREAM_END); |
||||
inflateEnd(strm); |
||||
gz.left = strm->avail_in; |
||||
gz.next = strm->next_in; |
||||
|
||||
/* save the location of the end of the compressed data */ |
||||
end = lseek(gz.fd, 0L, SEEK_CUR) - gz.left; |
||||
|
||||
/* check gzip trailer and save total for deflate */ |
||||
if (crc != read4(&gz)) |
||||
bye("invalid compressed data--crc mismatch in ", name); |
||||
tot = strm->total_out; |
||||
if ((tot & 0xffffffffUL) != read4(&gz)) |
||||
bye("invalid compressed data--length mismatch in", name); |
||||
|
||||
/* if not at end of file, warn */ |
||||
if (gz.left || readin(&gz)) |
||||
fprintf(stderr, |
||||
"gzappend warning: junk at end of gzip file overwritten\n"); |
||||
|
||||
/* clear last block bit */ |
||||
lseek(gz.fd, lastoff - (lastbit != 0), SEEK_SET); |
||||
if (read(gz.fd, gz.buf, 1) != 1) bye("reading after seek on ", name); |
||||
*gz.buf = (unsigned char)(*gz.buf ^ (1 << ((8 - lastbit) & 7))); |
||||
lseek(gz.fd, -1L, SEEK_CUR); |
||||
if (write(gz.fd, gz.buf, 1) != 1) bye("writing after seek to ", name); |
||||
|
||||
/* if window wrapped, build dictionary from window by rotating */ |
||||
if (full) { |
||||
rotate(window, DSIZE, have); |
||||
have = DSIZE; |
||||
} |
||||
|
||||
/* set up deflate stream with window, crc, total_in, and leftover bits */ |
||||
ret = deflateInit2(strm, level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); |
||||
if (ret != Z_OK) bye("out of memory", ""); |
||||
deflateSetDictionary(strm, window, have); |
||||
strm->adler = crc; |
||||
strm->total_in = tot; |
||||
if (left) { |
||||
lseek(gz.fd, --end, SEEK_SET); |
||||
if (read(gz.fd, gz.buf, 1) != 1) bye("reading after seek on ", name); |
||||
deflatePrime(strm, 8 - left, *gz.buf); |
||||
} |
||||
lseek(gz.fd, end, SEEK_SET); |
||||
|
||||
/* clean up and return */ |
||||
free(window); |
||||
free(gz.buf); |
||||
return gz.fd; |
||||
} |
||||
|
||||
/* append file "name" to gzip file gd using deflate stream strm -- if last
|
||||
is true, then finish off the deflate stream at the end */ |
||||
local void gztack(char *name, int gd, z_stream *strm, int last) |
||||
{ |
||||
int fd, len, ret; |
||||
unsigned left; |
||||
unsigned char *in, *out; |
||||
|
||||
/* open file to compress and append */ |
||||
fd = 0; |
||||
if (name != NULL) { |
||||
fd = open(name, O_RDONLY, 0); |
||||
if (fd == -1) |
||||
fprintf(stderr, "gzappend warning: %s not found, skipping ...\n", |
||||
name); |
||||
} |
||||
|
||||
/* allocate buffers */ |
||||
in = fd == -1 ? NULL : malloc(CHUNK); |
||||
out = malloc(CHUNK); |
||||
if (out == NULL) bye("out of memory", ""); |
||||
|
||||
/* compress input file and append to gzip file */ |
||||
do { |
||||
/* get more input */ |
||||
len = fd == -1 ? 0 : read(fd, in, CHUNK); |
||||
if (len == -1) { |
||||
fprintf(stderr, |
||||
"gzappend warning: error reading %s, skipping rest ...\n", |
||||
name); |
||||
len = 0; |
||||
} |
||||
strm->avail_in = (unsigned)len; |
||||
strm->next_in = in; |
||||
if (len) strm->adler = crc32(strm->adler, in, (unsigned)len); |
||||
|
||||
/* compress and write all available output */ |
||||
do { |
||||
strm->avail_out = CHUNK; |
||||
strm->next_out = out; |
||||
ret = deflate(strm, last && len == 0 ? Z_FINISH : Z_NO_FLUSH); |
||||
left = CHUNK - strm->avail_out; |
||||
while (left) { |
||||
len = write(gd, out + CHUNK - strm->avail_out - left, left); |
||||
if (len == -1) bye("writing gzip file", ""); |
||||
left -= (unsigned)len; |
||||
} |
||||
} while (strm->avail_out == 0 && ret != Z_STREAM_END); |
||||
} while (len != 0); |
||||
|
||||
/* write trailer after last entry */ |
||||
if (last) { |
||||
deflateEnd(strm); |
||||
out[0] = (unsigned char)(strm->adler); |
||||
out[1] = (unsigned char)(strm->adler >> 8); |
||||
out[2] = (unsigned char)(strm->adler >> 16); |
||||
out[3] = (unsigned char)(strm->adler >> 24); |
||||
out[4] = (unsigned char)(strm->total_in); |
||||
out[5] = (unsigned char)(strm->total_in >> 8); |
||||
out[6] = (unsigned char)(strm->total_in >> 16); |
||||
out[7] = (unsigned char)(strm->total_in >> 24); |
||||
len = 8; |
||||
do { |
||||
ret = write(gd, out + 8 - len, len); |
||||
if (ret == -1) bye("writing gzip file", ""); |
||||
len -= ret; |
||||
} while (len); |
||||
close(gd); |
||||
} |
||||
|
||||
/* clean up and return */ |
||||
free(out); |
||||
if (in != NULL) free(in); |
||||
if (fd > 0) close(fd); |
||||
} |
||||
|
||||
/* process the compression level option if present, scan the gzip file, and
|
||||
append the specified files, or append the data from stdin if no other file |
||||
names are provided on the command line -- the gzip file must be writable |
||||
and seekable */ |
||||
int main(int argc, char **argv) |
||||
{ |
||||
int gd, level; |
||||
z_stream strm; |
||||
|
||||
/* ignore command name */ |
||||
argv++; |
||||
|
||||
/* provide usage if no arguments */ |
||||
if (*argv == NULL) { |
||||
printf("gzappend 1.1 (4 Nov 2003) Copyright (C) 2003 Mark Adler\n"); |
||||
printf( |
||||
"usage: gzappend [-level] file.gz [ addthis [ andthis ... ]]\n"); |
||||
return 0; |
||||
} |
||||
|
||||
/* set compression level */ |
||||
level = Z_DEFAULT_COMPRESSION; |
||||
if (argv[0][0] == '-') { |
||||
if (argv[0][1] < '0' || argv[0][1] > '9' || argv[0][2] != 0) |
||||
bye("invalid compression level", ""); |
||||
level = argv[0][1] - '0'; |
||||
if (*++argv == NULL) bye("no gzip file name after options", ""); |
||||
} |
||||
|
||||
/* prepare to append to gzip file */ |
||||
gd = gzscan(*argv++, &strm, level); |
||||
|
||||
/* append files on command line, or from stdin if none */ |
||||
if (*argv == NULL) |
||||
gztack(NULL, gd, &strm, 1); |
||||
else |
||||
do { |
||||
gztack(*argv, gd, &strm, argv[1] == NULL); |
||||
} while (*++argv != NULL); |
||||
return 0; |
||||
} |
Loading…
Reference in new issue