在十六进制中搜索特定字节

发布于 2024-11-05 19:29:33 字数 135 浏览 1 评论 0原文

我有一个包含十六进制字节的 buffer[],我想搜索该缓冲区以查找特定字节。例如:

我的缓冲区有 4096 字节,如果字节 45 34 67 23 (一起)位于此缓冲区内(就像在缓冲)。

你知道我该怎么做吗?编程语言是C。

I have a buffer[] including HEX bytes and I want to search this buffer to find specific bytes. For example:

My buffer has 4096 bytes and I want to search in this if the bytes 45 34 67 23 (together) are inside this buffer (like searching a string in a buffer).

Have you any idea how can I do that? The programming language is C.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千仐 2024-11-12 19:29:33

只是“暴力”它:)

haystacklen = 4096;
needlelen = 4;

foundat = -1;
index = 0; /* start at a different number if searching for 2nd occurrence */
while (index < haystacklen - needlelen + 1) {
    if ((buffer[index + 0] == 45)
     && (buffer[index + 1] == 34)
     && (buffer[index + 2] == 67)
     && (buffer[index + 3] == 23))
    {
        foundat = index;
    }
    index++;
}
/* foundat has the index of the 1st search bytes or -1 */

Just "brute-force" it :)

haystacklen = 4096;
needlelen = 4;

foundat = -1;
index = 0; /* start at a different number if searching for 2nd occurrence */
while (index < haystacklen - needlelen + 1) {
    if ((buffer[index + 0] == 45)
     && (buffer[index + 1] == 34)
     && (buffer[index + 2] == 67)
     && (buffer[index + 3] == 23))
    {
        foundat = index;
    }
    index++;
}
/* foundat has the index of the 1st search bytes or -1 */
撩人痒 2024-11-12 19:29:33

您也可以使用这个更快的版本。但您必须记住,由于 MAKEDWORD 宏,这仅适用于 x86/little endian 处理器。

#define MAKEDWORD(a,b,c,d) ((uint32_t) (((uint32_t)a) & 0xFF) | ((((uint32_t)b) & 0xFF) << 8) | ((((uint32_t)c) & 0xFF) << 16) | ((((uint32_t)d) & 0xFF) << 24))
#define NEEDLE (MAKEDWORD(45,34,67,23))

// get the start and end address of the buffer
uint8_t *ptrEndBuffer = ((uint8_t*)buffer) + (4096 - sizeof(NEEDLE));
uint8_t *ptrStartBuffer = (uint8_t*)buffer - 1; // subtract -1 because we also want to get index 0

// while the result is not 0 we are good
while (ptrEndBuffer - ptrStartBuffer) {
    if ((*(uint32_t*)ptrEndBuffer) == NEEDLE) // get an whole integer instead of just one char
        break; // leave the loop if we found a match

    ptrEndBuffer--;
}

// the index will be -1 if we couldn't find a match else we subtract the start address + the 1 we first removed from the end buffer
int index = ((ptrEndBuffer == ptrStartBuffer) ? (-1) : (ptrEndBuffer - (ptrStartBuffer + 1)));

You could also use this much faster version. But you have to keep in mind that this only works for x86 / little endian processors because of the MAKEDWORD macro.

#define MAKEDWORD(a,b,c,d) ((uint32_t) (((uint32_t)a) & 0xFF) | ((((uint32_t)b) & 0xFF) << 8) | ((((uint32_t)c) & 0xFF) << 16) | ((((uint32_t)d) & 0xFF) << 24))
#define NEEDLE (MAKEDWORD(45,34,67,23))

// get the start and end address of the buffer
uint8_t *ptrEndBuffer = ((uint8_t*)buffer) + (4096 - sizeof(NEEDLE));
uint8_t *ptrStartBuffer = (uint8_t*)buffer - 1; // subtract -1 because we also want to get index 0

// while the result is not 0 we are good
while (ptrEndBuffer - ptrStartBuffer) {
    if ((*(uint32_t*)ptrEndBuffer) == NEEDLE) // get an whole integer instead of just one char
        break; // leave the loop if we found a match

    ptrEndBuffer--;
}

// the index will be -1 if we couldn't find a match else we subtract the start address + the 1 we first removed from the end buffer
int index = ((ptrEndBuffer == ptrStartBuffer) ? (-1) : (ptrEndBuffer - (ptrStartBuffer + 1)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文