0

How can i search for a specified byte array and replace everything from the beginning to this byte array (included)

Basically i have my pattern to find

byte[] find = { 0x00, 0x48, 0x00 };

and

byte[] ddsHeaderDXT1 = { 0x44, 0x44, 0x53, 0x20, 0x7C, 0x00, 0x00, 0x00, 0x07, 0x10, 0x08, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4E, 0x56, 0x54, 0x54, 0x06, 0x00, 0x02, 0x00, 0x20, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x44, 0x58, 0x54, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 };

the thing i wanna do is to search for my find array, once it's found, select all bytes from the beginning of the file to this find array included, and replace all this with my ddsHeaderDXT1 array. The file can be pretty big but the pattern is always at the beginning (less than 500 first bytes), not always at the same offset and can be found only once.

I have already tried rene's code here but it doesn't do anything. Also i get my file from OpenFileDialog and read its bytes

byte[] src = File.ReadAllBytes(ofdFilePath);
Asval
  • 3
  • 1
  • "_I have already tried rene's code here but it doesn't do anything_". I doubt it does not do anything. What have you tried exactly, and how did that not work precisely as you expected? –  Nov 18 '18 at 19:32
  • If you want to replace everything from the beginning and `ddsHeaderDXT1` is shorter than the position found, what do you expect to happen? – Olivier Jacot-Descombes Nov 18 '18 at 19:38
  • @elgonzo i have tried `ReplaceBytes(src, find, ddsHeaderDXT1);` and it just didn't change my file but there was no error, maybe i did something wrong. @OlivierJacot-Descombes then i just wanna insert and increase the file size ¯\_(ツ)_/¯ – Asval Nov 18 '18 at 20:06
  • What I mean is, `ddsHeaderDXT1` has a length of 110. If you find the pattern at position 400, you can fill the bytes at indexes 0 .. 109 with it. With what are you going to fill the bytes 110 .. 399? – Olivier Jacot-Descombes Nov 18 '18 at 20:10
  • @OlivierJacot-Descombes then i just wanna delete the rest – Asval Nov 18 '18 at 20:11

1 Answers1

0

The following should do the trick:

public static bool TryFindAndReplace<T>(
    T[] source,
    T[] pattern,
    T[] replacement,
    out T[] newArray)
{
    if (source == null)
        throw new ArgumentNullException(nameof(source));

    if (pattern == null)
        throw new ArgumentNullException(nameof(pattern));

    if (replacement == null)
        throw new ArgumentNullException(nameof(replacement));

    newArray = null;

    if (pattern.Length > source.Length)
        return false;

    for (var start = 0; 
         start < source.Length - pattern.Length + 1; 
         start += 1)
    {
        var segment = new ArraySegment<T>(source, start, pattern.Length);

        if (Enumerable.SequenceEqual(segment, pattern))
        {
            newArray = replacement.Concat(source.Skip(start + pattern.Length))
                                  .ToArray();
            return true;
        }
    }

    return false;
}

Simple to read and therefore simple to understand. Its not the fastest solution by any means, but things need to be as fast as you need them to be, not faster.

If you need a more performant implementation, you can optimize from here.

InBetween
  • 32,319
  • 3
  • 50
  • 90
  • thanks for the answer, i'm still kinda new to c#, what does means and how can i use this? – Asval Nov 18 '18 at 20:17
  • @Asval it’s generics. It enables you to being able to use this method with any type, not just bytes. Think of `T` as “insert type here” – InBetween Nov 18 '18 at 21:41
  • oh alright i got it, it's smart :) thanks btw your code works like a charm :D – Asval Nov 18 '18 at 22:25