Created
August 2, 2016 22:02
-
-
Save spiiin/500262e8d9da86f10a093bbb41833360 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def findBlocksInRom(blocks, romData): | |
#инициализация массива найденных индексов | |
blockFound = [-1,]*len(romData) | |
#ШАГ 1 Размечаем найденные в ROM блоки | |
for blockIndex, block in enumerate(blocks): | |
#экранируем символы в строке для поиска | |
blockStr = re.escape(''.join(blockStr)) | |
#отмечаем все адреса, по которым нашёлся блок | |
for m in re.finditer(blockStr, romData, re.DOTALL): | |
blockFound[m.start()] = blockIndex | |
#ШАГ 2 Находим самую длинную цепочки из разных найденных блоков | |
def calcLongestStrip(blockFound, blockBeginStride): | |
blockBeginStride = 4 | |
longestStrip = [] | |
blockFoundLen = len(blockFound) | |
for x in xrange(blockFoundLen): | |
curIndexes = set() | |
#если по данному адресу был найден блок, проверим, сколько блоков найдено в окрестности 256 байт него | |
if blockFound[x] == -1: | |
continue | |
for lenIndex in xrange(256): | |
ind = x + lenIndex * blockBeginStride | |
if ind >= blockFoundLen: | |
break | |
if blockFound[ind] != -1: | |
curIndexes.add(blockFound[ind]) | |
if len(curIndexes) > 3: | |
longestStrip.append((x, len(curIndexes), curIndexes)) | |
#сортируем результат по частоте встреченных блоков | |
return sorted(longestStrip, key = lambda v:v[1], reverse = True) | |
return calcLongestStrip(blockFound, blockBeginStride) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment