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
|
#ifndef MUPDF_FITZ_STREAM_H
#define MUPDF_FITZ_STREAM_H
#include "mupdf/fitz/system.h"
#include "mupdf/fitz/context.h"
#include "mupdf/fitz/buffer.h"
/*
fz_stream is a buffered reader capable of seeking in both
directions.
Streams are reference counted, so references must be dropped
by a call to fz_close.
Only the data between rp and wp is valid.
*/
typedef struct fz_stream_s fz_stream;
/*
fz_open_file: Open the named file and wrap it in a stream.
filename: Path to a file. On non-Windows machines the filename should
be exactly as it would be passed to open(2). On Windows machines, the
path should be UTF-8 encoded so that non-ASCII characters can be
represented. Other platforms do the encoding as standard anyway (and
in most cases, particularly for MacOS and Linux, the encoding they
use is UTF-8 anyway).
*/
fz_stream *fz_open_file(fz_context *ctx, const char *filename);
fz_stream *fz_open_fd_progressive(fz_context *ctx, int fd, int bps);
fz_stream *fz_open_file_progressive(fz_context *ctx, const char *filename, int bps);
/*
fz_open_file_w: Open the named file and wrap it in a stream.
This function is only available when compiling for Win32.
filename: Wide character path to the file as it would be given
to _wopen().
*/
fz_stream *fz_open_file_w(fz_context *ctx, const wchar_t *filename);
/*
fz_open_fd: Wrap an open file descriptor in a stream.
file: An open file descriptor supporting bidirectional
seeking. The stream will take ownership of the file
descriptor, so it may not be modified or closed after the call
to fz_open_fd. When the stream is closed it will also close
the file descriptor.
*/
fz_stream *fz_open_fd(fz_context *ctx, int file);
/*
fz_open_memory: Open a block of memory as a stream.
data: Pointer to start of data block. Ownership of the data block is
NOT passed in.
len: Number of bytes in data block.
Returns pointer to newly created stream. May throw exceptions on
failure to allocate.
*/
fz_stream *fz_open_memory(fz_context *ctx, unsigned char *data, int len);
/*
fz_open_buffer: Open a buffer as a stream.
buf: The buffer to open. Ownership of the buffer is NOT passed in
(this function takes it's own reference).
Returns pointer to newly created stream. May throw exceptions on
failure to allocate.
*/
fz_stream *fz_open_buffer(fz_context *ctx, fz_buffer *buf);
/*
fz_close: Close an open stream.
Drops a reference for the stream. Once no references remain
the stream will be closed, as will any file descriptor the
stream is using.
Does not throw exceptions.
*/
void fz_close(fz_stream *stm);
/*
fz_tell: return the current reading position within a stream
*/
int fz_tell(fz_stream *stm);
/*
fz_seek: Seek within a stream.
stm: The stream to seek within.
offset: The offset to seek to.
whence: From where the offset is measured (see fseek).
*/
void fz_seek(fz_stream *stm, int offset, int whence);
/*
fz_read: Read from a stream into a given data block.
stm: The stream to read from.
data: The data block to read into.
len: The length of the data block (in bytes).
Returns the number of bytes read. May throw exceptions.
*/
int fz_read(fz_stream *stm, unsigned char *data, int len);
/*
fz_read_all: Read all of a stream into a buffer.
stm: The stream to read from
initial: Suggested initial size for the buffer.
Returns a buffer created from reading from the stream. May throw
exceptions on failure to allocate.
*/
fz_buffer *fz_read_all(fz_stream *stm, int initial);
enum
{
FZ_STREAM_META_PROGRESSIVE = 1,
FZ_STREAM_META_LENGTH = 2
};
int fz_stream_meta(fz_stream *stm, int key, int size, void *ptr);
void fz_rebind_stream(fz_stream *stm, fz_context *ctx);
typedef int (fz_stream_read_fn)(fz_stream *stm, unsigned char *buf, int len);
typedef void (fz_stream_close_fn)(fz_context *ctx, void *state);
typedef void (fz_stream_seek_fn)(fz_stream *stm, int offset, int whence);
typedef int (fz_stream_meta_fn)(fz_stream *stm, int key, int size, void *ptr);
typedef fz_stream *(fz_stream_rebind_fn)(fz_stream *stm);
struct fz_stream_s
{
fz_context *ctx;
int refs;
int error;
int eof;
int pos;
int avail;
int bits;
unsigned char *bp, *rp, *wp, *ep;
void *state;
fz_stream_read_fn *read;
fz_stream_close_fn *close;
fz_stream_seek_fn *seek;
fz_stream_meta_fn *meta;
fz_stream_rebind_fn *rebind;
unsigned char buf[4096];
};
fz_stream *fz_new_stream(fz_context *ctx,
void *state,
fz_stream_read_fn *read,
fz_stream_close_fn *close,
fz_stream_rebind_fn *rebind);
fz_stream *fz_keep_stream(fz_stream *stm);
void fz_fill_buffer(fz_stream *stm);
/*
fz_read_best: Attempt to read a stream into a buffer. If truncated
is NULL behaves as fz_read_all, otherwise does not throw exceptions
in the case of failure, but instead sets a truncated flag.
stm: The stream to read from.
initial: Suggested initial size for the buffer.
truncated: Flag to store success/failure indication in.
Returns a buffer created from reading from the stream.
*/
fz_buffer *fz_read_best(fz_stream *stm, int initial, int *truncated);
void fz_read_line(fz_stream *stm, char *buf, int max);
static inline int fz_read_byte(fz_stream *stm)
{
if (stm->rp == stm->wp)
{
fz_fill_buffer(stm);
return stm->rp < stm->wp ? *stm->rp++ : EOF;
}
return *stm->rp++;
}
static inline int fz_peek_byte(fz_stream *stm)
{
if (stm->rp == stm->wp)
{
fz_fill_buffer(stm);
return stm->rp < stm->wp ? *stm->rp : EOF;
}
return *stm->rp;
}
static inline void fz_unread_byte(fz_stream *stm)
{
if (stm->rp > stm->bp)
stm->rp--;
}
static inline int fz_is_eof(fz_stream *stm)
{
if (stm->rp == stm->wp)
{
if (stm->eof)
return 1;
return fz_peek_byte(stm) == EOF;
}
return 0;
}
static inline unsigned int fz_read_bits(fz_stream *stm, int n)
{
unsigned int x;
if (n <= stm->avail)
{
stm->avail -= n;
x = (stm->bits >> stm->avail) & ((1 << n) - 1);
}
else
{
x = stm->bits & ((1 << stm->avail) - 1);
n -= stm->avail;
stm->avail = 0;
while (n > 8)
{
x = (x << 8) | fz_read_byte(stm);
n -= 8;
}
if (n > 0)
{
stm->bits = fz_read_byte(stm);
stm->avail = 8 - n;
x = (x << n) | (stm->bits >> stm->avail);
}
}
return x;
}
static inline void fz_sync_bits(fz_stream *stm)
{
stm->avail = 0;
}
static inline int fz_is_eof_bits(fz_stream *stm)
{
return fz_is_eof(stm) && (stm->avail == 0 || stm->bits == EOF);
}
#endif
|