summaryrefslogtreecommitdiff
path: root/fitz/stm_read.c
blob: 9938b6e67ea61674a80d27026800da54882fbde5 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Input streams.
 */

#include "fitz.h"

fz_error
fz_readimp(fz_stream *stm)
{
	fz_buffer *buf = stm->buffer;
	fz_error error;
	fz_error reason;
	int produced;
	int n;

	if (stm->dead)
		return fz_throw("assert: read from dead stream");

	if (buf->eof)
		return fz_okay;

	fz_rewindbuffer(buf);
	if (buf->ep - buf->wp == 0)
		fz_growbuffer(buf);

	switch (stm->kind)
	{

	case FZ_SFILE:
		n = read(stm->file, buf->wp, buf->ep - buf->wp);
		if (n == -1)
		{
			stm->dead = 1;
			return fz_throw("syserr: read: %s", strerror(errno));
		}

		if (n == 0)
			buf->eof = 1;
		buf->wp += n;

		return fz_okay;

	case FZ_SFILTER:
		produced = 0;

		while (1)
		{
			reason = fz_process(stm->filter, stm->chain->buffer, buf);

			if (stm->filter->produced)
				produced = 1;

			if (reason == fz_ioneedin)
			{
				error = fz_readimp(stm->chain);
				if (error)
				{
					stm->dead = 1;
					return fz_rethrow(error, "cannot read from input stream");
				}
			}

			else if (reason == fz_ioneedout)
			{
				if (produced)
					return 0;

				if (buf->rp > buf->bp)
					fz_rewindbuffer(buf);
				else
					fz_growbuffer(buf);
			}

			else if (reason == fz_iodone)
			{
				return fz_okay;
			}

			else
			{
				stm->dead = 1;
				return fz_rethrow(reason, "cannot process filter");
			}
		}

	case FZ_SBUFFER:
		return fz_okay;

	default:
		return fz_throw("assert: unknown stream type");
	}
}

int
fz_tell(fz_stream *stm)
{
	fz_buffer *buf = stm->buffer;
	int t;

	if (stm->dead)
		return EOF;

	switch (stm->kind)
	{
	case FZ_SFILE:
		t = lseek(stm->file, 0, 1);
		if (t < 0)
		{
			fz_warn("syserr: lseek: %s", strerror(errno));
			stm->dead = 1;
			return EOF;
		}
		return t - (buf->wp - buf->rp);

	case FZ_SFILTER:
		return stm->filter->count - (buf->wp - buf->rp);

	case FZ_SBUFFER:
		return buf->rp - buf->bp;

	default:
		return EOF;
	}
}

fz_error
fz_seek(fz_stream *stm, int offset, int whence)
{
	fz_error error;
	fz_buffer *buf = stm->buffer;
	int t, c;

	if (stm->dead)
		return fz_throw("assert: seek in dead stream");

	if (whence == 1)
	{
		int cur = fz_tell(stm);
		if (cur < 0)
			return fz_throw("cannot tell current position");
		offset = cur + offset;
		whence = 0;
	}

	buf->eof = 0;

	switch (stm->kind)
	{
	case FZ_SFILE:
		t = lseek(stm->file, offset, whence);
		if (t < 0)
		{
			stm->dead = 1;
			return fz_throw("syserr: lseek: %s", strerror(errno));
		}

		buf->rp = buf->bp;
		buf->wp = buf->bp;

		return fz_okay;

	case FZ_SFILTER:
		if (whence == 0)
		{
			if (offset < fz_tell(stm))
			{
				stm->dead = 1;
				return fz_throw("assert: seek backwards in filter");
			}
			while (fz_tell(stm) < offset)
			{
				c = fz_readbyte(stm);
				if (c == EOF)
				{
					error = fz_readerror(stm);
					if (error)
						return fz_rethrow(error, "cannot seek forward in filter");
					break;
				}
			}
			return fz_okay;
		}

		stm->dead = 1;
		return fz_throw("assert: relative seek in filter");

	case FZ_SBUFFER:
		if (whence == 0)
			buf->rp = CLAMP(buf->bp + offset, buf->bp, buf->ep);
		else
			buf->rp = CLAMP(buf->ep + offset, buf->bp, buf->ep);
		return fz_okay;

	default:
		return fz_throw("unknown stream type");
	}
}

fz_error
fz_read(int *np, fz_stream *stm, unsigned char *mem, int n)
{
	fz_error error;
	fz_buffer *buf = stm->buffer;
	int i = 0;

	while (i < n)
	{
		while (buf->rp < buf->wp && i < n)
			mem[i++] = *buf->rp++;

		if (buf->rp == buf->wp)
		{
			if (buf->eof)
			{
				*np = i;
				return fz_okay;
			}

			error = fz_readimp(stm);
			if (error)
				return fz_rethrow(error, "cannot produce data");
		}
	}

	*np = i;
	return fz_okay;
}

fz_error
fz_readerror(fz_stream *stm)
{
	fz_error error;
	if (stm->error)
	{
		error = stm->error;
		stm->error = fz_okay;
		return fz_rethrow(error, "delayed read error");
	}
	return fz_okay;
}

int
fz_readbytex(fz_stream *stm)
{
	fz_buffer *buf = stm->buffer;

	if (buf->rp == buf->wp)
	{
		if (!buf->eof && !stm->error)
		{
			fz_error error = fz_readimp(stm);
			if (error)
				stm->error = fz_rethrow(error, "cannot read data");
		}
	}

	return buf->rp < buf->wp ? *buf->rp++ : EOF ;
}

int
fz_peekbytex(fz_stream *stm)
{
	fz_buffer *buf = stm->buffer;

	if (buf->rp == buf->wp)
	{
		if (!buf->eof && !stm->error)
		{
			fz_error error = fz_readimp(stm);
			if (error)
				stm->error = fz_rethrow(error, "cannot read data");
		}
	}

	return buf->rp < buf->wp ? *buf->rp : EOF ;
}