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
;
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
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.
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.
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
);
88 scoped_ptr
<ByteSlice
> data_
;
91 ScopedVector
<MachOImageReader
> fat_images_
;
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_