History

move stuff around and just use Biobuf* everywhere

Sigrid Haflínudóttir committed 6 years ago

Commit ecd40a81 parent 745debc8 files · +82 −64Browse files

Unified · Split

8 files
  1. buffer.c +1 −1
  2. main.c +3 −3
  3. misc.c +29 −2
  4. object.c +3 −30
  5. pdf.c +10 −12
  6. pdf.h +28 −8
  7. stream.c +2 −2
  8. string.c +6 −6
buffer.c+1−1
@@ -58,7 +58,7 @@ bufdata(Buffer *b, int *sz) · 57 unmodified lines
5858 }
5959
6060 int
61−bufreadn(Buffer *b, void *bio, int sz)
61+bufreadn(Buffer *b, Biobuf *bio, int sz)
6262 {
6363 int n, end;
6464
main.c+3−3
@@ -17,8 +17,8 @@ usage(void) · 16 unmodified lines
1717 void
1818 threadmain(int argc, char **argv)
1919 {
20− int fd;
2120 Pdf *pdf;
21+ Biobuf *b;
2222 Object *v;
2323
2424 quotefmtinstall();
@@ -43,9 +43,9 @@ threadmain(int argc, char **argv) · 18 unmodified lines
4343
4444 if(argc != 1)
4545 usage();
46− if((fd = open(argv[0], OREAD)) < 0)
46+ if((b = Bopen(argv[0], OREAD)) == nil)
4747 sysfatal("%r");
48− if((pdf = pdfopen(fd)) == nil)
48+ if((pdf = pdfopen(b)) == nil)
4949 sysfatal("%s: %r", argv[0]);
5050 if((v = dictget(pdf->info, "Creator")) != nil)
5151 fprint(2, "creator: %s\n", v->str);
misc.c+29−2
@@ -3,7 +3,35 @@ · 2 unmodified lines
33 #include <bio.h>
44 #include "pdf.h"
55
6−/* 7.2.2 whitespace */
6+static char *otypes[] = {
7+ [Obool] = "bool",
8+ [Onum] = "num",
9+ [Ostr] = "str",
10+ [Oname] = "name",
11+ [Oarray] = "array",
12+ [Odict] = "dict",
13+ [Ostream] = "stream",
14+ [Onull] = "null",
15+ [Oindir] = "indir",
16+};
17+
18+Object null = {
19+ .type = Onull,
20+};
21+
22+int
23+Tfmt(Fmt *f)
24+{
25+ Object *o;
26+
27+ o = va_arg(f->args, Object*);
28+ if(o == nil || o == &null)
29+ return fmtprint(f, "null");
30+ if(o->type < 0 || o->type >= nelem(otypes))
31+ return fmtprint(f, "????");
32+ return fmtprint(f, "%s", otypes[o->type]);
33+}
34+
735 int
836 isws(int c)
937 {
@@ -12,7 +40,6 @@ isws(int c) · 2 unmodified lines
1240 c == ' ';
1341 }
1442
15−/* 7.2.2 delimeters */
1643 int
1744 isdelim(int c)
1845 {
object.c+3−30
@@ -9,38 +9,9 @@ Object *pdfname(Biobuf *b); · 8 unmodified lines
99 Object *pdfarray(Pdf *pdf, Biobuf *b);
1010 Object *pdfdict(Pdf *pdf, Biobuf *b);
1111
12−Object null = {
13− .type = Onull,
14−};
15−
16−static char *otypes[] = {
17− [Obool] = "bool",
18− [Onum] = "num",
19− [Ostr] = "str",
20− [Oname] = "name",
21− [Oarray] = "array",
22− [Odict] = "dict",
23− [Ostream] = "stream",
24− [Onull] = "null",
25− [Oindir] = "indir",
26−};
27−
28−int
29−Tfmt(Fmt *f)
30−{
31− Object *o;
32−
33− o = va_arg(f->args, Object*);
34− if(o == nil || o == &null)
35− return fmtprint(f, "null");
36− if(o->type < 0 || o->type >= nelem(otypes))
37− return fmtprint(f, "????");
38− return fmtprint(f, "%s", otypes[o->type]);
39−}
40−
4112 /* General function to parse an object of any type. */
4213 Object *
43−pdfobj(Pdf *pdf, void *b)
14+pdfobj(Pdf *pdf, Biobuf *b)
4415 {
4516 Object *o, *o2;
4617 vlong off;
@@ -61,6 +32,8 @@ pdfobj(Pdf *pdf, void *b) · 14 unmodified lines
6132 if((o = pdfdict(pdf, b)) != nil){
6233 /* check for attached stream */
6334 off = Boffset(b);
35+ do; while(isws(Bgetc(b)));
36+ Bungetc(b);
6437 if(Bread(b, s, 7) == 7 && memcmp(s, "stream", 6) == 0 && isws(c = s[6])){
6538 /* there IS a stream */
6639 if(c == '\r' && (c = Bgetc(b)) < 0)
pdf.c+10−12
@@ -4,6 +4,8 @@ · 3 unmodified lines
44 #include <ctype.h>
55 #include "pdf.h"
66
7+int Tfmt(Fmt *f);
8+
79 /*
810 * pre-1.5 xref section reader
911 * PDF>=1.5 may have BOTH (or either) old xref format and xref streams
@@ -96,10 +98,9 @@ err: · 86 unmodified lines
9698 }
9799
98100 Pdf *
99−pdfopen(int fd)
101+pdfopen(Biobuf *b)
100102 {
101103 Pdf *pdf;
102− Biobuf *b;
103104 Object *o;
104105 char tmp[64], *s, *x;
105106 int xref0; /* 7.5.4 xref subsection first object number */
@@ -110,9 +111,8 @@ pdfopen(int fd) · 4 unmodified lines
110111
111112 fmtinstall('T', Tfmt);
112113
113− b = nil;
114114 o = nil;
115− if((pdf = calloc(1, sizeof(*pdf))) == nil || (b = Bfdopen(fd, OREAD)) == nil)
115+ if((pdf = calloc(1, sizeof(*pdf))) == nil)
116116 goto err;
117117 pdf->bio = b;
118118
@@ -152,7 +152,7 @@ badtrailer: · 33 unmodified lines
152152 goto err;
153153 }
154154 morexref:
155− off = Bseek(b, 0, 1);
155+ off = Boffset(b);
156156 n = sizeof(tmp)-1;
157157 if((n = Bread(b, tmp, n)) < 16){
158158 badxref:
@@ -163,16 +163,14 @@ badxref: · 4 unmodified lines
163163 if(memcmp(tmp, "xref", 4) == 0){
164164 /* 7.5.4 xref */
165165 x = tmp+4;
166− nxref = -1;
167− if((xref0 = strtol(x, &x, 10)) < 0 || (nxref = strtol(x, &x, 10)) < 1){
168− werrstr("xref0=%d nxref=%d", xref0, nxref);
169− goto badxref;
170− }
171−
166+ xref0 = strtol(x, &x, 10);
167+ nxref = strtol(x, &x, 10);
172168 /* skip whitespace and move to the first subsection */
173169 for(; isws(*x) && x < tmp+n; x++);
174170 n = x-tmp+off;
175− if(Bseek(b, n, 0) != n || xrefread(pdf, xref0, nxref) != 0)
171+ if(Bseek(b, n, 0) != n)
172+ goto badxref;
173+ if(xref0 >= 0 && nxref > 0 && xrefread(pdf, xref0, nxref) != 0)
176174 goto badxref;
177175 goto morexref; /* there could be more updates, try it */
178176 }else if(memcmp(tmp, "trailer", 7) == 0){ /* 7.5.5 file trailer */
pdf.h+28−8
@@ -19,6 +19,8 @@ typedef struct Stream Stream; · 18 unmodified lines
1919 typedef struct Xref Xref;
2020 #pragma incomplete Filter
2121
22+#pragma varargck type "T" Object *
23+
2224 struct Buffer {
2325 uchar *b;
2426 int ro;
@@ -71,7 +73,7 @@ struct KeyValue { · 46 unmodified lines
7173 };
7274
7375 struct Pdf {
74− void *bio;
76+ Biobuf *bio;
7577 Xref *xref;
7678 int nxref; /* 7.5.4 xref subsection number of objects */
7779
@@ -87,15 +89,23 @@ struct Xref { · 9 unmodified lines
8789
8890 struct Stream {
8991 Buffer buf;
90− void *bio;
92+ Biobuf *bio;
9193 };
9294
9395 extern Object null;
9496
95−Pdf *pdfopen(int fd);
97+Pdf *pdfopen(Biobuf *b);
9698 void pdfclose(Pdf *pdf);
9799
98−Object *pdfobj(Pdf *pdf, void *b);
100+/*
101+ * Parse an object.
102+ */
103+Object *pdfobj(Pdf *pdf, Biobuf *bio);
104+
105+/*
106+ * Deallocate the object and all its children. Refcount is
107+ * considered.
108+ */
99109 void pdfobjfree(Object *o);
100110
101111 /*
@@ -105,9 +115,22 @@ void pdfobjfree(Object *o); · 3 unmodified lines
105115 */
106116 Object *pdfeval(Object **o);
107117
118+/*
119+ * Increment refcount of an object. Freeing an object (or its
120+ * parent) decrements ref count.
121+ */
108122 Object *pdfref(Object *o);
109123
124+/*
125+ * Returns 1 if the character is whitespace. 0 otherwise.
126+ * See 7.2.2.
127+ */
110128 int isws(int c);
129+
130+/*
131+ * Returns 1 if the character is a delimiter. 0 otherwise.
132+ * See 7.2.2.
133+ */
111134 int isdelim(int c);
112135
113136 Object *dictget(Object *o, char *name);
@@ -127,10 +150,7 @@ void bufinit(Buffer *b, uchar *d, int sz); · 13 unmodified lines
127150 void buffree(Buffer *b);
128151 int bufeof(Buffer *b);
129152 uchar *bufdata(Buffer *b, int *sz);
130−int bufreadn(Buffer *b, void *bio, int sz);
153+int bufreadn(Buffer *b, Biobuf *bio, int sz);
131154 int bufput(Buffer *b, uchar *d, int sz);
132155 int bufget(Buffer *b, uchar *d, int sz);
133156 void bufdump(Buffer *b);
134−
135−#pragma varargck type "T" Object *
136−int Tfmt(Fmt *f);
stream.c+2−2
@@ -8,7 +8,7 @@ bufiof(Biobufhdr *b, void *data, long n) · 7 unmodified lines
88 {
99 Stream *s;
1010
11− s = (void*)((char*)b - sizeof(*s));
11+ s = (Stream*)((char*)b - sizeof(*s));
1212
1313 return bufget(&s->buf, data, n);
1414 }
@@ -66,7 +66,7 @@ streamopen(Object *o) · 51 unmodified lines
6666 buffree(&b);
6767 return nil;
6868 }
69− s->bio = (uchar*)(s+1);
69+ s->bio = (Biobuf*)(s+1);
7070 s->buf = b;
7171 Binit(s->bio, Bfildes(o->pdf->bio), OREAD);
7272 Biofn(s->bio, bufiof);
string.c+6−6
@@ -62,7 +62,7 @@ pdfstring(Biobuf *b) · 61 unmodified lines
6262 return nil;
6363
6464 for(paren = sz = 0;;){
65− if((c = Bgetc(b)) <= 0)
65+ if((c = Bgetc(b)) < 0)
6666 break;
6767
6868 switch(c){
@@ -81,7 +81,7 @@ pdfstring(Biobuf *b) · 12 unmodified lines
8181 paren--;
8282 if(paren < 1){
8383 c = 0;
84− break;
84+ goto end;
8585 }
8686 continue;
8787
@@ -99,7 +99,7 @@ pdfstring(Biobuf *b) · 11 unmodified lines
9999 oct[i] = 0;
100100 c = strtol(oct, nil, 8);
101101 }else if(c >= nelem(esc) || (c = esc[c]) == 0){
102− werrstr("unknown escape char %c", c);
102+ werrstr("unknown escape char '%c'", c);
103103 goto err;
104104 }else if(c < 0){
105105 continue;
@@ -108,13 +108,13 @@ pdfstring(Biobuf *b) · 2 unmodified lines
108108
109109 default:
110110 if(paren < 1){
111− werrstr("unexpected char '%c'", c);
111+ werrstr("unexpected char %02x '%c'", (u8int)c, c);
112112 goto err;
113113 }
114114 break;
115115 }
116116
117− if(c <= 0)
117+ if(c < 0)
118118 break;
119119 if(sz+1 > maxsz){
120120 maxsz *= 2;
@@ -124,7 +124,7 @@ pdfstring(Biobuf *b) · 3 unmodified lines
124124 }
125125 s[sz++] = c;
126126 }
127−
127+end:
128128 if(paren != 0){
129129 werrstr("bad paren");
130130 goto err;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t