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);
5
  • "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? Commented Nov 18, 2018 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? Commented Nov 18, 2018 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 ¯_(ツ)_/¯ Commented Nov 18, 2018 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? Commented Nov 18, 2018 at 20:10
  • @OlivierJacot-Descombes then i just wanna delete the rest Commented Nov 18, 2018 at 20:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the answer, i'm still kinda new to c#, what does <T> means and how can i use this?
@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”
oh alright i got it, it's smart :) thanks btw your code works like a charm :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.