aboutsummaryrefslogtreecommitdiff
path: root/lib/xfrm/src/gzip.c
blob: 67224f7fd98f99768d89f67f62120378f9719af8 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * gzip.c
 *
 * Copyright (C) 2021 David Oberhollenzer <goliath@infraroot.at>
 */
#include <stdlib.h>
#include <stdio.h>
#include <zlib.h>

#include "xfrm/stream.h"
#include "xfrm/compress.h"

typedef struct {
	xfrm_stream_t base;

	z_stream strm;
	bool compress;
} xfrm_stream_gzip_t;

static const int zlib_action[] = {
	[XFRM_STREAM_FLUSH_NONE] = Z_NO_FLUSH,
	[XFRM_STREAM_FLUSH_SYNC] = Z_SYNC_FLUSH,
	[XFRM_STREAM_FLUSH_FULL] = Z_FINISH,
};

static int process_data(xfrm_stream_t *stream, const void *in,
			sqfs_u32 in_size, void *out, sqfs_u32 out_size,
			sqfs_u32 *in_read, sqfs_u32 *out_written,
			int flush_mode)
{
	xfrm_stream_gzip_t *gzip = (xfrm_stream_gzip_t *)stream;
	sqfs_u32 diff;
	int ret;

	if (flush_mode < 0 || flush_mode >= XFRM_STREAM_FLUSH_COUNT)
		flush_mode = XFRM_STREAM_FLUSH_NONE;

	while (in_size > 0 && out_size > 0) {
		gzip->strm.next_in = (void *)in;
		gzip->strm.avail_in = in_size;

		gzip->strm.next_out = out;
		gzip->strm.avail_out = out_size;

		if (gzip->compress) {
			ret = deflate(&gzip->strm, zlib_action[flush_mode]);
		} else {
			ret = inflate(&gzip->strm, zlib_action[flush_mode]);
		}

		if (ret == Z_STREAM_ERROR)
			return XFRM_STREAM_ERROR;

		diff = in_size - gzip->strm.avail_in;
		in = (const char *)in + diff;
		in_size -= diff;
		*in_read += diff;

		diff = out_size - gzip->strm.avail_out;
		out = (char *)out + diff;
		out_size -= diff;
		*out_written += diff;

		if (ret == Z_STREAM_END) {
			if (gzip->compress) {
				ret = deflateReset(&gzip->strm);
			} else {
				ret = inflateReset(&gzip->strm);
			}

			if (ret != Z_OK)
				return XFRM_STREAM_ERROR;

			return XFRM_STREAM_END;
		}

		if (ret == Z_BUF_ERROR)
			return XFRM_STREAM_BUFFER_FULL;
	}

	return XFRM_STREAM_OK;
}

static void destroy(sqfs_object_t *obj)
{
	xfrm_stream_gzip_t *gzip = (xfrm_stream_gzip_t *)obj;

	if (gzip->compress) {
		deflateEnd(&gzip->strm);
	} else {
		inflateEnd(&gzip->strm);
	}
	free(gzip);
}

static xfrm_stream_t *create_stream(const compressor_config_t *cfg,
				    bool compress)
{
	xfrm_stream_gzip_t *gzip = calloc(1, sizeof(*gzip));
	xfrm_stream_t *xfrm = (xfrm_stream_t *)gzip;
	int ret;

	if (gzip == NULL) {
		perror("creating gzip stream compressor");
		return NULL;
	}

	if (compress) {
		int level = COMP_GZIP_DEFAULT_LEVEL;
		int wnd = COMP_GZIP_DEFAULT_WINDOW;

		if (cfg != NULL) {
			level = cfg->level;
			wnd = cfg->opt.gzip.window_size;
		}

		ret = deflateInit2(&gzip->strm, level, Z_DEFLATED,
				   wnd + 16, 8, Z_DEFAULT_STRATEGY);
	} else {
		ret = inflateInit2(&gzip->strm, 16 + 15);
	}

	if (ret != Z_OK) {
		fputs("internal error creating gzip compressor.\n", stderr);
		free(gzip);
		return NULL;
	}

	gzip->compress = compress;
	xfrm->process_data = process_data;
	sqfs_object_init(xfrm, destroy, NULL);
	return xfrm;
}

xfrm_stream_t *compressor_stream_gzip_create(const compressor_config_t *cfg)
{
	return create_stream(cfg, true);
}

xfrm_stream_t *decompressor_stream_gzip_create(void)
{
	return create_stream(NULL, false);
}