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 #ifdef HAVE_ERRNO_H
29 #include <errno.h>
30 #endif
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h>
33 #endif
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #endif
37 #include <time.h>
38 #ifdef HAVE_ZLIB_H
39 #include <zlib.h>
40 #endif
41
42 #include "archive.h"
43 #include "archive_private.h"
44 #include "archive_string.h"
45 #include "archive_write_private.h"
46
47 #if ARCHIVE_VERSION_NUMBER < 4000000
48 int
archive_write_set_compression_gzip(struct archive * a)49 archive_write_set_compression_gzip(struct archive *a)
50 {
51 __archive_write_filters_free(a);
52 return (archive_write_add_filter_gzip(a));
53 }
54 #endif
55
56 /* Don't compile this if we don't have zlib. */
57
58 struct private_data {
59 int compression_level;
60 int timestamp;
61 char *original_filename;
62 #ifdef HAVE_ZLIB_H
63 z_stream stream;
64 int64_t total_in;
65 unsigned char *compressed;
66 size_t compressed_buffer_size;
67 unsigned long crc;
68 #else
69 struct archive_write_program_data *pdata;
70 #endif
71 };
72
73 /*
74 * Yuck. zlib.h is not const-correct, so I need this one bit
75 * of ugly hackery to convert a const * pointer to a non-const pointer.
76 */
77 #define SET_NEXT_IN(st,src) \
78 (st)->stream.next_in = (Bytef *)(uintptr_t)(const void *)(src)
79
80 static int archive_compressor_gzip_options(struct archive_write_filter *,
81 const char *, const char *);
82 static int archive_compressor_gzip_open(struct archive_write_filter *);
83 static int archive_compressor_gzip_write(struct archive_write_filter *,
84 const void *, size_t);
85 static int archive_compressor_gzip_close(struct archive_write_filter *);
86 static int archive_compressor_gzip_free(struct archive_write_filter *);
87 #ifdef HAVE_ZLIB_H
88 static int drive_compressor(struct archive_write_filter *,
89 struct private_data *, int finishing);
90 #endif
91
92
93 /*
94 * Add a gzip compression filter to this write handle.
95 */
96 int
archive_write_add_filter_gzip(struct archive * _a)97 archive_write_add_filter_gzip(struct archive *_a)
98 {
99 struct archive_write *a = (struct archive_write *)_a;
100 struct archive_write_filter *f = __archive_write_allocate_filter(_a);
101 struct private_data *data;
102 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
103 ARCHIVE_STATE_NEW, "archive_write_add_filter_gzip");
104
105 data = calloc(1, sizeof(*data));
106 if (data == NULL) {
107 archive_set_error(&a->archive, ENOMEM, "Out of memory");
108 return (ARCHIVE_FATAL);
109 }
110 f->data = data;
111 f->open = &archive_compressor_gzip_open;
112 f->options = &archive_compressor_gzip_options;
113 f->close = &archive_compressor_gzip_close;
114 f->free = &archive_compressor_gzip_free;
115 f->code = ARCHIVE_FILTER_GZIP;
116 f->name = "gzip";
117
118 data->original_filename = NULL;
119 #ifdef HAVE_ZLIB_H
120 data->compression_level = Z_DEFAULT_COMPRESSION;
121 return (ARCHIVE_OK);
122 #else
123 data->pdata = __archive_write_program_allocate("gzip");
124 if (data->pdata == NULL) {
125 free(data);
126 archive_set_error(&a->archive, ENOMEM, "Out of memory");
127 return (ARCHIVE_FATAL);
128 }
129 data->compression_level = 0;
130 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
131 "Using external gzip program");
132 return (ARCHIVE_WARN);
133 #endif
134 }
135
136 static int
archive_compressor_gzip_free(struct archive_write_filter * f)137 archive_compressor_gzip_free(struct archive_write_filter *f)
138 {
139 struct private_data *data = (struct private_data *)f->data;
140
141 #ifdef HAVE_ZLIB_H
142 free(data->compressed);
143 #else
144 __archive_write_program_free(data->pdata);
145 #endif
146 free((void*)data->original_filename);
147 free(data);
148 f->data = NULL;
149 return (ARCHIVE_OK);
150 }
151
152 /*
153 * Set write options.
154 */
155 static int
archive_compressor_gzip_options(struct archive_write_filter * f,const char * key,const char * value)156 archive_compressor_gzip_options(struct archive_write_filter *f, const char *key,
157 const char *value)
158 {
159 struct private_data *data = (struct private_data *)f->data;
160
161 if (strcmp(key, "compression-level") == 0) {
162 if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
163 value[1] != '\0')
164 return (ARCHIVE_WARN);
165 data->compression_level = value[0] - '0';
166 return (ARCHIVE_OK);
167 }
168 if (strcmp(key, "timestamp") == 0) {
169 data->timestamp = (value == NULL)?-1:1;
170 return (ARCHIVE_OK);
171 }
172 if (strcmp(key, "original-filename") == 0) {
173 free((void*)data->original_filename);
174 data->original_filename = NULL;
175 if (value)
176 data->original_filename = strdup(value);
177 return (ARCHIVE_OK);
178 }
179
180 /* Note: The "warn" return is just to inform the options
181 * supervisor that we didn't handle it. It will generate
182 * a suitable error if no one used this option. */
183 return (ARCHIVE_WARN);
184 }
185
186 #ifdef HAVE_ZLIB_H
187 /*
188 * Setup callback.
189 */
190 static int
archive_compressor_gzip_open(struct archive_write_filter * f)191 archive_compressor_gzip_open(struct archive_write_filter *f)
192 {
193 struct private_data *data = (struct private_data *)f->data;
194 int ret;
195
196 if (data->compressed == NULL) {
197 size_t bs = 65536, bpb;
198 if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
199 /* Buffer size should be a multiple number of
200 * the of bytes per block for performance. */
201 bpb = archive_write_get_bytes_per_block(f->archive);
202 if (bpb > bs)
203 bs = bpb;
204 else if (bpb != 0)
205 bs -= bs % bpb;
206 }
207 data->compressed_buffer_size = bs;
208 data->compressed = malloc(data->compressed_buffer_size);
209 if (data->compressed == NULL) {
210 archive_set_error(f->archive, ENOMEM,
211 "Can't allocate data for compression buffer");
212 return (ARCHIVE_FATAL);
213 }
214 }
215
216 data->crc = crc32(0L, NULL, 0);
217 data->stream.next_out = data->compressed;
218 data->stream.avail_out = (uInt)data->compressed_buffer_size;
219
220 /* Prime output buffer with a gzip header. */
221 data->compressed[0] = 0x1f; /* GZip signature bytes */
222 data->compressed[1] = 0x8b;
223 data->compressed[2] = 0x08; /* "Deflate" compression */
224 data->compressed[3] = data->original_filename == NULL ? 0 : 0x8;
225 if (data->timestamp >= 0) {
226 time_t t = time(NULL);
227 data->compressed[4] = (uint8_t)(t)&0xff; /* Timestamp */
228 data->compressed[5] = (uint8_t)(t>>8)&0xff;
229 data->compressed[6] = (uint8_t)(t>>16)&0xff;
230 data->compressed[7] = (uint8_t)(t>>24)&0xff;
231 } else
232 memset(&data->compressed[4], 0, 4);
233 if (data->compression_level == 9)
234 data->compressed[8] = 2;
235 else if(data->compression_level == 1)
236 data->compressed[8] = 4;
237 else
238 data->compressed[8] = 0;
239 data->compressed[9] = 3; /* OS=Unix */
240 data->stream.next_out += 10;
241 data->stream.avail_out -= 10;
242
243 if (data->original_filename != NULL) {
244 strcpy((char*)data->compressed + 10, data->original_filename);
245 data->stream.next_out += strlen(data->original_filename) + 1;
246 data->stream.avail_out -= strlen(data->original_filename) + 1;
247 }
248
249 f->write = archive_compressor_gzip_write;
250
251 /* Initialize compression library. */
252 ret = deflateInit2(&(data->stream),
253 data->compression_level,
254 Z_DEFLATED,
255 -15 /* < 0 to suppress zlib header */,
256 8,
257 Z_DEFAULT_STRATEGY);
258
259 if (ret == Z_OK) {
260 f->data = data;
261 return (ARCHIVE_OK);
262 }
263
264 /* Library setup failed: clean up. */
265 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC, "Internal error "
266 "initializing compression library");
267
268 /* Override the error message if we know what really went wrong. */
269 switch (ret) {
270 case Z_STREAM_ERROR:
271 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
272 "Internal error initializing "
273 "compression library: invalid setup parameter");
274 break;
275 case Z_MEM_ERROR:
276 archive_set_error(f->archive, ENOMEM,
277 "Internal error initializing compression library");
278 break;
279 case Z_VERSION_ERROR:
280 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
281 "Internal error initializing "
282 "compression library: invalid library version");
283 break;
284 }
285
286 return (ARCHIVE_FATAL);
287 }
288
289 /*
290 * Write data to the compressed stream.
291 */
292 static int
archive_compressor_gzip_write(struct archive_write_filter * f,const void * buff,size_t length)293 archive_compressor_gzip_write(struct archive_write_filter *f, const void *buff,
294 size_t length)
295 {
296 struct private_data *data = (struct private_data *)f->data;
297 int ret;
298
299 /* Update statistics */
300 data->crc = crc32(data->crc, (const Bytef *)buff, (uInt)length);
301 data->total_in += length;
302
303 /* Compress input data to output buffer */
304 SET_NEXT_IN(data, buff);
305 data->stream.avail_in = (uInt)length;
306 if ((ret = drive_compressor(f, data, 0)) != ARCHIVE_OK)
307 return (ret);
308
309 return (ARCHIVE_OK);
310 }
311
312 /*
313 * Finish the compression...
314 */
315 static int
archive_compressor_gzip_close(struct archive_write_filter * f)316 archive_compressor_gzip_close(struct archive_write_filter *f)
317 {
318 unsigned char trailer[8];
319 struct private_data *data = (struct private_data *)f->data;
320 int ret;
321
322 /* Finish compression cycle */
323 ret = drive_compressor(f, data, 1);
324 if (ret == ARCHIVE_OK) {
325 /* Write the last compressed data. */
326 ret = __archive_write_filter(f->next_filter,
327 data->compressed,
328 data->compressed_buffer_size - data->stream.avail_out);
329 }
330 if (ret == ARCHIVE_OK) {
331 /* Build and write out 8-byte trailer. */
332 trailer[0] = (uint8_t)(data->crc)&0xff;
333 trailer[1] = (uint8_t)(data->crc >> 8)&0xff;
334 trailer[2] = (uint8_t)(data->crc >> 16)&0xff;
335 trailer[3] = (uint8_t)(data->crc >> 24)&0xff;
336 trailer[4] = (uint8_t)(data->total_in)&0xff;
337 trailer[5] = (uint8_t)(data->total_in >> 8)&0xff;
338 trailer[6] = (uint8_t)(data->total_in >> 16)&0xff;
339 trailer[7] = (uint8_t)(data->total_in >> 24)&0xff;
340 ret = __archive_write_filter(f->next_filter, trailer, 8);
341 }
342
343 switch (deflateEnd(&(data->stream))) {
344 case Z_OK:
345 break;
346 default:
347 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
348 "Failed to clean up compressor");
349 ret = ARCHIVE_FATAL;
350 }
351 return ret;
352 }
353
354 /*
355 * Utility function to push input data through compressor,
356 * writing full output blocks as necessary.
357 *
358 * Note that this handles both the regular write case (finishing ==
359 * false) and the end-of-archive case (finishing == true).
360 */
361 static int
drive_compressor(struct archive_write_filter * f,struct private_data * data,int finishing)362 drive_compressor(struct archive_write_filter *f,
363 struct private_data *data, int finishing)
364 {
365 int ret;
366
367 for (;;) {
368 if (data->stream.avail_out == 0) {
369 ret = __archive_write_filter(f->next_filter,
370 data->compressed,
371 data->compressed_buffer_size);
372 if (ret != ARCHIVE_OK)
373 return (ARCHIVE_FATAL);
374 data->stream.next_out = data->compressed;
375 data->stream.avail_out =
376 (uInt)data->compressed_buffer_size;
377 }
378
379 /* If there's nothing to do, we're done. */
380 if (!finishing && data->stream.avail_in == 0)
381 return (ARCHIVE_OK);
382
383 ret = deflate(&(data->stream),
384 finishing ? Z_FINISH : Z_NO_FLUSH );
385
386 switch (ret) {
387 case Z_OK:
388 /* In non-finishing case, check if compressor
389 * consumed everything */
390 if (!finishing && data->stream.avail_in == 0)
391 return (ARCHIVE_OK);
392 /* In finishing case, this return always means
393 * there's more work */
394 break;
395 case Z_STREAM_END:
396 /* This return can only occur in finishing case. */
397 return (ARCHIVE_OK);
398 default:
399 /* Any other return value indicates an error. */
400 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
401 "GZip compression failed:"
402 " deflate() call returned status %d",
403 ret);
404 return (ARCHIVE_FATAL);
405 }
406 }
407 }
408
409 #else /* HAVE_ZLIB_H */
410
411 static int
archive_compressor_gzip_open(struct archive_write_filter * f)412 archive_compressor_gzip_open(struct archive_write_filter *f)
413 {
414 struct private_data *data = (struct private_data *)f->data;
415 struct archive_string as;
416 int r;
417
418 archive_string_init(&as);
419 archive_strcpy(&as, "gzip");
420
421 /* Specify compression level. */
422 if (data->compression_level > 0) {
423 archive_strcat(&as, " -");
424 archive_strappend_char(&as, '0' + data->compression_level);
425 }
426 if (data->timestamp < 0)
427 /* Do not save timestamp. */
428 archive_strcat(&as, " -n");
429 else if (data->timestamp > 0)
430 /* Save timestamp. */
431 archive_strcat(&as, " -N");
432
433 f->write = archive_compressor_gzip_write;
434 r = __archive_write_program_open(f, data->pdata, as.s);
435 archive_string_free(&as);
436 return (r);
437 }
438
439 static int
archive_compressor_gzip_write(struct archive_write_filter * f,const void * buff,size_t length)440 archive_compressor_gzip_write(struct archive_write_filter *f, const void *buff,
441 size_t length)
442 {
443 struct private_data *data = (struct private_data *)f->data;
444
445 return __archive_write_program_write(f, data->pdata, buff, length);
446 }
447
448 static int
archive_compressor_gzip_close(struct archive_write_filter * f)449 archive_compressor_gzip_close(struct archive_write_filter *f)
450 {
451 struct private_data *data = (struct private_data *)f->data;
452
453 return __archive_write_program_close(f, data->pdata);
454 }
455
456 #endif /* HAVE_ZLIB_H */
457