Mysterius一字节溢出 [英] Mysterius one byte overflow
问题描述
嘿..!
再次让我陷入了困境……
我正在制作一个程序,可以读取文件(exe和dll)并进行比较一个字节接一个字节,然后保存差异....
现在,我遇到了问题...当我读取文件时,它崩溃了.当二进制读取器到达位置3(RAW偏移量0x03)时,它崩溃并显示以下语句:算术运算导致溢出.我尝试了不同的文件,它始终位于位置3.
这是我的代码:
Hey..!
Once again i got myself into a pickle...
I am making a program that reads files (exe and dll) and compares them byte for byte and then save the diffrences....
Now, i get a problem... when i read the file it crashes. when the binary reader reaches position 3 (RAW offset 0x03) it crashes with the sentence: Arithmic operation resulted in an overflow. I tried different files and its always at position 3.
Here is my code:
Dim f1 As String
Dim f1l As Long
Dim f1h As String
Dim f1c As String
Dim f1o As Long 'fil1 start offset
Dim f2 As String
Dim f2l As Long
Dim f2h As String
Dim f2c As String
Dim f2o As Long
statuslbl.Text = "Gathering Information on files"
f1 = TextBox1.Text
f1c = My.Computer.FileSystem.ReadAllText(f1)
f2 = TextBox2.Text
f2c = My.Computer.FileSystem.ReadAllText(f2)
opb.Value = 0
rpb.Value = 0
Application.DoEvents()
If FileLen(f1) > FileLen(f2) Then rpb.Maximum = FileLen(f1)
If FileLen(f1) < FileLen(f2) Then rpb.Maximum = FileLen(f2)
f1l = FileLen(f1)
f1h = getMd5Hash(f1c)
f2l = FileLen(f2)
f2h = getMd5Hash(f2c)
his.AppendText(Environment.NewLine & "File1 length: " & f1l)
his.AppendText(Environment.NewLine & "File2 length: " & f2l)
his.AppendText(Environment.NewLine)
his.AppendText(Environment.NewLine & "File1 hash: " & f1h)
his.AppendText(Environment.NewLine & "File2 hash: " & f2h)
Application.DoEvents()
statuslbl.Text = "Comparing Bytes"
Dim f1r As New IO.BinaryReader(File.Open(f1, FileMode.Open))
Dim f2r As New IO.BinaryReader(File.Open(f2, FileMode.Open))
f1o = f1r.BaseStream.Position
f2o = f2r.BaseStream.Position
his.AppendText(Environment.NewLine)
his.AppendText(Environment.NewLine & "File1 start RAWoffset: " & f1o)
his.AppendText(Environment.NewLine & "File2 start RAWoffset: " & f2o)
Dim finished As Boolean = False
Dim table() As diffrence
Dim counter As Integer = 0
While finished = False
Dim ob As Byte
Dim nb As Byte
If f1r.BaseStream.Position.Equals(f1l) = False Then
ob = f1r.Read
Else
ob = &HFF
End If
If f2r.BaseStream.Position.Equals(f2l) = False Then
nb = f2r.Read
Else
nb = &HFF
End If
If ob.Equals(nb) = False Then
Dim dif As New diffrence
dif.ByteOffset = f2r.BaseStream.Position
dif.NewByte = nb
dif.OldByte = ob
table(counter) = dif
counter = counter + 1
End If
If f1r.BaseStream.Position.Equals(f1l) = True Then
If f2r.BaseStream.Position.Equals(f2l) = True Then
finished = True
End If
End If
End While
statuslbl.Text = "Compared done"
dgv.DataSource = table
End Sub
diffrence()类是我自己编写的,下面是该代码:
The diffrence() class is one i wrote myself, here is the code for that:
Public Class diffrence
Dim boffset As Long
Dim oldb As Byte
Dim newb As Byte
Public Property ByteOffset()
Get
Return boffset
End Get
Set(ByVal value)
boffset = value
End Set
End Property
Public Property NewByte()
Get
Return newb
End Get
Set(ByVal value)
newb = value
End Set
End Property
Public Property OldByte()
Get
Return oldb
End Get
Set(ByVal value)
oldb = value
End Set
End Property
End Class
我真的希望有人可以帮我解决这个问题....!
问候
马丁
马丁·约根森
I really hope someone can help me with this....!
Regards
Martin
Martin Jørgensen
推荐答案
我不知道您是否尝试这样做,但是我使用它比较两个文件是否相等
I dont know if this what you try to do but i use it to compare two files for equality
'compare
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If TextBox1.TextLength = 0 Or TextBox3.TextLength = 0 Then
MsgBox("You must select two files to compare", MsgBoxStyle.Exclamation, "File Error")
Exit Sub
End If
Dim hash As System.Security.Cryptography.HashAlgorithm
hash = System.Security.Cryptography.HashAlgorithm.Create
Dim fs1 As New FileStream(TextBox1.Text, FileMode.Open)
Dim hashF1() As Byte = hash.ComputeHash(fs1)
fs1.Close()
Dim fs2 As New FileStream(TextBox3.Text, FileMode.Open)
Dim hashF2() As Byte = hash.ComputeHash(fs2)
fs2.Close()
If BitConverter.ToString(hashF1) = BitConverter.ToString(hashF2) Then
Label1.Text = "File is the same"
Label1.BackColor = Color.LimeGreen
Else
Label1.Text = "File is not the same"
Label1.BackColor = Color.Red
End If
End Sub
kaymaf
这篇关于Mysterius一字节溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!