Unified · Split
| @@ -58,7 +58,7 @@ bufdata(Buffer *b, int *sz) · 57 unmodified lines | ||
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | int |
| 61 | −bufreadn(Buffer *b, void *bio, int sz) | |
| 61 | +bufreadn(Buffer *b, Biobuf *bio, int sz) | |
| 62 | 62 | { |
| 63 | 63 | int n, end; |
| 64 | 64 | |
| @@ -17,8 +17,8 @@ usage(void) · 16 unmodified lines | ||
| 17 | 17 | void |
| 18 | 18 | threadmain(int argc, char **argv) |
| 19 | 19 | { |
| 20 | − int fd; | |
| 21 | 20 | Pdf *pdf; |
| 21 | + Biobuf *b; | |
| 22 | 22 | Object *v; |
| 23 | 23 | |
| 24 | 24 | quotefmtinstall(); |
| @@ -43,9 +43,9 @@ threadmain(int argc, char **argv) · 18 unmodified lines | ||
| 43 | 43 | |
| 44 | 44 | if(argc != 1) |
| 45 | 45 | usage(); |
| 46 | − if((fd = open(argv[0], OREAD)) < 0) | |
| 46 | + if((b = Bopen(argv[0], OREAD)) == nil) | |
| 47 | 47 | sysfatal("%r"); |
| 48 | − if((pdf = pdfopen(fd)) == nil) | |
| 48 | + if((pdf = pdfopen(b)) == nil) | |
| 49 | 49 | sysfatal("%s: %r", argv[0]); |
| 50 | 50 | if((v = dictget(pdf->info, "Creator")) != nil) |
| 51 | 51 | fprint(2, "creator: %s\n", v->str); |
| @@ -3,7 +3,35 @@ · 2 unmodified lines | ||
| 3 | 3 | #include <bio.h> |
| 4 | 4 | #include "pdf.h" |
| 5 | 5 | |
| 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 | + | |
| 7 | 35 | int |
| 8 | 36 | isws(int c) |
| 9 | 37 | { |
| @@ -12,7 +40,6 @@ isws(int c) · 2 unmodified lines | ||
| 12 | 40 | c == ' '; |
| 13 | 41 | } |
| 14 | 42 | |
| 15 | −/* 7.2.2 delimeters */ | |
| 16 | 43 | int |
| 17 | 44 | isdelim(int c) |
| 18 | 45 | { |
| @@ -9,38 +9,9 @@ Object *pdfname(Biobuf *b); · 8 unmodified lines | ||
| 9 | 9 | Object *pdfarray(Pdf *pdf, Biobuf *b); |
| 10 | 10 | Object *pdfdict(Pdf *pdf, Biobuf *b); |
| 11 | 11 | |
| 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 | − | |
| 41 | 12 | /* General function to parse an object of any type. */ |
| 42 | 13 | Object * |
| 43 | −pdfobj(Pdf *pdf, void *b) | |
| 14 | +pdfobj(Pdf *pdf, Biobuf *b) | |
| 44 | 15 | { |
| 45 | 16 | Object *o, *o2; |
| 46 | 17 | vlong off; |
| @@ -61,6 +32,8 @@ pdfobj(Pdf *pdf, void *b) · 14 unmodified lines | ||
| 61 | 32 | if((o = pdfdict(pdf, b)) != nil){ |
| 62 | 33 | /* check for attached stream */ |
| 63 | 34 | off = Boffset(b); |
| 35 | + do; while(isws(Bgetc(b))); | |
| 36 | + Bungetc(b); | |
| 64 | 37 | if(Bread(b, s, 7) == 7 && memcmp(s, "stream", 6) == 0 && isws(c = s[6])){ |
| 65 | 38 | /* there IS a stream */ |
| 66 | 39 | if(c == '\r' && (c = Bgetc(b)) < 0) |
| @@ -4,6 +4,8 @@ · 3 unmodified lines | ||
| 4 | 4 | #include <ctype.h> |
| 5 | 5 | #include "pdf.h" |
| 6 | 6 | |
| 7 | +int Tfmt(Fmt *f); | |
| 8 | + | |
| 7 | 9 | /* |
| 8 | 10 | * pre-1.5 xref section reader |
| 9 | 11 | * PDF>=1.5 may have BOTH (or either) old xref format and xref streams |
| @@ -96,10 +98,9 @@ err: · 86 unmodified lines | ||
| 96 | 98 | } |
| 97 | 99 | |
| 98 | 100 | Pdf * |
| 99 | −pdfopen(int fd) | |
| 101 | +pdfopen(Biobuf *b) | |
| 100 | 102 | { |
| 101 | 103 | Pdf *pdf; |
| 102 | − Biobuf *b; | |
| 103 | 104 | Object *o; |
| 104 | 105 | char tmp[64], *s, *x; |
| 105 | 106 | int xref0; /* 7.5.4 xref subsection first object number */ |
| @@ -110,9 +111,8 @@ pdfopen(int fd) · 4 unmodified lines | ||
| 110 | 111 | |
| 111 | 112 | fmtinstall('T', Tfmt); |
| 112 | 113 | |
| 113 | − b = nil; | |
| 114 | 114 | o = nil; |
| 115 | − if((pdf = calloc(1, sizeof(*pdf))) == nil || (b = Bfdopen(fd, OREAD)) == nil) | |
| 115 | + if((pdf = calloc(1, sizeof(*pdf))) == nil) | |
| 116 | 116 | goto err; |
| 117 | 117 | pdf->bio = b; |
| 118 | 118 | |
| @@ -152,7 +152,7 @@ badtrailer: · 33 unmodified lines | ||
| 152 | 152 | goto err; |
| 153 | 153 | } |
| 154 | 154 | morexref: |
| 155 | − off = Bseek(b, 0, 1); | |
| 155 | + off = Boffset(b); | |
| 156 | 156 | n = sizeof(tmp)-1; |
| 157 | 157 | if((n = Bread(b, tmp, n)) < 16){ |
| 158 | 158 | badxref: |
| @@ -163,16 +163,14 @@ badxref: · 4 unmodified lines | ||
| 163 | 163 | if(memcmp(tmp, "xref", 4) == 0){ |
| 164 | 164 | /* 7.5.4 xref */ |
| 165 | 165 | 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); | |
| 172 | 168 | /* skip whitespace and move to the first subsection */ |
| 173 | 169 | for(; isws(*x) && x < tmp+n; x++); |
| 174 | 170 | 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) | |
| 176 | 174 | goto badxref; |
| 177 | 175 | goto morexref; /* there could be more updates, try it */ |
| 178 | 176 | }else if(memcmp(tmp, "trailer", 7) == 0){ /* 7.5.5 file trailer */ |
| @@ -19,6 +19,8 @@ typedef struct Stream Stream; · 18 unmodified lines | ||
| 19 | 19 | typedef struct Xref Xref; |
| 20 | 20 | #pragma incomplete Filter |
| 21 | 21 | |
| 22 | +#pragma varargck type "T" Object * | |
| 23 | + | |
| 22 | 24 | struct Buffer { |
| 23 | 25 | uchar *b; |
| 24 | 26 | int ro; |
| @@ -71,7 +73,7 @@ struct KeyValue { · 46 unmodified lines | ||
| 71 | 73 | }; |
| 72 | 74 | |
| 73 | 75 | struct Pdf { |
| 74 | − void *bio; | |
| 76 | + Biobuf *bio; | |
| 75 | 77 | Xref *xref; |
| 76 | 78 | int nxref; /* 7.5.4 xref subsection number of objects */ |
| 77 | 79 | |
| @@ -87,15 +89,23 @@ struct Xref { · 9 unmodified lines | ||
| 87 | 89 | |
| 88 | 90 | struct Stream { |
| 89 | 91 | Buffer buf; |
| 90 | − void *bio; | |
| 92 | + Biobuf *bio; | |
| 91 | 93 | }; |
| 92 | 94 | |
| 93 | 95 | extern Object null; |
| 94 | 96 | |
| 95 | −Pdf *pdfopen(int fd); | |
| 97 | +Pdf *pdfopen(Biobuf *b); | |
| 96 | 98 | void pdfclose(Pdf *pdf); |
| 97 | 99 | |
| 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 | + */ | |
| 99 | 109 | void pdfobjfree(Object *o); |
| 100 | 110 | |
| 101 | 111 | /* |
| @@ -105,9 +115,22 @@ void pdfobjfree(Object *o); · 3 unmodified lines | ||
| 105 | 115 | */ |
| 106 | 116 | Object *pdfeval(Object **o); |
| 107 | 117 | |
| 118 | +/* | |
| 119 | + * Increment refcount of an object. Freeing an object (or its | |
| 120 | + * parent) decrements ref count. | |
| 121 | + */ | |
| 108 | 122 | Object *pdfref(Object *o); |
| 109 | 123 | |
| 124 | +/* | |
| 125 | + * Returns 1 if the character is whitespace. 0 otherwise. | |
| 126 | + * See 7.2.2. | |
| 127 | + */ | |
| 110 | 128 | int isws(int c); |
| 129 | + | |
| 130 | +/* | |
| 131 | + * Returns 1 if the character is a delimiter. 0 otherwise. | |
| 132 | + * See 7.2.2. | |
| 133 | + */ | |
| 111 | 134 | int isdelim(int c); |
| 112 | 135 | |
| 113 | 136 | Object *dictget(Object *o, char *name); |
| @@ -127,10 +150,7 @@ void bufinit(Buffer *b, uchar *d, int sz); · 13 unmodified lines | ||
| 127 | 150 | void buffree(Buffer *b); |
| 128 | 151 | int bufeof(Buffer *b); |
| 129 | 152 | uchar *bufdata(Buffer *b, int *sz); |
| 130 | −int bufreadn(Buffer *b, void *bio, int sz); | |
| 153 | +int bufreadn(Buffer *b, Biobuf *bio, int sz); | |
| 131 | 154 | int bufput(Buffer *b, uchar *d, int sz); |
| 132 | 155 | int bufget(Buffer *b, uchar *d, int sz); |
| 133 | 156 | void bufdump(Buffer *b); |
| 134 | − | |
| 135 | −#pragma varargck type "T" Object * | |
| 136 | −int Tfmt(Fmt *f); | |
| @@ -8,7 +8,7 @@ bufiof(Biobufhdr *b, void *data, long n) · 7 unmodified lines | ||
| 8 | 8 | { |
| 9 | 9 | Stream *s; |
| 10 | 10 | |
| 11 | − s = (void*)((char*)b - sizeof(*s)); | |
| 11 | + s = (Stream*)((char*)b - sizeof(*s)); | |
| 12 | 12 | |
| 13 | 13 | return bufget(&s->buf, data, n); |
| 14 | 14 | } |
| @@ -66,7 +66,7 @@ streamopen(Object *o) · 51 unmodified lines | ||
| 66 | 66 | buffree(&b); |
| 67 | 67 | return nil; |
| 68 | 68 | } |
| 69 | − s->bio = (uchar*)(s+1); | |
| 69 | + s->bio = (Biobuf*)(s+1); | |
| 70 | 70 | s->buf = b; |
| 71 | 71 | Binit(s->bio, Bfildes(o->pdf->bio), OREAD); |
| 72 | 72 | Biofn(s->bio, bufiof); |
| @@ -62,7 +62,7 @@ pdfstring(Biobuf *b) · 61 unmodified lines | ||
| 62 | 62 | return nil; |
| 63 | 63 | |
| 64 | 64 | for(paren = sz = 0;;){ |
| 65 | − if((c = Bgetc(b)) <= 0) | |
| 65 | + if((c = Bgetc(b)) < 0) | |
| 66 | 66 | break; |
| 67 | 67 | |
| 68 | 68 | switch(c){ |
| @@ -81,7 +81,7 @@ pdfstring(Biobuf *b) · 12 unmodified lines | ||
| 81 | 81 | paren--; |
| 82 | 82 | if(paren < 1){ |
| 83 | 83 | c = 0; |
| 84 | − break; | |
| 84 | + goto end; | |
| 85 | 85 | } |
| 86 | 86 | continue; |
| 87 | 87 | |
| @@ -99,7 +99,7 @@ pdfstring(Biobuf *b) · 11 unmodified lines | ||
| 99 | 99 | oct[i] = 0; |
| 100 | 100 | c = strtol(oct, nil, 8); |
| 101 | 101 | }else if(c >= nelem(esc) || (c = esc[c]) == 0){ |
| 102 | − werrstr("unknown escape char %c", c); | |
| 102 | + werrstr("unknown escape char '%c'", c); | |
| 103 | 103 | goto err; |
| 104 | 104 | }else if(c < 0){ |
| 105 | 105 | continue; |
| @@ -108,13 +108,13 @@ pdfstring(Biobuf *b) · 2 unmodified lines | ||
| 108 | 108 | |
| 109 | 109 | default: |
| 110 | 110 | if(paren < 1){ |
| 111 | − werrstr("unexpected char '%c'", c); | |
| 111 | + werrstr("unexpected char %02x '%c'", (u8int)c, c); | |
| 112 | 112 | goto err; |
| 113 | 113 | } |
| 114 | 114 | break; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | − if(c <= 0) | |
| 117 | + if(c < 0) | |
| 118 | 118 | break; |
| 119 | 119 | if(sz+1 > maxsz){ |
| 120 | 120 | maxsz *= 2; |
| @@ -124,7 +124,7 @@ pdfstring(Biobuf *b) · 3 unmodified lines | ||
| 124 | 124 | } |
| 125 | 125 | s[sz++] = c; |
| 126 | 126 | } |
| 127 | − | |
| 127 | +end: | |
| 128 | 128 | if(paren != 0){ |
| 129 | 129 | werrstr("bad paren"); |
| 130 | 130 | goto err; |