summaryrefslogtreecommitdiff
path: root/fitz/stm_filter.c
blob: 543d41feb71514bec2aa24c637b23eac62dd62c7 (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
#include "fitz.h"

fz_error
fz_process(fz_filter *f, fz_buffer *in, fz_buffer *out)
{
	fz_error reason;
	unsigned char *oldrp;
	unsigned char *oldwp;

	oldrp = in->rp;
	oldwp = out->wp;

	if (f->done)
		return fz_iodone;

	assert(!out->eof);

	reason = f->process(f, in, out);

	assert(in->rp <= in->wp);
	assert(out->wp <= out->ep);

	f->consumed = in->rp > oldrp;
	f->produced = out->wp > oldwp;
	f->count += out->wp - oldwp;

	/* iodone or error */
	if (reason != fz_ioneedin && reason != fz_ioneedout)
	{
		if (reason != fz_iodone)
			reason = fz_rethrow(reason, "cannot process filter");
		out->eof = 1;
		f->done = 1;
	}

	return reason;
}

fz_filter *
fz_keepfilter(fz_filter *f)
{
	f->refs ++;
	return f;
}

void
fz_dropfilter(fz_filter *f)
{
	if (--f->refs == 0)
	{
		if (f->drop)
			f->drop(f);
		fz_free(f);
	}
}