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