如何检查Array中是否存在(Sub)数组

时间:2017-03-07 20:30:20

标签: c windows

我需要Traceback (most recent call last): File "/home/vagrant/code/my-project/tests/test_loadfirmware.py", line 92, in test_check_size self.assertEquals(fake.check_size(), File "/home/vagrant/code/my-project/test_s3.py", line 80, in check_size bucket_obj = s3_conn.get_bucket(bucket) File "/home/vagrant/code/my-project/venv/local/lib/python2.7/site-packages/boto/s3/connection.py", line 506, in get_bucket return self.head_bucket(bucket_name, headers=headers) File "/home/vagrant/code/my-project/venv/local/lib/python2.7/site-packages/boto/s3/connection.py", line 525, in head_bucket response = self.make_request('HEAD', bucket_name, headers=headers) File "/home/vagrant/code/my-project/venv/local/lib/python2.7/site-packages/boto/s3/connection.py", line 664, in make_request return super(S3Connection, self).make_request( TypeError: must be type, not Mock strstr之间的内容来检查数组是否存在于内存范围内。

我要实现的目标:

memcmp

我有什么想法可以这样吗? (应该使用Windows)

1 个答案:

答案 0 :(得分:4)

函数mem_in_mem应该取两个数组的大小。这是一个简单的实现:

#include <string.h>

void *mem_in_mem(const void *haystack, size_t n1, const void *needle, size_t n2) {
    const unsigned char *p1 = haystack;
    const unsigned char *p2 = needle;

    if (n2 == 0)
        return (void*)p1;
    if (n2 > n1)
        return NULL;

    const unsigned char *p3 = p1 + n1 - n2 + 1;
    for (const unsigned char *p = p1; (p = memchr(p, *p2, p3 - p)) != NULL; p++) {
        if (!memcmp(p, p2, n2))
            return (void*)p;
    }
    return NULL;
}

你会这样调用它:

BYTE a[] = { 0x01, 0x02, 0x03, 0x04 };
BYTE b[] = { 0x02, 0x03 };
if (mem_in_mem(a, sizeof a, b, sizeof b)) {
    printf("b is in memory range of a\n");
}