查找C中重复出现的字节
我试图修改一个函数,使它能够遍历内存块并找到重复的字节段。我编写的当前函数可以找到单个函数,但我认为我的指针和逻辑有问题 这里是我的原始函数,用于查找单个字节。它返回找到它的次数以及偏移量存储在pOffsets中的次数:查找C中重复出现的字节,c,C,我试图修改一个函数,使它能够遍历内存块并找到重复的字节段。我编写的当前函数可以找到单个函数,但我认为我的指针和逻辑有问题 这里是我的原始函数,用于查找单个字节。它返回找到它的次数以及偏移量存储在pOffsets中的次数: //pOffsets points to an array large enough to hold the results //blockAddress points to the first byte of the memory region //blockLength i
//pOffsets points to an array large enough to hold the results
//blockAddress points to the first byte of the memory region
//blockLength is the number of bytes in the memory region
//Byte is the value to be searched for
//maxBytes is the maximum number of bytes that are to be copied
uint32_t findOccurrencesOfByte(uint32_t* const pOffsets,
const uint8_t* const blockAddress, uint32_t blockLength,
uint8_t Byte, uint32_t maxBytes) {
uint32_t count, pos;
count = 0;
for (pos = 0; count < maxBytes && pos < blockLength; pos++) {
if (*(blockAddress + pos) == Byte) {
*(pOffsets + count) = pos;
count++;
}
}
return count;
}
//pOffsets指向一个足以容纳结果的数组
//blockAddress指向内存区域的第一个字节
//blockLength是内存区域中的字节数
//Byte是要搜索的值
//maxBytes是要复制的最大字节数
uint32\u t findOccurrencesOfByte(uint32\u t*常量偏移,
const uint8_t*const blockAddress,uint32_t blockLength,
uint8字节,uint32最大字节){
uint32_t计数,位置;
计数=0;
对于(pos=0;计数
这是我试图更改以查找字节的部分的代码:
//pOffsets points to an array of dimension at least 256
//blockAddress points to the first byte of the memory region
//blockLength is number of bytes in the memory region
//pPattern points to a copy of the pattern that is to be found
//patternLength is the number of bytes in the target pattern
uint32_t findOccurrencesOfPattern(uint32_t* const pOffsets,
const uint8_t* const blockAddress, uint32_t blockLength,
const uint8_t* const pPattern, uint8_t patternLength) {
uint32_t count, pos, i;
count = 0;
for (pos = 0; pos < blockLength; pos++) {
for (i = 0; patternLength != 0; patternLength--) {
if (*(blockAddress + pos + i) == *pPattern) {
*(pOffsets + count) = pos;
count++;
}
}
}
return count;
}
//pOffsets指向维度至少为256的数组
//blockAddress指向内存区域的第一个字节
//blockLength是内存区域中的字节数
//pPattern指向要找到的模式的副本
//patternLength是目标模式中的字节数
uint32\u t findOccurrencesOfPattern(uint32\u t*常量偏移,
const uint8_t*const blockAddress,uint32_t blockLength,
常数uint8_t*常数pPattern,uint8_t pattern长度){
uint32计数,位置,i;
计数=0;
用于(pos=0;pos
谁能帮我找出我做错了什么?感谢基本上,您要做的是从内存块中的每个点开始匹配模式。减少
patternLength
没有意义,因为您需要多次使用该长度
uint32\u t findOccurrencesOfPattern(uint32\u t*const poffset,
const uint8_t*const blockAddress,uint32_t blockLength,
常数uint8_t*常数pPattern,uint8_t pattern长度){
uint32计数,位置,i;
计数=0;
对于(pos=0;pos
此外,您不希望增加内部for的计数,因为您将对每个匹配的字节(即使是部分匹配的字节)进行计数
您应该在blockLength-patternLength
处停止迭代,因为在此之后您肯定找不到任何其他匹配项
方法应该是:假设您找到了匹配项(
ok=1
initially)。然后检查是否是这样。当你发现矛盾时,说它不正常并打破循环(你已经确定它不匹配)。基本上,你要做的是从内存块中的每个点开始匹配模式。减少patternLength
没有意义,因为您需要多次使用该长度
uint32\u t findOccurrencesOfPattern(uint32\u t*const poffset,
const uint8_t*const blockAddress,uint32_t blockLength,
常数uint8_t*常数pPattern,uint8_t pattern长度){
uint32计数,位置,i;
计数=0;
对于(pos=0;pos
此外,您不希望增加内部for的计数,因为您将对每个匹配的字节(即使是部分匹配的字节)进行计数
您应该在blockLength-patternLength
处停止迭代,因为在此之后您肯定找不到任何其他匹配项
方法应该是:假设您找到了匹配项(
ok=1
initially)。然后检查是否是这样。当您发现矛盾时,说它不正常并中断循环(您已经确定它不匹配)。如果您正在存储指向匹配起始位置的指针数组。输入类型应为uint8\u t**。uint8_t**是指向uint8_t*的指针,uint8_t*是匹配位置的地址
在同一输入参数中不需要有两次const
我也同意上面的评论,带[]的索引使代码更具可读性
以下内容应该适合您
#define MAX_MATCHES 2
const uint8_t data[] = { 1,2,3,4,5,6,1,2,3,4,5,6,8,7,6,4,5,6 };
const uint8_t pattern[] = { 4,5,6 };
const uint8_t * startAddress[MAX_MATCHES];
uint32_t findOccurrencesOfPattern(const uint8_t** matchAddress,
const uint8_t* blockAddress, uint32_t blockLength,
const uint8_t* pPattern, uint8_t patternLength)
{
int count = 0;
for (int i = 0; i <= blockLength - patternLength; i++)
{
bool match = true;
for (int j = 0; j < patternLength; j++)
{
if (blockAddress[i + j] != pPattern[j])
{
match = false;
break;
}
}
if (match)
{
match = false;
matchAddress[count] = &blockAddress[i];
count++;
//Don't look for overlapping patterns
i += patternLength;
//Don't overrun matchAdress
if (count >= MAX_MATCHES)
{
return count;
}
}
}
return count;
}
如果存储指向匹配起始位置的指针数组。输入类型应为uint8\u t**。uint8_t**是指向uint8_t*的指针,uint8_t*是匹配位置的地址 在同一输入参数中不需要有两次
const
我也同意上面的评论,带[]的索引使代码更具可读性
以下内容应该适合您
#define MAX_MATCHES 2
const uint8_t data[] = { 1,2,3,4,5,6,1,2,3,4,5,6,8,7,6,4,5,6 };
const uint8_t pattern[] = { 4,5,6 };
const uint8_t * startAddress[MAX_MATCHES];
uint32_t findOccurrencesOfPattern(const uint8_t** matchAddress,
const uint8_t* blockAddress, uint32_t blockLength,
const uint8_t* pPattern, uint8_t patternLength)
{
int count = 0;
for (int i = 0; i <= blockLength - patternLength; i++)
{
bool match = true;
for (int j = 0; j < patternLength; j++)
{
if (blockAddress[i + j] != pPattern[j])
{
match = false;
break;
}
}
if (match)
{
match = false;
matchAddress[count] = &blockAddress[i];
count++;
//Don't look for overlapping patterns
i += patternLength;
//Don't overrun matchAdress
if (count >= MAX_MATCHES)
{
return count;
}
}
}
return count;
}
你真的需要写那个函数吗?标准C库为此提供了大部分构建块。是的,不幸的是…
for(i=0;patternLength!=0;patternLength--){
-减少