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