From 222f03d887574f53e3c3861f994eb6f673dbcc22 Mon Sep 17 00:00:00 2001 From: Bernd Lörwald Date: Sat, 8 Nov 2014 17:56:55 +0100 Subject: Tools/ConnectionPatcher: allow to find pattern multiple times --- src/tools/connection_patcher/Patcher.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/tools/connection_patcher/Patcher.cpp b/src/tools/connection_patcher/Patcher.cpp index 65cf1704472..92d9dacedf3 100644 --- a/src/tools/connection_patcher/Patcher.cpp +++ b/src/tools/connection_patcher/Patcher.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include namespace @@ -50,17 +51,15 @@ namespace std::copy(data.begin(), data.end(), std::ostream_iterator(ofs)); } - size_t SearchOffset (std::vector const& binary, std::vector const& pattern) + std::set SearchOffset (std::vector const& binary, std::vector const& pattern) { - for (size_t i = 0; i < binary.size(); i++) + std::set offsets; + for (size_t i = 0; (i + pattern.size()) < binary.size(); i++) { size_t matches = 0; for (size_t j = 0; j < pattern.size(); j++) { - if (pattern.size() > (binary.size() - i)) - throw std::runtime_error("unable to find pattern"); - if (pattern[j] == 0) { matches++; @@ -74,10 +73,13 @@ namespace } if (matches == pattern.size()) - return i; + { + offsets.insert(i); + i += matches; + } } - throw std::runtime_error("unable to find pattern"); + return offsets.empty() ? throw std::runtime_error("unable to find pattern") : offsets; } } @@ -96,12 +98,14 @@ namespace Connection_Patcher if (pattern.empty()) return; - size_t const offset(SearchOffset(binary, pattern)); - std::cout << "Found offset " << offset << std::endl; + for (size_t const offset : SearchOffset(binary, pattern)) + { + std::cout << "Found offset " << offset << std::endl; - if (offset != 0 && binary.size() >= bytes.size()) - for (size_t i = 0; i < bytes.size(); i++) - binary[offset + i] = bytes[i]; + if (offset != 0 && binary.size() >= bytes.size()) + for (size_t i = 0; i < bytes.size(); i++) + binary[offset + i] = bytes[i]; + } } void Patcher::Finish(boost::filesystem::path out) -- cgit v1.2.3