-
Notifications
You must be signed in to change notification settings - Fork 59
/
CPDFStream.cls
363 lines (280 loc) · 11.2 KB
/
CPDFStream.cls
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "CPDFStream"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'Author: dzzie@yahoo.com
'Site: http://sandsprite.com
Option Explicit
Public Enum ObjType
Unknown = 0
Flash = 1
U3d = 2
TTFFont = 3
prc = 4
xml = 5
End Enum
Public Index As Long
Public Header As String
Public escapedHeader As String
Public isBinary As Boolean
Public RawObject As String
Public ObjectStartOffset As Long
Public ObjectEndOffset As Long
Public ContainsStream As Boolean
Public isCompressed As Boolean
'Public isASCIIHexDecode As Boolean
'theses are related to streams
Public startOffset As Long
Public EndOffset As Long
Public CompressedSize As Long
Public DecompressedSize As Long
Public OriginalData As String
Public DecompressedData As String
Public OriginalDataCRC As String
Public DecompressedDataCRC As String
Public HeaderCRC As String
Public ContentType As ObjType
Public UsesUnsupportedFilter As Boolean
Public Message As String
Public FileExtension As String
Public FileType As String
Public StreamDecompressor As New CApplyFilters
'Private Const unsptFilters = "ASCII85Decode,LZWDecode,RunLengthDecode,DCTDecode,CCITTFaxDecode,JBIG2Decode,JPXDecode"
Private Function FindHeaderEnd(ByVal hstart) As Long
On Error Resume Next
Dim hend As Long
Dim tmp As String
Dim nested As Long
Dim level As Long
Dim c As Long
'If Index = 112 Then Stop
'this is some ugly ass logic...just retooled it some 1.3.12
'but its still really bad...seems to work though
hstart = hstart + 2
Do While 1
tryAgain:
hend = InStr(hstart, RawObject, ">>")
If hend < 1 Then Exit Do
If hend - hstart = 0 Then 'there was a >>>>
hstart = hend + 2
If nested = 0 Then
Exit Do
Else
nested = nested - 1
End If
GoTo tryAgain
End If
tmp = Mid(RawObject, hstart, hend - hstart)
If Len(tmp) = 0 Then Exit Do
If Err.Number <> 0 Then Exit Do
If InStr(1, tmp, "<<") > 1 Then 'a nested tag was found
c = GetCount(tmp, "<<") 'maybe multiple...
If c > 2 Then nested = nested + c - 2
hstart = hend + 2
Else
If nested = 0 Then
Exit Do 'no more nested tags found were at end
Else
nested = nested - 1
hstart = hend + 2
End If
End If
Loop
FindHeaderEnd = hend
End Function
Function ParseSelf()
'works off of the rawobject
'6 0 obj
'<</Filter /FlateDecode /Length 314>>stream____endstream
'endobj
'Filters can also support multiple encodings sequentially
'/Filter [ /ASCIIHexDecode /LZWDecode /ASCII85Decode /RunLengthDecode /FlateDecode ]
'apparently filters also support abbreviations...Fl is known to work as FlateDecode...
Dim hstart As Long
Dim hend As Long
Dim sStart As Long
Dim sEnd As Long
Dim tmp As String
Dim activeData As String
Dim a, b
hstart = InStr(RawObject, "<<") 'do we need to count the nesting? they can be...
'hend = InStrRev(RawObject, ">>") '<--problem if embedded data contains >> (does happen)...
'sStart = InStr(1, RawObject, "stream", vbTextCompare)
'sEnd = InStr(1, RawObject, "endstream", vbTextCompare)
'possible failure octet-stream>>stream
'if there is a header an a stream, make sure stream start marker is after
'If sStart > 0 Then 'it has a stream we may need to adjust our default hend
' If hstart > 0 And hend > sStart Then 'why yes we do
' tmp = Mid(RawObject, 1, sStart)
' hend = InStrRev(tmp, ">>") 'dirty fix but should be good enough?
' DebugMsg "Stream: " & Index & " Adjusting hend to " & hend
' End If
'End If
If hstart > 0 Then hend = FindHeaderEnd(hstart)
sStart = InStr(IIf(hend > 0, hend, 1), RawObject, "stream", vbTextCompare)
sEnd = InStr(IIf(sStart > 0, sStart, 1), RawObject, "endstream", vbTextCompare)
If hstart > 0 And hend > hstart Then
Header = Mid(RawObject, hstart, hend - hstart + Len(">>"))
'Header = Replace(Header, Chr(0), "_Chr(0)_")
Header = Replace(Header, Chr(0), Empty)
Header = Replace(Header, "þÿ", Empty)
HeaderCRC = CRC32(Header)
escapedHeader = EscapeHeader(Header)
StreamDecompressor.DetermineFilters Me, escapedHeader
Else
'need to handle stuff like: obj[/ICCBased 119 0 R]
If Len(RawObject) > 4 Then
If sStart > 0 Then hend = sStart Else hend = Len(RawObject)
If hend > 0 Then
Header = Mid(RawObject, 4, hend)
Header = Replace(Header, Chr(0), Empty)
Header = Replace(Header, "þÿ", Empty)
HeaderCRC = CRC32(Header)
escapedHeader = EscapeHeader(Header)
End If
End If
End If
If sStart > 0 And sEnd > sStart Then
ContainsStream = True
sStart = sStart + Len("stream")
startOffset = ObjectStartOffset + sStart - 1
EndOffset = ObjectStartOffset + sEnd - 1
OriginalData = MyTrim(Mid(RawObject, sStart, sEnd - sStart))
CompressedSize = Len(OriginalData)
OriginalDataCRC = CRC32(OriginalData)
If isCompressed Then StreamDecompressor.ApplyFilters
'If InStr(1, RawObject, "DCTDecode", vbTextCompare) Then Stop
If isCompressed And StreamDecompressor.UnsupportedFilter = False _
And StreamDecompressor.DecompressionError = False And DecompressedSize > 0 Then
activeData = DecompressedData
DecompressedDataCRC = CRC32(DecompressedData)
Else
activeData = OriginalData
End If
If StreamDecompressor.DecompressionError = True Then
Me.Message = StreamDecompressor.DecompErrorMessage
ElseIf StreamDecompressor.UnsupportedFilter Then
Me.Message = "Uses unsupported filter " & StreamDecompressor.GetActiveFiltersAsString
End If
SetContentType activeData
End If
Dim xx As String
xx = Mid(activeData, 1, 20)
If InStr(1, xx, Chr(0)) > 0 Then isBinary = True
End Function
Private Sub SetContentType(ad)
If VBA.Left(ad, 3) = "CWS" Then
ContentType = Flash
FileExtension = ".swf"
FileType = "Flash File"
ElseIf VBA.Left(ad, 3) = "FWS" Then
ContentType = Flash
FileExtension = ".swf"
FileType = "Flash File"
ElseIf VBA.Left(ad, 3) = "U3D" Then
ContentType = U3d
FileExtension = ".u3d"
FileType = "U3d File"
ElseIf VBA.Left(ad, 3) = "PRC" Then
ContentType = prc
FileExtension = ".prc"
FileType = "PRC File"
ElseIf InStr(1, ad, "maxp", vbTextCompare) > 0 Then
ContentType = TTFFont
FileExtension = ".ttf"
FileType = "Font File"
ElseIf InStr(1, ad, "glyf", vbTextCompare) > 0 Then
ContentType = TTFFont
FileExtension = ".ttf"
FileType = "Font File"
ElseIf InStr(1, ad, "xmlns:", vbTextCompare) > 0 Then
ContentType = xml
FileExtension = ".xml"
FileType = "Xml Data"
End If
End Sub
Private Function MyTrim(strin As String)
On Error Resume Next
Dim s As String
s = strin
While Right(s, 1) = Chr(&HD) Or Right(s, 1) = Chr(&HA)
s = Mid(s, 1, Len(s) - 1)
EndOffset = EndOffset - 1
Wend
While VBA.Left(s, 1) = Chr(&HD) Or Left(s, 1) = Chr(&HA)
s = Mid(s, 2)
startOffset = startOffset + 1
Wend
MyTrim = s
End Function
Private Function lpad(s, minLen) As String
Dim l As Long
l = InStr(s, ":")
If l < minLen Then
lpad = Space(minLen - l) & s
Else
lpad = s
End If
End Function
Function GetHeaderWithViewOptions() As String
If Form1.mnuAutoEscapeHeaders.Checked Then
If Form1.mnuVisualFormatHeaders.Checked Then
GetHeaderWithViewOptions = VisualFormatHeader(escapedHeader)
Else
GetHeaderWithViewOptions = escapedHeader
End If
ElseIf Form1.mnuVisualFormatHeaders.Checked Then
GetHeaderWithViewOptions = VisualFormatHeader(Me.Header)
Else
GetHeaderWithViewOptions = Me.Header
End If
End Function
Function GetDetailsReport()
Dim r() As String
Const sz = 22
On Error Resume Next
push r, lpad("Object Index: ", sz) & Index
push r, lpad("Object Start Offset: 0x", sz) & Hex(ObjectStartOffset) & " (" & ObjectStartOffset & ")"
push r, lpad("Object End Offset: 0x", sz) & Hex(ObjectEndOffset) & " (" & ObjectEndOffset & ")"
If Me.ContainsStream Then
push r, lpad("Stream Start Offset: 0x", sz) & Hex(startOffset) & " (" & startOffset & ")"
push r, lpad("Stream End Offset: 0x", sz) & Hex(EndOffset) & " (" & EndOffset & ")"
If Me.isCompressed Then
push r, lpad("Compressed Size: 0x", sz) & Hex(CompressedSize) & " (" & CompressedSize & ")"
push r, lpad("Compressed CRC: 0x", sz) & Hex(CRC32(Me.OriginalData))
push r, lpad("DecompFilters: ", sz) & StreamDecompressor.GetActiveFiltersAsString()
push r, lpad("Unsupported Filters?: ", sz) & StreamDecompressor.UnsupportedFilter
'push r, lpad("iText Decompressors?: ", sz) & csharp.Initilized
If StreamDecompressor.DecompressionError = True Then
push r, lpad("Decompress Error: ", sz) & StreamDecompressor.DecompErrorMessage
Else
If Not StreamDecompressor.UnsupportedFilter Then
push r, lpad("DeCompressed Size: 0x", sz) & Hex(DecompressedSize) & " (" & DecompressedSize & ")"
End If
push r, lpad("DeCompressed CRC: 0x", sz) & Hex(CRC32(Me.DecompressedData))
push r, lpad("Expansion Ratio: ", sz) & Round((DecompressedSize / CompressedSize), 2) & "x (" & DecompressedSize - CompressedSize & " bytes)"
End If
Else
push r, lpad("Raw Data Size: 0x", sz) & Hex(Me.CompressedSize)
push r, lpad("CRC32: 0x", sz) & Hex(CRC32(Me.OriginalData))
End If
End If
push r, lpad("Detected Type:", sz) & FileExtension
If Len(Message) > 0 Then push r, lpad("Message: ", sz) & Message
push r, lpad("HeaderCRC: ", sz) & Hex(Me.HeaderCRC)
push r, lpad("Header: ", sz) & vbCrLf & vbCrLf & GetHeaderWithViewOptions()
GetDetailsReport = Join(r, vbCrLf)
End Function
Private Sub Class_Initialize()
FileExtension = ".unk"
FileType = "Unknown"
End Sub