diff options
Diffstat (limited to 'fitz')
-rw-r--r-- | fitz/base_memory.c | 23 | ||||
-rw-r--r-- | fitz/base_object.c | 20 | ||||
-rw-r--r-- | fitz/filt_jpxd.c | 19 | ||||
-rw-r--r-- | fitz/fitz.h | 19 | ||||
-rw-r--r-- | fitz/memento.c | 4 | ||||
-rw-r--r-- | fitz/res_bitmap.c | 7 | ||||
-rw-r--r-- | fitz/res_font.c | 33 | ||||
-rw-r--r-- | fitz/res_pixmap.c | 26 | ||||
-rw-r--r-- | fitz/stm_read.c | 11 |
9 files changed, 88 insertions, 74 deletions
diff --git a/fitz/base_memory.c b/fitz/base_memory.c index 27474cc3..cf972542 100644 --- a/fitz/base_memory.c +++ b/fitz/base_memory.c @@ -36,6 +36,29 @@ fz_malloc_array(fz_context *ctx, unsigned int count, unsigned int size) } void * +fz_calloc(fz_context *ctx, unsigned int count, unsigned int size) +{ + void *p; + + if (count == 0 || size == 0) + return 0; + + if (count > UINT_MAX / size) + { + fprintf(stderr, "fatal error: out of memory (integer overflow)\n"); + abort(); + } + + p = calloc(count, size); + if (!p) + { + fprintf(stderr, "fatal error: out of memory\n"); + abort(); + } + return p; +} + +void * fz_resize_array(fz_context *ctx, void *p, unsigned int count, unsigned int size) { void *np; diff --git a/fitz/base_object.c b/fitz/base_object.c index fae4b1b4..6307e5e0 100644 --- a/fitz/base_object.c +++ b/fitz/base_object.c @@ -795,16 +795,16 @@ fz_free_dict(fz_obj *obj) void fz_drop_obj(fz_obj *obj) { - assert(obj != NULL); - if (--obj->refs == 0) - { - if (obj->kind == FZ_ARRAY) - fz_free_array(obj); - else if (obj->kind == FZ_DICT) - fz_free_dict(obj); - else - fz_free(obj->ctx, obj); - } + if (obj == NULL) + return; + if (--obj->refs) + return; + if (obj->kind == FZ_ARRAY) + fz_free_array(obj); + else if (obj->kind == FZ_DICT) + fz_free_dict(obj); + else + fz_free(obj->ctx, obj); } /* Pretty printing objects */ diff --git a/fitz/filt_jpxd.c b/fitz/filt_jpxd.c index d0d6cdef..5700a664 100644 --- a/fitz/filt_jpxd.c +++ b/fitz/filt_jpxd.c @@ -18,8 +18,8 @@ static void fz_opj_info_callback(const char *msg, void *client_data) /* fprintf(stderr, "openjpeg info: %s", msg); */ } -fz_error -fz_load_jpx_image(fz_context *ctx, fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace *defcs) +fz_pixmap * +fz_load_jpx_image(fz_context *ctx, unsigned char *data, int size, fz_colorspace *defcs) { fz_pixmap *img; opj_event_mgr_t evtmgr; @@ -34,7 +34,7 @@ fz_load_jpx_image(fz_context *ctx, fz_pixmap **imgp, unsigned char *data, int si int x, y, k, v; if (size < 2) - return fz_error_make("not enough data to determine image format"); + fz_throw(ctx, "not enough data to determine image format"); /* Check for SOC marker -- if found we have a bare J2K stream */ if (data[0] == 0xFF && data[1] == 0x4F) @@ -61,16 +61,16 @@ fz_load_jpx_image(fz_context *ctx, fz_pixmap **imgp, unsigned char *data, int si opj_destroy_decompress(info); if (!jpx) - return fz_error_make("opj_decode failed"); + fz_throw(ctx, "opj_decode failed"); for (k = 1; k < jpx->numcomps; k++) { if (jpx->comps[k].w != jpx->comps[0].w) - return fz_error_make("image components have different width"); + fz_throw(ctx, "image components have different width"); if (jpx->comps[k].h != jpx->comps[0].h) - return fz_error_make("image components have different height"); + fz_throw(ctx, "image components have different height"); if (jpx->comps[k].prec != jpx->comps[0].prec) - return fz_error_make("image components have different precision"); + fz_throw(ctx, "image components have different precision"); } n = jpx->numcomps; @@ -112,7 +112,7 @@ fz_load_jpx_image(fz_context *ctx, fz_pixmap **imgp, unsigned char *data, int si if (!img) { opj_image_destroy(jpx); - return fz_error_make("out of memory"); + fz_throw(ctx, "out of memory"); } p = img->samples; @@ -148,6 +148,5 @@ fz_load_jpx_image(fz_context *ctx, fz_pixmap **imgp, unsigned char *data, int si opj_image_destroy(jpx); - *imgp = img; - return fz_okay; + return img; } diff --git a/fitz/fitz.h b/fitz/fitz.h index 048e1c35..aa9ff222 100644 --- a/fitz/fitz.h +++ b/fitz/fitz.h @@ -202,6 +202,7 @@ void fz_free_context(fz_context *ctx); /* The following throw exceptions on failure to allocate */ void *fz_malloc(fz_context *ctx, unsigned int size); void *fz_malloc_array(fz_context *ctx, unsigned int count, unsigned int size); +void *fz_calloc(fz_context *ctx, unsigned int count, unsigned int size); void *fz_resize_array(fz_context *ctx, void *p, unsigned int count, unsigned int size); char *fz_strdup(fz_context *ctx, char *s); @@ -545,7 +546,7 @@ void fz_seek(fz_stream *stm, int offset, int whence); int fz_read(fz_stream *stm, unsigned char *buf, int len); void fz_read_line(fz_stream *stm, char *buf, int max); -fz_error fz_read_all(fz_buffer **bufp, fz_stream *stm, int initial); +fz_buffer *fz_read_all(fz_stream *stm, int initial); static inline int fz_read_byte(fz_stream *stm) { @@ -696,11 +697,11 @@ void fz_gamma_pixmap(fz_pixmap *pix, float gamma); fz_pixmap *fz_scale_pixmap(fz_context *ctx, fz_pixmap *src, float x, float y, float w, float h); fz_pixmap *fz_scale_pixmap_gridfit(fz_context *ctx, fz_pixmap *src, float x, float y, float w, float h, int gridfit); -fz_error fz_write_pnm(fz_pixmap *pixmap, char *filename); -fz_error fz_write_pam(fz_pixmap *pixmap, char *filename, int savealpha); -fz_error fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha); +void fz_write_pnm(fz_context *ctx, fz_pixmap *pixmap, char *filename); +void fz_write_pam(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha); +void fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha); -fz_error fz_load_jpx_image(fz_context *ctx, fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace *dcs); +fz_pixmap *fz_load_jpx_image(fz_context *ctx, unsigned char *data, int size, fz_colorspace *dcs); /* * Bitmaps have 1 component per bit. Only used for creating halftoned versions @@ -722,7 +723,7 @@ fz_bitmap *fz_keep_bitmap(fz_bitmap *bit); void fz_clear_bitmap(fz_bitmap *bit); void fz_drop_bitmap(fz_context *ctx, fz_bitmap *bit); -fz_error fz_write_pbm(fz_bitmap *bitmap, char *filename); +void fz_write_pbm(fz_context *ctx, fz_bitmap *bitmap, char *filename); /* * A halftone is a set of threshold tiles, one per component. Each threshold @@ -806,7 +807,7 @@ struct fz_font_s fz_buffer **t3procs; /* has 256 entries if used */ float *t3widths; /* has 256 entries if used */ void *t3xref; /* a pdf_xref for the callback */ - fz_error (*t3run)(void *xref, fz_obj *resources, fz_buffer *contents, + void (*t3run)(void *xref, fz_obj *resources, fz_buffer *contents, struct fz_device_s *dev, fz_matrix ctm); fz_rect bbox; @@ -818,8 +819,8 @@ struct fz_font_s fz_font *fz_new_type3_font(fz_context *ctx, char *name, fz_matrix matrix); -fz_error fz_new_font_from_memory(fz_context *ctx, fz_font **fontp, unsigned char *data, int len, int index); -fz_error fz_new_font_from_file(fz_context *ctx, fz_font **fontp, char *path, int index); +fz_font *fz_new_font_from_memory(fz_context *ctx, unsigned char *data, int len, int index); +fz_font *fz_new_font_from_file(fz_context *ctx, char *path, int index); fz_font *fz_keep_font(fz_font *font); void fz_drop_font(fz_context *ctx, fz_font *font); diff --git a/fitz/memento.c b/fitz/memento.c index a9885072..24e19e3a 100644 --- a/fitz/memento.c +++ b/fitz/memento.c @@ -487,8 +487,10 @@ static void Memento_event(void) globals.countdown = globals.paranoia; } - if (globals.sequence == globals.breakAt) + if (globals.sequence == globals.breakAt) { + fprintf(stderr, "Breaking at event %d\n", globals.breakAt); Memento_breakpoint(); + } } int Memento_breakAt(int event) diff --git a/fitz/res_bitmap.c b/fitz/res_bitmap.c index 3b13883b..02bb6f44 100644 --- a/fitz/res_bitmap.c +++ b/fitz/res_bitmap.c @@ -46,8 +46,8 @@ fz_clear_bitmap(fz_bitmap *bit) * Write bitmap to PBM file */ -fz_error -fz_write_pbm(fz_bitmap *bitmap, char *filename) +void +fz_write_pbm(fz_context *ctx, fz_bitmap *bitmap, char *filename) { FILE *fp; unsigned char *p; @@ -55,7 +55,7 @@ fz_write_pbm(fz_bitmap *bitmap, char *filename) fp = fopen(filename, "wb"); if (!fp) - return fz_error_make("cannot open file '%s': %s", filename, strerror(errno)); + fz_throw(ctx, "cannot open file '%s': %s", filename, strerror(errno)); assert(bitmap->n == 1); @@ -72,5 +72,4 @@ fz_write_pbm(fz_bitmap *bitmap, char *filename) } fclose(fp); - return fz_okay; } diff --git a/fitz/res_font.c b/fitz/res_font.c index 47e22ff1..a9a1fdf4 100644 --- a/fitz/res_font.c +++ b/fitz/res_font.c @@ -179,8 +179,8 @@ fz_finalize_freetype(fz_context *ctx) } } -fz_error -fz_new_font_from_file(fz_context *ctx, fz_font **fontp, char *path, int index) +fz_font * +fz_new_font_from_file(fz_context *ctx, char *path, int index) { FT_Face face; fz_error error; @@ -189,11 +189,11 @@ fz_new_font_from_file(fz_context *ctx, fz_font **fontp, char *path, int index) error = fz_init_freetype(ctx); if (error) - return fz_error_note(error, "cannot init freetype library"); + fz_throw(ctx, "cannot init freetype library"); fterr = FT_New_Face(fz_ftlib, path, index, &face); if (fterr) - return fz_error_make("freetype: cannot load font: %s", ft_error_string(fterr)); + fz_throw(ctx, "freetype: cannot load font: %s", ft_error_string(fterr)); font = fz_new_font(ctx, face->family_name); font->ft_face = face; @@ -202,12 +202,11 @@ fz_new_font_from_file(fz_context *ctx, fz_font **fontp, char *path, int index) font->bbox.x1 = face->bbox.xMax * 1000 / face->units_per_EM; font->bbox.y1 = face->bbox.yMax * 1000 / face->units_per_EM; - *fontp = font; - return fz_okay; + return font; } -fz_error -fz_new_font_from_memory(fz_context *ctx, fz_font **fontp, unsigned char *data, int len, int index) +fz_font * +fz_new_font_from_memory(fz_context *ctx, unsigned char *data, int len, int index) { FT_Face face; fz_error error; @@ -216,11 +215,11 @@ fz_new_font_from_memory(fz_context *ctx, fz_font **fontp, unsigned char *data, i error = fz_init_freetype(ctx); if (error) - return fz_error_note(error, "cannot init freetype library"); + fz_throw(ctx, "cannot init freetype library"); fterr = FT_New_Memory_Face(fz_ftlib, data, len, index, &face); if (fterr) - return fz_error_make("freetype: cannot load font: %s", ft_error_string(fterr)); + fz_throw(ctx, "freetype: cannot load font: %s", ft_error_string(fterr)); font = fz_new_font(ctx, face->family_name); font->ft_face = face; @@ -229,8 +228,7 @@ fz_new_font_from_memory(fz_context *ctx, fz_font **fontp, unsigned char *data, i font->bbox.x1 = face->bbox.xMax * 1000 / face->units_per_EM; font->bbox.y1 = face->bbox.yMax * 1000 / face->units_per_EM; - *fontp = font; - return fz_okay; + return font; } static fz_matrix @@ -514,7 +512,6 @@ fz_new_type3_font(fz_context *ctx, char *name, fz_matrix matrix) fz_pixmap * fz_render_t3_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_colorspace *model) { - fz_error error; fz_matrix ctm; fz_buffer *contents; fz_bbox bbox; @@ -532,9 +529,8 @@ fz_render_t3_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_co ctm = fz_concat(font->t3matrix, trm); dev = fz_new_bbox_device(ctx, &bbox); - error = font->t3run(font->t3xref, font->t3resources, contents, dev, ctm); - if (error) - fz_error_handle(error, "cannot draw type3 glyph"); + font->t3run(font->t3xref, font->t3resources, contents, dev, ctm); + /* RJW: "cannot draw type3 glyph" */ if (dev->flags & FZ_CHARPROC_MASK) { @@ -565,9 +561,8 @@ fz_render_t3_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_co cache = fz_new_glyph_cache(ctx); dev = fz_new_draw_device_type3(ctx, cache, glyph); - error = font->t3run(font->t3xref, font->t3resources, contents, dev, ctm); - if (error) - fz_error_handle(error, "cannot draw type3 glyph"); + font->t3run(font->t3xref, font->t3resources, contents, dev, ctm); + /* RJW: "cannot draw type3 glyph" */ fz_free_device(dev); fz_free_glyph_cache(ctx, cache); diff --git a/fitz/res_pixmap.c b/fitz/res_pixmap.c index a34933f5..471dcc72 100644 --- a/fitz/res_pixmap.c +++ b/fitz/res_pixmap.c @@ -293,19 +293,19 @@ fz_gamma_pixmap(fz_pixmap *pix, float gamma) * Write pixmap to PNM file (without alpha channel) */ -fz_error -fz_write_pnm(fz_pixmap *pixmap, char *filename) +void +fz_write_pnm(fz_context *ctx, fz_pixmap *pixmap, char *filename) { FILE *fp; unsigned char *p; int len; if (pixmap->n != 1 && pixmap->n != 2 && pixmap->n != 4) - return fz_error_make("pixmap must be grayscale or rgb to write as pnm"); + fz_throw(ctx, "pixmap must be grayscale or rgb to write as pnm"); fp = fopen(filename, "wb"); if (!fp) - return fz_error_make("cannot open file '%s': %s", filename, strerror(errno)); + fz_throw(ctx, "cannot open file '%s': %s", filename, strerror(errno)); if (pixmap->n == 1 || pixmap->n == 2) fprintf(fp, "P5\n"); @@ -340,15 +340,14 @@ fz_write_pnm(fz_pixmap *pixmap, char *filename) } fclose(fp); - return fz_okay; } /* * Write pixmap to PAM file (with or without alpha channel) */ -fz_error -fz_write_pam(fz_pixmap *pixmap, char *filename, int savealpha) +void +fz_write_pam(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha) { unsigned char *sp; int y, w, k; @@ -361,7 +360,7 @@ fz_write_pam(fz_pixmap *pixmap, char *filename, int savealpha) fp = fopen(filename, "wb"); if (!fp) - return fz_error_make("cannot open file '%s': %s", filename, strerror(errno)); + fz_throw(ctx, "cannot open file '%s': %s", filename, strerror(errno)); fprintf(fp, "P7\n"); fprintf(fp, "WIDTH %d\n", pixmap->w); @@ -392,8 +391,6 @@ fz_write_pam(fz_pixmap *pixmap, char *filename, int savealpha) } fclose(fp); - - return fz_okay; } /* @@ -430,7 +427,7 @@ static void putchunk(char *tag, unsigned char *data, int size, FILE *fp) put32(sum, fp); } -fz_error +void fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha) { static const unsigned char pngsig[8] = { 137, 80, 78, 71, 13, 10, 26, 10 }; @@ -443,7 +440,7 @@ fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha) int err; if (pixmap->n != 1 && pixmap->n != 2 && pixmap->n != 4) - return fz_error_make("pixmap must be grayscale or rgb to write as png"); + fz_throw(ctx, "pixmap must be grayscale or rgb to write as png"); sn = pixmap->n; dn = pixmap->n; @@ -488,7 +485,7 @@ fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha) { fz_free(ctx, udata); fz_free(ctx, cdata); - return fz_error_make("cannot compress image data"); + fz_throw(ctx, "cannot compress image data"); } fp = fopen(filename, "wb"); @@ -496,7 +493,7 @@ fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha) { fz_free(ctx, udata); fz_free(ctx, cdata); - return fz_error_make("cannot open file '%s': %s", filename, strerror(errno)); + fz_throw(ctx, "cannot open file '%s': %s", filename, strerror(errno)); } big32(head+0, pixmap->w); @@ -515,5 +512,4 @@ fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha) fz_free(ctx, udata); fz_free(ctx, cdata); - return fz_okay; } diff --git a/fitz/stm_read.c b/fitz/stm_read.c index 23f758eb..58379887 100644 --- a/fitz/stm_read.c +++ b/fitz/stm_read.c @@ -94,8 +94,8 @@ fz_fill_buffer(fz_stream *stm) } } -fz_error -fz_read_all(fz_buffer **bufp, fz_stream *stm, int initial) +fz_buffer * +fz_read_all(fz_stream *stm, int initial) { fz_buffer *buf; int n; @@ -114,14 +114,14 @@ fz_read_all(fz_buffer **bufp, fz_stream *stm, int initial) if (buf->len / 200 > initial) { fz_drop_buffer(ctx, buf); - return fz_error_make("compression bomb detected"); + fz_throw(ctx, "compression bomb detected"); } n = fz_read(stm, buf->data + buf->len, buf->cap - buf->len); if (n < 0) { fz_drop_buffer(ctx, buf); - return fz_error_note(n, "read error"); + fz_throw(ctx, "read error"); } if (n == 0) break; @@ -129,8 +129,7 @@ fz_read_all(fz_buffer **bufp, fz_stream *stm, int initial) buf->len += n; } - *bufp = buf; - return fz_okay; + return buf; } void |