我尝试将 sparse.img 解析为 unsparse.img 我已成功获得总块和第一个块,问题是当尝试获取第二个块标头时,这意味着无法获取块标头类型问题是我在哪里获得下一个标头块
我的代码用于获取缓冲区
Dim hSparseImage = -1
Dim bSparseImage = False
Dim openfile = "userdata.img"
Dim infos As New FileInfo(openfile)
Dim fileLength = infos.Length
Dim count = 1048576
Dim EXT4_CHUNK_HEADER_SIZE = 12
Dim sparseheader As SPARSE_HEADER
Dim chunkheader As CHUNK_HEADER
Dim stream As New FileStream(openfile, FileMode.Open, FileAccess.Read)
Using reader As New BinaryReader(stream)
Dim buffer(fileLength) As Byte
reader.BaseStream.Seek(0, SeekOrigin.Begin)
reader.Read(buffer, 0, 28)
sparseheader = parsingheader(buffer)
Dim magic = sparseheader.dwMagic
Dim header_magic = (Val("&HE" & Hex(magic)))
Dim dwChunkSize As Long = 0
Dim offsetnya As Long = 0
Dim nextt = 0
Dim chunkh As Integer
Dim hexchunktype As Int32
If header_magic = SPARSE_MAGIC Then
Dim totalchunk = sparseheader.dwTotalChunks
If totalchunk > 0 Then
For i = 0 To totalchunk - 1
reader.Read(buffer, 0, 12)
chunkheader = parsingchunk(buffer)
Dim chunktype = chunkheader.wChunkType
hexchunktype = (Val("&HE" & Hex(chunktype)))
If hexchunktype = SPARSE_RAW_CHUNK Then
dwChunkSize = chunkheader.dwChunkSize * sparseheader.dwBlockSize
RichTextBox1.Text += "chunk total size " & chunkheader.dwTotalSize & vbNewLine
ElseIf hexchunktype = SPARSE_FILL_CHUNK Then
RichTextBox1.Text += "fill" & vbNewLine
ElseIf hexchunktype = SPARSE_DONT_CARE Then
RichTextBox1.Text += "DoNT care" & vbNewLine
Else
RichTextBox1.Text += "Invalid data" & vbNewLine
End If
chunkh = chunkh + chunkheader.dwTotalSize
RichTextBox1.Text += i & chunkh & vbNewLine & vbNewLine
Next
Else
RichTextBox1.Text += "Pailit get total chunk"
End If
End If
End Using
stream.Close()
并用于获取结构并获取缓冲区数组以进行结构
Private Function parsingheader(ByVal bytes As Byte()) As SPARSE_HEADER
Dim stuff As SPARSE_HEADER
Dim handle As GCHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned)
Try
stuff = CType(Marshal.PtrToStructure(handle.AddrOfPinnedObject(), GetType(SPARSE_HEADER)), SPARSE_HEADER)
Finally
handle.Free()
End Try
Return stuff
End Function
Private Function parsingchunk(ByVal bytes As Byte()) As CHUNK_HEADER
Dim stuff As CHUNK_HEADER
Dim handle As GCHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned)
Try
stuff = CType(Marshal.PtrToStructure(handle.AddrOfPinnedObject(), GetType(CHUNK_HEADER)), CHUNK_HEADER)
Finally
handle.Free()
End Try
Return stuff
End Function
Private Structure SPARSE_HEADER
Public dwMagic As Int32
Public wVerMajor As Int16
Public wVerMinor As Int16
Public wSparseHeaderSize As Int16
Public wChunkHeaderSize As Int16
Public dwBlockSize As Int32
Public dwTotalBlocks As Int32
Public dwTotalChunks As Int32
Public dwImageChecksum As Int32
End Structure
Private Structure CHUNK_HEADER
Public wChunkType As Int16
Public wReserved As Int16
Public dwChunkSize As Int32
Public dwTotalSize As Int32
End Structur