archive_write_add_filter_xz.c revision 228753
1/*-
2 * Copyright (c) 2009 Michihiro NAKAJIMA
3 * Copyright (c) 2003-2007 Tim Kientzle
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "archive_platform.h"
28
29__FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_compression_xz.c 201108 2009-12-28 03:28:21Z kientzle $");
30
31#ifdef HAVE_ERRNO_H
32#include <errno.h>
33#endif
34#ifdef HAVE_STDLIB_H
35#include <stdlib.h>
36#endif
37#ifdef HAVE_STRING_H
38#include <string.h>
39#endif
40#include <time.h>
41#ifdef HAVE_LZMA_H
42#include <lzma.h>
43#endif
44
45#include "archive.h"
46#include "archive_private.h"
47#include "archive_write_private.h"
48
49#ifndef HAVE_LZMA_H
50int
51archive_write_set_compression_xz(struct archive *a)
52{
53	archive_set_error(a, ARCHIVE_ERRNO_MISC,
54	    "xz compression not supported on this platform");
55	return (ARCHIVE_FATAL);
56}
57
58int
59archive_write_set_compression_lzma(struct archive *a)
60{
61	archive_set_error(a, ARCHIVE_ERRNO_MISC,
62	    "lzma compression not supported on this platform");
63	return (ARCHIVE_FATAL);
64}
65#else
66/* Don't compile this if we don't have liblzma. */
67
68struct private_data {
69	lzma_stream	 stream;
70	lzma_filter	 lzmafilters[2];
71	lzma_options_lzma lzma_opt;
72	int64_t		 total_in;
73	unsigned char	*compressed;
74	size_t		 compressed_buffer_size;
75};
76
77struct private_config {
78	int		 compression_level;
79};
80
81static int	archive_compressor_xz_init(struct archive_write *);
82static int	archive_compressor_xz_options(struct archive_write *,
83		    const char *, const char *);
84static int	archive_compressor_xz_finish(struct archive_write *);
85static int	archive_compressor_xz_write(struct archive_write *,
86		    const void *, size_t);
87static int	drive_compressor(struct archive_write *, struct private_data *,
88		    int finishing);
89
90
91/*
92 * Allocate, initialize and return a archive object.
93 */
94int
95archive_write_set_compression_xz(struct archive *_a)
96{
97	struct private_config *config;
98	struct archive_write *a = (struct archive_write *)_a;
99	__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
100	    ARCHIVE_STATE_NEW, "archive_write_set_compression_xz");
101	config = calloc(1, sizeof(*config));
102	if (config == NULL) {
103		archive_set_error(&a->archive, ENOMEM, "Out of memory");
104		return (ARCHIVE_FATAL);
105	}
106	a->compressor.config = config;
107	a->compressor.finish = archive_compressor_xz_finish;
108	config->compression_level = LZMA_PRESET_DEFAULT;
109	a->compressor.init = &archive_compressor_xz_init;
110	a->compressor.options = &archive_compressor_xz_options;
111	a->archive.compression_code = ARCHIVE_COMPRESSION_XZ;
112	a->archive.compression_name = "xz";
113	return (ARCHIVE_OK);
114}
115
116/* LZMA is handled identically, we just need a different compression
117 * code set.  (The liblzma setup looks at the code to determine
118 * the one place that XZ and LZMA require different handling.) */
119int
120archive_write_set_compression_lzma(struct archive *_a)
121{
122	struct archive_write *a = (struct archive_write *)_a;
123	int r = archive_write_set_compression_xz(_a);
124	if (r != ARCHIVE_OK)
125		return (r);
126	a->archive.compression_code = ARCHIVE_COMPRESSION_LZMA;
127	a->archive.compression_name = "lzma";
128	return (ARCHIVE_OK);
129}
130
131static int
132archive_compressor_xz_init_stream(struct archive_write *a,
133    struct private_data *state)
134{
135	static const lzma_stream lzma_stream_init_data = LZMA_STREAM_INIT;
136	int ret;
137
138	state->stream = lzma_stream_init_data;
139	state->stream.next_out = state->compressed;
140	state->stream.avail_out = state->compressed_buffer_size;
141	if (a->archive.compression_code == ARCHIVE_COMPRESSION_XZ)
142		ret = lzma_stream_encoder(&(state->stream),
143		    state->lzmafilters, LZMA_CHECK_CRC64);
144	else
145		ret = lzma_alone_encoder(&(state->stream), &state->lzma_opt);
146	if (ret == LZMA_OK)
147		return (ARCHIVE_OK);
148
149	switch (ret) {
150	case LZMA_MEM_ERROR:
151		archive_set_error(&a->archive, ENOMEM,
152		    "Internal error initializing compression library: "
153		    "Cannot allocate memory");
154		break;
155	default:
156		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
157		    "Internal error initializing compression library: "
158		    "It's a bug in liblzma");
159		break;
160	}
161	return (ARCHIVE_FATAL);
162}
163
164/*
165 * Setup callback.
166 */
167static int
168archive_compressor_xz_init(struct archive_write *a)
169{
170	int ret;
171	struct private_data *state;
172	struct private_config *config;
173
174	if (a->client_opener != NULL) {
175		ret = (a->client_opener)(&a->archive, a->client_data);
176		if (ret != ARCHIVE_OK)
177			return (ret);
178	}
179
180	state = (struct private_data *)malloc(sizeof(*state));
181	if (state == NULL) {
182		archive_set_error(&a->archive, ENOMEM,
183		    "Can't allocate data for compression");
184		return (ARCHIVE_FATAL);
185	}
186	memset(state, 0, sizeof(*state));
187	config = a->compressor.config;
188
189	/*
190	 * See comment above.  We should set compressed_buffer_size to
191	 * max(bytes_per_block, 65536), but the code can't handle that yet.
192	 */
193	state->compressed_buffer_size = a->bytes_per_block;
194	state->compressed = (unsigned char *)malloc(state->compressed_buffer_size);
195	if (state->compressed == NULL) {
196		archive_set_error(&a->archive, ENOMEM,
197		    "Can't allocate data for compression buffer");
198		free(state);
199		return (ARCHIVE_FATAL);
200	}
201	a->compressor.write = archive_compressor_xz_write;
202
203	/* Initialize compression library. */
204	if (lzma_lzma_preset(&state->lzma_opt, config->compression_level)) {
205		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
206		    "Internal error initializing compression library");
207		free(state->compressed);
208		free(state);
209	}
210	state->lzmafilters[0].id = LZMA_FILTER_LZMA2;
211	state->lzmafilters[0].options = &state->lzma_opt;
212	state->lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
213	ret = archive_compressor_xz_init_stream(a, state);
214	if (ret == LZMA_OK) {
215		a->compressor.data = state;
216		return (0);
217	}
218	/* Library setup failed: clean up. */
219	free(state->compressed);
220	free(state);
221
222	return (ARCHIVE_FATAL);
223}
224
225/*
226 * Set write options.
227 */
228static int
229archive_compressor_xz_options(struct archive_write *a, const char *key,
230    const char *value)
231{
232	struct private_config *config;
233
234	config = (struct private_config *)a->compressor.config;
235	if (strcmp(key, "compression-level") == 0) {
236		if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
237		    value[1] != '\0')
238			return (ARCHIVE_WARN);
239		config->compression_level = value[0] - '0';
240		if (config->compression_level > 6)
241			config->compression_level = 6;
242		return (ARCHIVE_OK);
243	}
244
245	return (ARCHIVE_WARN);
246}
247
248/*
249 * Write data to the compressed stream.
250 */
251static int
252archive_compressor_xz_write(struct archive_write *a, const void *buff,
253    size_t length)
254{
255	struct private_data *state;
256	int ret;
257
258	state = (struct private_data *)a->compressor.data;
259	if (a->client_writer == NULL) {
260		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
261		    "No write callback is registered?  "
262		    "This is probably an internal programming error.");
263		return (ARCHIVE_FATAL);
264	}
265
266	/* Update statistics */
267	state->total_in += length;
268
269	/* Compress input data to output buffer */
270	state->stream.next_in = buff;
271	state->stream.avail_in = length;
272	if ((ret = drive_compressor(a, state, 0)) != ARCHIVE_OK)
273		return (ret);
274
275	a->archive.file_position += length;
276	return (ARCHIVE_OK);
277}
278
279
280/*
281 * Finish the compression...
282 */
283static int
284archive_compressor_xz_finish(struct archive_write *a)
285{
286	ssize_t block_length, target_block_length, bytes_written;
287	int ret;
288	struct private_data *state;
289	unsigned tocopy;
290
291	ret = ARCHIVE_OK;
292	state = (struct private_data *)a->compressor.data;
293	if (state != NULL) {
294		if (a->client_writer == NULL) {
295			archive_set_error(&a->archive,
296			    ARCHIVE_ERRNO_PROGRAMMER,
297			    "No write callback is registered?  "
298			    "This is probably an internal programming error.");
299			ret = ARCHIVE_FATAL;
300			goto cleanup;
301		}
302
303		/* By default, always pad the uncompressed data. */
304		if (a->pad_uncompressed) {
305			tocopy = a->bytes_per_block -
306			    (state->total_in % a->bytes_per_block);
307			while (tocopy > 0 && tocopy < (unsigned)a->bytes_per_block) {
308				state->stream.next_in = a->nulls;
309				state->stream.avail_in = tocopy < a->null_length ?
310				    tocopy : a->null_length;
311				state->total_in += state->stream.avail_in;
312				tocopy -= state->stream.avail_in;
313				ret = drive_compressor(a, state, 0);
314				if (ret != ARCHIVE_OK)
315					goto cleanup;
316			}
317		}
318
319		/* Finish compression cycle */
320		if (((ret = drive_compressor(a, state, 1))) != ARCHIVE_OK)
321			goto cleanup;
322
323		/* Optionally, pad the final compressed block. */
324		block_length = state->stream.next_out - state->compressed;
325
326		/* Tricky calculation to determine size of last block. */
327		if (a->bytes_in_last_block <= 0)
328			/* Default or Zero: pad to full block */
329			target_block_length = a->bytes_per_block;
330		else
331			/* Round length to next multiple of bytes_in_last_block. */
332			target_block_length = a->bytes_in_last_block *
333			    ( (block_length + a->bytes_in_last_block - 1) /
334				a->bytes_in_last_block);
335		if (target_block_length > a->bytes_per_block)
336			target_block_length = a->bytes_per_block;
337		if (block_length < target_block_length) {
338			memset(state->stream.next_out, 0,
339			    target_block_length - block_length);
340			block_length = target_block_length;
341		}
342
343		/* Write the last block */
344		bytes_written = (a->client_writer)(&a->archive, a->client_data,
345		    state->compressed, block_length);
346		if (bytes_written <= 0) {
347			ret = ARCHIVE_FATAL;
348			goto cleanup;
349		}
350		a->archive.raw_position += bytes_written;
351
352		/* Cleanup: shut down compressor, release memory, etc. */
353	cleanup:
354		lzma_end(&(state->stream));
355		free(state->compressed);
356		free(state);
357	}
358	free(a->compressor.config);
359	a->compressor.config = NULL;
360	return (ret);
361}
362
363/*
364 * Utility function to push input data through compressor,
365 * writing full output blocks as necessary.
366 *
367 * Note that this handles both the regular write case (finishing ==
368 * false) and the end-of-archive case (finishing == true).
369 */
370static int
371drive_compressor(struct archive_write *a, struct private_data *state, int finishing)
372{
373	ssize_t bytes_written;
374	int ret;
375
376	for (;;) {
377		if (state->stream.avail_out == 0) {
378			bytes_written = (a->client_writer)(&a->archive,
379			    a->client_data, state->compressed,
380			    state->compressed_buffer_size);
381			if (bytes_written <= 0) {
382				/* TODO: Handle this write failure */
383				return (ARCHIVE_FATAL);
384			} else if ((size_t)bytes_written < state->compressed_buffer_size) {
385				/* Short write: Move remaining to
386				 * front of block and keep filling */
387				memmove(state->compressed,
388				    state->compressed + bytes_written,
389				    state->compressed_buffer_size - bytes_written);
390			}
391			a->archive.raw_position += bytes_written;
392			state->stream.next_out
393			    = state->compressed +
394			    state->compressed_buffer_size - bytes_written;
395			state->stream.avail_out = bytes_written;
396		}
397
398		/* If there's nothing to do, we're done. */
399		if (!finishing && state->stream.avail_in == 0)
400			return (ARCHIVE_OK);
401
402		ret = lzma_code(&(state->stream),
403		    finishing ? LZMA_FINISH : LZMA_RUN );
404
405		switch (ret) {
406		case LZMA_OK:
407			/* In non-finishing case, check if compressor
408			 * consumed everything */
409			if (!finishing && state->stream.avail_in == 0)
410				return (ARCHIVE_OK);
411			/* In finishing case, this return always means
412			 * there's more work */
413			break;
414		case LZMA_STREAM_END:
415			/* This return can only occur in finishing case. */
416			if (finishing)
417				return (ARCHIVE_OK);
418			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
419			    "lzma compression data error");
420			return (ARCHIVE_FATAL);
421		case LZMA_MEMLIMIT_ERROR:
422			archive_set_error(&a->archive, ENOMEM,
423			    "lzma compression error: "
424			    "%ju MiB would have been needed",
425			    (uintmax_t)((lzma_memusage(&(state->stream)) + 1024 * 1024 -1)
426				/ (1024 * 1024)));
427			return (ARCHIVE_FATAL);
428		default:
429			/* Any other return value indicates an error. */
430			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
431			    "lzma compression failed:"
432			    " lzma_code() call returned status %d",
433			    ret);
434			return (ARCHIVE_FATAL);
435		}
436	}
437}
438
439#endif /* HAVE_LZMA_H */
440