1 #include "mupdf/fitz.h"
3 void fz_rebind_stream(fz_stream *stm, fz_context *ctx)
5 if (stm == NULL || stm->ctx == ctx)
9 stm = (stm->rebind == NULL ? NULL : stm->rebind(stm));
10 } while (stm != NULL);
14 fz_new_stream(fz_context *ctx, void *state,
15 fz_stream_next_fn *next,
16 fz_stream_close_fn *close,
17 fz_stream_rebind_fn *rebind)
23 stm = fz_malloc_struct(ctx, fz_stream);
53 fz_keep_stream(fz_stream *stm)
61 fz_close(fz_stream *stm)
69 stm->close(stm->ctx, stm->state);
70 fz_free(stm->ctx, stm);
76 typedef struct fz_file_stream_s
79 unsigned char buffer[4096];
82 static int next_file(fz_stream *stm, int n)
84 fz_file_stream *state = stm->state;
86 /* n is only a hint, that we can safely ignore */
87 n = read(state->file, state->buffer, sizeof(state->buffer));
89 fz_throw(stm->ctx, FZ_ERROR_GENERIC, "read error: %s", strerror(errno));
90 stm->rp = state->buffer;
91 stm->wp = state->buffer + n;
99 static void seek_file(fz_stream *stm, int offset, int whence)
101 fz_file_stream *state = stm->state;
102 int n = lseek(state->file, offset, whence);
104 fz_throw(stm->ctx, FZ_ERROR_GENERIC, "cannot lseek: %s", strerror(errno));
106 stm->rp = state->buffer;
107 stm->wp = state->buffer;
110 static void close_file(fz_context *ctx, void *state_)
112 fz_file_stream *state = state_;
113 int n = close(state->file);
115 fz_warn(ctx, "close error: %s", strerror(errno));
120 fz_open_fd(fz_context *ctx, int fd)
123 fz_file_stream *state = fz_malloc_struct(ctx, fz_file_stream);
128 stm = fz_new_stream(ctx, state, next_file, close_file, NULL);
135 stm->seek = seek_file;
141 fz_open_file(fz_context *ctx, const char *name)
144 char *s = (char*)name;
147 d = wname = fz_malloc(ctx, (strlen(name)+1) * sizeof(wchar_t));
149 s += fz_chartorune(&c, s);
153 fd = _wopen(wname, O_BINARY | O_RDONLY, 0);
156 int fd = open(name, O_BINARY | O_RDONLY, 0);
159 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open %s", name);
160 return fz_open_fd(ctx, fd);
165 fz_open_file_w(fz_context *ctx, const wchar_t *name)
167 int fd = _wopen(name, O_BINARY | O_RDONLY, 0);
169 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file %ls", name);
170 return fz_open_fd(ctx, fd);
176 static int next_buffer(fz_stream *stm, int max)
181 static void seek_buffer(fz_stream *stm, int offset, int whence)
183 int pos = stm->pos - (stm->wp - stm->rp);
184 /* Convert to absolute pos */
187 offset += pos; /* Was relative to current pos */
189 else if (whence == 2)
191 offset += stm->pos; /* Was relative to end */
196 if (offset > stm->pos)
198 stm->rp += offset - pos;
201 static void close_buffer(fz_context *ctx, void *state_)
203 fz_buffer *state = (fz_buffer *)state_;
205 fz_drop_buffer(ctx, state);
209 fz_open_buffer(fz_context *ctx, fz_buffer *buf)
213 fz_keep_buffer(ctx, buf);
214 stm = fz_new_stream(ctx, buf, next_buffer, close_buffer, NULL);
215 stm->seek = seek_buffer;
218 stm->wp = buf->data + buf->len;
226 fz_open_memory(fz_context *ctx, unsigned char *data, int len)
230 stm = fz_new_stream(ctx, NULL, next_buffer, close_buffer, NULL);
231 stm->seek = seek_buffer;
234 stm->wp = data + len;