memsearch() replaced with a Boyer-Moore Horspool implementation due to Quick Search being buggy in some rare cases
This commit is contained in:
parent
7bd0e4f5b4
commit
66e0cfc185
@ -23,27 +23,26 @@ int memcmp(const void *buf1, const void *buf2, u32 size)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Quick Search algorithm, adapted from http://igm.univ-mlv.fr/~lecroq/string/node19.html#SECTION00190
|
//Boyer-Moore Horspool algorithm, adapted from http://www-igm.univ-mlv.fr/~lecroq/string/node18.html#SECTION00180
|
||||||
u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize)
|
u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize)
|
||||||
{
|
{
|
||||||
const u8 *patternc = (const u8 *)pattern;
|
const u8 *patternc = (const u8 *)pattern;
|
||||||
|
|
||||||
//Preprocessing
|
|
||||||
u32 table[256];
|
u32 table[256];
|
||||||
|
|
||||||
|
//Preprocessing
|
||||||
for(u32 i = 0; i < 256; i++)
|
for(u32 i = 0; i < 256; i++)
|
||||||
table[i] = patternSize + 1;
|
table[i] = patternSize;
|
||||||
for(u32 i = 0; i < patternSize; i++)
|
for(u32 i = 0; i < patternSize - 1; i++)
|
||||||
table[patternc[i]] = patternSize - i;
|
table[patternc[i]] = patternSize - i - 1;
|
||||||
|
|
||||||
//Searching
|
//Searching
|
||||||
u32 j = 0;
|
u32 j = 0;
|
||||||
|
|
||||||
while(j <= size - patternSize)
|
while(j <= size - patternSize)
|
||||||
{
|
{
|
||||||
if(memcmp(pattern, startPos + j, patternSize) == 0)
|
u8 c = startPos[j + patternSize - 1];
|
||||||
|
if(patternc[patternSize - 1] == c && memcmp(pattern, startPos + j, patternSize - 1) == 0)
|
||||||
return startPos + j;
|
return startPos + j;
|
||||||
j += table[startPos[j + patternSize]];
|
j += table[c];
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Quick Search algorithm adapted from http://igm.univ-mlv.fr/~lecroq/string/node19.html#SECTION00190
|
* Boyer-Moore Horspool algorithm adapted from http://www-igm.univ-mlv.fr/~lecroq/string/node18.html#SECTION00180
|
||||||
* memcpy, memset32 and memcmp adapted from https://github.com/mid-kid/CakesForeveryWan/blob/557a8e8605ab3ee173af6497486e8f22c261d0e2/source/memfuncs.c
|
* memcpy, memset32 and memcmp adapted from https://github.com/mid-kid/CakesForeveryWan/blob/557a8e8605ab3ee173af6497486e8f22c261d0e2/source/memfuncs.c
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -61,23 +61,22 @@ int memcmp(const void *buf1, const void *buf2, u32 size)
|
|||||||
u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize)
|
u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize)
|
||||||
{
|
{
|
||||||
const u8 *patternc = (const u8 *)pattern;
|
const u8 *patternc = (const u8 *)pattern;
|
||||||
|
|
||||||
//Preprocessing
|
|
||||||
u32 table[256];
|
u32 table[256];
|
||||||
|
|
||||||
|
//Preprocessing
|
||||||
for(u32 i = 0; i < 256; i++)
|
for(u32 i = 0; i < 256; i++)
|
||||||
table[i] = patternSize + 1;
|
table[i] = patternSize;
|
||||||
for(u32 i = 0; i < patternSize; i++)
|
for(u32 i = 0; i < patternSize - 1; i++)
|
||||||
table[patternc[i]] = patternSize - i;
|
table[patternc[i]] = patternSize - i - 1;
|
||||||
|
|
||||||
//Searching
|
//Searching
|
||||||
u32 j = 0;
|
u32 j = 0;
|
||||||
|
|
||||||
while(j <= size - patternSize)
|
while(j <= size - patternSize)
|
||||||
{
|
{
|
||||||
if(memcmp(pattern, startPos + j, patternSize) == 0)
|
u8 c = startPos[j + patternSize - 1];
|
||||||
|
if(patternc[patternSize - 1] == c && memcmp(pattern, startPos + j, patternSize - 1) == 0)
|
||||||
return startPos + j;
|
return startPos + j;
|
||||||
j += table[startPos[j + patternSize]];
|
j += table[c];
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Quick Search algorithm adapted from http://igm.univ-mlv.fr/~lecroq/string/node19.html#SECTION00190
|
* Boyer-Moore Horspool algorithm adapted from http://www-igm.univ-mlv.fr/~lecroq/string/node18.html#SECTION00180
|
||||||
* memcpy, memset32 and memcmp adapted from https://github.com/mid-kid/CakesForeveryWan/blob/557a8e8605ab3ee173af6497486e8f22c261d0e2/source/memfuncs.c
|
* memcpy, memset32 and memcmp adapted from https://github.com/mid-kid/CakesForeveryWan/blob/557a8e8605ab3ee173af6497486e8f22c261d0e2/source/memfuncs.c
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user