Import libarchive-2.4.17. See NEWS for details.
[dragonfly.git] / contrib / libarchive-2 / 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 /* Don't compile this if we don't have zlib. */
29 #if HAVE_ZLIB_H
30
31 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_set_compression_gzip.c,v 1.16 2008/02/21 03:21:50 kientzle Exp $");
32
33 #ifdef HAVE_ERRNO_H
34 #include <errno.h>
35 #endif
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #endif
42 #include <time.h>
43 #ifdef HAVE_ZLIB_H
44 #include <zlib.h>
45 #endif
46
47 #include "archive.h"
48 #include "archive_private.h"
49 #include "archive_write_private.h"
50
51 struct private_data {
52         z_stream         stream;
53         int64_t          total_in;
54         unsigned char   *compressed;
55         size_t           compressed_buffer_size;
56         unsigned long    crc;
57 };
58
59
60 /*
61  * Yuck.  zlib.h is not const-correct, so I need this one bit
62  * of ugly hackery to convert a const * pointer to a non-const pointer.
63  */
64 #define SET_NEXT_IN(st,src)                                     \
65         (st)->stream.next_in = (Bytef *)(uintptr_t)(const void *)(src)
66
67 static int      archive_compressor_gzip_finish(struct archive_write *);
68 static int      archive_compressor_gzip_init(struct archive_write *);
69 static int      archive_compressor_gzip_write(struct archive_write *,
70                     const void *, size_t);
71 static int      drive_compressor(struct archive_write *, struct private_data *,
72                     int finishing);
73
74
75 /*
76  * Allocate, initialize and return a archive object.
77  */
78 int
79 archive_write_set_compression_gzip(struct archive *_a)
80 {
81         struct archive_write *a = (struct archive_write *)_a;
82         __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
83             ARCHIVE_STATE_NEW, "archive_write_set_compression_gzip");
84         a->compressor.init = &archive_compressor_gzip_init;
85         a->archive.compression_code = ARCHIVE_COMPRESSION_GZIP;
86         a->archive.compression_name = "gzip";
87         return (ARCHIVE_OK);
88 }
89
90 /*
91  * Setup callback.
92  */
93 static int
94 archive_compressor_gzip_init(struct archive_write *a)
95 {
96         int ret;
97         struct private_data *state;
98         time_t t;
99
100         a->archive.compression_code = ARCHIVE_COMPRESSION_GZIP;
101         a->archive.compression_name = "gzip";
102
103         if (a->client_opener != NULL) {
104                 ret = (a->client_opener)(&a->archive, a->client_data);
105                 if (ret != ARCHIVE_OK)
106                         return (ret);
107         }
108
109         /*
110          * The next check is a temporary workaround until the gzip
111          * code can be overhauled some.  The code should not require
112          * that compressed_buffer_size == bytes_per_block.  Removing
113          * this assumption will allow us to compress larger chunks at
114          * a time, which should improve overall performance
115          * marginally.  As a minor side-effect, such a cleanup would
116          * allow us to support truly arbitrary block sizes.
117          */
118         if (a->bytes_per_block < 10) {
119                 archive_set_error(&a->archive, EINVAL,
120                     "GZip compressor requires a minimum 10 byte block size");
121                 return (ARCHIVE_FATAL);
122         }
123
124         state = (struct private_data *)malloc(sizeof(*state));
125         if (state == NULL) {
126                 archive_set_error(&a->archive, ENOMEM,
127                     "Can't allocate data for compression");
128                 return (ARCHIVE_FATAL);
129         }
130         memset(state, 0, sizeof(*state));
131
132         /*
133          * See comment above.  We should set compressed_buffer_size to
134          * max(bytes_per_block, 65536), but the code can't handle that yet.
135          */
136         state->compressed_buffer_size = a->bytes_per_block;
137         state->compressed = (unsigned char *)malloc(state->compressed_buffer_size);
138         state->crc = crc32(0L, NULL, 0);
139
140         if (state->compressed == NULL) {
141                 archive_set_error(&a->archive, ENOMEM,
142                     "Can't allocate data for compression buffer");
143                 free(state);
144                 return (ARCHIVE_FATAL);
145         }
146
147         state->stream.next_out = state->compressed;
148         state->stream.avail_out = state->compressed_buffer_size;
149
150         /* Prime output buffer with a gzip header. */
151         t = time(NULL);
152         state->compressed[0] = 0x1f; /* GZip signature bytes */
153         state->compressed[1] = 0x8b;
154         state->compressed[2] = 0x08; /* "Deflate" compression */
155         state->compressed[3] = 0; /* No options */
156         state->compressed[4] = (t)&0xff;  /* Timestamp */
157         state->compressed[5] = (t>>8)&0xff;
158         state->compressed[6] = (t>>16)&0xff;
159         state->compressed[7] = (t>>24)&0xff;
160         state->compressed[8] = 0; /* No deflate options */
161         state->compressed[9] = 3; /* OS=Unix */
162         state->stream.next_out += 10;
163         state->stream.avail_out -= 10;
164
165         a->compressor.write = archive_compressor_gzip_write;
166         a->compressor.finish = archive_compressor_gzip_finish;
167
168         /* Initialize compression library. */
169         ret = deflateInit2(&(state->stream),
170             Z_DEFAULT_COMPRESSION,
171             Z_DEFLATED,
172             -15 /* < 0 to suppress zlib header */,
173             8,
174             Z_DEFAULT_STRATEGY);
175
176         if (ret == Z_OK) {
177                 a->compressor.data = state;
178                 return (0);
179         }
180
181         /* Library setup failed: clean up. */
182         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Internal error "
183             "initializing compression library");
184         free(state->compressed);
185         free(state);
186
187         /* Override the error message if we know what really went wrong. */
188         switch (ret) {
189         case Z_STREAM_ERROR:
190                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
191                     "Internal error initializing "
192                     "compression library: invalid setup parameter");
193                 break;
194         case Z_MEM_ERROR:
195                 archive_set_error(&a->archive, ENOMEM, "Internal error initializing "
196                     "compression library");
197                 break;
198         case Z_VERSION_ERROR:
199                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
200                     "Internal error initializing "
201                     "compression library: invalid library version");
202                 break;
203         }
204
205         return (ARCHIVE_FATAL);
206 }
207
208 /*
209  * Write data to the compressed stream.
210  */
211 static int
212 archive_compressor_gzip_write(struct archive_write *a, const void *buff,
213     size_t length)
214 {
215         struct private_data *state;
216         int ret;
217
218         state = (struct private_data *)a->compressor.data;
219         if (a->client_writer == NULL) {
220                 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
221                     "No write callback is registered?  "
222                     "This is probably an internal programming error.");
223                 return (ARCHIVE_FATAL);
224         }
225
226         /* Update statistics */
227         state->crc = crc32(state->crc, (const Bytef *)buff, length);
228         state->total_in += length;
229
230         /* Compress input data to output buffer */
231         SET_NEXT_IN(state, buff);
232         state->stream.avail_in = length;
233         if ((ret = drive_compressor(a, state, 0)) != ARCHIVE_OK)
234                 return (ret);
235
236         a->archive.file_position += length;
237         return (ARCHIVE_OK);
238 }
239
240
241 /*
242  * Finish the compression...
243  */
244 static int
245 archive_compressor_gzip_finish(struct archive_write *a)
246 {
247         ssize_t block_length, target_block_length, bytes_written;
248         int ret;
249         struct private_data *state;
250         unsigned tocopy;
251         unsigned char trailer[8];
252
253         state = (struct private_data *)a->compressor.data;
254         ret = 0;
255         if (a->client_writer == NULL) {
256                 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
257                     "No write callback is registered?  "
258                     "This is probably an internal programming error.");
259                 ret = ARCHIVE_FATAL;
260                 goto cleanup;
261         }
262
263         /* By default, always pad the uncompressed data. */
264         if (a->pad_uncompressed) {
265                 tocopy = a->bytes_per_block -
266                     (state->total_in % a->bytes_per_block);
267                 while (tocopy > 0 && tocopy < (unsigned)a->bytes_per_block) {
268                         SET_NEXT_IN(state, a->nulls);
269                         state->stream.avail_in = tocopy < a->null_length ?
270                             tocopy : a->null_length;
271                         state->crc = crc32(state->crc, a->nulls,
272                             state->stream.avail_in);
273                         state->total_in += state->stream.avail_in;
274                         tocopy -= state->stream.avail_in;
275                         ret = drive_compressor(a, state, 0);
276                         if (ret != ARCHIVE_OK)
277                                 goto cleanup;
278                 }
279         }
280
281         /* Finish compression cycle */
282         if (((ret = drive_compressor(a, state, 1))) != ARCHIVE_OK)
283                 goto cleanup;
284
285         /* Build trailer: 4-byte CRC and 4-byte length. */
286         trailer[0] = (state->crc)&0xff;
287         trailer[1] = (state->crc >> 8)&0xff;
288         trailer[2] = (state->crc >> 16)&0xff;
289         trailer[3] = (state->crc >> 24)&0xff;
290         trailer[4] = (state->total_in)&0xff;
291         trailer[5] = (state->total_in >> 8)&0xff;
292         trailer[6] = (state->total_in >> 16)&0xff;
293         trailer[7] = (state->total_in >> 24)&0xff;
294
295         /* Add trailer to current block. */
296         tocopy = 8;
297         if (tocopy > state->stream.avail_out)
298                 tocopy = state->stream.avail_out;
299         memcpy(state->stream.next_out, trailer, tocopy);
300         state->stream.next_out += tocopy;
301         state->stream.avail_out -= tocopy;
302
303         /* If it overflowed, flush and start a new block. */
304         if (tocopy < 8) {
305                 bytes_written = (a->client_writer)(&a->archive, a->client_data,
306                     state->compressed, state->compressed_buffer_size);
307                 if (bytes_written <= 0) {
308                         ret = ARCHIVE_FATAL;
309                         goto cleanup;
310                 }
311                 a->archive.raw_position += bytes_written;
312                 state->stream.next_out = state->compressed;
313                 state->stream.avail_out = state->compressed_buffer_size;
314                 memcpy(state->stream.next_out, trailer + tocopy, 8-tocopy);
315                 state->stream.next_out += 8-tocopy;
316                 state->stream.avail_out -= 8-tocopy;
317         }
318
319         /* Optionally, pad the final compressed block. */
320         block_length = state->stream.next_out - state->compressed;
321
322
323         /* Tricky calculation to determine size of last block. */
324         target_block_length = block_length;
325         if (a->bytes_in_last_block <= 0)
326                 /* Default or Zero: pad to full block */
327                 target_block_length = a->bytes_per_block;
328         else
329                 /* Round length to next multiple of bytes_in_last_block. */
330                 target_block_length = a->bytes_in_last_block *
331                     ( (block_length + a->bytes_in_last_block - 1) /
332                         a->bytes_in_last_block);
333         if (target_block_length > a->bytes_per_block)
334                 target_block_length = a->bytes_per_block;
335         if (block_length < target_block_length) {
336                 memset(state->stream.next_out, 0,
337                     target_block_length - block_length);
338                 block_length = target_block_length;
339         }
340
341         /* Write the last block */
342         bytes_written = (a->client_writer)(&a->archive, a->client_data,
343             state->compressed, block_length);
344         if (bytes_written <= 0) {
345                 ret = ARCHIVE_FATAL;
346                 goto cleanup;
347         }
348         a->archive.raw_position += bytes_written;
349
350         /* Cleanup: shut down compressor, release memory, etc. */
351 cleanup:
352         switch (deflateEnd(&(state->stream))) {
353         case Z_OK:
354                 break;
355         default:
356                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
357                     "Failed to clean up compressor");
358                 ret = ARCHIVE_FATAL;
359         }
360         free(state->compressed);
361         free(state);
362         return (ret);
363 }
364
365 /*
366  * Utility function to push input data through compressor,
367  * writing full output blocks as necessary.
368  *
369  * Note that this handles both the regular write case (finishing ==
370  * false) and the end-of-archive case (finishing == true).
371  */
372 static int
373 drive_compressor(struct archive_write *a, struct private_data *state, int finishing)
374 {
375         ssize_t bytes_written;
376         int ret;
377
378         for (;;) {
379                 if (state->stream.avail_out == 0) {
380                         bytes_written = (a->client_writer)(&a->archive,
381                             a->client_data, state->compressed,
382                             state->compressed_buffer_size);
383                         if (bytes_written <= 0) {
384                                 /* TODO: Handle this write failure */
385                                 return (ARCHIVE_FATAL);
386                         } else if ((size_t)bytes_written < state->compressed_buffer_size) {
387                                 /* Short write: Move remaining to
388                                  * front of block and keep filling */
389                                 memmove(state->compressed,
390                                     state->compressed + bytes_written,
391                                     state->compressed_buffer_size - bytes_written);
392                         }
393                         a->archive.raw_position += bytes_written;
394                         state->stream.next_out
395                             = state->compressed +
396                             state->compressed_buffer_size - bytes_written;
397                         state->stream.avail_out = bytes_written;
398                 }
399
400                 /* If there's nothing to do, we're done. */
401                 if (!finishing && state->stream.avail_in == 0)
402                         return (ARCHIVE_OK);
403
404                 ret = deflate(&(state->stream),
405                     finishing ? Z_FINISH : Z_NO_FLUSH );
406
407                 switch (ret) {
408                 case Z_OK:
409                         /* In non-finishing case, check if compressor
410                          * consumed everything */
411                         if (!finishing && state->stream.avail_in == 0)
412                                 return (ARCHIVE_OK);
413                         /* In finishing case, this return always means
414                          * there's more work */
415                         break;
416                 case Z_STREAM_END:
417                         /* This return can only occur in finishing case. */
418                         return (ARCHIVE_OK);
419                 default:
420                         /* Any other return value indicates an error. */
421                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
422                             "GZip compression failed:"
423                             " deflate() call returned status %d",
424                             ret);
425                         return (ARCHIVE_FATAL);
426                 }
427         }
428 }
429
430 #endif /* HAVE_ZLIB_H */