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

#include <zlib.h>

typedef struct fz_flate_s fz_flate;

struct fz_flate_s
{
	fz_stream *chain;
	z_stream z;
};

static void *zalloc(void *opaque, unsigned int items, unsigned int size)
{
	return fz_calloc(opaque, items, size);
}

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

static int
read_flated(fz_stream *stm, unsigned char *outbuf, int outlen)
{
	fz_flate *state = stm->state;
	fz_stream *chain = state->chain;
	z_streamp zp = &state->z;
	int code;

	zp->next_out = outbuf;
	zp->avail_out = outlen;

	while (zp->avail_out > 0)
	{
		if (chain->rp == chain->wp)
			fz_fill_buffer(chain);

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

		code = inflate(zp, Z_SYNC_FLUSH);

		chain->rp = chain->wp - zp->avail_in;

		if (code == Z_STREAM_END)
		{
			return outlen - zp->avail_out;
		}
		else if (code == Z_BUF_ERROR)
		{
			fz_warn("premature end of data in flate filter");
			return outlen - zp->avail_out;
		}
		else if (code == Z_DATA_ERROR && zp->avail_in == 0)
		{
			fz_warn("ignoring zlib error: %s", zp->msg);
			return outlen - zp->avail_out;
		}
		else if (code != Z_OK)
		{
			return fz_error_make("zlib error: %s", zp->msg);
		}
	}

	return outlen - zp->avail_out;
}

static void
close_flated(fz_stream *stm)
{
	fz_flate *state = stm->state;
	int code;

	code = inflateEnd(&state->z);
	if (code != Z_OK)
		fz_warn("zlib error: inflateEnd: %s", state->z.msg);

	fz_close(state->chain);
	fz_free(stm->ctx, state);
}

fz_stream *
fz_open_flated(fz_stream *chain)
{
	fz_flate *state;
	int code;

	state = fz_malloc(chain->ctx, sizeof(fz_flate));
	state->chain = chain;

	state->z.zalloc = zalloc;
	state->z.zfree = zfree;
	state->z.opaque = chain->ctx;
	state->z.next_in = NULL;
	state->z.avail_in = 0;

	code = inflateInit(&state->z);
	if (code != Z_OK)
		fz_warn("zlib error: inflateInit: %s", state->z.msg);

	return fz_new_stream(chain->ctx, state, read_flated, close_flated);
}