summaryrefslogtreecommitdiff
path: root/source/fitz/filter-jbig2.c
diff options
context:
space:
mode:
authorSebastian Rasmussen <sebras@gmail.com>2017-08-06 00:42:34 +0800
committerSebastian Rasmussen <sebras@gmail.com>2017-08-07 22:04:49 +0800
commit5ad87e299f4e3affcca12a0173429861d01bb610 (patch)
treea3f87b6770564225c71c49f45b7e59eaa9cf9d90 /source/fitz/filter-jbig2.c
parentfa1d8f9edbf7fabdcdc88adff95b06618150af05 (diff)
downloadmupdf-5ad87e299f4e3affcca12a0173429861d01bb610.tar.xz
Set custom allocator for JBIG2 library.
Diffstat (limited to 'source/fitz/filter-jbig2.c')
-rw-r--r--source/fitz/filter-jbig2.c47
1 files changed, 45 insertions, 2 deletions
diff --git a/source/fitz/filter-jbig2.c b/source/fitz/filter-jbig2.c
index 7e7a9420..a822f1ca 100644
--- a/source/fitz/filter-jbig2.c
+++ b/source/fitz/filter-jbig2.c
@@ -248,16 +248,24 @@ fz_open_jbig2d(fz_context *ctx, fz_stream *chain, fz_jbig2_globals *globals)
typedef struct fz_jbig2d_s fz_jbig2d;
+struct fz_jbig2_alloc_s
+{
+ Jbig2Allocator alloc;
+ fz_context *ctx;
+};
+
struct fz_jbig2_globals_s
{
fz_storable storable;
Jbig2GlobalCtx *gctx;
+ struct fz_jbig2_alloc_s alloc;
};
struct fz_jbig2d_s
{
fz_stream *chain;
Jbig2Ctx *ctx;
+ struct fz_jbig2_alloc_s alloc;
fz_jbig2_globals *gctx;
Jbig2Image *page;
int idx;
@@ -341,12 +349,43 @@ error_callback(void *data, const char *msg, Jbig2Severity severity, int32_t seg_
return 0;
}
+static void *fz_jbig2_alloc(Jbig2Allocator *allocator, size_t size)
+{
+ fz_context *ctx = ((struct fz_jbig2_alloc_s *) allocator)->ctx;
+ return fz_malloc_no_throw(ctx, size);
+}
+
+static void fz_jbig2_free(Jbig2Allocator *allocator, void *p)
+{
+ fz_context *ctx = ((struct fz_jbig2_alloc_s *) allocator)->ctx;
+ return fz_free(ctx, p);
+}
+
+static void *fz_jbig2_realloc(Jbig2Allocator *allocator, void *p, size_t size)
+{
+ fz_context *ctx = ((struct fz_jbig2_alloc_s *) allocator)->ctx;
+ if (size == 0)
+ {
+ fz_free(ctx, p);
+ return NULL;
+ }
+ if (p == NULL)
+ return fz_malloc(ctx, size);
+ return fz_resize_array_no_throw(ctx, p, 1, size);
+}
+
fz_jbig2_globals *
fz_load_jbig2_globals(fz_context *ctx, fz_buffer *buf)
{
fz_jbig2_globals *globals = fz_malloc_struct(ctx, fz_jbig2_globals);
+ Jbig2Ctx *jctx;
+
+ globals->alloc.ctx = ctx;
+ globals->alloc.alloc.alloc = fz_jbig2_alloc;
+ globals->alloc.alloc.free = fz_jbig2_free;
+ globals->alloc.alloc.realloc = fz_jbig2_realloc;
- Jbig2Ctx *jctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, NULL, error_callback, ctx);
+ jctx = jbig2_ctx_new((Jbig2Allocator *) &globals->alloc, JBIG2_OPTIONS_EMBEDDED, NULL, error_callback, ctx);
jbig2_data_in(jctx, buf->data, buf->len);
FZ_INIT_STORABLE(globals, 1, fz_drop_jbig2_globals_imp);
@@ -375,7 +414,11 @@ fz_open_jbig2d(fz_context *ctx, fz_stream *chain, fz_jbig2_globals *globals)
state = fz_malloc_struct(ctx, fz_jbig2d);
state->gctx = globals;
state->chain = chain;
- state->ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, globals ? globals->gctx : NULL, error_callback, ctx);
+ state->alloc.ctx = ctx;
+ state->alloc.alloc.alloc = fz_jbig2_alloc;
+ state->alloc.alloc.free = fz_jbig2_free;
+ state->alloc.alloc.realloc = fz_jbig2_realloc;
+ state->ctx = jbig2_ctx_new((Jbig2Allocator *) &state->alloc, JBIG2_OPTIONS_EMBEDDED, globals ? globals->gctx : NULL, error_callback, ctx);
state->page = NULL;
state->idx = 0;
}