// Copyright 2017 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "components/zucchini/heuristic_ensemble_matcher.h" #include <algorithm> #include <memory> #include <string> #include <utility> #include <vector> #include "base/functional/bind.h" #include "base/logging.h" #include "base/memory/raw_ref.h" #include "base/numerics/safe_conversions.h" #include "base/strings/stringprintf.h" #include "components/zucchini/binary_data_histogram.h" #include "components/zucchini/element_detection.h" #include "components/zucchini/image_utils.h" #include "components/zucchini/io_utils.h" namespace zucchini { namespace { /******** Helper Functions ********/ // Uses |detector| to find embedded executables inside |image|, and returns the // result on success, or std::nullopt on failure, which occurs if too many (> // |kElementLimit|) elements are found. std::optional<std::vector<Element>> FindEmbeddedElements( ConstBufferView image, const std::string& name, ElementDetector&& detector) { … } // Determines whether a proposed comparison between Elements should be rejected // early, to decrease the likelihood of creating false-positive matches, which // may be costly for patching. Our heuristic simply prohibits big difference in // size (relative and absolute) between matched elements. bool UnsafeDifference(const Element& old_element, const Element& new_element) { … } std::ostream& operator<<(std::ostream& stream, const Element& elt) { … } /******** MatchingInfoOut ********/ // A class to output detailed information during ensemble matching. Extracting // the functionality to a separate class decouples formatting and printing logic // from matching logic. The base class consists of stubs. class MatchingInfoOut { … }; /******** MatchingInfoTerse ********/ // A terse MatchingInfoOut that prints only basic information, using LOG(). class MatchingInfoOutTerse : public MatchingInfoOut { … }; /******** MatchingInfoOutVerbose ********/ // A verbose MatchingInfoOut that prints detailed information using |out_|, // including comparison pairs, scores, and a text grid representation of // pairwise matching results. class MatchingInfoOutVerbose : public MatchingInfoOut { … }; } // namespace /******** HeuristicEnsembleMatcher ********/ HeuristicEnsembleMatcher::HeuristicEnsembleMatcher(std::ostream* out) : … { … } HeuristicEnsembleMatcher::~HeuristicEnsembleMatcher() = default; bool HeuristicEnsembleMatcher::RunMatch(ConstBufferView old_image, ConstBufferView new_image) { … } } // namespace zucchini