diff options
author | Paul Gardiner <paul.gardiner@artifex.com> | 2018-01-04 15:11:24 +0000 |
---|---|---|
committer | Paul Gardiner <paul.gardiner@artifex.com> | 2018-01-19 13:52:25 +0000 |
commit | 457873fbf7fd6d40242722f3a51b41428302d0ca (patch) | |
tree | 2539ea3bc1490012f5f5bd95d0f9064d999eca04 /source/fitz/output.c | |
parent | 371890461adeff0bcc8d4986f666c59055bebc70 (diff) | |
download | mupdf-457873fbf7fd6d40242722f3a51b41428302d0ca.tar.xz |
Add fz_output_as_stream
This provides a way for some output streams to also be read, a feature
needed for the sake of document signing. Currently this is supported only
for file output.
Diffstat (limited to 'source/fitz/output.c')
-rw-r--r-- | source/fitz/output.c | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/source/fitz/output.c b/source/fitz/output.c index 4c6aa6b4..18791603 100644 --- a/source/fitz/output.c +++ b/source/fitz/output.c @@ -158,6 +158,14 @@ file_drop(fz_context *ctx, void *opaque) fz_warn(ctx, "cannot fclose: %s", strerror(errno)); } +static fz_stream * +file_as_stream(fz_context *ctx, void *opaque) +{ + FILE *file = opaque; + fflush(file); + return fz_open_file_ptr_no_close(ctx, file); +}; + fz_output * fz_new_output(fz_context *ctx, void *state, fz_output_write_fn *write, fz_output_close_fn *close, fz_output_drop_fn *drop) { @@ -197,7 +205,7 @@ fz_new_output_with_path(fz_context *ctx, const char *filename, int append) if (errno != ENOENT) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot remove file '%s': %s", filename, strerror(errno)); } - file = fz_fopen_utf8(filename, append ? "ab" : "wb"); + file = fz_fopen_utf8(filename, "rb+"); #else /* Ensure we create a brand new file. We don't want to clobber our old file. */ if (!append) @@ -206,7 +214,7 @@ fz_new_output_with_path(fz_context *ctx, const char *filename, int append) if (errno != ENOENT) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot remove file '%s': %s", filename, strerror(errno)); } - file = fopen(filename, append ? "ab" : "wb"); + file = fopen(filename, "rb+"); #endif if (!file) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno)); @@ -214,6 +222,7 @@ fz_new_output_with_path(fz_context *ctx, const char *filename, int append) out = fz_new_output(ctx, file, file_write, NULL, file_drop); out->seek = file_seek; out->tell = file_tell; + out->as_stream = file_as_stream; return out; } @@ -293,6 +302,15 @@ fz_tell_output(fz_context *ctx, fz_output *out) return out->tell(ctx, out->state); } +fz_stream * +fz_stream_from_output(fz_context *ctx, fz_output *out) +{ + if (!out) return 0; + if (out->as_stream == NULL) + fz_throw(ctx, FZ_ERROR_GENERIC, "Cannot derive input stream from output stream"); + return out->as_stream(ctx, out->state); +} + static void fz_write_emit(fz_context *ctx, void *out, int c) { |