shithub: pdffs

Download patch

ref: ac1954db2882d7e6ff01eb490e68de7294424b4d
parent: 7c2e795156b5d969bdc807da910a35aa3afd315a
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Fri Nov 20 18:12:20 EST 2020

sort out integers and store the top of the document, not just the root

--- a/array.c
+++ b/array.c
@@ -72,5 +72,5 @@
 int
 arrayint(Object *o, int i)
 {
-	return (o = arrayget(o, i))->type == Onum ? o->num : 0;
+	return (o = arrayget(o, i))->type == Onum ? o->num.i : 0;
 }
--- a/dict.c
+++ b/dict.c
@@ -82,13 +82,19 @@
 	return &null;
 }
 
-vlong
-dictint(Object *o, char *name)
+int
+dictintopt(Object *o, char *name, int def)
 {
 	o = dictget(o, name);
-	return o->type == Onum ? o->num : 0;
+	return o->type == Onum ? o->num.i : def;
 }
 
+int
+dictint(Object *o, char *name)
+{
+	return dictintopt(o, name, 0);
+}
+
 char *
 dictstring(Object *o, char *name)
 {
@@ -120,7 +126,7 @@
 			werrstr("not an integer array");
 			return -1;
 		}
-		el[n] = v->num;
+		el[n] = v->num.i;
 	}
 
 	return n;
--- a/filter.c
+++ b/filter.c
@@ -11,6 +11,7 @@
 static int
 fCopy(void *aux, Buffer *bi, Buffer *bo)
 {
+	USED(aux);
 	bufput(bo, bi->b, bi->sz);
 	return 0;
 }
--- a/main.c
+++ b/main.c
@@ -37,16 +37,20 @@
 		sysfatal("%r");
 	if((pdf = pdfopen(b)) == nil)
 		sysfatal("%s: %r", argv[0]);
-	for(v = pdf->root, i = 1; i < argc; i++){
+	for(v = pdf->top, i = 1; v != nil && i < argc; i++){
 		if(argv[i][0] == '['){
 			n = atoi(argv[i]+1);
 			v = arrayget(v, n);
-		}else if((argv[i][0] == '@') && argv[i][1] == 0 && v->type == Ostream){
+		}else if(argv[i][0] == '.' && argv[i][1] == 0 && v->type == Ostream){
 			if((s = Sopen(v)) == nil)
 				sysfatal("%r");
 			if(write(1, s->buf.b, s->buf.sz) != s->buf.sz)
 				sysfatal("write failed");
 			Sclose(s);
+			v = nil;
+			break;
+		}else if(argv[i][0] == '@' && argv[i][1] == 0 && v->type == Ostream){
+			fprint(2, "%d %d\n", v->stream.off, v->stream.len);
 			v = nil;
 			break;
 		}else{
--- a/misc.c
+++ b/misc.c
@@ -32,7 +32,7 @@
 		return fmtprint(f, o->bool ? "true" : "false");
 
 	case Onum:
-		return fmtprint(f, "%g", o->num);
+		return fmtprint(f, "%g", o->num.d);
 
 	case Ostr:
 		if(isutf8(o->str, o->len))
--- a/object.c
+++ b/object.c
@@ -119,7 +119,8 @@
 		if((o = calloc(1, sizeof(*o))) == nil)
 			goto err;
 		o->pdf = pdf;
-		Sgetd(s, &o->num); /* get the first number */
+		Sgetd(s, &o->num.d); /* get the first number */
+		o->num.i = o->num.d;
 		off = Soffset(s); /* seek here if not an indirect object later */
 
 		if((o2 = pdfobj(pdf, s)) != nil && o2->type == Onum){ /* second object is number too */
@@ -128,14 +129,14 @@
 				goto err;
 			if(c == 'R'){ /* indirect object */
 				o->type = Oindir;
-				o->indir.id = o->num;
-				o->indir.gen = o2->num;
+				o->indir.id = o->num.i;
+				o->indir.gen = o2->num.i;
 				pdfobjfree(o2);
 				return o;
 			}
 			if(c == 'o' && Sgetc(s) == 'b' && Sgetc(s) == 'j'){ /* object */
-				xref.id = o->num;
-				xref.gen = o2->num;
+				xref.id = o->num.i;
+				xref.gen = o2->num.i;
 				/* FIXME put into a map */
 				pdfobjfree(o2);
 				if((o2 = pdfobj(pdf, s)) != nil){
--- a/pdf.c
+++ b/pdf.c
@@ -20,7 +20,7 @@
 
 	pdf->root = pdfref(dictget(o, "Root"));
 	pdf->info = pdfref(dictget(o, "Info"));
-	pdfobjfree(o);
+	pdf->top = o;
 
 	return 0;
 err:
--- a/pdf.h
+++ b/pdf.h
@@ -41,8 +41,13 @@
 	Pdf *pdf;
 	union {
 		int bool;
-		double num;
+
 		struct {
+			double d;
+			int i;
+		}num;
+
+		struct {
 			char *str;
 			int len;
 		};
@@ -82,6 +87,7 @@
 	Xref *xref;
 	int nxref; /* 7.5.4 xref subsection number of objects */
 
+	Object *top;
 	Object *root; /* 7.7.2 root object */
 	Object *info; /* 14.3.3 info dictionary */
 };
@@ -155,7 +161,8 @@
 int arrayint(Object *o, int i);
 
 Object *dictget(Object *o, char *name);
-vlong dictint(Object *o, char *name);
+int dictint(Object *o, char *name);
+int dictintopt(Object *o, char *name, int def);
 char *dictstring(Object *o, char *name);
 Object *dictdict(Object *o, char *name);
 int dictints(Object *o, char *name, int *el, int nel);
--- a/xref.c
+++ b/xref.c
@@ -193,7 +193,7 @@
 		pdf->root = pdfref(p);
 	if((p = dictget(o, "Info")) != &null)
 		pdf->info = pdfref(p);
-	pdfobjfree(o);
+	pdf->top = o;
 
 	return 0;
 err: