Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / common / safe_browsing / mach_o_image_reader_mac.h
blob894beda533a6a51783826f01f29014da3951767b
1 // Copyright 2015 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.
5 #ifndef CHROME_COMMON_SAFE_BROWSING_MACH_O_IMAGE_READER_MAC_H_
6 #define CHROME_COMMON_SAFE_BROWSING_MACH_O_IMAGE_READER_MAC_H_
8 #include <mach-o/loader.h>
9 #include <stdint.h>
11 #include <vector>
13 #include "base/logging.h"
14 #include "base/macros.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
18 namespace safe_browsing {
20 class ByteSlice;
22 // MachOImageReader is used to extract information about a Mach-O binary image.
23 // This class supports fat and thin images. Initialize() must be called before
24 // any other methods; if it returns false, it is illegal to call any other
25 // methods on this class.
26 class MachOImageReader {
27 public:
28 // Represents a Mach-O load command, including all of its data.
29 struct LoadCommand {
30 LoadCommand();
31 ~LoadCommand();
33 uint32_t cmd() const {
34 return as_command<load_command>()->cmd;
37 uint32_t cmdsize() const {
38 return as_command<load_command>()->cmdsize;
41 template <typename T>
42 const T* as_command() const {
43 if (data.size() < sizeof(T))
44 return nullptr;
45 return reinterpret_cast<const T*>(&data[0]);
48 std::vector<uint8_t> data;
51 // Returns true if |magic| is any Mach-O magic number. This can be used on the
52 // first four bytes of a file (either in little- or big-endian) to quickly
53 // determine whether or not the file is potentially a Mach-O file. An instance
54 // of this class must be used for a true validity check.
55 static bool IsMachOMagicValue(uint32_t magic);
57 MachOImageReader();
58 ~MachOImageReader();
60 // Initializes the instance and verifies that the data is a valid Mach-O
61 // image. This does not take ownership of the bytes, so the data must
62 // remain valid for the lifetime of this object. Returns true if the
63 // instance is initialized and valid, false if the file could not be parsed
64 // as a Mach-O image.
65 bool Initialize(const uint8_t* image, size_t image_size);
67 // Returns whether this is a fat Mach-O image. If this returns true, it is
68 // only valid to call GetFatImages() and none of the other methods.
69 bool IsFat();
71 // It is only valid to call this method if IsFat() returns true. This
72 // returns an image reader for each architecture in the fat file.
73 std::vector<MachOImageReader*> GetFatImages();
75 // Returns whether the image is a 64-bit image.
76 bool Is64Bit();
78 // Retrieves the mach_header structure for the appropriate architecture.
79 const mach_header* GetMachHeader();
80 const mach_header_64* GetMachHeader64();
82 // Returns the Mach-O filetype field from the header.
83 uint32_t GetFileType();
85 // Returns an array of all the load commands in the image.
86 const std::vector<MachOImageReader::LoadCommand>& GetLoadCommands();
88 // If the image has a LC_CODE_SIGNATURE command, this retreives the code
89 // signature blob in the __LINKEDIT segment.
90 bool GetCodeSignatureInfo(std::vector<uint8_t>* info);
92 private:
93 scoped_ptr<ByteSlice> data_;
95 bool is_fat_;
96 ScopedVector<MachOImageReader> fat_images_;
98 bool is_64_bit_;
99 std::vector<LoadCommand> commands_;
101 DISALLOW_COPY_AND_ASSIGN(MachOImageReader);
104 } // namespace safe_browsing
106 #endif // CHROME_COMMON_SAFE_BROWSING_MACH_O_IMAGE_READER_MAC_H_