summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2017-12-24 22:32:59 +0000
committerVincent Sanders <vince@kyllikki.org>2017-12-24 22:32:59 +0000
commitfb3cf89cbbf5ba7a2844f6016a88c6c4429ecda1 (patch)
treedc83f8c4208a58148e78d6e4e0c5916435b80ba2
parent4a0316bbd73df526e42207cda82a12a79da4abaf (diff)
downloadlibnspdf-fb3cf89cbbf5ba7a2844f6016a88c6c4429ecda1.tar.gz
libnspdf-fb3cf89cbbf5ba7a2844f6016a88c6c4429ecda1.tar.bz2
decode indirect objects
-rw-r--r--src/cos_decode.c94
-rw-r--r--src/cos_object.c1
-rw-r--r--src/pdf_doc.c2
3 files changed, 71 insertions, 26 deletions
diff --git a/src/cos_decode.c b/src/cos_decode.c
index a1ca318..8873060 100644
--- a/src/cos_decode.c
+++ b/src/cos_decode.c
@@ -396,7 +396,7 @@ cos_decode_list(struct pdf_doc *doc,
return res;
}
- printf("found a list\n");
+ //printf("found a list\n");
cosobj = calloc(1, sizeof(struct cos_object));
if (cosobj == NULL) {
@@ -647,13 +647,12 @@ cos_attempt_decode_reference(struct pdf_doc *doc,
nspdferror res;
uint64_t offset;
uint8_t c;
- struct cos_object *generation; /* generation object, reused for output */
- struct cos_reference *nref; /* new reference */
+ struct cos_object *generation; /* generation object */
offset = *offset_out;
res = cos_decode_number(doc, &offset, &generation);
- if (res != 0) {
+ if (res != NSPDFERROR_OK) {
/* no error if next token could not be decoded as a number */
return NSPDFERROR_OK;
}
@@ -673,36 +672,81 @@ cos_attempt_decode_reference(struct pdf_doc *doc,
}
/* two int in a row, look for the R */
- c = DOC_BYTE(doc, offset++);
- if (c != 'R') {
- /* no R so not a reference */
- cos_free_object(generation);
- return NSPDFERROR_OK;
- }
+ c = DOC_BYTE(doc, offset);
+ if (c == 'R') {
+ struct cos_reference *nref; /* new reference */
- /* found reference */
+ //printf("found object reference\n");
+ offset ++;
- //printf("found reference\n");
- doc_skip_ws(doc, &offset);
+ doc_skip_ws(doc, &offset);
- nref = calloc(1, sizeof(struct cos_reference));
- if (nref == NULL) {
- /** \todo free objects */
- return NSPDFERROR_NOMEM; /* memory error */
- }
+ nref = calloc(1, sizeof(struct cos_reference));
+ if (nref == NULL) {
+ cos_free_object(generation);
+ return NSPDFERROR_NOMEM; /* memory error */
+ }
- nref->id = (*cosobj_out)->u.i;
- nref->generation = generation->u.i;
+ nref->id = (*cosobj_out)->u.i;
+ nref->generation = generation->u.i;
- cos_free_object(*cosobj_out);
+ /* overwrite input object for output (it has to be an int which has no
+ * allocation to free)
+ */
+ (*cosobj_out)->type = COS_TYPE_REFERENCE;
+ (*cosobj_out)->u.reference = nref;
- generation->type = COS_TYPE_REFERENCE;
- generation->u.reference = nref;
+ *offset_out = offset;
- *cosobj_out = generation;
+ } else if ((c == 'o') &&
+ (DOC_BYTE(doc, offset + 1) == 'b') &&
+ (DOC_BYTE(doc, offset + 2) == 'j')) {
+ struct cos_object *indirect; /* indirect object */
+ //printf("indirect\n");
+ offset += 3;
- *offset_out = offset;
+ res = doc_skip_ws(doc, &offset);
+ if (res != NSPDFERROR_OK) {
+ cos_free_object(generation);
+ return res;
+ }
+ //printf("decoding\n");
+
+ res = cos_decode_object(doc, &offset, &indirect);
+ if (res != NSPDFERROR_OK) {
+ cos_free_object(generation);
+ return res;
+ }
+ //printf("parsed object type %d\nendobj\n",indirect->type);
+
+ if ((DOC_BYTE(doc, offset ) != 'e') &&
+ (DOC_BYTE(doc, offset + 1) != 'n') &&
+ (DOC_BYTE(doc, offset + 2) != 'd') &&
+ (DOC_BYTE(doc, offset + 1) != 'o') &&
+ (DOC_BYTE(doc, offset + 2) != 'b') &&
+ (DOC_BYTE(doc, offset + 3) != 'j')) {
+ cos_free_object(indirect);
+ cos_free_object(generation);
+ return NSPDFERROR_SYNTAX;
+ }
+ offset += 6;
+ //printf("skipping\n");
+
+ res = doc_skip_ws(doc, &offset);
+ if (res != NSPDFERROR_OK) {
+ cos_free_object(indirect);
+ cos_free_object(generation);
+ return res;
+ }
+
+ cos_free_object(*cosobj_out);
+
+ *cosobj_out = indirect;
+
+ *offset_out = offset;
+ }
+ cos_free_object(generation);
return NSPDFERROR_OK;
}
diff --git a/src/cos_object.c b/src/cos_object.c
index 3abe7e8..5ec41a9 100644
--- a/src/cos_object.c
+++ b/src/cos_object.c
@@ -59,6 +59,7 @@ nspdferror cos_free_object(struct cos_object *cos_obj)
aentry = aentry->next;
free(oaentry);
}
+ break;
case COS_TYPE_STREAM:
free(cos_obj->u.stream);
diff --git a/src/pdf_doc.c b/src/pdf_doc.c
index 7bba54f..4a5cad1 100644
--- a/src/pdf_doc.c
+++ b/src/pdf_doc.c
@@ -99,7 +99,7 @@ xref_get_referenced(struct pdf_doc *doc, struct cos_object **cobj_out)
entry->object = indirect;
}
- cobj = entry->object;
+ *cobj_out = entry->object;
return NSPDFERROR_OK;
}