来自xml文件的vb.net十六进制扫描

时间:2021-03-13 14:03:17

I was experimenting with some hex scanner sources.

我正在尝试一些十六进制扫描仪源。

Following code works but is very slow:

以下代码有效,但速度很慢:

Public Class frmMain

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadFile.Click

        Dim ArrayHold() As Byte
        Dim Index As Integer = 0
        Dim Str As New StringBuilder
        Dim tStr As String = ""
        Dim tempStr As String = ""
        Dim IndexEnd As Integer = 0
        Dim InputString As String = ""

        OpenDia.Filter = "All Files|*.*"

        If OpenDia.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim myStreamReader As StreamReader = Nothing
            myStreamReader = File.OpenText(OpenDia.FileName)
            InputString = myStreamReader.ReadToEnd()
            ArrayHold = Encoding.Default.GetBytes(InputString)

            Do
                IndexEnd = Index + 9

                For x As Integer = Index To IndexEnd

                    If x > UBound(ArrayHold) Then
                        tempStr = tempStr
                    Else
                        tStr = UCase(Convert.ToString(ArrayHold(x), 16))

                    If tStr.Length < 2 Then tStr = "0" & tStr

                    Str.Append(tStr)
                    tempStr = tempStr & Chr(ArrayHold(x))

                    End If
                Next

                Index = Index + 10
            Loop While IndexEnd < UBound(ArrayHold)

            If InStr(1, Str.ToString, "58354f2150254041505b345c505a58353428505e2937434329377d2445494341522d5354414e4441", vbTextCompare) Then
                Label1.Text = "Eicar-test-signature virus Detected!"
            End If
        End If
    End Sub
End Class

To speed it up I can use this format in XML file:

为了加快速度,我可以在XML文件中使用这种格式:

?xml version="1.0"?>
<signatures>
  <signature>
    <name>Eicar-Test-Signature</name>
      <hex>58354f2150254041505b345c505a58353428505e2937434329377d2445494341522d5354414e4441</hex>
  </signature>
  <signature>
    <name>Mid/Kakworm-Z</name>
      <hex>66732e4372656174655465787446696c652877642b276b616b2e72656727293b74322e77726974652827524547454449</hex>
  </signature>
</signatures>

But I don't know how to read and implement XML files in VB.NET. Is it hard and can anyone help?

但我不知道如何在VB.NET中阅读和实现XML文件。这很难,任何人都可以帮忙吗?

2 个解决方案

#1


4  

Okay, here is an example of how you can read your XML file:

好的,这是一个如何读取XML文件的示例:

Dim xml = <?xml version="1.0"?>
          <signatures>
            <signature>
              <name>Eicar-Test-Signature</name>
              <hex>58354f2150254041505b345c505a58353428505e2937434329377d2445494341522d5354414e4441</hex>
            </signature>
            <signature>
              <name>Mid/Kakworm-Z</name>
              <hex>66732e4372656174655465787446696c652877642b276b616b2e72656727293b74322e77726974652827524547454449</hex>
            </signature>
          </signatures>

Dim dict As New Dictionary(Of String, String)
For Each signature As XElement In xml.Root.Elements
  dict.Add(signature.<name>.Value, signature.<hex>.Value)
Next

Instead of having embedded XML in your code (as in above example), you would probably be using XDocument.Load or XDocument.Parse.

您可能正在使用XDocument.Load或XDocument.Parse,而不是在代码中嵌入XML(如上例所示)。

#2


2  

Neolisk covered the LINQ to XML part nicely. Here's a way to simplify your loop to one single For loop, using Step. Also, it uses the StreamReader in a Using block, which will ensure that the StreamReader is properly closed and disposed of.

Neolisk很好地介绍了LINQ to XML部分。这是一种使用Step简化循环到单个For循环的方法。此外,它在Using块中使用StreamReader,这将确保StreamReader被正确关闭和处理。

If OpenDia.ShowDialog = Windows.Forms.DialogResult.OK Then

    Using myStreamReader As StreamReader = File.OpenText(openDia.FileName)

        ArrayHold = Encoding.Default.GetBytes(myStreamReader.ReadToEnd())
    End Using

    Dim arrayLength As Integer = ArrayHold.Length - 1

    For i As Integer = 0 To arrayLength Step 10
        Str.Append(UCase(Convert.ToString(ArrayHold(i), 16).PadLeft(2, "0"c)))
    Next

    If dict.ContainsKey(Str.ToString()) Then
        Label1.Text = dict(str.ToString())
    End If 
End If

This uses StringBuilder without the extra strings and should give a minor boost in performance as well, though I'm betting your biggest hit was looping through the string you build one character at a time, checking each character.

这使用StringBuilder而没有额外的字符串,并且应该稍微提升性能,虽然我打赌你最大的打击是循环通过你一次构建一个字符的字符串,检查每个字符。

Edit

Added code for checking the dictionary (based on Neolisk's answer for parsing the XML to a dictionary) for completeness.

添加了用于检查字典的代码(基于Neolisk将XML解析为字典的答案)以获得完整性。

#1


4  

Okay, here is an example of how you can read your XML file:

好的,这是一个如何读取XML文件的示例:

Dim xml = <?xml version="1.0"?>
          <signatures>
            <signature>
              <name>Eicar-Test-Signature</name>
              <hex>58354f2150254041505b345c505a58353428505e2937434329377d2445494341522d5354414e4441</hex>
            </signature>
            <signature>
              <name>Mid/Kakworm-Z</name>
              <hex>66732e4372656174655465787446696c652877642b276b616b2e72656727293b74322e77726974652827524547454449</hex>
            </signature>
          </signatures>

Dim dict As New Dictionary(Of String, String)
For Each signature As XElement In xml.Root.Elements
  dict.Add(signature.<name>.Value, signature.<hex>.Value)
Next

Instead of having embedded XML in your code (as in above example), you would probably be using XDocument.Load or XDocument.Parse.

您可能正在使用XDocument.Load或XDocument.Parse,而不是在代码中嵌入XML(如上例所示)。

#2


2  

Neolisk covered the LINQ to XML part nicely. Here's a way to simplify your loop to one single For loop, using Step. Also, it uses the StreamReader in a Using block, which will ensure that the StreamReader is properly closed and disposed of.

Neolisk很好地介绍了LINQ to XML部分。这是一种使用Step简化循环到单个For循环的方法。此外,它在Using块中使用StreamReader,这将确保StreamReader被正确关闭和处理。

If OpenDia.ShowDialog = Windows.Forms.DialogResult.OK Then

    Using myStreamReader As StreamReader = File.OpenText(openDia.FileName)

        ArrayHold = Encoding.Default.GetBytes(myStreamReader.ReadToEnd())
    End Using

    Dim arrayLength As Integer = ArrayHold.Length - 1

    For i As Integer = 0 To arrayLength Step 10
        Str.Append(UCase(Convert.ToString(ArrayHold(i), 16).PadLeft(2, "0"c)))
    Next

    If dict.ContainsKey(Str.ToString()) Then
        Label1.Text = dict(str.ToString())
    End If 
End If

This uses StringBuilder without the extra strings and should give a minor boost in performance as well, though I'm betting your biggest hit was looping through the string you build one character at a time, checking each character.

这使用StringBuilder而没有额外的字符串,并且应该稍微提升性能,虽然我打赌你最大的打击是循环通过你一次构建一个字符的字符串,检查每个字符。

Edit

Added code for checking the dictionary (based on Neolisk's answer for parsing the XML to a dictionary) for completeness.

添加了用于检查字典的代码(基于Neolisk将XML解析为字典的答案)以获得完整性。