Replace memmem call with Boyer-Moore-Horspool search

This commit is contained in:
Marcus Holland-Moritz 2023-06-22 11:38:32 +02:00
parent 01a1a74bf8
commit 38cb73cb53

View File

@ -19,8 +19,10 @@
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>. * along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <algorithm>
#include <cstddef> #include <cstddef>
#include <cstring> #include <cstring>
#include <functional>
#include <mutex> #include <mutex>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@ -66,15 +68,16 @@ class filesystem_parser {
break; break;
} }
auto ps = mm.as<void>(start); auto ss = mm.span(start);
auto pc = ::memmem(ps, mm.size() - start, magic.data(), magic.size()); auto it = std::search(
ss.begin(), ss.end(),
std::boyer_moore_horspool_searcher(magic.begin(), magic.end()));
if (!pc) { if (it == ss.end()) {
break; break;
} }
file_off_t pos = start + static_cast<uint8_t const*>(pc) - file_off_t pos = start + std::distance(ss.begin(), it);
static_cast<uint8_t const*>(ps);
if (pos + sizeof(file_header) >= mm.size()) { if (pos + sizeof(file_header) >= mm.size()) {
break; break;
@ -106,7 +109,7 @@ class filesystem_parser {
break; break;
} }
ps = mm.as<void>(pos + sh->length + sizeof(section_header_v2)); auto ps = mm.as<void>(pos + sh->length + sizeof(section_header_v2));
if (::memcmp(ps, magic.data(), magic.size()) == 0 and if (::memcmp(ps, magic.data(), magic.size()) == 0 and
reinterpret_cast<section_header_v2 const*>(ps)->number == 1) { reinterpret_cast<section_header_v2 const*>(ps)->number == 1) {