summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/mupdf/pdf/object.h4
-rw-r--r--source/pdf/pdf-object.c42
-rw-r--r--source/pdf/pdf-write.c4
3 files changed, 44 insertions, 6 deletions
diff --git a/include/mupdf/pdf/object.h b/include/mupdf/pdf/object.h
index 88d5c24b..de899b80 100644
--- a/include/mupdf/pdf/object.h
+++ b/include/mupdf/pdf/object.h
@@ -76,7 +76,9 @@ pdf_obj *pdf_array_get(pdf_obj *array, int i);
void pdf_array_put(pdf_obj *array, int i, pdf_obj *obj);
void pdf_array_push(pdf_obj *array, pdf_obj *obj);
void pdf_array_push_drop(pdf_obj *array, pdf_obj *obj);
-void pdf_array_insert(pdf_obj *array, pdf_obj *obj);
+void pdf_array_insert(pdf_obj *array, pdf_obj *obj, int index);
+void pdf_array_insert_drop(pdf_obj *array, pdf_obj *obj, int index);
+void pdf_array_delete(pdf_obj *array, int index);
int pdf_array_contains(pdf_obj *array, pdf_obj *obj);
int pdf_dict_len(pdf_obj *dict);
diff --git a/source/pdf/pdf-object.c b/source/pdf/pdf-object.c
index 88af2639..c4623f0a 100644
--- a/source/pdf/pdf-object.c
+++ b/source/pdf/pdf-object.c
@@ -608,7 +608,7 @@ pdf_array_push_drop(pdf_obj *obj, pdf_obj *item)
}
void
-pdf_array_insert(pdf_obj *obj, pdf_obj *item)
+pdf_array_insert(pdf_obj *obj, pdf_obj *item, int i)
{
RESOLVE(obj);
@@ -618,16 +618,52 @@ pdf_array_insert(pdf_obj *obj, pdf_obj *item)
fz_warn(obj->doc->ctx, "assert: not an array (%s)", pdf_objkindstr(obj));
else
{
+ if (i < 0 || i > obj->u.a.len)
+ fz_throw(obj->doc->ctx, FZ_ERROR_GENERIC, "attempt to insert object %d in array of length %d", i, obj->u.a.len);
if (obj->u.a.len + 1 > obj->u.a.cap)
pdf_array_grow(obj);
- memmove(obj->u.a.items + 1, obj->u.a.items, obj->u.a.len * sizeof(pdf_obj*));
- obj->u.a.items[0] = pdf_keep_obj(item);
+ memmove(obj->u.a.items + i + 1, obj->u.a.items + i, (obj->u.a.len - i) * sizeof(pdf_obj*));
+ obj->u.a.items[i] = pdf_keep_obj(item);
obj->u.a.len++;
}
object_altered(obj, item);
}
+void
+pdf_array_insert_drop(pdf_obj *obj, pdf_obj *item, int i)
+{
+ fz_context *ctx = obj->doc->ctx;
+ fz_try(ctx)
+ {
+ pdf_array_insert(obj, item, i);
+ }
+ fz_always(ctx)
+ {
+ pdf_drop_obj(item);
+ }
+ fz_catch(ctx)
+ {
+ fz_rethrow(ctx);
+ }
+}
+
+void
+pdf_array_delete(pdf_obj *obj, int i)
+{
+ if (!obj)
+ return; /* Can't warn :( */
+ if (obj->kind != PDF_ARRAY)
+ fz_warn(obj->doc->ctx, "assert: not an array (%s)", pdf_objkindstr(obj));
+ else
+ {
+ pdf_drop_obj(obj->u.a.items[i]);
+ obj->u.a.items[i] = 0;
+ obj->u.a.len--;
+ memmove(obj->u.a.items + i, obj->u.a.items + i + 1, (obj->u.a.len - i) * sizeof(pdf_obj*));
+ }
+}
+
int
pdf_array_contains(pdf_obj *arr, pdf_obj *obj)
{
diff --git a/source/pdf/pdf-write.c b/source/pdf/pdf-write.c
index 9d768d0f..00718848 100644
--- a/source/pdf/pdf-write.c
+++ b/source/pdf/pdf-write.c
@@ -1506,9 +1506,9 @@ static void addhexfilter(pdf_document *doc, pdf_obj *dict)
}
else if (pdf_is_array(f))
{
- pdf_array_insert(f, ahx);
+ pdf_array_insert(f, ahx, 0);
if (pdf_is_array(dp))
- pdf_array_insert(dp, nullobj);
+ pdf_array_insert(dp, nullobj, 0);
}
else
f = ahx;