summaryrefslogtreecommitdiff
path: root/fitz
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2011-12-17 00:49:23 +0000
committerRobin Watts <robin.watts@artifex.com>2011-12-17 00:49:23 +0000
commit3eae1449777c4ecccc73a156c7dfff42f927ccc4 (patch)
treeb0cd1bc08b3af218dd06c501402782db91ad0523 /fitz
parentc53b6af33c996a7ae6815ac15254297d43f43a9c (diff)
downloadmupdf-3eae1449777c4ecccc73a156c7dfff42f927ccc4.tar.xz
More memsqueezing fixes; stream creation.
Diffstat (limited to 'fitz')
-rw-r--r--fitz/filt_basic.c117
-rw-r--r--fitz/filt_dctd.c34
-rw-r--r--fitz/filt_faxd.c42
-rw-r--r--fitz/filt_flate.c26
-rw-r--r--fitz/filt_jbig2d.c44
-rw-r--r--fitz/filt_lzwd.c69
-rw-r--r--fitz/filt_predict.c88
7 files changed, 268 insertions, 152 deletions
diff --git a/fitz/filt_basic.c b/fitz/filt_basic.c
index 3e86514f..b0ff340f 100644
--- a/fitz/filt_basic.c
+++ b/fitz/filt_basic.c
@@ -41,10 +41,17 @@ fz_open_null(fz_stream *chain, int len)
struct null_filter *state;
fz_context *ctx = chain->ctx;
- assert(chain);
- state = fz_malloc_struct(ctx, struct null_filter);
- state->chain = chain;
- state->remain = len;
+ fz_try(ctx)
+ {
+ state = fz_malloc_struct(ctx, struct null_filter);
+ state->chain = chain;
+ state->remain = len;
+ }
+ fz_catch(ctx)
+ {
+ fz_close(chain);
+ fz_rethrow(ctx);
+ }
return fz_new_stream(ctx, state, read_null, close_null);
}
@@ -145,12 +152,21 @@ fz_stream *
fz_open_ahxd(fz_stream *chain)
{
fz_ahxd *state;
+ fz_context *ctx = chain->ctx;
- state = fz_malloc_struct(chain->ctx, fz_ahxd);
- state->chain = chain;
- state->eod = 0;
+ fz_try(ctx)
+ {
+ state = fz_malloc_struct(ctx, fz_ahxd);
+ state->chain = chain;
+ state->eod = 0;
+ }
+ fz_catch(ctx)
+ {
+ fz_close(chain);
+ fz_rethrow(ctx);
+ }
- return fz_new_stream(chain->ctx, state, read_ahxd, close_ahxd);
+ return fz_new_stream(ctx, state, read_ahxd, close_ahxd);
}
/* ASCII 85 Decode */
@@ -282,15 +298,23 @@ fz_stream *
fz_open_a85d(fz_stream *chain)
{
fz_a85d *state;
+ fz_context *ctx = chain->ctx;
- assert(chain);
- state = fz_malloc_struct(chain->ctx, fz_a85d);
- state->chain = chain;
- state->rp = state->bp;
- state->wp = state->bp;
- state->eod = 0;
+ fz_try(ctx)
+ {
+ state = fz_malloc_struct(ctx, fz_a85d);
+ state->chain = chain;
+ state->rp = state->bp;
+ state->wp = state->bp;
+ state->eod = 0;
+ }
+ fz_catch(ctx)
+ {
+ fz_close(chain);
+ fz_rethrow(ctx);
+ }
- return fz_new_stream(chain->ctx, state, read_a85d, close_a85d);
+ return fz_new_stream(ctx, state, read_a85d, close_a85d);
}
/* Run Length Decode */
@@ -370,15 +394,23 @@ fz_stream *
fz_open_rld(fz_stream *chain)
{
fz_rld *state;
+ fz_context *ctx = chain->ctx;
- assert(chain);
- state = fz_malloc_struct(chain->ctx, fz_rld);
- state->chain = chain;
- state->run = 0;
- state->n = 0;
- state->c = 0;
+ fz_try(ctx)
+ {
+ state = fz_malloc_struct(ctx, fz_rld);
+ state->chain = chain;
+ state->run = 0;
+ state->n = 0;
+ state->c = 0;
+ }
+ fz_catch(ctx)
+ {
+ fz_close(chain);
+ fz_rethrow(ctx);
+ }
- return fz_new_stream(chain->ctx, state, read_rld, close_rld);
+ return fz_new_stream(ctx, state, read_rld, close_rld);
}
/* RC4 Filter */
@@ -414,12 +446,21 @@ fz_stream *
fz_open_arc4(fz_stream *chain, unsigned char *key, unsigned keylen)
{
fz_arc4c *state;
+ fz_context *ctx = chain->ctx;
- state = fz_malloc_struct(chain->ctx, fz_arc4c);
- state->chain = chain;
- fz_arc4_init(&state->arc4, key, keylen);
+ fz_try(ctx)
+ {
+ state = fz_malloc_struct(ctx, fz_arc4c);
+ state->chain = chain;
+ fz_arc4_init(&state->arc4, key, keylen);
+ }
+ fz_catch(ctx)
+ {
+ fz_close(chain);
+ fz_rethrow(ctx);
+ }
- return fz_new_stream(chain->ctx, state, read_arc4, close_arc4);
+ return fz_new_stream(ctx, state, read_arc4, close_arc4);
}
/* AES Filter */
@@ -496,14 +537,22 @@ fz_stream *
fz_open_aesd(fz_stream *chain, unsigned char *key, unsigned keylen)
{
fz_aesd *state;
+ fz_context *ctx = chain->ctx;
- assert(chain);
- state = fz_malloc_struct(chain->ctx, fz_aesd);
- state->chain = chain;
- aes_setkey_dec(&state->aes, key, keylen * 8);
- state->ivcount = 0;
- state->rp = state->bp;
- state->wp = state->bp;
+ fz_try(ctx)
+ {
+ state = fz_malloc_struct(ctx, fz_aesd);
+ state->chain = chain;
+ aes_setkey_dec(&state->aes, key, keylen * 8);
+ state->ivcount = 0;
+ state->rp = state->bp;
+ state->wp = state->bp;
+ }
+ fz_catch(ctx)
+ {
+ fz_close(chain);
+ fz_rethrow(ctx);
+ }
- return fz_new_stream(chain->ctx, state, read_aesd, close_aesd);
+ return fz_new_stream(ctx, state, read_aesd, close_aesd);
}
diff --git a/fitz/filt_dctd.c b/fitz/filt_dctd.c
index bae91195..3209d2ce 100644
--- a/fitz/filt_dctd.c
+++ b/fitz/filt_dctd.c
@@ -207,19 +207,31 @@ skip:
fz_stream *
fz_open_dctd(fz_stream *chain, fz_obj *params)
{
- fz_dctd *state;
+ fz_dctd *state = NULL;
fz_obj *obj;
+ fz_context *ctx = chain->ctx;
- state = fz_malloc_struct(chain->ctx, fz_dctd);
- memset(state, 0, sizeof(fz_dctd));
- state->ctx = chain->ctx;
- state->chain = chain;
- state->color_transform = -1; /* unset */
- state->init = 0;
+ fz_var(state);
- obj = fz_dict_gets(params, "ColorTransform");
- if (obj)
- state->color_transform = fz_to_int(obj);
+ fz_try(ctx)
+ {
+ state = fz_malloc_struct(chain->ctx, fz_dctd);
+ memset(state, 0, sizeof(fz_dctd));
+ state->ctx = ctx;
+ state->chain = chain;
+ state->color_transform = -1; /* unset */
+ state->init = 0;
+
+ obj = fz_dict_gets(params, "ColorTransform");
+ if (obj)
+ state->color_transform = fz_to_int(obj);
+ }
+ fz_catch(ctx)
+ {
+ fz_free(ctx, state);
+ fz_close(chain);
+ fz_rethrow(ctx);
+ }
- return fz_new_stream(chain->ctx, state, read_dctd, close_dctd);
+ return fz_new_stream(ctx, state, read_dctd, close_dctd);
}
diff --git a/fitz/filt_faxd.c b/fitz/filt_faxd.c
index 309844bf..0a2426d0 100644
--- a/fitz/filt_faxd.c
+++ b/fitz/filt_faxd.c
@@ -662,28 +662,28 @@ close_faxd(fz_context *ctx, void *state_)
fz_stream *
fz_open_faxd(fz_stream *chain, fz_obj *params)
{
- fz_faxd *fax;
+ fz_faxd *fax = NULL;
fz_obj *obj;
- fz_context *ctx;
-
- assert(chain);
- ctx = chain->ctx;
- fax = fz_malloc_struct(ctx, fz_faxd);
- fax->chain = chain;
+ fz_context *ctx = chain->ctx;
- fax->ref = NULL;
- fax->dst = NULL;
-
- fax->k = 0;
- fax->end_of_line = 0;
- fax->encoded_byte_align = 0;
- fax->columns = 1728;
- fax->rows = 0;
- fax->end_of_block = 1;
- fax->black_is_1 = 0;
+ fz_var(fax);
fz_try(ctx)
{
+ fax = fz_malloc_struct(ctx, fz_faxd);
+ fax->chain = chain;
+
+ fax->ref = NULL;
+ fax->dst = NULL;
+
+ fax->k = 0;
+ fax->end_of_line = 0;
+ fax->encoded_byte_align = 0;
+ fax->columns = 1728;
+ fax->rows = 0;
+ fax->end_of_block = 1;
+ fax->black_is_1 = 0;
+
obj = fz_dict_gets(params, "K");
if (obj) fax->k = fz_to_int(obj);
@@ -726,9 +726,13 @@ fz_open_faxd(fz_stream *chain, fz_obj *params)
}
fz_catch(ctx)
{
- fz_free(ctx, fax->dst);
- fz_free(ctx, fax->ref);
+ if (fax)
+ {
+ fz_free(ctx, fax->dst);
+ fz_free(ctx, fax->ref);
+ }
fz_free(ctx, fax);
+ fz_close(chain);
fz_rethrow(ctx);
}
diff --git a/fitz/filt_flate.c b/fitz/filt_flate.c
index d83228a1..2eb0c563 100644
--- a/fitz/filt_flate.c
+++ b/fitz/filt_flate.c
@@ -83,33 +83,35 @@ close_flated(fz_context *ctx, void *state_)
fz_stream *
fz_open_flated(fz_stream *chain)
{
- fz_flate *state;
+ fz_flate *state = NULL;
int code = Z_OK;
fz_context *ctx = chain->ctx;
fz_var(code);
-
- state = fz_malloc_struct(ctx, fz_flate);
- state->chain = chain;
-
- state->z.zalloc = zalloc;
- state->z.zfree = zfree;
- state->z.opaque = ctx;
- state->z.next_in = NULL;
- state->z.avail_in = 0;
+ fz_var(state);
fz_try(ctx)
{
+ state = fz_malloc_struct(ctx, fz_flate);
+ state->chain = chain;
+
+ state->z.zalloc = zalloc;
+ state->z.zfree = zfree;
+ state->z.opaque = ctx;
+ state->z.next_in = NULL;
+ state->z.avail_in = 0;
+
code = inflateInit(&state->z);
if (code != Z_OK)
fz_throw(ctx, "zlib error: inflateInit: %s", state->z.msg);
}
fz_catch(ctx)
{
- if (code == Z_OK)
+ if (state && code == Z_OK)
inflateEnd(&state->z);
fz_free(ctx, state);
+ fz_close(chain);
fz_rethrow(ctx);
}
- return fz_new_stream(chain->ctx, state, read_flated, close_flated);
+ return fz_new_stream(ctx, state, read_flated, close_flated);
}
diff --git a/fitz/filt_jbig2d.c b/fitz/filt_jbig2d.c
index 69e3df50..2d2e2eb9 100644
--- a/fitz/filt_jbig2d.c
+++ b/fitz/filt_jbig2d.c
@@ -81,21 +81,41 @@ read_jbig2d(fz_stream *stm, unsigned char *buf, int len)
fz_stream *
fz_open_jbig2d(fz_stream *chain, fz_buffer *globals)
{
- fz_jbig2d *state;
+ fz_jbig2d *state = NULL;
+ fz_context *ctx = chain->ctx;
- state = fz_malloc_struct(chain->ctx, fz_jbig2d);
- state->chain = chain;
- state->ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, NULL, NULL, NULL);
- state->gctx = NULL;
- state->page = NULL;
- state->idx = 0;
+ fz_var(state);
- if (globals)
+ fz_try(ctx)
{
- jbig2_data_in(state->ctx, globals->data, globals->len);
- state->gctx = jbig2_make_global_ctx(state->ctx);
- state->ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, state->gctx, NULL, NULL);
+ state = fz_malloc_struct(chain->ctx, fz_jbig2d);
+ state->ctx = NULL;
+ state->gctx = NULL;
+ state->chain = chain;
+ state->ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, NULL, NULL, NULL);
+ state->page = NULL;
+ state->idx = 0;
+
+ if (globals)
+ {
+ jbig2_data_in(state->ctx, globals->data, globals->len);
+ state->gctx = jbig2_make_global_ctx(state->ctx);
+ state->ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, state->gctx, NULL, NULL);
+ }
+ }
+ fz_catch(ctx)
+ {
+ if (state)
+ {
+ if (state->gctx)
+ jbig2_global_ctx_free(state->gctx);
+ if (state->ctx)
+ jbig2_ctx_free(state->ctx);
+ }
+ fz_free(ctx, state);
+ fz_close(chain);
+ fz_rethrow(ctx);
}
- return fz_new_stream(chain->ctx, state, read_jbig2d, close_jbig2d);
+ return fz_new_stream(ctx, state, read_jbig2d, close_jbig2d);
}
diff --git a/fitz/filt_lzwd.c b/fitz/filt_lzwd.c
index 1ac588c4..04aec9c4 100644
--- a/fitz/filt_lzwd.c
+++ b/fitz/filt_lzwd.c
@@ -166,42 +166,53 @@ close_lzwd(fz_context *ctx, void *state_)
fz_stream *
fz_open_lzwd(fz_stream *chain, fz_obj *params)
{
- fz_lzwd *lzw;
+ fz_lzwd *lzw = NULL;
fz_obj *obj;
int i;
+ fz_context *ctx = chain->ctx;
- assert(chain);
- lzw = fz_malloc_struct(chain->ctx, fz_lzwd);
- lzw->chain = chain;
- lzw->eod = 0;
- lzw->early_change = 1;
+ fz_var(lzw);
- obj = fz_dict_gets(params, "EarlyChange");
- if (obj)
- lzw->early_change = !!fz_to_int(obj);
-
- for (i = 0; i < 256; i++)
+ fz_try(ctx)
{
- lzw->table[i].value = i;
- lzw->table[i].first_char = i;
- lzw->table[i].length = 1;
- lzw->table[i].prev = -1;
- }
+ lzw = fz_malloc_struct(ctx, fz_lzwd);
+ lzw->chain = chain;
+ lzw->eod = 0;
+ lzw->early_change = 1;
+
+ obj = fz_dict_gets(params, "EarlyChange");
+ if (obj)
+ lzw->early_change = !!fz_to_int(obj);
+
+ for (i = 0; i < 256; i++)
+ {
+ lzw->table[i].value = i;
+ lzw->table[i].first_char = i;
+ lzw->table[i].length = 1;
+ lzw->table[i].prev = -1;
+ }
- for (i = 256; i < NUM_CODES; i++)
+ for (i = 256; i < NUM_CODES; i++)
+ {
+ lzw->table[i].value = 0;
+ lzw->table[i].first_char = 0;
+ lzw->table[i].length = 0;
+ lzw->table[i].prev = -1;
+ }
+
+ lzw->code_bits = MIN_BITS;
+ lzw->code = -1;
+ lzw->next_code = LZW_FIRST;
+ lzw->old_code = -1;
+ lzw->rp = lzw->bp;
+ lzw->wp = lzw->bp;
+ }
+ fz_catch(ctx)
{
- lzw->table[i].value = 0;
- lzw->table[i].first_char = 0;
- lzw->table[i].length = 0;
- lzw->table[i].prev = -1;
+ fz_free(ctx, lzw);
+ fz_close(chain);
+ fz_rethrow(ctx);
}
- lzw->code_bits = MIN_BITS;
- lzw->code = -1;
- lzw->next_code = LZW_FIRST;
- lzw->old_code = -1;
- lzw->rp = lzw->bp;
- lzw->wp = lzw->bp;
-
- return fz_new_stream(chain->ctx, lzw, read_lzwd, close_lzwd);
+ return fz_new_stream(ctx, lzw, read_lzwd, close_lzwd);
}
diff --git a/fitz/filt_predict.c b/fitz/filt_predict.c
index ab4a2eaa..30393e54 100644
--- a/fitz/filt_predict.c
+++ b/fitz/filt_predict.c
@@ -190,53 +190,71 @@ close_predict(fz_context *ctx, void *state_)
fz_stream *
fz_open_predict(fz_stream *chain, fz_obj *params)
{
- fz_predict *state;
+ fz_predict *state = NULL;
fz_obj *obj;
fz_context *ctx = chain->ctx;
- state = fz_malloc_struct(ctx, fz_predict);
- state->chain = chain;
+ fz_var(state);
- state->predictor = 1;
- state->columns = 1;
- state->colors = 1;
- state->bpc = 8;
-
- obj = fz_dict_gets(params, "Predictor");
- if (obj)
- state->predictor = fz_to_int(obj);
-
- if (state->predictor != 1 && state->predictor != 2 &&
- state->predictor != 10 && state->predictor != 11 &&
- state->predictor != 12 && state->predictor != 13 &&
- state->predictor != 14 && state->predictor != 15)
+ fz_try(ctx)
{
- fz_warn(ctx, "invalid predictor: %d", state->predictor);
+ state = fz_malloc_struct(ctx, fz_predict);
+ state->in = NULL;
+ state->out = NULL;
+ state->chain = chain;
+
state->predictor = 1;
- }
+ state->columns = 1;
+ state->colors = 1;
+ state->bpc = 8;
+
+ obj = fz_dict_gets(params, "Predictor");
+ if (obj)
+ state->predictor = fz_to_int(obj);
+
+ if (state->predictor != 1 && state->predictor != 2 &&
+ state->predictor != 10 && state->predictor != 11 &&
+ state->predictor != 12 && state->predictor != 13 &&
+ state->predictor != 14 && state->predictor != 15)
+ {
+ fz_warn(ctx, "invalid predictor: %d", state->predictor);
+ state->predictor = 1;
+ }
- obj = fz_dict_gets(params, "Columns");
- if (obj)
- state->columns = fz_to_int(obj);
+ obj = fz_dict_gets(params, "Columns");
+ if (obj)
+ state->columns = fz_to_int(obj);
- obj = fz_dict_gets(params, "Colors");
- if (obj)
- state->colors = fz_to_int(obj);
+ obj = fz_dict_gets(params, "Colors");
+ if (obj)
+ state->colors = fz_to_int(obj);
- obj = fz_dict_gets(params, "BitsPerComponent");
- if (obj)
- state->bpc = fz_to_int(obj);
+ obj = fz_dict_gets(params, "BitsPerComponent");
+ if (obj)
+ state->bpc = fz_to_int(obj);
- state->stride = (state->bpc * state->colors * state->columns + 7) / 8;
- state->bpp = (state->bpc * state->colors + 7) / 8;
+ state->stride = (state->bpc * state->colors * state->columns + 7) / 8;
+ state->bpp = (state->bpc * state->colors + 7) / 8;
- state->in = fz_malloc(ctx, state->stride + 1);
- state->out = fz_malloc(ctx, state->stride);
- state->ref = fz_malloc(ctx, state->stride);
- state->rp = state->out;
- state->wp = state->out;
+ state->in = fz_malloc(ctx, state->stride + 1);
+ state->out = fz_malloc(ctx, state->stride);
+ state->ref = fz_malloc(ctx, state->stride);
+ state->rp = state->out;
+ state->wp = state->out;
- memset(state->ref, 0, state->stride);
+ memset(state->ref, 0, state->stride);
+ }
+ fz_catch(ctx)
+ {
+ if (state)
+ {
+ fz_free(ctx, state->in);
+ fz_free(ctx, state->out);
+ }
+ fz_free(ctx, state);
+ fz_close(chain);
+ fz_rethrow(ctx);
+ }
return fz_new_stream(ctx, state, read_predict, close_predict);
}