-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpdf_param_parser.py
More file actions
326 lines (273 loc) · 11 KB
/
pdf_param_parser.py
File metadata and controls
326 lines (273 loc) · 11 KB
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
#!/usr/bin/env python3
"""
windsurf helped with this code, testing it out.
"""
class pdf_param_parser:
def __init__(self, data):
self.data = data
self.pos = 0
self.length = len(data)
def skip_whitespace(self):
"""Skip whitespace characters"""
while self.pos < self.length and self.data[self.pos] in b' \t\n\r\f\0':
self.pos += 1
def get_current_char(self):
"""Get current character or None if at end"""
if self.pos < self.length:
return self.data[self.pos]
return None
def parse_name(self):
"""Parse a PDF name object (starts with /)"""
if self.get_current_char() != ord(b'/'):
return None
self.pos += 1 # Skip the '/'
start = self.pos
while self.pos < self.length:
c = self.data[self.pos]
# End of name on delimiter
if c in b' \t\n\r\f\0()<>[]{}/%':
break
# Handle #-escaped characters
if c == ord(b'#'):
if self.pos + 2 >= self.length:
break
self.pos += 3 # Skip the # and two hex digits
continue
self.pos += 1
return self.data[start:self.pos].decode('latin-1')
def parse_value(self):
"""Parse a PDF value (dictionary, array, string, number, name, boolean, null)"""
self.skip_whitespace()
if self.pos >= self.length:
return None
c = self.get_current_char()
# Dictionary
if c == ord(b'<') and self.pos + 1 < self.length and self.data[self.pos + 1] == ord(b'<'):
return self.parse_dictionary()
# Array
elif c == ord(b'['):
return self.parse_array()
# String (hex)
elif c == ord(b'<'):
return self.parse_hex_string()
# String (literal)
elif c == ord(b'('):
return self.parse_literal_string()
# Name
elif c == ord(b'/'):
return self.parse_name()
# Number or boolean/null
elif c in b'-+0123456789.':
return self.parse_number_or_ref()
# Boolean or null
elif c in b'tfn':
return self.parse_keyword()
return None
def parse_dictionary(self):
"""Parse a PDF dictionary"""
if self.data[self.pos:self.pos+2] != b'<<':
return None
self.pos += 2
result = {}
while True:
self.skip_whitespace()
if self.pos >= self.length:
break
# Check for end of dictionary
if self.data[self.pos:self.pos+2] == b'>>':
self.pos += 2
break
# Parse key (must be a name)
key = self.parse_name()
if key is None:
break
# Parse value
value = self.parse_value()
if value is not None:
result[key] = value
return result
def parse_array(self):
"""Parse a PDF array"""
if self.get_current_char() != ord(b'['):
return None
self.pos += 1
result = []
while True:
self.skip_whitespace()
if self.pos >= self.length:
break
# Check for end of array
if self.get_current_char() == ord(b']'):
self.pos += 1
break
# Parse array element
value = self.parse_value()
if value is not None:
result.append(value)
else:
break
return result
def parse_literal_string(self):
"""Parse a literal string (enclosed in parentheses)"""
if self.get_current_char() != ord(b'('):
return None
self.pos += 1
depth = 1
start = self.pos
result = []
while self.pos < self.length:
c = self.get_current_char()
if c == ord(b'\\'): # Escape sequence
self.pos += 1
if self.pos < self.length:
# Handle escape sequences (simplified)
esc = self.data[self.pos]
if esc in b'nrtbf()\\':
result.append(self.data[start:self.pos-1])
# Handle common escape sequences
if esc == ord(b'n'):
result.append(b'\n')
elif esc == ord(b'r'):
result.append(b'\r')
elif esc == ord(b't'):
result.append(b'\t')
elif esc == ord(b'b'):
result.append(b'\b')
elif esc == ord('f'):
result.append(b'\f')
else:
result.append(bytes([esc]))
start = self.pos + 1
# Handle octal escape sequences
elif ord(b'0') <= esc <= ord('7'):
# Parse up to 3 octal digits
val = 0
count = 0
while (count < 3 and
self.pos < self.length and
ord(b'0') <= self.data[self.pos] <= ord('7')):
val = (val << 3) + (self.data[self.pos] - ord(b'0'))
self.pos += 1
count += 1
result.append(self.data[start:self.pos-count-1])
result.append(bytes([val]))
start = self.pos
continue
self.pos += 1
elif c == ord(b'('):
depth += 1
self.pos += 1
elif c == ord(b')'):
depth -= 1
if depth == 0:
result.append(self.data[start:self.pos])
self.pos += 1
break
self.pos += 1
else:
self.pos += 1
return b''.join(result).decode('latin-1', errors='replace')
def parse_hex_string(self):
"""Parse a hex string (enclosed in angle brackets)"""
if self.get_current_char() != ord(b'<'):
return None
self.pos += 1
start = self.pos
hex_digits = b'0123456789ABCDEFabcdef'
while (self.pos < self.length and
self.data[self.pos] != ord(b'>') and
self.data[self.pos] in hex_digits + b' \t\n\r\f\0'):
self.pos += 1
if self.pos >= self.length or self.data[self.pos] != ord(b'>'):
return None
hex_str = self.data[start:self.pos].translate(None, b' \t\n\r\f\0')
self.pos += 1 # Skip the '>'
# Convert hex string to bytes
try:
return bytes.fromhex(hex_str.decode('ascii')).decode('latin-1', errors='replace')
except:
return None
def parse_number_or_ref(self):
"""Parse a number or object reference"""
start = self.pos
# Handle sign
if self.get_current_char() in b'+-':
self.pos += 1
# Parse integer part
while (self.pos < self.length and
ord(b'0') <= self.data[self.pos] <= ord('9')):
self.pos += 1
# Parse decimal part
if (self.pos < self.length and
self.data[self.pos] == ord(b'.')):
self.pos += 1
while (self.pos < self.length and
ord(b'0') <= self.data[self.pos] <= ord('9')):
self.pos += 1
# Check if this is an object reference (number number R)
saved_pos = self.pos
self.skip_whitespace()
if (self.pos + 1 < self.length and
ord(b'0') <= self.data[self.pos] <= ord('9')):
# Parse second number
start2 = self.pos
while (self.pos < self.length and
ord(b'0') <= self.data[self.pos] <= ord('9')):
self.pos += 1
self.skip_whitespace()
if (self.pos < self.length and
self.data[self.pos] == ord(b'R')):
# It's a reference
obj_num = int(self.data[start:start2])
gen_num = int(self.data[start2:self.pos])
self.pos += 1
return {'type': 'reference', 'obj_num': obj_num, 'gen_num': gen_num}
# Not a reference, just a number
self.pos = saved_pos
return float(self.data[start:self.pos].decode('ascii'))
def parse_keyword(self):
"""Parse boolean or null keywords"""
if self.data.startswith(b'true', self.pos):
self.pos += 4
return True
elif self.data.startswith(b'false', self.pos):
self.pos += 5
return False
elif self.data.startswith(b'null', self.pos):
self.pos += 4
return None
return None
def find_dict_end(data, start_pos):
"""Find the matching '>>' for a '<<' at start_pos, handling nesting."""
if not data.startswith(b'<<', start_pos):
return -1
depth = 1
pos = start_pos + 2 # Skip the opening '<<'
length = len(data)
while pos < length - 1: # Need at least 2 bytes left for '>>'
if data.startswith(b'<<', pos):
depth += 1
pos += 2
elif data.startswith(b'>>', pos):
depth -= 1
if depth == 0:
return pos + 2 # Return position after the closing '>>'
pos += 2
else:
pos += 1
return -1 # No matching '>>' found
def parse_pdf_parameters(data):
"""Parse PDF parameters from binary data into a structured dictionary"""
if not data:
return {}
# Look for dictionary pattern
dict_start = data.find(b'<<')
if dict_start >= 0:
dict_end = find_dict_end(data, dict_start)
if dict_end > dict_start:
# Create a new parser with just the dictionary content
parser = pdf_param_parser(b'<<' + data[dict_start+2:dict_end] + b'>>')
return parser.parse_dictionary() or {}
# If no dictionary found or error, try parsing as is
parser = pdf_param_parser(data)
return parser.parse_dictionary() or {}