Python 3搜索两个二进制文件的差异

时间:2014-03-07 20:15:26

标签: python python-3.x comparison hex

我正在寻找一个程序,它将从两个RAW磁盘映像中获取输入然后打开它们,以便我可以找出十六进制值的两个十六进制值之间的差异。然后我想看看经常出现的差异,以便我可以对其进行某种分析。

这是我已经拥有的代码,我认为它应该可行,但事实并非如此。它偶尔会挂在hexFormat中的formattedCorrectly行上,但我想我已经解决了这个问题。现在的问题是,我知道我给它的文件有差异,但它不会返回任何文件。

def main():
    print("You must enter a valid raw/dd image of a drive.")
    given = input("Enter path to the first image file: ")
    given2 = input("Enter path to the second image file: ")
    firstDiffs = []
    secondDiffs = []
    x = 0
    y = 1
    with open(given, "rb") as infile:
    #   data = infile.read()
        with open(given2, "rb") as infile2:
    #       data2 = infile2.read()

            while x < y:
                data = infile.read(1024 + 1 * x)
                data2 = infile2.read(1024 + 1 * x)
                value1 = hexFormat(data, x)
                value2 = hexFormat(data2, x)
                if value1 != value2:
                    print("The values at location ", x,"do not match")
                    print("The value from file 1 is: ", value1)
                    print("The value from file 2 is: ", value2)
                    firstDiffs.append(value1)
                    secondDiffs.append(value2)
                    firstDiff = firstDiffs.count(value1)
                    secondDiff = secondDiffs.count(value2)
                    print("This is the ", firstDiff,"occurrence of this value coming from file 1.")
                    print("This is the ", secondDiff, "occurrence of this value coming from file 2.")
                    infile.close()
                    infile2.close()
                else:
                    x = x + 1
                    y = y + 1

def hexFormat(data, location): #This function is used to properly format the hex values being read off the disk
    formattedCorrectly = hex(data[location]) #This created formattedCorrectly as the hex value found at the location given
    strippedData = formattedCorrectly[2:] #This removes the "0x" from in front of the value to allow for easier processing
    if len(strippedData) == 2: #This if statement just ensures that two values are returned, as hex values of double zero and with a leading zero were being problematic
        return (strippedData)
    elif len(strippedData) == 1:
        return ("0"+strippedData)
    else:
        return ("00")


main()

感谢您的帮助, -DJ

0 个答案:

没有答案