C# 查找字节模式的偏移量,检查特定字节,更改字节,导出字节数组的一部分
C# Find offset of byte pattern, check specific byte, change byte, export part of byte array
这可能很长。我有一个二进制文件,其中包含一些信息。
我想做的事情:
- 从 OpenFileDialog 读取文件(二进制)
- 我正在搜索此文件中的特定字节
- 我正在获取那个字节的偏移量,然后我正在检查偏移量+2
的字节值
- 基本if for(如果offset+2的值是0x08,那么做这个,如果不是,那么做别的)
- 现在,搜索另一个字节模式的偏移量。
- 复制从该偏移量到文件末尾的所有内容
- 将复制的字节数组保存到文件。
所以,这是我的每一步代码。
第一步:
1.
Byte[] bytes;
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
path = ofd.FileName;
bytes = File.ReadAllBytes(path);
第二步,在此文件中搜索特定模式。我在 Whosebug 上使用了一些帮助,结果是这样的:
来自 Whosebug 的无效:
static public List<int> SearchBytePattern(byte[] pattern, byte[] bytes)
{
List<int> positions = new List<int>();
int patternLength = pattern.Length;
int totalLength = bytes.Length;
byte firstMatchByte = pattern[0];
for (int i = 0; i < totalLength; i++)
{
if (firstMatchByte == bytes[i] && totalLength - i >= patternLength)
{
byte[] match = new byte[patternLength];
Array.Copy(bytes, i, match, 0, patternLength);
if (match.SequenceEqual<byte>(pattern))
{
positions.Add(i);
i += patternLength - 1;
}
}
}
return positions;
}
我无法搜索模式:
void CheckCamera()
{
Byte[] szukajkamera = { 0x02, 0x00, 0x08, 0x00, 0x20};
List<int> positions = SearchBytePattern(szukajkamera, bytes);
foreach (var item in positions){
MessageBox.Show(item.ToString("X2"));
IndexCamera = item;
}
int OffsetCameraCheck = IndexCamera + 2;
}
项目现在是我的偏移量,其中 02 00 08 00 20 在文件中。
现在,我该如何检查 bytes(offset=IndexCamera+2) == 0x08 ?
我可以 array.IndexOf,但在我要找的 08 之前还有很多 08。
对于第 5 步,我也在做这件事,但是当 Buffer.BlockCopy 问我长度时,这对我来说是不可能的。
对于第 5 步及以后,我需要在同一个文件中再次搜索另一个模式,获取它的偏移量并从该偏移量复制到最后。如果我想要这样,那么我需要 buffer.blockcopy 到非空字节数组,但我只需要它为空!我完全失去了它。请帮我。
谢谢!
我该怎么做 bytes(offset=IndexCamera+2) == 0x08 ?
if(bytes[IndexCamera+2] == 0x08)....
在进行模式搜索时,上述答案确实有效,但您需要对其进行调整以搜索更多模式。
例如:
如果您正在寻找 08 1D 1A AA 43 88 33
的位置
那么你需要这样的东西:
public static unsafe long IndexOf(this byte[] haystack, byte[] needle, long startOffset = 0)
{
fixed (byte* h = haystack) fixed (byte* n = needle)
{
for (byte* hNext = h + startOffset, hEnd = h + haystack.LongLength + 1 - needle.LongLength, nEnd = n + needle.LongLength; hNext < hEnd; hNext++)
for (byte* hInc = hNext, nInc = n; *nInc == *hInc; hInc++)
if (++nInc == nEnd)
return hNext - h;
return -1;
}
}
注意:感谢编写此代码的 Dylan Nicholson。
这可能很长。我有一个二进制文件,其中包含一些信息。
我想做的事情:
- 从 OpenFileDialog 读取文件(二进制)
- 我正在搜索此文件中的特定字节
- 我正在获取那个字节的偏移量,然后我正在检查偏移量+2 的字节值
- 基本if for(如果offset+2的值是0x08,那么做这个,如果不是,那么做别的)
- 现在,搜索另一个字节模式的偏移量。
- 复制从该偏移量到文件末尾的所有内容
- 将复制的字节数组保存到文件。
所以,这是我的每一步代码。 第一步: 1.
Byte[] bytes;
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
path = ofd.FileName;
bytes = File.ReadAllBytes(path);
第二步,在此文件中搜索特定模式。我在 Whosebug 上使用了一些帮助,结果是这样的:
来自 Whosebug 的无效:
static public List<int> SearchBytePattern(byte[] pattern, byte[] bytes)
{
List<int> positions = new List<int>();
int patternLength = pattern.Length;
int totalLength = bytes.Length;
byte firstMatchByte = pattern[0];
for (int i = 0; i < totalLength; i++)
{
if (firstMatchByte == bytes[i] && totalLength - i >= patternLength)
{
byte[] match = new byte[patternLength];
Array.Copy(bytes, i, match, 0, patternLength);
if (match.SequenceEqual<byte>(pattern))
{
positions.Add(i);
i += patternLength - 1;
}
}
}
return positions;
}
我无法搜索模式:
void CheckCamera()
{
Byte[] szukajkamera = { 0x02, 0x00, 0x08, 0x00, 0x20};
List<int> positions = SearchBytePattern(szukajkamera, bytes);
foreach (var item in positions){
MessageBox.Show(item.ToString("X2"));
IndexCamera = item;
}
int OffsetCameraCheck = IndexCamera + 2;
}
项目现在是我的偏移量,其中 02 00 08 00 20 在文件中。 现在,我该如何检查 bytes(offset=IndexCamera+2) == 0x08 ? 我可以 array.IndexOf,但在我要找的 08 之前还有很多 08。
对于第 5 步,我也在做这件事,但是当 Buffer.BlockCopy 问我长度时,这对我来说是不可能的。 对于第 5 步及以后,我需要在同一个文件中再次搜索另一个模式,获取它的偏移量并从该偏移量复制到最后。如果我想要这样,那么我需要 buffer.blockcopy 到非空字节数组,但我只需要它为空!我完全失去了它。请帮我。 谢谢!
我该怎么做 bytes(offset=IndexCamera+2) == 0x08 ?
if(bytes[IndexCamera+2] == 0x08)....
在进行模式搜索时,上述答案确实有效,但您需要对其进行调整以搜索更多模式。
例如: 如果您正在寻找 08 1D 1A AA 43 88 33
的位置那么你需要这样的东西:
public static unsafe long IndexOf(this byte[] haystack, byte[] needle, long startOffset = 0)
{
fixed (byte* h = haystack) fixed (byte* n = needle)
{
for (byte* hNext = h + startOffset, hEnd = h + haystack.LongLength + 1 - needle.LongLength, nEnd = n + needle.LongLength; hNext < hEnd; hNext++)
for (byte* hInc = hNext, nInc = n; *nInc == *hInc; hInc++)
if (++nInc == nEnd)
return hNext - h;
return -1;
}
}
注意:感谢编写此代码的 Dylan Nicholson。