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
|
#include "mupdf/fitz/stream.h"
#include "mupdf/fitz/string.h"
#if (defined(_WIN32) || defined(_WIN64)) && !defined(NDEBUG)
#include "windows.h"
static void
show_progress(int av, int pos)
{
char text[80];
sprintf(text, "Have %d, Want %d\n", av, pos);
OutputDebugStringA(text);
}
#else
#define show_progress(A,B) do {} while (0)
#endif
/* File stream - progressive reading to simulate http download */
typedef struct prog_state
{
int fd;
int length;
int available;
int bps;
clock_t start_time;
unsigned char buffer[4096];
} prog_state;
static int next_prog(fz_context *ctx, fz_stream *stm, int len)
{
prog_state *ps = (prog_state *)stm->state;
int n;
unsigned char *buf = ps->buffer;
if (len > sizeof(ps->buffer))
len = sizeof(ps->buffer);
/* Simulate more data having arrived */
if (ps->available < ps->length)
{
int av = (int)((float)(clock() - ps->start_time) * ps->bps / (CLOCKS_PER_SEC*8));
if (av > ps->length)
av = ps->length;
ps->available = av;
/* Limit any fetches to be within the data we have */
if (av < ps->length && len + stm->pos > av)
{
len = av - stm->pos;
if (len <= 0)
{
show_progress(av, stm->pos);
fz_throw(ctx, FZ_ERROR_TRYLATER, "Not enough data yet");
}
}
}
n = (len > 0 ? read(ps->fd, buf, len) : 0);
if (n < 0)
fz_throw(ctx, FZ_ERROR_GENERIC, "read error: %s", strerror(errno));
stm->rp = ps->buffer + stm->pos;
stm->wp = ps->buffer + stm->pos + n;
stm->pos += n;
if (n == 0)
return EOF;
return *stm->rp++;
}
static void seek_prog(fz_context *ctx, fz_stream *stm, int offset, int whence)
{
prog_state *ps = (prog_state *)stm->state;
int n;
/* Simulate more data having arrived */
if (ps->available < ps->length)
{
int av = (int)((float)(clock() - ps->start_time) * ps->bps / (CLOCKS_PER_SEC*8));
if (av > ps->length)
av = ps->length;
ps->available = av;
}
if (ps->available < ps->length)
{
if (whence == SEEK_END)
{
show_progress(ps->available, ps->length);
fz_throw(ctx, FZ_ERROR_TRYLATER, "Not enough data to seek to end yet");
}
}
if (whence == SEEK_CUR)
{
whence = SEEK_SET;
offset += stm->pos;
if (offset > ps->available)
{
show_progress(ps->available, offset);
fz_throw(ctx, FZ_ERROR_TRYLATER, "Not enough data to seek (relatively) to offset yet");
}
}
if (whence == SEEK_SET)
{
if (offset > ps->available)
{
show_progress(ps->available, offset);
fz_throw(ctx, FZ_ERROR_TRYLATER, "Not enough data to seek to offset yet");
}
}
n = lseek(ps->fd, offset, whence);
if (n < 0)
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot lseek: %s", strerror(errno));
stm->pos = n;
stm->wp = stm->rp;
}
static void close_prog(fz_context *ctx, void *state)
{
prog_state *ps = (prog_state *)state;
int n = close(ps->fd);
if (n < 0)
fz_warn(ctx, "close error: %s", strerror(errno));
fz_free(ctx, state);
}
static int meta_prog(fz_context *ctx, fz_stream *stm, int key, int size, void *ptr)
{
prog_state *ps = (prog_state *)stm->state;
switch(key)
{
case FZ_STREAM_META_PROGRESSIVE:
return 1;
break;
case FZ_STREAM_META_LENGTH:
return ps->length;
}
return -1;
}
fz_stream *
fz_open_fd_progressive(fz_context *ctx, int fd, int bps)
{
fz_stream *stm;
prog_state *state;
state = fz_malloc_struct(ctx, prog_state);
state->fd = fd;
state->bps = bps;
state->start_time = clock();
state->available = 0;
state->length = lseek(state->fd, 0, SEEK_END);
lseek(state->fd, 0, SEEK_SET);
fz_try(ctx)
{
stm = fz_new_stream(ctx, state, next_prog, close_prog);
}
fz_catch(ctx)
{
fz_free(ctx, state);
fz_rethrow(ctx);
}
stm->seek = seek_prog;
stm->meta = meta_prog;
return stm;
}
fz_stream *
fz_open_file_progressive(fz_context *ctx, const char *name, int bps)
{
#if defined(_WIN32) || defined(_WIN64)
char *s = (char*)name;
wchar_t *wname, *d;
int c, fd;
d = wname = fz_malloc(ctx, (strlen(name)+1) * sizeof(wchar_t));
while (*s) {
s += fz_chartorune(&c, s);
*d++ = c;
}
*d = 0;
fd = _wopen(wname, O_BINARY | O_RDONLY, 0);
fz_free(ctx, wname);
#else
int fd = open(name, O_BINARY | O_RDONLY, 0);
#endif
if (fd == -1)
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open %s", name);
return fz_open_fd_progressive(ctx, fd, bps);
}
|