home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / cvs-1.8.7-src.tgz / tar.out / fsf / cvs / src / zlib.c < prev   
C/C++ Source or Header  |  1996-09-28  |  12KB  |  434 lines

  1. /* zlib.c --- interface to the zlib compression library
  2.    Ian Lance Taylor <ian@cygnus.com>
  3.  
  4.    This file is part of GNU CVS.
  5.  
  6.    GNU CVS is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by the
  8.    Free Software Foundation; either version 2, or (at your option) any
  9.    later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  19.  
  20. /* The routines in this file are the interface between the CVS
  21.    client/server support and the zlib compression library.  */
  22.  
  23. #include <assert.h>
  24. #include "cvs.h"
  25. #include "buffer.h"
  26.  
  27. #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
  28.  
  29. #include "zlib.h"
  30.  
  31. /* OS/2 doesn't have EIO.  FIXME: this whole notion of turning
  32.    a different error into EIO strikes me as pretty dubious.  */
  33. #if !defined (EIO)
  34. #define EIO EBADPOS
  35. #endif
  36.  
  37. /* The compression interface is built upon the buffer data structure.
  38.    We provide a buffer type which compresses or decompresses the data
  39.    which passes through it.  An input buffer decompresses the data
  40.    read from an underlying buffer, and an output buffer compresses the
  41.    data before writing it to an underlying buffer.  */
  42.  
  43. /* This structure is the closure field of the buffer.  */
  44.  
  45. struct compress_buffer
  46. {
  47.     /* The underlying buffer.  */
  48.     struct buffer *buf;
  49.     /* The compression information.  */
  50.     z_stream zstr;
  51. };
  52.  
  53. static void compress_error PROTO((int, int, z_stream *, const char *));
  54. static int compress_buffer_input PROTO((void *, char *, int, int, int *));
  55. static int compress_buffer_output PROTO((void *, const char *, int, int *));
  56. static int compress_buffer_flush PROTO((void *));
  57. static int compress_buffer_block PROTO((void *, int));
  58. static int compress_buffer_shutdown_input PROTO((void *));
  59. static int compress_buffer_shutdown_output PROTO((void *));
  60.  
  61. /* Report an error from one of the zlib functions.  */
  62.  
  63. static void
  64. compress_error (status, zstatus, zstr, msg)
  65.      int status;
  66.      int zstatus;
  67.      z_stream *zstr;
  68.      const char *msg;
  69. {
  70.     int hold_errno;
  71.     const char *zmsg;
  72.     char buf[100];
  73.  
  74.     hold_errno = errno;
  75.  
  76.     zmsg = zstr->msg;
  77.     if (zmsg == NULL)
  78.     {
  79.         sprintf (buf, "error %d", zstatus);
  80.     zmsg = buf;
  81.     }
  82.  
  83.     error (status,
  84.        zstatus == Z_ERRNO ? hold_errno : 0,
  85.        "%s: %s", msg, zmsg);
  86. }
  87.  
  88. /* Create a compression buffer.  */
  89.  
  90. struct buffer *
  91. compress_buffer_initialize (buf, input, level, memory)
  92.      struct buffer *buf;
  93.      int input;
  94.      int level;
  95.      void (*memory) PROTO((struct buffer *));
  96. {
  97.     struct compress_buffer *n;
  98.     int zstatus;
  99.  
  100.     n = (struct compress_buffer *) xmalloc (sizeof *n);
  101.     memset (n, 0, sizeof *n);
  102.  
  103.     n->buf = buf;
  104.  
  105.     if (input)
  106.     zstatus = inflateInit (&n->zstr);
  107.     else
  108.     zstatus = deflateInit (&n->zstr, level);
  109.     if (zstatus != Z_OK)
  110.     compress_error (1, zstatus, &n->zstr, "compression initialization");
  111.  
  112.     /* There may already be data buffered on BUF.  For an output
  113.        buffer, this is OK, because these routines will just use the
  114.        buffer routines to append data to the (uncompressed) data
  115.        already on BUF.  An input buffer expects to handle a single
  116.        buffer_data of buffered input to be uncompressed, so that is OK
  117.        provided there is only one buffer.  At present that is all
  118.        there ever will be; if this changes, compress_buffer_input must
  119.        be modified to handle multiple input buffers.  */
  120.     assert (! input || buf->data == NULL || buf->data->next == NULL);
  121.  
  122.     return buf_initialize (input ? compress_buffer_input : NULL,
  123.                input ? NULL : compress_buffer_output,
  124.                input ? NULL : compress_buffer_flush,
  125.                compress_buffer_block,
  126.                (input
  127.                 ? compress_buffer_shutdown_input
  128.                 : compress_buffer_shutdown_output),
  129.                memory,
  130.                n);
  131. }
  132.  
  133. /* Input data from a compression buffer.  */
  134.  
  135. static int
  136. compress_buffer_input (closure, data, need, size, got)
  137.      void *closure;
  138.      char *data;
  139.      int need;
  140.      int size;
  141.      int *got;
  142. {
  143.     struct compress_buffer *cb = (struct compress_buffer *) closure;
  144.     struct buffer_data *bd;
  145.  
  146.     if (cb->buf->input == NULL)
  147.     abort ();
  148.  
  149.     /* We use a single buffer_data structure to buffer up data which
  150.        the z_stream structure won't use yet.  We can safely store this
  151.        on cb->buf->data, because we never call the buffer routines on
  152.        cb->buf; we only call the buffer input routine, since that
  153.        gives us the semantics we want.  As noted in
  154.        compress_buffer_initialize, the buffer_data structure may
  155.        already exist, and hold data which was already read and
  156.        buffered before the decompression began.  */
  157.     bd = cb->buf->data;
  158.     if (bd == NULL)
  159.     {
  160.     bd = ((struct buffer_data *) malloc (sizeof (struct buffer_data)));
  161.     if (bd == NULL)
  162.         return -2;
  163.     bd->text = (char *) malloc (BUFFER_DATA_SIZE);
  164.     if (bd->text == NULL)
  165.     {
  166.         free (bd);
  167.         return -2;
  168.     }
  169.     bd->bufp = bd->text;
  170.     bd->size = 0;
  171.     cb->buf->data = bd;
  172.     }
  173.  
  174.     cb->zstr.avail_out = size;
  175.     cb->zstr.next_out = (Bytef *) data;
  176.  
  177.     while (1)
  178.     {
  179.     int zstatus, sofar, status, nread;
  180.  
  181.     /* First try to inflate any data we already have buffered up.
  182.        This is useful even if we don't have any buffered data,
  183.        because there may be data buffered inside the z_stream
  184.        structure.  */
  185.  
  186.     cb->zstr.avail_in = bd->size;
  187.     cb->zstr.next_in = (Bytef *) bd->bufp;
  188.  
  189.     do
  190.     {
  191.         zstatus = inflate (&cb->zstr, Z_NO_FLUSH);
  192.         if (zstatus == Z_STREAM_END)
  193.         break;
  194.         if (zstatus != Z_OK && zstatus != Z_BUF_ERROR)
  195.         {
  196.         compress_error (0, zstatus, &cb->zstr, "inflate");
  197.         return EIO;
  198.         }
  199.     } while (cb->zstr.avail_in > 0
  200.          && cb->zstr.avail_out > 0);
  201.  
  202.     bd->size = cb->zstr.avail_in;
  203.     bd->bufp = (char *) cb->zstr.next_in;
  204.  
  205.     if (zstatus == Z_STREAM_END)
  206.         return -1;
  207.  
  208.     /* If we have obtained NEED bytes, then return, unless NEED is
  209.            zero and we haven't obtained anything at all.  If NEED is
  210.            zero, we will keep reading from the underlying buffer until
  211.            we either can't read anything, or we have managed to
  212.            inflate at least one byte.  */
  213.     sofar = size - cb->zstr.avail_out;
  214.     if (sofar > 0 && sofar >= need)
  215.         break;
  216.  
  217.     /* All our buffered data should have been processed at this
  218.            point.  */
  219.     assert (bd->size == 0);
  220.  
  221.     /* This will work well in the server, because this call will
  222.        do an unblocked read and fetch all the available data.  In
  223.        the client, this will read a single byte from the stdio
  224.        stream, which will cause us to call inflate once per byte.
  225.        It would be more efficient if we could make a call which
  226.        would fetch all the available bytes, and at least one byte.  */
  227.  
  228.     status = (*cb->buf->input) (cb->buf->closure, bd->text,
  229.                     need > 0 ? 1 : 0,
  230.                     BUFFER_DATA_SIZE, &nread);
  231.     if (status != 0)
  232.         return status;
  233.  
  234.     /* If we didn't read anything, then presumably the buffer is
  235.            in nonblocking mode, and we should just get out now with
  236.            whatever we've inflated.  */
  237.     if (nread == 0)
  238.     {
  239.         assert (need == 0);
  240.         break;
  241.     }
  242.  
  243.     bd->bufp = bd->text;
  244.     bd->size = nread;
  245.     }
  246.  
  247.     *got = size - cb->zstr.avail_out;
  248.  
  249.     return 0;
  250. }
  251.  
  252. /* Output data to a compression buffer.  */
  253.  
  254. static int
  255. compress_buffer_output (closure, data, have, wrote)
  256.      void *closure;
  257.      const char *data;
  258.      int have;
  259.      int *wrote;
  260. {
  261.     struct compress_buffer *cb = (struct compress_buffer *) closure;
  262.  
  263.     cb->zstr.avail_in = have;
  264.     cb->zstr.next_in = (unsigned char *) data;
  265.  
  266.     while (cb->zstr.avail_in > 0)
  267.     {
  268.     char buffer[BUFFER_DATA_SIZE];
  269.     int zstatus;
  270.  
  271.     cb->zstr.avail_out = BUFFER_DATA_SIZE;
  272.     cb->zstr.next_out = (unsigned char *) buffer;
  273.  
  274.     zstatus = deflate (&cb->zstr, Z_NO_FLUSH);
  275.     if (zstatus != Z_OK)
  276.     {
  277.         compress_error (0, zstatus, &cb->zstr, "deflate");
  278.         return EIO;
  279.     }
  280.  
  281.     if (cb->zstr.avail_out != BUFFER_DATA_SIZE)
  282.         buf_output (cb->buf, buffer,
  283.             BUFFER_DATA_SIZE - cb->zstr.avail_out);
  284.     }
  285.  
  286.     *wrote = have;
  287.  
  288.     /* We will only be here because buf_send_output was called on the
  289.        compression buffer.  That means that we should now call
  290.        buf_send_output on the underlying buffer.  */
  291.     return buf_send_output (cb->buf);
  292. }
  293.  
  294. /* Flush a compression buffer.  */
  295.  
  296. static int
  297. compress_buffer_flush (closure)
  298.      void *closure;
  299. {
  300.     struct compress_buffer *cb = (struct compress_buffer *) closure;
  301.  
  302.     cb->zstr.avail_in = 0;
  303.     cb->zstr.next_in = NULL;
  304.  
  305.     while (1)
  306.     {
  307.     char buffer[BUFFER_DATA_SIZE];
  308.     int zstatus;
  309.  
  310.     cb->zstr.avail_out = BUFFER_DATA_SIZE;
  311.     cb->zstr.next_out = (unsigned char *) buffer;
  312.  
  313.     zstatus = deflate (&cb->zstr, Z_SYNC_FLUSH);
  314.  
  315.     /* The deflate function will return Z_BUF_ERROR if it can't do
  316.            anything, which in this case means that all data has been
  317.            flushed.  */
  318.     if (zstatus == Z_BUF_ERROR)
  319.         break;
  320.  
  321.     if (zstatus != Z_OK)
  322.     {
  323.         compress_error (0, zstatus, &cb->zstr, "deflate flush");
  324.         return EIO;
  325.     }
  326.  
  327.     if (cb->zstr.avail_out != BUFFER_DATA_SIZE)
  328.         buf_output (cb->buf, buffer,
  329.             BUFFER_DATA_SIZE - cb->zstr.avail_out);
  330.  
  331.     /* If the deflate function did not fill the output buffer,
  332.            then all data has been flushed.  */
  333.     if (cb->zstr.avail_out > 0)
  334.         break;
  335.     }
  336.  
  337.     /* Now flush the underlying buffer.  Note that if the original
  338.        call to buf_flush passed 1 for the BLOCK argument, then the
  339.        buffer will already have been set into blocking mode, so we
  340.        should always pass 0 here.  */
  341.     return buf_flush (cb->buf, 0);
  342. }
  343.  
  344. /* The block routine for a compression buffer.  */
  345.  
  346. static int
  347. compress_buffer_block (closure, block)
  348.      void *closure;
  349.      int block;
  350. {
  351.     struct compress_buffer *cb = (struct compress_buffer *) closure;
  352.  
  353.     if (block)
  354.     return set_block (cb->buf);
  355.     else
  356.     return set_nonblock (cb->buf);
  357. }
  358.  
  359. /* Shut down an input buffer.  */
  360.  
  361. static int
  362. compress_buffer_shutdown_input (closure)
  363.      void *closure;
  364. {
  365.     struct compress_buffer *cb = (struct compress_buffer *) closure;
  366.     int zstatus;
  367.  
  368.     /* Pick up any trailing data, such as the checksum.  */
  369.     while (1)
  370.     {
  371.     int status, nread;
  372.     char buf[100];
  373.  
  374.     status = compress_buffer_input (cb, buf, 0, sizeof buf, &nread);
  375.     if (status == -1)
  376.         break;
  377.     if (status != 0)
  378.         return status;
  379.     }
  380.  
  381.     zstatus = inflateEnd (&cb->zstr);
  382.     if (zstatus != Z_OK)
  383.     {
  384.     compress_error (0, zstatus, &cb->zstr, "inflateEnd");
  385.     return EIO;
  386.     }
  387.  
  388.     return buf_shutdown (cb->buf);
  389. }
  390.  
  391. /* Shut down an output buffer.  */
  392.  
  393. static int
  394. compress_buffer_shutdown_output (closure)
  395.      void *closure;
  396. {
  397.     struct compress_buffer *cb = (struct compress_buffer *) closure;
  398.     int zstatus, status;
  399.  
  400.     do
  401.     {
  402.     char buffer[BUFFER_DATA_SIZE];
  403.  
  404.     cb->zstr.avail_out = BUFFER_DATA_SIZE;
  405.     cb->zstr.next_out = (unsigned char *) buffer;
  406.  
  407.     zstatus = deflate (&cb->zstr, Z_FINISH);
  408.     if (zstatus != Z_OK && zstatus != Z_STREAM_END)
  409.     {
  410.         compress_error (0, zstatus, &cb->zstr, "deflate finish");
  411.         return EIO;
  412.     }
  413.  
  414.     if (cb->zstr.avail_out != BUFFER_DATA_SIZE)
  415.         buf_output (cb->buf, buffer,
  416.             BUFFER_DATA_SIZE - cb->zstr.avail_out);
  417.     } while (zstatus != Z_STREAM_END);
  418.  
  419.     zstatus = deflateEnd (&cb->zstr);
  420.     if (zstatus != Z_OK)
  421.     {
  422.     compress_error (0, zstatus, &cb->zstr, "deflateEnd");
  423.     return EIO;
  424.     }
  425.  
  426.     status = buf_flush (cb->buf, 1);
  427.     if (status != 0)
  428.     return status;
  429.  
  430.     return buf_shutdown (cb->buf);
  431. }
  432.  
  433. #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */
  434.