archive_read_support_filter_zstd.c revision 324417
1/*-
2 * Copyright (c) 2009-2011 Sean Purcell
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: stable/11/contrib/libarchive/libarchive/archive_read_support_filter_zstd.c 324417 2017-10-08 20:54:53Z mm $");
29
30#ifdef HAVE_ERRNO_H
31#include <errno.h>
32#endif
33
34#ifdef HAVE_ERRNO_H
35#include <errno.h>
36#endif
37#include <stdio.h>
38#ifdef HAVE_STDLIB_H
39#include <stdlib.h>
40#endif
41#ifdef HAVE_STRING_H
42#include <string.h>
43#endif
44#ifdef HAVE_UNISTD_H
45#include <unistd.h>
46#endif
47#if HAVE_ZSTD_H
48#include <zstd.h>
49#endif
50
51#include "archive.h"
52#include "archive_endian.h"
53#include "archive_private.h"
54#include "archive_read_private.h"
55
56#if HAVE_ZSTD_H && HAVE_LIBZSTD
57
58struct private_data {
59	ZSTD_DStream	*dstream;
60	unsigned char	*out_block;
61	size_t		 out_block_size;
62	int64_t		 total_out;
63	char		 in_frame; /* True = in the middle of a zstd frame. */
64	char		 eof; /* True = found end of compressed data. */
65};
66
67/* Zstd Filter. */
68static ssize_t	zstd_filter_read(struct archive_read_filter *, const void**);
69static int	zstd_filter_close(struct archive_read_filter *);
70#endif
71
72/*
73 * Note that we can detect zstd compressed files even if we can't decompress
74 * them.  (In fact, we like detecting them because we can give better error
75 * messages.)  So the bid framework here gets compiled even if no zstd library
76 * is available.
77 */
78static int	zstd_bidder_bid(struct archive_read_filter_bidder *,
79		    struct archive_read_filter *);
80static int	zstd_bidder_init(struct archive_read_filter *);
81
82int
83archive_read_support_filter_zstd(struct archive *_a)
84{
85	struct archive_read *a = (struct archive_read *)_a;
86	struct archive_read_filter_bidder *bidder;
87
88	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
89	    ARCHIVE_STATE_NEW, "archive_read_support_filter_zstd");
90
91	if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
92		return (ARCHIVE_FATAL);
93
94	bidder->data = NULL;
95	bidder->name = "zstd";
96	bidder->bid = zstd_bidder_bid;
97	bidder->init = zstd_bidder_init;
98	bidder->options = NULL;
99	bidder->free = NULL;
100#if HAVE_ZSTD_H && HAVE_LIBZSTD
101	return (ARCHIVE_OK);
102#else
103	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
104	    "Using external zstd program for zstd decompression");
105	return (ARCHIVE_WARN);
106#endif
107}
108
109/*
110 * Test whether we can handle this data.
111 */
112static int
113zstd_bidder_bid(struct archive_read_filter_bidder *self,
114    struct archive_read_filter *filter)
115{
116	const unsigned char *buffer;
117	ssize_t avail;
118	unsigned prefix;
119
120	/* Zstd frame magic values */
121	const unsigned zstd_magic = 0xFD2FB528U;
122
123	(void) self; /* UNUSED */
124
125	buffer = __archive_read_filter_ahead(filter, 4, &avail);
126	if (buffer == NULL)
127		return (0);
128
129	prefix = archive_le32dec(buffer);
130	if (prefix == zstd_magic)
131		return (32);
132
133	return (0);
134}
135
136#if !(HAVE_ZSTD_H && HAVE_LIBZSTD)
137
138/*
139 * If we don't have the library on this system, we can't do the
140 * decompression directly.  We can, however, try to run "zstd -d"
141 * in case that's available.
142 */
143static int
144zstd_bidder_init(struct archive_read_filter *self)
145{
146	int r;
147
148	r = __archive_read_program(self, "zstd -d -qq");
149	/* Note: We set the format here even if __archive_read_program()
150	 * above fails.  We do, after all, know what the format is
151	 * even if we weren't able to read it. */
152	self->code = ARCHIVE_FILTER_ZSTD;
153	self->name = "zstd";
154	return (r);
155}
156
157#else
158
159/*
160 * Initialize the filter object
161 */
162static int
163zstd_bidder_init(struct archive_read_filter *self)
164{
165	struct private_data *state;
166	const size_t out_block_size = ZSTD_DStreamOutSize();
167	void *out_block;
168	ZSTD_DStream *dstream;
169
170	self->code = ARCHIVE_FILTER_ZSTD;
171	self->name = "zstd";
172
173	state = (struct private_data *)calloc(sizeof(*state), 1);
174	out_block = (unsigned char *)malloc(out_block_size);
175	dstream = ZSTD_createDStream();
176
177	if (state == NULL || out_block == NULL || dstream == NULL) {
178		free(out_block);
179		free(state);
180		ZSTD_freeDStream(dstream); /* supports free on NULL */
181		archive_set_error(&self->archive->archive, ENOMEM,
182		    "Can't allocate data for zstd decompression");
183		return (ARCHIVE_FATAL);
184	}
185
186	self->data = state;
187
188	state->out_block_size = out_block_size;
189	state->out_block = out_block;
190	state->dstream = dstream;
191	self->read = zstd_filter_read;
192	self->skip = NULL; /* not supported */
193	self->close = zstd_filter_close;
194
195	state->eof = 0;
196	state->in_frame = 0;
197
198	return (ARCHIVE_OK);
199}
200
201static ssize_t
202zstd_filter_read(struct archive_read_filter *self, const void **p)
203{
204	struct private_data *state;
205	size_t decompressed;
206	ssize_t avail_in;
207	ZSTD_outBuffer out;
208	ZSTD_inBuffer in;
209
210	state = (struct private_data *)self->data;
211
212	out = (ZSTD_outBuffer) { state->out_block, state->out_block_size, 0 };
213
214	/* Try to fill the output buffer. */
215	while (out.pos < out.size && !state->eof) {
216		if (!state->in_frame) {
217			const size_t ret = ZSTD_initDStream(state->dstream);
218			if (ZSTD_isError(ret)) {
219				archive_set_error(&self->archive->archive,
220				    ARCHIVE_ERRNO_MISC,
221				    "Error initializing zstd decompressor: %s",
222				    ZSTD_getErrorName(ret));
223				return (ARCHIVE_FATAL);
224			}
225		}
226		in.src = __archive_read_filter_ahead(self->upstream, 1,
227		    &avail_in);
228		if (avail_in < 0) {
229			return avail_in;
230		}
231		if (in.src == NULL && avail_in == 0) {
232			if (!state->in_frame) {
233				/* end of stream */
234				state->eof = 1;
235				break;
236			} else {
237				archive_set_error(&self->archive->archive,
238				    ARCHIVE_ERRNO_MISC,
239				    "Truncated zstd input");
240				return (ARCHIVE_FATAL);
241			}
242		}
243		in.size = avail_in;
244		in.pos = 0;
245
246		{
247			const size_t ret =
248			    ZSTD_decompressStream(state->dstream, &out, &in);
249
250			if (ZSTD_isError(ret)) {
251				archive_set_error(&self->archive->archive,
252				    ARCHIVE_ERRNO_MISC,
253				    "Zstd decompression failed: %s",
254				    ZSTD_getErrorName(ret));
255				return (ARCHIVE_FATAL);
256			}
257
258			/* Decompressor made some progress */
259			__archive_read_filter_consume(self->upstream, in.pos);
260
261			/* ret guaranteed to be > 0 if frame isn't done yet */
262			state->in_frame = (ret != 0);
263		}
264	}
265
266	decompressed = out.pos;
267	state->total_out += decompressed;
268	if (decompressed == 0)
269		*p = NULL;
270	else
271		*p = state->out_block;
272	return (decompressed);
273}
274
275/*
276 * Clean up the decompressor.
277 */
278static int
279zstd_filter_close(struct archive_read_filter *self)
280{
281	struct private_data *state;
282
283	state = (struct private_data *)self->data;
284
285	ZSTD_freeDStream(state->dstream);
286	free(state->out_block);
287	free(state);
288
289	return (ARCHIVE_OK);
290}
291
292#endif /* HAVE_ZLIB_H && HAVE_LIBZSTD */
293