xref: /freebsd/contrib/libarchive/libarchive/archive_write_add_filter_gzip.c (revision 401026e4825a05abba6f945cf1b74b3328876fa2)
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 = ARCHIVE_OK;
195 	int init_success;
196 
197 	if (data->compressed == NULL) {
198 		size_t bs = 65536, bpb;
199 		if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
200 			/* Buffer size should be a multiple number of
201 			 * the of bytes per block for performance. */
202 			bpb = archive_write_get_bytes_per_block(f->archive);
203 			if (bpb > bs)
204 				bs = bpb;
205 			else if (bpb != 0)
206 				bs -= bs % bpb;
207 		}
208 		data->compressed_buffer_size = bs;
209 		data->compressed = malloc(data->compressed_buffer_size);
210 		if (data->compressed == NULL) {
211 			archive_set_error(f->archive, ENOMEM,
212 			    "Can't allocate data for compression buffer");
213 			return (ARCHIVE_FATAL);
214 		}
215 	}
216 
217 	data->crc = crc32(0L, NULL, 0);
218 	data->stream.next_out = data->compressed;
219 	data->stream.avail_out = (uInt)data->compressed_buffer_size;
220 
221 	/* Prime output buffer with a gzip header. */
222 	data->compressed[0] = 0x1f; /* GZip signature bytes */
223 	data->compressed[1] = 0x8b;
224 	data->compressed[2] = 0x08; /* "Deflate" compression */
225 	data->compressed[3] = 0x00; /* Flags */
226 	if (data->timestamp >= 0) {
227 		time_t t = time(NULL);
228 		data->compressed[4] = (uint8_t)(t)&0xff;  /* Timestamp */
229 		data->compressed[5] = (uint8_t)(t>>8)&0xff;
230 		data->compressed[6] = (uint8_t)(t>>16)&0xff;
231 		data->compressed[7] = (uint8_t)(t>>24)&0xff;
232 	} else {
233 		memset(&data->compressed[4], 0, 4);
234 	}
235 	if (data->compression_level == 9) {
236 		data->compressed[8] = 2;
237 	} else if(data->compression_level == 1) {
238 		data->compressed[8] = 4;
239 	} else {
240 		data->compressed[8] = 0;
241 	}
242 	data->compressed[9] = 3; /* OS=Unix */
243 	data->stream.next_out += 10;
244 	data->stream.avail_out -= 10;
245 
246 	if (data->original_filename != NULL) {
247 		/* Limit "original filename" to 32k or the
248 		 * remaining space in the buffer, whichever is smaller.
249 		 */
250 		int ofn_length = strlen(data->original_filename);
251 		int ofn_max_length = 32768;
252 		int ofn_space_available = data->compressed
253 			+ data->compressed_buffer_size
254 			- data->stream.next_out
255 			- 1;
256 		if (ofn_max_length > ofn_space_available) {
257 			ofn_max_length = ofn_space_available;
258 		}
259 		if (ofn_length < ofn_max_length) {
260 			data->compressed[3] |= 0x8;
261 			strcpy((char*)data->compressed + 10,
262 			       data->original_filename);
263 			data->stream.next_out += ofn_length + 1;
264 			data->stream.avail_out -= ofn_length + 1;
265 		} else {
266 			archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
267 					  "Gzip 'Original Filename' ignored because it is too long");
268 			ret = ARCHIVE_WARN;
269 		}
270 	}
271 
272 	f->write = archive_compressor_gzip_write;
273 
274 	/* Initialize compression library. */
275 	init_success = deflateInit2(&(data->stream),
276 	    data->compression_level,
277 	    Z_DEFLATED,
278 	    -15 /* < 0 to suppress zlib header */,
279 	    8,
280 	    Z_DEFAULT_STRATEGY);
281 
282 	if (init_success == Z_OK) {
283 		f->data = data;
284 		return (ret);
285 	}
286 
287 	/* Library setup failed: clean up. */
288 	archive_set_error(f->archive, ARCHIVE_ERRNO_MISC, "Internal error "
289 	    "initializing compression library");
290 
291 	/* Override the error message if we know what really went wrong. */
292 	switch (init_success) {
293 	case Z_STREAM_ERROR:
294 		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
295 		    "Internal error initializing "
296 		    "compression library: invalid setup parameter");
297 		break;
298 	case Z_MEM_ERROR:
299 		archive_set_error(f->archive, ENOMEM,
300 		    "Internal error initializing compression library");
301 		break;
302 	case Z_VERSION_ERROR:
303 		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
304 		    "Internal error initializing "
305 		    "compression library: invalid library version");
306 		break;
307 	}
308 
309 	return (ARCHIVE_FATAL);
310 }
311 
312 /*
313  * Write data to the compressed stream.
314  */
315 static int
archive_compressor_gzip_write(struct archive_write_filter * f,const void * buff,size_t length)316 archive_compressor_gzip_write(struct archive_write_filter *f, const void *buff,
317     size_t length)
318 {
319 	struct private_data *data = (struct private_data *)f->data;
320 	int ret;
321 
322 	/* Update statistics */
323 	data->crc = crc32(data->crc, (const Bytef *)buff, (uInt)length);
324 	data->total_in += length;
325 
326 	/* Compress input data to output buffer */
327 	SET_NEXT_IN(data, buff);
328 	data->stream.avail_in = (uInt)length;
329 	if ((ret = drive_compressor(f, data, 0)) != ARCHIVE_OK)
330 		return (ret);
331 
332 	return (ARCHIVE_OK);
333 }
334 
335 /*
336  * Finish the compression...
337  */
338 static int
archive_compressor_gzip_close(struct archive_write_filter * f)339 archive_compressor_gzip_close(struct archive_write_filter *f)
340 {
341 	unsigned char trailer[8];
342 	struct private_data *data = (struct private_data *)f->data;
343 	int ret;
344 
345 	/* Finish compression cycle */
346 	ret = drive_compressor(f, data, 1);
347 	if (ret == ARCHIVE_OK) {
348 		/* Write the last compressed data. */
349 		ret = __archive_write_filter(f->next_filter,
350 		    data->compressed,
351 		    data->compressed_buffer_size - data->stream.avail_out);
352 	}
353 	if (ret == ARCHIVE_OK) {
354 		/* Build and write out 8-byte trailer. */
355 		trailer[0] = (uint8_t)(data->crc)&0xff;
356 		trailer[1] = (uint8_t)(data->crc >> 8)&0xff;
357 		trailer[2] = (uint8_t)(data->crc >> 16)&0xff;
358 		trailer[3] = (uint8_t)(data->crc >> 24)&0xff;
359 		trailer[4] = (uint8_t)(data->total_in)&0xff;
360 		trailer[5] = (uint8_t)(data->total_in >> 8)&0xff;
361 		trailer[6] = (uint8_t)(data->total_in >> 16)&0xff;
362 		trailer[7] = (uint8_t)(data->total_in >> 24)&0xff;
363 		ret = __archive_write_filter(f->next_filter, trailer, 8);
364 	}
365 
366 	switch (deflateEnd(&(data->stream))) {
367 	case Z_OK:
368 		break;
369 	default:
370 		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
371 		    "Failed to clean up compressor");
372 		ret = ARCHIVE_FATAL;
373 	}
374 	return ret;
375 }
376 
377 /*
378  * Utility function to push input data through compressor,
379  * writing full output blocks as necessary.
380  *
381  * Note that this handles both the regular write case (finishing ==
382  * false) and the end-of-archive case (finishing == true).
383  */
384 static int
drive_compressor(struct archive_write_filter * f,struct private_data * data,int finishing)385 drive_compressor(struct archive_write_filter *f,
386     struct private_data *data, int finishing)
387 {
388 	int ret;
389 
390 	for (;;) {
391 		if (data->stream.avail_out == 0) {
392 			ret = __archive_write_filter(f->next_filter,
393 			    data->compressed,
394 			    data->compressed_buffer_size);
395 			if (ret != ARCHIVE_OK)
396 				return (ARCHIVE_FATAL);
397 			data->stream.next_out = data->compressed;
398 			data->stream.avail_out =
399 			    (uInt)data->compressed_buffer_size;
400 		}
401 
402 		/* If there's nothing to do, we're done. */
403 		if (!finishing && data->stream.avail_in == 0)
404 			return (ARCHIVE_OK);
405 
406 		ret = deflate(&(data->stream),
407 		    finishing ? Z_FINISH : Z_NO_FLUSH );
408 
409 		switch (ret) {
410 		case Z_OK:
411 			/* In non-finishing case, check if compressor
412 			 * consumed everything */
413 			if (!finishing && data->stream.avail_in == 0)
414 				return (ARCHIVE_OK);
415 			/* In finishing case, this return always means
416 			 * there's more work */
417 			break;
418 		case Z_STREAM_END:
419 			/* This return can only occur in finishing case. */
420 			return (ARCHIVE_OK);
421 		default:
422 			/* Any other return value indicates an error. */
423 			archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
424 			    "GZip compression failed:"
425 			    " deflate() call returned status %d",
426 			    ret);
427 			return (ARCHIVE_FATAL);
428 		}
429 	}
430 }
431 
432 #else /* HAVE_ZLIB_H */
433 
434 static int
archive_compressor_gzip_open(struct archive_write_filter * f)435 archive_compressor_gzip_open(struct archive_write_filter *f)
436 {
437 	struct private_data *data = (struct private_data *)f->data;
438 	struct archive_string as;
439 	int r;
440 
441 	archive_string_init(&as);
442 	archive_strcpy(&as, "gzip");
443 
444 	/* Specify compression level. */
445 	if (data->compression_level > 0) {
446 		archive_strcat(&as, " -");
447 		archive_strappend_char(&as, '0' + data->compression_level);
448 	}
449 	if (data->timestamp < 0)
450 		/* Do not save timestamp. */
451 		archive_strcat(&as, " -n");
452 	else if (data->timestamp > 0)
453 		/* Save timestamp. */
454 		archive_strcat(&as, " -N");
455 
456 	f->write = archive_compressor_gzip_write;
457 	r = __archive_write_program_open(f, data->pdata, as.s);
458 	archive_string_free(&as);
459 	return (r);
460 }
461 
462 static int
archive_compressor_gzip_write(struct archive_write_filter * f,const void * buff,size_t length)463 archive_compressor_gzip_write(struct archive_write_filter *f, const void *buff,
464     size_t length)
465 {
466 	struct private_data *data = (struct private_data *)f->data;
467 
468 	return __archive_write_program_write(f, data->pdata, buff, length);
469 }
470 
471 static int
archive_compressor_gzip_close(struct archive_write_filter * f)472 archive_compressor_gzip_close(struct archive_write_filter *f)
473 {
474 	struct private_data *data = (struct private_data *)f->data;
475 
476 	return __archive_write_program_close(f, data->pdata);
477 }
478 
479 #endif /* HAVE_ZLIB_H */
480