windows 如何在 BYTE 数组中搜索模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7319391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:00:48  来源:igfitidea点击:

How to search in a BYTE array for a pattern?

c++cwindowspattern-matching

提问by kampi

I have a Byte array :

我有一个字节数组:

BYTE Buffer[20000];this array contains the following data:

BYTE Buffer[20000];该数组包含以下数据:

00FFFFFFFFFFFF0010AC4C4053433442341401030A2F1E78EEEE95A3544C99260F5054A54B00714F8180B3000101010101010101010121399030621A274068B03600DA281100001C000000FF003457314D44304353423443530A000000FC0044454C4C2050323231300A2020000000FD00384B1E5310000A20202020202000FA

00FFFFFFFFFFFF0010AC4C4053433442341401030A2F1E78EEEE95A3544C99260F5054A54B00714F8180B3000101010101010101010121399030621A274068B03600DA281100001C000000FF003457314D44304353423443530A000000FC0044454C4C2050323231300A2020000000FD00384B1E5310000A20202020202000FA

My question is how can i search this array for a pattern like "000000FC"? I don't really think it is important, but i need the index where i can find my pattern too. Could someone provide an example for this, because i don't really understand this :(

我的问题是如何在这个数组中搜索像“ 000000FC”这样的模式?我真的不认为这很重要,但我也需要索引来找到我的模式。有人可以为此提供一个例子,因为我真的不明白这一点:(

回答by Kerrek SB

Since you're in C++, do it the C++ way:

既然你在 C++ 中,就用 C++ 的方式来做:

char a[] = { 0, 0, 0, 0xFC };
char Buffer[20000] = ...

std::string needle(a, a + 4);
std::string haystack(Buffer, Buffer + 20000);  // or "+ sizeof Buffer"

std::size_t n = haystack.find(needle);

if (n == std::string::npos)
{
    // not found
}
else
{
    // position is n
}

You can also use an algorithm to search the array directly:

您还可以使用算法直接搜索数组:

#include <algorithm>
#include <iterator>

auto it = std::search(
    std::begin(Buffer), std::end(Buffer),
    std::begin(a), std::end(a));

if (it == std::end(Buffer))
{
    // not found
}
else
{
    // subrange found at std::distance(std::begin(Buffer), it)
}

Or, in C++17, you can use a string view:

或者,在 C++17 中,您可以使用字符串视图:

std::string_view sv(std::begin(Buffer), std::end(Buffer));

if (std::size_t n = sv.find(needle); n != sv.npos)
{
    // found at position n
}
else
{
    // not found
}

回答by Jon

You want something like memmem(that code is licensed with the GPL).

您想要类似的东西memmem(该代码已获得 GPL 许可)。

However, it should not be difficult to roll your own. Like in memmem's implementation, you need a loop that uses memchrto find the first character of your needle in the haystack, and memcmpto test each hit and see if all of your needle is there.

但是,自己滚动应该不难。就像 inmemmem的实现一样,您需要一个循环,用于memchr在大海捞针中查找针的第一个字符,并memcmp测试每次命中并查看是否所有针都在那里。

回答by Tarion

Try this, just needed it:

试试这个,刚需要它:

// Returns a pointer to the first byte of needle inside haystack, 
static uint8_t* bytes_find(uint8_t* haystack, size_t haystackLen, uint8_t* needle, size_t needleLen) {
    if (needleLen > haystackLen) {
        return false;
    }
    uint8_t* match = memchr(haystack, needle[0], haystackLen);
    if (match != NULL) {
        size_t remaining = haystackLen - ((uint8_t*)match - haystack);
        if (needleLen <= remaining) {
            if (memcmp(match, needle, needleLen) == 0) {
                return match;
            }
        }
    }
    return NULL;
}

回答by BullyWiiPlaza

Here's a simple/naive solution using C buffers:

这是一个使用 C 缓冲区的简单/天真的解决方案:

const char *find_needle(const char *haystack, size_t haystack_length, const char *needle, size_t needle_length) {
    for (size_t haystack_index = 0; haystack_index < haystack_length; haystack_index++) {

        bool needle_found = true;
        for (size_t needle_index = 0; needle_index < needle_length; needle_index++) {
            const auto haystack_character = haystack[haystack_index + needle_index];
            const auto needle_character = needle[needle_index];
            if (haystack_character == needle_character) {
                continue;
            } else {
                needle_found = false;
                break;
            }
        }

        if (needle_found) {
            return &haystack[haystack_index];
        }
    }

    return nullptr;
}

A more efficient solution would be using the Knuth-Morris-Prattalgorithmfor example but the implementation is also more complex.

例如,更有效的解决方案是使用Knuth-Morris-Pratt算法,但实现也更复杂。