| 18 | using namespace wolv::literals; |
| 19 | |
| 20 | class AlgorithmSimple : public Algorithm { |
| 21 | public: |
| 22 | AlgorithmSimple() : Algorithm("hex.diffing.algorithm.simple.name", "hex.diffing.algorithm.simple.description") {} |
| 23 | |
| 24 | [[nodiscard]] std::vector<DiffTree> analyze(prv::Provider *providerA, prv::Provider *providerB) const override { |
| 25 | wolv::container::IntervalTree<DifferenceType> differences; |
| 26 | |
| 27 | // Set up readers for both providers |
| 28 | auto readerA = prv::ProviderReader(providerA); |
| 29 | auto readerB = prv::ProviderReader(providerB); |
| 30 | |
| 31 | auto &task = TaskManager::getCurrentTask(); |
| 32 | |
| 33 | // Iterate over both providers and compare the bytes |
| 34 | for (auto itA = readerA.begin(), itB = readerB.begin(); itA < readerA.end() && itB < readerB.end(); ++itA, ++itB) { |
| 35 | // Stop comparing if the diff task was canceled |
| 36 | if (task.wasInterrupted()) |
| 37 | break; |
| 38 | |
| 39 | // If the bytes are different, find the end of the difference |
| 40 | if (*itA != *itB) { |
| 41 | u64 start = itA.getAddress(); |
| 42 | size_t size = 0; |
| 43 | |
| 44 | while (itA != readerA.end() && itB != readerB.end() && *itA != *itB) { |
| 45 | ++itA; |
| 46 | ++itB; |
| 47 | ++size; |
| 48 | } |
| 49 | |
| 50 | // Add the difference to the list |
| 51 | differences.emplace({ start, (start + size) - 1 }, DifferenceType::Mismatch); |
| 52 | } |
| 53 | |
| 54 | // Update the progress bar |
| 55 | task.update(itA.getAddress()); |
| 56 | } |
| 57 | |
| 58 | auto otherDifferences = differences; |
| 59 | |
| 60 | // If one provider is larger than the other, add the extra bytes to the list |
| 61 | if (providerA->getActualSize() != providerB->getActualSize()) { |
| 62 | auto endA = providerA->getActualSize() + 1; |
| 63 | auto endB = providerB->getActualSize() + 1; |
| 64 | |
| 65 | if (endA > endB) { |
| 66 | differences.emplace({ endB, endA }, DifferenceType::Insertion); |
| 67 | otherDifferences.emplace({ endB, endA }, DifferenceType::Deletion); |
| 68 | } |
| 69 | else { |
| 70 | differences.emplace({ endA, endB }, DifferenceType::Insertion); |
| 71 | otherDifferences.emplace({ endB, endA }, DifferenceType::Insertion); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return { differences, otherDifferences }; |
| 76 | } |
| 77 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected