~ft/pdffs

ref: 8a7f9b4b5c158944978efb13bc03dfd6e42899d3 pdffs/object.c -rw-r--r-- 4.0 KiB
8a7f9b4bNoam Preil [parsing] fix unexpected report for n 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <u.h>
#include <libc.h>
#include <ctype.h>
#include "pdf.h"

Object *pdfstring(Stream *s);
Object *pdfname(Stream *s);
Object *pdfarray(Pdf *pdf, Stream *s);
Object *pdfdict(Pdf *pdf, Stream *s);

/* General function to parse an object of any type. */
Object *
pdfobj(Pdf *pdf, Stream *s)
{
	Object *o, *o2;
	vlong off;
	int c, tf;
	Xref xref;
	char b[16];

	o = o2 = nil;
	do; while(isws(c = Sgetc(s)));
	if(c < 0)
		goto err;

	switch(c){
	case '<': /* dictionary or a string */
		c = Sgetc(s);
		if(c == '<'){
			Sseek(s, -2, 1);
			if((o = pdfdict(pdf, s)) != nil){
				/* check for attached stream */
				off = Soffset(s);
				do; while(isws(Sgetc(s)));
				Sungetc(s);
				if(Sread(s, b, 7) == 7 && memcmp(b, "stream", 6) == 0 && isws(c = b[6])){
					/* there IS a stream */
					if(c == '\r' && (c = Sgetc(s)) < 0)
						goto err;
					if(c != '\n'){
						werrstr("stream has no newline after dict");
						goto err;
					}
					o->stream.off = Soffset(s);
					o->type = Ostream;
					o->stream.len = dictint(o, "Length");
					return o;
				}
				Sseek(s, off, 0);
			}
			return o;
		}
		Sungetc(s);
		/* fall through */

	case '(':
		Sungetc(s);
		if((o = pdfstring(s)) != nil)
			o->pdf = pdf;
		return o;

	case '/':
		Sungetc(s);
		if((o = pdfname(s)) != nil)
			o->pdf = pdf;
		return o;

	case '[':
		Sungetc(s);
		if((o = pdfarray(pdf, s)) != nil)
			o->pdf = pdf;
		return o;

	case 'n':
		off = Soffset(s);
		if(Sgetc(s) == 'u' && Sgetc(s) == 'l' && Sgetc(s) == 'l' && (isws(c = Sgetc(s)) || isdelim(c))){
			Sungetc(s);
			return &null;
		}
		Sseek(s, off, 0);
		c = 'n';
		goto unexpected;

	case 't':
		off = Soffset(s);
		tf = 1;
		if(Sgetc(s) == 'r' && Sgetc(s) == 'u' && Sgetc(s) == 'e' && (isws(c = Sgetc(s)) || isdelim(c)))
			goto bool;
		Sseek(s, off, 0);
		c = 't';
		goto unexpected;

	case 'f':
		off = Soffset(s);
		tf = 0;
		if(Sgetc(s) == 'a' && Sgetc(s) == 'l' && Sgetc(s) == 's' && Sgetc(s) == 'e' && (isws(c = Sgetc(s)) || isdelim(c)))
			goto bool;
		Sseek(s, off, 0);
		c = 'f';
		goto unexpected;
bool:
		Sungetc(s);
		if((o = calloc(1, sizeof(*o))) == nil)
			goto err;
		o->type = Obool;
		o->pdf = pdf;
		o->bool = tf;
		return o;

	default:
		if(!isdigit(c) && c != '-'){
unexpected:
			Sungetc(s);
			werrstr("unexpected char '%c' at %#x+%#x (%d left)", c, Sobjoffset(s), Soffset(s), Ssize(s));
			goto err;
		}
		 /* it could be a number or an indirect object */
		Sungetc(s);
		if((o = calloc(1, sizeof(*o))) == nil)
			goto err;
		o->pdf = pdf;
		Sgetd(s, &o->num.d); /* get the first number */
		o->num.i = o->num.d;
		off = Soffset(s); /* seek here if not an indirect object later */

		if((o2 = pdfobj(pdf, s)) != nil && o2->type == Onum){ /* second object is number too */
			do; while(isws(c = Sgetc(s)));
			if(c < 0)
				goto err;
			if(c == 'R'){ /* indirect object */
				o->type = Oindir;
				o->indir.id = o->num.i;
				o->indir.gen = o2->num.i;
				pdfobjfree(o2);
				return o;
			}
			if(c == 'o' && Sgetc(s) == 'b' && Sgetc(s) == 'j'){ /* object */
				xref.id = o->num.i;
				xref.gen = o2->num.i;
				/* FIXME put into a map */
				pdfobjfree(o2);
				if((o2 = pdfobj(pdf, s)) != nil){
					pdfobjfree(o);
					return o2;
				}else{
					werrstr("obj: %r");
					goto err;
				}
			}
		}

		/* just a number, go back and return it */
		o->type = Onum;
		if(Sseek(s, off, 0) != off){
			werrstr("seek failed");
			goto err;
		}
		return o;
	}

err:
	werrstr("object: %r");
	pdfobjfree(o);
	pdfobjfree(o2);
	return nil;
}

void
pdfobjfree(Object *o)
{
	int i;

	if(o == nil || --o->ref >= 0)
		return;

	switch(o->type){
	case Onull:
		return;

	case Ostr:
	case Oname:
		free(o->str);
		break;

	case Obool:
	case Onum:
		break;

	case Oarray:
		for(i = 0; i < o->array.ne; i++)
			pdfobjfree(o->array.e[i]);
		free(o->array.e);
		break;

	case Odict:
	case Ostream:
		for(i = 0; i < o->dict.nkv; i++){
			free(o->dict.kv[i].key);
			pdfobjfree(o->dict.kv[i].value);
		}
		free(o->dict.kv);
		break;

	case Oindir:
		break;
	}

	free(o);
}

Object *
pdfref(Object *o)
{
	o->ref++;
	return o;
}