def test_zeroes(self): z1 = fieldtypes.ZeroField('one', 0, 1) self.assertEqual(len(z1), 1) self.assertIn('ctypes.c_ubyte*1 )', z1.to_string('\x00\x00\x00\x00')) z2 = fieldtypes.ZeroField('two', 0, 2) self.assertEqual(len(z2), 2) self.assertIn('ctypes.c_ubyte*2 )', z2.to_string('\x00\x00\x00\x00'))
def _find_zeroes(self, _record, offset, size): """ iterate over the bytes until a byte if not \x00 """ _bytes = _record.bytes # print 'offset:%x blen:%d'%(offset, len(bytes)) # print repr(bytes) assert (offset % self._word_size == 0) # aligned_off = (offset)%self._target_platform.get_word_size() start = offset # if aligned_off != 0: # align to next # start += (self._target_platform.get_word_size() - aligned_off) # size -= (self._target_platform.get_word_size() - aligned_off) # iterate matches = array.array('i') for i in range(start, start + size, self._word_size): # PERF TODO: bytes or struct test ? # print repr(bytes[start+i:start+i+self._target_platform.get_word_size()]) if _w(_bytes[start + i:start + i + self._word_size]) == self._zeroes: matches.append(start + i) # print matches # collate if len(matches) == 0: return [] # lets try to get fields fields = [] # first we need to collate neighbors collates = list() prev = matches[0] - self._word_size x = [] # PERF TODO: whats is algo here for i in matches: if i - self._word_size == prev: x.append(i) else: collates.append(x) x = [i] prev = i collates.append(x) # log.debug(collates) # we now have collated, lets create fields for field in collates: flen = len(field) if flen > 1: size = self._word_size * flen elif flen == 1: size = self._word_size else: continue # make a field _offset = start + field[0] fields.append( fieldtypes.ZeroField('zerroes_%d' % _offset, _offset, size)) # we have all fields return fields