// Copyright 2015 The Chromium Authors. All rights reserved. // 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 #include #include #include "base/logging.h" #include "base/macros.h" #include "base/memory/scoped_ptr.h" #include "base/memory/scoped_vector.h" 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(); uint32_t cmd() const { return as_command()->cmd; } uint32_t cmdsize() const { return as_command()->cmdsize; } template const T* as_command() const { const T* command = reinterpret_cast(&data[0]); if (data.size() < sizeof(T) || command->cmdsize < sizeof(T)) return nullptr; return command; } std::vector data; }; MachOImageReader(); ~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 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& GetLoadCommands(); // If the image has a LC_CODE_SIGNATURE command, this retreives the code // signature blob in the __LINKEDIT segment. bool GetCodeSignatureInfo(std::vector* info); private: scoped_ptr data_; bool is_fat_; ScopedVector fat_images_; bool is_64_bit_; std::vector commands_; DISALLOW_COPY_AND_ASSIGN(MachOImageReader); }; } // namespace safe_browsing #endif // CHROME_COMMON_SAFE_BROWSING_MACH_O_IMAGE_READER_MAC_H_