summaryrefslogtreecommitdiff
path: root/source/pdf/pdf-repair.c
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2013-06-24 21:01:27 +0100
committerRobin Watts <robin.watts@artifex.com>2013-06-25 10:05:25 +0100
commit07dd8540ae72b0e7be64d04c38d8c19b83d184b6 (patch)
treedfaa48e8eeeb46fac17f8eb0cb8f226da9b841ca /source/pdf/pdf-repair.c
parent8a2b650a9d3910d9b5104b1d2560b6a34bf7583e (diff)
downloadmupdf-07dd8540ae72b0e7be64d04c38d8c19b83d184b6.tar.xz
Update pdf_obj's to have a pdf_document field.
Remove the fz_context field to avoid the structure growing.
Diffstat (limited to 'source/pdf/pdf-repair.c')
-rw-r--r--source/pdf/pdf-repair.c71
1 files changed, 36 insertions, 35 deletions
diff --git a/source/pdf/pdf-repair.c b/source/pdf/pdf-repair.c
index 86888089..8d50bb65 100644
--- a/source/pdf/pdf-repair.c
+++ b/source/pdf/pdf-repair.c
@@ -15,11 +15,12 @@ struct entry
};
static int
-pdf_repair_obj(fz_stream *file, pdf_lexbuf *buf, int *stmofsp, int *stmlenp, pdf_obj **encrypt, pdf_obj **id, int *tmpofs)
+pdf_repair_obj(pdf_document *doc, pdf_lexbuf *buf, int *stmofsp, int *stmlenp, pdf_obj **encrypt, pdf_obj **id, int *tmpofs)
{
pdf_token tok;
int stm_len;
int n;
+ fz_stream *file = doc->file;
fz_context *ctx = file->ctx;
*stmofsp = 0;
@@ -40,7 +41,7 @@ pdf_repair_obj(fz_stream *file, pdf_lexbuf *buf, int *stmofsp, int *stmlenp, pdf
/* Send NULL xref so we don't try to resolve references */
fz_try(ctx)
{
- dict = pdf_parse_dict(NULL, file, buf);
+ dict = pdf_parse_dict(doc, file, buf);
}
fz_catch(ctx)
{
@@ -49,7 +50,7 @@ pdf_repair_obj(fz_stream *file, pdf_lexbuf *buf, int *stmofsp, int *stmlenp, pdf
if (file->eof)
fz_rethrow_message(ctx, "broken object at EOF ignored");
/* Silently swallow the error */
- dict = pdf_new_dict(ctx, 2);
+ dict = pdf_new_dict(doc, 2);
}
obj = pdf_dict_gets(dict, "Type");
@@ -223,7 +224,7 @@ pdf_repair_obj_stm(pdf_document *xref, int num, int gen)
/* Entered with file locked, remains locked throughout. */
void
-pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
+pdf_repair_xref(pdf_document *doc, pdf_lexbuf *buf)
{
pdf_obj *dict, *obj = NULL;
pdf_obj *length;
@@ -245,7 +246,7 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
pdf_token tok;
int next;
int i, n, c;
- fz_context *ctx = xref->ctx;
+ fz_context *ctx = doc->ctx;
fz_var(encrypt);
fz_var(id);
@@ -254,9 +255,9 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
fz_var(list);
fz_var(obj);
- xref->dirty = 1;
+ doc->dirty = 1;
- fz_seek(xref->file, 0, 0);
+ fz_seek(doc->file, 0, 0);
fz_try(ctx)
{
@@ -266,36 +267,36 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
list = fz_malloc_array(ctx, listcap, sizeof(struct entry));
/* look for '%PDF' version marker within first kilobyte of file */
- n = fz_read(xref->file, (unsigned char *)buf->scratch, fz_mini(buf->size, 1024));
+ n = fz_read(doc->file, (unsigned char *)buf->scratch, fz_mini(buf->size, 1024));
if (n < 0)
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot read from file");
- fz_seek(xref->file, 0, 0);
+ fz_seek(doc->file, 0, 0);
for (i = 0; i < n - 4; i++)
{
if (memcmp(&buf->scratch[i], "%PDF", 4) == 0)
{
- fz_seek(xref->file, i + 8, 0); /* skip "%PDF-X.Y" */
+ fz_seek(doc->file, i + 8, 0); /* skip "%PDF-X.Y" */
break;
}
}
/* skip comment line after version marker since some generators
* forget to terminate the comment with a newline */
- c = fz_read_byte(xref->file);
+ c = fz_read_byte(doc->file);
while (c >= 0 && (c == ' ' || c == '%'))
- c = fz_read_byte(xref->file);
- fz_unread_byte(xref->file);
+ c = fz_read_byte(doc->file);
+ fz_unread_byte(doc->file);
while (1)
{
- tmpofs = fz_tell(xref->file);
+ tmpofs = fz_tell(doc->file);
if (tmpofs < 0)
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot tell in file");
fz_try(ctx)
{
- tok = pdf_lex(xref->file, buf);
+ tok = pdf_lex(doc->file, buf);
}
fz_catch(ctx)
{
@@ -321,7 +322,7 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
{
fz_try(ctx)
{
- tok = pdf_repair_obj(xref->file, buf, &stm_ofs, &stm_len, &encrypt, &id, &tmpofs);
+ tok = pdf_repair_obj(doc, buf, &stm_ofs, &stm_len, &encrypt, &id, &tmpofs);
}
fz_catch(ctx)
{
@@ -367,7 +368,7 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
{
fz_try(ctx)
{
- dict = pdf_parse_dict(xref, xref->file, buf);
+ dict = pdf_parse_dict(doc, doc->file, buf);
}
fz_catch(ctx)
{
@@ -414,7 +415,7 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
}
else if (tok == PDF_TOK_ERROR)
- fz_read_byte(xref->file);
+ fz_read_byte(doc->file);
else if (tok == PDF_TOK_EOF)
break;
@@ -426,11 +427,11 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
Dummy access to entry to assure sufficient space in the xref table
and avoid repeated reallocs in the loop
*/
- (void)pdf_get_populating_xref_entry(xref, maxnum);
+ (void)pdf_get_populating_xref_entry(doc, maxnum);
for (i = 0; i < listlen; i++)
{
- entry = pdf_get_populating_xref_entry(xref, list[i].num);
+ entry = pdf_get_populating_xref_entry(doc, list[i].num);
entry->type = 'n';
entry->ofs = list[i].ofs;
entry->gen = list[i].gen;
@@ -440,9 +441,9 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
/* correct stream length for unencrypted documents */
if (!encrypt && list[i].stm_len >= 0)
{
- dict = pdf_load_object(xref, list[i].num, list[i].gen);
+ dict = pdf_load_object(doc, list[i].num, list[i].gen);
- length = pdf_new_int(ctx, list[i].stm_len);
+ length = pdf_new_int(doc, list[i].stm_len);
pdf_dict_puts(dict, "Length", length);
pdf_drop_obj(length);
@@ -450,7 +451,7 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
}
}
- entry = pdf_get_populating_xref_entry(xref, 0);
+ entry = pdf_get_populating_xref_entry(doc, 0);
entry->type = 'f';
entry->ofs = 0;
entry->gen = 65535;
@@ -458,9 +459,9 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
entry->obj = NULL;
next = 0;
- for (i = pdf_xref_len(xref) - 1; i >= 0; i--)
+ for (i = pdf_xref_len(doc) - 1; i >= 0; i--)
{
- entry = pdf_get_populating_xref_entry(xref, i);
+ entry = pdf_get_populating_xref_entry(doc, i);
if (entry->type == 'f')
{
entry->ofs = next;
@@ -472,26 +473,26 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
/* create a repaired trailer, Root will be added later */
- obj = pdf_new_dict(ctx, 5);
+ obj = pdf_new_dict(doc, 5);
/* During repair there is only a single xref section */
- pdf_set_populating_xref_trailer(xref, obj);
+ pdf_set_populating_xref_trailer(doc, obj);
pdf_drop_obj(obj);
obj = NULL;
- obj = pdf_new_int(ctx, maxnum + 1);
- pdf_dict_puts(pdf_trailer(xref), "Size", obj);
+ obj = pdf_new_int(doc, maxnum + 1);
+ pdf_dict_puts(pdf_trailer(doc), "Size", obj);
pdf_drop_obj(obj);
obj = NULL;
if (root)
{
- pdf_dict_puts(pdf_trailer(xref), "Root", root);
+ pdf_dict_puts(pdf_trailer(doc), "Root", root);
pdf_drop_obj(root);
root = NULL;
}
if (info)
{
- pdf_dict_puts(pdf_trailer(xref), "Info", info);
+ pdf_dict_puts(pdf_trailer(doc), "Info", info);
pdf_drop_obj(info);
info = NULL;
}
@@ -501,12 +502,12 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
if (pdf_is_indirect(encrypt))
{
/* create new reference with non-NULL xref pointer */
- obj = pdf_new_indirect(ctx, pdf_to_num(encrypt), pdf_to_gen(encrypt), xref);
+ obj = pdf_new_indirect(doc, pdf_to_num(encrypt), pdf_to_gen(encrypt));
pdf_drop_obj(encrypt);
encrypt = obj;
obj = NULL;
}
- pdf_dict_puts(pdf_trailer(xref), "Encrypt", encrypt);
+ pdf_dict_puts(pdf_trailer(doc), "Encrypt", encrypt);
pdf_drop_obj(encrypt);
encrypt = NULL;
}
@@ -516,12 +517,12 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf)
if (pdf_is_indirect(id))
{
/* create new reference with non-NULL xref pointer */
- obj = pdf_new_indirect(ctx, pdf_to_num(id), pdf_to_gen(id), xref);
+ obj = pdf_new_indirect(doc, pdf_to_num(id), pdf_to_gen(id));
pdf_drop_obj(id);
id = obj;
obj = NULL;
}
- pdf_dict_puts(pdf_trailer(xref), "ID", id);
+ pdf_dict_puts(pdf_trailer(doc), "ID", id);
pdf_drop_obj(id);
id = NULL;
}