1 # A script for analyzing the output of NPSPY and merging data about streams.
\r
6 def ReadFile(filename, flags='rb'):
\r
7 """Returns the contents of a file."""
\r
8 file = open(filename, flags)
\r
14 def WriteFile(filename, contents):
\r
15 """Overwrites the file with the given contents."""
\r
16 file = open(filename, 'w')
\r
17 file.write(contents)
\r
21 # sample line: 'NPP_NewStream(0x645c898, 0x56ba900("application/x-shockwave-flash"), 0x64bb3b0 (http://weeklyad.target.com/target/flash/target/target.swf?ver=090326), TRUE, NP_NORMAL)'
\r
23 def __init__(self, line):
\r
24 split = line.split(', ')
\r
26 self.mime_type = split[1].split('"')[1]
\r
27 self.url = split[2].split(' ')[1].strip('()')
\r
28 self.seekable = split[3]
\r
29 self.type = split[4].strip(')')
\r
33 self.address = split[2].split(' ')[0]
\r
35 print 'parsing error on ' + line
\r
38 if self.type != 'NP_NORMAL':
\r
39 print 'line got unexpected type: ' + line
\r
42 def main(argv=None):
\r
49 print 'need filename'
\r
51 file = ReadFile(argv[1])
\r
52 for line in file.splitlines():
\r
53 if line.startswith('NPP_NewStream('):
\r
54 if line.count('(') < 3:
\r
55 print 'unknown format for line: ' + line
\r
60 elif line.startswith('NPP_Write('):
\r
61 # sample: NPP_Write(0x645c898, 0x64bb3b0, 0, 16384, 0x56c1000("CW")))
\r
62 split = line.split(', ')
\r
64 start = int(split[2])
\r
65 size = int(split[3])
\r
67 for stream in streams:
\r
68 if stream.address == address:
\r
69 if stream.size != start:
\r
70 print 'error: starting at wrong place for write ' + stream.url + ' ' + str(stream.size) + ' ' + str(start)
\r
76 print "couldn't find stream to match NPP_Write " + line
\r
77 elif line.startswith('NPP_DestroyStream('):
\r
78 # sample: NPP_DestroyStream(0x645c898, 0x64bb3b0, NPRES_DONE)
\r
79 split = line.split(', ')
\r
81 status = split[2].strip(')')
\r
83 for stream in streams:
\r
84 if stream.address == address:
\r
85 stream.status = status
\r
86 stream.address = '' # address can be reused
\r
91 print "couldn't find stream to match NPP_DestroyStream " + line
\r
95 for stream in streams:
\r
96 if stream.status != 'NPRES_DONE':
\r
97 print 'error: no NPP_DestroyStream with success for ' + stream.url + ' ' + stream.status + '.'
\r
98 output.append(', '.join([stream.url, stream.mime_type, str(stream.size), stream.seekable]))
\r
99 output_file = argv[1].replace('.', '_analyzed.')
\r
101 WriteFile(output_file, '\n'.join(output))
\r
104 if __name__ == "__main__":
\r