#ifndef __ZLIB_H__
#define __ZLIB_H__

#define ZLIB_VERSION "1.1.3"

#define PALMOS
#include "zconf.h"

/* constants */
#define Z_NO_FLUSH      0
#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
#define Z_SYNC_FLUSH    2
#define Z_FULL_FLUSH    3
#define Z_FINISH        4
/* Allowed flush values; see deflate() below for details */
#define Z_OK            0
#define Z_STREAM_END    1
#define Z_NEED_DICT     2


Quickstart ZLib porting and programming

You need to include SysZLib.h in the program.  Normally it is only in
the file you do the compression/decompression, but if you need it in
multiple files, all but one needs to "#define NOZLIBDEFS".

Before calling any ZLib function, you need to add a line with:
ZLSetup;
Multiple calls to this won't hurt.

Before exiting you should call
ZLTeardown;
to undo the effects of the setup.

These simply do the Palm System Library open and close, so consult
Palm Documentation for details.

Both deflate and inflate have 3 basic calls, init, the actual
(de)compression, and an end/cleanup function.

Deflate and inflate both use about 40k on the dynamic heap.  Less if
the number of bits is less than the default.

The init function comes in two forms allowing different levels of
control (bypassing defaults).  These are simply all #defined to the
most general call in the SysZLib.h header.

all return error information.

DEFLATE

deflateInit2(strm, level, method, windowBits, memLevel, strategy)

deflateInit(strm, level) calls the above, method is Z_DEFLATED,
windowBits is 13 (ZLib on larger computers is normally 15), memLevel
is 6 (Zlib on larger computers is 8), strategy is Z_DEFAULT_STRATEGY).

These are documented in the ZLib docs.  Normally you can just use
deflateInit.

strm is a pointer to a z_stream structure which is filled in by the
init function and is used by the rest.

deflate(strm,func);

strm is the same pointer used in the init.  The func parameter is 0 to
just continue, but other numbers to flush the buffers or to flush and
complete the compression.

deflateEnd(strm); just deallocates structures and memory.

INFLATE

inflateInit2(strm, windowBits) inflateInit(strm) - as above with
windowBits of 15.  Memory usage is a few K plus 2 ** windowBits.

inflate(strm,func);

strm is the same pointer used in the init.  The func parameter is 0 to
just continue, but other numbers to flush the buffers or to flush and
complete the decompression.

inflateEnd(strm); just deallocates structures and memory.

OTHER NOTES (COMMON)

The windowBits can be set to a negative number which will bypass
internal checksum and other functions.  Many if not most programs that
do their own crc use this flag.

The key members of the structure are 

next_in which points to the input buffer (the next input byte)

avail_in which has the number of bytes available i nthe input buffer

next_out which points to the output buffer (next byte out will be
written here)

avail_out how much space is left in the output buffer.

After doing an inflate or deflate operation these will move (avail_x
will decrease, and the next_x pointers will move forward).

Typical usage is

read IBBYTES into INBUF

OUTBUF has OBYTES of space

z.avail_out=OBYTES, z.next_out=OUTBUF;
z.avail_in=IBBYTES, z.next_in=INBUF;

error = XXflate(&z,Z_SYNC_FLUSH)

write (OBYTES - z.avail_out) bytes from OUTBUF, then reset the _outs
as above.

To read more in you might want to memmove z.avail_in bytes from
z.next_in to INBUF to make more room if OUTBUF wasn't big enough to
hold the entire (de)compressed stream.

Then you can continue reading in at INBUF+z.avail_in.

continue until error says Z_STREAM_END (or Z_BUFFER_ERROR if a
negative windowBits is used and an extra EOF byte is not inserted -
see zlib.h or the ZLib docs or source).

In any case for inflation when there is no new input and no output
occurs it is complete.  For Deflation you need to replace Z_SYNC_FLUSH
with Z_FINISH to flush the last bytes out.

See the many examples included here to see these in context.

