src/zlibmem.c
l_uint8 *
zlibCompress(l_uint8  *datain,
             size_t    nin,
             size_t   *pnout)
{
l_uint8  *dataout;
l_int32   status;
size_t    nbytes;
l_uint8  *bufferin, *bufferout;
BBUFFER  *bbin, *bbout;
z_stream  z;

    PROCNAME("zlibCompress");

    if (!datain)
        return (l_uint8 *)ERROR_PTR("datain not defined", procName, NULL);

        /* Set up fixed size buffers used in z_stream */
    if ((bufferin = (l_uint8 *)CALLOC(L_BUF_SIZE, sizeof(l_uint8))) == NULL)
        return (l_uint8 *)ERROR_PTR("bufferin not made", procName, NULL);
    if ((bufferout = (l_uint8 *)CALLOC(L_BUF_SIZE, sizeof(l_uint8))) == NULL)
        return (l_uint8 *)ERROR_PTR("bufferout not made", procName, NULL);

        /* Set up bbuffers and load bbin with the data */
    if ((bbin = bbufferCreate(datain, nin)) == NULL)
        return (l_uint8 *)ERROR_PTR("bbin not made", procName, NULL);
    if ((bbout = bbufferCreate(NULL, 0)) == NULL)
        return (l_uint8 *)ERROR_PTR("bbout not made", procName, NULL);

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

    z.next_in = bufferin;
    z.avail_in = 0;
    z.next_out = bufferout;
    z.avail_out = L_BUF_SIZE;

    deflateInit(&z, ZLIB_COMPRESSION_LEVEL);

    for ( ; ; ) {
        if (z.avail_in == 0) {
            z.next_in = bufferin;
            bbufferWrite(bbin, bufferin, L_BUF_SIZE, &nbytes);
/*            fprintf(stderr, " wrote %d bytes to bufferin\n", nbytes); */
            z.avail_in = nbytes;
        }
        if (z.avail_in == 0)
            break;
        status = deflate(&z, Z_SYNC_FLUSH);
/*        fprintf(stderr, " status is %d, bytesleft = %d, totalout = %d\n",
                  status, z.avail_out, z.total_out); */
        nbytes = L_BUF_SIZE - z.avail_out;
        if (nbytes) {
            bbufferRead(bbout, bufferout, nbytes);
/*            fprintf(stderr, " read %d bytes from bufferout\n", nbytes); */
        }
        z.next_out = bufferout;
        z.avail_out = L_BUF_SIZE;
    }

    deflateEnd(&z);

    bbufferDestroy(&bbin);
    dataout = bbufferDestroyAndSaveData(&bbout, pnout);

    FREE(bufferin);
    FREE(bufferout);
    return dataout;
}
/*!
 *  zlibCompress()
 *
 *      Input:  datain (byte buffer with input data)
 *              nin    (number of bytes of input data)
 *              &nout  (<return> number of bytes of output data)
 *      Return: dataout (compressed data), or null on error
 *
 *  Notes:
 *      (1) We repeatedly read in and fill up an input buffer,
 *          compress the data, and read it back out.  zlib
 *          uses two byte buffers internally in the z_stream
 *          data structure.  We use the bbuffers to feed data
 *          into the fixed bufferin, and feed it out of bufferout,
 *          in the same way that a pair of streams would normally
 *          be used if the data were being read from one file
 *          and written to another.  This is done iteratively,
 *          compressing L_BUF_SIZE bytes of input data at a time.
 */
zlibCompress()
Input:  datain (byte buffer with input data)
nin    (number of bytes of input data)
&nout  ( number of bytes of output data)
Return: dataout (compressed data), or null on error

(1) We repeatedly read in and fill up an input buffer,
compress the data, and read it back out.  zlib
uses two byte buffers internally in the z_stream
data structure.  We use the bbuffers to feed data
into the fixed bufferin, and feed it out of bufferout,
in the same way that a pair of streams would normally
be used if the data were being read from one file
and written to another.  This is done iteratively,
compressing L_BUF_SIZE bytes of input data at a time.
zlibCompress () Input : datain ( byte buffer with input data ) nin ( number of bytes of input data ) &nout ( number of bytes of output data ) Return : dataout ( compressed data ) or null on error ( 1 ) We repeatedly read in and fill up an input buffer compress the data and read it back out . zlib uses two byte buffers internally in the z_stream data structure . We use the bbuffers to feed data into the fixed bufferin and feed it out of bufferout in the same way that a pair of streams would normally be used if the data were being read from one file and written to another . This is done iteratively compressing L_BUF_SIZE bytes of input data at a time .
l_uint8 *
zlibCompress(l_uint8  *datain,
             size_t    nin,
             size_t   *pnout)
{
l_uint8  *dataout;
l_int32   status;
size_t    nbytes;
l_uint8  *bufferin, *bufferout;
BBUFFER  *bbin, *bbout;
z_stream  z;
    PROCNAME("zlibCompress");
    if (!datain)
        return (l_uint8 *)ERROR_PTR("datain not defined", procName, NULL);
    if ((bufferin = (l_uint8 *)CALLOC(L_BUF_SIZE, sizeof(l_uint8))) == NULL)
        return (l_uint8 *)ERROR_PTR("bufferin not made", procName, NULL);
    if ((bufferout = (l_uint8 *)CALLOC(L_BUF_SIZE, sizeof(l_uint8))) == NULL)
        return (l_uint8 *)ERROR_PTR("bufferout not made", procName, NULL);
    if ((bbin = bbufferCreate(datain, nin)) == NULL)
        return (l_uint8 *)ERROR_PTR("bbin not made", procName, NULL);
    if ((bbout = bbufferCreate(NULL, 0)) == NULL)
        return (l_uint8 *)ERROR_PTR("bbout not made", procName, NULL);
    z.zalloc = (alloc_func)0;
    z.zfree = (free_func)0;
    z.opaque = (voidpf)0;
    z.next_in = bufferin;
    z.avail_in = 0;
    z.next_out = bufferout;
    z.avail_out = L_BUF_SIZE;
    deflateInit(&z, ZLIB_COMPRESSION_LEVEL);
    for ( ; ; ) {
        if (z.avail_in == 0) {
            z.next_in = bufferin;
            bbufferWrite(bbin, bufferin, L_BUF_SIZE, &nbytes);
            z.avail_in = nbytes;
        }
        if (z.avail_in == 0)
            break;
        status = deflate(&z, Z_SYNC_FLUSH);
        nbytes = L_BUF_SIZE - z.avail_out;
        if (nbytes) {
            bbufferRead(bbout, bufferout, nbytes);
        }
        z.next_out = bufferout;
        z.avail_out = L_BUF_SIZE;
    }
    deflateEnd(&z);
    bbufferDestroy(&bbin);
    dataout = bbufferDestroyAndSaveData(&bbout, pnout);
    FREE(bufferin);
    FREE(bufferout);
    return dataout;
}
l_uint8 * zlibCompress ( l_uint8 * datain , size_t nin , size_t * pnout ) { l_uint8 * dataout ; l_int32 status ; size_t nbytes ; l_uint8 * bufferin , * bufferout ; BBUFFER * bbin , * bbout ; z_stream z ; PROCNAME ( " " ) ; if ( ! datain ) return ( l_uint8 * ) ERROR_PTR ( " " , procName , NULL ) ; if ( ( bufferin = ( l_uint8 * ) CALLOC ( L_BUF_SIZE , sizeof ( l_uint8 ) ) ) == NULL ) return ( l_uint8 * ) ERROR_PTR ( " " , procName , NULL ) ; if ( ( bufferout = ( l_uint8 * ) CALLOC ( L_BUF_SIZE , sizeof ( l_uint8 ) ) ) == NULL ) return ( l_uint8 * ) ERROR_PTR ( " " , procName , NULL ) ; if ( ( bbin = bbufferCreate ( datain , nin ) ) == NULL ) return ( l_uint8 * ) ERROR_PTR ( " " , procName , NULL ) ; if ( ( bbout = bbufferCreate ( NULL , 0 ) ) == NULL ) return ( l_uint8 * ) ERROR_PTR ( " " , procName , NULL ) ; z . zalloc = ( alloc_func ) 0 ; z . zfree = ( free_func ) 0 ; z . opaque = ( voidpf ) 0 ; z . next_in = bufferin ; z . avail_in = 0 ; z . next_out = bufferout ; z . avail_out = L_BUF_SIZE ; deflateInit ( & z , ZLIB_COMPRESSION_LEVEL ) ; for ( ; ; ) { if ( z . avail_in == 0 ) { z . next_in = bufferin ; bbufferWrite ( bbin , bufferin , L_BUF_SIZE , & nbytes ) ; z . avail_in = nbytes ; } if ( z . avail_in == 0 ) break ; status = deflate ( & z , Z_SYNC_FLUSH ) ; nbytes = L_BUF_SIZE - z . avail_out ; if ( nbytes ) { bbufferRead ( bbout , bufferout , nbytes ) ; } z . next_out = bufferout ; z . avail_out = L_BUF_SIZE ; } deflateEnd ( & z ) ; bbufferDestroy ( & bbin ) ; dataout = bbufferDestroyAndSaveData ( & bbout , pnout ) ; FREE ( bufferin ) ; FREE ( bufferout ) ; return dataout ; }
zlibCompress()
      Input:  datain (byte buffer with input data)
              nin    (number of bytes of input data)
              &nout  (<return> number of bytes of output data)
      Return: dataout (compressed data), or null on error
zlibCompress () Input : datain ( byte buffer with input data ) nin ( number of bytes of input data ) &nout ( <return > number of bytes of output data ) Return : dataout ( compressed data ) or null on error
/* Set up fixed size buffers used in z_stream */ /* Set up bbuffers and load bbin with the data */ /*            fprintf(stderr, " wrote %d bytes to bufferin\n", nbytes); */ /*        fprintf(stderr, " status is %d, bytesleft = %d, totalout = %d\n",
                  status, z.avail_out, z.total_out); */ /*            fprintf(stderr, " read %d bytes from bufferout\n", nbytes); */