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