summaryrefslogtreecommitdiff
path: root/lib/fstream/uncompress/istream_compressor.c
blob: 75edd1b72e62224ef462d9c89560243640db3827 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * istream_compressor.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "../internal.h"

static const char *comp_get_filename(istream_t *strm)
{
	istream_comp_t *comp = (istream_comp_t *)strm;

	return comp->wrapped->get_filename(comp->wrapped);
}

static void comp_destroy(sqfs_object_t *obj)
{
	istream_comp_t *comp = (istream_comp_t *)obj;

	comp->cleanup(comp);
	sqfs_destroy(comp->wrapped);
	free(comp);
}

istream_t *istream_compressor_create(istream_t *strm, int comp_id)
{
	istream_comp_t *comp = NULL;
	sqfs_object_t *obj;
	istream_t *base;

	switch (comp_id) {
	case FSTREAM_COMPRESSOR_GZIP:
#ifdef WITH_GZIP
		comp = istream_gzip_create(strm->get_filename(strm));
#endif
		break;
	case FSTREAM_COMPRESSOR_XZ:
#ifdef WITH_XZ
		comp = istream_xz_create(strm->get_filename(strm));
#endif
		break;
	case FSTREAM_COMPRESSOR_ZSTD:
#if defined(WITH_ZSTD) && defined(HAVE_ZSTD_STREAM)
		comp = istream_zstd_create(strm->get_filename(strm));
#endif
		break;
	case FSTREAM_COMPRESSOR_BZIP2:
#ifdef WITH_BZIP2
		comp = istream_bzip2_create(strm->get_filename(strm));
#endif
		break;
	default:
		break;
	}

	if (comp == NULL)
		return NULL;

	comp->wrapped = strm;

	base = (istream_t *)comp;
	base->get_filename = comp_get_filename;
	base->buffer = comp->uncompressed;
	base->eof = false;

	obj = (sqfs_object_t *)comp;
	obj->destroy = comp_destroy;
	return base;
}