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
|
#include "fitz-internal.h"
fz_stream *
fz_new_stream(fz_context *ctx, void *state,
int(*read)(fz_stream *stm, unsigned char *buf, int len),
void(*close)(fz_context *ctx, void *state))
{
fz_stream *stm;
fz_try(ctx)
{
stm = fz_malloc_struct(ctx, fz_stream);
}
fz_catch(ctx)
{
close(ctx, state);
fz_rethrow(ctx);
}
stm->refs = 1;
stm->error = 0;
stm->eof = 0;
stm->pos = 0;
stm->bits = 0;
stm->avail = 0;
stm->locked = 0;
stm->bp = stm->buf;
stm->rp = stm->bp;
stm->wp = stm->bp;
stm->ep = stm->buf + sizeof stm->buf;
stm->state = state;
stm->read = read;
stm->close = close;
stm->seek = NULL;
stm->ctx = ctx;
return stm;
}
void
fz_lock_stream(fz_stream *stm)
{
if (stm)
{
fz_lock(stm->ctx, FZ_LOCK_FILE);
stm->locked = 1;
}
}
fz_stream *
fz_keep_stream(fz_stream *stm)
{
stm->refs ++;
return stm;
}
void
fz_close(fz_stream *stm)
{
if (!stm)
return;
stm->refs --;
if (stm->refs == 0)
{
if (stm->close)
stm->close(stm->ctx, stm->state);
if (stm->locked)
fz_unlock(stm->ctx, FZ_LOCK_FILE);
fz_free(stm->ctx, stm);
}
}
/* File stream */
static int read_file(fz_stream *stm, unsigned char *buf, int len)
{
int n = read(*(int*)stm->state, buf, len);
fz_assert_lock_held(stm->ctx, FZ_LOCK_FILE);
if (n < 0)
fz_throw(stm->ctx, "read error: %s", strerror(errno));
return n;
}
static void seek_file(fz_stream *stm, int offset, int whence)
{
int n = lseek(*(int*)stm->state, offset, whence);
fz_assert_lock_held(stm->ctx, FZ_LOCK_FILE);
if (n < 0)
fz_throw(stm->ctx, "cannot lseek: %s", strerror(errno));
stm->pos = n;
stm->rp = stm->bp;
stm->wp = stm->bp;
}
static void close_file(fz_context *ctx, void *state)
{
int n = close(*(int*)state);
if (n < 0)
fz_warn(ctx, "close error: %s", strerror(errno));
fz_free(ctx, state);
}
fz_stream *
fz_open_fd(fz_context *ctx, int fd)
{
fz_stream *stm;
int *state;
state = fz_malloc_struct(ctx, int);
*state = fd;
fz_try(ctx)
{
stm = fz_new_stream(ctx, state, read_file, close_file);
}
fz_catch(ctx)
{
fz_free(ctx, state);
fz_rethrow(ctx);
}
stm->seek = seek_file;
return stm;
}
fz_stream *
fz_open_file(fz_context *ctx, const char *name)
{
int fd = open(name, O_BINARY | O_RDONLY, 0);
if (fd == -1)
fz_throw(ctx, "cannot open %s", name);
return fz_open_fd(ctx, fd);
}
#ifdef _WIN32
fz_stream *
fz_open_file_w(fz_context *ctx, const wchar_t *name)
{
int fd = _wopen(name, O_BINARY | O_RDONLY, 0);
if (fd == -1)
return NULL;
return fz_open_fd(ctx, fd);
}
#endif
/* Memory stream */
static int read_buffer(fz_stream *stm, unsigned char *buf, int len)
{
return 0;
}
static void seek_buffer(fz_stream *stm, int offset, int whence)
{
if (whence == 0)
stm->rp = stm->bp + offset;
if (whence == 1)
stm->rp += offset;
if (whence == 2)
stm->rp = stm->ep - offset;
stm->rp = CLAMP(stm->rp, stm->bp, stm->ep);
stm->wp = stm->ep;
}
static void close_buffer(fz_context *ctx, void *state_)
{
fz_buffer *state = (fz_buffer *)state_;
if (state)
fz_drop_buffer(ctx, state);
}
fz_stream *
fz_open_buffer(fz_context *ctx, fz_buffer *buf)
{
fz_stream *stm;
fz_keep_buffer(ctx, buf);
stm = fz_new_stream(ctx, buf, read_buffer, close_buffer);
stm->seek = seek_buffer;
stm->bp = buf->data;
stm->rp = buf->data;
stm->wp = buf->data + buf->len;
stm->ep = buf->data + buf->len;
stm->pos = buf->len;
return stm;
}
fz_stream *
fz_open_memory(fz_context *ctx, unsigned char *data, int len)
{
fz_stream *stm;
stm = fz_new_stream(ctx, NULL, read_buffer, close_buffer);
stm->seek = seek_buffer;
stm->bp = data;
stm->rp = data;
stm->wp = data + len;
stm->ep = data + len;
stm->pos = len;
return stm;
}
|