File: mach_o_image_reader_mac.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (106 lines) | stat: -rw-r--r-- 3,425 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_COMMON_SAFE_BROWSING_MACH_O_IMAGE_READER_MAC_H_
#define CHROME_COMMON_SAFE_BROWSING_MACH_O_IMAGE_READER_MAC_H_

#include <mach-o/loader.h>
#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <vector>

namespace safe_browsing {

class ByteSlice;

// MachOImageReader is used to extract information about a Mach-O binary image.
// This class supports fat and thin images. Initialize() must be called before
// any other methods; if it returns false, it is illegal to call any other
// methods on this class.
class MachOImageReader {
 public:
  // Represents a Mach-O load command, including all of its data.
  struct LoadCommand {
    LoadCommand();
    LoadCommand(const LoadCommand& other);
    ~LoadCommand();

    uint32_t cmd() const {
      return as_command<load_command>()->cmd;
    }

    uint32_t cmdsize() const {
      return as_command<load_command>()->cmdsize;
    }

    template <typename T>
    const T* as_command() const {
      if (data.size() < sizeof(T))
        return nullptr;
      return reinterpret_cast<const T*>(&data[0]);
    }

    std::vector<uint8_t> data;
  };

  // Returns true if |magic| is any Mach-O magic number. This can be used on the
  // first four bytes of a file (either in little- or big-endian) to quickly
  // determine whether or not the file is potentially a Mach-O file. An instance
  // of this class must be used for a true validity check.
  static bool IsMachOMagicValue(uint32_t magic);

  MachOImageReader();

  MachOImageReader(const MachOImageReader&) = delete;
  MachOImageReader& operator=(const MachOImageReader&) = delete;

  ~MachOImageReader();

  // Initializes the instance and verifies that the data is a valid Mach-O
  // image. This does not take ownership of the bytes, so the data must
  // remain valid for the lifetime of this object. Returns true if the
  // instance is initialized and valid, false if the file could not be parsed
  // as a Mach-O image.
  bool Initialize(const uint8_t* image, size_t image_size);

  // Returns whether this is a fat Mach-O image. If this returns true, it is
  // only valid to call GetFatImages() and none of the other methods.
  bool IsFat();

  // It is only valid to call this method if IsFat() returns true. This
  // returns an image reader for each architecture in the fat file.
  std::vector<MachOImageReader*> GetFatImages();

  // Returns whether the image is a 64-bit image.
  bool Is64Bit();

  // Retrieves the mach_header structure for the appropriate architecture.
  const mach_header* GetMachHeader();
  const mach_header_64* GetMachHeader64();

  // Returns the Mach-O filetype field from the header.
  uint32_t GetFileType();

  // Returns an array of all the load commands in the image.
  const std::vector<MachOImageReader::LoadCommand>& GetLoadCommands();

  // If the image has a LC_CODE_SIGNATURE command, this retreives the code
  // signature blob in the __LINKEDIT segment.
  bool GetCodeSignatureInfo(std::vector<uint8_t>* info);

 private:
  std::unique_ptr<ByteSlice> data_;

  bool is_fat_;
  std::vector<std::unique_ptr<MachOImageReader>> fat_images_;

  bool is_64_bit_;
  std::vector<LoadCommand> commands_;
};

}  // namespace safe_browsing

#endif  // CHROME_COMMON_SAFE_BROWSING_MACH_O_IMAGE_READER_MAC_H_