Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / common / safe_browsing / mach_o_image_reader_mac.h
blob36d1e257349a9d6680bb2b8f7ba7662922ceaa8a
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 const T* command = reinterpret_cast<const T*>(&data[0]);
44 if (data.size() < sizeof(T) || command->cmdsize < sizeof(T))
45 return nullptr;
46 return command;
49 std::vector<uint8_t> data;
52 MachOImageReader();
53 ~MachOImageReader();
55 // Initializes the instance and verifies that the data is a valid Mach-O
56 // image. This does not take ownership of the bytes, so the data must
57 // remain valid for the lifetime of this object. Returns true if the
58 // instance is initialized and valid, false if the file could not be parsed
59 // as a Mach-O image.
60 bool Initialize(const uint8_t* image, size_t image_size);
62 // Returns whether this is a fat Mach-O image. If this returns true, it is
63 // only valid to call GetFatImages() and none of the other methods.
64 bool IsFat();
66 // It is only valid to call this method if IsFat() returns true. This
67 // returns an image reader for each architecture in the fat file.
68 std::vector<MachOImageReader*> GetFatImages();
70 // Returns whether the image is a 64-bit image.
71 bool Is64Bit();
73 // Retrieves the mach_header structure for the appropriate architecture.
74 const mach_header* GetMachHeader();
75 const mach_header_64* GetMachHeader64();
77 // Returns the Mach-O filetype field from the header.
78 uint32_t GetFileType();
80 // Returns an array of all the load commands in the image.
81 const std::vector<MachOImageReader::LoadCommand>& GetLoadCommands();
83 // If the image has a LC_CODE_SIGNATURE command, this retreives the code
84 // signature blob in the __LINKEDIT segment.
85 bool GetCodeSignatureInfo(std::vector<uint8_t>* info);
87 private:
88 scoped_ptr<ByteSlice> data_;
90 bool is_fat_;
91 ScopedVector<MachOImageReader> fat_images_;
93 bool is_64_bit_;
94 std::vector<LoadCommand> commands_;
96 DISALLOW_COPY_AND_ASSIGN(MachOImageReader);
99 } // namespace safe_browsing
101 #endif // CHROME_COMMON_SAFE_BROWSING_MACH_O_IMAGE_READER_MAC_H_