diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2017-08-17 15:01:09 +0200 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2017-11-01 13:08:56 +0100 |
commit | f595e889b91a674eb94db7ca4d832da54f5194cd (patch) | |
tree | 273b4a7cc465477e428d0c628a19a61301baaae0 /source/fitz | |
parent | 2910531c99e80bede06d2f1460459e8f6ce79961 (diff) | |
download | mupdf-f595e889b91a674eb94db7ca4d832da54f5194cd.tar.xz |
Use int64_t for public file API offsets.
Don't mess with conditional compilation with LARGEFILE -- always expose
64-bit file offsets in our public API.
Diffstat (limited to 'source/fitz')
32 files changed, 190 insertions, 122 deletions
diff --git a/source/fitz/color-icc-create.c b/source/fitz/color-icc-create.c index 58acedf5..2a8da172 100644 --- a/source/fitz/color-icc-create.c +++ b/source/fitz/color-icc-create.c @@ -1,6 +1,8 @@ #include "mupdf/fitz.h" #include "icc34.h" +#include <string.h> + #define SAVEICCPROFILE 0 #define ICC_HEADER_SIZE 128 #define ICC_TAG_SIZE 12 diff --git a/source/fitz/draw-edge.c b/source/fitz/draw-edge.c index 8510f216..244a4834 100644 --- a/source/fitz/draw-edge.c +++ b/source/fitz/draw-edge.c @@ -1,6 +1,12 @@ #include "mupdf/fitz.h" #include "draw-imp.h" +#include <assert.h> +#include <limits.h> +#include <math.h> +#include <stdlib.h> +#include <string.h> + /* * Global Edge List -- list of straight path segments for scan conversion * diff --git a/source/fitz/draw-edgebuffer.c b/source/fitz/draw-edgebuffer.c index db2bbe3a..ffc892e1 100644 --- a/source/fitz/draw-edgebuffer.c +++ b/source/fitz/draw-edgebuffer.c @@ -1,6 +1,10 @@ #include "mupdf/fitz.h" #include "draw-imp.h" +#include <assert.h> +#include <stdlib.h> +#include <string.h> + #undef DEBUG_SCAN_CONVERTER /* Define ourselves a 'fixed' type for clarity */ diff --git a/source/fitz/draw-paint.c b/source/fitz/draw-paint.c index 5735f615..43d546b3 100644 --- a/source/fitz/draw-paint.c +++ b/source/fitz/draw-paint.c @@ -2226,7 +2226,7 @@ fz_paint_pixmap(fz_pixmap * restrict dst, const fz_pixmap * restrict src, int al if (dst->n - dst->alpha != src->n - src->alpha) { - fprintf(stderr, "fz_paint_pixmap - FIXME\n"); + // fprintf(stderr, "fz_paint_pixmap - FIXME\n"); return; } assert(dst->n - dst->alpha == src->n - src->alpha); @@ -2355,7 +2355,7 @@ fz_paint_pixmap_with_overprint(fz_pixmap * restrict dst, const fz_pixmap * restr if (dst->n - dst->alpha != src->n - src->alpha) { - fprintf(stderr, "fz_paint_pixmap_with_overprint - FIXME\n"); + // fprintf(stderr, "fz_paint_pixmap_with_overprint - FIXME\n"); return; } assert(dst->n - dst->alpha == src->n - src->alpha); diff --git a/source/fitz/draw-rasterize.c b/source/fitz/draw-rasterize.c index 2e399834..8b55b668 100644 --- a/source/fitz/draw-rasterize.c +++ b/source/fitz/draw-rasterize.c @@ -1,6 +1,8 @@ #include "mupdf/fitz.h" #include "draw-imp.h" +#include <string.h> + void fz_new_aa_context(fz_context *ctx) { #ifndef AA_BITS diff --git a/source/fitz/error.c b/source/fitz/error.c index 6820034d..0f371bc6 100644 --- a/source/fitz/error.c +++ b/source/fitz/error.c @@ -211,6 +211,7 @@ void fz_rethrow_if(fz_context *ctx, int err) /* Android specific code to take fprintf to LOG */ #ifdef __ANDROID__ +#include <unistd.h> #include <android/log.h> #define LOG_TAG "libmupdf" @@ -220,7 +221,7 @@ static int android_log_fill = 0; static char android_log_buffer2[4096]; -int fz_android_fprintf(FILE *file, const char *fmt, ...) +int fz_android_fprintf(void *file, const char *fmt, ...) { va_list args; char *p, *q; diff --git a/source/fitz/filter-basic.c b/source/fitz/filter-basic.c index 959006f9..b64d8b66 100644 --- a/source/fitz/filter-basic.c +++ b/source/fitz/filter-basic.c @@ -16,7 +16,7 @@ struct null_filter { fz_stream *chain; size_t remain; - fz_off_t offset; + int64_t offset; unsigned char buffer[4096]; }; @@ -41,8 +41,8 @@ next_null(fz_context *ctx, fz_stream *stm, size_t max) return EOF; state->chain->rp += n; state->remain -= n; - state->offset += (fz_off_t)n; - stm->pos += (fz_off_t)n; + state->offset += (int64_t)n; + stm->pos += (int64_t)n; return *stm->rp++; } @@ -56,7 +56,7 @@ close_null(fz_context *ctx, void *state_) } fz_stream * -fz_open_null(fz_context *ctx, fz_stream *chain, int len, fz_off_t offset) +fz_open_null(fz_context *ctx, fz_stream *chain, int len, int64_t offset) { struct null_filter *state = NULL; @@ -106,7 +106,7 @@ next_concat(fz_context *ctx, fz_stream *stm, size_t max) { stm->rp = state->chain[state->current]->rp; stm->wp = state->chain[state->current]->wp; - stm->pos += (fz_off_t)n; + stm->pos += (int64_t)n; return *stm->rp++; } else @@ -581,7 +581,7 @@ next_arc4(fz_context *ctx, fz_stream *stm, size_t max) stm->wp = state->buffer + n; fz_arc4_encrypt(&state->arc4, stm->rp, state->chain->rp, n); state->chain->rp += n; - stm->pos += (fz_off_t)n; + stm->pos += (int64_t)n; return *stm->rp++; } diff --git a/source/fitz/filter-dct.c b/source/fitz/filter-dct.c index cdec5d3f..644d0dfc 100644 --- a/source/fitz/filter-dct.c +++ b/source/fitz/filter-dct.c @@ -1,5 +1,6 @@ #include "mupdf/fitz.h" +#include <stdio.h> #include <jpeglib.h> #ifndef SHARE_JPEG diff --git a/source/fitz/filter-jbig2.c b/source/fitz/filter-jbig2.c index dd46269b..cbb70988 100644 --- a/source/fitz/filter-jbig2.c +++ b/source/fitz/filter-jbig2.c @@ -2,6 +2,8 @@ #ifdef HAVE_LURATECH +#include <string.h> + #include <ldf_jb2.h> typedef struct fz_jbig2d_s fz_jbig2d; diff --git a/source/fitz/glyph.c b/source/fitz/glyph.c index f5d97768..4c22b94b 100644 --- a/source/fitz/glyph.c +++ b/source/fitz/glyph.c @@ -57,6 +57,8 @@ fz_glyph_height(fz_context *ctx, fz_glyph *glyph) } #ifndef NDEBUG +#include <stdio.h> + void fz_dump_glyph(fz_glyph *glyph) { diff --git a/source/fitz/load-jpeg.c b/source/fitz/load-jpeg.c index 810d49e0..b088325b 100644 --- a/source/fitz/load-jpeg.c +++ b/source/fitz/load-jpeg.c @@ -1,5 +1,6 @@ #include "mupdf/fitz.h" +#include <stdio.h> #include <string.h> #include <limits.h> diff --git a/source/fitz/load-jpx.c b/source/fitz/load-jpx.c index f2751a58..3d7735d5 100644 --- a/source/fitz/load-jpx.c +++ b/source/fitz/load-jpx.c @@ -279,10 +279,10 @@ jpx_read_image(fz_context *ctx, fz_jpxd *state, const unsigned char *data, size_ { err = JP2_Decompress_GetProp(doc, cJP2_Prop_Width, &widths[k], -1, k); if (err != cJP2_Error_OK) - fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get width for component %ld: %d", k, (int) err); + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get width for component %d: %d", (int) k, (int) err); err = JP2_Decompress_GetProp(doc, cJP2_Prop_Height, &heights[k], -1, k); if (err != cJP2_Error_OK) - fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get height for component %ld: %d", k, (int) err); + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get height for component %d: %d", (int) k, (int) err); state->width = fz_maxi(state->width, widths[k]); state->height = fz_maxi(state->height, heights[k]); @@ -380,10 +380,10 @@ jpx_read_image(fz_context *ctx, fz_jpxd *state, const unsigned char *data, size_ { err = JP2_Decompress_GetProp(doc, cJP2_Prop_Bits_Per_Sample, &state->bpss[k], -1, k); if (err != cJP2_Error_OK) - fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get bits per sample for component %ld: %d", k, (int) err); + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get bits per sample for component %d: %d", (int) k, (int) err); err = JP2_Decompress_GetProp(doc, cJP2_Prop_Signed_Samples, &state->signs[k], -1, k); if (err != cJP2_Error_OK) - fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get signed for component %ld: %d", k, (int) err); + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get signed for component %d: %d", (int) k, (int) err); } if (state->signs[k]) state->signs[k] = 1 << (state->bpss[k] - 1); diff --git a/source/fitz/load-jxr.c b/source/fitz/load-jxr.c index 29877575..789fcfe6 100644 --- a/source/fitz/load-jxr.c +++ b/source/fitz/load-jxr.c @@ -2,6 +2,9 @@ #ifdef HAVE_JPEGXR +#include <math.h> +#include <string.h> + #include <jpegxr.h> struct info diff --git a/source/fitz/memory.c b/source/fitz/memory.c index 5aa8d78c..b935853d 100644 --- a/source/fitz/memory.c +++ b/source/fitz/memory.c @@ -98,7 +98,9 @@ fz_malloc_array_no_throw(fz_context *ctx, size_t count, size_t size) if (count > SIZE_MAX / size) { - fprintf(stderr, "error: malloc of array (" FZ_FMT_zu " x " FZ_FMT_zu " bytes) failed (size_t overflow)", count, size); + char buf[100]; + fz_snprintf(buf, sizeof buf, "error: malloc of array (%zu x %zu bytes) failed (size_t overflow)", count, size); + fprintf(stderr, "%s\n", buf); return NULL; } @@ -137,7 +139,9 @@ fz_calloc_no_throw(fz_context *ctx, size_t count, size_t size) if (count > SIZE_MAX / size) { - fprintf(stderr, "error: calloc (" FZ_FMT_zu " x " FZ_FMT_zu " bytes) failed (size_t overflow)\n", count, size); + char buf[100]; + fz_snprintf(buf, sizeof buf, "error: calloc of array (%zu x %zu bytes) failed (size_t overflow)", count, size); + fprintf(stderr, "%s\n", buf); return NULL; } @@ -180,7 +184,9 @@ fz_resize_array_no_throw(fz_context *ctx, void *p, size_t count, size_t size) if (count > SIZE_MAX / size) { - fprintf(stderr, "error: resize array (" FZ_FMT_zu " x " FZ_FMT_zu " bytes) failed (size_t overflow)\n", count, size); + char buf[100]; + fz_snprintf(buf, sizeof buf, "error: resize array (%zu x %zu bytes) failed (size_t overflow)", count, size); + fprintf(stderr, "%s\n", buf); return NULL; } diff --git a/source/fitz/output-pcl.c b/source/fitz/output-pcl.c index e3999b25..3898ac9c 100644 --- a/source/fitz/output-pcl.c +++ b/source/fitz/output-pcl.c @@ -1,7 +1,8 @@ #include "mupdf/fitz.h" -#include <string.h> #include <limits.h> +#include <stdlib.h> +#include <string.h> /* Lifted from ghostscript gdevjlm.h */ /* diff --git a/source/fitz/output-pclm.c b/source/fitz/output-pclm.c index 0d1f728c..8f05f3bf 100644 --- a/source/fitz/output-pclm.c +++ b/source/fitz/output-pclm.c @@ -2,7 +2,6 @@ #include <string.h> #include <limits.h> -#include <zlib.h> const char *fz_pclm_write_options_usage = "PCLm output options:\n" @@ -29,7 +28,7 @@ fz_parse_pclm_options(fz_context *ctx, fz_pclm_options *opts, const char *args) } if (fz_has_option(ctx, args, "strip-height", &val)) { - int i = atoi(val); + int i = fz_atoi(val); if (i <= 0) fz_throw(ctx, FZ_ERROR_GENERIC, "Unsupported PCLm strip height %d (suggest 16)", i); opts->strip_height = i; @@ -65,7 +64,7 @@ typedef struct pclm_band_writer_s int obj_num; int xref_max; - fz_off_t *xref; + int64_t *xref; int pages; int page_max; int *page_obj; @@ -77,7 +76,7 @@ typedef struct pclm_band_writer_s static int new_obj(fz_context *ctx, pclm_band_writer *writer) { - fz_off_t pos = fz_tell_output(ctx, writer->super.out); + int64_t pos = fz_tell_output(ctx, writer->super.out); if (writer->obj_num >= writer->xref_max) { @@ -192,7 +191,7 @@ flush_strip(fz_context *ctx, pclm_band_writer *writer, int fill) /* Buffer is full, compress it and write it. */ if (writer->options.compress) { - uLongf destLen = writer->complen; + size_t destLen = writer->complen; fz_deflate(ctx, writer->compbuf, &destLen, data, len, FZ_DEFLATE_DEFAULT); len = destLen; data = writer->compbuf; @@ -245,7 +244,7 @@ pclm_drop_band_writer(fz_context *ctx, fz_band_writer *writer_) /* We actually do the trailer writing in the drop */ if (writer->xref_max > 2) { - fz_off_t t_pos; + int64_t t_pos; /* Catalog */ writer->xref[1] = fz_tell_output(ctx, out); @@ -264,7 +263,7 @@ pclm_drop_band_writer(fz_context *ctx, fz_band_writer *writer_) fz_write_printf(ctx, out, "xref\n0 %d\n0000000000 65535 f \n", writer->obj_num); for (i = 1; i < writer->obj_num; i++) fz_write_printf(ctx, out, "%010zd 00000 n \n", writer->xref[i]); - fz_write_printf(ctx, out, "trailer\n<<\n/Size %d\n/Root 1 0 R\n>>\nstartxref\n%Zd\n%%%%EOF\n", writer->obj_num, t_pos); + fz_write_printf(ctx, out, "trailer\n<<\n/Size %d\n/Root 1 0 R\n>>\nstartxref\n%ld\n%%%%EOF\n", writer->obj_num, t_pos); } fz_free(ctx, writer->stripbuf); diff --git a/source/fitz/output-png.c b/source/fitz/output-png.c index d21bc59b..709d65f0 100644 --- a/source/fitz/output-png.c +++ b/source/fitz/output-png.c @@ -1,5 +1,7 @@ #include "mupdf/fitz.h" +#include <string.h> + #include <zlib.h> static inline void big32(unsigned char *buf, unsigned int v) diff --git a/source/fitz/output-psd.c b/source/fitz/output-psd.c index d39051dd..37d51bc8 100644 --- a/source/fitz/output-psd.c +++ b/source/fitz/output-psd.c @@ -1,5 +1,7 @@ #include "mupdf/fitz.h" +#include <string.h> + void fz_save_pixmap_as_psd(fz_context *ctx, fz_pixmap *pixmap, const char *filename) { @@ -118,7 +120,7 @@ psd_write_header(fz_context *ctx, fz_band_writer *writer_, const fz_colorspace * size_t len2; if (name == NULL) { - sprintf(text, "Spot%d", i-4); + fz_snprintf(text, sizeof text, "Spot%d", i-4); name = text; } len2 = strlen(name); @@ -147,7 +149,7 @@ psd_write_header(fz_context *ctx, fz_band_writer *writer_, const fz_colorspace * char text[32]; if (name == NULL) { - sprintf(text, "Spot%d", i-4); + fz_snprintf(text, sizeof text, "Spot%d", i-4); name = text; } len2 = strlen(name); @@ -277,9 +279,9 @@ psd_write_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_st fz_write_data(ctx, out, buffer, b - buffer); b = buffer; } - fz_seek_output(ctx, out, plane_inc, FZ_SEEK_CUR); + fz_seek_output(ctx, out, plane_inc, SEEK_CUR); } - fz_seek_output(ctx, out, w * h * (1-n), FZ_SEEK_CUR); + fz_seek_output(ctx, out, w * h * (1-n), SEEK_CUR); } static void diff --git a/source/fitz/output.c b/source/fitz/output.c index 44efc414..06b25be5 100644 --- a/source/fitz/output.c +++ b/source/fitz/output.c @@ -1,3 +1,8 @@ +#define _LARGEFILE_SOURCE +#ifndef _FILE_OFFSET_BITS +#define _FILE_OFFSET_BITS 64 +#endif + #include "mupdf/fitz.h" #include "fitz-imp.h" @@ -118,19 +123,27 @@ std_write(fz_context *ctx, void *opaque, const void *buffer, size_t count) } static void -file_seek(fz_context *ctx, void *opaque, fz_off_t off, int whence) +file_seek(fz_context *ctx, void *opaque, int64_t off, int whence) { FILE *file = opaque; - int n = fz_fseek(file, off, whence); +#if defined(_WIN32) || defined(_WIN64) + int n = _fseeki64(file, off, whence); +#else + int n = fseeko(file, off, whence); +#endif if (n < 0) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot fseek: %s", strerror(errno)); } -static fz_off_t +static int64_t file_tell(fz_context *ctx, void *opaque) { FILE *file = opaque; - fz_off_t off = fz_ftell(file); +#if defined(_WIN32) || defined(_WIN64) + int64_t off = _ftelli64(file); +#else + int64_t off = ftello(file); +#endif if (off == -1) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot ftell: %s", strerror(errno)); return off; @@ -167,35 +180,41 @@ fz_new_output(fz_context *ctx, void *state, fz_output_write_fn *write, fz_output } fz_output * -fz_new_output_with_file_ptr(fz_context *ctx, FILE *file, int close) -{ - fz_output *out = fz_new_output(ctx, file, file_write, close ? file_close : NULL); - out->seek = file_seek; - out->tell = file_tell; - return out; -} - -fz_output * fz_new_output_with_path(fz_context *ctx, const char *filename, int append) { FILE *file; + fz_output *out; if (!strcmp(filename, "/dev/null") || !fz_strcasecmp(filename, "nul:")) return NULL; +#if defined(_WIN32) || defined(_WIN64) /* Ensure we create a brand new file. We don't want to clobber our old file. */ if (!append) { - if (fz_remove(filename) < 0) + if (fz_remove_utf8(filename) < 0) if (errno != ENOENT) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot remove file '%s': %s", filename, strerror(errno)); } - - file = fz_fopen(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) + { + if (remove(filename) < 0) + if (errno != ENOENT) + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot remove file '%s': %s", filename, strerror(errno)); + } + file = fopen(filename, append ? "ab" : "wb"); +#endif if (!file) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno)); - return fz_new_output_with_file_ptr(ctx, file, 1); + out = fz_new_output(ctx, file, file_write, file_close); + out->seek = file_seek; + out->tell = file_tell; + + return out; } static void @@ -206,16 +225,16 @@ buffer_write(fz_context *ctx, void *opaque, const void *data, size_t len) } static void -buffer_seek(fz_context *ctx, void *opaque, fz_off_t off, int whence) +buffer_seek(fz_context *ctx, void *opaque, int64_t off, int whence) { fz_throw(ctx, FZ_ERROR_GENERIC, "cannot seek in buffer: %s", strerror(errno)); } -static fz_off_t +static int64_t buffer_tell(fz_context *ctx, void *opaque) { fz_buffer *buffer = opaque; - return (fz_off_t)buffer->len; + return (int64_t)buffer->len; } static void @@ -245,7 +264,7 @@ fz_drop_output(fz_context *ctx, fz_output *out) } void -fz_seek_output(fz_context *ctx, fz_output *out, fz_off_t off, int whence) +fz_seek_output(fz_context *ctx, fz_output *out, int64_t off, int whence) { if (!out) return; if (out->seek == NULL) @@ -253,7 +272,7 @@ fz_seek_output(fz_context *ctx, fz_output *out, fz_off_t off, int whence) out->seek(ctx, out->state, off, whence); } -fz_off_t +int64_t fz_tell_output(fz_context *ctx, fz_output *out) { if (!out) return 0; diff --git a/source/fitz/printf.c b/source/fitz/printf.c index 3fe8c7e4..bbdda93f 100644 --- a/source/fitz/printf.c +++ b/source/fitz/printf.c @@ -5,6 +5,22 @@ #include <stdarg.h> #include <stdio.h> +#ifdef _MSC_VER +#if _MSC_VER < 1500 /* MSVC 2008 */ +int snprintf(char *s, size_t n, const char *fmt, ...) +{ + int r; + va_list ap; + va_start(ap, fmt); + r = vsprintf(s, fmt, ap); + va_end(ap); + return r; +} +#else if _MSC_VER < 1900 /* MSVC 2015 */ +#define snprintf _snprintf +#endif +#endif + static const char *fz_hex_digits = "0123456789abcdef"; struct fmtbuf @@ -290,11 +306,7 @@ fz_format_string(fz_context *ctx, void *user, void (*emit)(fz_context *ctx, void bits = 0; if (c == 'l') { c = *fmt++; - bits = sizeof(long) * 8; - if (c == 'l') { - c = *fmt++; - bits = 64; - } + bits = sizeof(int64_t) * 8; if (c == 0) break; } @@ -310,12 +322,6 @@ fz_format_string(fz_context *ctx, void *user, void (*emit)(fz_context *ctx, void if (c == 0) break; } - if (c == 'Z') { - c = *fmt++; - bits = sizeof(fz_off_t) * 8; - if (c == 0) - break; - } switch (c) { default: diff --git a/source/fitz/separation.c b/source/fitz/separation.c index 8a154347..82f6ead7 100644 --- a/source/fitz/separation.c +++ b/source/fitz/separation.c @@ -1,5 +1,8 @@ #include "mupdf/fitz.h" +#include <assert.h> +#include <string.h> + enum { FZ_SEPARATION_DISABLED_RENDER = 3 diff --git a/source/fitz/stext-device.c b/source/fitz/stext-device.c index 069caa9f..117cd72e 100644 --- a/source/fitz/stext-device.c +++ b/source/fitz/stext-device.c @@ -3,8 +3,7 @@ #include <math.h> #include <float.h> - -#include <stdio.h> /* for debug printing */ +#include <string.h> /* Extract text into blocks and lines. */ diff --git a/source/fitz/store.c b/source/fitz/store.c index bb9426ba..e2303fd3 100644 --- a/source/fitz/store.c +++ b/source/fitz/store.c @@ -1,6 +1,7 @@ #include "mupdf/fitz.h" #include <assert.h> +#include <limits.h> #include <stdio.h> typedef struct fz_item_s fz_item; diff --git a/source/fitz/stream-open.c b/source/fitz/stream-open.c index d7cff535..ffd61b66 100644 --- a/source/fitz/stream-open.c +++ b/source/fitz/stream-open.c @@ -1,3 +1,9 @@ +#define _LARGEFILE_SOURCE +#ifndef _FILE_OFFSET_BITS +#define _FILE_OFFSET_BITS 64 +#endif + +#include "mupdf/fitz.h" #include "fitz-imp.h" #include <string.h> @@ -7,7 +13,12 @@ int fz_file_exists(fz_context *ctx, const char *path) { - FILE *file = fz_fopen(path, "rb"); + FILE *file; +#if defined(_WIN32) || defined(_WIN64) + file = fz_fopen_utf8(path, "rb"); +#else + file = fopen(path, "rb"); +#endif if (file) fclose(file); return !!file; @@ -66,6 +77,9 @@ fz_drop_stream(fz_context *ctx, fz_stream *stm) /* File stream */ +// TODO: WIN32: HANDLE CreateFileW(), etc. +// TODO: POSIX: int creat(), read(), write(), lseeko, etc. + typedef struct fz_file_stream_s { FILE *file; @@ -82,20 +96,28 @@ static int next_file(fz_context *ctx, fz_stream *stm, size_t n) fz_throw(ctx, FZ_ERROR_GENERIC, "read error: %s", strerror(errno)); stm->rp = state->buffer; stm->wp = state->buffer + n; - stm->pos += (fz_off_t)n; + stm->pos += (int64_t)n; if (n == 0) return EOF; return *stm->rp++; } -static void seek_file(fz_context *ctx, fz_stream *stm, fz_off_t offset, int whence) +static void seek_file(fz_context *ctx, fz_stream *stm, int64_t offset, int whence) { fz_file_stream *state = stm->state; - fz_off_t n = fz_fseek(state->file, offset, whence); +#if defined(_WIN32) || defined(_WIN64) + int64_t n = _fseeki64(state->file, offset, whence); +#else + int64_t n = fseeko(state->file, offset, whence); +#endif if (n < 0) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot seek: %s", strerror(errno)); - stm->pos = fz_ftell(state->file); +#if defined(_WIN32) || defined(_WIN64) + stm->pos = _ftelli64(state->file); +#else + stm->pos = ftello(state->file); +#endif stm->rp = state->buffer; stm->wp = state->buffer; } @@ -109,7 +131,7 @@ static void close_file(fz_context *ctx, void *state_) fz_free(ctx, state); } -fz_stream * +static fz_stream * fz_open_file_ptr(fz_context *ctx, FILE *file) { fz_stream *stm; @@ -125,35 +147,25 @@ fz_open_file_ptr(fz_context *ctx, FILE *file) fz_stream * fz_open_file(fz_context *ctx, const char *name) { - FILE *f; + FILE *file; #if defined(_WIN32) || defined(_WIN64) - char *s = (char*)name; - wchar_t *wname, *d; - int c; - d = wname = fz_malloc(ctx, (strlen(name)+1) * sizeof(wchar_t)); - while (*s) { - s += fz_chartorune(&c, s); - *d++ = c; - } - *d = 0; - f = _wfopen(wname, L"rb"); - fz_free(ctx, wname); + file = fz_fopen_utf8(name, "rb"); #else - f = fz_fopen(name, "rb"); + file = fopen(name, "rb"); #endif - if (f == NULL) + if (file == NULL) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open %s: %s", name, strerror(errno)); - return fz_open_file_ptr(ctx, f); + return fz_open_file_ptr(ctx, file); } #if defined(_WIN32) || defined(_WIN64) fz_stream * fz_open_file_w(fz_context *ctx, const wchar_t *name) { - FILE *f = _wfopen(name, L"rb"); - if (f == NULL) + FILE *file = _wfopen(name, L"rb"); + if (file == NULL) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file %ls: %s", name, strerror(errno)); - return fz_open_file_ptr(ctx, f); + return fz_open_file_ptr(ctx, file); } #endif @@ -164,9 +176,9 @@ static int next_buffer(fz_context *ctx, fz_stream *stm, size_t max) return EOF; } -static void seek_buffer(fz_context *ctx, fz_stream *stm, fz_off_t offset, int whence) +static void seek_buffer(fz_context *ctx, fz_stream *stm, int64_t offset, int whence) { - fz_off_t pos = stm->pos - (stm->wp - stm->rp); + int64_t pos = stm->pos - (stm->wp - stm->rp); /* Convert to absolute pos */ if (whence == 1) { @@ -202,7 +214,7 @@ fz_open_buffer(fz_context *ctx, fz_buffer *buf) stm->rp = buf->data; stm->wp = buf->data + buf->len; - stm->pos = (fz_off_t)buf->len; + stm->pos = (int64_t)buf->len; return stm; } @@ -218,7 +230,7 @@ fz_open_memory(fz_context *ctx, const unsigned char *data, size_t len) stm->rp = (unsigned char *)data; stm->wp = (unsigned char *)data + len; - stm->pos = (fz_off_t)len; + stm->pos = (int64_t)len; return stm; } diff --git a/source/fitz/stream-prog.c b/source/fitz/stream-prog.c index eeb33242..89158832 100644 --- a/source/fitz/stream-prog.c +++ b/source/fitz/stream-prog.c @@ -10,8 +10,8 @@ typedef struct prog_state { FILE *file; - fz_off_t length; - fz_off_t available; + int64_t length; + int64_t available; int bps; clock_t start_time; unsigned char buffer[4096]; @@ -29,7 +29,7 @@ static int next_prog(fz_context *ctx, fz_stream *stm, size_t len) /* Simulate more data having arrived */ if (ps->available < ps->length) { - fz_off_t av = (fz_off_t)((float)(clock() - ps->start_time) * ps->bps / (CLOCKS_PER_SEC*8)); + int64_t av = (int64_t)((float)(clock() - ps->start_time) * ps->bps / (CLOCKS_PER_SEC*8)); if (av > ps->length) av = ps->length; ps->available = av; @@ -47,13 +47,13 @@ static int next_prog(fz_context *ctx, fz_stream *stm, size_t len) 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 += (fz_off_t)n; + stm->pos += (int64_t)n; if (n == 0) return EOF; return *stm->rp++; } -static void seek_prog(fz_context *ctx, fz_stream *stm, fz_off_t offset, int whence) +static void seek_prog(fz_context *ctx, fz_stream *stm, int64_t offset, int whence) { prog_state *ps = (prog_state *)stm->state; @@ -83,7 +83,7 @@ static void seek_prog(fz_context *ctx, fz_stream *stm, fz_off_t offset, int when fz_throw(ctx, FZ_ERROR_TRYLATER, "Not enough data to seek to offset yet"); } - if (fz_fseek(ps->file, offset, whence) != 0) + if (fseek(ps->file, offset, whence) != 0) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot seek: %s", strerror(errno)); stm->pos = offset; stm->wp = stm->rp; @@ -110,7 +110,7 @@ static int meta_prog(fz_context *ctx, fz_stream *stm, int key, int size, void *p return -1; } -fz_stream * +static fz_stream * fz_open_file_ptr_progressive(fz_context *ctx, FILE *file, int bps) { fz_stream *stm; @@ -122,9 +122,9 @@ fz_open_file_ptr_progressive(fz_context *ctx, FILE *file, int bps) state->start_time = clock(); state->available = 0; - fz_fseek(state->file, 0, SEEK_END); - state->length = fz_ftell(state->file); - fz_fseek(state->file, 0, SEEK_SET); + fseek(state->file, 0, SEEK_END); + state->length = ftell(state->file); + fseek(state->file, 0, SEEK_SET); stm = fz_new_stream(ctx, state, next_prog, close_prog); stm->seek = seek_prog; @@ -138,19 +138,9 @@ fz_open_file_progressive(fz_context *ctx, const char *name, int bps) { FILE *f; #if defined(_WIN32) || defined(_WIN64) - char *s = (char*)name; - wchar_t *wname, *d; - int c; - d = wname = fz_malloc(ctx, (strlen(name)+1) * sizeof(wchar_t)); - while (*s) { - s += fz_chartorune(&c, s); - *d++ = c; - } - *d = 0; - f = _wfopen(wname, L"rb"); - fz_free(ctx, wname); + f = fz_fopen_utf8(name, "rb"); #else - f = fz_fopen(name, "rb"); + f = fopen(name, "rb"); #endif if (f == NULL) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open %s", name); diff --git a/source/fitz/stream-read.c b/source/fitz/stream-read.c index 118589f9..1a2bda01 100644 --- a/source/fitz/stream-read.c +++ b/source/fitz/stream-read.c @@ -137,14 +137,14 @@ fz_read_line(fz_context *ctx, fz_stream *stm, char *mem, size_t n) return (s == mem && c == EOF) ? NULL : mem; } -fz_off_t +int64_t fz_tell(fz_context *ctx, fz_stream *stm) { return stm->pos - (stm->wp - stm->rp); } void -fz_seek(fz_context *ctx, fz_stream *stm, fz_off_t offset, int whence) +fz_seek(fz_context *ctx, fz_stream *stm, int64_t offset, int whence) { stm->avail = 0; /* Reset bit reading */ if (stm->seek) diff --git a/source/fitz/string.c b/source/fitz/string.c index e3c62d02..dbcc2c22 100644 --- a/source/fitz/string.c +++ b/source/fitz/string.c @@ -459,11 +459,11 @@ int fz_atoi(const char *s) return atoi(s); } -fz_off_t fz_atoo(const char *s) +int64_t fz_atoi64(const char *s) { if (s == NULL) return 0; - return fz_atoo_imp(s); + return atoll(s); } int fz_is_page_range(fz_context *ctx, const char *s) diff --git a/source/fitz/time.c b/source/fitz/time.c index 99c0952c..9fb8cfa4 100644 --- a/source/fitz/time.c +++ b/source/fitz/time.c @@ -2,6 +2,8 @@ #include "mupdf/fitz.h" +#include <stdio.h> +#include <errno.h> #include <time.h> #include <windows.h> @@ -79,7 +81,7 @@ fz_wchar_from_utf8(const char *s) return r; } -FILE * +void * fz_fopen_utf8(const char *name, const char *mode) { wchar_t *wname, *wmode; diff --git a/source/fitz/untar.c b/source/fitz/untar.c index d8ae6336..ad93e1e3 100644 --- a/source/fitz/untar.c +++ b/source/fitz/untar.c @@ -59,7 +59,7 @@ static void ensure_tar_entries(fz_context *ctx, fz_tar_archive *tar) tar->count = 0; - fz_seek(ctx, file, 0, FZ_SEEK_SET); + fz_seek(ctx, file, 0, SEEK_SET); while (1) { diff --git a/source/fitz/unzip.c b/source/fitz/unzip.c index 0bcce0fd..c5798fa8 100644 --- a/source/fitz/unzip.c +++ b/source/fitz/unzip.c @@ -230,7 +230,7 @@ static void ensure_zip_entries(fz_context *ctx, fz_zip_archive *zip) size_t size, back, maxback; size_t i, n; - fz_seek(ctx, file, 0, FZ_SEEK_END); + fz_seek(ctx, file, 0, SEEK_END); size = fz_tell(ctx, file); maxback = fz_minz(size, 0xFFFF + sizeof buf); @@ -238,7 +238,7 @@ static void ensure_zip_entries(fz_context *ctx, fz_zip_archive *zip) while (back < maxback) { - fz_seek(ctx, file, (fz_off_t)(size - back), 0); + fz_seek(ctx, file, (int64_t)(size - back), 0); n = fz_read(ctx, file, buf, sizeof buf); if (n < 4) break; diff --git a/source/fitz/xml.c b/source/fitz/xml.c index bcd3179c..06583952 100644 --- a/source/fitz/xml.c +++ b/source/fitz/xml.c @@ -88,6 +88,7 @@ struct fz_xml_s fz_xml *up, *down, *tail, *prev, *next; }; +#if 0 static void xml_indent(int n) { while (n--) { @@ -144,6 +145,7 @@ void fz_debug_xml(fz_xml *item, int level) printf(")%s\n", item->name); } } +#endif fz_xml *fz_xml_prev(fz_xml *item) { diff --git a/source/fitz/zip.c b/source/fitz/zip.c index 45ce4488..fbca0590 100644 --- a/source/fitz/zip.c +++ b/source/fitz/zip.c @@ -69,7 +69,7 @@ fz_write_zip_entry(fz_context *ctx, fz_zip_writer *zip, const char *name, fz_buf void fz_close_zip_writer(fz_context *ctx, fz_zip_writer *zip) { - fz_off_t offset = fz_tell_output(ctx, zip->output); + int64_t offset = fz_tell_output(ctx, zip->output); fz_write_data(ctx, zip->output, zip->central->data, zip->central->len); |