-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacter_parser.py
More file actions
54 lines (38 loc) · 1.28 KB
/
Copy pathcharacter_parser.py
File metadata and controls
54 lines (38 loc) · 1.28 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
import json
import base64
import binascii
def parse_png(bytes_data):
if not is_valid_png(bytes_data):
return None
decoded_png = get_text_chunk(bytes_data)
if decoded_png is None:
return None
return json.loads(decoded_png)
def is_valid_png(bytes_data):
if len(bytes_data) < 8:
return False
png_signature = b"\x89PNG\r\n\x1a\n"
return bytes_data.startswith(png_signature)
def get_text_chunk(bytes_data):
offset = 8
base64_data = None
while offset < len(bytes_data):
if offset + 8 > len(bytes_data):
break
length = int.from_bytes(bytes_data[offset : offset + 4], byteorder="big")
offset += 4
chunk_type = bytes_data[offset : offset + 4].decode("utf-8")
offset += 4
if chunk_type == "tEXt":
null_separator_index = bytes_data.index(0, offset)
if null_separator_index == -1:
break
try:
text = bytes_data[null_separator_index + 1 : offset + length].decode(
"utf-8"
)
base64_data = base64.b64decode(text).decode("utf-8")
except (binascii.Error, UnicodeDecodeError):
return None
offset += length + 4
return base64_data