How to search in a BYTE array for a pattern?

后端 未结 4 1178
南旧
南旧 2021-01-04 09:02

I have a Byte array :

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

00FFFFFFFFFFFF0010AC4C4053433442341401030A2

4条回答
  •  抹茶落季
    2021-01-04 09:33

    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;
    }
    

提交回复
热议问题