0

I writing a Visual Basic app and stuck at one point:
I need that app read the file, select everything before DDS string, cut it from file and paste to new file.

Image

Then after edit a DDS insert that header.

Problem is, this header before DDS have not fixed length: every file of this type have different header. I tried to mess with System.IO.FileStream but got no result.

Is this even possible to do?

Jimi
  • 29,621
  • 8
  • 43
  • 61
ShevNR
  • 3
  • 1
  • There is code in [byte\[\] array pattern search](https://stackoverflow.com/q/283456/1115360) which will help you do that. You can use the [File.ReadAllBytes(String) Method](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readallbytes?view=netframework-4.8) to get the bytes from the file into an array. – Andrew Morton Apr 11 '20 at 11:47
  • Thanks, I look on it. – ShevNR Apr 11 '20 at 11:52

1 Answers1

0

The header length is not that much, a simple search pattern is probably enough.

Pass the sequence of Bytes to find inside the File and the File path to the FindHeader method.
It returns a byte array containing all the bytes collected before the specified sequence is found.

This is a simple patter matching that seeks forward until it finds the first byte that can match the specified sequence.
It then reads a buffer and compares the buffer with the sequence:
- if it's a match, returns the bytes accumulated until that point;
- if it's not, it backtracks from the current position of a [Sequence Length] - 1 positions (inside the current Stream buffer) and continues.

You can call it like this:

Dim closeSequence = New Byte() { &H44, &H44, &H53 }
Dim headerBytes = FindHeader([Source File 1 Path], closeSequence)

Now we have the Header of the first source file.

The data section of the second source file is then:

Dim sourceFile2DataStart = FindHeader([Source File 2 Path], closeSequence).Length + closeSequence.Length
Dim dataLength = New FileInfo([Source File 2 Path]).Length - sourceFile2DataStart

We need to create a third file which will contain the Header of the fist file and the data read from the second file.

' Create a read buffer. The buffer length is less than or equal to the data length
Dim bufferLength As Integer = CInt(If(dataLength >= 1024, 1024, dataLength))
Dim buffer As Byte() = New Byte(bufferLength - 1) {}
Dim read As Integer = 0

Using two FileStream objects, we create a new Destination File, write the header of the first Source File, the closeSequence that identifies the start of the data section, then we read a buffer from the second Source File and write the buffer to the Destination File:

Dim patchworkFilePath as string = [Path of the Destination File]

Using sWriter As FileStream = New FileStream(patchworkFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None),
    sReader As FileStream = New FileStream([Source File 2 Path], FileMode.Open, FileAccess.Read, FileShare.None)
    sReader.Seek(sourceFile2DataStart, SeekOrigin.Begin)
    sWriter.Write(header1Bytes, 0, header1Bytes.Length)
    sWriter.Write(closeSequence, 0, closeSequence.Length)

    While True
        read = sReader.Read(buffer, 0, buffer.Length)
        If read = 0 Then Exit While
        sWriter.Write(buffer, 0, read)
    End While
End Using

The Header reader method:

Public Function FindHeader(filePath As String, headerClosure As Byte()) As Byte()
    Dim byteToFind = headerClosure(0)
    Dim buffer = New Byte(headerClosure.Length - 1) {}
    Dim header = New List(Of Byte)(2048)
    Dim read As Integer = 0

    Using fs As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None)
        While fs.Position <= (fs.Length - headerClosure.Length)
            read = fs.ReadByte()
            If read = byteToFind Then
                fs.Read(buffer, 1, buffer.Length - 1)
                buffer(0) = CByte(read)
                If buffer.SequenceEqual(headerClosure) Then Exit While
                fs.Seek(-(buffer.Length - 1), SeekOrigin.Current)
            End If
            header.Add(CByte(read))
        End While
    End Using
    Return header.ToArray()
End Function
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • Appreciate for great answer, works well! I slightly changed it (I want to get separate files from file (.xbt -> .hdr (header) + .dds) I managed to get only .hdr binary data from .xbt, now will try to get .dds). Thanks. – ShevNR Apr 12 '20 at 20:16