xref: /aosp_15_r20/external/zucchini/ensemble_matcher.cc (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 #include "components/zucchini/ensemble_matcher.h"
6*a03ca8b9SKrzysztof Kosiński 
7*a03ca8b9SKrzysztof Kosiński #include <algorithm>
8*a03ca8b9SKrzysztof Kosiński #include <limits>
9*a03ca8b9SKrzysztof Kosiński 
10*a03ca8b9SKrzysztof Kosiński #include "base/containers/cxx20_erase.h"
11*a03ca8b9SKrzysztof Kosiński #include "base/logging.h"
12*a03ca8b9SKrzysztof Kosiński 
13*a03ca8b9SKrzysztof Kosiński namespace zucchini {
14*a03ca8b9SKrzysztof Kosiński 
15*a03ca8b9SKrzysztof Kosiński /******** EnsembleMatcher ********/
16*a03ca8b9SKrzysztof Kosiński 
17*a03ca8b9SKrzysztof Kosiński EnsembleMatcher::EnsembleMatcher() = default;
18*a03ca8b9SKrzysztof Kosiński 
19*a03ca8b9SKrzysztof Kosiński EnsembleMatcher::~EnsembleMatcher() = default;
20*a03ca8b9SKrzysztof Kosiński 
Trim()21*a03ca8b9SKrzysztof Kosiński void EnsembleMatcher::Trim() {
22*a03ca8b9SKrzysztof Kosiński   // Trim rule: If > 1 DEX files are found then ignore all DEX. This is done
23*a03ca8b9SKrzysztof Kosiński   // because we do not yet support MultiDex, under which contents can move
24*a03ca8b9SKrzysztof Kosiński   // across file boundary between "old" and "new" archives. When this occurs,
25*a03ca8b9SKrzysztof Kosiński   // forcing matches of DEX files and patching them separately can result in
26*a03ca8b9SKrzysztof Kosiński   // larger patches than naive patching.
27*a03ca8b9SKrzysztof Kosiński   auto is_match_dex = [](const ElementMatch& match) {
28*a03ca8b9SKrzysztof Kosiński     return match.exe_type() == kExeTypeDex;
29*a03ca8b9SKrzysztof Kosiński   };
30*a03ca8b9SKrzysztof Kosiński   auto num_dex = std::count_if(matches_.begin(), matches_.end(), is_match_dex);
31*a03ca8b9SKrzysztof Kosiński   if (num_dex > 1) {
32*a03ca8b9SKrzysztof Kosiński     LOG(WARNING) << "Found " << num_dex << " DEX: Ignoring all.";
33*a03ca8b9SKrzysztof Kosiński     base::EraseIf(matches_, is_match_dex);
34*a03ca8b9SKrzysztof Kosiński   }
35*a03ca8b9SKrzysztof Kosiński }
36*a03ca8b9SKrzysztof Kosiński 
37*a03ca8b9SKrzysztof Kosiński }  // namespace zucchini
38