zlib - Chicken - SWIG

Table of Contents

Building the example
zlib.h
How the zlib wrapper was developed

Building the example

zlib must be installed for this example to work.
Just type make to build this example.
If zlib is not installed in /usr/lib and /usr/include, then do something similar to the following:
make SWIGOPT="-I/usr/local/include" LIBS="-L/usr/local/lib -lz"

zlib.h

001: /* zlib.h -- interface of the 'zlib' general purpose compression library
002:   version 1.1.4, March 11th, 2002
003: 
004:   Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler
005: 
006:   This software is provided 'as-is', without any express or implied
007:   warranty.  In no event will the authors be held liable for any damages
008:   arising from the use of this software.
009: 
010:   Permission is granted to anyone to use this software for any purpose,
011:   including commercial applications, and to alter it and redistribute it
012:   freely, subject to the following restrictions:
013: 
014:   1. The origin of this software must not be misrepresented; you must not
015:      claim that you wrote the original software. If you use this software
016:      in a product, an acknowledgment in the product documentation would be
017:      appreciated but is not required.
018:   2. Altered source versions must be plainly marked as such, and must not be
019:      misrepresented as being the original software.
020:   3. This notice may not be removed or altered from any source distribution.
021: 
022:   Jean-loup Gailly        Mark Adler
023:   jloup@gzip.org          madler@alumni.caltech.edu
024: 
025: 
026:   The data format used by the zlib library is described by RFCs (Request for
027:   Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
028:   (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
029: */
030: 
031: #ifndef _ZLIB_H
032: #define _ZLIB_H
033: 
034: #include "zconf.h"
035: 
036: #ifdef __cplusplus
037: extern "C" {
038: #endif
039: 
040: #define ZLIB_VERSION "1.1.4"
041: 
042: /* 
043:      The 'zlib' compression library provides in-memory compression and
044:   decompression functions, including integrity checks of the uncompressed
045:   data.  This version of the library supports only one compression method
046:   (deflation) but other algorithms will be added later and will have the same
047:   stream interface.
048: 
049:      Compression can be done in a single step if the buffers are large
050:   enough (for example if an input file is mmap'ed), or can be done by
051:   repeated calls of the compression function.  In the latter case, the
052:   application must provide more input and/or consume the output
053:   (providing more output space) before each call.
054: 
055:      The library also supports reading and writing files in gzip (.gz) format
056:   with an interface similar to that of stdio.
057: 
058:      The library does not install any signal handler. The decoder checks
059:   the consistency of the compressed data, so the library should never
060:   crash even in case of corrupted input.
061: */
062: 
063: typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
064: typedef void   (*free_func)  OF((voidpf opaque, voidpf address));
065: 
066: struct internal_state;
067: 
068: typedef struct z_stream_s {
069:     Bytef    *next_in;  /* next input byte */
070:     uInt     avail_in;  /* number of bytes available at next_in */
071:     uLong    total_in;  /* total nb of input bytes read so far */
072: 
073:     Bytef    *next_out; /* next output byte should be put there */
074:     uInt     avail_out; /* remaining free space at next_out */
075:     uLong    total_out; /* total nb of bytes output so far */
076: 
077:     char     *msg;      /* last error message, NULL if no error */
078:     struct internal_state FAR *state; /* not visible by applications */
079: 
080:     alloc_func zalloc;  /* used to allocate the internal state */
081:     free_func  zfree;   /* used to free the internal state */
082:     voidpf     opaque;  /* private data object passed to zalloc and zfree */
083: 
084:     int     data_type;  /* best guess about the data type: ascii or binary */
085:     uLong   adler;      /* adler32 value of the uncompressed data */
086:     uLong   reserved;   /* reserved for future use */
087: } z_stream;
088: 
089: typedef z_stream FAR *z_streamp;
090: 
091: /*
092:    The application must update next_in and avail_in when avail_in has
093:    dropped to zero. It must update next_out and avail_out when avail_out
094:    has dropped to zero. The application must initialize zalloc, zfree and
095:    opaque before calling the init function. All other fields are set by the
096:    compression library and must not be updated by the application.
097: 
098:    The opaque value provided by the application will be passed as the first
099:    parameter for calls of zalloc and zfree. This can be useful for custom
100:    memory management. The compression library attaches no meaning to the
101:    opaque value.
102: 
103:    zalloc must return Z_NULL if there is not enough memory for the object.
104:    If zlib is used in a multi-threaded application, zalloc and zfree must be
105:    thread safe.
106: 
107:    On 16-bit systems, the functions zalloc and zfree must be able to allocate
108:    exactly 65536 bytes, but will not be required to allocate more than this
109:    if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
110:    pointers returned by zalloc for objects of exactly 65536 bytes *must*
111:    have their offset normalized to zero. The default allocation function
112:    provided by this library ensures this (see zutil.c). To reduce memory
113:    requirements and avoid any allocation of 64K objects, at the expense of
114:    compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
115: 
116:    The fields total_in and total_out can be used for statistics or
117:    progress reports. After compression, total_in holds the total size of
118:    the uncompressed data and may be saved for use in the decompressor
119:    (particularly if the decompressor wants to decompress everything in
120:    a single step).
121: */
122: 
123:                         /* constants */
124: 
125: #define Z_NO_FLUSH      0
126: #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
127: #define Z_SYNC_FLUSH    2
128: #define Z_FULL_FLUSH    3
129: #define Z_FINISH        4
130: /* Allowed flush values; see deflate() below for details */
131: 
132: #define Z_OK            0
133: #define Z_STREAM_END    1
134: #define Z_NEED_DICT     2
135: #define Z_ERRNO        (-1)
136: #define Z_STREAM_ERROR (-2)
137: #define Z_DATA_ERROR   (-3)
138: #define Z_MEM_ERROR    (-4)
139: #define Z_BUF_ERROR    (-5)
140: #define Z_VERSION_ERROR (-6)
141: /* Return codes for the compression/decompression functions. Negative
142:  * values are errors, positive values are used for special but normal events.
143:  */
144: 
145: #define Z_NO_COMPRESSION         0
146: #define Z_BEST_SPEED             1
147: #define Z_BEST_COMPRESSION       9
148: #define Z_DEFAULT_COMPRESSION  (-1)
149: /* compression levels */
150: 
151: #define Z_FILTERED            1
152: #define Z_HUFFMAN_ONLY        2
153: #define Z_DEFAULT_STRATEGY    0
154: /* compression strategy; see deflateInit2() below for details */
155: 
156: #define Z_BINARY   0
157: #define Z_ASCII    1
158: #define Z_UNKNOWN  2
159: /* Possible values of the data_type field */
160: 
161: #define Z_DEFLATED   8
162: /* The deflate compression method (the only one supported in this version) */
163: 
164: #define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
165: 
166: #define zlib_version zlibVersion()
167: /* for compatibility with versions < 1.0.2 */
168: 
169:                         /* basic functions */
170: 
171: ZEXTERN const char * ZEXPORT zlibVersion OF((void));
172: /* The application can compare zlibVersion and ZLIB_VERSION for consistency.
173:    If the first character differs, the library code actually used is
174:    not compatible with the zlib.h header file used by the application.
175:    This check is automatically made by deflateInit and inflateInit.
176:  */
177: 
178: /* 
179: ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
180: 
181:      Initializes the internal stream state for compression. The fields
182:    zalloc, zfree and opaque must be initialized before by the caller.
183:    If zalloc and zfree are set to Z_NULL, deflateInit updates them to
184:    use default allocation functions.
185: 
186:      The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
187:    1 gives best speed, 9 gives best compression, 0 gives no compression at
188:    all (the input data is simply copied a block at a time).
189:    Z_DEFAULT_COMPRESSION requests a default compromise between speed and
190:    compression (currently equivalent to level 6).
191: 
192:      deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
193:    enough memory, Z_STREAM_ERROR if level is not a valid compression level,
194:    Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
195:    with the version assumed by the caller (ZLIB_VERSION).
196:    msg is set to null if there is no error message.  deflateInit does not
197:    perform any compression: this will be done by deflate().
198: */
199: 
200: 
201: ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
202: /*
203:     deflate compresses as much data as possible, and stops when the input
204:   buffer becomes empty or the output buffer becomes full. It may introduce some
205:   output latency (reading input without producing any output) except when
206:   forced to flush.
207: 
208:     The detailed semantics are as follows. deflate performs one or both of the
209:   following actions:
210: 
211:   - Compress more input starting at next_in and update next_in and avail_in
212:     accordingly. If not all input can be processed (because there is not
213:     enough room in the output buffer), next_in and avail_in are updated and
214:     processing will resume at this point for the next call of deflate().
215: 
216:   - Provide more output starting at next_out and update next_out and avail_out
217:     accordingly. This action is forced if the parameter flush is non zero.
218:     Forcing flush frequently degrades the compression ratio, so this parameter
219:     should be set only when necessary (in interactive applications).
220:     Some output may be provided even if flush is not set.
221: 
222:   Before the call of deflate(), the application should ensure that at least
223:   one of the actions is possible, by providing more input and/or consuming
224:   more output, and updating avail_in or avail_out accordingly; avail_out
225:   should never be zero before the call. The application can consume the
226:   compressed output when it wants, for example when the output buffer is full
227:   (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
228:   and with zero avail_out, it must be called again after making room in the
229:   output buffer because there might be more output pending.
230: 
231:     If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
232:   flushed to the output buffer and the output is aligned on a byte boundary, so
233:   that the decompressor can get all input data available so far. (In particular
234:   avail_in is zero after the call if enough output space has been provided
235:   before the call.)  Flushing may degrade compression for some compression
236:   algorithms and so it should be used only when necessary.
237: 
238:     If flush is set to Z_FULL_FLUSH, all output is flushed as with
239:   Z_SYNC_FLUSH, and the compression state is reset so that decompression can
240:   restart from this point if previous compressed data has been damaged or if
241:   random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
242:   the compression.
243: 
244:     If deflate returns with avail_out == 0, this function must be called again
245:   with the same value of the flush parameter and more output space (updated
246:   avail_out), until the flush is complete (deflate returns with non-zero
247:   avail_out).
248: 
249:     If the parameter flush is set to Z_FINISH, pending input is processed,
250:   pending output is flushed and deflate returns with Z_STREAM_END if there
251:   was enough output space; if deflate returns with Z_OK, this function must be
252:   called again with Z_FINISH and more output space (updated avail_out) but no
253:   more input data, until it returns with Z_STREAM_END or an error. After
254:   deflate has returned Z_STREAM_END, the only possible operations on the
255:   stream are deflateReset or deflateEnd.
256:   
257:     Z_FINISH can be used immediately after deflateInit if all the compression
258:   is to be done in a single step. In this case, avail_out must be at least
259:   0.1% larger than avail_in plus 12 bytes.  If deflate does not return
260:   Z_STREAM_END, then it must be called again as described above.
261: 
262:     deflate() sets strm->adler to the adler32 checksum of all input read
263:   so far (that is, total_in bytes).
264: 
265:     deflate() may update data_type if it can make a good guess about
266:   the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
267:   binary. This field is only for information purposes and does not affect
268:   the compression algorithm in any manner.
269: 
270:     deflate() returns Z_OK if some progress has been made (more input
271:   processed or more output produced), Z_STREAM_END if all input has been
272:   consumed and all output has been produced (only when flush is set to
273:   Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
274:   if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
275:   (for example avail_in or avail_out was zero).
276: */
277: 
278: 
279: ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
280: /*
281:      All dynamically allocated data structures for this stream are freed.
282:    This function discards any unprocessed input and does not flush any
283:    pending output.
284: 
285:      deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
286:    stream state was inconsistent, Z_DATA_ERROR if the stream was freed
287:    prematurely (some input or output was discarded). In the error case,
288:    msg may be set but then points to a static string (which must not be
289:    deallocated).
290: */
291: 
292: 
293: /* 
294: ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
295: 
296:      Initializes the internal stream state for decompression. The fields
297:    next_in, avail_in, zalloc, zfree and opaque must be initialized before by
298:    the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
299:    value depends on the compression method), inflateInit determines the
300:    compression method from the zlib header and allocates all data structures
301:    accordingly; otherwise the allocation will be deferred to the first call of
302:    inflate.  If zalloc and zfree are set to Z_NULL, inflateInit updates them to
303:    use default allocation functions.
304: 
305:      inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
306:    memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
307:    version assumed by the caller.  msg is set to null if there is no error
308:    message. inflateInit does not perform any decompression apart from reading
309:    the zlib header if present: this will be done by inflate().  (So next_in and
310:    avail_in may be modified, but next_out and avail_out are unchanged.)
311: */
312: 
313: 
314: ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
315: /*
316:     inflate decompresses as much data as possible, and stops when the input
317:   buffer becomes empty or the output buffer becomes full. It may some
318:   introduce some output latency (reading input without producing any output)
319:   except when forced to flush.
320: 
321:   The detailed semantics are as follows. inflate performs one or both of the
322:   following actions:
323: 
324:   - Decompress more input starting at next_in and update next_in and avail_in
325:     accordingly. If not all input can be processed (because there is not
326:     enough room in the output buffer), next_in is updated and processing
327:     will resume at this point for the next call of inflate().
328: 
329:   - Provide more output starting at next_out and update next_out and avail_out
330:     accordingly.  inflate() provides as much output as possible, until there
331:     is no more input data or no more space in the output buffer (see below
332:     about the flush parameter).
333: 
334:   Before the call of inflate(), the application should ensure that at least
335:   one of the actions is possible, by providing more input and/or consuming
336:   more output, and updating the next_* and avail_* values accordingly.
337:   The application can consume the uncompressed output when it wants, for
338:   example when the output buffer is full (avail_out == 0), or after each
339:   call of inflate(). If inflate returns Z_OK and with zero avail_out, it
340:   must be called again after making room in the output buffer because there
341:   might be more output pending.
342: 
343:     If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
344:   output as possible to the output buffer. The flushing behavior of inflate is
345:   not specified for values of the flush parameter other than Z_SYNC_FLUSH
346:   and Z_FINISH, but the current implementation actually flushes as much output
347:   as possible anyway.
348: 
349:     inflate() should normally be called until it returns Z_STREAM_END or an
350:   error. However if all decompression is to be performed in a single step
351:   (a single call of inflate), the parameter flush should be set to
352:   Z_FINISH. In this case all pending input is processed and all pending
353:   output is flushed; avail_out must be large enough to hold all the
354:   uncompressed data. (The size of the uncompressed data may have been saved
355:   by the compressor for this purpose.) The next operation on this stream must
356:   be inflateEnd to deallocate the decompression state. The use of Z_FINISH
357:   is never required, but can be used to inform inflate that a faster routine
358:   may be used for the single inflate() call.
359: 
360:      If a preset dictionary is needed at this point (see inflateSetDictionary
361:   below), inflate sets strm-adler to the adler32 checksum of the
362:   dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise 
363:   it sets strm->adler to the adler32 checksum of all output produced
364:   so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
365:   an error code as described below. At the end of the stream, inflate()
366:   checks that its computed adler32 checksum is equal to that saved by the
367:   compressor and returns Z_STREAM_END only if the checksum is correct.
368: 
369:     inflate() returns Z_OK if some progress has been made (more input processed
370:   or more output produced), Z_STREAM_END if the end of the compressed data has
371:   been reached and all uncompressed output has been produced, Z_NEED_DICT if a
372:   preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
373:   corrupted (input stream not conforming to the zlib format or incorrect
374:   adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
375:   (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
376:   enough memory, Z_BUF_ERROR if no progress is possible or if there was not
377:   enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
378:   case, the application may then call inflateSync to look for a good
379:   compression block.
380: */
381: 
382: 
383: ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
384: /*
385:      All dynamically allocated data structures for this stream are freed.
386:    This function discards any unprocessed input and does not flush any
387:    pending output.
388: 
389:      inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
390:    was inconsistent. In the error case, msg may be set but then points to a
391:    static string (which must not be deallocated).
392: */
393: 
394:                         /* Advanced functions */
395: 
396: /*
397:     The following functions are needed only in some special applications.
398: */
399: 
400: /*   
401: ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
402:                                      int  level,
403:                                      int  method,
404:                                      int  windowBits,
405:                                      int  memLevel,
406:                                      int  strategy));
407: 
408:      This is another version of deflateInit with more compression options. The
409:    fields next_in, zalloc, zfree and opaque must be initialized before by
410:    the caller.
411: 
412:      The method parameter is the compression method. It must be Z_DEFLATED in
413:    this version of the library.
414: 
415:      The windowBits parameter is the base two logarithm of the window size
416:    (the size of the history buffer).  It should be in the range 8..15 for this
417:    version of the library. Larger values of this parameter result in better
418:    compression at the expense of memory usage. The default value is 15 if
419:    deflateInit is used instead.
420: 
421:      The memLevel parameter specifies how much memory should be allocated
422:    for the internal compression state. memLevel=1 uses minimum memory but
423:    is slow and reduces compression ratio; memLevel=9 uses maximum memory
424:    for optimal speed. The default value is 8. See zconf.h for total memory
425:    usage as a function of windowBits and memLevel.
426: 
427:      The strategy parameter is used to tune the compression algorithm. Use the
428:    value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
429:    filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
430:    string match).  Filtered data consists mostly of small values with a
431:    somewhat random distribution. In this case, the compression algorithm is
432:    tuned to compress them better. The effect of Z_FILTERED is to force more
433:    Huffman coding and less string matching; it is somewhat intermediate
434:    between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
435:    the compression ratio but not the correctness of the compressed output even
436:    if it is not set appropriately.
437: 
438:       deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
439:    memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
440:    method). msg is set to null if there is no error message.  deflateInit2 does
441:    not perform any compression: this will be done by deflate().
442: */
443:                             
444: ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
445:                                              const Bytef *dictionary,
446:                                              uInt  dictLength));
447: /*
448:      Initializes the compression dictionary from the given byte sequence
449:    without producing any compressed output. This function must be called
450:    immediately after deflateInit, deflateInit2 or deflateReset, before any
451:    call of deflate. The compressor and decompressor must use exactly the same
452:    dictionary (see inflateSetDictionary).
453: 
454:      The dictionary should consist of strings (byte sequences) that are likely
455:    to be encountered later in the data to be compressed, with the most commonly
456:    used strings preferably put towards the end of the dictionary. Using a
457:    dictionary is most useful when the data to be compressed is short and can be
458:    predicted with good accuracy; the data can then be compressed better than
459:    with the default empty dictionary.
460: 
461:      Depending on the size of the compression data structures selected by
462:    deflateInit or deflateInit2, a part of the dictionary may in effect be
463:    discarded, for example if the dictionary is larger than the window size in
464:    deflate or deflate2. Thus the strings most likely to be useful should be
465:    put at the end of the dictionary, not at the front.
466: 
467:      Upon return of this function, strm->adler is set to the Adler32 value
468:    of the dictionary; the decompressor may later use this value to determine
469:    which dictionary has been used by the compressor. (The Adler32 value
470:    applies to the whole dictionary even if only a subset of the dictionary is
471:    actually used by the compressor.)
472: 
473:      deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
474:    parameter is invalid (such as NULL dictionary) or the stream state is
475:    inconsistent (for example if deflate has already been called for this stream
476:    or if the compression method is bsort). deflateSetDictionary does not
477:    perform any compression: this will be done by deflate().
478: */
479: 
480: ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
481:                                     z_streamp source));
482: /*
483:      Sets the destination stream as a complete copy of the source stream.
484: 
485:      This function can be useful when several compression strategies will be
486:    tried, for example when there are several ways of pre-processing the input
487:    data with a filter. The streams that will be discarded should then be freed
488:    by calling deflateEnd.  Note that deflateCopy duplicates the internal
489:    compression state which can be quite large, so this strategy is slow and
490:    can consume lots of memory.
491: 
492:      deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
493:    enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
494:    (such as zalloc being NULL). msg is left unchanged in both source and
495:    destination.
496: */
497: 
498: ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
499: /*
500:      This function is equivalent to deflateEnd followed by deflateInit,
501:    but does not free and reallocate all the internal compression state.
502:    The stream will keep the same compression level and any other attributes
503:    that may have been set by deflateInit2.
504: 
505:       deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
506:    stream state was inconsistent (such as zalloc or state being NULL).
507: */
508: 
509: ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
510: 				      int level,
511: 				      int strategy));
512: /*
513:      Dynamically update the compression level and compression strategy.  The
514:    interpretation of level and strategy is as in deflateInit2.  This can be
515:    used to switch between compression and straight copy of the input data, or
516:    to switch to a different kind of input data requiring a different
517:    strategy. If the compression level is changed, the input available so far
518:    is compressed with the old level (and may be flushed); the new level will
519:    take effect only at the next call of deflate().
520: 
521:      Before the call of deflateParams, the stream state must be set as for
522:    a call of deflate(), since the currently available input may have to
523:    be compressed and flushed. In particular, strm->avail_out must be non-zero.
524: 
525:      deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
526:    stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
527:    if strm->avail_out was zero.
528: */
529: 
530: /*   
531: ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
532:                                      int  windowBits));
533: 
534:      This is another version of inflateInit with an extra parameter. The
535:    fields next_in, avail_in, zalloc, zfree and opaque must be initialized
536:    before by the caller.
537: 
538:      The windowBits parameter is the base two logarithm of the maximum window
539:    size (the size of the history buffer).  It should be in the range 8..15 for
540:    this version of the library. The default value is 15 if inflateInit is used
541:    instead. If a compressed stream with a larger window size is given as
542:    input, inflate() will return with the error code Z_DATA_ERROR instead of
543:    trying to allocate a larger window.
544: 
545:       inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
546:    memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
547:    memLevel). msg is set to null if there is no error message.  inflateInit2
548:    does not perform any decompression apart from reading the zlib header if
549:    present: this will be done by inflate(). (So next_in and avail_in may be
550:    modified, but next_out and avail_out are unchanged.)
551: */
552: 
553: ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
554:                                              const Bytef *dictionary,
555:                                              uInt  dictLength));
556: /*
557:      Initializes the decompression dictionary from the given uncompressed byte
558:    sequence. This function must be called immediately after a call of inflate
559:    if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
560:    can be determined from the Adler32 value returned by this call of
561:    inflate. The compressor and decompressor must use exactly the same
562:    dictionary (see deflateSetDictionary).
563: 
564:      inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
565:    parameter is invalid (such as NULL dictionary) or the stream state is
566:    inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
567:    expected one (incorrect Adler32 value). inflateSetDictionary does not
568:    perform any decompression: this will be done by subsequent calls of
569:    inflate().
570: */
571: 
572: ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
573: /* 
574:     Skips invalid compressed data until a full flush point (see above the
575:   description of deflate with Z_FULL_FLUSH) can be found, or until all
576:   available input is skipped. No output is provided.
577: 
578:     inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
579:   if no more input was provided, Z_DATA_ERROR if no flush point has been found,
580:   or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
581:   case, the application may save the current current value of total_in which
582:   indicates where valid compressed data was found. In the error case, the
583:   application may repeatedly call inflateSync, providing more input each time,
584:   until success or end of the input data.
585: */
586: 
587: ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
588: /*
589:      This function is equivalent to inflateEnd followed by inflateInit,
590:    but does not free and reallocate all the internal decompression state.
591:    The stream will keep attributes that may have been set by inflateInit2.
592: 
593:       inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
594:    stream state was inconsistent (such as zalloc or state being NULL).
595: */
596: 
597: 
598:                         /* utility functions */
599: 
600: /*
601:      The following utility functions are implemented on top of the
602:    basic stream-oriented functions. To simplify the interface, some
603:    default options are assumed (compression level and memory usage,
604:    standard memory allocation functions). The source code of these
605:    utility functions can easily be modified if you need special options.
606: */
607: 
608: ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen,
609:                                  const Bytef *source, uLong sourceLen));
610: /*
611:      Compresses the source buffer into the destination buffer.  sourceLen is
612:    the byte length of the source buffer. Upon entry, destLen is the total
613:    size of the destination buffer, which must be at least 0.1% larger than
614:    sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
615:    compressed buffer.
616:      This function can be used to compress a whole file at once if the
617:    input file is mmap'ed.
618:      compress returns Z_OK if success, Z_MEM_ERROR if there was not
619:    enough memory, Z_BUF_ERROR if there was not enough room in the output
620:    buffer.
621: */
622: 
623: ZEXTERN int ZEXPORT compress2 OF((Bytef *dest,   uLongf *destLen,
624:                                   const Bytef *source, uLong sourceLen,
625:                                   int level));
626: /*
627:      Compresses the source buffer into the destination buffer. The level
628:    parameter has the same meaning as in deflateInit.  sourceLen is the byte
629:    length of the source buffer. Upon entry, destLen is the total size of the
630:    destination buffer, which must be at least 0.1% larger than sourceLen plus
631:    12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
632: 
633:      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
634:    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
635:    Z_STREAM_ERROR if the level parameter is invalid.
636: */
637: 
638: ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
639:                                    const Bytef *source, uLong sourceLen));
640: /*
641:      Decompresses the source buffer into the destination buffer.  sourceLen is
642:    the byte length of the source buffer. Upon entry, destLen is the total
643:    size of the destination buffer, which must be large enough to hold the
644:    entire uncompressed data. (The size of the uncompressed data must have
645:    been saved previously by the compressor and transmitted to the decompressor
646:    by some mechanism outside the scope of this compression library.)
647:    Upon exit, destLen is the actual size of the compressed buffer.
648:      This function can be used to decompress a whole file at once if the
649:    input file is mmap'ed.
650: 
651:      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
652:    enough memory, Z_BUF_ERROR if there was not enough room in the output
653:    buffer, or Z_DATA_ERROR if the input data was corrupted.
654: */
655: 
656: 
657: typedef voidp gzFile;
658: 
659: ZEXTERN gzFile ZEXPORT gzopen  OF((const char *path, const char *mode));
660: /*
661:      Opens a gzip (.gz) file for reading or writing. The mode parameter
662:    is as in fopen ("rb" or "wb") but can also include a compression level
663:    ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
664:    Huffman only compression as in "wb1h". (See the description
665:    of deflateInit2 for more information about the strategy parameter.)
666: 
667:      gzopen can be used to read a file which is not in gzip format; in this
668:    case gzread will directly read from the file without decompression.
669: 
670:      gzopen returns NULL if the file could not be opened or if there was
671:    insufficient memory to allocate the (de)compression state; errno
672:    can be checked to distinguish the two cases (if errno is zero, the
673:    zlib error is Z_MEM_ERROR).  */
674: 
675: ZEXTERN gzFile ZEXPORT gzdopen  OF((int fd, const char *mode));
676: /*
677:      gzdopen() associates a gzFile with the file descriptor fd.  File
678:    descriptors are obtained from calls like open, dup, creat, pipe or
679:    fileno (in the file has been previously opened with fopen).
680:    The mode parameter is as in gzopen.
681:      The next call of gzclose on the returned gzFile will also close the
682:    file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
683:    descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
684:      gzdopen returns NULL if there was insufficient memory to allocate
685:    the (de)compression state.
686: */
687: 
688: ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
689: /*
690:      Dynamically update the compression level or strategy. See the description
691:    of deflateInit2 for the meaning of these parameters.
692:      gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
693:    opened for writing.
694: */
695: 
696: ZEXTERN int ZEXPORT    gzread  OF((gzFile file, voidp buf, unsigned len));
697: /*
698:      Reads the given number of uncompressed bytes from the compressed file.
699:    If the input file was not in gzip format, gzread copies the given number
700:    of bytes into the buffer.
701:      gzread returns the number of uncompressed bytes actually read (0 for
702:    end of file, -1 for error). */
703: 
704: ZEXTERN int ZEXPORT    gzwrite OF((gzFile file, 
705: 				   const voidp buf, unsigned len));
706: /*
707:      Writes the given number of uncompressed bytes into the compressed file.
708:    gzwrite returns the number of uncompressed bytes actually written
709:    (0 in case of error).
710: */
711: 
712: ZEXTERN int ZEXPORTVA   gzprintf OF((gzFile file, const char *format, ...));
713: /*
714:      Converts, formats, and writes the args to the compressed file under
715:    control of the format string, as in fprintf. gzprintf returns the number of
716:    uncompressed bytes actually written (0 in case of error).
717: */
718: 
719: ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
720: /*
721:       Writes the given null-terminated string to the compressed file, excluding
722:    the terminating null character.
723:       gzputs returns the number of characters written, or -1 in case of error.
724: */
725: 
726: ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
727: /*
728:       Reads bytes from the compressed file until len-1 characters are read, or
729:    a newline character is read and transferred to buf, or an end-of-file
730:    condition is encountered.  The string is then terminated with a null
731:    character.
732:       gzgets returns buf, or Z_NULL in case of error.
733: */
734: 
735: ZEXTERN int ZEXPORT    gzputc OF((gzFile file, int c));
736: /*
737:       Writes c, converted to an unsigned char, into the compressed file.
738:    gzputc returns the value that was written, or -1 in case of error.
739: */
740: 
741: ZEXTERN int ZEXPORT    gzgetc OF((gzFile file));
742: /*
743:       Reads one byte from the compressed file. gzgetc returns this byte
744:    or -1 in case of end of file or error.
745: */
746: 
747: ZEXTERN int ZEXPORT    gzflush OF((gzFile file, int flush));
748: /*
749:      Flushes all pending output into the compressed file. The parameter
750:    flush is as in the deflate() function. The return value is the zlib
751:    error number (see function gzerror below). gzflush returns Z_OK if
752:    the flush parameter is Z_FINISH and all output could be flushed.
753:      gzflush should be called only when strictly necessary because it can
754:    degrade compression.
755: */
756: 
757: ZEXTERN z_off_t ZEXPORT    gzseek OF((gzFile file,
758: 				      z_off_t offset, int whence));
759: /* 
760:       Sets the starting position for the next gzread or gzwrite on the
761:    given compressed file. The offset represents a number of bytes in the
762:    uncompressed data stream. The whence parameter is defined as in lseek(2);
763:    the value SEEK_END is not supported.
764:      If the file is opened for reading, this function is emulated but can be
765:    extremely slow. If the file is opened for writing, only forward seeks are
766:    supported; gzseek then compresses a sequence of zeroes up to the new
767:    starting position.
768: 
769:       gzseek returns the resulting offset location as measured in bytes from
770:    the beginning of the uncompressed stream, or -1 in case of error, in
771:    particular if the file is opened for writing and the new starting position
772:    would be before the current position.
773: */
774: 
775: ZEXTERN int ZEXPORT    gzrewind OF((gzFile file));
776: /*
777:      Rewinds the given file. This function is supported only for reading.
778: 
779:    gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
780: */
781: 
782: ZEXTERN z_off_t ZEXPORT    gztell OF((gzFile file));
783: /*
784:      Returns the starting position for the next gzread or gzwrite on the
785:    given compressed file. This position represents a number of bytes in the
786:    uncompressed data stream.
787: 
788:    gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
789: */
790: 
791: ZEXTERN int ZEXPORT gzeof OF((gzFile file));
792: /*
793:      Returns 1 when EOF has previously been detected reading the given
794:    input stream, otherwise zero.
795: */
796: 
797: ZEXTERN int ZEXPORT    gzclose OF((gzFile file));
798: /*
799:      Flushes all pending output if necessary, closes the compressed file
800:    and deallocates all the (de)compression state. The return value is the zlib
801:    error number (see function gzerror below).
802: */
803: 
804: ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
805: /*
806:      Returns the error message for the last error which occurred on the
807:    given compressed file. errnum is set to zlib error number. If an
808:    error occurred in the file system and not in the compression library,
809:    errnum is set to Z_ERRNO and the application may consult errno
810:    to get the exact error code.
811: */
812: 
813:                         /* checksum functions */
814: 
815: /*
816:      These functions are not related to compression but are exported
817:    anyway because they might be useful in applications using the
818:    compression library.
819: */
820: 
821: ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
822: 
823: /*
824:      Update a running Adler-32 checksum with the bytes buf[0..len-1] and
825:    return the updated checksum. If buf is NULL, this function returns
826:    the required initial value for the checksum.
827:    An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
828:    much faster. Usage example:
829: 
830:      uLong adler = adler32(0L, Z_NULL, 0);
831: 
832:      while (read_buffer(buffer, length) != EOF) {
833:        adler = adler32(adler, buffer, length);
834:      }
835:      if (adler != original_adler) error();
836: */
837: 
838: ZEXTERN uLong ZEXPORT crc32   OF((uLong crc, const Bytef *buf, uInt len));
839: /*
840:      Update a running crc with the bytes buf[0..len-1] and return the updated
841:    crc. If buf is NULL, this function returns the required initial value
842:    for the crc. Pre- and post-conditioning (one's complement) is performed
843:    within this function so it shouldn't be done by the application.
844:    Usage example:
845: 
846:      uLong crc = crc32(0L, Z_NULL, 0);
847: 
848:      while (read_buffer(buffer, length) != EOF) {
849:        crc = crc32(crc, buffer, length);
850:      }
851:      if (crc != original_crc) error();
852: */
853: 
854: 
855:                         /* various hacks, don't look :) */
856: 
857: /* deflateInit and inflateInit are macros to allow checking the zlib version
858:  * and the compiler's view of z_stream:
859:  */
860: ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
861:                                      const char *version, int stream_size));
862: ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
863:                                      const char *version, int stream_size));
864: ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int  level, int  method,
865:                                       int windowBits, int memLevel,
866:                                       int strategy, const char *version,
867:                                       int stream_size));
868: ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int  windowBits,
869:                                       const char *version, int stream_size));
870: #define deflateInit(strm, level) \
871:         deflateInit_((strm), (level),       ZLIB_VERSION, sizeof(z_stream))
872: #define inflateInit(strm) \
873:         inflateInit_((strm),                ZLIB_VERSION, sizeof(z_stream))
874: #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
875:         deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
876:                       (strategy),           ZLIB_VERSION, sizeof(z_stream))
877: #define inflateInit2(strm, windowBits) \
878:         inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
879: 
880: 
881: #if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)
882:     struct internal_state {int dummy;}; /* hack for buggy compilers */
883: #endif
884: 
885: ZEXTERN const char   * ZEXPORT zError           OF((int err));
886: ZEXTERN int            ZEXPORT inflateSyncPoint OF((z_streamp z));
887: ZEXTERN const uLongf * ZEXPORT get_crc_table    OF((void));
888: 
889: #ifdef __cplusplus
890: }
891: #endif
892: 
893: #endif /* _ZLIB_H */
      

How the zlib wrapper was developed

Attempt #1

/* File : example.i */
%module example
%{
/* Put headers and other declarations here */
#include "zlib.h"
%}

%include typemaps.i

%include "zlib.h"
      
The result is:
% swig -chicken -I/usr/include example.i
/usr/include/zlib.h:63: Syntax error in input.
/usr/include/zlib.h:78: Syntax error in input.
/usr/include/zlib.h:80: Syntax error in input.
      
The first problem we see is that the macro OF(...) is not defined.

Attempt #2

We make sure to include zconf.h so that SWIG can see the definition of OF(...). We try again.
/* File : example.i */
%module example
%{
/* Put headers and other declarations here */
#include "zlib.h"
%}

%include typemaps.i

%include "zconf.h"
%include "zlib.h"
      
The result is:
% swig -chicken -I/usr/include example.i
      
This seems to work! But we should take a peek inside the generated example_wrap.c to see what the names of the Scheme procedures will be.
% grep C_intern example_wrap.c
  C_word err = C_intern2 (&a, errorhook);
    sym = C_intern (&a, 21, "example:max-mem-level");
    sym = C_intern (&a, 17, "example:max-wbits");
    sym = C_intern (&a, 16, "example:seek-set");
    sym = C_intern (&a, 16, "example:seek-cur");
    sym = C_intern (&a, 16, "example:seek-end");
    sym = C_intern (&a, 20, "example:zlib-version");
    sym = C_intern (&a, 28, "example:z-stream-next-in-set");
    sym = C_intern (&a, 28, "example:z-stream-next-in-get");
    sym = C_intern (&a, 29, "example:z-stream-avail-in-set");
    sym = C_intern (&a, 29, "example:z-stream-avail-in-get");
    sym = C_intern (&a, 29, "example:z-stream-total-in-set");
    sym = C_intern (&a, 29, "example:z-stream-total-in-get");
    sym = C_intern (&a, 29, "example:z-stream-next-out-set");
    sym = C_intern (&a, 29, "example:z-stream-next-out-get");
    sym = C_intern (&a, 30, "example:z-stream-avail-out-set");
    sym = C_intern (&a, 30, "example:z-stream-avail-out-get");
    sym = C_intern (&a, 30, "example:z-stream-total-out-set");
    sym = C_intern (&a, 30, "example:z-stream-total-out-get");
    sym = C_intern (&a, 24, "example:z-stream-msg-set");
    sym = C_intern (&a, 24, "example:z-stream-msg-get");
    sym = C_intern (&a, 26, "example:z-stream-state-set");
    sym = C_intern (&a, 26, "example:z-stream-state-get");
    sym = C_intern (&a, 27, "example:z-stream-zalloc-set");
    sym = C_intern (&a, 27, "example:z-stream-zalloc-get");
    sym = C_intern (&a, 26, "example:z-stream-zfree-set");
    sym = C_intern (&a, 26, "example:z-stream-zfree-get");
    sym = C_intern (&a, 27, "example:z-stream-opaque-set");
    sym = C_intern (&a, 27, "example:z-stream-opaque-get");
    sym = C_intern (&a, 30, "example:z-stream-data-type-set");
    sym = C_intern (&a, 30, "example:z-stream-data-type-get");
    sym = C_intern (&a, 26, "example:z-stream-adler-set");
    sym = C_intern (&a, 26, "example:z-stream-adler-get");
    sym = C_intern (&a, 29, "example:z-stream-reserved-set");
    sym = C_intern (&a, 29, "example:z-stream-reserved-get");
    sym = C_intern (&a, 20, "example:new-z-stream");
    sym = C_intern (&a, 23, "example:delete-z-stream");
    sym = C_intern (&a, 18, "example:z-no-flush");
    sym = C_intern (&a, 23, "example:z-partial-flush");
    sym = C_intern (&a, 20, "example:z-sync-flush");
    sym = C_intern (&a, 20, "example:z-full-flush");
    sym = C_intern (&a, 16, "example:z-finish");
    sym = C_intern (&a, 12, "example:z-ok");
    sym = C_intern (&a, 20, "example:z-stream-end");
    sym = C_intern (&a, 19, "example:z-need-dict");
    sym = C_intern (&a, 15, "example:z-errno");
    sym = C_intern (&a, 22, "example:z-stream-error");
    sym = C_intern (&a, 20, "example:z-data-error");
    sym = C_intern (&a, 19, "example:z-mem-error");
    sym = C_intern (&a, 19, "example:z-buf-error");
    sym = C_intern (&a, 23, "example:z-version-error");
    sym = C_intern (&a, 24, "example:z-no-compression");
    sym = C_intern (&a, 20, "example:z-best-speed");
    sym = C_intern (&a, 26, "example:z-best-compression");
    sym = C_intern (&a, 29, "example:z-default-compression");
    sym = C_intern (&a, 18, "example:z-filtered");
    sym = C_intern (&a, 22, "example:z-huffman-only");
    sym = C_intern (&a, 26, "example:z-default-strategy");
    sym = C_intern (&a, 16, "example:z-binary");
    sym = C_intern (&a, 15, "example:z-ascii");
    sym = C_intern (&a, 17, "example:z-unknown");
    sym = C_intern (&a, 18, "example:z-deflated");
    sym = C_intern (&a, 14, "example:z-null");
    sym = C_intern (&a, 19, "example:zlibversion");
    sym = C_intern (&a, 15, "example:deflate");
    sym = C_intern (&a, 18, "example:deflateend");
    sym = C_intern (&a, 15, "example:inflate");
    sym = C_intern (&a, 18, "example:inflateend");
    sym = C_intern (&a, 28, "example:deflatesetdictionary");
    sym = C_intern (&a, 19, "example:deflatecopy");
    sym = C_intern (&a, 20, "example:deflatereset");
    sym = C_intern (&a, 21, "example:deflateparams");
    sym = C_intern (&a, 28, "example:inflatesetdictionary");
    sym = C_intern (&a, 19, "example:inflatesync");
    sym = C_intern (&a, 20, "example:inflatereset");
    sym = C_intern (&a, 16, "example:compress");
    sym = C_intern (&a, 17, "example:compress2");
    sym = C_intern (&a, 18, "example:uncompress");
    sym = C_intern (&a, 14, "example:gzopen");
    sym = C_intern (&a, 15, "example:gzdopen");
    sym = C_intern (&a, 19, "example:gzsetparams");
    sym = C_intern (&a, 14, "example:gzread");
    sym = C_intern (&a, 15, "example:gzwrite");
    sym = C_intern (&a, 16, "example:gzprintf");
    sym = C_intern (&a, 14, "example:gzputs");
    sym = C_intern (&a, 14, "example:gzgets");
    sym = C_intern (&a, 14, "example:gzputc");
    sym = C_intern (&a, 14, "example:gzgetc");
    sym = C_intern (&a, 15, "example:gzflush");
    sym = C_intern (&a, 14, "example:gzseek");
    sym = C_intern (&a, 16, "example:gzrewind");
    sym = C_intern (&a, 14, "example:gztell");
    sym = C_intern (&a, 13, "example:gzeof");
    sym = C_intern (&a, 15, "example:gzclose");
    sym = C_intern (&a, 15, "example:gzerror");
    sym = C_intern (&a, 15, "example:adler32");
    sym = C_intern (&a, 13, "example:crc32");
    sym = C_intern (&a, 20, "example:deflateinit-");
    sym = C_intern (&a, 20, "example:inflateinit-");
    sym = C_intern (&a, 21, "example:deflateinit2-");
    sym = C_intern (&a, 21, "example:inflateinit2-");
    sym = C_intern (&a, 32, "example:internal-state-dummy-set");
    sym = C_intern (&a, 32, "example:internal-state-dummy-get");
    sym = C_intern (&a, 26, "example:new-internal-state");
    sym = C_intern (&a, 29, "example:delete-internal-state");
    sym = C_intern (&a, 14, "example:zerror");
    sym = C_intern (&a, 24, "example:inflatesyncpoint");
    sym = C_intern (&a, 21, "example:get-crc-table");
      
In fact, we want the Scheme procedure names to begin with zlib instead of example. For example:zlib-version, we want zlib-version. And we want dashes when the case switches to/from upper/lowercase; ex. the function deflateEnd() should be the Scheme procedure zlib-deflate-end.

Attempt #3

We make sure to add -prefix zlib -mixed to the swig command line, and we rename ZLIB_VERSION to VERSION. We try again.
/* File : example.i */
%module example
%{
/* Put headers and other declarations here */
#include "zlib.h"
%}

%include typemaps.i

%rename(VERSION) ZLIB_VERSION;

%include "zconf.h"
%include "zlib.h"
      
The result is:
% swig -chicken -prefix zlib -mixed -I/usr/include example.i
% grep C_intern example_wrap.c
  C_word err = C_intern2 (&a, errorhook);
    sym = C_intern (&a, 18, "zlib:max-mem-level");
    sym = C_intern (&a, 14, "zlib:max-wbits");
    sym = C_intern (&a, 13, "zlib:seek-set");
    sym = C_intern (&a, 13, "zlib:seek-cur");
    sym = C_intern (&a, 13, "zlib:seek-end");
    sym = C_intern (&a, 12, "zlib:version");
    sym = C_intern (&a, 25, "zlib:z-stream-next-in-set");
    sym = C_intern (&a, 25, "zlib:z-stream-next-in-get");
    sym = C_intern (&a, 26, "zlib:z-stream-avail-in-set");
    sym = C_intern (&a, 26, "zlib:z-stream-avail-in-get");
    sym = C_intern (&a, 26, "zlib:z-stream-total-in-set");
    sym = C_intern (&a, 26, "zlib:z-stream-total-in-get");
    sym = C_intern (&a, 26, "zlib:z-stream-next-out-set");
    sym = C_intern (&a, 26, "zlib:z-stream-next-out-get");
    sym = C_intern (&a, 27, "zlib:z-stream-avail-out-set");
    sym = C_intern (&a, 27, "zlib:z-stream-avail-out-get");
    sym = C_intern (&a, 27, "zlib:z-stream-total-out-set");
    sym = C_intern (&a, 27, "zlib:z-stream-total-out-get");
    sym = C_intern (&a, 21, "zlib:z-stream-msg-set");
    sym = C_intern (&a, 21, "zlib:z-stream-msg-get");
    sym = C_intern (&a, 23, "zlib:z-stream-state-set");
    sym = C_intern (&a, 23, "zlib:z-stream-state-get");
    sym = C_intern (&a, 24, "zlib:z-stream-zalloc-set");
    sym = C_intern (&a, 24, "zlib:z-stream-zalloc-get");
    sym = C_intern (&a, 23, "zlib:z-stream-zfree-set");
    sym = C_intern (&a, 23, "zlib:z-stream-zfree-get");
    sym = C_intern (&a, 24, "zlib:z-stream-opaque-set");
    sym = C_intern (&a, 24, "zlib:z-stream-opaque-get");
    sym = C_intern (&a, 27, "zlib:z-stream-data-type-set");
    sym = C_intern (&a, 27, "zlib:z-stream-data-type-get");
    sym = C_intern (&a, 23, "zlib:z-stream-adler-set");
    sym = C_intern (&a, 23, "zlib:z-stream-adler-get");
    sym = C_intern (&a, 26, "zlib:z-stream-reserved-set");
    sym = C_intern (&a, 26, "zlib:z-stream-reserved-get");
    sym = C_intern (&a, 17, "zlib:new-z-stream");
    sym = C_intern (&a, 20, "zlib:delete-z-stream");
    sym = C_intern (&a, 15, "zlib:z-no-flush");
    sym = C_intern (&a, 20, "zlib:z-partial-flush");
    sym = C_intern (&a, 17, "zlib:z-sync-flush");
    sym = C_intern (&a, 17, "zlib:z-full-flush");
    sym = C_intern (&a, 13, "zlib:z-finish");
    sym = C_intern (&a, 9, "zlib:z-ok");
    sym = C_intern (&a, 17, "zlib:z-stream-end");
    sym = C_intern (&a, 16, "zlib:z-need-dict");
    sym = C_intern (&a, 12, "zlib:z-errno");
    sym = C_intern (&a, 19, "zlib:z-stream-error");
    sym = C_intern (&a, 17, "zlib:z-data-error");
    sym = C_intern (&a, 16, "zlib:z-mem-error");
    sym = C_intern (&a, 16, "zlib:z-buf-error");
    sym = C_intern (&a, 20, "zlib:z-version-error");
    sym = C_intern (&a, 21, "zlib:z-no-compression");
    sym = C_intern (&a, 17, "zlib:z-best-speed");
    sym = C_intern (&a, 23, "zlib:z-best-compression");
    sym = C_intern (&a, 26, "zlib:z-default-compression");
    sym = C_intern (&a, 15, "zlib:z-filtered");
    sym = C_intern (&a, 19, "zlib:z-huffman-only");
    sym = C_intern (&a, 23, "zlib:z-default-strategy");
    sym = C_intern (&a, 13, "zlib:z-binary");
    sym = C_intern (&a, 12, "zlib:z-ascii");
    sym = C_intern (&a, 14, "zlib:z-unknown");
    sym = C_intern (&a, 15, "zlib:z-deflated");
    sym = C_intern (&a, 11, "zlib:z-null");
    sym = C_intern (&a, 17, "zlib:zlib-version");
    sym = C_intern (&a, 12, "zlib:deflate");
    sym = C_intern (&a, 16, "zlib:deflate-end");
    sym = C_intern (&a, 12, "zlib:inflate");
    sym = C_intern (&a, 16, "zlib:inflate-end");
    sym = C_intern (&a, 27, "zlib:deflate-set-dictionary");
    sym = C_intern (&a, 17, "zlib:deflate-copy");
    sym = C_intern (&a, 18, "zlib:deflate-reset");
    sym = C_intern (&a, 19, "zlib:deflate-params");
    sym = C_intern (&a, 27, "zlib:inflate-set-dictionary");
    sym = C_intern (&a, 17, "zlib:inflate-sync");
    sym = C_intern (&a, 18, "zlib:inflate-reset");
    sym = C_intern (&a, 13, "zlib:compress");
    sym = C_intern (&a, 14, "zlib:compress2");
    sym = C_intern (&a, 15, "zlib:uncompress");
    sym = C_intern (&a, 11, "zlib:gzopen");
    sym = C_intern (&a, 12, "zlib:gzdopen");
    sym = C_intern (&a, 16, "zlib:gzsetparams");
    sym = C_intern (&a, 11, "zlib:gzread");
    sym = C_intern (&a, 12, "zlib:gzwrite");
    sym = C_intern (&a, 13, "zlib:gzprintf");
    sym = C_intern (&a, 11, "zlib:gzputs");
    sym = C_intern (&a, 11, "zlib:gzgets");
    sym = C_intern (&a, 11, "zlib:gzputc");
    sym = C_intern (&a, 11, "zlib:gzgetc");
    sym = C_intern (&a, 12, "zlib:gzflush");
    sym = C_intern (&a, 11, "zlib:gzseek");
    sym = C_intern (&a, 13, "zlib:gzrewind");
    sym = C_intern (&a, 11, "zlib:gztell");
    sym = C_intern (&a, 10, "zlib:gzeof");
    sym = C_intern (&a, 12, "zlib:gzclose");
    sym = C_intern (&a, 12, "zlib:gzerror");
    sym = C_intern (&a, 12, "zlib:adler32");
    sym = C_intern (&a, 10, "zlib:crc32");
    sym = C_intern (&a, 18, "zlib:deflate-init-");
    sym = C_intern (&a, 18, "zlib:inflate-init-");
    sym = C_intern (&a, 19, "zlib:deflate-init2-");
    sym = C_intern (&a, 19, "zlib:inflate-init2-");
    sym = C_intern (&a, 29, "zlib:internal-state-dummy-set");
    sym = C_intern (&a, 29, "zlib:internal-state-dummy-get");
    sym = C_intern (&a, 23, "zlib:new-internal-state");
    sym = C_intern (&a, 26, "zlib:delete-internal-state");
    sym = C_intern (&a, 12, "zlib:ze-rror");
    sym = C_intern (&a, 23, "zlib:inflate-sync-point");
    sym = C_intern (&a, 18, "zlib:get-crc-table");
      
Much better. The only problem is the identifier zlib:ze-rror, and we are missing zlib:deflate-init and zlib:inflate-init because they are defined as macros (see macro definitions).

Attempt #4

We make sure to rename zError to z_error, and we inline some helper functions for the zlib:...-init macros. We try again.
/* File : example.i */
%module example
%{
/* Put headers and other declarations here */
#include "zlib.h"
%}

%include typemaps.i

%rename(VERSION) ZLIB_VERSION;
%rename(z_error) zError;

%include "zconf.h"
%include "zlib.h"

%inline %{
/* %inline blocks are seen by SWIG and are inserted into the header
   portion of example_wrap.c, so that they are also seen by the C
   compiler. */
int deflate_init(z_streamp strm, int level) {
  return deflateInit(strm,level); /* call macro */
}
int inflate_init(z_streamp strm) {
  return inflateInit(strm); /* call macro */
}
%}


      
The result is:
% swig -chicken -prefix zlib -mixed -I/usr/include example.i
% grep C_intern example_wrap.c
  C_word err = C_intern2 (&a, errorhook);
    sym = C_intern (&a, 18, "zlib:max-mem-level");
    sym = C_intern (&a, 14, "zlib:max-wbits");
    sym = C_intern (&a, 13, "zlib:seek-set");
    sym = C_intern (&a, 13, "zlib:seek-cur");
    sym = C_intern (&a, 13, "zlib:seek-end");
    sym = C_intern (&a, 12, "zlib:version");
    sym = C_intern (&a, 25, "zlib:z-stream-next-in-set");
    sym = C_intern (&a, 25, "zlib:z-stream-next-in-get");
    sym = C_intern (&a, 26, "zlib:z-stream-avail-in-set");
    sym = C_intern (&a, 26, "zlib:z-stream-avail-in-get");
    sym = C_intern (&a, 26, "zlib:z-stream-total-in-set");
    sym = C_intern (&a, 26, "zlib:z-stream-total-in-get");
    sym = C_intern (&a, 26, "zlib:z-stream-next-out-set");
    sym = C_intern (&a, 26, "zlib:z-stream-next-out-get");
    sym = C_intern (&a, 27, "zlib:z-stream-avail-out-set");
    sym = C_intern (&a, 27, "zlib:z-stream-avail-out-get");
    sym = C_intern (&a, 27, "zlib:z-stream-total-out-set");
    sym = C_intern (&a, 27, "zlib:z-stream-total-out-get");
    sym = C_intern (&a, 21, "zlib:z-stream-msg-set");
    sym = C_intern (&a, 21, "zlib:z-stream-msg-get");
    sym = C_intern (&a, 23, "zlib:z-stream-state-set");
    sym = C_intern (&a, 23, "zlib:z-stream-state-get");
    sym = C_intern (&a, 24, "zlib:z-stream-zalloc-set");
    sym = C_intern (&a, 24, "zlib:z-stream-zalloc-get");
    sym = C_intern (&a, 23, "zlib:z-stream-zfree-set");
    sym = C_intern (&a, 23, "zlib:z-stream-zfree-get");
    sym = C_intern (&a, 24, "zlib:z-stream-opaque-set");
    sym = C_intern (&a, 24, "zlib:z-stream-opaque-get");
    sym = C_intern (&a, 27, "zlib:z-stream-data-type-set");
    sym = C_intern (&a, 27, "zlib:z-stream-data-type-get");
    sym = C_intern (&a, 23, "zlib:z-stream-adler-set");
    sym = C_intern (&a, 23, "zlib:z-stream-adler-get");
    sym = C_intern (&a, 26, "zlib:z-stream-reserved-set");
    sym = C_intern (&a, 26, "zlib:z-stream-reserved-get");
    sym = C_intern (&a, 17, "zlib:new-z-stream");
    sym = C_intern (&a, 20, "zlib:delete-z-stream");
    sym = C_intern (&a, 15, "zlib:z-no-flush");
    sym = C_intern (&a, 20, "zlib:z-partial-flush");
    sym = C_intern (&a, 17, "zlib:z-sync-flush");
    sym = C_intern (&a, 17, "zlib:z-full-flush");
    sym = C_intern (&a, 13, "zlib:z-finish");
    sym = C_intern (&a, 9, "zlib:z-ok");
    sym = C_intern (&a, 17, "zlib:z-stream-end");
    sym = C_intern (&a, 16, "zlib:z-need-dict");
    sym = C_intern (&a, 12, "zlib:z-errno");
    sym = C_intern (&a, 19, "zlib:z-stream-error");
    sym = C_intern (&a, 17, "zlib:z-data-error");
    sym = C_intern (&a, 16, "zlib:z-mem-error");
    sym = C_intern (&a, 16, "zlib:z-buf-error");
    sym = C_intern (&a, 20, "zlib:z-version-error");
    sym = C_intern (&a, 21, "zlib:z-no-compression");
    sym = C_intern (&a, 17, "zlib:z-best-speed");
    sym = C_intern (&a, 23, "zlib:z-best-compression");
    sym = C_intern (&a, 26, "zlib:z-default-compression");
    sym = C_intern (&a, 15, "zlib:z-filtered");
    sym = C_intern (&a, 19, "zlib:z-huffman-only");
    sym = C_intern (&a, 23, "zlib:z-default-strategy");
    sym = C_intern (&a, 13, "zlib:z-binary");
    sym = C_intern (&a, 12, "zlib:z-ascii");
    sym = C_intern (&a, 14, "zlib:z-unknown");
    sym = C_intern (&a, 15, "zlib:z-deflated");
    sym = C_intern (&a, 11, "zlib:z-null");
    sym = C_intern (&a, 17, "zlib:zlib-version");
    sym = C_intern (&a, 12, "zlib:deflate");
    sym = C_intern (&a, 16, "zlib:deflate-end");
    sym = C_intern (&a, 12, "zlib:inflate");
    sym = C_intern (&a, 16, "zlib:inflate-end");
    sym = C_intern (&a, 27, "zlib:deflate-set-dictionary");
    sym = C_intern (&a, 17, "zlib:deflate-copy");
    sym = C_intern (&a, 18, "zlib:deflate-reset");
    sym = C_intern (&a, 19, "zlib:deflate-params");
    sym = C_intern (&a, 27, "zlib:inflate-set-dictionary");
    sym = C_intern (&a, 17, "zlib:inflate-sync");
    sym = C_intern (&a, 18, "zlib:inflate-reset");
    sym = C_intern (&a, 13, "zlib:compress");
    sym = C_intern (&a, 14, "zlib:compress2");
    sym = C_intern (&a, 15, "zlib:uncompress");
    sym = C_intern (&a, 11, "zlib:gzopen");
    sym = C_intern (&a, 12, "zlib:gzdopen");
    sym = C_intern (&a, 16, "zlib:gzsetparams");
    sym = C_intern (&a, 11, "zlib:gzread");
    sym = C_intern (&a, 12, "zlib:gzwrite");
    sym = C_intern (&a, 13, "zlib:gzprintf");
    sym = C_intern (&a, 11, "zlib:gzputs");
    sym = C_intern (&a, 11, "zlib:gzgets");
    sym = C_intern (&a, 11, "zlib:gzputc");
    sym = C_intern (&a, 11, "zlib:gzgetc");
    sym = C_intern (&a, 12, "zlib:gzflush");
    sym = C_intern (&a, 11, "zlib:gzseek");
    sym = C_intern (&a, 13, "zlib:gzrewind");
    sym = C_intern (&a, 11, "zlib:gztell");
    sym = C_intern (&a, 10, "zlib:gzeof");
    sym = C_intern (&a, 12, "zlib:gzclose");
    sym = C_intern (&a, 12, "zlib:gzerror");
    sym = C_intern (&a, 12, "zlib:adler32");
    sym = C_intern (&a, 10, "zlib:crc32");
    sym = C_intern (&a, 18, "zlib:deflate-init-");
    sym = C_intern (&a, 18, "zlib:inflate-init-");
    sym = C_intern (&a, 19, "zlib:deflate-init2-");
    sym = C_intern (&a, 19, "zlib:inflate-init2-");
    sym = C_intern (&a, 29, "zlib:internal-state-dummy-set");
    sym = C_intern (&a, 29, "zlib:internal-state-dummy-get");
    sym = C_intern (&a, 23, "zlib:new-internal-state");
    sym = C_intern (&a, 26, "zlib:delete-internal-state");
    sym = C_intern (&a, 12, "zlib:z-error");
    sym = C_intern (&a, 23, "zlib:inflate-sync-point");
    sym = C_intern (&a, 18, "zlib:get-crc-table");
    sym = C_intern (&a, 17, "zlib:deflate-init");
    sym = C_intern (&a, 17, "zlib:inflate-init");
      
Perfect! Now let's integrate this zlib extension into a CHICKEN interpreter. To save some time, in this Examples/chicken/zlib directory:
  1. Backup the original example.i.
  2. Copy and paste the example.i text from above and put it into the file called example.i
  3. Run 'make' as per Building the example.
  4. Run the resultant executable zlib.
The interpreter interaction is as follows:
% ./zlib
zlib

  A SWIG example for the CHICKEN compiler
  Author: Jonah Beckford, February 2003

Scheme Procedures:

zlib:max-mem-level
zlib:max-wbits
zlib:seek-set
zlib:seek-cur
zlib:seek-end
zlib:version
zlib:z-stream-next-in-set
zlib:z-stream-next-in-get
zlib:z-stream-avail-in-set
...
zlib:get-crc-table
zlib:deflate-init
zlib:inflate-init
; This is the CHICKEN interpreter - Version 0, Build 1095 - windows-cygwin-x86
; (c)2000-2003 Felix L. Winkelmann
>>> (define s (zlib:new-z-stream))
>>> s
#<tagged pointer #<c++ "z_stream *">(#<pointer 6d9290>)>
>>> (zlib:z-stream-next-in-get s)
#f
>>> (zlib:z-stream-next-in-set s "some dummy stream data")
Error: Type error. Expected _p_Bytef: "bad argument type"
>>> (exit)
      
Apparently we cannot use Scheme strings as Bytef *. The SWIG manual shows many ways how to handle strings and byte arrays, but to be simplistic, let's just make the Bytef * look like a char *, which is automatically handled as a string by SWIG CHICKEN.

Attempt #5

We make sure to add an %apply construct so that Bytef * is handled the same as char * to SWIG. We try again.
/* File : example.i */
%module example
%{
/* Put headers and other declarations here */
#include "zlib.h"
%}

%include typemaps.i

%rename(VERSION) ZLIB_VERSION;
%rename(z_error) zError;
%apply char * { Bytef * };

%include "zconf.h"
%include "zlib.h"

%inline %{
/* %inline blocks are seen by SWIG and are inserted into the header
   portion of example_wrap.c, so that they are also seen by the C
   compiler. */
int deflate_init(z_streamp strm, int level) {
  return deflateInit(strm,level); /* call macro */
}
int inflate_init(z_streamp strm) {
  return inflateInit(strm); /* call macro */
}
%}
      
Build the example once more.
The interpreter interaction is as follows:
% ./zlib
zlib

  A SWIG example for the CHICKEN compiler
  Author: Jonah Beckford, February 2003

Scheme Procedures:

zlib:max-mem-level
zlib:max-wbits
zlib:seek-set
zlib:seek-cur
zlib:seek-end
zlib:version
zlib:z-stream-next-in-set
zlib:z-stream-next-in-get
zlib:z-stream-avail-in-set
...
zlib:get-crc-table
zlib:deflate-init
zlib:inflate-init
; This is the CHICKEN interpreter - Version 0, Build 1095 - windows-cygwin-x86
; (c)2000-2003 Felix L. Winkelmann
>>> (define s (zlib:new-z-stream))
Init zstream
>>> (zlib:z-stream-zalloc-set s #f) 
>>> (zlib:z-stream-zfree-set s #f)
>>> (zlib:z-stream-opaque-set s #f)
>>> (zlib:deflate-init s (zlib:z-default-compression))
0
Deflate something small so we don't need to loop/stream data
>>> (define in "some dummy data")
>>> (define out (make-string 1000))
>>> (zlib:z-stream-next-in-set s in)
>>> (zlib:z-stream-avail-in-set s (string-length in))
>>> (zlib:z-stream-next-out-set s out)
>>> (zlib:z-stream-avail-out-set s (string-length out))
>>> (zlib:deflate s (zlib:z-finish))
1 ;; (zlib:z-stream-end) == 1, which is good
>>> (zlib:z-stream-total-out-get s)
23.
>>> out
"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "
      
We see the problem ... the compression is occurring as it should, but we cannot see any of the compressed output. This is because when SWIG CHICKEN passes a Scheme string to a C function, it duplicates the string before calling the C function. We want to save the memory address that zlib:z-stream-next-out-set is using, so we can display this later. While we are at it, we can foresee that compress, compress2 and uncompress will all need some finessing to work with mutating strings.

Attempt #6

When we have to finesse strings, we must use typemaps. As well, we define some functions to save and restore the next_out element. We try again.
/* File : example.i */
%module example
%{
/* Put headers and other declarations here */
#include "zlib.h"
%}

%include typemaps.i

%rename(VERSION) ZLIB_VERSION;
%rename(z_error) zError;
%apply char * { Bytef * };
	
/* Allow the sourceLen to be automatically filled in from the length
   of the 'source' string */
%typemap(in) (const Bytef *source, uLong sourceLen)
%{  if (!C_swig_is_string ($input)) {
    swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument $input is not a string");
  }
  $2 = (uLong) C_header_size ($input);
  $1 = C_c_string ($input);
%}

/* Allocate space the size of which is determined by the Scheme
   integer argument, and make a temporary integer so we can set
   destLen. */
%typemap(in) (Bytef *dest, uLongf *destLen) (uLong len)
%{  if (!C_swig_is_fixnum ($input)) {
    swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument $input is not a integer");
  }
  len = (uLong) C_unfix ($input);
  $2 = &len;
  $1 = (char *) malloc (*$2);
%}

/* Return the mutated string as a new object. */
%typemap(argout) (Bytef *dest, uLongf *destLen) 
(C_word *scmstr) 
%{  scmstr = C_alloc (C_SIZEOF_STRING (*$2));
  SWIG_APPEND_VALUE(C_string (&scmstr, *$2, $1));
  free ($1);
%}
	
%include "zconf.h"
%include "zlib.h"
	
/* Ignore destLen as an input argument, and make a temporary integer so
   we can set destLen. */
%typemap(in, numinputs=0) uLongf *destLen (uLong len)
"$1 = &len;";

/* Return a sized string as a new object. */
%typemap(argout)
(void *outstr, uLongf *destLen) (C_word *scmstr) 
%{  scmstr = C_alloc (C_SIZEOF_STRING (*$2));
  SWIG_APPEND_VALUE(C_string (&scmstr, *$2, $1));
%}
	
%inline %{
/* %inline blocks are seen by SWIG and are inserted into the header
   portion of example_wrap.c, so that they are also seen by the C
   compiler. */
int deflate_init(z_streamp strm, int level) {
  return deflateInit(strm,level); /* call macro */
}
int inflate_init(z_streamp strm) {
  return inflateInit(strm); /* call macro */
}
void* z_stream_save_next_out(z_streamp strm) {
  return (void*) strm->next_out;
}
void z_stream_get_next_chunk(z_streamp strm, void *outstr, uLongf *destLen) {
  *destLen = strm->next_out - (Bytef*)outstr;
}
%}
      
And that's it. Try building the entire example from the Makefile. Run ./zlib test-zlib.scm to test it out.