diff options
-rw-r--r-- | apps/common/pdfapp.c | 21 | ||||
-rw-r--r-- | apps/common/pdftool.c | 7 | ||||
-rw-r--r-- | apps/pdfclean.c | 8 | ||||
-rw-r--r-- | apps/pdfdraw.c | 4 | ||||
-rw-r--r-- | apps/pdfextract.c | 10 | ||||
-rw-r--r-- | apps/pdfinfo.c | 4 | ||||
-rw-r--r-- | fitz/base_hash.c | 80 | ||||
-rw-r--r-- | fitz/base_memory.c | 37 | ||||
-rw-r--r-- | fitz/fitz_base.h | 8 | ||||
-rw-r--r-- | fitz/fitz_stream.h | 44 | ||||
-rw-r--r-- | fitz/obj_array.c | 185 | ||||
-rw-r--r-- | fitz/obj_dict.c | 220 | ||||
-rw-r--r-- | fitz/obj_parse.c | 135 | ||||
-rw-r--r-- | fitz/obj_simple.c | 77 | ||||
-rw-r--r-- | mupdf/mupdf.h | 28 | ||||
-rw-r--r-- | mupdf/pdf_annot.c | 54 | ||||
-rw-r--r-- | mupdf/pdf_cmap_load.c | 7 | ||||
-rw-r--r-- | mupdf/pdf_colorspace1.c | 4 | ||||
-rw-r--r-- | mupdf/pdf_font.c | 4 | ||||
-rw-r--r-- | mupdf/pdf_function.c | 7 | ||||
-rw-r--r-- | mupdf/pdf_image.c | 7 | ||||
-rw-r--r-- | mupdf/pdf_interpret.c | 36 | ||||
-rw-r--r-- | mupdf/pdf_outline.c | 36 | ||||
-rw-r--r-- | mupdf/pdf_page.c | 12 | ||||
-rw-r--r-- | mupdf/pdf_pagetree.c | 124 | ||||
-rw-r--r-- | mupdf/pdf_parse.c | 370 | ||||
-rw-r--r-- | mupdf/pdf_pattern.c | 7 | ||||
-rw-r--r-- | mupdf/pdf_repair.c | 20 | ||||
-rw-r--r-- | mupdf/pdf_shade.c | 7 | ||||
-rw-r--r-- | mupdf/pdf_store.c | 51 | ||||
-rw-r--r-- | mupdf/pdf_xobject.c | 7 | ||||
-rw-r--r-- | mupdf/pdf_xref.c | 9 |
32 files changed, 580 insertions, 1050 deletions
diff --git a/apps/common/pdfapp.c b/apps/common/pdfapp.c index 5aeb699c..7af22d04 100644 --- a/apps/common/pdfapp.c +++ b/apps/common/pdfapp.c @@ -131,9 +131,7 @@ void pdfapp_open(pdfapp_t *app, char *filename) if (app->xref->info) fz_keepobj(app->xref->info); - error = pdf_loadoutline(&app->outline, app->xref); - if (error) - pdfapp_error(app, error); + app->outline = pdf_loadoutline(app->xref); app->doctitle = filename; if (strrchr(app->doctitle, '\\')) @@ -145,9 +143,7 @@ void pdfapp_open(pdfapp_t *app, char *filename) obj = fz_dictgets(app->xref->info, "Title"); if (obj) { - error = pdf_toutf8(&app->doctitle, obj); - if (error) - pdfapp_error(app, error); + app->doctitle = pdf_toutf8(obj); } } @@ -155,9 +151,7 @@ void pdfapp_open(pdfapp_t *app, char *filename) * Start at first page */ - error = pdf_getpagecount(app->xref, &app->pagecount); - if (error) - pdfapp_error(app, error); + app->pagecount = pdf_getpagecount(app->xref); app->shrinkwrap = 1; if (app->pageno < 1) @@ -245,10 +239,7 @@ static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage) pdf_droppage(app->page); app->page = nil; - error = pdf_getpageobject(app->xref, app->pageno, &obj); - if (error) - pdfapp_error(app, error); - + obj = pdf_getpageobject(app->xref, app->pageno); error = pdf_loadpage(&app->page, app->xref, obj); if (error) pdfapp_error(app, error); @@ -314,9 +305,7 @@ static void pdfapp_gotopage(pdfapp_t *app, fz_obj *obj) fz_error error; int page; - error = pdf_findpageobject(app->xref, obj, &page); - if (error) - pdfapp_error(app, error); + page = pdf_findpageobject(app->xref, obj); if (app->histlen + 1 == 256) { diff --git a/apps/common/pdftool.c b/apps/common/pdftool.c index 5961e3fb..772fc9a0 100644 --- a/apps/common/pdftool.c +++ b/apps/common/pdftool.c @@ -69,12 +69,7 @@ void openxref(char *filename, char *password, int dieonbadpass) if (xref->info) fz_keepobj(xref->info); - error = pdf_getpagecount(xref, &pagecount); - if (error) - { - fz_catch(error, "cannot determine page count, attempting to continue."); - pagecount = 0; - } + pagecount = pdf_getpagecount(xref); } void closexref(void) diff --git a/apps/pdfclean.c b/apps/pdfclean.c index 7db29e0c..a558d17d 100644 --- a/apps/pdfclean.c +++ b/apps/pdfclean.c @@ -142,11 +142,11 @@ static void expandstream(fz_obj *obj, int oid, int gen) if (error) die(error); - fz_copydict(&newdict, obj); + newdict = fz_copydict(obj); fz_dictdels(newdict, "Filter"); fz_dictdels(newdict, "DecodeParms"); - fz_newint(&newlen, buf->wp - buf->rp); + newlen = fz_newint(buf->wp - buf->rp); fz_dictputs(newdict, "Length", newlen); fz_dropobj(newlen); @@ -227,9 +227,9 @@ static void savexref(void) } fprintf(out, "\n"); - fz_newdict(&trailer, 5); + trailer = fz_newdict(5); - fz_newint(&obj, xref->len); + obj = fz_newint(xref->len); fz_dictputs(trailer, "Size", obj); fz_dropobj(obj); diff --git a/apps/pdfdraw.c b/apps/pdfdraw.c index 4cc3bd51..22122e73 100644 --- a/apps/pdfdraw.c +++ b/apps/pdfdraw.c @@ -93,9 +93,7 @@ static void drawloadpage(int pagenum, struct benchmark *loadtimes) gettime(&start); } - error = pdf_getpageobject(xref, pagenum, &pageobj); - if (error) - die(error); + pageobj = pdf_getpageobject(xref, pagenum); error = pdf_loadpage(&drawpage, xref, pageobj); if (error) die(error); diff --git a/apps/pdfextract.c b/apps/pdfextract.c index 2bead4c5..4f4fb24a 100644 --- a/apps/pdfextract.c +++ b/apps/pdfextract.c @@ -38,14 +38,10 @@ static void saveimage(fz_obj *obj, int num, int gen) int x; int y; - error = fz_newindirect(&ref, num, gen, xref); - if (error) - die(error); - - error = pdf_newstore(&xref->store); - if (error) - die(error); + ref = fz_newindirect(num, gen, xref); + xref->store = pdf_newstore(); + error = pdf_loadimage(&img, xref, ref); if (error) die(error); diff --git a/apps/pdfinfo.c b/apps/pdfinfo.c index 36325f10..5c9bd927 100644 --- a/apps/pdfinfo.c +++ b/apps/pdfinfo.c @@ -673,9 +673,7 @@ gatherinfo(int show, int page) fz_obj *shade; fz_obj *pattern; - error = pdf_getpageobject(xref, page, &pageobj); - if (error) - die(error); + pageobj = pdf_getpageobject(xref, page); if (!pageobj) die(fz_throw("cannot retrieve info from page %d", page)); diff --git a/fitz/base_hash.c b/fitz/base_hash.c index 1b817209..cd866041 100644 --- a/fitz/base_hash.c +++ b/fitz/base_hash.c @@ -45,32 +45,21 @@ static unsigned hash(unsigned char *s, int len) return hash; } -fz_error -fz_newhash(fz_hashtable **tablep, int initialsize, int keylen) +fz_hashtable * +fz_newhash(int initialsize, int keylen) { fz_hashtable *table; assert(keylen <= MAXKEYLEN); - table = *tablep = fz_malloc(sizeof(fz_hashtable)); - if (!table) - return fz_rethrow(-1, "out of memory: hash table struct"); - + table = fz_malloc(sizeof(fz_hashtable)); table->keylen = keylen; table->size = initialsize; table->load = 0; - table->ents = fz_malloc(sizeof(fz_hashentry) * table->size); - if (!table->ents) - { - fz_free(table); - *tablep = nil; - return fz_rethrow(-1, "out of memory: hash table entries (size=%d)", initialsize); - } - memset(table->ents, 0, sizeof(fz_hashentry) * table->size); - return fz_okay; + return table; } void @@ -105,51 +94,34 @@ fz_drophash(fz_hashtable *table) fz_free(table); } -fz_error +void fz_resizehash(fz_hashtable *table, int newsize) { - fz_error error; - fz_hashentry *newents; - fz_hashentry *oldents; - int oldload; - int oldsize; + fz_hashentry *oldents = table->ents; + int oldsize = table->size; + int oldload = table->load; int i; - oldsize = table->size; - oldload = table->load; - oldents = table->ents; - if (newsize < oldload * 8 / 10) - return fz_throw("assert: resize hash too small"); - - newents = fz_malloc(sizeof(fz_hashentry) * newsize); - if (!newents) - return fz_rethrow(-1, "out of memory: hash table (size=%d)", newsize); + { + fz_throw("assert: resize hash too small"); + return; + } + table->ents = fz_malloc(sizeof(fz_hashentry) * newsize); + memset(table->ents, 0, sizeof(fz_hashentry) * table->size); table->size = newsize; table->load = 0; - table->ents = newents; - memset(table->ents, 0, sizeof(fz_hashentry) * table->size); for (i = 0; i < oldsize; i++) { if (oldents[i].val) { - error = fz_hashinsert(table, oldents[i].key, oldents[i].val); - if (error) - { - table->size = oldsize; - table->load = oldload; - table->ents = oldents; - fz_free(newents); - return fz_rethrow(error, "cannot re-insert old entries"); - } + fz_hashinsert(table, oldents[i].key, oldents[i].val); } } fz_free(oldents); - - return fz_okay; } void * @@ -171,19 +143,16 @@ fz_hashfind(fz_hashtable *table, void *key) } } -fz_error +void fz_hashinsert(fz_hashtable *table, void *key, void *val) { - fz_error error; fz_hashentry *ents; unsigned size; unsigned pos; if (table->load > table->size * 8 / 10) { - error = fz_resizehash(table, table->size * 2); - if (error) - return fz_rethrow(error, "cannot resize hash table"); + fz_resizehash(table, table->size * 2); } ents = table->ents; @@ -197,19 +166,17 @@ fz_hashinsert(fz_hashtable *table, void *key, void *val) memcpy(ents[pos].key, key, table->keylen); ents[pos].val = val; table->load ++; - return fz_okay; + return; } if (memcmp(key, &ents[pos].key, table->keylen) == 0) - return fz_throw("assert: overwrite hash slot"); + fz_warn("assert: overwrite hash slot"); pos = (pos + 1) % size; } - - return fz_okay; } -fz_error +void fz_hashremove(fz_hashtable *table, void *key) { fz_hashentry *ents = table->ents; @@ -220,7 +187,10 @@ fz_hashremove(fz_hashtable *table, void *key) while (1) { if (!ents[pos].val) - return fz_throw("assert: remove inexistant hash entry"); + { + fz_warn("assert: remove inexistant hash entry"); + return; + } if (memcmp(key, &ents[pos].key, table->keylen) == 0) { @@ -246,7 +216,7 @@ fz_hashremove(fz_hashtable *table, void *key) table->load --; - return fz_okay; + return; } pos = (pos + 1) % size; diff --git a/fitz/base_memory.c b/fitz/base_memory.c index 1f224ddb..d7b81fc4 100644 --- a/fitz/base_memory.c +++ b/fitz/base_memory.c @@ -2,31 +2,40 @@ void * fz_malloc(int n) { - void *p = malloc(n); - if (!p) - fz_throw("cannot malloc %d bytes", n); - return p; + void *p = malloc(n); + if (!p) + { + fprintf(stderr, "fatal error: out of memory\n"); + abort(); + } + return p; } void * fz_realloc(void *p, int n) { - void *np = realloc(p, n); - if (np == nil) - fz_throw("cannot realloc %d bytes", n); - return np; + void *np = realloc(p, n); + if (np == nil) + { + fprintf(stderr, "fatal error: out of memory\n"); + abort(); + } + return np; } void fz_free(void *p) { - free(p); + free(p); } char * fz_strdup(char *s) { - char *ns = malloc(strlen(s) + 1); - if (!ns) - fz_throw("cannot strdup %lu bytes", (unsigned long)strlen(s) + 1); - memcpy(ns, s, strlen(s) + 1); - return ns; + char *ns = malloc(strlen(s) + 1); + if (!ns) + { + fprintf(stderr, "fatal error: out of memory\n"); + abort(); + } + memcpy(ns, s, strlen(s) + 1); + return ns; } diff --git a/fitz/fitz_base.h b/fitz/fitz_base.h index 6983d0e5..e5dce5f9 100644 --- a/fitz/fitz_base.h +++ b/fitz/fitz_base.h @@ -191,15 +191,15 @@ char *fz_strdup(char *s); typedef struct fz_hashtable_s fz_hashtable; -fz_error fz_newhash(fz_hashtable **tablep, int initialsize, int keylen); -fz_error fz_resizehash(fz_hashtable *table, int newsize); +fz_hashtable * fz_newhash(int initialsize, int keylen); +void fz_resizehash(fz_hashtable *table, int newsize); void fz_debughash(fz_hashtable *table); void fz_emptyhash(fz_hashtable *table); void fz_drophash(fz_hashtable *table); void *fz_hashfind(fz_hashtable *table, void *key); -fz_error fz_hashinsert(fz_hashtable *table, void *key, void *val); -fz_error fz_hashremove(fz_hashtable *table, void *key); +void fz_hashinsert(fz_hashtable *table, void *key, void *val); +void fz_hashremove(fz_hashtable *table, void *key); int fz_hashlen(fz_hashtable *table); void *fz_hashgetkey(fz_hashtable *table, int idx); diff --git a/fitz/fitz_stream.h b/fitz/fitz_stream.h index 30036f47..fc8eb023 100644 --- a/fitz/fitz_stream.h +++ b/fitz/fitz_stream.h @@ -62,20 +62,18 @@ struct fz_obj_s } u; }; -fz_error fz_newnull(fz_obj **op); -fz_error fz_newbool(fz_obj **op, int b); -fz_error fz_newint(fz_obj **op, int i); -fz_error fz_newreal(fz_obj **op, float f); -fz_error fz_newname(fz_obj **op, char *str); -fz_error fz_newstring(fz_obj **op, char *str, int len); -fz_error fz_newindirect(fz_obj **op, int num, int gen, pdf_xref *xref); - -fz_error fz_newarray(fz_obj **op, int initialcap); -fz_error fz_newdict(fz_obj **op, int initialcap); -fz_error fz_copyarray(fz_obj **op, fz_obj *array); -fz_error fz_copydict(fz_obj **op, fz_obj *dict); -fz_error fz_deepcopyarray(fz_obj **op, fz_obj *array); -fz_error fz_deepcopydict(fz_obj **op, fz_obj *dict); +fz_obj * fz_newnull(void); +fz_obj * fz_newbool(int b); +fz_obj * fz_newint(int i); +fz_obj * fz_newreal(float f); +fz_obj * fz_newname(char *str); +fz_obj * fz_newstring(char *str, int len); +fz_obj * fz_newindirect(int num, int gen, pdf_xref *xref); + +fz_obj * fz_newarray(int initialcap); +fz_obj * fz_newdict(int initialcap); +fz_obj * fz_copyarray(fz_obj *array); +fz_obj * fz_copydict(fz_obj *dict); fz_obj *fz_keepobj(fz_obj *obj); void fz_dropobj(fz_obj *obj); @@ -105,12 +103,12 @@ int fz_tostrlen(fz_obj *obj); int fz_tonum(fz_obj *obj); int fz_togen(fz_obj *obj); -fz_error fz_newnamefromstring(fz_obj **op, fz_obj *str); +fz_obj * fz_newnamefromstring(fz_obj *str); int fz_arraylen(fz_obj *array); fz_obj *fz_arrayget(fz_obj *array, int i); -fz_error fz_arrayput(fz_obj *array, int i, fz_obj *obj); -fz_error fz_arraypush(fz_obj *array, fz_obj *obj); +void fz_arrayput(fz_obj *array, int i, fz_obj *obj); +void fz_arraypush(fz_obj *array, fz_obj *obj); int fz_dictlen(fz_obj *dict); fz_obj *fz_dictgetkey(fz_obj *dict, int idx); @@ -118,18 +116,18 @@ fz_obj *fz_dictgetval(fz_obj *dict, int idx); fz_obj *fz_dictget(fz_obj *dict, fz_obj *key); fz_obj *fz_dictgets(fz_obj *dict, char *key); fz_obj *fz_dictgetsa(fz_obj *dict, char *key, char *abbrev); -fz_error fz_dictput(fz_obj *dict, fz_obj *key, fz_obj *val); -fz_error fz_dictputs(fz_obj *dict, char *key, fz_obj *val); -fz_error fz_dictdel(fz_obj *dict, fz_obj *key); -fz_error fz_dictdels(fz_obj *dict, char *key); +void fz_dictput(fz_obj *dict, fz_obj *key, fz_obj *val); +void fz_dictputs(fz_obj *dict, char *key, fz_obj *val); +void fz_dictdel(fz_obj *dict, fz_obj *key); +void fz_dictdels(fz_obj *dict, char *key); void fz_sortdict(fz_obj *dict); int fz_sprintobj(char *s, int n, fz_obj *obj, int tight); int fz_fprintobj(FILE *fp, fz_obj *obj, int tight); void fz_debugobj(fz_obj *obj); -fz_error fz_parseobj(fz_obj **objp, pdf_xref *xref, char *s); -fz_error fz_packobj(fz_obj **objp, pdf_xref *xref, char *fmt, ...); +fz_error fz_parseobj(fz_obj **, pdf_xref *xref, char *s); +fz_error fz_packobj(fz_obj **, pdf_xref *xref, char *fmt, ...); char *fz_objkindstr(fz_obj *obj); diff --git a/fitz/obj_array.c b/fitz/obj_array.c index 64062042..ac1e1fa8 100644 --- a/fitz/obj_array.c +++ b/fitz/obj_array.c @@ -3,16 +3,13 @@ void fz_freearray(fz_obj *obj); -fz_error -fz_newarray(fz_obj **op, int initialcap) +fz_obj * +fz_newarray(int initialcap) { fz_obj *obj; int i; - obj = *op = fz_malloc(sizeof (fz_obj)); - if (!obj) - return fz_rethrow(-1, "out of memory: array struct"); - + obj = fz_malloc(sizeof (fz_obj)); obj->refs = 1; obj->kind = FZ_ARRAY; @@ -20,119 +17,29 @@ fz_newarray(fz_obj **op, int initialcap) obj->u.a.cap = initialcap > 0 ? initialcap : 6; obj->u.a.items = fz_malloc(sizeof (fz_obj*) * obj->u.a.cap); - if (!obj->u.a.items) - { - fz_free(obj); - return fz_rethrow(-1, "out of memory: array item buffer"); - } - for (i = 0; i < obj->u.a.cap; i++) obj->u.a.items[i] = nil; - return fz_okay; + return obj; } -fz_error -fz_copyarray(fz_obj **op, fz_obj *obj) -{ - fz_error error; - fz_obj *new; - int i; - - if (!fz_isarray(obj)) - return fz_throw("assert: not an array (%s)", fz_objkindstr(obj)); - - error = fz_newarray(&new, fz_arraylen(obj)); - if (error) - return fz_rethrow(error, "cannot create new array"); - - for (i = 0; i < fz_arraylen(obj); i++) - { - error = fz_arraypush(new, fz_arrayget(obj, i)); - if (error) - { - fz_freearray(new); - return fz_rethrow(error, "cannot add item to array"); - } - } - - *op = new; - - return fz_okay; -} - -fz_error -fz_deepcopyarray(fz_obj **op, fz_obj *obj) +fz_obj * +fz_copyarray(fz_obj *obj) { - fz_error error; fz_obj *new; - fz_obj *val; int i; if (!fz_isarray(obj)) - return fz_throw("assert: not an array (%s)", fz_objkindstr(obj)); + fz_warn("assert: not an array (%s)", fz_objkindstr(obj)); - error = fz_newarray(&new, fz_arraylen(obj)); - if (error) - return fz_rethrow(error, "cannot create new array"); + new = fz_newarray(fz_arraylen(obj)); for (i = 0; i < fz_arraylen(obj); i++) { - val = fz_arrayget(obj, i); - - if (fz_isarray(val)) - { - error = fz_deepcopyarray(&val, val); - if (error) - { - fz_freearray(new); - return fz_rethrow(error, "cannot deep copy item"); - } - - error = fz_arraypush(new, val); - if (error) - { - fz_dropobj(val); - fz_freearray(new); - return fz_rethrow(error, "cannot add copied item to array"); - } - - fz_dropobj(val); - } - - else if (fz_isdict(val)) - { - error = fz_deepcopydict(&val, val); - if (error) - { - fz_freearray(new); - return fz_rethrow(error, "cannot deep copy item"); - } - - error = fz_arraypush(new, val); - if (error) - { - fz_dropobj(val); - fz_freearray(new); - return fz_rethrow(error, "cannot add copied item to array"); - } - fz_dropobj(val); - } - - else - { - error = fz_arraypush(new, val); - if (error) - { - fz_freearray(new); - return fz_rethrow(error, "cannot add copied item to array"); - } - } + fz_arraypush(new, fz_arrayget(obj, i)); } - *op = new; - - return fz_okay; + return new; } int @@ -158,66 +65,46 @@ fz_arrayget(fz_obj *obj, int i) return obj->u.a.items[i]; } -fz_error +void fz_arrayput(fz_obj *obj, int i, fz_obj *item) { obj = fz_resolveindirect(obj); if (!fz_isarray(obj)) - return fz_throw("assert: not an array (%s)", fz_objkindstr(obj)); - if (i < 0) - return fz_throw("assert: index %d < 0", i); - if (i >= obj->u.a.len) - return fz_throw("assert: index %d > length %d", i, obj->u.a.len); - - if (obj->u.a.items[i]) - fz_dropobj(obj->u.a.items[i]); - obj->u.a.items[i] = fz_keepobj(item); - - return fz_okay; -} - -static fz_error -growarray(fz_obj *obj) -{ - fz_obj **newitems; - int newcap; - int i; - - newcap = obj->u.a.cap * 2; - newitems = fz_realloc(obj->u.a.items, sizeof (fz_obj*) * newcap); - if (!newitems) - return fz_rethrow(-1, "out of memory: resize item buffer"); - - obj->u.a.items = newitems; - for (i = obj->u.a.cap ; i < newcap; i++) - obj->u.a.items[i] = nil; - obj->u.a.cap = newcap; - - return fz_okay; + fz_warn("assert: not an array (%s)", fz_objkindstr(obj)); + else if (i < 0) + fz_warn("assert: index %d < 0", i); + else if (i >= obj->u.a.len) + fz_warn("assert: index %d > length %d", i, obj->u.a.len); + else + { + if (obj->u.a.items[i]) + fz_dropobj(obj->u.a.items[i]); + obj->u.a.items[i] = fz_keepobj(item); + } } -fz_error +void fz_arraypush(fz_obj *obj, fz_obj *item) { - fz_error error; - obj = fz_resolveindirect(obj); if (!fz_isarray(obj)) - return fz_throw("assert: not an array (%s)", fz_objkindstr(obj)); - - if (obj->u.a.len + 1 > obj->u.a.cap) + fz_warn("assert: not an array (%s)", fz_objkindstr(obj)); + else { - error = growarray(obj); - if (error) - return fz_rethrow(error, "cannot grow item buffer"); + if (obj->u.a.len + 1 > obj->u.a.cap) + { + int i; + int newcap = obj->u.a.cap * 2; + obj->u.a.items = fz_realloc(obj->u.a.items, sizeof (fz_obj*) * newcap); + for (i = obj->u.a.cap ; i < newcap; i++) + obj->u.a.items[i] = nil; + obj->u.a.cap = newcap; + } + obj->u.a.items[obj->u.a.len] = fz_keepobj(item); + obj->u.a.len++; } - - obj->u.a.items[obj->u.a.len] = fz_keepobj(item); - obj->u.a.len++; - - return fz_okay; } void diff --git a/fitz/obj_dict.c b/fitz/obj_dict.c index 2af2eca5..1742df92 100644 --- a/fitz/obj_dict.c +++ b/fitz/obj_dict.c @@ -19,16 +19,13 @@ static inline int keystrcmp(fz_obj *key, char *s) return -1; } -fz_error -fz_newdict(fz_obj **op, int initialcap) +fz_obj * +fz_newdict(int initialcap) { fz_obj *obj; int i; - obj = *op = fz_malloc(sizeof (fz_obj)); - if (!obj) - return fz_rethrow(-1, "out of memory: dict struct"); - + obj = fz_malloc(sizeof (fz_obj)); obj->refs = 1; obj->kind = FZ_DICT; @@ -37,141 +34,29 @@ fz_newdict(fz_obj **op, int initialcap) obj->u.d.cap = initialcap > 0 ? initialcap : 10; obj->u.d.items = fz_malloc(sizeof(fz_keyval) * obj->u.d.cap); - if (!obj->u.d.items) - { - fz_free(obj); - return fz_rethrow(-1, "out of memory: dict item buffer"); - } - for (i = 0; i < obj->u.d.cap; i++) { obj->u.d.items[i].k = nil; obj->u.d.items[i].v = nil; } - return fz_okay; + return obj; } -fz_error -fz_copydict(fz_obj **op, fz_obj *obj) -{ - fz_error error; - fz_obj *new; - int i; - - if (!fz_isdict(obj)) - return fz_throw("assert: not a dict (%s)", fz_objkindstr(obj)); - - error = fz_newdict(&new, obj->u.d.cap); - if (error) - return fz_rethrow(error, "cannot create new dict"); - - for (i = 0; i < fz_dictlen(obj); i++) - { - error = fz_dictput(new, fz_dictgetkey(obj, i), fz_dictgetval(obj, i)); - if (error) - { - fz_dropobj(new); - return fz_rethrow(error, "cannot copy dict entry"); - } - } - - *op = new; - return fz_okay; -} - -fz_error -fz_deepcopydict(fz_obj **op, fz_obj *obj) +fz_obj * +fz_copydict(fz_obj *obj) { - fz_error error; fz_obj *new; - fz_obj *val; int i; if (!fz_isdict(obj)) - return fz_throw("assert: not a dict (%s)", fz_objkindstr(obj)); - - error = fz_newdict(&new, obj->u.d.cap); - if (error) - return fz_rethrow(error, "cannot create new dict"); + fz_throw("assert: not a dict (%s)", fz_objkindstr(obj)); + new = fz_newdict(obj->u.d.cap); for (i = 0; i < fz_dictlen(obj); i++) - { - val = fz_dictgetval(obj, i); - - if (fz_isarray(val)) - { - error = fz_deepcopyarray(&val, val); - if (error) - { - fz_dropobj(new); - return fz_rethrow(error, "cannot deep copy item"); - } - error = fz_dictput(new, fz_dictgetkey(obj, i), val); - if (error) - { - fz_dropobj(val); - fz_dropobj(new); - return fz_rethrow(error, "cannot add copied dict entry"); - } - fz_dropobj(val); - } - - else if (fz_isdict(val)) - { - error = fz_deepcopydict(&val, val); - if (error) - { - fz_dropobj(new); - return fz_rethrow(error, "cannot deep copy item"); - } - error = fz_dictput(new, fz_dictgetkey(obj, i), val); - if (error) - { - fz_dropobj(val); - fz_dropobj(new); - return fz_rethrow(error, "cannot add copied dict entry"); - } - fz_dropobj(val); - } - - else - { - error = fz_dictput(new, fz_dictgetkey(obj, i), val); - if (error) - { - fz_dropobj(new); - return fz_rethrow(error, "cannot copy dict entry"); - } - } - } - - *op = new; - return fz_okay; -} - -static fz_error -growdict(fz_obj *obj) -{ - fz_keyval *newitems; - int newcap; - int i; - - newcap = obj->u.d.cap * 2; - - newitems = fz_realloc(obj->u.d.items, sizeof(fz_keyval) * newcap); - if (!newitems) - return fz_rethrow(-1, "out of memory: resize item buffer"); - - obj->u.d.items = newitems; - for (i = obj->u.d.cap; i < newcap; i++) - { - obj->u.d.items[i].k = nil; - obj->u.d.items[i].v = nil; - } - obj->u.d.cap = newcap; + fz_dictput(new, fz_dictgetkey(obj, i), fz_dictgetval(obj, i)); - return fz_okay; + return new; } int @@ -211,7 +96,8 @@ fz_dictgetval(fz_obj *obj, int i) return obj->u.d.items[i].v; } -static inline int dictfinds(fz_obj *obj, char *key) +static inline int +fz_dictfinds(fz_obj *obj, char *key) { if (obj->u.d.sorted) { @@ -251,7 +137,7 @@ fz_dictgets(fz_obj *obj, char *key) if (!fz_isdict(obj)) return nil; - i = dictfinds(obj, key); + i = fz_dictfinds(obj, key); if (i >= 0) return obj->u.d.items[i].v; @@ -276,39 +162,52 @@ fz_dictgetsa(fz_obj *obj, char *key, char *abbrev) return fz_dictgets(obj, abbrev); } -fz_error +void fz_dictput(fz_obj *obj, fz_obj *key, fz_obj *val) { - fz_error error; char *s; int i; obj = fz_resolveindirect(obj); if (!fz_isdict(obj)) - return fz_throw("assert: not a dict (%s)", fz_objkindstr(obj)); + { + fz_warn("assert: not a dict (%s)", fz_objkindstr(obj)); + return; + } if (fz_isname(key)) s = fz_toname(key); else - return fz_throw("assert: key is not a name (%s)", fz_objkindstr(obj)); + { + fz_warn("assert: key is not a name (%s)", fz_objkindstr(obj)); + return; + } if (!val) - return fz_throw("assert: val does not exist for key (%s)", s); + { + fz_warn("assert: val does not exist for key (%s)", s); + return; + } - i = dictfinds(obj, s); + i = fz_dictfinds(obj, s); if (i >= 0) { fz_dropobj(obj->u.d.items[i].v); obj->u.d.items[i].v = fz_keepobj(val); - return fz_okay; + return; } if (obj->u.d.len + 1 > obj->u.d.cap) { - error = growdict(obj); - if (error) - return fz_rethrow(error, "cannot grow dict item buffer"); + int newcap = obj->u.d.cap * 2; + obj->u.d.items = fz_realloc(obj->u.d.items, sizeof(fz_keyval) * newcap); + for (i = obj->u.d.cap; i < newcap; i++) + { + obj->u.d.items[i].k = nil; + obj->u.d.items[i].v = nil; + } + obj->u.d.cap = newcap; } /* borked! */ @@ -319,55 +218,44 @@ fz_dictput(fz_obj *obj, fz_obj *key, fz_obj *val) obj->u.d.items[obj->u.d.len].k = fz_keepobj(key); obj->u.d.items[obj->u.d.len].v = fz_keepobj(val); obj->u.d.len ++; - - return fz_okay; } -fz_error +void fz_dictputs(fz_obj *obj, char *key, fz_obj *val) { - fz_error error; - fz_obj *keyobj; - error = fz_newname(&keyobj, key); - if (error) - return fz_rethrow(error, "cannot create dict key"); - error = fz_dictput(obj, keyobj, val); + fz_obj *keyobj = fz_newname(key); + fz_dictput(obj, keyobj, val); fz_dropobj(keyobj); - if (error) - return fz_rethrow(error, "cannot insert dict entry"); - return fz_okay; } -fz_error +void fz_dictdels(fz_obj *obj, char *key) { - int i; - obj = fz_resolveindirect(obj); if (!fz_isdict(obj)) - return fz_throw("assert: not a dict (%s)", fz_objkindstr(obj)); - - i = dictfinds(obj, key); - if (i >= 0) + fz_warn("assert: not a dict (%s)", fz_objkindstr(obj)); + else { - fz_dropobj(obj->u.d.items[i].k); - fz_dropobj(obj->u.d.items[i].v); - obj->u.d.sorted = 0; - obj->u.d.items[i] = obj->u.d.items[obj->u.d.len-1]; - obj->u.d.len --; + int i = fz_dictfinds(obj, key); + if (i >= 0) + { + fz_dropobj(obj->u.d.items[i].k); + fz_dropobj(obj->u.d.items[i].v); + obj->u.d.sorted = 0; + obj->u.d.items[i] = obj->u.d.items[obj->u.d.len-1]; + obj->u.d.len --; + } } - - return fz_okay; } -fz_error +void fz_dictdel(fz_obj *obj, fz_obj *key) { if (fz_isname(key)) - return fz_dictdels(obj, fz_toname(key)); + fz_dictdels(obj, fz_toname(key)); else - return fz_throw("assert: key is not a name (%s)", fz_objkindstr(obj)); + fz_warn("assert: key is not a name (%s)", fz_objkindstr(obj)); } void diff --git a/fitz/obj_parse.c b/fitz/obj_parse.c index cf7b29b9..1b500d3d 100644 --- a/fitz/obj_parse.c +++ b/fitz/obj_parse.c @@ -60,9 +60,8 @@ static void parsekeyword(char **sp, char *b, char *eb) *sp = s; } -static fz_error parsename(fz_obj **obj, char **sp) +static fz_obj * parsename(char **sp) { - fz_error error; char buf[64]; char *s = *sp; char *p = buf; @@ -73,15 +72,11 @@ static fz_error parsename(fz_obj **obj, char **sp) *p++ = 0; *sp = s; - error = fz_newname(obj, buf); - if (error) - return fz_rethrow(error, "cannot create name"); - return fz_okay; + return fz_newname(buf); } -static fz_error parsenumber(fz_obj **obj, char **sp) +static fz_obj * parsenumber(char **sp) { - fz_error error; char buf[32]; char *s = *sp; char *p = buf; @@ -97,26 +92,19 @@ static fz_error parsenumber(fz_obj **obj, char **sp) *sp = s; if (strchr(buf, '.')) - error = fz_newreal(obj, atof(buf)); - else - error = fz_newint(obj, atoi(buf)); - - if (error) - return fz_rethrow(error, "cannot parse number"); - return fz_okay; + return fz_newreal(atof(buf)); + return fz_newint(atoi(buf)); } static fz_error parsedict(fz_obj **obj, pdf_xref *xref, char **sp, struct vap *v) { - fz_error error = fz_okay; + fz_error error; fz_obj *dict = nil; fz_obj *key = nil; fz_obj *val = nil; char *s = *sp; - error = fz_newdict(&dict, 8); - if (error) - return fz_rethrow(error, "cannot create dict"); + dict = fz_newdict(8); s += 2; /* skip "<<" */ @@ -144,12 +132,7 @@ static fz_error parsedict(fz_obj **obj, pdf_xref *xref, char **sp, struct vap *v goto cleanup; } - error = parsename(&key, &s); - if (error) - { - error = fz_rethrow(error, "cannot parse key"); - goto cleanup; - } + key = parsename(&s); skipwhite(&s); @@ -160,12 +143,7 @@ static fz_error parsedict(fz_obj **obj, pdf_xref *xref, char **sp, struct vap *v goto cleanup; } - error = fz_dictput(dict, key, val); - if (error) - { - error = fz_rethrow(error, "cannot insert dict entry"); - goto cleanup; - } + fz_dictput(dict, key, val); fz_dropobj(val); val = nil; fz_dropobj(key); key = nil; @@ -191,9 +169,7 @@ static fz_error parsearray(fz_obj **obj, pdf_xref *xref, char **sp, struct vap * fz_obj *o; char *s = *sp; - error = fz_newarray(&a, 8); - if (error) - return fz_rethrow(error, "cannot create array"); + a = fz_newarray(8); s ++; /* skip '[' */ @@ -214,13 +190,7 @@ static fz_error parsearray(fz_obj **obj, pdf_xref *xref, char **sp, struct vap * return fz_rethrow(error, "cannot parse item"); } - error = fz_arraypush(a, o); - if (error) - { - fz_dropobj(o); - fz_dropobj(a); - return fz_rethrow(error, "cannot add item to array"); - } + fz_arraypush(a, o); fz_dropobj(o); } @@ -230,9 +200,8 @@ static fz_error parsearray(fz_obj **obj, pdf_xref *xref, char **sp, struct vap * return fz_okay; } -static fz_error parsestring(fz_obj **obj, char **sp) +static fz_obj * parsestring(char **sp) { - fz_error error; char buf[512]; char *s = *sp; char *p = buf; @@ -290,16 +259,11 @@ static fz_error parsestring(fz_obj **obj, char **sp) } *sp = s; - - error = fz_newstring(obj, buf, p - buf - 1); - if (error) - return fz_rethrow(error, "cannot create string"); - return fz_okay; + return fz_newstring(buf, p - buf - 1); } -static fz_error parsehexstring(fz_obj **obj, char **sp) +static fz_obj * parsehexstring(char **sp) { - fz_error error; char buf[512]; char *s = *sp; char *p = buf; @@ -330,10 +294,7 @@ static fz_error parsehexstring(fz_obj **obj, char **sp) } *sp = s; - error = fz_newstring(obj, buf, p - buf); - if (error) - return fz_rethrow(error, "cannot create string"); - return fz_okay; + return fz_newstring(buf, p - buf); } static fz_error parseobj(fz_obj **obj, pdf_xref *xref, char **sp, struct vap *v) @@ -349,8 +310,6 @@ static fz_error parseobj(fz_obj **obj, pdf_xref *xref, char **sp, struct vap *v) skipwhite(&s); - error = fz_okay; - if (v != nil && *s == '%') { s ++; @@ -358,47 +317,39 @@ static fz_error parseobj(fz_obj **obj, pdf_xref *xref, char **sp, struct vap *v) switch (*s) { case 'o': *obj = fz_keepobj(va_arg(v->ap, fz_obj*)); break; - case 'b': error = fz_newbool(obj, va_arg(v->ap, int)); break; - case 'i': error = fz_newint(obj, va_arg(v->ap, int)); break; - case 'f': error = fz_newreal(obj, (float)va_arg(v->ap, double)); break; - case 'n': error = fz_newname(obj, va_arg(v->ap, char*)); break; + case 'b': *obj = fz_newbool(va_arg(v->ap, int)); break; + case 'i': *obj = fz_newint(va_arg(v->ap, int)); break; + case 'f': *obj = fz_newreal((float)va_arg(v->ap, double)); break; + case 'n': *obj = fz_newname(va_arg(v->ap, char*)); break; case 'r': num = va_arg(v->ap, int); gen = va_arg(v->ap, int); - error = fz_newindirect(obj, num, gen, xref); + *obj = fz_newindirect(num, gen, xref); break; case 's': tmp = va_arg(v->ap, char*); - error = fz_newstring(obj, tmp, strlen(tmp)); + *obj = fz_newstring(tmp, strlen(tmp)); break; case '#': tmp = va_arg(v->ap, char*); len = va_arg(v->ap, int); - error = fz_newstring(obj, tmp, len); + *obj = fz_newstring(tmp, len); break; default: - error = fz_throw("unknown format specifier in packobj: '%c'", *s); - break; + return fz_throw("unknown format specifier in packobj: '%c'", *s); } - if (error) - error = fz_rethrow(error, "cannot create object for %% format"); - s ++; } else if (*s == '/') { - error = parsename(obj, &s); - if (error) - error = fz_rethrow(error, "cannot parse name"); + *obj = parsename(&s); } else if (*s == '(') { - error = parsestring(obj, &s); - if (error) - error = fz_rethrow(error, "cannot parse string"); + *obj = parsestring(&s); } else if (*s == '<') @@ -407,13 +358,11 @@ static fz_error parseobj(fz_obj **obj, pdf_xref *xref, char **sp, struct vap *v) { error = parsedict(obj, xref, &s, v); if (error) - error = fz_rethrow(error, "cannot parse dict"); + return fz_rethrow(error, "cannot parse dict"); } else { - error = parsehexstring(obj, &s); - if (error) - error = fz_rethrow(error, "cannot parse hex string"); + *obj = parsehexstring(&s); } } @@ -421,14 +370,12 @@ static fz_error parseobj(fz_obj **obj, pdf_xref *xref, char **sp, struct vap *v) { error = parsearray(obj, xref, &s, v); if (error) - error = fz_rethrow(error, "cannot parse array"); + return fz_rethrow(error, "cannot parse array"); } else if (*s == '-' || *s == '.' || (*s >= '0' && *s <= '9')) { - error = parsenumber(obj, &s); - if (error) - error = fz_rethrow(error, "cannot parse number"); + *obj = parsenumber(&s); } else if (isregular(*s)) @@ -436,32 +383,20 @@ static fz_error parseobj(fz_obj **obj, pdf_xref *xref, char **sp, struct vap *v) parsekeyword(&s, buf, buf + sizeof buf); if (strcmp("true", buf) == 0) - { - error = fz_newbool(obj, 1); - if (error) - error = fz_rethrow(error, "cannot create bool (true)"); - } + *obj = fz_newbool(1); else if (strcmp("false", buf) == 0) - { - error = fz_newbool(obj, 0); - if (error) - error = fz_rethrow(error, "cannot create bool (false)"); - } + *obj = fz_newbool(0); else if (strcmp("null", buf) == 0) - { - error = fz_newnull(obj); - if (error) - error = fz_rethrow(error, "cannot create null object"); - } + *obj = fz_newnull(); else - error = fz_throw("undefined keyword %s", buf); + return fz_throw("undefined keyword %s", buf); } else - error = fz_throw("syntax error: unknown byte 0x%d", *s); + return fz_throw("syntax error: unknown byte 0x%d", *s); *sp = s; - return error; /* already rethrown */ + return fz_okay; } fz_error fz_packobj(fz_obj **op, pdf_xref *xref, char *fmt, ...) diff --git a/fitz/obj_simple.c b/fitz/obj_simple.c index 8736f89a..b6b9281f 100644 --- a/fitz/obj_simple.c +++ b/fitz/obj_simple.c @@ -4,71 +4,78 @@ extern void fz_freearray(fz_obj *array); extern void fz_freedict(fz_obj *dict); -#define NEWOBJ(KIND,SIZE) \ - fz_obj *o; \ - o = *op = fz_malloc(SIZE); \ - if (!o) return fz_rethrow(-1, "out of memory: dynamic object"); \ - o->refs = 1; \ - o->kind = KIND; - -fz_error -fz_newnull(fz_obj **op) +fz_obj * +fz_newnull(void) { - NEWOBJ(FZ_NULL, sizeof (fz_obj)); - return fz_okay; + fz_obj *o = fz_malloc(sizeof(fz_obj)); + o->refs = 1; + o->kind = FZ_NULL; + return o; } -fz_error -fz_newbool(fz_obj **op, int b) +fz_obj * +fz_newbool(int b) { - NEWOBJ(FZ_BOOL, sizeof (fz_obj)); + fz_obj *o = fz_malloc(sizeof(fz_obj)); + o->refs = 1; + o->kind = FZ_BOOL; o->u.b = b; - return fz_okay; + return o; } -fz_error -fz_newint(fz_obj **op, int i) +fz_obj * +fz_newint(int i) { - NEWOBJ(FZ_INT, sizeof (fz_obj)); + fz_obj *o = fz_malloc(sizeof(fz_obj)); + o->refs = 1; + o->kind = FZ_INT; o->u.i = i; - return fz_okay; + return o; } -fz_error -fz_newreal(fz_obj **op, float f) +fz_obj * +fz_newreal(float f) { - NEWOBJ(FZ_REAL, sizeof (fz_obj)); + fz_obj *o = fz_malloc(sizeof(fz_obj)); + o->refs = 1; + o->kind = FZ_REAL; o->u.f = f; - return fz_okay; + return o; } -fz_error -fz_newstring(fz_obj **op, char *str, int len) +fz_obj * +fz_newstring(char *str, int len) { - NEWOBJ(FZ_STRING, offsetof(fz_obj, u.s.buf) + len + 1); + fz_obj *o = fz_malloc(offsetof(fz_obj, u.s.buf) + len + 1); + o->refs = 1; + o->kind = FZ_STRING; o->u.s.len = len; memcpy(o->u.s.buf, str, len); o->u.s.buf[len] = '\0'; - return fz_okay; + return o; } -fz_error -fz_newname(fz_obj **op, char *str) +fz_obj * +fz_newname(char *str) { - NEWOBJ(FZ_NAME, offsetof(fz_obj, u.n) + strlen(str) + 1); + fz_obj *o = fz_malloc(offsetof(fz_obj, u.n) + strlen(str) + 1); + o->refs = 1; + o->kind = FZ_NAME; strcpy(o->u.n, str); - return fz_okay; + return o; } -fz_error -fz_newindirect(fz_obj **op, int num, int gen, pdf_xref *xref) +fz_obj * +fz_newindirect(int num, int gen, pdf_xref *xref) { - NEWOBJ(FZ_INDIRECT, sizeof (fz_obj)); + fz_obj *o = fz_malloc(sizeof(fz_obj)); + o->refs = 1; + o->kind = FZ_INDIRECT; o->u.r.num = num; o->u.r.gen = gen; o->u.r.xref = xref; o->u.r.obj = nil; - return fz_okay; + return o; } fz_obj * diff --git a/mupdf/mupdf.h b/mupdf/mupdf.h index 393aa37c..325d068f 100644 --- a/mupdf/mupdf.h +++ b/mupdf/mupdf.h @@ -41,8 +41,8 @@ fz_error pdf_parseindobj(fz_obj **op, pdf_xref *xref, fz_stream *f, char *buf, i fz_rect pdf_torect(fz_obj *array); fz_matrix pdf_tomatrix(fz_obj *array); -fz_error pdf_toutf8(char **dstp, fz_obj *src); -fz_error pdf_toucs2(unsigned short **dstp, fz_obj *src); +char * pdf_toutf8(fz_obj *src); +unsigned short * pdf_toucs2(fz_obj *src); /* * Encryption @@ -188,18 +188,16 @@ typedef enum pdf_itemkind_e PDF_KFONT } pdf_itemkind; -fz_error pdf_newstore(pdf_store **storep); +pdf_store * pdf_newstore(void); void pdf_emptystore(pdf_store *store); void pdf_dropstore(pdf_store *store); void pdf_agestoreditems(pdf_store *store); -fz_error pdf_evictageditems(pdf_store *store); +void pdf_evictageditems(pdf_store *store); -fz_error pdf_storeitem(pdf_store *store, pdf_itemkind tag, fz_obj *key, void *val); +void pdf_storeitem(pdf_store *store, pdf_itemkind tag, fz_obj *key, void *val); void *pdf_finditem(pdf_store *store, pdf_itemkind tag, fz_obj *key); -fz_error pdf_removeitem(pdf_store *store, pdf_itemkind tag, fz_obj *key); - -fz_error pdf_loadresources(fz_obj **rdb, pdf_xref *xref, fz_obj *orig); +void pdf_removeitem(pdf_store *store, pdf_itemkind tag, fz_obj *key); /* * Functions @@ -554,15 +552,15 @@ struct pdf_outline_s fz_error pdf_loadnametree(fz_obj **dictp, pdf_xref *xref, fz_obj *root); fz_obj *pdf_lookupdest(pdf_xref *xref, fz_obj *nameddest); -fz_error pdf_newlink(pdf_link**, pdf_linkkind kind, fz_rect rect, fz_obj *dest); -fz_error pdf_loadlink(pdf_link **linkp, pdf_xref *xref, fz_obj *dict); +pdf_link *pdf_newlink(pdf_linkkind kind, fz_rect rect, fz_obj *dest); +pdf_link *pdf_loadlink(pdf_xref *xref, fz_obj *dict); void pdf_droplink(pdf_link *link); -fz_error pdf_loadoutline(pdf_outline **outlinep, pdf_xref *xref); +pdf_outline *pdf_loadoutline(pdf_xref *xref); void pdf_debugoutline(pdf_outline *outline, int level); void pdf_dropoutline(pdf_outline *outline); -fz_error pdf_loadannots(pdf_comment **, pdf_link **, pdf_xref *, fz_obj *annots); +void pdf_loadannots(pdf_comment **, pdf_link **, pdf_xref *, fz_obj *annots); /* * Page tree, pages and related objects @@ -596,9 +594,9 @@ struct pdf_textline_s }; /* pagetree.c */ -fz_error pdf_getpagecount(pdf_xref *xref, int *pagesp); -fz_error pdf_getpageobject(pdf_xref *xref, int p, fz_obj **pagep); -fz_error pdf_findpageobject(pdf_xref *xref, fz_obj *pageobj, int *page); +int pdf_getpagecount(pdf_xref *xref); +fz_obj * pdf_getpageobject(pdf_xref *xref, int p); +int pdf_findpageobject(pdf_xref *xref, fz_obj *pageobj); /* page.c */ fz_error pdf_loadpage(pdf_page **pagep, pdf_xref *xref, fz_obj *ref); diff --git a/mupdf/pdf_annot.c b/mupdf/pdf_annot.c index 9d4082de..eda2b5d7 100644 --- a/mupdf/pdf_annot.c +++ b/mupdf/pdf_annot.c @@ -1,28 +1,19 @@ #include "fitz.h" #include "mupdf.h" -static fz_error -loadcomment(pdf_comment **commentp, pdf_xref *xref, fz_obj *dict) -{ - return fz_okay; -} - -fz_error -pdf_newlink(pdf_link **linkp, pdf_linkkind kind, fz_rect bbox, fz_obj *dest) +pdf_link * +pdf_newlink(pdf_linkkind kind, fz_rect bbox, fz_obj *dest) { pdf_link *link; link = fz_malloc(sizeof(pdf_link)); - if (!link) - return fz_rethrow(-1, "out of memory"); link->kind = kind; link->rect = bbox; link->dest = fz_keepobj(dest); link->next = nil; - *linkp = link; - return fz_okay; + return link; } void @@ -61,11 +52,9 @@ resolvedest(pdf_xref *xref, fz_obj *dest) return nil; } -fz_error -pdf_loadlink(pdf_link **linkp, pdf_xref *xref, fz_obj *dict) +pdf_link * +pdf_loadlink(pdf_xref *xref, fz_obj *dict) { - fz_error error; - pdf_link *link; fz_obj *dest; fz_obj *action; fz_obj *obj; @@ -74,7 +63,6 @@ pdf_loadlink(pdf_link **linkp, pdf_xref *xref, fz_obj *dict) pdf_logpage("load link {\n"); - link = nil; dest = nil; obj = fz_dictgets(dict, "Rect"); @@ -123,19 +111,15 @@ pdf_loadlink(pdf_link **linkp, pdf_xref *xref, fz_obj *dict) if (dest) { - error = pdf_newlink(&link, kind, bbox, dest); - if (error) - return fz_rethrow(error, "cannot create link"); - *linkp = link; + return pdf_newlink(kind, bbox, dest); } - return fz_okay; + return nil; } -fz_error +void pdf_loadannots(pdf_comment **cp, pdf_link **lp, pdf_xref *xref, fz_obj *annots) { - fz_error error; pdf_comment *comment; pdf_link *link; fz_obj *subtype; @@ -154,38 +138,18 @@ pdf_loadannots(pdf_comment **cp, pdf_link **lp, pdf_xref *xref, fz_obj *annots) subtype = fz_dictgets(obj, "Subtype"); if (fz_isname(subtype) && !strcmp(fz_toname(subtype), "Link")) { - pdf_link *temp = nil; - - error = pdf_loadlink(&temp, xref, obj); - if (error) - { - if (link) - pdf_droplink(link); - return fz_rethrow(error, "cannot load annotation link"); - } - + pdf_link *temp = pdf_loadlink(xref, obj); if (temp) { temp->next = link; link = temp; } } - else - { - error = loadcomment(&comment, xref, obj); - if (error) - { - if (link) - pdf_droplink(link); - return fz_rethrow(error, "cannot load annotation comment"); - } - } } pdf_logpage("}\n"); *cp = comment; *lp = link; - return fz_okay; } diff --git a/mupdf/pdf_cmap_load.c b/mupdf/pdf_cmap_load.c index ae6f8f7a..7376abac 100644 --- a/mupdf/pdf_cmap_load.c +++ b/mupdf/pdf_cmap_load.c @@ -76,12 +76,7 @@ pdf_loadembeddedcmap(pdf_cmap **cmapp, pdf_xref *xref, fz_obj *stmref) pdf_logfont("}\n"); - error = pdf_storeitem(xref->store, PDF_KCMAP, stmref, cmap); - if (error) - { - error = fz_rethrow(error, "cannot store cmap resource"); - goto cleanup; - } + pdf_storeitem(xref->store, PDF_KCMAP, stmref, cmap); *cmapp = cmap; return fz_okay; diff --git a/mupdf/pdf_colorspace1.c b/mupdf/pdf_colorspace1.c index 0893bfb7..d798649c 100644 --- a/mupdf/pdf_colorspace1.c +++ b/mupdf/pdf_colorspace1.c @@ -749,9 +749,7 @@ pdf_loadcolorspace(fz_colorspace **csp, pdf_xref *xref, fz_obj *obj) if (error) return fz_rethrow(error, "cannot load colorspace"); - error = pdf_storeitem(xref->store, PDF_KCOLORSPACE, obj, *csp); - if (error) - return fz_rethrow(error, "cannot store colorspace resource"); + pdf_storeitem(xref->store, PDF_KCOLORSPACE, obj, *csp); return fz_okay; } diff --git a/mupdf/pdf_font.c b/mupdf/pdf_font.c index 8b94686f..d0d3007f 100644 --- a/mupdf/pdf_font.c +++ b/mupdf/pdf_font.c @@ -983,9 +983,7 @@ pdf_loadfont(pdf_fontdesc **fontdescp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict if (error) return fz_rethrow(error, "cannot load font"); - error = pdf_storeitem(xref->store, PDF_KFONT, dict, *fontdescp); - if (error) - return fz_rethrow(error, "cannot store font resource"); + pdf_storeitem(xref->store, PDF_KFONT, dict, *fontdescp); return fz_okay; } diff --git a/mupdf/pdf_function.c b/mupdf/pdf_function.c index 86cda433..82baf1b6 100644 --- a/mupdf/pdf_function.c +++ b/mupdf/pdf_function.c @@ -1527,12 +1527,7 @@ pdf_loadfunction(pdf_function **funcp, pdf_xref *xref, fz_obj *ref) pdf_logrsrc("}\n"); - error = pdf_storeitem(xref->store, PDF_KFUNCTION, ref, func); - if (error) - { - pdf_dropfunction(func); - return fz_rethrow(error, "cannot store function resource"); - } + pdf_storeitem(xref->store, PDF_KFUNCTION, ref, func); *funcp = func; return fz_okay; diff --git a/mupdf/pdf_image.c b/mupdf/pdf_image.c index eb2f28e6..6f5d1be9 100644 --- a/mupdf/pdf_image.c +++ b/mupdf/pdf_image.c @@ -449,12 +449,7 @@ pdf_loadimage(pdf_image **imgp, pdf_xref *xref, fz_obj *dict) pdf_logimage("}\n"); - error = pdf_storeitem(xref->store, PDF_KIMAGE, dict, img); - if (error) - { - fz_dropimage((fz_image*)img); - return error; - } + pdf_storeitem(xref->store, PDF_KIMAGE, dict, img); *imgp = img; return fz_okay; diff --git a/mupdf/pdf_interpret.c b/mupdf/pdf_interpret.c index 94b9fdc3..76755714 100644 --- a/mupdf/pdf_interpret.c +++ b/mupdf/pdf_interpret.c @@ -1357,19 +1357,15 @@ pdf_runcsi(pdf_csi *csi, pdf_xref *xref, fz_obj *rdb, fz_stream *file) } else if (tok == PDF_TINT || tok == PDF_TREAL) { - error = fz_newreal(&obj, atof(buf)); - if (error) return fz_rethrow(error, "cannot create number"); - error = fz_arraypush(csi->array, obj); + obj = fz_newreal(atof(buf)); + fz_arraypush(csi->array, obj); fz_dropobj(obj); - if (error) return fz_rethrow(error, "cannot add number to array"); } else if (tok == PDF_TSTRING) { - error = fz_newstring(&obj, buf, len); - if (error) return fz_rethrow(error, "cannot create string"); - error = fz_arraypush(csi->array, obj); + obj = fz_newstring(buf, len); + fz_arraypush(csi->array, obj); fz_dropobj(obj); - if (error) return fz_rethrow(error, "cannot add string to array"); } else if (tok == PDF_TEOF) { @@ -1389,8 +1385,7 @@ pdf_runcsi(pdf_csi *csi, pdf_xref *xref, fz_obj *rdb, fz_stream *file) /* optimize text-object array parsing */ case PDF_TOARRAY: - error = fz_newarray(&csi->array, 8); - if (error) return fz_rethrow(error, "cannot create array"); + csi->array = fz_newarray(8); break; case PDF_TODICT: @@ -1400,44 +1395,37 @@ pdf_runcsi(pdf_csi *csi, pdf_xref *xref, fz_obj *rdb, fz_stream *file) break; case PDF_TNAME: - error = fz_newname(&csi->stack[csi->top], buf); - if (error) return fz_rethrow(error, "cannot create name"); + csi->stack[csi->top] = fz_newname(buf); csi->top ++; break; case PDF_TINT: - error = fz_newint(&csi->stack[csi->top], atoi(buf)); - if (error) return fz_rethrow(error, "cannot create integer"); + csi->stack[csi->top] = fz_newint(atoi(buf)); csi->top ++; break; case PDF_TREAL: - error = fz_newreal(&csi->stack[csi->top], atof(buf)); - if (error) return fz_rethrow(error, "cannot create real"); + csi->stack[csi->top] = fz_newreal(atof(buf)); csi->top ++; break; case PDF_TSTRING: - error = fz_newstring(&csi->stack[csi->top], buf, len); - if (error) return fz_rethrow(error, "cannot create string"); + csi->stack[csi->top] = fz_newstring(buf, len); csi->top ++; break; case PDF_TTRUE: - error = fz_newbool(&csi->stack[csi->top], 1); - if (error) return fz_rethrow(error, "cannot create true"); + csi->stack[csi->top] = fz_newbool(1); csi->top ++; break; case PDF_TFALSE: - error = fz_newbool(&csi->stack[csi->top], 0); - if (error) return fz_rethrow(error, "cannot create false"); + csi->stack[csi->top] = fz_newbool(0); csi->top ++; break; case PDF_TNULL: - error = fz_newnull(&csi->stack[csi->top]); - if (error) return fz_rethrow(error, "cannot create null"); + csi->stack[csi->top] = fz_newnull(); csi->top ++; break; diff --git a/mupdf/pdf_outline.c b/mupdf/pdf_outline.c index 69ff8730..7fe6b886 100644 --- a/mupdf/pdf_outline.c +++ b/mupdf/pdf_outline.c @@ -1,10 +1,9 @@ #include "fitz.h" #include "mupdf.h" -static fz_error -loadoutline(pdf_outline **nodep, pdf_xref *xref, fz_obj *dict) +static pdf_outline * +loadoutline(pdf_xref *xref, fz_obj *dict) { - fz_error error; pdf_outline *node; fz_obj *obj; @@ -19,25 +18,19 @@ loadoutline(pdf_outline **nodep, pdf_xref *xref, fz_obj *dict) obj = fz_dictgets(dict, "Title"); if (obj) { - error = pdf_toutf8(&node->title, obj); - if (error) - return fz_rethrow(error, "cannot convert Title to UTF-8"); + node->title = pdf_toutf8(obj); pdf_logpage("title %s\n", node->title); } if (fz_dictgets(dict, "Dest") || fz_dictgets(dict, "A")) { - error = pdf_loadlink(&node->link, xref, dict); - if (error) - return fz_rethrow(error, "cannot load link"); + node->link = pdf_loadlink(xref, dict); } obj = fz_dictgets(dict, "First"); if (obj) { - error = loadoutline(&node->child, xref, obj); - if (error) - return fz_rethrow(error, "cannot load outline"); + node->child = loadoutline(xref, obj); } pdf_logpage("}\n"); @@ -45,19 +38,15 @@ loadoutline(pdf_outline **nodep, pdf_xref *xref, fz_obj *dict) obj = fz_dictgets(dict, "Next"); if (obj) { - error = loadoutline(&node->next, xref, obj); - if (error) - return fz_rethrow(error, "cannot load outline"); + node->next = loadoutline(xref, obj); } - *nodep = node; - return fz_okay; + return node; } -fz_error -pdf_loadoutline(pdf_outline **nodep, pdf_xref *xref) +pdf_outline * +pdf_loadoutline(pdf_xref *xref) { - fz_error error; pdf_outline *node; fz_obj *obj; fz_obj *first; @@ -72,16 +61,13 @@ pdf_loadoutline(pdf_outline **nodep, pdf_xref *xref) first = fz_dictgets(obj, "First"); if (first) { - error = loadoutline(&node, xref, first); - if (error) - return fz_rethrow(error, "cannot load outline"); + node = loadoutline(xref, first); } } pdf_logpage("}\n"); - *nodep = node; - return fz_okay; + return node; } void diff --git a/mupdf/pdf_page.c b/mupdf/pdf_page.c index 0c502d1b..9b42513d 100644 --- a/mupdf/pdf_page.c +++ b/mupdf/pdf_page.c @@ -205,9 +205,7 @@ pdf_loadpage(pdf_page **pagep, pdf_xref *xref, fz_obj *dict) obj = fz_dictgets(dict, "Annots"); if (obj) { - error = pdf_loadannots(&comments, &links, xref, obj); - if (error) - return fz_rethrow(error, "cannot load annotations"); + pdf_loadannots(&comments, &links, xref, obj); } /* @@ -216,9 +214,7 @@ pdf_loadpage(pdf_page **pagep, pdf_xref *xref, fz_obj *dict) if (!xref->store) { - error = pdf_newstore(&xref->store); - if (error) - return fz_rethrow(error, "cannot create resource store"); + xref->store = pdf_newstore(); } /* @@ -231,9 +227,7 @@ pdf_loadpage(pdf_page **pagep, pdf_xref *xref, fz_obj *dict) else { fz_warn("cannot find page resources, proceeding anyway."); - error = fz_newdict(&rdb, 0); - if (error) - return fz_rethrow(error, "cannot create fake page resources"); + rdb = fz_newdict(0); } /* diff --git a/mupdf/pdf_pagetree.c b/mupdf/pdf_pagetree.c index a639e92a..ba6d8ec1 100644 --- a/mupdf/pdf_pagetree.c +++ b/mupdf/pdf_pagetree.c @@ -9,10 +9,9 @@ struct stuff fz_obj *rotate; }; -static fz_error +static void getpagecount(pdf_xref *xref, fz_obj *node, int *pagesp) { - fz_error error; fz_obj *type; fz_obj *kids; fz_obj *count; @@ -23,7 +22,7 @@ getpagecount(pdf_xref *xref, fz_obj *node, int *pagesp) if (!fz_isdict(node)) { fz_warn("pagetree node is missing, igoring missing pages..."); - return fz_okay; + return; } type = fz_dictgets(node, "Type"); @@ -65,25 +64,20 @@ getpagecount(pdf_xref *xref, fz_obj *node, int *pagesp) /* prevent infinite recursion possible in maliciously crafted PDFs */ if (obj == node) - return fz_throw("corrupted pdf file"); + { + fz_warn("cyclic page tree"); + return; + } - error = getpagecount(xref, obj, &pages); - if (error) - return fz_rethrow(error, "cannot load pagesubtree (%d %d R)", fz_tonum(obj), fz_togen(obj)); + getpagecount(xref, obj, &pages); } if (pages != fz_toint(count)) { fz_warn("page tree node contains incorrect number of pages, continuing..."); - - error = fz_newint(&count, pages); - if (!error) - { - error = fz_dictputs(node, "Count", count); - fz_dropobj(count); - } - if (error) - return fz_rethrow(error, "cannot correct wrong page count"); + count = fz_newint(pages); + fz_dictputs(node, "Count", count); + fz_dropobj(count); } pdf_logpage("%d pages\n", pages); @@ -92,17 +86,15 @@ getpagecount(pdf_xref *xref, fz_obj *node, int *pagesp) pdf_logpage("}\n"); } - - return fz_okay; } -fz_error -pdf_getpagecount(pdf_xref *xref, int *pagesp) +int +pdf_getpagecount(pdf_xref *xref) { - fz_error error; fz_obj *ref; fz_obj *catalog; fz_obj *pages; + int count; ref = fz_dictgets(xref->trailer, "Root"); catalog = fz_resolveindirect(ref); @@ -110,20 +102,17 @@ pdf_getpagecount(pdf_xref *xref, int *pagesp) pages = fz_dictgets(catalog, "Pages"); pdf_logpage("determining page count (%d %d R) {\n", fz_tonum(pages), fz_togen(pages)); - *pagesp = 0; - error = getpagecount(xref, pages, pagesp); - if (error) - return fz_rethrow(error, "cannot determine page count"); + count = 0; + getpagecount(xref, pages, &count); pdf_logpage("}\n"); - return fz_okay; + return count; } -static fz_error +static void getpageobject(pdf_xref *xref, struct stuff inherit, fz_obj *node, int *pagesp, int pageno, fz_obj **pagep) { - fz_error error; char *typestr; fz_obj *type; fz_obj *kids; @@ -135,7 +124,7 @@ getpageobject(pdf_xref *xref, struct stuff inherit, fz_obj *node, int *pagesp, i { fz_warn("pagetree node is missing, ignoring missing pages..."); *pagep = nil; - return fz_okay; + return; } type = fz_dictgets(node, "Type"); @@ -171,33 +160,25 @@ getpageobject(pdf_xref *xref, struct stuff inherit, fz_obj *node, int *pagesp, i if (inherit.resources && !fz_dictgets(node, "Resources")) { pdf_logpage("inherited resources\n"); - error = fz_dictputs(node, "Resources", inherit.resources); - if (error) - return fz_rethrow(error, "cannot inherit page tree resources"); + fz_dictputs(node, "Resources", inherit.resources); } if (inherit.mediabox && !fz_dictgets(node, "MediaBox")) { pdf_logpage("inherit mediabox\n"); - error = fz_dictputs(node, "MediaBox", inherit.mediabox); - if (error) - return fz_rethrow(error, "cannot inherit page tree mediabox"); + fz_dictputs(node, "MediaBox", inherit.mediabox); } if (inherit.cropbox && !fz_dictgets(node, "CropBox")) { pdf_logpage("inherit cropbox\n"); - error = fz_dictputs(node, "CropBox", inherit.cropbox); - if (error) - return fz_rethrow(error, "cannot inherit page tree cropbox"); + fz_dictputs(node, "CropBox", inherit.cropbox); } if (inherit.rotate && !fz_dictgets(node, "Rotate")) { pdf_logpage("inherit rotate\n"); - error = fz_dictputs(node, "Rotate", inherit.rotate); - if (error) - return fz_rethrow(error, "cannot inherit page tree rotate"); + fz_dictputs(node, "Rotate", inherit.rotate); } *pagep = node; @@ -212,7 +193,7 @@ getpageobject(pdf_xref *xref, struct stuff inherit, fz_obj *node, int *pagesp, i if (*pagesp + fz_toint(count) < pageno) { (*pagesp) += fz_toint(count); - return fz_okay; + return; } inh = fz_dictgets(node, "Resources"); @@ -235,27 +216,26 @@ getpageobject(pdf_xref *xref, struct stuff inherit, fz_obj *node, int *pagesp, i /* prevent infinite recursion possible in maliciously crafted PDFs */ if (obj == node) - return fz_throw("corrupted pdf file"); + { + fz_warn("cyclic page tree"); + return; + } - error = getpageobject(xref, inherit, obj, pagesp, pageno, pagep); - if (error) - return fz_rethrow(error, "cannot load pagesubtree (%d %d R)", fz_tonum(obj), fz_togen(obj)); + getpageobject(xref, inherit, obj, pagesp, pageno, pagep); } pdf_logpage("}\n"); } - - return fz_okay; } -fz_error -pdf_getpageobject(pdf_xref *xref, int pageno, fz_obj **pagep) +fz_obj * +pdf_getpageobject(pdf_xref *xref, int pageno) { - fz_error error; struct stuff inherit; fz_obj *ref; fz_obj *catalog; fz_obj *pages; + fz_obj *page; int count; inherit.resources = nil; @@ -269,28 +249,27 @@ pdf_getpageobject(pdf_xref *xref, int pageno, fz_obj **pagep) pages = fz_dictgets(catalog, "Pages"); pdf_logpage("get page %d (%d %d R) {\n", pageno, fz_tonum(pages), fz_togen(pages)); - *pagep = nil; + page = nil; count = 0; - error = getpageobject(xref, inherit, pages, &count, pageno, pagep); - if (error) - return fz_rethrow(error, "cannot find page %d", pageno); + getpageobject(xref, inherit, pages, &count, pageno, &page); + if (!page) + fz_warn("cannot find page %d", pageno); pdf_logpage("}\n"); - return fz_okay; + return page; } -static fz_error +static void findpageobject(pdf_xref *xref, fz_obj *node, fz_obj *page, int *pagenop, int *foundp) { - fz_error error; char *typestr; fz_obj *type; fz_obj *kids; int i; if (!fz_isdict(node)) - return fz_throw("pagetree node is missing"); + return; type = fz_dictgets(node, "Type"); kids = fz_dictgets(node, "Kids"); @@ -337,26 +316,25 @@ findpageobject(pdf_xref *xref, fz_obj *node, fz_obj *page, int *pagenop, int *fo /* prevent infinite recursion possible in maliciously crafted PDFs */ if (obj == node) - return fz_throw("corrupted pdf file"); + { + fz_warn("cyclic page tree"); + return; + } - error = findpageobject(xref, obj, page, pagenop, foundp); - if (error) - return fz_rethrow(error, "cannot load pagesubtree (%d %d R)", fz_tonum(obj), fz_togen(obj)); + findpageobject(xref, obj, page, pagenop, foundp); } pdf_logpage("}\n"); } - - return fz_okay; } -fz_error -pdf_findpageobject(pdf_xref *xref, fz_obj *page, int *pagenop) +int +pdf_findpageobject(pdf_xref *xref, fz_obj *page) { - fz_error error; fz_obj *ref; fz_obj *catalog; fz_obj *pages; + int pageno; int found; ref = fz_dictgets(xref->trailer, "Root"); @@ -365,17 +343,15 @@ pdf_findpageobject(pdf_xref *xref, fz_obj *page, int *pagenop) pages = fz_dictgets(catalog, "Pages"); pdf_logpage("find page object (%d %d R) (%d %d R) {\n", fz_tonum(page), fz_togen(page), fz_tonum(pages), fz_togen(pages)); - *pagenop = 0; + pageno = 0; found = 0; - error = findpageobject(xref, pages, page, pagenop, &found); - if (error) - return fz_rethrow(error, "cannot find page object (%d %d R)", fz_tonum(page), fz_togen(page)); + findpageobject(xref, pages, page, &pageno, &found); pdf_logpage("}\n"); if (!found) - return fz_throw("cannot find page object (%d %d R)", fz_tonum(page), fz_togen(page)); + fz_warn("cannot find page object (%d %d R)", fz_tonum(page), fz_togen(page)); - return fz_okay; + return pageno; } diff --git a/mupdf/pdf_parse.c b/mupdf/pdf_parse.c index 016a638d..cbe9c490 100644 --- a/mupdf/pdf_parse.c +++ b/mupdf/pdf_parse.c @@ -27,11 +27,11 @@ fz_matrix pdf_tomatrix(fz_obj *array) return m; } -fz_error -pdf_toutf8(char **dstp, fz_obj *src) +char * +pdf_toutf8(fz_obj *src) { unsigned char *srcptr = (unsigned char *) fz_tostrbuf(src); - char *dstptr; + char *dstptr, *dst; int srclen = fz_tostrlen(src); int dstlen = 0; int ucs; @@ -45,9 +45,7 @@ pdf_toutf8(char **dstp, fz_obj *src) dstlen += runelen(ucs); } - dstptr = *dstp = fz_malloc(dstlen + 1); - if (!dstptr) - return fz_rethrow(-1, "out of memory: utf-8 string"); + dstptr = dst = fz_malloc(dstlen + 1); for (i = 2; i < srclen; i += 2) { @@ -61,9 +59,7 @@ pdf_toutf8(char **dstp, fz_obj *src) for (i = 0; i < srclen; i++) dstlen += runelen(pdf_docencoding[srcptr[i]]); - dstptr = *dstp = fz_malloc(dstlen + 1); - if (!dstptr) - return fz_rethrow(-1, "out of memory: utf-8 string"); + dstptr = dst = fz_malloc(dstlen + 1); for (i = 0; i < srclen; i++) { @@ -73,31 +69,27 @@ pdf_toutf8(char **dstp, fz_obj *src) } *dstptr = '\0'; - return fz_okay; + return dst; } -fz_error -pdf_toucs2(unsigned short **dstp, fz_obj *src) +unsigned short * +pdf_toucs2(fz_obj *src) { unsigned char *srcptr = (unsigned char *) fz_tostrbuf(src); - unsigned short *dstptr; + unsigned short *dstptr, *dst; int srclen = fz_tostrlen(src); int i; if (srclen > 2 && srcptr[0] == 254 && srcptr[1] == 255) { - dstptr = *dstp = fz_malloc(((srclen - 2) / 2 + 1) * sizeof(short)); - if (!dstptr) - return fz_rethrow(-1, "out of memory: ucs-2 string"); + dstptr = dst = fz_malloc(((srclen - 2) / 2 + 1) * sizeof(short)); for (i = 2; i < srclen; i += 2) *dstptr++ = (srcptr[i] << 8) | srcptr[i+1]; } else { - dstptr = *dstp = fz_malloc((srclen + 1) * sizeof(short)); - if (!dstptr) - return fz_rethrow(-1, "out of memory: ucs-2 string"); + dstptr = dst = fz_malloc((srclen + 1) * sizeof(short)); for (i = 0; i < srclen; i++) *dstptr++ = pdf_docencoding[srcptr[i]]; } @@ -116,53 +108,39 @@ pdf_parsearray(fz_obj **op, pdf_xref *xref, fz_stream *file, char *buf, int cap) pdf_token_e tok; int len; - error = fz_newarray(&ary, 4); - if (error) - return fz_rethrow(error, "cannot create array"); + ary = fz_newarray(4); while (1) { error = pdf_lex(&tok, file, buf, cap, &len); if (error) - goto cleanup; + { + fz_dropobj(ary); + return fz_rethrow(error, "cannot parse array"); + } if (tok != PDF_TINT && tok != PDF_TR) { if (n > 0) { - error = fz_newint(&obj, a); - if (error) - goto cleanup; - error = fz_arraypush(ary, obj); - if (error) - goto cleanup; + obj = fz_newint(a); + fz_arraypush(ary, obj); fz_dropobj(obj); - obj = nil; } if (n > 1) { - error = fz_newint(&obj, b); - if (error) - goto cleanup; - error = fz_arraypush(ary, obj); - if (error) - goto cleanup; + obj = fz_newint(b); + fz_arraypush(ary, obj); fz_dropobj(obj); - obj = nil; } n = 0; } if (tok == PDF_TINT && n == 2) { - error = fz_newint(&obj, a); - if (error) - goto cleanup; - error = fz_arraypush(ary, obj); - if (error) - goto cleanup; + obj = fz_newint(a); + fz_arraypush(ary, obj); fz_dropobj(obj); - obj = nil; a = b; n --; } @@ -172,6 +150,7 @@ pdf_parsearray(fz_obj **op, pdf_xref *xref, fz_stream *file, char *buf, int cap) case PDF_TCARRAY: *op = ary; return fz_okay; + case PDF_TINT: if (n == 0) a = atoi(buf); @@ -179,44 +158,77 @@ pdf_parsearray(fz_obj **op, pdf_xref *xref, fz_stream *file, char *buf, int cap) b = atoi(buf); n ++; break; + case PDF_TR: if (n != 2) - goto cleanup; - error = fz_newindirect(&obj, a, b, xref); - if (error) - goto cleanup; + { + fz_dropobj(ary); + return fz_throw("cannot parse indirect reference in array"); + } + obj = fz_newindirect(a, b, xref); + fz_arraypush(ary, obj); + fz_dropobj(obj); n = 0; break; - case PDF_TOARRAY: error = pdf_parsearray(&obj, xref, file, buf, cap); break; - case PDF_TODICT: error = pdf_parsedict(&obj, xref, file, buf, cap); break; - case PDF_TNAME: error = fz_newname(&obj, buf); break; - case PDF_TREAL: error = fz_newreal(&obj, atof(buf)); break; - case PDF_TSTRING: error = fz_newstring(&obj, buf, len); break; - case PDF_TTRUE: error = fz_newbool(&obj, 1); break; - case PDF_TFALSE: error = fz_newbool(&obj, 0); break; - case PDF_TNULL: error = fz_newnull(&obj); break; - default: goto cleanup; - } - if (error) - goto cleanup; - - if (obj) - { - error = fz_arraypush(ary, obj); + case PDF_TOARRAY: + error = pdf_parsearray(&obj, xref, file, buf, cap); if (error) - goto cleanup; + { + fz_dropobj(ary); + return fz_rethrow(error, "cannot parse array"); + } + fz_arraypush(ary, obj); fz_dropobj(obj); - } + break; + + case PDF_TODICT: + error = pdf_parsedict(&obj, xref, file, buf, cap); + if (error) + { + fz_dropobj(ary); + return fz_rethrow(error, "cannot parse array"); + } + fz_arraypush(ary, obj); + fz_dropobj(obj); + break; - obj = nil; - } + case PDF_TNAME: + obj = fz_newname(buf); + fz_arraypush(ary, obj); + fz_dropobj(obj); + break; + case PDF_TREAL: + obj = fz_newreal(atof(buf)); + fz_arraypush(ary, obj); + fz_dropobj(obj); + break; + case PDF_TSTRING: + obj = fz_newstring(buf, len); + fz_arraypush(ary, obj); + fz_dropobj(obj); + break; + case PDF_TTRUE: + obj = fz_newbool(1); + fz_arraypush(ary, obj); + fz_dropobj(obj); + break; + case PDF_TFALSE: + obj = fz_newbool(0); + fz_arraypush(ary, obj); + fz_dropobj(obj); + break; + case PDF_TNULL: + obj = fz_newnull(); + fz_arraypush(ary, obj); + fz_dropobj(obj); + break; -cleanup: - if (obj) fz_dropobj(obj); - if (ary) fz_dropobj(ary); - *op = nil; - return fz_rethrow(error, "cannot parse array"); + default: + fz_dropobj(ary); + return fz_rethrow(error, "cannot parse token in array"); + } + } } fz_error @@ -230,15 +242,16 @@ pdf_parsedict(fz_obj **op, pdf_xref *xref, fz_stream *file, char *buf, int cap) int len; int a, b; - error = fz_newdict(&dict, 8); - if (error) - return fz_rethrow(error, "cannot create dict"); + dict = fz_newdict(8); while (1) { error = pdf_lex(&tok, file, buf, cap, &len); if (error) - goto cleanup; + { + fz_dropobj(dict); + return fz_rethrow(error, "cannot parse dict"); + } skip: if (tok == PDF_TCDICT) @@ -256,46 +269,64 @@ skip: if (tok != PDF_TNAME) { - error = fz_throw("invalid key in dict"); - goto cleanup; + fz_dropobj(dict); + return fz_throw("invalid key in dict");; } - error = fz_newname(&key, buf); - if (error) - goto cleanup; + key = fz_newname(buf); error = pdf_lex(&tok, file, buf, cap, &len); if (error) - goto cleanup; + { + fz_dropobj(dict); + return fz_rethrow(error, "cannot parse dict"); + } switch (tok) { - case PDF_TOARRAY: error = pdf_parsearray(&val, xref, file, buf, cap); break; - case PDF_TODICT: error = pdf_parsedict(&val, xref, file, buf, cap); break; - case PDF_TNAME: error = fz_newname(&val, buf); break; - case PDF_TREAL: error = fz_newreal(&val, atof(buf)); break; - case PDF_TSTRING: error = fz_newstring(&val, buf, len); break; - case PDF_TTRUE: error = fz_newbool(&val, 1); break; - case PDF_TFALSE: error = fz_newbool(&val, 0); break; - case PDF_TNULL: error = fz_newnull(&val); break; + case PDF_TOARRAY: + error = pdf_parsearray(&val, xref, file, buf, cap); + if (error) + { + fz_dropobj(key); + fz_dropobj(dict); + return fz_rethrow(error, "cannot parse dict"); + } + break; + + case PDF_TODICT: + error = pdf_parsedict(&val, xref, file, buf, cap); + if (error) + { + fz_dropobj(key); + fz_dropobj(dict); + return fz_rethrow(error, "cannot parse dict"); + } + break; + + case PDF_TNAME: val = fz_newname(buf); break; + case PDF_TREAL: val = fz_newreal(atof(buf)); break; + case PDF_TSTRING: val = fz_newstring(buf, len); break; + case PDF_TTRUE: val = fz_newbool(1); break; + case PDF_TFALSE: val = fz_newbool(0); break; + case PDF_TNULL: val = fz_newnull(); break; case PDF_TINT: a = atoi(buf); error = pdf_lex(&tok, file, buf, cap, &len); if (error) - goto cleanup; + { + fz_dropobj(key); + fz_dropobj(dict); + return fz_rethrow(error, "cannot parse dict"); + } if (tok == PDF_TCDICT || tok == PDF_TNAME || (tok == PDF_TKEYWORD && !strcmp(buf, "ID"))) { - error = fz_newint(&val, a); - if (error) - goto cleanup; - error = fz_dictput(dict, key, val); - if (error) - goto cleanup; + val = fz_newint(a); + fz_dictput(dict, key, val); fz_dropobj(val); fz_dropobj(key); - key = val = nil; goto skip; } if (tok == PDF_TINT) @@ -303,39 +334,29 @@ skip: b = atoi(buf); error = pdf_lex(&tok, file, buf, cap, &len); if (error) - goto cleanup; + { + fz_dropobj(key); + fz_dropobj(dict); + return fz_rethrow(error, "cannot parse dict"); + } if (tok == PDF_TR) { - error = fz_newindirect(&val, a, b, xref); + val = fz_newindirect(a, b, xref); break; } } - error = fz_throw("invalid indirect reference in dict"); - goto cleanup; + fz_dropobj(key); + fz_dropobj(dict); + return fz_throw("invalid indirect reference in dict"); default: - error = fz_throw("unknown token in dict"); - goto cleanup; + return fz_throw("unknown token in dict"); } - if (error) - goto cleanup; - - error = fz_dictput(dict, key, val); - if (error) - goto cleanup; - + fz_dictput(dict, key, val); fz_dropobj(val); fz_dropobj(key); - key = val = nil; } - -cleanup: - if (key) fz_dropobj(key); - if (val) fz_dropobj(val); - if (dict) fz_dropobj(dict); - *op = nil; - return fz_rethrow(error, "cannot parse dict"); } fz_error @@ -351,20 +372,26 @@ pdf_parsestmobj(fz_obj **op, pdf_xref *xref, fz_stream *file, char *buf, int cap switch (tok) { - case PDF_TOARRAY: error = pdf_parsearray(op, xref, file, buf, cap); break; - case PDF_TODICT: error = pdf_parsedict(op, xref, file, buf, cap); break; - case PDF_TNAME: error = fz_newname(op, buf); break; - case PDF_TREAL: error = fz_newreal(op, atof(buf)); break; - case PDF_TSTRING: error = fz_newstring(op, buf, len); break; - case PDF_TTRUE: error = fz_newbool(op, 1); break; - case PDF_TFALSE: error = fz_newbool(op, 0); break; - case PDF_TNULL: error = fz_newnull(op); break; - case PDF_TINT: error = fz_newint(op, atoi(buf)); break; + case PDF_TOARRAY: + error = pdf_parsearray(op, xref, file, buf, cap); + if (error) + return fz_rethrow(error, "cannot parse object stream"); + break; + case PDF_TODICT: + error = pdf_parsedict(op, xref, file, buf, cap); + if (error) + return fz_rethrow(error, "cannot parse object stream"); + break; + case PDF_TNAME: *op = fz_newname(buf); break; + case PDF_TREAL: *op = fz_newreal(atof(buf)); break; + case PDF_TSTRING: *op = fz_newstring(buf, len); break; + case PDF_TTRUE: *op = fz_newbool(1); break; + case PDF_TFALSE: *op = fz_newbool(0); break; + case PDF_TNULL: *op = fz_newnull(); break; + case PDF_TINT: *op = fz_newint(atoi(buf)); break; default: return fz_throw("unknown token in object stream"); } - if (error) - return fz_rethrow(error, "cannot parse object stream"); return fz_okay; } @@ -381,44 +408,58 @@ pdf_parseindobj(fz_obj **op, pdf_xref *xref, int a, b; error = pdf_lex(&tok, file, buf, cap, &len); - if (error || tok != PDF_TINT) - goto cleanup; + if (error) + return fz_rethrow(error, "cannot parse indirect object (%d %d R)", num, gen); + if (tok != PDF_TINT) + return fz_throw("cannot parse indirect object (%d %d R)", num, gen); num = atoi(buf); error = pdf_lex(&tok, file, buf, cap, &len); - if (error || tok != PDF_TINT) - goto cleanup; + if (error) + return fz_rethrow(error, "cannot parse indirect object (%d %d R)", num, gen); + if (tok != PDF_TINT) + return fz_throw("cannot parse indirect object (%d %d R)", num, gen); gen = atoi(buf); error = pdf_lex(&tok, file, buf, cap, &len); - if (error || tok != PDF_TOBJ) - goto cleanup; + if (error) + return fz_rethrow(error, "cannot parse indirect object (%d %d R)", num, gen); + if (tok != PDF_TOBJ) + return fz_throw("cannot parse indirect object (%d %d R)", num, gen); error = pdf_lex(&tok, file, buf, cap, &len); if (error) - goto cleanup; + return fz_rethrow(error, "cannot parse indirect object (%d %d R)", num, gen); switch (tok) { - case PDF_TOARRAY: error = pdf_parsearray(&obj, xref, file, buf, cap); break; - case PDF_TODICT: error = pdf_parsedict(&obj, xref, file, buf, cap); break; - case PDF_TNAME: error = fz_newname(&obj, buf); break; - case PDF_TREAL: error = fz_newreal(&obj, atof(buf)); break; - case PDF_TSTRING: error = fz_newstring(&obj, buf, len); break; - case PDF_TTRUE: error = fz_newbool(&obj, 1); break; - case PDF_TFALSE: error = fz_newbool(&obj, 0); break; - case PDF_TNULL: error = fz_newnull(&obj); break; + case PDF_TOARRAY: + error = pdf_parsearray(&obj, xref, file, buf, cap); + if (error) + return fz_rethrow(error, "cannot parse indirect object (%d %d R)", num, gen); + break; + + case PDF_TODICT: + error = pdf_parsedict(&obj, xref, file, buf, cap); + if (error) + return fz_rethrow(error, "cannot parse indirect object (%d %d R)", num, gen); + break; + + case PDF_TNAME: obj = fz_newname(buf); break; + case PDF_TREAL: obj = fz_newreal(atof(buf)); break; + case PDF_TSTRING: obj = fz_newstring(buf, len); break; + case PDF_TTRUE: obj = fz_newbool(1); break; + case PDF_TFALSE: obj = fz_newbool(0); break; + case PDF_TNULL: obj = fz_newnull(); break; case PDF_TINT: a = atoi(buf); error = pdf_lex(&tok, file, buf, cap, &len); if (error) - goto cleanup; + return fz_rethrow(error, "cannot parse indirect object (%d %d R)", num, gen); if (tok == PDF_TSTREAM || tok == PDF_TENDOBJ) { - error = fz_newint(&obj, a); - if (error) - goto cleanup; + obj = fz_newint(a); goto skip; } if (tok == PDF_TINT) @@ -426,28 +467,29 @@ pdf_parseindobj(fz_obj **op, pdf_xref *xref, b = atoi(buf); error = pdf_lex(&tok, file, buf, cap, &len); if (error) - goto cleanup; + return fz_rethrow(error, "cannot parse indirect object (%d %d R)", num, gen); if (tok == PDF_TR) { - error = fz_newindirect(&obj, a, b, xref); + obj = fz_newindirect(a, b, xref); break; } } - goto cleanup; + return fz_throw("cannot parse indirect object (%d %d R)", num, gen); case PDF_TENDOBJ: - error = fz_newnull(&obj); + obj = fz_newnull(); goto skip; default: - goto cleanup; + return fz_throw("cannot parse indirect object (%d %d R)", num, gen); } - if (error) - goto cleanup; error = pdf_lex(&tok, file, buf, cap, &len); if (error) - goto cleanup; + { + fz_dropobj(obj); + return fz_rethrow(error, "cannot parse indirect object (%d %d R)", num, gen); + } skip: if (tok == PDF_TSTREAM) @@ -463,7 +505,10 @@ skip: } error = fz_readerror(file); if (error) - goto cleanup; + { + fz_dropobj(obj); + return fz_rethrow(error, "cannot parse indirect object (%d %d R)", num, gen); + } stmofs = fz_tell(file); } else if (tok == PDF_TENDOBJ) @@ -481,12 +526,5 @@ skip: if (ostmofs) *ostmofs = stmofs; *op = obj; return fz_okay; - -cleanup: - if (obj) - fz_dropobj(obj); - if (error) - return fz_rethrow(error, "cannot parse indirect object (%d %d R)", num, gen); - return fz_throw("cannot parse indirect object (%d %d R)", num, gen); } diff --git a/mupdf/pdf_pattern.c b/mupdf/pdf_pattern.c index 85beb893..9f768151 100644 --- a/mupdf/pdf_pattern.c +++ b/mupdf/pdf_pattern.c @@ -70,12 +70,7 @@ pdf_loadpattern(pdf_pattern **patp, pdf_xref *xref, fz_obj *dict) pat->matrix.e, pat->matrix.f); /* Store pattern now, to avoid possible recursion if objects refer back to this one */ - error = pdf_storeitem(xref->store, PDF_KPATTERN, dict, pat); - if (error) - { - pdf_droppattern(pat); - return fz_rethrow(error, "cannot store pattern resource"); - } + pdf_storeitem(xref->store, PDF_KPATTERN, dict, pat); /* * Locate resources diff --git a/mupdf/pdf_repair.c b/mupdf/pdf_repair.c index 255a31ae..3f004e5b 100644 --- a/mupdf/pdf_repair.c +++ b/mupdf/pdf_repair.c @@ -318,24 +318,10 @@ pdf_repairxref(pdf_xref *xref, char *filename) goto cleanup; } - error = fz_newint(&length, list[i].stmlen); - if (error) - { - fz_dropobj(dict); - error = fz_rethrow(error, "cannot create integer object"); - goto cleanup; - } - - error = fz_dictputs(dict, "Length", length); - if (error) - { - fz_dropobj(length); - fz_dropobj(dict); - error = fz_rethrow(error, "cannot update stream length"); - goto cleanup; - } - + length = fz_newint(list[i].stmlen); + fz_dictputs(dict, "Length", length); fz_dropobj(length); + fz_dropobj(dict); } } diff --git a/mupdf/pdf_shade.c b/mupdf/pdf_shade.c index a07ad1f1..d67bb6a7 100644 --- a/mupdf/pdf_shade.c +++ b/mupdf/pdf_shade.c @@ -305,12 +305,7 @@ pdf_loadshade(fz_shade **shadep, pdf_xref *xref, fz_obj *dict) return fz_rethrow(error, "could not load shading dictionary"); } - error = pdf_storeitem(xref->store, PDF_KSHADE, dict, *shadep); - if (error) - { - fz_dropshade(*shadep); - return fz_rethrow(error, "could not put shading dictionary in store"); - } + pdf_storeitem(xref->store, PDF_KSHADE, dict, *shadep); return fz_okay; } diff --git a/mupdf/pdf_store.c b/mupdf/pdf_store.c index bc11afaa..cf5b57f0 100644 --- a/mupdf/pdf_store.c +++ b/mupdf/pdf_store.c @@ -89,27 +89,16 @@ static void dropitem(pdf_itemkind kind, void *val) } } -fz_error -pdf_newstore(pdf_store **storep) +pdf_store * +pdf_newstore(void) { - fz_error error; pdf_store *store; store = fz_malloc(sizeof(pdf_store)); - if (!store) - return fz_rethrow(-1, "out of memory: store struct"); - - error = fz_newhash(&store->hash, 4096, sizeof(struct refkey)); - if (error) - { - fz_free(store); - return fz_rethrow(error, "cannot create hash"); - } - + store->hash = fz_newhash(4096, sizeof(struct refkey)); store->root = nil; - *storep = store; - return fz_okay; + return store; } void @@ -161,10 +150,9 @@ static void evictitem(pdf_item *item) fz_free(item); } -fz_error +void pdf_evictageditems(pdf_store *store) { - fz_error error; pdf_item *item; pdf_item *next; pdf_item *prev; @@ -175,12 +163,9 @@ pdf_evictageditems(pdf_store *store) { key = fz_hashgetkey(store->hash, i); item = fz_hashfind(store->hash, key); - if (item && item->age > itemmaxage(item->kind)) { - error = fz_hashremove(store->hash, key); - if (error) - return error; + fz_hashremove(store->hash, key); evictitem(item); } } @@ -189,7 +174,6 @@ pdf_evictageditems(pdf_store *store) for (item = store->root; item; item = next) { next = item->next; - if (item->age > itemmaxage(item->kind)) { if (!prev) @@ -201,8 +185,6 @@ pdf_evictageditems(pdf_store *store) else prev = item; } - - return fz_okay; } void @@ -222,16 +204,12 @@ pdf_agestoreditems(pdf_store *store) item->age++; } -fz_error +void pdf_storeitem(pdf_store *store, pdf_itemkind kind, fz_obj *key, void *val) { - fz_error error; pdf_item *item; item = fz_malloc(sizeof(pdf_item)); - if (!item) - return fz_rethrow(-1, "out of memory: store list node"); - item->kind = kind; item->key = fz_keepobj(key); item->val = val; @@ -248,9 +226,7 @@ pdf_storeitem(pdf_store *store, pdf_itemkind kind, fz_obj *key, void *val) refkey.oid = fz_tonum(key); refkey.gen = fz_togen(key); - error = fz_hashinsert(store->hash, &refkey, item); - if (error) - return fz_rethrow(error, "cannot insert object into hash"); + fz_hashinsert(store->hash, &refkey, item); } else { @@ -261,7 +237,6 @@ pdf_storeitem(pdf_store *store, pdf_itemkind kind, fz_obj *key, void *val) } keepitem(kind, val); - return fz_okay; } void * @@ -300,15 +275,14 @@ pdf_finditem(pdf_store *store, pdf_itemkind kind, fz_obj *key) return nil; } -fz_error +void pdf_removeitem(pdf_store *store, pdf_itemkind kind, fz_obj *key) { - fz_error error; pdf_item *item, *prev; struct refkey refkey; if (key == nil) - return fz_okay; + return; if (fz_isindirect(key)) { @@ -322,9 +296,6 @@ pdf_removeitem(pdf_store *store, pdf_itemkind kind, fz_obj *key) error = fz_hashremove(store->hash, &refkey); if (error) return fz_rethrow(error, "cannot remove item from store"); - - pdf_logrsrc("remove item %s (%d %d R) ptr=%p\n", kindstr(kind), fz_tonum(key), fz_togen(key), item->val); - dropitem(kind, item->val); fz_dropobj(item->key); fz_free(item); @@ -349,8 +320,6 @@ pdf_removeitem(pdf_store *store, pdf_itemkind kind, fz_obj *key) prev = item; } } - - return fz_okay; } void diff --git a/mupdf/pdf_xobject.c b/mupdf/pdf_xobject.c index eb9f15f8..e11424a7 100644 --- a/mupdf/pdf_xobject.c +++ b/mupdf/pdf_xobject.c @@ -24,12 +24,7 @@ pdf_loadxobject(pdf_xobject **formp, pdf_xref *xref, fz_obj *dict) /* Store item immediately, to avoid infinite recursion if contained objects refer again to this xobject */ - error = pdf_storeitem(xref->store, PDF_KXOBJECT, dict, form); - if (error) - { - pdf_dropxobject(form); - return fz_rethrow(error, "cannot store xobject resource"); - } + pdf_storeitem(xref->store, PDF_KXOBJECT, dict, form); pdf_logrsrc("load xobject (%d %d R) ptr=%p {\n", fz_tonum(dict), fz_togen(dict), form); diff --git a/mupdf/pdf_xref.c b/mupdf/pdf_xref.c index 1e2147ea..d79ec663 100644 --- a/mupdf/pdf_xref.c +++ b/mupdf/pdf_xref.c @@ -180,9 +180,7 @@ pdf_cacheobject(pdf_xref *xref, int oid, int gen) if (x->type == 'f' || x->type == 'd') { - error = fz_newnull(&x->obj); - if (error) - return fz_rethrow(error, "cannot create null for empty object slot"); + x->obj = fz_newnull(); return fz_okay; } @@ -230,10 +228,7 @@ pdf_loadobject(fz_obj **objp, pdf_xref *xref, int oid, int gen) else { fz_warn("cannot load missing object (%d %d R), assuming null object", oid, gen); - - error = fz_newnull(&xref->table[oid].obj); - if (error) - return fz_rethrow(error, "cannot create missing object (%d %d R)", oid, gen); + xref->table[oid].obj = fz_newnull(); } return fz_okay; |