xref: /aosp_15_r20/external/zucchini/ensemble_matcher.h (revision a03ca8b91e029cd15055c20c78c2e087c84792e4)
1*a03ca8b9SKrzysztof Kosiński // Copyright 2017 The Chromium Authors. All rights reserved.
2*a03ca8b9SKrzysztof Kosiński // Use of this source code is governed by a BSD-style license that can be
3*a03ca8b9SKrzysztof Kosiński // found in the LICENSE file.
4*a03ca8b9SKrzysztof Kosiński 
5*a03ca8b9SKrzysztof Kosiński #ifndef COMPONENTS_ZUCCHINI_ENSEMBLE_MATCHER_H_
6*a03ca8b9SKrzysztof Kosiński #define COMPONENTS_ZUCCHINI_ENSEMBLE_MATCHER_H_
7*a03ca8b9SKrzysztof Kosiński 
8*a03ca8b9SKrzysztof Kosiński #include <stddef.h>
9*a03ca8b9SKrzysztof Kosiński 
10*a03ca8b9SKrzysztof Kosiński #include <vector>
11*a03ca8b9SKrzysztof Kosiński 
12*a03ca8b9SKrzysztof Kosiński #include "components/zucchini/buffer_view.h"
13*a03ca8b9SKrzysztof Kosiński #include "components/zucchini/element_detection.h"
14*a03ca8b9SKrzysztof Kosiński #include "components/zucchini/image_utils.h"
15*a03ca8b9SKrzysztof Kosiński 
16*a03ca8b9SKrzysztof Kosiński namespace zucchini {
17*a03ca8b9SKrzysztof Kosiński 
18*a03ca8b9SKrzysztof Kosiński // A base class for ensemble matching strategies, which identify Elements in a
19*a03ca8b9SKrzysztof Kosiński // "new" and "old" archives, and match each "new" Element to an "old" Element.
20*a03ca8b9SKrzysztof Kosiński // Matched pairs can then be passed to Disassembler for architecture-specific
21*a03ca8b9SKrzysztof Kosiński // patching. Notes:
22*a03ca8b9SKrzysztof Kosiński // - A matched Element pair must have the same ExecutableType.
23*a03ca8b9SKrzysztof Kosiński // - Special case: Exact matches are ignored, since they can be patched directly
24*a03ca8b9SKrzysztof Kosiński //   without architecture-specific patching.
25*a03ca8b9SKrzysztof Kosiński // - Multiple "new" Elements may match a common "old" Element.
26*a03ca8b9SKrzysztof Kosiński // - A "new" Element may have no match. This can happen when no viable match
27*a03ca8b9SKrzysztof Kosiński //   exists, or when an exact match is skipped.
28*a03ca8b9SKrzysztof Kosiński class EnsembleMatcher {
29*a03ca8b9SKrzysztof Kosiński  public:
30*a03ca8b9SKrzysztof Kosiński   EnsembleMatcher();
31*a03ca8b9SKrzysztof Kosiński   EnsembleMatcher(const EnsembleMatcher&) = delete;
32*a03ca8b9SKrzysztof Kosiński   const EnsembleMatcher& operator=(const EnsembleMatcher&) = delete;
33*a03ca8b9SKrzysztof Kosiński   virtual ~EnsembleMatcher();
34*a03ca8b9SKrzysztof Kosiński 
35*a03ca8b9SKrzysztof Kosiński   // Interface to main matching feature. Returns whether match was successful.
36*a03ca8b9SKrzysztof Kosiński   // This should be called at most once per instace.
37*a03ca8b9SKrzysztof Kosiński   virtual bool RunMatch(ConstBufferView old_image,
38*a03ca8b9SKrzysztof Kosiński                         ConstBufferView new_image) = 0;
39*a03ca8b9SKrzysztof Kosiński 
40*a03ca8b9SKrzysztof Kosiński   // Accessors to RunMatch() results.
matches()41*a03ca8b9SKrzysztof Kosiński   const std::vector<ElementMatch>& matches() const { return matches_; }
42*a03ca8b9SKrzysztof Kosiński 
num_identical()43*a03ca8b9SKrzysztof Kosiński   size_t num_identical() const { return num_identical_; }
44*a03ca8b9SKrzysztof Kosiński 
45*a03ca8b9SKrzysztof Kosiński  protected:
46*a03ca8b9SKrzysztof Kosiński   // Post-processes |matches_| to remove potentially unfavorable entries.
47*a03ca8b9SKrzysztof Kosiński   void Trim();
48*a03ca8b9SKrzysztof Kosiński 
49*a03ca8b9SKrzysztof Kosiński   // Storage of matched elements: A list of matched pairs, where the list of
50*a03ca8b9SKrzysztof Kosiński   // "new" elements have increasing offsets and don't overlap. May be empty.
51*a03ca8b9SKrzysztof Kosiński   std::vector<ElementMatch> matches_;
52*a03ca8b9SKrzysztof Kosiński 
53*a03ca8b9SKrzysztof Kosiński   // Number of identical matches found in match candidates. These should be
54*a03ca8b9SKrzysztof Kosiński   // excluded from |matches_|.
55*a03ca8b9SKrzysztof Kosiński   size_t num_identical_ = 0;
56*a03ca8b9SKrzysztof Kosiński };
57*a03ca8b9SKrzysztof Kosiński 
58*a03ca8b9SKrzysztof Kosiński }  // namespace zucchini
59*a03ca8b9SKrzysztof Kosiński 
60*a03ca8b9SKrzysztof Kosiński #endif  // COMPONENTS_ZUCCHINI_ENSEMBLE_MATCHER_H_
61