summaryrefslogtreecommitdiff
path: root/fitz/filt_flate.c
blob: c9e6698bf51ac6b8e79ab1c0618b368ae65e8ec6 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "fitz_base.h"
#include "fitz_stream.h"

#include <zlib.h>

typedef struct fz_flate_s fz_flate;

struct fz_flate_s
{
	fz_filter super;
	z_stream z;
};

static void *
zmalloc(void *opaque, unsigned int items, unsigned int size)
{
	return fz_malloc(items * size);
}

static void
zfree(void *opaque, void *ptr)
{
	fz_free(ptr);
}

fz_error
fz_newflated(fz_filter **fp, fz_obj *params)
{
	fz_error eo;
	fz_obj *obj;
	int zipfmt;
	int ei;

	FZ_NEWFILTER(fz_flate, f, flated);

	f->z.zalloc = zmalloc;
	f->z.zfree = zfree;
	f->z.opaque = nil;
	f->z.next_in = nil;
	f->z.avail_in = 0;

	zipfmt = 0;

	if (params)
	{
		obj = fz_dictgets(params, "ZIP");
		if (obj) zipfmt = fz_tobool(obj);
	}

	if (zipfmt)
	{
		/* if windowbits is negative the zlib header is skipped */
		ei = inflateInit2(&f->z, -15);
	}
	else
		ei = inflateInit(&f->z);

	if (ei != Z_OK)
	{
		eo = fz_throw("zlib error: inflateInit: %s", f->z.msg);
		fz_free(f);
		return eo;
	}

	return fz_okay;
}

void
fz_dropflated(fz_filter *f)
{
	z_streamp zp = &((fz_flate*)f)->z;
	int err;

	err = inflateEnd(zp);
	if (err != Z_OK)
		fprintf(stderr, "inflateEnd: %s", zp->msg);
}

fz_error
fz_processflated(fz_filter *f, fz_buffer *in, fz_buffer *out)
{
	z_streamp zp = &((fz_flate*)f)->z;
	int err;

	if (in->rp == in->wp && !in->eof)
		return fz_ioneedin;
	if (out->wp == out->ep)
		return fz_ioneedout;

	zp->next_in = in->rp;
	zp->avail_in = in->wp - in->rp;

	zp->next_out = out->wp;
	zp->avail_out = out->ep - out->wp;

	err = inflate(zp, Z_NO_FLUSH);

	/* Make sure we call it with Z_FINISH at the end of input */
	if (err == Z_OK && in->eof && zp->avail_in == 0 && zp->avail_out > 0)
		err = inflate(zp, Z_FINISH);

	in->rp = in->wp - zp->avail_in;
	out->wp = out->ep - zp->avail_out;

	if (err == Z_STREAM_END || err == Z_BUF_ERROR)
	{
		return fz_iodone;
	}
	else if (err == Z_OK)
	{
		if (in->rp == in->wp && !in->eof)
			return fz_ioneedin;
		if (out->wp == out->ep)
			return fz_ioneedout;
		return fz_ioneedin; /* hmm, what's going on here? */
	}
	else
	{
		return fz_throw("zlib error: inflate: %s", zp->msg);
	}
}

fz_error
fz_newflatee(fz_filter **fp, fz_obj *params)
{
	fz_obj *obj;
	fz_error eo;
	int effort;
	int zipfmt;
	int ei;

	FZ_NEWFILTER(fz_flate, f, flatee);

	effort = -1;
	zipfmt = 0;

	if (params)
	{
		obj = fz_dictgets(params, "Effort");
		if (obj) effort = fz_toint(obj);
		obj = fz_dictgets(params, "ZIP");
		if (obj) zipfmt = fz_tobool(obj);
	}

	f->z.zalloc = zmalloc;
	f->z.zfree = zfree;
	f->z.opaque = nil;
	f->z.next_in = nil;
	f->z.avail_in = 0;

	if (zipfmt)
		ei = deflateInit2(&f->z, effort, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
	else
		ei = deflateInit(&f->z, effort);

	if (ei != Z_OK)
	{
		eo = fz_throw("zlib error: deflateInit: %s", f->z.msg);
		fz_free(f);
		return eo;
	}

	return fz_okay;
}

void
fz_dropflatee(fz_filter *f)
{
	z_streamp zp = &((fz_flate*)f)->z;
	int err;

	err = deflateEnd(zp);
	if (err != Z_OK)
		fprintf(stderr, "deflateEnd: %s", zp->msg);

	fz_free(f);
}

fz_error
fz_processflatee(fz_filter *f, fz_buffer *in, fz_buffer *out)
{
	z_streamp zp = &((fz_flate*)f)->z;
	int err;

	if (in->rp == in->wp && !in->eof)
		return fz_ioneedin;
	if (out->wp == out->ep)
		return fz_ioneedout;

	zp->next_in = in->rp;
	zp->avail_in = in->wp - in->rp;

	zp->next_out = out->wp;
	zp->avail_out = out->ep - out->wp;

	err = deflate(zp, in->eof ? Z_FINISH : Z_NO_FLUSH);

	in->rp = in->wp - zp->avail_in;
	out->wp = out->ep - zp->avail_out;

	if (err == Z_STREAM_END)
	{
		return fz_iodone;
	}
	else if (err == Z_OK)
	{
		if (in->rp == in->wp && !in->eof)
			return fz_ioneedin;
		if (out->wp == out->ep)
			return fz_ioneedout;
		return fz_ioneedin; /* hmm? */
	}
	else
	{
		return fz_throw("zlib error: deflate: %s", zp->msg);
	}
}