]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/cvs/src/zlib.c
This commit was generated by cvs2svn to compensate for changes in r53657,
[FreeBSD/FreeBSD.git] / contrib / cvs / src / zlib.c
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 <assert.h>
20 #include "cvs.h"
21 #include "buffer.h"
22
23 #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
24
25 #include "zlib.h"
26
27 /* OS/2 doesn't have EIO.  FIXME: this whole notion of turning
28    a different error into EIO strikes me as pretty dubious.  */
29 #if !defined (EIO)
30 #define EIO EBADPOS
31 #endif
32
33 /* The compression interface is built upon the buffer data structure.
34    We provide a buffer type which compresses or decompresses the data
35    which passes through it.  An input buffer decompresses the data
36    read from an underlying buffer, and an output buffer compresses the
37    data before writing it to an underlying buffer.  */
38
39 /* This structure is the closure field of the buffer.  */
40
41 struct compress_buffer
42 {
43     /* The underlying buffer.  */
44     struct buffer *buf;
45     /* The compression information.  */
46     z_stream zstr;
47 };
48
49 static void compress_error PROTO((int, int, z_stream *, const char *));
50 static int compress_buffer_input PROTO((void *, char *, int, int, int *));
51 static int compress_buffer_output PROTO((void *, const char *, int, int *));
52 static int compress_buffer_flush PROTO((void *));
53 static int compress_buffer_block PROTO((void *, int));
54 static int compress_buffer_shutdown_input PROTO((void *));
55 static int compress_buffer_shutdown_output PROTO((void *));
56
57 /* Report an error from one of the zlib functions.  */
58
59 static void
60 compress_error (status, zstatus, zstr, msg)
61      int status;
62      int zstatus;
63      z_stream *zstr;
64      const char *msg;
65 {
66     int hold_errno;
67     const char *zmsg;
68     char buf[100];
69
70     hold_errno = errno;
71
72     zmsg = zstr->msg;
73     if (zmsg == NULL)
74     {
75         sprintf (buf, "error %d", zstatus);
76         zmsg = buf;
77     }
78
79     error (status,
80            zstatus == Z_ERRNO ? hold_errno : 0,
81            "%s: %s", msg, zmsg);
82 }
83
84 /* Create a compression buffer.  */
85
86 struct buffer *
87 compress_buffer_initialize (buf, input, level, memory)
88      struct buffer *buf;
89      int input;
90      int level;
91      void (*memory) PROTO((struct buffer *));
92 {
93     struct compress_buffer *n;
94     int zstatus;
95
96     n = (struct compress_buffer *) xmalloc (sizeof *n);
97     memset (n, 0, sizeof *n);
98
99     n->buf = buf;
100
101     if (input)
102         zstatus = inflateInit (&n->zstr);
103     else
104         zstatus = deflateInit (&n->zstr, level);
105     if (zstatus != Z_OK)
106         compress_error (1, zstatus, &n->zstr, "compression initialization");
107
108     /* There may already be data buffered on BUF.  For an output
109        buffer, this is OK, because these routines will just use the
110        buffer routines to append data to the (uncompressed) data
111        already on BUF.  An input buffer expects to handle a single
112        buffer_data of buffered input to be uncompressed, so that is OK
113        provided there is only one buffer.  At present that is all
114        there ever will be; if this changes, compress_buffer_input must
115        be modified to handle multiple input buffers.  */
116     assert (! input || buf->data == NULL || buf->data->next == NULL);
117
118     return buf_initialize (input ? compress_buffer_input : NULL,
119                            input ? NULL : compress_buffer_output,
120                            input ? NULL : compress_buffer_flush,
121                            compress_buffer_block,
122                            (input
123                             ? compress_buffer_shutdown_input
124                             : compress_buffer_shutdown_output),
125                            memory,
126                            n);
127 }
128
129 /* Input data from a compression buffer.  */
130
131 static int
132 compress_buffer_input (closure, data, need, size, got)
133      void *closure;
134      char *data;
135      int need;
136      int size;
137      int *got;
138 {
139     struct compress_buffer *cb = (struct compress_buffer *) closure;
140     struct buffer_data *bd;
141
142     if (cb->buf->input == NULL)
143         abort ();
144
145     /* We use a single buffer_data structure to buffer up data which
146        the z_stream structure won't use yet.  We can safely store this
147        on cb->buf->data, because we never call the buffer routines on
148        cb->buf; we only call the buffer input routine, since that
149        gives us the semantics we want.  As noted in
150        compress_buffer_initialize, the buffer_data structure may
151        already exist, and hold data which was already read and
152        buffered before the decompression began.  */
153     bd = cb->buf->data;
154     if (bd == NULL)
155     {
156         bd = ((struct buffer_data *) malloc (sizeof (struct buffer_data)));
157         if (bd == NULL)
158             return -2;
159         bd->text = (char *) malloc (BUFFER_DATA_SIZE);
160         if (bd->text == NULL)
161         {
162             free (bd);
163             return -2;
164         }
165         bd->bufp = bd->text;
166         bd->size = 0;
167         cb->buf->data = bd;
168     }
169
170     cb->zstr.avail_out = size;
171     cb->zstr.next_out = (Bytef *) data;
172
173     while (1)
174     {
175         int zstatus, sofar, status, nread;
176
177         /* First try to inflate any data we already have buffered up.
178            This is useful even if we don't have any buffered data,
179            because there may be data buffered inside the z_stream
180            structure.  */
181
182         cb->zstr.avail_in = bd->size;
183         cb->zstr.next_in = (Bytef *) bd->bufp;
184
185         do
186         {
187             zstatus = inflate (&cb->zstr, Z_NO_FLUSH);
188             if (zstatus == Z_STREAM_END)
189                 break;
190             if (zstatus != Z_OK && zstatus != Z_BUF_ERROR)
191             {
192                 compress_error (0, zstatus, &cb->zstr, "inflate");
193                 return EIO;
194             }
195         } while (cb->zstr.avail_in > 0
196                  && cb->zstr.avail_out > 0);
197
198         bd->size = cb->zstr.avail_in;
199         bd->bufp = (char *) cb->zstr.next_in;
200
201         if (zstatus == Z_STREAM_END)
202             return -1;
203
204         /* If we have obtained NEED bytes, then return, unless NEED is
205            zero and we haven't obtained anything at all.  If NEED is
206            zero, we will keep reading from the underlying buffer until
207            we either can't read anything, or we have managed to
208            inflate at least one byte.  */
209         sofar = size - cb->zstr.avail_out;
210         if (sofar > 0 && sofar >= need)
211             break;
212
213         /* All our buffered data should have been processed at this
214            point.  */
215         assert (bd->size == 0);
216
217         /* This will work well in the server, because this call will
218            do an unblocked read and fetch all the available data.  In
219            the client, this will read a single byte from the stdio
220            stream, which will cause us to call inflate once per byte.
221            It would be more efficient if we could make a call which
222            would fetch all the available bytes, and at least one byte.  */
223
224         status = (*cb->buf->input) (cb->buf->closure, bd->text,
225                                     need > 0 ? 1 : 0,
226                                     BUFFER_DATA_SIZE, &nread);
227         if (status != 0)
228             return status;
229
230         /* If we didn't read anything, then presumably the buffer is
231            in nonblocking mode, and we should just get out now with
232            whatever we've inflated.  */
233         if (nread == 0)
234         {
235             assert (need == 0);
236             break;
237         }
238
239         bd->bufp = bd->text;
240         bd->size = nread;
241     }
242
243     *got = size - cb->zstr.avail_out;
244
245     return 0;
246 }
247
248 /* Output data to a compression buffer.  */
249
250 static int
251 compress_buffer_output (closure, data, have, wrote)
252      void *closure;
253      const char *data;
254      int have;
255      int *wrote;
256 {
257     struct compress_buffer *cb = (struct compress_buffer *) closure;
258
259     cb->zstr.avail_in = have;
260     cb->zstr.next_in = (unsigned char *) data;
261
262     while (cb->zstr.avail_in > 0)
263     {
264         char buffer[BUFFER_DATA_SIZE];
265         int zstatus;
266
267         cb->zstr.avail_out = BUFFER_DATA_SIZE;
268         cb->zstr.next_out = (unsigned char *) buffer;
269
270         zstatus = deflate (&cb->zstr, Z_NO_FLUSH);
271         if (zstatus != Z_OK)
272         {
273             compress_error (0, zstatus, &cb->zstr, "deflate");
274             return EIO;
275         }
276
277         if (cb->zstr.avail_out != BUFFER_DATA_SIZE)
278             buf_output (cb->buf, buffer,
279                         BUFFER_DATA_SIZE - cb->zstr.avail_out);
280     }
281
282     *wrote = have;
283
284     /* We will only be here because buf_send_output was called on the
285        compression buffer.  That means that we should now call
286        buf_send_output on the underlying buffer.  */
287     return buf_send_output (cb->buf);
288 }
289
290 /* Flush a compression buffer.  */
291
292 static int
293 compress_buffer_flush (closure)
294      void *closure;
295 {
296     struct compress_buffer *cb = (struct compress_buffer *) closure;
297
298     cb->zstr.avail_in = 0;
299     cb->zstr.next_in = NULL;
300
301     while (1)
302     {
303         char buffer[BUFFER_DATA_SIZE];
304         int zstatus;
305
306         cb->zstr.avail_out = BUFFER_DATA_SIZE;
307         cb->zstr.next_out = (unsigned char *) buffer;
308
309         zstatus = deflate (&cb->zstr, Z_SYNC_FLUSH);
310
311         /* The deflate function will return Z_BUF_ERROR if it can't do
312            anything, which in this case means that all data has been
313            flushed.  */
314         if (zstatus == Z_BUF_ERROR)
315             break;
316
317         if (zstatus != Z_OK)
318         {
319             compress_error (0, zstatus, &cb->zstr, "deflate flush");
320             return EIO;
321         }
322
323         if (cb->zstr.avail_out != BUFFER_DATA_SIZE)
324             buf_output (cb->buf, buffer,
325                         BUFFER_DATA_SIZE - cb->zstr.avail_out);
326
327         /* If the deflate function did not fill the output buffer,
328            then all data has been flushed.  */
329         if (cb->zstr.avail_out > 0)
330             break;
331     }
332
333     /* Now flush the underlying buffer.  Note that if the original
334        call to buf_flush passed 1 for the BLOCK argument, then the
335        buffer will already have been set into blocking mode, so we
336        should always pass 0 here.  */
337     return buf_flush (cb->buf, 0);
338 }
339
340 /* The block routine for a compression buffer.  */
341
342 static int
343 compress_buffer_block (closure, block)
344      void *closure;
345      int block;
346 {
347     struct compress_buffer *cb = (struct compress_buffer *) closure;
348
349     if (block)
350         return set_block (cb->buf);
351     else
352         return set_nonblock (cb->buf);
353 }
354
355 /* Shut down an input buffer.  */
356
357 static int
358 compress_buffer_shutdown_input (closure)
359      void *closure;
360 {
361     struct compress_buffer *cb = (struct compress_buffer *) closure;
362     int zstatus;
363
364     /* Pick up any trailing data, such as the checksum.  */
365     while (1)
366     {
367         int status, nread;
368         char buf[100];
369
370         status = compress_buffer_input (cb, buf, 0, sizeof buf, &nread);
371         if (status == -1)
372             break;
373         if (status != 0)
374             return status;
375     }
376
377     zstatus = inflateEnd (&cb->zstr);
378     if (zstatus != Z_OK)
379     {
380         compress_error (0, zstatus, &cb->zstr, "inflateEnd");
381         return EIO;
382     }
383
384     return buf_shutdown (cb->buf);
385 }
386
387 /* Shut down an output buffer.  */
388
389 static int
390 compress_buffer_shutdown_output (closure)
391      void *closure;
392 {
393     struct compress_buffer *cb = (struct compress_buffer *) closure;
394     int zstatus, status;
395
396     do
397     {
398         char buffer[BUFFER_DATA_SIZE];
399
400         cb->zstr.avail_out = BUFFER_DATA_SIZE;
401         cb->zstr.next_out = (unsigned char *) buffer;
402
403         zstatus = deflate (&cb->zstr, Z_FINISH);
404         if (zstatus != Z_OK && zstatus != Z_STREAM_END)
405         {
406             compress_error (0, zstatus, &cb->zstr, "deflate finish");
407             return EIO;
408         }
409
410         if (cb->zstr.avail_out != BUFFER_DATA_SIZE)
411             buf_output (cb->buf, buffer,
412                         BUFFER_DATA_SIZE - cb->zstr.avail_out);
413     } while (zstatus != Z_STREAM_END);
414
415     zstatus = deflateEnd (&cb->zstr);
416     if (zstatus != Z_OK)
417     {
418         compress_error (0, zstatus, &cb->zstr, "deflateEnd");
419         return EIO;
420     }
421
422     status = buf_flush (cb->buf, 1);
423     if (status != 0)
424         return status;
425
426     return buf_shutdown (cb->buf);
427 }
428
429
430
431 /* Here is our librarified gzip implementation.  It is very minimal
432    but attempts to be RFC1952 compliant.  */
433 /* Note that currently only the client uses the gzip library.  If we
434    make the server use it too (which should be straightforward), then
435    filter_stream_through_program, filter_through_gzip, and
436    filter_through_gunzip can go away.  */
437
438 /* BUF should contain SIZE bytes of gzipped data (RFC1952/RFC1951).
439    We are to uncompress the data and write the result to the file
440    descriptor FD.  If something goes wrong, give an error message
441    mentioning FULLNAME as the name of the file for FD (and make it a
442    fatal error if we can't recover from it).  */
443
444 void
445 gunzip_and_write (fd, fullname, buf, size)
446     int fd;
447     char *fullname;
448     unsigned char *buf;
449     size_t size;
450 {
451     size_t pos;
452     z_stream zstr;
453     int zstatus;
454     unsigned char outbuf[32768];
455     unsigned long crc;
456
457     if (buf[0] != 31 || buf[1] != 139)
458         error (1, 0, "gzipped data does not start with gzip identification");
459     if (buf[2] != 8)
460         error (1, 0, "only the deflate compression method is supported");
461
462     /* Skip over the fixed header, and then skip any of the variable-length
463        fields.  */
464     pos = 10;
465     if (buf[3] & 4)
466         pos += buf[pos] + (buf[pos + 1] << 8) + 2;
467     if (buf[3] & 8)
468         pos += strlen (buf + pos) + 1;
469     if (buf[3] & 16)
470         pos += strlen (buf + pos) + 1;
471     if (buf[3] & 2)
472         pos += 2;
473
474     memset (&zstr, 0, sizeof zstr);
475     /* Passing a negative argument tells zlib not to look for a zlib
476        (RFC1950) header.  This is an undocumented feature; I suppose if
477        we wanted to be anal we could synthesize a header instead,
478        but why bother?  */
479     zstatus = inflateInit2 (&zstr, -15);
480
481     if (zstatus != Z_OK)
482         compress_error (1, zstatus, &zstr, fullname);
483
484     /* I don't see why we should have to include the 8 byte trailer in
485        avail_in.  But I see that zlib/gzio.c does, and it seemed to fix
486        a fairly rare bug in which we'd get a Z_BUF_ERROR for no obvious
487        reason.  */
488     zstr.avail_in = size - pos;
489     zstr.next_in = buf + pos;
490
491     crc = crc32 (0, NULL, 0);
492
493     do
494     {
495         zstr.avail_out = sizeof (outbuf);
496         zstr.next_out = outbuf;
497         zstatus = inflate (&zstr, Z_NO_FLUSH);
498         if (zstatus != Z_STREAM_END && zstatus != Z_OK)
499             compress_error (1, zstatus, &zstr, fullname);
500         if (write (fd, outbuf, sizeof (outbuf) - zstr.avail_out) < 0)
501             error (1, errno, "writing decompressed file %s", fullname);
502         crc = crc32 (crc, outbuf, sizeof (outbuf) - zstr.avail_out);
503     } while (zstatus != Z_STREAM_END);
504     zstatus = inflateEnd (&zstr);
505     if (zstatus != Z_OK)
506         compress_error (0, zstatus, &zstr, fullname);
507
508     if (crc != (buf[zstr.total_in + 10]
509                 + (buf[zstr.total_in + 11] << 8)
510                 + (buf[zstr.total_in + 12] << 16)
511                 + (buf[zstr.total_in + 13] << 24)))
512         error (1, 0, "CRC error uncompressing %s", fullname);
513
514     if (zstr.total_out != (buf[zstr.total_in + 14]
515                            + (buf[zstr.total_in + 15] << 8)
516                            + (buf[zstr.total_in + 16] << 16)
517                            + (buf[zstr.total_in + 17] << 24)))
518         error (1, 0, "invalid length uncompressing %s", fullname);
519 }
520
521 /* Read all of FD and put the gzipped data (RFC1952/RFC1951) into *BUF,
522    replacing previous contents of *BUF.  *BUF is malloc'd and *SIZE is
523    its allocated size.  Put the actual number of bytes of data in
524    *LEN.  If something goes wrong, give an error message mentioning
525    FULLNAME as the name of the file for FD (and make it a fatal error
526    if we can't recover from it).  LEVEL is the compression level (1-9).  */
527
528 void
529 read_and_gzip (fd, fullname, buf, size, len, level)
530     int fd;
531     char *fullname;
532     unsigned char **buf;
533     size_t *size;
534     size_t *len;
535     int level;
536 {
537     z_stream zstr;
538     int zstatus;
539     unsigned char inbuf[8192];
540     int nread;
541     unsigned long crc;
542
543     if (*size < 1024)
544     {
545         *size = 1024;
546         *buf = (unsigned char *) xrealloc (*buf, *size);
547     }
548     (*buf)[0] = 31;
549     (*buf)[1] = 139;
550     (*buf)[2] = 8;
551     (*buf)[3] = 0;
552     (*buf)[4] = (*buf)[5] = (*buf)[6] = (*buf)[7] = 0;
553     /* Could set this based on level, but why bother?  */
554     (*buf)[8] = 0;
555     (*buf)[9] = 255;
556
557     memset (&zstr, 0, sizeof zstr);
558     zstatus = deflateInit2 (&zstr, level, Z_DEFLATED, -15, 8,
559                             Z_DEFAULT_STRATEGY);
560     crc = crc32 (0, NULL, 0);
561     if (zstatus != Z_OK)
562         compress_error (1, zstatus, &zstr, fullname);
563     zstr.avail_out = *size;
564     zstr.next_out = *buf + 10;
565
566     while (1)
567     {
568         int finish = 0;
569
570         nread = read (fd, inbuf, sizeof inbuf);
571         if (nread < 0)
572             error (1, errno, "cannot read %s", fullname);
573         else if (nread == 0)
574             /* End of file.  */
575             finish = 1;
576         crc = crc32 (crc, inbuf, nread);
577         zstr.next_in = inbuf;
578         zstr.avail_in = nread;
579
580         do
581         {
582             size_t offset;
583
584             /* I don't see this documented anywhere, but deflate seems
585                to tend to dump core sometimes if we pass it Z_FINISH and
586                a small (e.g. 2147 byte) avail_out.  So we insist on at
587                least 4096 bytes (that is what zlib/gzio.c uses).  */
588
589             if (zstr.avail_out < 4096)
590             {
591                 offset = zstr.next_out - *buf;
592                 *size *= 2;
593                 *buf = xrealloc (*buf, *size);
594                 zstr.next_out = *buf + offset;
595                 zstr.avail_out = *size - offset;
596             }
597
598             zstatus = deflate (&zstr, finish ? Z_FINISH : 0);
599             if (zstatus == Z_STREAM_END)
600                 goto done;
601             else if (zstatus != Z_OK)
602                 compress_error (0, zstatus, &zstr, fullname);
603         } while (zstr.avail_out == 0);
604     }
605  done:
606     *(*buf + zstr.total_out + 10) = crc & 0xff;
607     *(*buf + zstr.total_out + 11) = (crc >> 8) & 0xff;
608     *(*buf + zstr.total_out + 12) = (crc >> 16) & 0xff;
609     *(*buf + zstr.total_out + 13) = (crc >> 24) & 0xff;
610
611     *(*buf + zstr.total_out + 14) = zstr.total_in & 0xff;
612     *(*buf + zstr.total_out + 15) = (zstr.total_in >> 8) & 0xff;
613     *(*buf + zstr.total_out + 16) = (zstr.total_in >> 16) & 0xff;
614     *(*buf + zstr.total_out + 17) = (zstr.total_in >> 24) & 0xff;
615
616     *len = zstr.total_out + 18;
617
618     zstatus = deflateEnd (&zstr);
619     if (zstatus != Z_OK)
620         compress_error (0, zstatus, &zstr, fullname);
621 }
622 #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */