# (C) Kirils Solovjovs, 2017 from warnings import warn from struct import unpack from collections import OrderedDict class MTConfig(object): def __init__(self, datFileName, idxFileName=None): self.__db = open(datFileName, 'rb') if idxFileName is None: self.__index = None else: self.__index = open(idxFileName, 'rb') self.__ip = 0 self.mapping = {0xfe0009 : 'comment', 0xfe0001: 'record_id', 0xfe000a: 'disabled', 0xfe000d: 'default', 0xffffff: 'index_id', 0xfe0008: 'active', 0xfe0010: 'name', } self.decode = False self.returnTypes = False self.preserveOrder = False self.parsers = {} self.filters = {} self.__idFormat = "_%c%x" self.__idFormatTypeLocation = 1 @property def idFormat(self): return self.__idFormat @idFormat.setter def idFormat(self, idFormat): for i in map(chr, range(33, 48)): if i not in idFormat: break if i == "0": raise Exception("Unable to detect type position inside id format. Try using less characters.") self.__idFormat = idFormat test = idFormat %(i,0xAABBCC) if i in test: self.__idFormatTypeLocation = test.index(i) else: self.__idFormatTypeLocation = None def __iter__(self): return self def next(self): if self.__index is None: try: size, = unpack("> 24 blen = None data = None ''' btype ........ 0000000, - boolean ,,1,1,,, - M2 block (len = short) ,,11,,,, - binary data block (len = short) ,,,,,,,1 - one byte ,,,,,,1, - ??? ,,,,,1,, - ??? ,,,11,,, - 128bit int ,,,,1,,, - int (four bytes) ,,,1,,,, - long (8 bytes) ,,1,,,,, - string ,1,,,,,, - ??? unused? or long array of? 1,,,,,,, - short array of types (MT notation) (CAPITAL X = list of x) a,A, (0x18) IPv6 address (or duid) b,B, bool M, multi q,Q, (0x10) big number r,R, (0x31) mac address s,S, (0x21) string u,U, unsigned integer ''' if btype == 0x21: #freelength short string blen, = unpack("B",record[bpointer]) bpointer += 1 data = record[bpointer:bpointer+blen] mtype = "s" elif btype == 0x31: #freelength list of bytes (mac address) blen, = unpack("B",record[bpointer]) bpointer += 1 mtype = "r" data = map(ord,record[bpointer:bpointer+blen]) elif btype == 0x08: #int blen = 4 data, = unpack(" (%02X) %s %s {%i}" % (btype & ~0x80, block, type(data), len(item)) for block_s in item: (btype_s,data_s) = item[block_s] print " "*(2 if (btype & 0x80 == 0x80) else 1)+"-> (%02X) %s %s %s%s" % (btype_s, block_s, type(data_s),"{%i} "%len(data_s) if isinstance(data_s,(str,list,dict)) else "", data_s) print "End of record" print