diff options
author | Bernd Lörwald <bloerwald@googlemail.com> | 2014-11-08 17:56:55 +0100 |
---|---|---|
committer | Bernd Lörwald <bloerwald@googlemail.com> | 2014-11-08 18:11:00 +0100 |
commit | 222f03d887574f53e3c3861f994eb6f673dbcc22 (patch) | |
tree | 2e2d68492487ae86d08885f794f626b2d97d2ff8 | |
parent | a1aea0546ccf49fa6dd4b78dce9b5fe1ccd41622 (diff) |
Tools/ConnectionPatcher: allow to find pattern multiple times
-rw-r--r-- | src/tools/connection_patcher/Patcher.cpp | 28 |
1 files changed, 16 insertions, 12 deletions
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 <fstream> #include <iostream> #include <iterator> +#include <set> #include <stdexcept> namespace @@ -50,17 +51,15 @@ namespace std::copy(data.begin(), data.end(), std::ostream_iterator<unsigned char>(ofs)); } - size_t SearchOffset (std::vector<unsigned char> const& binary, std::vector<unsigned char> const& pattern) + std::set<size_t> SearchOffset (std::vector<unsigned char> const& binary, std::vector<unsigned char> const& pattern) { - for (size_t i = 0; i < binary.size(); i++) + std::set<size_t> 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) |