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>
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
{
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
{
28 // Represents a Mach-O load command, including all of its data.
33 uint32_t cmd() const {
34 return as_command
<load_command
>()->cmd
;
37 uint32_t cmdsize() const {
38 return as_command
<load_command
>()->cmdsize
;
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
))
49 std::vector
<uint8_t> data
;
52 // Returns true if |magic| is any Mach-O magic number. This can be used on the
53 // first four bytes of a file (either in little- or big-endian) to quickly
54 // determine whether or not the file is potentially a Mach-O file. An instance
55 // of this class must be used for a true validity check.
56 static bool IsMachOMagicValue(uint32_t magic
);
61 // Initializes the instance and verifies that the data is a valid Mach-O
62 // image. This does not take ownership of the bytes, so the data must
63 // remain valid for the lifetime of this object. Returns true if the
64 // instance is initialized and valid, false if the file could not be parsed
66 bool Initialize(const uint8_t* image
, size_t image_size
);
68 // Returns whether this is a fat Mach-O image. If this returns true, it is
69 // only valid to call GetFatImages() and none of the other methods.
72 // It is only valid to call this method if IsFat() returns true. This
73 // returns an image reader for each architecture in the fat file.
74 std::vector
<MachOImageReader
*> GetFatImages();
76 // Returns whether the image is a 64-bit image.
79 // Retrieves the mach_header structure for the appropriate architecture.
80 const mach_header
* GetMachHeader();
81 const mach_header_64
* GetMachHeader64();
83 // Returns the Mach-O filetype field from the header.
84 uint32_t GetFileType();
86 // Returns an array of all the load commands in the image.
87 const std::vector
<MachOImageReader::LoadCommand
>& GetLoadCommands();
89 // If the image has a LC_CODE_SIGNATURE command, this retreives the code
90 // signature blob in the __LINKEDIT segment.
91 bool GetCodeSignatureInfo(std::vector
<uint8_t>* info
);
94 scoped_ptr
<ByteSlice
> data_
;
97 ScopedVector
<MachOImageReader
> fat_images_
;
100 std::vector
<LoadCommand
> commands_
;
102 DISALLOW_COPY_AND_ASSIGN(MachOImageReader
);
105 } // namespace safe_browsing
107 #endif // CHROME_COMMON_SAFE_BROWSING_MACH_O_IMAGE_READER_MAC_H_