]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/archive_write_set_compression_gzip.c
Merge r183430 from vendor/top/dist to head/contrib/top, although with
[FreeBSD/FreeBSD.git] / lib / libarchive / archive_write_set_compression_gzip.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "archive_platform.h"
27
28 __FBSDID("$FreeBSD$");
29
30 #ifdef HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39 #include <time.h>
40 #ifdef HAVE_ZLIB_H
41 #include <zlib.h>
42 #endif
43
44 #include "archive.h"
45 #include "archive_private.h"
46 #include "archive_write_private.h"
47
48 #ifndef HAVE_ZLIB_H
49 int
50 archive_write_set_compression_gzip(struct archive *_a)
51 {
52         /* Unsupported gzip compression, we don't have zlib */
53         return (ARCHIVE_FATAL);
54 }
55 #else
56 /* Don't compile this if we don't have zlib. */
57
58 struct private_data {
59         z_stream         stream;
60         int64_t          total_in;
61         unsigned char   *compressed;
62         size_t           compressed_buffer_size;
63         unsigned long    crc;
64         /* Options */
65         int              compression_level;
66 };
67
68
69 /*
70  * Yuck.  zlib.h is not const-correct, so I need this one bit
71  * of ugly hackery to convert a const * pointer to a non-const pointer.
72  */
73 #define SET_NEXT_IN(st,src)                                     \
74         (st)->stream.next_in = (Bytef *)(uintptr_t)(const void *)(src)
75
76 static int      archive_compressor_gzip_finish(struct archive_write *);
77 static int      archive_compressor_gzip_init(struct archive_write *);
78 static int      archive_compressor_gzip_options(struct archive_write *,
79                     const char *, const char *);
80 static int      archive_compressor_gzip_write(struct archive_write *,
81                     const void *, size_t);
82 static int      drive_compressor(struct archive_write *, struct private_data *,
83                     int finishing);
84
85
86 /*
87  * Allocate, initialize and return a archive object.
88  */
89 int
90 archive_write_set_compression_gzip(struct archive *_a)
91 {
92         struct archive_write *a = (struct archive_write *)_a;
93         __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
94             ARCHIVE_STATE_NEW, "archive_write_set_compression_gzip");
95         a->compressor.init = &archive_compressor_gzip_init;
96         a->archive.compression_code = ARCHIVE_COMPRESSION_GZIP;
97         a->archive.compression_name = "gzip";
98         return (ARCHIVE_OK);
99 }
100
101 /*
102  * Setup callback.
103  */
104 static int
105 archive_compressor_gzip_init(struct archive_write *a)
106 {
107         int ret;
108         struct private_data *state;
109         time_t t;
110
111         a->archive.compression_code = ARCHIVE_COMPRESSION_GZIP;
112         a->archive.compression_name = "gzip";
113
114         if (a->client_opener != NULL) {
115                 ret = (a->client_opener)(&a->archive, a->client_data);
116                 if (ret != ARCHIVE_OK)
117                         return (ret);
118         }
119
120         /*
121          * The next check is a temporary workaround until the gzip
122          * code can be overhauled some.  The code should not require
123          * that compressed_buffer_size == bytes_per_block.  Removing
124          * this assumption will allow us to compress larger chunks at
125          * a time, which should improve overall performance
126          * marginally.  As a minor side-effect, such a cleanup would
127          * allow us to support truly arbitrary block sizes.
128          */
129         if (a->bytes_per_block < 10) {
130                 archive_set_error(&a->archive, EINVAL,
131                     "GZip compressor requires a minimum 10 byte block size");
132                 return (ARCHIVE_FATAL);
133         }
134
135         state = (struct private_data *)malloc(sizeof(*state));
136         if (state == NULL) {
137                 archive_set_error(&a->archive, ENOMEM,
138                     "Can't allocate data for compression");
139                 return (ARCHIVE_FATAL);
140         }
141         memset(state, 0, sizeof(*state));
142
143         /*
144          * See comment above.  We should set compressed_buffer_size to
145          * max(bytes_per_block, 65536), but the code can't handle that yet.
146          */
147         state->compressed_buffer_size = a->bytes_per_block;
148         state->compressed = (unsigned char *)malloc(state->compressed_buffer_size);
149         state->crc = crc32(0L, NULL, 0);
150         state->compression_level = Z_DEFAULT_COMPRESSION;
151
152         if (state->compressed == NULL) {
153                 archive_set_error(&a->archive, ENOMEM,
154                     "Can't allocate data for compression buffer");
155                 free(state);
156                 return (ARCHIVE_FATAL);
157         }
158
159         state->stream.next_out = state->compressed;
160         state->stream.avail_out = state->compressed_buffer_size;
161
162         /* Prime output buffer with a gzip header. */
163         t = time(NULL);
164         state->compressed[0] = 0x1f; /* GZip signature bytes */
165         state->compressed[1] = 0x8b;
166         state->compressed[2] = 0x08; /* "Deflate" compression */
167         state->compressed[3] = 0; /* No options */
168         state->compressed[4] = (t)&0xff;  /* Timestamp */
169         state->compressed[5] = (t>>8)&0xff;
170         state->compressed[6] = (t>>16)&0xff;
171         state->compressed[7] = (t>>24)&0xff;
172         state->compressed[8] = 0; /* No deflate options */
173         state->compressed[9] = 3; /* OS=Unix */
174         state->stream.next_out += 10;
175         state->stream.avail_out -= 10;
176
177         a->compressor.options = archive_compressor_gzip_options;
178         a->compressor.write = archive_compressor_gzip_write;
179         a->compressor.finish = archive_compressor_gzip_finish;
180
181         /* Initialize compression library. */
182         ret = deflateInit2(&(state->stream),
183             state->compression_level,
184             Z_DEFLATED,
185             -15 /* < 0 to suppress zlib header */,
186             8,
187             Z_DEFAULT_STRATEGY);
188
189         if (ret == Z_OK) {
190                 a->compressor.data = state;
191                 return (0);
192         }
193
194         /* Library setup failed: clean up. */
195         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Internal error "
196             "initializing compression library");
197         free(state->compressed);
198         free(state);
199
200         /* Override the error message if we know what really went wrong. */
201         switch (ret) {
202         case Z_STREAM_ERROR:
203                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
204                     "Internal error initializing "
205                     "compression library: invalid setup parameter");
206                 break;
207         case Z_MEM_ERROR:
208                 archive_set_error(&a->archive, ENOMEM, "Internal error initializing "
209                     "compression library");
210                 break;
211         case Z_VERSION_ERROR:
212                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
213                     "Internal error initializing "
214                     "compression library: invalid library version");
215                 break;
216         }
217
218         return (ARCHIVE_FATAL);
219 }
220
221 /*
222  * Set write options.
223  */
224 static int
225 archive_compressor_gzip_options(struct archive_write *a, const char *key,
226     const char *value)
227 {
228         struct private_data *state;
229         int ret;
230
231         state = (struct private_data *)a->compressor.data;
232         if (strcmp(key, "compression-level") == 0) {
233                 int level;
234
235                 if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
236                     value[1] != '\0')
237                         return (ARCHIVE_WARN);
238                 level = value[0] - '0';
239                 if (level == state->compression_level)
240                         return (ARCHIVE_OK);
241                 
242                 ret = deflateParams(&(state->stream), level,
243                     Z_DEFAULT_STRATEGY);
244                 if (ret == Z_OK) {
245                         state->compression_level = level;
246                         return (ARCHIVE_OK);
247                 }
248                 switch (ret) {
249                 case Z_STREAM_ERROR:
250                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
251                             "Internal error updating params "
252                             "compression library: state was inconsistent "
253                             "or parameter was invalid");
254                         break;
255                 case Z_BUF_ERROR:
256                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
257                             "Internal error updating params "
258                             "compression library: out buffer was zero");
259                         break;
260                 default:
261                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
262                             "Internal error updatng params "
263                             "compression library");
264                         break;
265                 }
266                 return (ARCHIVE_FATAL);
267         }
268
269         return (ARCHIVE_WARN);
270 }
271
272 /*
273  * Write data to the compressed stream.
274  */
275 static int
276 archive_compressor_gzip_write(struct archive_write *a, const void *buff,
277     size_t length)
278 {
279         struct private_data *state;
280         int ret;
281
282         state = (struct private_data *)a->compressor.data;
283         if (a->client_writer == NULL) {
284                 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
285                     "No write callback is registered?  "
286                     "This is probably an internal programming error.");
287                 return (ARCHIVE_FATAL);
288         }
289
290         /* Update statistics */
291         state->crc = crc32(state->crc, (const Bytef *)buff, length);
292         state->total_in += length;
293
294         /* Compress input data to output buffer */
295         SET_NEXT_IN(state, buff);
296         state->stream.avail_in = length;
297         if ((ret = drive_compressor(a, state, 0)) != ARCHIVE_OK)
298                 return (ret);
299
300         a->archive.file_position += length;
301         return (ARCHIVE_OK);
302 }
303
304
305 /*
306  * Finish the compression...
307  */
308 static int
309 archive_compressor_gzip_finish(struct archive_write *a)
310 {
311         ssize_t block_length, target_block_length, bytes_written;
312         int ret;
313         struct private_data *state;
314         unsigned tocopy;
315         unsigned char trailer[8];
316
317         state = (struct private_data *)a->compressor.data;
318         ret = 0;
319         if (a->client_writer == NULL) {
320                 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
321                     "No write callback is registered?  "
322                     "This is probably an internal programming error.");
323                 ret = ARCHIVE_FATAL;
324                 goto cleanup;
325         }
326
327         /* By default, always pad the uncompressed data. */
328         if (a->pad_uncompressed) {
329                 tocopy = a->bytes_per_block -
330                     (state->total_in % a->bytes_per_block);
331                 while (tocopy > 0 && tocopy < (unsigned)a->bytes_per_block) {
332                         SET_NEXT_IN(state, a->nulls);
333                         state->stream.avail_in = tocopy < a->null_length ?
334                             tocopy : a->null_length;
335                         state->crc = crc32(state->crc, a->nulls,
336                             state->stream.avail_in);
337                         state->total_in += state->stream.avail_in;
338                         tocopy -= state->stream.avail_in;
339                         ret = drive_compressor(a, state, 0);
340                         if (ret != ARCHIVE_OK)
341                                 goto cleanup;
342                 }
343         }
344
345         /* Finish compression cycle */
346         if (((ret = drive_compressor(a, state, 1))) != ARCHIVE_OK)
347                 goto cleanup;
348
349         /* Build trailer: 4-byte CRC and 4-byte length. */
350         trailer[0] = (state->crc)&0xff;
351         trailer[1] = (state->crc >> 8)&0xff;
352         trailer[2] = (state->crc >> 16)&0xff;
353         trailer[3] = (state->crc >> 24)&0xff;
354         trailer[4] = (state->total_in)&0xff;
355         trailer[5] = (state->total_in >> 8)&0xff;
356         trailer[6] = (state->total_in >> 16)&0xff;
357         trailer[7] = (state->total_in >> 24)&0xff;
358
359         /* Add trailer to current block. */
360         tocopy = 8;
361         if (tocopy > state->stream.avail_out)
362                 tocopy = state->stream.avail_out;
363         memcpy(state->stream.next_out, trailer, tocopy);
364         state->stream.next_out += tocopy;
365         state->stream.avail_out -= tocopy;
366
367         /* If it overflowed, flush and start a new block. */
368         if (tocopy < 8) {
369                 bytes_written = (a->client_writer)(&a->archive, a->client_data,
370                     state->compressed, state->compressed_buffer_size);
371                 if (bytes_written <= 0) {
372                         ret = ARCHIVE_FATAL;
373                         goto cleanup;
374                 }
375                 a->archive.raw_position += bytes_written;
376                 state->stream.next_out = state->compressed;
377                 state->stream.avail_out = state->compressed_buffer_size;
378                 memcpy(state->stream.next_out, trailer + tocopy, 8-tocopy);
379                 state->stream.next_out += 8-tocopy;
380                 state->stream.avail_out -= 8-tocopy;
381         }
382
383         /* Optionally, pad the final compressed block. */
384         block_length = state->stream.next_out - state->compressed;
385
386
387         /* Tricky calculation to determine size of last block. */
388         target_block_length = block_length;
389         if (a->bytes_in_last_block <= 0)
390                 /* Default or Zero: pad to full block */
391                 target_block_length = a->bytes_per_block;
392         else
393                 /* Round length to next multiple of bytes_in_last_block. */
394                 target_block_length = a->bytes_in_last_block *
395                     ( (block_length + a->bytes_in_last_block - 1) /
396                         a->bytes_in_last_block);
397         if (target_block_length > a->bytes_per_block)
398                 target_block_length = a->bytes_per_block;
399         if (block_length < target_block_length) {
400                 memset(state->stream.next_out, 0,
401                     target_block_length - block_length);
402                 block_length = target_block_length;
403         }
404
405         /* Write the last block */
406         bytes_written = (a->client_writer)(&a->archive, a->client_data,
407             state->compressed, block_length);
408         if (bytes_written <= 0) {
409                 ret = ARCHIVE_FATAL;
410                 goto cleanup;
411         }
412         a->archive.raw_position += bytes_written;
413
414         /* Cleanup: shut down compressor, release memory, etc. */
415 cleanup:
416         switch (deflateEnd(&(state->stream))) {
417         case Z_OK:
418                 break;
419         default:
420                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
421                     "Failed to clean up compressor");
422                 ret = ARCHIVE_FATAL;
423         }
424         free(state->compressed);
425         free(state);
426         return (ret);
427 }
428
429 /*
430  * Utility function to push input data through compressor,
431  * writing full output blocks as necessary.
432  *
433  * Note that this handles both the regular write case (finishing ==
434  * false) and the end-of-archive case (finishing == true).
435  */
436 static int
437 drive_compressor(struct archive_write *a, struct private_data *state, int finishing)
438 {
439         ssize_t bytes_written;
440         int ret;
441
442         for (;;) {
443                 if (state->stream.avail_out == 0) {
444                         bytes_written = (a->client_writer)(&a->archive,
445                             a->client_data, state->compressed,
446                             state->compressed_buffer_size);
447                         if (bytes_written <= 0) {
448                                 /* TODO: Handle this write failure */
449                                 return (ARCHIVE_FATAL);
450                         } else if ((size_t)bytes_written < state->compressed_buffer_size) {
451                                 /* Short write: Move remaining to
452                                  * front of block and keep filling */
453                                 memmove(state->compressed,
454                                     state->compressed + bytes_written,
455                                     state->compressed_buffer_size - bytes_written);
456                         }
457                         a->archive.raw_position += bytes_written;
458                         state->stream.next_out
459                             = state->compressed +
460                             state->compressed_buffer_size - bytes_written;
461                         state->stream.avail_out = bytes_written;
462                 }
463
464                 /* If there's nothing to do, we're done. */
465                 if (!finishing && state->stream.avail_in == 0)
466                         return (ARCHIVE_OK);
467
468                 ret = deflate(&(state->stream),
469                     finishing ? Z_FINISH : Z_NO_FLUSH );
470
471                 switch (ret) {
472                 case Z_OK:
473                         /* In non-finishing case, check if compressor
474                          * consumed everything */
475                         if (!finishing && state->stream.avail_in == 0)
476                                 return (ARCHIVE_OK);
477                         /* In finishing case, this return always means
478                          * there's more work */
479                         break;
480                 case Z_STREAM_END:
481                         /* This return can only occur in finishing case. */
482                         return (ARCHIVE_OK);
483                 default:
484                         /* Any other return value indicates an error. */
485                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
486                             "GZip compression failed:"
487                             " deflate() call returned status %d",
488                             ret);
489                         return (ARCHIVE_FATAL);
490                 }
491         }
492 }
493
494 #endif /* HAVE_ZLIB_H */