shithub: pdffs

Download patch

ref: b051539715907b60f5ccb8570f4665ad45b3fbaf
parent: ee088488b4e8adc5cdb7ba02d7906d3f370a453c
author: Noam Preil <noam@pixelhero.dev>
date: Tue Jun 1 13:16:40 EDT 2021

[array] split out arrayadd

--- a/array.c
+++ b/array.c
@@ -8,7 +8,6 @@
 pdfarray(Pdf *pdf, Stream *s)
 {
 	Object *o, *m;
-	Object **a;
 	int c, noel;
 
 	o = calloc(1, sizeof(*o));
@@ -30,13 +29,10 @@
 			continue;
 		}
 
-		if((a = realloc(o->array.e, (o->array.ne+1)*sizeof(Object*))) == nil){
+		if(!arrayadd(o, m)){
 			pdfobjfree(m);
 			goto err;
 		}
-
-		o->array.e = a;
-		a[o->array.ne++] = m;
 	}
 
 	if(c != ']'){
@@ -49,6 +45,19 @@
 	werrstr("array: %r");
 	pdfobjfree(o);
 	return nil;
+}
+
+int
+arrayadd(Object *arr, Object *o)
+{
+	Object **a;
+	a = realloc(arr->array.e, (arr->array.ne+1)*sizeof(Object*));
+	if(a == nil)
+		return 0;
+	arr->array.e = a;
+	a[arr->array.ne] = o;
+	arr->array.ne += 1;
+	return 1;
 }
 
 int
--- a/pdf.h
+++ b/pdf.h
@@ -190,6 +190,7 @@
 int arraylen(Object *o);
 Object *arrayget(Object *o, int i);
 int arrayint(Object *o, int i);
+int arrayadd(Object *a, Object *o);
 
 Object *dictget(Object *o, char *name);
 int dictint(Object *o, char *name);