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