History

attach dicts to streams if there is one

Sigrid Haflínudóttir committed 6 years ago

Commit 51cd3bf1 parent a080ae87 files · +70 −20Browse files

Unified · Split

7 files
  1. array.c +2 −2
  2. dict.c +3 −3
  3. eval.c +1 −1
  4. main.c +2 −0
  5. object.c +45 −9
  6. pdf.c +9 −4
  7. pdf.h +8 −1
array.c+2−2
@@ -6,7 +6,7 @@ · 5 unmodified lines
66 /* 7.3.6 Array Objects */
77
88 Object *
9−pdfarray(Biobuf *b)
9+pdfarray(Pdf *pdf, Biobuf *b)
1010 {
1111 Object *o, *m;
1212 Object **a;
@@ -25,7 +25,7 @@ pdfarray(Biobuf *b) · 12 unmodified lines
2525 }
2626
2727 Bungetc(b);
28− if((m = pdfobject(b)) == nil){
28+ if((m = pdfobject(pdf, b)) == nil){
2929 noel = 1;
3030 continue;
3131 }
dict.c+3−3
@@ -6,7 +6,7 @@ · 5 unmodified lines
66 /* 7.3.7 Dictionary Objects */
77
88 Object *
9−pdfdict(Biobuf *b)
9+pdfdict(Pdf *pdf, Biobuf *b)
1010 {
1111 Object *o, *k, *v;
1212 KeyValue *kv;
@@ -33,11 +33,11 @@ pdfdict(Biobuf *b) · 20 unmodified lines
3333 }
3434
3535 Bungetc(b);
36− if((k = pdfobject(b)) == nil){
36+ if((k = pdfobject(pdf, b)) == nil){
3737 nokey = 1;
3838 continue;
3939 }
40− if((v = pdfobject(b)) == nil)
40+ if((v = pdfobject(pdf, b)) == nil)
4141 goto err;
4242 if(k->type != Oname){
4343 werrstr("expected name as a key");
eval.c+1−1
@@ -24,7 +24,7 @@ pdfeval(Pdf *pdf, Object *o) · 23 unmodified lines
2424 werrstr("xref seek failed");
2525 return -1;
2626 }
27− if((d = pdfobject(pdf->bio)) == nil)
27+ if((d = pdfobject(pdf, pdf->bio)) == nil)
2828 return -1;
2929 *o = *d;
3030 memset(d, 0, sizeof(*d));
main.c+2−0
@@ -2,6 +2,7 @@ · 1 unmodified line
22 #include <libc.h>
33 #include <thread.h>
44 #include <bio.h>
5+#include <flate.h>
56 #include "pdf.h"
67
78 int mainstacksize = 32768;
@@ -21,6 +22,7 @@ threadmain(int argc, char **argv) · 13 unmodified lines
2122 Object *v;
2223
2324 quotefmtinstall();
25+ deflateinit();
2426
2527 ARGBEGIN{
2628 default:
object.c+45−9
@@ -6,8 +6,8 @@ · 5 unmodified lines
66
77 Object *pdfstring(Biobuf *b);
88 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);
1111
1212 static Object null = {
1313 .type = Onull,
@@ -15,11 +15,13 @@ static Object null = { · 1 unmodified line
1515
1616 /* General function to parse an object of any type. */
1717 Object *
18−pdfobject(void *b)
18+pdfobject(Pdf *pdf, void *b)
1919 {
20− Object *o, *o2;
20+ Object *o, *o2, *m;
2121 vlong off;
2222 int c, tf;
23+ Xref xref;
24+ char s[16];
2325
2426 o = o2 = nil;
2527 do; while(isws(c = Bgetc(b)));
@@ -31,7 +33,29 @@ pdfobject(void *b) · 5 unmodified lines
3133 c = Bgetc(b);
3234 if(c == '<'){
3335 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+ }
3559 }
3660 Bungetc(b);
3761 /* fall through */
@@ -46,7 +70,7 @@ pdfobject(void *b) · 8 unmodified lines
4670
4771 case '[':
4872 Bungetc(b);
49− return pdfarray(b);
73+ return pdfarray(pdf, b);
5074
5175 case 'n':
5276 off = Boffset(b);
@@ -97,7 +121,7 @@ unexpected: · 44 unmodified lines
97121 Bgetd(b, &o->num); /* get the first number */
98122 off = Boffset(b); /* seek here if not an indirect object later */
99123
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 */
101125 do; while(isws(c = Bgetc(b)));
102126 if(c < 0)
103127 goto err;
@@ -109,9 +133,14 @@ unexpected: · 5 unmodified lines
109133 return o;
110134 }
111135 if(c == 'o' && Bgetc(b) == 'b' && Bgetc(b) == 'j'){ /* object */
112− freeobject(o2);
136+ xref.id = o->num;
137+ xref.gen = o2->num;
113138 /* 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;
115144 }
116145 }
117146
@@ -157,6 +186,13 @@ freeobject(Object *o) · 39 unmodified lines
157186
158187 case Odict:
159188 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+
160196 case Oindir:
161197 break;
162198 }
pdf.c+9−4
@@ -79,7 +79,7 @@ trailerread(Pdf *pdf) · 78 unmodified lines
7979 Object *o;
8080 KeyValue *kv;
8181
82− if((o = pdfobject(pdf->bio)) == nil)
82+ if((o = pdfobject(pdf, pdf->bio)) == nil)
8383 goto err;
8484
8585 if(o->type != Odict){
@@ -115,6 +115,7 @@ pdfopen(int fd) · 29 unmodified lines
115115 {
116116 Pdf *pdf;
117117 Biobuf *b;
118+ Object *o;
118119 char tmp[64], *s, *x;
119120 int xref0; /* 7.5.4 xref subsection first object number */
120121 int nxref; /* 7.5.4 xref subsection number of objects */
@@ -122,6 +123,7 @@ pdfopen(int fd) · 1 unmodified line
122123 int i, n, off;
123124
124125 b = nil;
126+ o = nil;
125127 if((pdf = calloc(1, sizeof(*pdf))) == nil || (b = Bfdopen(fd, OREAD)) == nil)
126128 goto err;
127129 pdf->bio = b;
@@ -192,15 +194,18 @@ badxref: · 64 unmodified lines
192194 werrstr("invalid trailer: %r");
193195 goto err;
194196 }
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+
198202 }
199203
200204 return pdf;
201205 err:
202206 werrstr("pdfopen: %r [at %p]", (void*)Boffset(b));
203207 pdfclose(pdf);
208+ freeobject(o);
204209 return nil;
205210 }
206211
pdf.h+8−1
@@ -40,6 +40,13 @@ struct Object { · 39 unmodified lines
4040 Object **e;
4141 int ne;
4242 }array;
43+
44+ struct {
45+ KeyValue *kv;
46+ int nkv;
47+ u32int length; /* packed */
48+ u32int offset;
49+ }stream;
4350 };
4451 };
4552
@@ -66,7 +73,7 @@ struct Xref { · 20 unmodified lines
6673 Pdf *pdfopen(int fd);
6774 void pdfclose(Pdf *pdf);
6875
69−Object *pdfobject(void *b);
76+Object *pdfobject(Pdf *pdf, void *b);
7077 void freeobject(Object *o);
7178
7279 /*

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