-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryparser.py
More file actions
executable file
·123 lines (100 loc) · 3.58 KB
/
binaryparser.py
File metadata and controls
executable file
·123 lines (100 loc) · 3.58 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
#!/usr/bin/python
# Imports:
import sys
import argparse
import operator
import packetdefinition
import pandas as pd
import datatypes
def readByteFromFile(fname):
'''read byte from binary file
Args:
fname: filename of the binary file
Returns:
byte: a single byte read from the file
Examples:
for byte in readByteFromFile("/path/to/file.bin"):
# do things
Description:
This generator opens up a binary file for reading and
yields one byte at a time until the entire file has been
read'''
with open(fname, "rb") as f:
byte = f.read(1)
while byte:
yield ord(byte)
byte = f.read(1)
def readPacketFromFile(fname):
'''read sbpp packet from binary file
Args:
fname: filename of the binary file
Returns:
packet: a single packet read from the file
Examples:
for packet in readPacketFromFile("/path/to/file.bin"):
# do things
Description:
This generator opens and reads a binary file, parsing and
yielding one sbpp packet from the file at a time. If an invalid
packet is found, it is ignored, and not returned'''
def calculateChecksum(byteList):
'''Calculate the sbpp packet checksum, an xor of the entire packet'''
return reduce(operator.xor, byteList)
byteGenerator = readByteFromFile(fname)
for byte in byteGenerator:
#print chr(byte)
# Sense the start of a packet
if byte == ord('B'):
byte = byteGenerator.next()
if byte == ord('R'):
byte = byteGenerator.next()
if byte == ord('R'):
byte = byteGenerator.next()
if byte == ord('I'):
byte = byteGenerator.next()
if byte == ord('S'):
byte = byteGenerator.next()
if byte == ord('O'):
byte = byteGenerator.next()
if byte == ord('N'):
byte = byteGenerator.next()
if byte == ord(':'):
# Get length:
data = []
for i in xrange(4):
byte = byteGenerator.next()
data.append(byte)
length = datatypes.bytes2uint32(data)
# Get data:
data = []
for i in xrange(length):
byte = byteGenerator.next()
data.append(byte)
yield data
# Main:
def parse(binaryFile, packetDefinitionFile):
''' Takes a BOPPS binary file name and a packet definition filename and returns
a pandas dataframe containing all the parsed binary data for which a valid specification
exists in the packet definition file. '''
# Read and parse the packet definition file to obtain a dictionary of packet parsing functions:
packetParser = packetdefinition.readPacketDefinitionFromFile(packetDefinitionFile)
if not packetParser:
print "reading the packet definition file '" + str(packetDefinitionFile) + "' failed!"
raise
# Read packets from file and parse each one in turn:
parsedpackets = []
for packet in readPacketFromFile(binaryFile):
# Form a list of dictionaries for each packet:
parsedPacket = packetParser(packet)
if parsedPacket:
parsedpackets.append(parsedPacket)
# Combine parsed packets together into single pandas dataframe:
df = pd.DataFrame(parsedpackets)
return df
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='This utility opens and parses a binary controllaw file into a pandas data frame.')
parser.add_argument('config', metavar='configfile', type=str, help='a SBPP packet definition file to ingest')
parser.add_argument('fname', metavar='binaryfile', type=str, help='a SBPP binary file to ingest')
args = parser.parse_args()
df = parse(binaryFile = args.fname, packetDefinitionFile = args.config)
print df