aboutsummaryrefslogtreecommitdiff
path: root/lib/fstream/compress/ostream_compressor.c
blob: 5137f1d2ec295d69fcf04b257752e367d68e81d9 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * ostream_compressor.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "../internal.h"

static int comp_append(ostream_t *strm, const void *data, size_t size)
{
	ostream_comp_t *comp = (ostream_comp_t *)strm;
	size_t diff;

	while (size > 0) {
		if (comp->inbuf_used >= BUFSZ) {
			if (comp->flush_inbuf(comp, false))
				return -1;
		}

		diff = BUFSZ - comp->inbuf_used;

		if (diff > size)
			diff = size;

		memcpy(comp->inbuf + comp->inbuf_used, data, diff);

		comp->inbuf_used += diff;
		data = (const char *)data + diff;
		size -= diff;
	}

	return 0;
}

static int comp_flush(ostream_t *strm)
{
	ostream_comp_t *comp = (ostream_comp_t *)strm;

	if (comp->inbuf_used > 0) {
		if (comp->flush_inbuf(comp, true))
			return -1;
	}

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

static const char *comp_get_filename(ostream_t *strm)
{
	ostream_comp_t *comp = (ostream_comp_t *)strm;

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

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

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

ostream_t *ostream_compressor_create(ostream_t *strm, int comp_id)
{
	ostream_comp_t *comp = NULL;
	sqfs_object_t *obj;
	ostream_t *base;

	switch (comp_id) {
	case FSTREAM_COMPRESSOR_GZIP:
#ifdef WITH_GZIP
		comp = ostream_gzip_create(strm->get_filename(strm));
#endif
		break;
	case FSTREAM_COMPRESSOR_XZ:
#ifdef WITH_XZ
		comp = ostream_xz_create(strm->get_filename(strm));
#endif
		break;
	default:
		break;
	}

	if (comp == NULL)
		return NULL;

	comp->wrapped = strm;
	comp->inbuf_used = 0;

	base = (ostream_t *)comp;
	base->append = comp_append;
	base->flush = comp_flush;
	base->get_filename = comp_get_filename;

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