Unified · Split
| @@ -6,7 +6,7 @@ · 5 unmodified lines | ||
| 6 | 6 | /* 7.3.6 Array Objects */ |
| 7 | 7 | |
| 8 | 8 | Object * |
| 9 | −pdfarray(Biobuf *b) | |
| 9 | +pdfarray(Pdf *pdf, Biobuf *b) | |
| 10 | 10 | { |
| 11 | 11 | Object *o, *m; |
| 12 | 12 | Object **a; |
| @@ -25,7 +25,7 @@ pdfarray(Biobuf *b) · 12 unmodified lines | ||
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | Bungetc(b); |
| 28 | − if((m = pdfobject(b)) == nil){ | |
| 28 | + if((m = pdfobject(pdf, b)) == nil){ | |
| 29 | 29 | noel = 1; |
| 30 | 30 | continue; |
| 31 | 31 | } |
| @@ -6,7 +6,7 @@ · 5 unmodified lines | ||
| 6 | 6 | /* 7.3.7 Dictionary Objects */ |
| 7 | 7 | |
| 8 | 8 | Object * |
| 9 | −pdfdict(Biobuf *b) | |
| 9 | +pdfdict(Pdf *pdf, Biobuf *b) | |
| 10 | 10 | { |
| 11 | 11 | Object *o, *k, *v; |
| 12 | 12 | KeyValue *kv; |
| @@ -33,11 +33,11 @@ pdfdict(Biobuf *b) · 20 unmodified lines | ||
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | Bungetc(b); |
| 36 | − if((k = pdfobject(b)) == nil){ | |
| 36 | + if((k = pdfobject(pdf, b)) == nil){ | |
| 37 | 37 | nokey = 1; |
| 38 | 38 | continue; |
| 39 | 39 | } |
| 40 | − if((v = pdfobject(b)) == nil) | |
| 40 | + if((v = pdfobject(pdf, b)) == nil) | |
| 41 | 41 | goto err; |
| 42 | 42 | if(k->type != Oname){ |
| 43 | 43 | werrstr("expected name as a key"); |
| @@ -24,7 +24,7 @@ pdfeval(Pdf *pdf, Object *o) · 23 unmodified lines | ||
| 24 | 24 | werrstr("xref seek failed"); |
| 25 | 25 | return -1; |
| 26 | 26 | } |
| 27 | − if((d = pdfobject(pdf->bio)) == nil) | |
| 27 | + if((d = pdfobject(pdf, pdf->bio)) == nil) | |
| 28 | 28 | return -1; |
| 29 | 29 | *o = *d; |
| 30 | 30 | memset(d, 0, sizeof(*d)); |
| @@ -2,6 +2,7 @@ · 1 unmodified line | ||
| 2 | 2 | #include <libc.h> |
| 3 | 3 | #include <thread.h> |
| 4 | 4 | #include <bio.h> |
| 5 | +#include <flate.h> | |
| 5 | 6 | #include "pdf.h" |
| 6 | 7 | |
| 7 | 8 | int mainstacksize = 32768; |
| @@ -21,6 +22,7 @@ threadmain(int argc, char **argv) · 13 unmodified lines | ||
| 21 | 22 | Object *v; |
| 22 | 23 | |
| 23 | 24 | quotefmtinstall(); |
| 25 | + deflateinit(); | |
| 24 | 26 | |
| 25 | 27 | ARGBEGIN{ |
| 26 | 28 | default: |
| @@ -6,8 +6,8 @@ · 5 unmodified lines | ||
| 6 | 6 | |
| 7 | 7 | Object *pdfstring(Biobuf *b); |
| 8 | 8 | Object *pdfname(Biobuf *b); |
| 9 | −Object *pdfarray(Biobuf *b); | |
| 10 | −Object *pdfdict(Biobuf *b); | |
| 9 | +Object *pdfarray(Pdf *pdf, Biobuf *b); | |
| 10 | +Object *pdfdict(Pdf *pdf, Biobuf *b); | |
| 11 | 11 | |
| 12 | 12 | static Object null = { |
| 13 | 13 | .type = Onull, |
| @@ -15,11 +15,13 @@ static Object null = { · 1 unmodified line | ||
| 15 | 15 | |
| 16 | 16 | /* General function to parse an object of any type. */ |
| 17 | 17 | Object * |
| 18 | −pdfobject(void *b) | |
| 18 | +pdfobject(Pdf *pdf, void *b) | |
| 19 | 19 | { |
| 20 | − Object *o, *o2; | |
| 20 | + Object *o, *o2, *m; | |
| 21 | 21 | vlong off; |
| 22 | 22 | int c, tf; |
| 23 | + Xref xref; | |
| 24 | + char s[16]; | |
| 23 | 25 | |
| 24 | 26 | o = o2 = nil; |
| 25 | 27 | do; while(isws(c = Bgetc(b))); |
| @@ -31,7 +33,29 @@ pdfobject(void *b) · 5 unmodified lines | ||
| 31 | 33 | c = Bgetc(b); |
| 32 | 34 | if(c == '<'){ |
| 33 | 35 | Bseek(b, -2, 1); |
| 34 | − return pdfdict(b); | |
| 36 | + if((o = pdfdict(pdf, b)) != nil){ | |
| 37 | + /* check for attached stream */ | |
| 38 | + off = Boffset(b); | |
| 39 | + if(Bread(b, s, 7) == 7 && memcmp(s, "stream", 6) == 0 && isws(c = s[6])){ | |
| 40 | + /* there IS a stream */ | |
| 41 | + if((m = pdfdictget(o, "Length")) == nil || pdfeval(pdf, m) != 0 || m->type != Onum){ | |
| 42 | + werrstr("stream has no valid /Length"); | |
| 43 | + goto err; | |
| 44 | + } | |
| 45 | + if(c == '\r' && (c = Bgetc(b)) < 0) | |
| 46 | + goto err; | |
| 47 | + if(c != '\n'){ | |
| 48 | + werrstr("stream has no newline after dict"); | |
| 49 | + goto err; | |
| 50 | + } | |
| 51 | + o->type = Ostream; | |
| 52 | + o->stream.length = m->num; | |
| 53 | + o->stream.offset = Boffset(b); | |
| 54 | + return o; | |
| 55 | + } | |
| 56 | + Bseek(b, off, 0); | |
| 57 | + return o; | |
| 58 | + } | |
| 35 | 59 | } |
| 36 | 60 | Bungetc(b); |
| 37 | 61 | /* fall through */ |
| @@ -46,7 +70,7 @@ pdfobject(void *b) · 8 unmodified lines | ||
| 46 | 70 | |
| 47 | 71 | case '[': |
| 48 | 72 | Bungetc(b); |
| 49 | − return pdfarray(b); | |
| 73 | + return pdfarray(pdf, b); | |
| 50 | 74 | |
| 51 | 75 | case 'n': |
| 52 | 76 | off = Boffset(b); |
| @@ -97,7 +121,7 @@ unexpected: · 44 unmodified lines | ||
| 97 | 121 | Bgetd(b, &o->num); /* get the first number */ |
| 98 | 122 | off = Boffset(b); /* seek here if not an indirect object later */ |
| 99 | 123 | |
| 100 | − if((o2 = pdfobject(b)) != nil && o2->type == Onum){ /* second object is number too */ | |
| 124 | + if((o2 = pdfobject(pdf, b)) != nil && o2->type == Onum){ /* second object is number too */ | |
| 101 | 125 | do; while(isws(c = Bgetc(b))); |
| 102 | 126 | if(c < 0) |
| 103 | 127 | goto err; |
| @@ -109,9 +133,14 @@ unexpected: · 5 unmodified lines | ||
| 109 | 133 | return o; |
| 110 | 134 | } |
| 111 | 135 | if(c == 'o' && Bgetc(b) == 'b' && Bgetc(b) == 'j'){ /* object */ |
| 112 | − freeobject(o2); | |
| 136 | + xref.id = o->num; | |
| 137 | + xref.gen = o2->num; | |
| 113 | 138 | /* FIXME put into a map */ |
| 114 | − return pdfobject(b); | |
| 139 | + freeobject(o); | |
| 140 | + freeobject(o2); | |
| 141 | + if((o = pdfobject(pdf, b)) != nil) | |
| 142 | + return o; | |
| 143 | + o2 = nil; | |
| 115 | 144 | } |
| 116 | 145 | } |
| 117 | 146 | |
| @@ -157,6 +186,13 @@ freeobject(Object *o) · 39 unmodified lines | ||
| 157 | 186 | |
| 158 | 187 | case Odict: |
| 159 | 188 | case Ostream: |
| 189 | + for(i = 0; i < o->dict.nkv; i++){ | |
| 190 | + free(o->dict.kv[i].key); | |
| 191 | + freeobject(o->dict.kv[i].value); | |
| 192 | + } | |
| 193 | + free(o->dict.kv); | |
| 194 | + break; | |
| 195 | + | |
| 160 | 196 | case Oindir: |
| 161 | 197 | break; |
| 162 | 198 | } |
| @@ -79,7 +79,7 @@ trailerread(Pdf *pdf) · 78 unmodified lines | ||
| 79 | 79 | Object *o; |
| 80 | 80 | KeyValue *kv; |
| 81 | 81 | |
| 82 | − if((o = pdfobject(pdf->bio)) == nil) | |
| 82 | + if((o = pdfobject(pdf, pdf->bio)) == nil) | |
| 83 | 83 | goto err; |
| 84 | 84 | |
| 85 | 85 | if(o->type != Odict){ |
| @@ -115,6 +115,7 @@ pdfopen(int fd) · 29 unmodified lines | ||
| 115 | 115 | { |
| 116 | 116 | Pdf *pdf; |
| 117 | 117 | Biobuf *b; |
| 118 | + Object *o; | |
| 118 | 119 | char tmp[64], *s, *x; |
| 119 | 120 | int xref0; /* 7.5.4 xref subsection first object number */ |
| 120 | 121 | int nxref; /* 7.5.4 xref subsection number of objects */ |
| @@ -122,6 +123,7 @@ pdfopen(int fd) · 1 unmodified line | ||
| 122 | 123 | int i, n, off; |
| 123 | 124 | |
| 124 | 125 | b = nil; |
| 126 | + o = nil; | |
| 125 | 127 | if((pdf = calloc(1, sizeof(*pdf))) == nil || (b = Bfdopen(fd, OREAD)) == nil) |
| 126 | 128 | goto err; |
| 127 | 129 | pdf->bio = b; |
| @@ -192,15 +194,18 @@ badxref: · 64 unmodified lines | ||
| 192 | 194 | werrstr("invalid trailer: %r"); |
| 193 | 195 | goto err; |
| 194 | 196 | } |
| 195 | − }else{ /* could be 7.5.8 xref stream (since PDF 1.5) */ | |
| 196 | − werrstr("FIXME xref streams not implemented"); | |
| 197 | − goto err; | |
| 197 | + }else if(isdigit(tmp[0])){ /* could be 7.5.8 xref stream (since PDF 1.5) */ | |
| 198 | + Bseek(b, xreftb, 0); | |
| 199 | + if((o = pdfobject(pdf, b)) == nil || pdfeval(pdf, o) != 0) | |
| 200 | + goto err; | |
| 201 | + | |
| 198 | 202 | } |
| 199 | 203 | |
| 200 | 204 | return pdf; |
| 201 | 205 | err: |
| 202 | 206 | werrstr("pdfopen: %r [at %p]", (void*)Boffset(b)); |
| 203 | 207 | pdfclose(pdf); |
| 208 | + freeobject(o); | |
| 204 | 209 | return nil; |
| 205 | 210 | } |
| 206 | 211 | |
| @@ -40,6 +40,13 @@ struct Object { · 39 unmodified lines | ||
| 40 | 40 | Object **e; |
| 41 | 41 | int ne; |
| 42 | 42 | }array; |
| 43 | + | |
| 44 | + struct { | |
| 45 | + KeyValue *kv; | |
| 46 | + int nkv; | |
| 47 | + u32int length; /* packed */ | |
| 48 | + u32int offset; | |
| 49 | + }stream; | |
| 43 | 50 | }; |
| 44 | 51 | }; |
| 45 | 52 | |
| @@ -66,7 +73,7 @@ struct Xref { · 20 unmodified lines | ||
| 66 | 73 | Pdf *pdfopen(int fd); |
| 67 | 74 | void pdfclose(Pdf *pdf); |
| 68 | 75 | |
| 69 | −Object *pdfobject(void *b); | |
| 76 | +Object *pdfobject(Pdf *pdf, void *b); | |
| 70 | 77 | void freeobject(Object *o); |
| 71 | 78 | |
| 72 | 79 | /* |