forked from feliam/CVE-2014-4377
-
Notifications
You must be signed in to change notification settings - Fork 0
/
miniPDF.py
160 lines (128 loc) · 3.63 KB
/
miniPDF.py
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
#Taken from https://github.com/feliam/miniPDF
import struct
#For constructing a minimal pdf file
class PDFObject:
def __init__(self):
self.n=None
self.v=None
def __str__(self):
s="%d %d obj\n"%(self.n,self.v)
for t in self.toks:
s+=t.__str__()
s+="\nendobj"
return s
class PDFStream(PDFObject):
def __init__(self, dict,stream):
PDFObject.__init__(self)
dict.add('Length', len(stream))
self.dict=dict
self.stream=stream
def __str__(self):
s=""
s+=self.dict.__str__()
s+="\nstream\n"
s+=self.stream
s+="\nendstream"
return s
class PDFArray(PDFObject):
def __init__(self,s):
PDFObject.__init__(self)
self.s=s
def __str__(self):
return "[%s]"%(" ".join([ o.__str__() for o in self.s]))
class PDFDict(PDFObject):
def __init__(self):
PDFObject.__init__(self)
self.dict = []
def add(self,name,obj):
self.dict.append((name,obj))
def __str__(self):
s="<<\n"
for name,obj in self.dict:
s+="/%s %s\n"%(name,obj)
s+=">>"
return s
class PDFName(PDFObject):
def __init__(self,s):
PDFObject.__init__(self)
self.s=s
def __str__(self):
return "/%s"%self.s
class PDFString(PDFObject):
def __init__(self,s):
PDFObject.__init__(self)
self.s=s
def __str__(self):
return "(%s)"%self.s
class PDFHexString(PDFObject):
def __init__(self,s):
PDFObject.__init__(self)
self.s=s
def __str__(self):
return "<" + "".join(["%02x"%ord(c) for c in self.s]) + ">"
class PDFOctalString(PDFObject):
def __init__(self,s):
PDFObject.__init__(self)
self.s="".join(["\\%03o"%ord(c) for c in s])
def __str__(self):
return "(%s)"%self.s
class PDFNum(PDFObject):
def __init__(self,s):
PDFObject.__init__(self)
self.s=s
def __str__(self):
return "%s"%self.s
class PDFRef(PDFObject):
def __init__(self,obj):
PDFObject.__init__(self)
self.obj=[obj]
def __str__(self):
return "%d %d R"%(self.obj[0].n,self.obj[0].v)
class PDFNull(PDFObject):
def __init__(self):
PDFObject.__init__(self)
def __str__(self):
return "null"
class PDFDoc():
def __init__(self):
self.objs=[]
def setRoot(self,root):
self.root=root
def setInfo(self,info):
self.info=info
def _add(self,obj):
obj.v=0
obj.n=1+len(self.objs)
self.objs.append(obj)
def add(self,obj):
if type(obj) != type([]):
self._add(obj);
else:
for o in obj:
self._add(o)
def _header(self):
return "%PDF-1.5\n"
def __str__(self):
doc1 = self._header()
xref = {}
for obj in self.objs:
xref[obj.n] = len(doc1)
doc1+="%d %d obj\n"%(obj.n,obj.v)
doc1+=obj.__str__()
doc1+="\nendobj\n"
posxref=len(doc1)
doc1+="xref\n"
doc1+="0 %d\n"%len(self.objs)
doc1+="0000000000 65535 f\n"
for xr in xref.keys():
doc1+= "%010d %05d n\n"%(xref[xr],0)
doc1+="trailer\n"
trailer = PDFDict()
trailer.add("Size",len(self.objs))
trailer.add("Root",PDFRef(self.root))
#trailer.add('Encrypt', "6 0 R")
# trailer.add("Info",PDFRef(self.info))
doc1+=trailer.__str__()
doc1+="\nstartxref\n%d\n"%posxref
doc1+="%%EOF\n\n"
return doc1