Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_UTILITY_SAFE_BROWSING_MAC_UDIF_H_ | 5 #ifndef CHROME_UTILITY_SAFE_BROWSING_MAC_UDIF_H_ |
| 6 #define CHROME_UTILITY_SAFE_BROWSING_MAC_UDIF_H_ | 6 #define CHROME_UTILITY_SAFE_BROWSING_MAC_UDIF_H_ |
| 7 | 7 |
| 8 #include <CoreFoundation/CoreFoundation.h> | 8 #include <CoreFoundation/CoreFoundation.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| 11 #include <sys/types.h> | 11 #include <sys/types.h> |
| 12 | 12 |
| 13 #include <memory> | 13 #include <memory> |
| 14 #include <string> | 14 #include <string> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/mac/scoped_cftyperef.h" | 17 #include "base/mac/scoped_cftyperef.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 | 19 |
| 20 namespace safe_browsing { | 20 namespace safe_browsing { |
| 21 namespace dmg { | 21 namespace dmg { |
| 22 | 22 |
| 23 class ReadStream; | 23 class ReadStream; |
| 24 class UDIFBlock; | 24 class UDIFBlock; |
| 25 | 25 |
| 26 #pragma pack(push, 1) | |
|
Robert Sesek
2017/06/09 21:46:19
Can this be left back in the .cc file now?
mortonm
2017/06/09 23:00:44
I kept it in the .h since we want to maintain a pr
mortonm
2017/06/09 23:11:13
I kept it in the .h since we want to maintain a pr
Robert Sesek
2017/06/12 16:38:39
You can also heap-allocate it and store it as a un
mortonm
2017/06/12 18:31:16
Done.
| |
| 27 | |
| 28 // The following structures come from the analysis provided by Jonathan Levin | |
| 29 // at <http://newosxbook.com/DMG.html>. | |
| 30 // | |
| 31 // Note that all fields are stored in big endian. | |
| 32 | |
| 33 struct UDIFChecksum { | |
| 34 uint32_t type; | |
| 35 uint32_t size; | |
| 36 uint32_t data[32]; | |
| 37 }; | |
| 38 | |
| 39 // The trailer structure for a UDIF file. | |
| 40 struct UDIFResourceFile { | |
| 41 static const uint32_t kSignature = 'koly'; | |
| 42 static const uint32_t kVersion = 4; | |
| 43 | |
| 44 uint32_t signature; | |
| 45 uint32_t version; | |
| 46 uint32_t header_size; // Size of this structure. | |
| 47 uint32_t flags; | |
| 48 uint64_t running_data_fork_offset; | |
| 49 uint64_t data_fork_offset; | |
| 50 uint64_t data_fork_length; | |
| 51 uint64_t rsrc_fork_offset; | |
| 52 uint64_t rsrc_fork_length; | |
| 53 uint32_t segment_number; | |
| 54 uint32_t segment_count; | |
| 55 uuid_t segment_id; | |
| 56 | |
| 57 UDIFChecksum data_checksum; | |
| 58 | |
| 59 uint64_t plist_offset; // Offset and length of the blkx plist. | |
| 60 uint64_t plist_length; | |
| 61 | |
| 62 uint8_t reserved1[64]; | |
| 63 | |
| 64 uint64_t code_signature_offset; | |
| 65 uint64_t code_signature_length; | |
| 66 | |
| 67 uint8_t reserved2[40]; | |
| 68 | |
| 69 UDIFChecksum master_checksum; | |
| 70 | |
| 71 uint32_t image_variant; | |
| 72 uint64_t sector_count; | |
| 73 | |
| 74 uint32_t reserved3; | |
| 75 uint32_t reserved4; | |
| 76 uint32_t reserved5; | |
| 77 }; | |
| 78 | |
| 79 #pragma pack(pop) | |
| 80 | |
| 26 // UDIFParser parses a Universal Disk Image Format file, allowing access to the | 81 // UDIFParser parses a Universal Disk Image Format file, allowing access to the |
| 27 // name, types, and data of the partitions held within the file. There is no | 82 // name, types, and data of the partitions held within the file. There is no |
| 28 // canonical documentation for UDIF, and not all disk images use UDIF (though | 83 // canonical documentation for UDIF, and not all disk images use UDIF (though |
| 29 // the majority do). Note that this implementation only handles UDIF and SPARSE | 84 // the majority do). Note that this implementation only handles UDIF and SPARSE |
| 30 // image types, not SPARSEBUNDLE. | 85 // image types, not SPARSEBUNDLE. |
| 31 // | 86 // |
| 32 // No guarantees are made about the position of ReadStream when using an | 87 // No guarantees are made about the position of ReadStream when using an |
| 33 // instance of this class. | 88 // instance of this class. |
| 34 // | 89 // |
| 35 // Note that this implementation relies on the UDIF blkx table to provide | 90 // Note that this implementation relies on the UDIF blkx table to provide |
| 36 // accurate partition information. Otherwise, Apple Partition Map and | 91 // accurate partition information. Otherwise, Apple Partition Map and |
| 37 // GUID would need to be parsed. See: | 92 // GUID would need to be parsed. See: |
| 38 // - http://opensource.apple.com/source/IOStorageFamily/IOStorageFamily-182.1. 1/IOApplePartitionScheme.h | 93 // - http://opensource.apple.com/source/IOStorageFamily/IOStorageFamily-182.1. 1/IOApplePartitionScheme.h |
| 39 // - http://opensource.apple.com/source/IOStorageFamily/IOStorageFamily-182.1. 1/IOGUIDPartitionScheme.h | 94 // - http://opensource.apple.com/source/IOStorageFamily/IOStorageFamily-182.1. 1/IOGUIDPartitionScheme.h |
| 40 // | 95 // |
| 41 // The following references are useful for understanding the UDIF format: | 96 // The following references are useful for understanding the UDIF format: |
| 42 // - http://newosxbook.com/DMG.html | 97 // - http://newosxbook.com/DMG.html |
| 43 // - http://www.macdisk.com/dmgen.php | 98 // - http://www.macdisk.com/dmgen.php |
| 44 class UDIFParser { | 99 class UDIFParser { |
| 45 public: | 100 public: |
| 46 // Constructs an instance from a stream. | 101 // Constructs an instance from a stream. |
| 47 explicit UDIFParser(ReadStream* stream); | 102 explicit UDIFParser(ReadStream* stream); |
| 48 ~UDIFParser(); | 103 ~UDIFParser(); |
| 49 | 104 |
| 105 // Parses the trailer structure. Only actually parses data from the file the | |
| 106 // first time it is called -- after that just returns the previous result when | |
| 107 // parsing was attempted. | |
| 108 bool ParseTrailer(); | |
| 109 | |
| 50 // Parses the UDIF file. This method must be called before any other method. | 110 // Parses the UDIF file. This method must be called before any other method. |
| 51 // If this returns false, it is not legal to call any other methods. | 111 // If this returns false, it is not legal to call any other methods. |
| 52 bool Parse(); | 112 bool Parse(); |
| 53 | 113 |
| 54 // Returns the number of partitions in this UDIF image. | 114 // Returns the number of partitions in this UDIF image. |
| 55 size_t GetNumberOfPartitions(); | 115 size_t GetNumberOfPartitions(); |
| 56 | 116 |
| 57 // Returns the partition name for a given partition number, in the range of | 117 // Returns the partition name for a given partition number, in the range of |
| 58 // [0,GetNumberOfPartitions()). This will include the number, name, and type | 118 // [0,GetNumberOfPartitions()). This will include the number, name, and type |
| 59 // of partition. E.g., "disk image (Apple_HFS : 2)". | 119 // of partition. E.g., "disk image (Apple_HFS : 2)". |
| 60 std::string GetPartitionName(size_t part_number); | 120 std::string GetPartitionName(size_t part_number); |
| 61 | 121 |
| 62 // Returns the partition type as a string for the given partition number. | 122 // Returns the partition type as a string for the given partition number. |
| 63 // E.g., "Apple_HFS" and "Apple_Free". | 123 // E.g., "Apple_HFS" and "Apple_Free". |
| 64 std::string GetPartitionType(size_t part_number); | 124 std::string GetPartitionType(size_t part_number); |
| 65 | 125 |
| 66 // Returns the size of the partition in bytes. | 126 // Returns the size of the partition in bytes. |
| 67 size_t GetPartitionSize(size_t part_number); | 127 size_t GetPartitionSize(size_t part_number); |
| 68 | 128 |
| 69 // Returns a stream of the raw partition data for the given partition | 129 // Returns a stream of the raw partition data for the given partition |
| 70 // number. | 130 // number. |
| 71 std::unique_ptr<ReadStream> GetPartitionReadStream(size_t part_number); | 131 std::unique_ptr<ReadStream> GetPartitionReadStream(size_t part_number); |
| 72 | 132 |
| 73 private: | 133 private: |
| 74 // Parses the blkx plist trailer structure. | 134 // Parses the blkx plist structure. |
| 75 bool ParseBlkx(); | 135 bool ParseBlkx(); |
| 76 | 136 |
| 77 ReadStream* const stream_; // The stream backing the UDIF image. Weak. | 137 ReadStream* const stream_; // The stream backing the UDIF image. Weak. |
| 78 std::vector<std::string> partition_names_; // The names of all partitions. | 138 std::vector<std::string> partition_names_; // The names of all partitions. |
| 79 // All blocks in the UDIF image. | 139 // All blocks in the UDIF image. |
| 80 std::vector<std::unique_ptr<const UDIFBlock>> blocks_; | 140 std::vector<std::unique_ptr<const UDIFBlock>> blocks_; |
| 81 uint16_t block_size_; // The image's block size, in bytes. | 141 uint16_t block_size_; // The image's block size, in bytes. |
| 142 UDIFResourceFile trailer_; | |
| 143 bool trailer_successfully_parsed_; | |
| 82 | 144 |
| 83 DISALLOW_COPY_AND_ASSIGN(UDIFParser); | 145 DISALLOW_COPY_AND_ASSIGN(UDIFParser); |
| 84 }; | 146 }; |
| 85 | 147 |
| 86 } // namespace dmg | 148 } // namespace dmg |
| 87 } // namespace safe_browsing | 149 } // namespace safe_browsing |
| 88 | 150 |
| 89 #endif // CHROME_UTILITY_SAFE_BROWSING_MAC_UDIF_H_ | 151 #endif // CHROME_UTILITY_SAFE_BROWSING_MAC_UDIF_H_ |
| OLD | NEW |