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/* The routines in this file are the interface between the CVS
17   client/server support and the zlib compression library.  */
18
19#include "cvs.h"
20#include "buffer.h"
21#include "pagealign_alloc.h"
22
23#if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
24
25#if HAVE_ZLIB_H
26# include <zlib.h>
27#else
28# include "zlib.h"
29#endif
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
45struct compress_buffer
46{
47    /* The underlying buffer.  */
48    struct buffer *buf;
49
50    /* The compression information.  */
51    z_stream zstr;
52    int level;
53};
54
55static void compress_error (int, int, z_stream *, const char *);
56static int compress_buffer_input (void *, char *, size_t, size_t, size_t *);
57static int compress_buffer_output (void *, const char *, size_t, size_t *);
58static int compress_buffer_flush (void *);
59static int compress_buffer_block (void *, bool);
60static int compress_buffer_get_fd (void *);
61static int compress_buffer_shutdown_input (struct buffer *);
62static int compress_buffer_shutdown_output (struct buffer *);
63
64/* Report an error from one of the zlib functions.  */
65
66static void
67compress_error (int status, int zstatus, z_stream *zstr, const char *msg)
68{
69    int hold_errno;
70    const char *zmsg;
71    char buf[100];
72
73    hold_errno = errno;
74
75    zmsg = zstr->msg;
76    if (zmsg == NULL)
77    {
78        sprintf (buf, "error %d", zstatus);
79	zmsg = buf;
80    }
81
82    error (status,
83	   zstatus == Z_ERRNO ? hold_errno : 0,
84	   "%s: %s", msg, zmsg);
85}
86
87
88
89/* Create a compression buffer.  */
90struct buffer *
91compress_buffer_initialize (struct buffer *buf, int input, int level,
92                            void (*memory) (struct buffer *))
93{
94    struct compress_buffer *n;
95    int zstatus;
96
97    n = xmalloc (sizeof *n);
98    memset (n, 0, sizeof *n);
99
100    n->buf = buf;
101    n->level = level;
102
103    if (input)
104	zstatus = inflateInit (&n->zstr);
105    else
106	zstatus = deflateInit (&n->zstr, level);
107    if (zstatus != Z_OK)
108	compress_error (1, zstatus, &n->zstr, "compression initialization");
109
110    /* There may already be data buffered on BUF.  For an output
111       buffer, this is OK, because these routines will just use the
112       buffer routines to append data to the (uncompressed) data
113       already on BUF.  An input buffer expects to handle a single
114       buffer_data of buffered input to be uncompressed, so that is OK
115       provided there is only one buffer.  At present that is all
116       there ever will be; if this changes, compress_buffer_input must
117       be modified to handle multiple input buffers.  */
118    assert (! input || buf->data == NULL || buf->data->next == NULL);
119
120    return buf_initialize (input ? compress_buffer_input : NULL,
121			   input ? NULL : compress_buffer_output,
122			   input ? NULL : compress_buffer_flush,
123			   compress_buffer_block, compress_buffer_get_fd,
124			   (input
125			    ? compress_buffer_shutdown_input
126			    : compress_buffer_shutdown_output),
127			   memory,
128			   n);
129}
130
131
132
133/* Input data from a compression buffer.  */
134static int
135compress_buffer_input (void *closure, char *data, size_t need, size_t size,
136		       size_t *got)
137{
138    struct compress_buffer *cb = closure;
139    struct buffer_data *bd;
140
141    assert (cb->buf->input);
142
143    /* We use a single buffer_data structure to buffer up data which
144       the z_stream structure won't use yet.  We can safely store this
145       on cb->buf->data, because we never call the buffer routines on
146       cb->buf; we only call the buffer input routine, since that
147       gives us the semantics we want.  As noted in
148       compress_buffer_initialize, the buffer_data structure may
149       already exist, and hold data which was already read and
150       buffered before the decompression began.  */
151    bd = cb->buf->data;
152    if (bd == NULL)
153    {
154	bd = xmalloc (sizeof (struct buffer_data));
155	if (bd == NULL)
156	    return -2;
157	bd->text = pagealign_xalloc (BUFFER_DATA_SIZE);
158	if (bd->text == NULL)
159	{
160	    free (bd);
161	    return -2;
162	}
163	bd->bufp = bd->text;
164	bd->size = 0;
165	cb->buf->data = bd;
166    }
167
168    cb->zstr.avail_out = size;
169    cb->zstr.next_out = (Bytef *) data;
170
171    while (1)
172    {
173	int zstatus, sofar, status;
174	size_t nread;
175
176	/* First try to inflate any data we already have buffered up.
177	   This is useful even if we don't have any buffered data,
178	   because there may be data buffered inside the z_stream
179	   structure.  */
180
181	cb->zstr.avail_in = bd->size;
182	cb->zstr.next_in = (Bytef *) bd->bufp;
183
184	do
185	{
186	    zstatus = inflate (&cb->zstr, Z_NO_FLUSH);
187	    if (zstatus == Z_STREAM_END)
188		break;
189	    if (zstatus != Z_OK && zstatus != Z_BUF_ERROR)
190	    {
191		compress_error (0, zstatus, &cb->zstr, "inflate");
192		return EIO;
193	    }
194	} while (cb->zstr.avail_in > 0
195		 && cb->zstr.avail_out > 0);
196
197	bd->size = cb->zstr.avail_in;
198	bd->bufp = (char *) cb->zstr.next_in;
199
200	sofar = size - cb->zstr.avail_out;
201
202	if (zstatus == Z_STREAM_END)
203	{
204	    /* If we read any data, then return it, relying on the fact that
205	     * we will get Z_STREAM_END on the next read too.
206	     */
207	    if (sofar > 0) break;
208
209	    /* Otherwise, return EOF.  */
210	    return -1;
211	}
212
213	/* If we have obtained NEED bytes, then return, unless NEED is
214           zero and we haven't obtained anything at all.  If NEED is
215           zero, we will attempt at least one nonblocking read and see if
216	   we can inflate anything then.  */
217	if (sofar > 0 && sofar >= need)
218	    break;
219
220	/* All our buffered data should have been processed at this
221           point.  */
222	assert (bd->size == 0);
223
224	/* This will work well in the server, because this call will
225	   do an unblocked read and fetch all the available data.  In
226	   the client, this will read a single byte from the stdio
227	   stream, which will cause us to call inflate once per byte.
228	   It would be more efficient if we could make a call which
229	   would fetch all the available bytes, and at least one byte.  */
230
231	status = (*cb->buf->input) (cb->buf->closure, bd->text,
232				    need > 0, BUFFER_DATA_SIZE, &nread);
233
234	if (status == -2)
235	    /* Don't try to recover from memory allcoation errors.  */
236	    return status;
237
238	if (status != 0)
239	{
240	    /* If we read any data, then return it, relying on the fact that
241	     * we will get the same error reading the underlying buffer
242	     * on the next read too.
243	     */
244	    if (sofar > 0) break;
245
246	    /* Otherwise, return EOF.  */
247	    return status;
248	}
249
250	/* If we didn't read anything, then presumably the buffer is
251           in nonblocking mode, and we should just get out now with
252           whatever we've inflated.  */
253	if (nread == 0)
254	{
255	    assert (need == 0);
256	    break;
257	}
258
259	bd->bufp = bd->text;
260	bd->size = nread;
261    }
262
263    *got = size - cb->zstr.avail_out;
264
265    return 0;
266}
267
268
269
270extern int gzip_level;
271
272/* Output data to a compression buffer.
273 *
274 * GLOBALS
275 *   gzip_level		If GZIP_LEVEL has changed to a value different from
276 *			CLOSURE->level, then set the compression level on the
277 *			stream to the new value.
278 */
279static int
280compress_buffer_output (void *closure, const char *data, size_t have,
281			size_t *wrote)
282{
283    struct compress_buffer *cb = closure;
284
285    /* This is only used within the while loop below, but allocated here for
286     * efficiency.
287     */
288    static char *buffer = NULL;
289    if (!buffer)
290	buffer = pagealign_xalloc (BUFFER_DATA_SIZE);
291
292    if (cb->level != gzip_level)
293    {
294	cb->level = gzip_level;
295	deflateParams (&cb->zstr, gzip_level, Z_DEFAULT_STRATEGY);
296    }
297
298    cb->zstr.avail_in = have;
299    cb->zstr.next_in = (unsigned char *) data;
300
301    while (cb->zstr.avail_in > 0)
302    {
303	int zstatus;
304
305	cb->zstr.avail_out = BUFFER_DATA_SIZE;
306	cb->zstr.next_out = (unsigned char *) buffer;
307
308	zstatus = deflate (&cb->zstr, Z_NO_FLUSH);
309	if (zstatus != Z_OK)
310	{
311	    compress_error (0, zstatus, &cb->zstr, "deflate");
312	    return EIO;
313	}
314
315	if (cb->zstr.avail_out != BUFFER_DATA_SIZE)
316	    buf_output (cb->buf, buffer,
317			BUFFER_DATA_SIZE - cb->zstr.avail_out);
318    }
319
320    *wrote = have;
321
322    /* We will only be here because buf_send_output was called on the
323       compression buffer.  That means that we should now call
324       buf_send_output on the underlying buffer.  */
325    return buf_send_output (cb->buf);
326}
327
328
329
330/* Flush a compression buffer.  */
331static int
332compress_buffer_flush (void *closure)
333{
334    struct compress_buffer *cb = closure;
335
336    /* This is only used within the while loop below, but allocated here for
337     * efficiency.
338     */
339    static char *buffer = NULL;
340    if (!buffer)
341	buffer = pagealign_xalloc (BUFFER_DATA_SIZE);
342
343    cb->zstr.avail_in = 0;
344    cb->zstr.next_in = NULL;
345
346    while (1)
347    {
348	int zstatus;
349
350	cb->zstr.avail_out = BUFFER_DATA_SIZE;
351	cb->zstr.next_out = (unsigned char *) buffer;
352
353	zstatus = deflate (&cb->zstr, Z_SYNC_FLUSH);
354
355	/* The deflate function will return Z_BUF_ERROR if it can't do
356           anything, which in this case means that all data has been
357           flushed.  */
358	if (zstatus == Z_BUF_ERROR)
359	    break;
360
361	if (zstatus != Z_OK)
362	{
363	    compress_error (0, zstatus, &cb->zstr, "deflate flush");
364	    return EIO;
365	}
366
367	if (cb->zstr.avail_out != BUFFER_DATA_SIZE)
368	    buf_output (cb->buf, buffer,
369			BUFFER_DATA_SIZE - cb->zstr.avail_out);
370
371	/* If the deflate function did not fill the output buffer,
372           then all data has been flushed.  */
373	if (cb->zstr.avail_out > 0)
374	    break;
375    }
376
377    /* Now flush the underlying buffer.  Note that if the original
378       call to buf_flush passed 1 for the BLOCK argument, then the
379       buffer will already have been set into blocking mode, so we
380       should always pass 0 here.  */
381    return buf_flush (cb->buf, 0);
382}
383
384
385
386/* The block routine for a compression buffer.  */
387static int
388compress_buffer_block (void *closure, bool block)
389{
390    struct compress_buffer *cb = closure;
391
392    if (block)
393	return set_block (cb->buf);
394    else
395	return set_nonblock (cb->buf);
396}
397
398
399
400/* Return the file descriptor underlying any child buffers.  */
401static int
402compress_buffer_get_fd (void *closure)
403{
404    struct compress_buffer *cb = closure;
405    return buf_get_fd (cb->buf);
406}
407
408
409
410/* Shut down an input buffer.  */
411static int
412compress_buffer_shutdown_input (struct buffer *buf)
413{
414    struct compress_buffer *cb = buf->closure;
415    int zstatus;
416
417    /* Don't make any attempt to pick up trailing data since we are shutting
418     * down.  If the client doesn't know we are shutting down, we might not
419     * see the EOF we are expecting.
420     */
421
422    zstatus = inflateEnd (&cb->zstr);
423    if (zstatus != Z_OK)
424    {
425	compress_error (0, zstatus, &cb->zstr, "inflateEnd");
426	return EIO;
427    }
428
429    return buf_shutdown (cb->buf);
430}
431
432
433
434/* Shut down an output buffer.  */
435static int
436compress_buffer_shutdown_output (struct buffer *buf)
437{
438    struct compress_buffer *cb = buf->closure;
439    int zstatus, status;
440
441    /* This is only used within the while loop below, but allocated here for
442     * efficiency.
443     */
444    static char *buffer = NULL;
445    if (!buffer)
446	buffer = pagealign_xalloc (BUFFER_DATA_SIZE);
447
448    do
449    {
450	cb->zstr.avail_out = BUFFER_DATA_SIZE;
451	cb->zstr.next_out = (unsigned char *) buffer;
452
453	zstatus = deflate (&cb->zstr, Z_FINISH);
454	if (zstatus != Z_OK && zstatus != Z_STREAM_END)
455	{
456	    compress_error (0, zstatus, &cb->zstr, "deflate finish");
457	    return EIO;
458	}
459
460	if (cb->zstr.avail_out != BUFFER_DATA_SIZE)
461	    buf_output (cb->buf, buffer,
462			BUFFER_DATA_SIZE - cb->zstr.avail_out);
463    } while (zstatus != Z_STREAM_END);
464
465    zstatus = deflateEnd (&cb->zstr);
466    if (zstatus != Z_OK)
467    {
468	compress_error (0, zstatus, &cb->zstr, "deflateEnd");
469	return EIO;
470    }
471
472    status = buf_flush (cb->buf, 1);
473    if (status != 0)
474	return status;
475
476    return buf_shutdown (cb->buf);
477}
478
479
480
481/* Here is our librarified gzip implementation.  It is very minimal
482   but attempts to be RFC1952 compliant.  */
483
484/* GZIP ID byte values */
485#define GZIP_ID1	31
486#define GZIP_ID2	139
487
488/* Compression methods */
489#define GZIP_CDEFLATE	8
490
491/* Flags */
492#define GZIP_FTEXT	1
493#define GZIP_FHCRC	2
494#define GZIP_FEXTRA	4
495#define GZIP_FNAME	8
496#define GZIP_FCOMMENT	16
497
498/* BUF should contain SIZE bytes of gzipped data (RFC1952/RFC1951).
499   We are to uncompress the data and write the result to the file
500   descriptor FD.  If something goes wrong, give a nonfatal error message
501   mentioning FULLNAME as the name of the file for FD.  Return 1 if
502   it is an error we can't recover from.  */
503
504int
505gunzip_and_write (int fd, const char *fullname, unsigned char *buf,
506		  size_t size)
507{
508    size_t pos;
509    z_stream zstr;
510    int zstatus;
511    unsigned char outbuf[32768];
512    unsigned long crc;
513
514    if (size < 10)
515    {
516	error (0, 0, "gzipped data too small - lacks complete header");
517	return 1;
518    }
519    if (buf[0] != GZIP_ID1 || buf[1] != GZIP_ID2)
520    {
521	error (0, 0, "gzipped data does not start with gzip identification");
522	return 1;
523    }
524    if (buf[2] != GZIP_CDEFLATE)
525    {
526	error (0, 0, "only the deflate compression method is supported");
527	return 1;
528    }
529
530    /* Skip over the fixed header, and then skip any of the variable-length
531       fields.  As we skip each field, we keep pos <= size. The checks
532       on positions and lengths are really checks for malformed or
533       incomplete gzip data.  */
534    pos = 10;
535    if (buf[3] & GZIP_FEXTRA)
536    {
537	if (pos + 2 >= size)
538	{
539	    error (0, 0, "%s lacks proper gzip XLEN field", fullname);
540	    return 1;
541	}
542	pos += buf[pos] + (buf[pos + 1] << 8) + 2;
543	if (pos > size)
544	{
545	    error (0, 0, "%s lacks proper gzip \"extra field\"", fullname);
546	    return 1;
547	}
548
549    }
550    if (buf[3] & GZIP_FNAME)
551    {
552	unsigned char *p = memchr(buf + pos, '\0', size - pos);
553	if (p == NULL)
554	{
555	    error (0, 0, "%s has bad gzip filename field", fullname);
556	    return 1;
557	}
558	pos = p - buf + 1;
559    }
560    if (buf[3] & GZIP_FCOMMENT)
561    {
562	unsigned char *p = memchr(buf + pos, '\0', size - pos);
563	if (p == NULL)
564	{
565	    error (0, 0, "%s has bad gzip comment field", fullname);
566	    return 1;
567	}
568	pos = p - buf + 1;
569    }
570    if (buf[3] & GZIP_FHCRC)
571    {
572	pos += 2;
573	if (pos > size)
574	{
575	    error (0, 0, "%s has bad gzip CRC16 field", fullname);
576	    return 1;
577	}
578    }
579
580    /* There could be no data to decompress - check and short circuit.  */
581    if (pos >= size)
582    {
583	error (0, 0, "gzip data incomplete for %s (no data)", fullname);
584	return 1;
585    }
586
587    memset (&zstr, 0, sizeof zstr);
588    /* Passing a negative argument tells zlib not to look for a zlib
589       (RFC1950) header.  This is an undocumented feature; I suppose if
590       we wanted to be anal we could synthesize a header instead,
591       but why bother?  */
592    zstatus = inflateInit2 (&zstr, -15);
593
594    if (zstatus != Z_OK)
595	compress_error (1, zstatus, &zstr, fullname);
596
597    /* I don't see why we should have to include the 8 byte trailer in
598       avail_in.  But I see that zlib/gzio.c does, and it seemed to fix
599       a fairly rare bug in which we'd get a Z_BUF_ERROR for no obvious
600       reason.  */
601    zstr.avail_in = size - pos;
602    zstr.next_in = buf + pos;
603
604    crc = crc32 (0, NULL, 0);
605
606    do
607    {
608	zstr.avail_out = sizeof (outbuf);
609	zstr.next_out = outbuf;
610	zstatus = inflate (&zstr, Z_NO_FLUSH);
611	if (zstatus != Z_STREAM_END && zstatus != Z_OK)
612	{
613	    compress_error (0, zstatus, &zstr, fullname);
614	    return 1;
615	}
616	if (write (fd, outbuf, sizeof (outbuf) - zstr.avail_out) < 0)
617	{
618	    error (0, errno, "writing decompressed file %s", fullname);
619	    return 1;
620	}
621	crc = crc32 (crc, outbuf, sizeof (outbuf) - zstr.avail_out);
622    } while (zstatus != Z_STREAM_END);
623    zstatus = inflateEnd (&zstr);
624    if (zstatus != Z_OK)
625	compress_error (0, zstatus, &zstr, fullname);
626
627    /* Check that there is still 8 trailer bytes remaining (CRC32
628       and ISIZE).  Check total decomp. data, plus header len (pos)
629       against input buffer total size.  */
630    pos += zstr.total_in;
631    if (size - pos != 8)
632    {
633	error (0, 0, "gzip data incomplete for %s (no trailer)", fullname);
634	return 1;
635    }
636
637    if (crc != ((unsigned long)buf[pos]
638		+ ((unsigned long)buf[pos + 1] << 8)
639		+ ((unsigned long)buf[pos + 2] << 16)
640		+ ((unsigned long)buf[pos + 3] << 24)))
641    {
642	error (0, 0, "CRC error uncompressing %s", fullname);
643	return 1;
644    }
645
646    if (zstr.total_out != ((unsigned long)buf[pos + 4]
647			   + ((unsigned long)buf[pos + 5] << 8)
648			   + ((unsigned long)buf[pos + 6] << 16)
649			   + ((unsigned long)buf[pos + 7] << 24)))
650    {
651	error (0, 0, "invalid length uncompressing %s", fullname);
652	return 1;
653    }
654
655    return 0;
656}
657
658/* Read all of FD and put the gzipped data (RFC1952/RFC1951) into *BUF,
659   replacing previous contents of *BUF.  *BUF is xmalloc'd and *SIZE is
660   its allocated size.  Put the actual number of bytes of data in
661   *LEN.  If something goes wrong, give a nonfatal error mentioning
662   FULLNAME as the name of the file for FD, and return 1 if we can't
663   recover from it).  LEVEL is the compression level (1-9).  */
664
665int
666read_and_gzip (int fd, const char *fullname, unsigned char **buf, size_t *size,
667               size_t *len, int level)
668{
669    z_stream zstr;
670    int zstatus;
671    unsigned char inbuf[8192];
672    int nread;
673    unsigned long crc;
674
675    if (*size < 1024)
676    {
677	unsigned char *newbuf;
678
679	*size = 1024;
680	newbuf = xrealloc (*buf, *size);
681	if (newbuf == NULL)
682	{
683	    error (0, 0, "out of memory");
684	    return 1;
685	}
686	*buf = newbuf;
687    }
688    (*buf)[0] = GZIP_ID1;
689    (*buf)[1] = GZIP_ID2;
690    (*buf)[2] = GZIP_CDEFLATE;
691    (*buf)[3] = 0;
692    (*buf)[4] = (*buf)[5] = (*buf)[6] = (*buf)[7] = 0;
693    /* Could set this based on level, but why bother?  */
694    (*buf)[8] = 0;
695    (*buf)[9] = 255;
696
697    memset (&zstr, 0, sizeof zstr);
698    zstatus = deflateInit2 (&zstr, level, Z_DEFLATED, -15, 8,
699			    Z_DEFAULT_STRATEGY);
700    crc = crc32 (0, NULL, 0);
701    if (zstatus != Z_OK)
702    {
703	compress_error (0, zstatus, &zstr, fullname);
704	return 1;
705    }
706
707    /* Adjust for 10-byte output header (filled in above) */
708    zstr.total_out = 10;
709    zstr.avail_out = *size - 10;
710    zstr.next_out = *buf + 10;
711
712    while (1)
713    {
714	int finish = 0;
715
716	nread = read (fd, inbuf, sizeof inbuf);
717	if (nread < 0)
718	{
719	    error (0, errno, "cannot read %s", fullname);
720	    return 1;
721	}
722	else if (nread == 0)
723	    /* End of file.  */
724	    finish = 1;
725	crc = crc32 (crc, inbuf, nread);
726	zstr.next_in = inbuf;
727	zstr.avail_in = nread;
728
729	do
730	{
731	    /* I don't see this documented anywhere, but deflate seems
732	       to tend to dump core sometimes if we pass it Z_FINISH and
733	       a small (e.g. 2147 byte) avail_out.  So we insist on at
734	       least 4096 bytes (that is what zlib/gzio.c uses).  */
735
736	    if (zstr.avail_out < 4096)
737	    {
738		unsigned char *newbuf;
739
740		assert(zstr.avail_out + zstr.total_out == *size);
741		assert(zstr.next_out == *buf + zstr.total_out);
742		*size *= 2;
743		newbuf = xrealloc (*buf, *size);
744		if (newbuf == NULL)
745		{
746		    error (0, 0, "out of memory");
747		    return 1;
748		}
749		*buf = newbuf;
750		zstr.next_out = *buf + zstr.total_out;
751		zstr.avail_out = *size - zstr.total_out;
752		assert(zstr.avail_out + zstr.total_out == *size);
753		assert(zstr.next_out == *buf + zstr.total_out);
754	    }
755
756	    zstatus = deflate (&zstr, finish ? Z_FINISH : 0);
757	    if (zstatus == Z_STREAM_END)
758		goto done;
759	    else if (zstatus != Z_OK)
760		compress_error (0, zstatus, &zstr, fullname);
761	} while (zstr.avail_out == 0);
762    }
763 done:
764    /* Need to add the CRC information (8 bytes)
765       to the end of the gzip'd output.
766       Ensure there is enough space in the output buffer
767       to do so.  */
768    if (zstr.avail_out < 8)
769    {
770	unsigned char *newbuf;
771
772	assert(zstr.avail_out + zstr.total_out == *size);
773	assert(zstr.next_out == *buf + zstr.total_out);
774	*size += 8 - zstr.avail_out;
775	newbuf = realloc (*buf, *size);
776	if (newbuf == NULL)
777	{
778	    error (0, 0, "out of memory");
779	    return 1;
780	}
781	*buf = newbuf;
782	zstr.next_out = *buf + zstr.total_out;
783	zstr.avail_out = *size - zstr.total_out;
784	assert(zstr.avail_out + zstr.total_out == *size);
785	assert(zstr.next_out == *buf + zstr.total_out);
786    }
787    *zstr.next_out++ = (unsigned char)(crc & 0xff);
788    *zstr.next_out++ = (unsigned char)((crc >> 8) & 0xff);
789    *zstr.next_out++ = (unsigned char)((crc >> 16) & 0xff);
790    *zstr.next_out++ = (unsigned char)((crc >> 24) & 0xff);
791
792    *zstr.next_out++ = (unsigned char)(zstr.total_in & 0xff);
793    *zstr.next_out++ = (unsigned char)((zstr.total_in >> 8) & 0xff);
794    *zstr.next_out++ = (unsigned char)((zstr.total_in >> 16) & 0xff);
795    *zstr.next_out++ = (unsigned char)((zstr.total_in >> 24) & 0xff);
796
797    zstr.total_out += 8;
798    zstr.avail_out -= 8;
799    assert(zstr.avail_out + zstr.total_out == *size);
800    assert(zstr.next_out == *buf + zstr.total_out);
801
802    *len = zstr.total_out;
803
804    zstatus = deflateEnd (&zstr);
805    if (zstatus != Z_OK)
806	compress_error (0, zstatus, &zstr, fullname);
807
808    return 0;
809}
810#endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */
811