xref: /aosp_15_r20/external/zucchini/element_detection.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_ELEMENT_DETECTION_H_
6*a03ca8b9SKrzysztof Kosiński #define COMPONENTS_ZUCCHINI_ELEMENT_DETECTION_H_
7*a03ca8b9SKrzysztof Kosiński 
8*a03ca8b9SKrzysztof Kosiński #include <stddef.h>
9*a03ca8b9SKrzysztof Kosiński 
10*a03ca8b9SKrzysztof Kosiński #include <memory>
11*a03ca8b9SKrzysztof Kosiński #include <optional>
12*a03ca8b9SKrzysztof Kosiński 
13*a03ca8b9SKrzysztof Kosiński #include "base/callback.h"
14*a03ca8b9SKrzysztof Kosiński #include "components/zucchini/buffer_view.h"
15*a03ca8b9SKrzysztof Kosiński #include "components/zucchini/image_utils.h"
16*a03ca8b9SKrzysztof Kosiński 
17*a03ca8b9SKrzysztof Kosiński namespace zucchini {
18*a03ca8b9SKrzysztof Kosiński 
19*a03ca8b9SKrzysztof Kosiński class Disassembler;
20*a03ca8b9SKrzysztof Kosiński 
21*a03ca8b9SKrzysztof Kosiński // Attempts to detect an executable located at start of |image|. If found,
22*a03ca8b9SKrzysztof Kosiński // returns the corresponding disassembler. Otherwise returns null.
23*a03ca8b9SKrzysztof Kosiński std::unique_ptr<Disassembler> MakeDisassemblerWithoutFallback(
24*a03ca8b9SKrzysztof Kosiński     ConstBufferView image);
25*a03ca8b9SKrzysztof Kosiński 
26*a03ca8b9SKrzysztof Kosiński // Attempts to create a disassembler corresponding to |exe_type| and initialize
27*a03ca8b9SKrzysztof Kosiński // it with |image|, On failure, returns null.
28*a03ca8b9SKrzysztof Kosiński std::unique_ptr<Disassembler> MakeDisassemblerOfType(ConstBufferView image,
29*a03ca8b9SKrzysztof Kosiński                                                      ExecutableType exe_type);
30*a03ca8b9SKrzysztof Kosiński 
31*a03ca8b9SKrzysztof Kosiński // Returns the version associated with disassembler of type |exe_type|.
32*a03ca8b9SKrzysztof Kosiński uint16_t DisassemblerVersionOfType(ExecutableType exe_type);
33*a03ca8b9SKrzysztof Kosiński 
34*a03ca8b9SKrzysztof Kosiński // Attempts to detect an element associated with |image| and returns it, or
35*a03ca8b9SKrzysztof Kosiński // returns nullopt if no element is detected.
36*a03ca8b9SKrzysztof Kosiński using ElementDetector =
37*a03ca8b9SKrzysztof Kosiński     base::RepeatingCallback<std::optional<Element>(ConstBufferView image)>;
38*a03ca8b9SKrzysztof Kosiński 
39*a03ca8b9SKrzysztof Kosiński // Implementation of ElementDetector using disassemblers.
40*a03ca8b9SKrzysztof Kosiński std::optional<Element> DetectElementFromDisassembler(ConstBufferView image);
41*a03ca8b9SKrzysztof Kosiński 
42*a03ca8b9SKrzysztof Kosiński // A class to scan through an image and iteratively detect elements.
43*a03ca8b9SKrzysztof Kosiński class ElementFinder {
44*a03ca8b9SKrzysztof Kosiński  public:
45*a03ca8b9SKrzysztof Kosiński   ElementFinder(ConstBufferView image, ElementDetector&& detector);
46*a03ca8b9SKrzysztof Kosiński   ElementFinder(const ElementFinder&) = delete;
47*a03ca8b9SKrzysztof Kosiński   const ElementFinder& operator=(const ElementFinder&) = delete;
48*a03ca8b9SKrzysztof Kosiński   ~ElementFinder();
49*a03ca8b9SKrzysztof Kosiński 
50*a03ca8b9SKrzysztof Kosiński   // Scans for the next executable using |detector|. Returns the next element
51*a03ca8b9SKrzysztof Kosiński   // found, or nullopt if no more element can be found.
52*a03ca8b9SKrzysztof Kosiński   std::optional<Element> GetNext();
53*a03ca8b9SKrzysztof Kosiński 
54*a03ca8b9SKrzysztof Kosiński  private:
55*a03ca8b9SKrzysztof Kosiński   ConstBufferView image_;
56*a03ca8b9SKrzysztof Kosiński   ElementDetector detector_;
57*a03ca8b9SKrzysztof Kosiński   offset_t pos_ = 0;
58*a03ca8b9SKrzysztof Kosiński };
59*a03ca8b9SKrzysztof Kosiński 
60*a03ca8b9SKrzysztof Kosiński }  // namespace zucchini
61*a03ca8b9SKrzysztof Kosiński 
62*a03ca8b9SKrzysztof Kosiński #endif  // COMPONENTS_ZUCCHINI_ELEMENT_DETECTION_H_
63