-#!/usr/bin/env python
+#!/usr/bin/env python3
#
# Migration Stream Analyzer
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
-# version 2 of the License, or (at your option) any later version.
+# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, see <http://www.gnu.org/licenses/>.
-from __future__ import print_function
-import numpy as np
import json
import os
import argparse
import collections
-import pprint
+import struct
+import sys
+
def mkdir_p(path):
try:
except OSError:
pass
+
class MigrationFile(object):
def __init__(self, filename):
self.filename = filename
self.file = open(self.filename, "rb")
def read64(self):
- return np.asscalar(np.fromfile(self.file, count=1, dtype='>i8')[0])
+ return int.from_bytes(self.file.read(8), byteorder='big', signed=True)
def read32(self):
- return np.asscalar(np.fromfile(self.file, count=1, dtype='>i4')[0])
+ return int.from_bytes(self.file.read(4), byteorder='big', signed=True)
def read16(self):
- return np.asscalar(np.fromfile(self.file, count=1, dtype='>i2')[0])
+ return int.from_bytes(self.file.read(2), byteorder='big', signed=True)
def read8(self):
- return np.asscalar(np.fromfile(self.file, count=1, dtype='>i1')[0])
+ return int.from_bytes(self.file.read(1), byteorder='big', signed=True)
def readstr(self, len = None):
- if len is None:
- len = self.read8()
- if len == 0:
- return ""
- return np.fromfile(self.file, count=1, dtype=('S%d' % len))[0]
+ return self.readvar(len).decode('utf-8')
def readvar(self, size = None):
if size is None:
# Find the last NULL byte, then the first brace after that. This should
# be the beginning of our JSON data.
- nulpos = data.rfind("\0")
- jsonpos = data.find("{", nulpos)
+ nulpos = data.rfind(b'\0')
+ jsonpos = data.find(b'{', nulpos)
# Check backwards from there and see whether we guessed right
self.file.seek(datapos + jsonpos - 5, 0)
# Seek back to where we were at the beginning
self.file.seek(entrypos, 0)
- return data[jsonpos:jsonpos + jsonlen]
+ # explicit decode() needed for Python 3.5 compatibility
+ return data[jsonpos:jsonpos + jsonlen].decode("utf-8")
def close(self):
self.file.close()
return str(self.__str__())
def __str__(self):
- return " ".join("{0:02x}".format(ord(c)) for c in self.data)
+ return " ".join("{0:02x}".format(c) for c in self.data)
def getDict(self):
return self.__str__()
def read(self):
super(VMSDFieldInt, self).read()
- self.sdata = np.fromstring(self.data, count=1, dtype=(self.sdtype))[0]
- self.udata = np.fromstring(self.data, count=1, dtype=(self.udtype))[0]
+ self.sdata = int.from_bytes(self.data, byteorder='big', signed=True)
+ self.udata = int.from_bytes(self.data, byteorder='big', signed=False)
self.data = self.sdata
return self.data
array_len = field.pop('array_len')
field['index'] = 0
new_fields.append(field)
- for i in xrange(1, array_len):
+ for i in range(1, array_len):
c = field.copy()
c['index'] = i
new_fields.append(c)
dump.read(desc_only = True)
print("desc.json")
- f = open("desc.json", "wb")
+ f = open("desc.json", "w")
f.truncate()
f.write(jsonenc.encode(dump.vmsd_desc))
f.close()
dump.read(write_memory = True)
dict = dump.getDict()
print("state.json")
- f = open("state.json", "wb")
+ f = open("state.json", "w")
f.truncate()
f.write(jsonenc.encode(dict))
f.close()
dump.read(desc_only = True)
print(jsonenc.encode(dump.vmsd_desc))
else:
- raise Exception("Please specify either -x, -d state or -d dump")
+ raise Exception("Please specify either -x, -d state or -d desc")