Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(435)

Side by Side Diff: chrome/utility/safe_browsing/mac/udif.h

Issue 2926473002: Mac Archive Type Sniffing (Closed)
Patch Set: simplified unit test since not parsing actual compressed data in DMGs Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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)
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
Jialiu Lin 2017/06/09 18:48:49 #pragma pack(pop)
mortonm 2017/06/09 19:47:21 Done.
26 // UDIFParser parses a Universal Disk Image Format file, allowing access to the 79 // 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 80 // 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 81 // 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 82 // the majority do). Note that this implementation only handles UDIF and SPARSE
30 // image types, not SPARSEBUNDLE. 83 // image types, not SPARSEBUNDLE.
31 // 84 //
32 // No guarantees are made about the position of ReadStream when using an 85 // No guarantees are made about the position of ReadStream when using an
33 // instance of this class. 86 // instance of this class.
34 // 87 //
35 // Note that this implementation relies on the UDIF blkx table to provide 88 // Note that this implementation relies on the UDIF blkx table to provide
36 // accurate partition information. Otherwise, Apple Partition Map and 89 // accurate partition information. Otherwise, Apple Partition Map and
37 // GUID would need to be parsed. See: 90 // GUID would need to be parsed. See:
38 // - http://opensource.apple.com/source/IOStorageFamily/IOStorageFamily-182.1. 1/IOApplePartitionScheme.h 91 // - 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 92 // - http://opensource.apple.com/source/IOStorageFamily/IOStorageFamily-182.1. 1/IOGUIDPartitionScheme.h
40 // 93 //
41 // The following references are useful for understanding the UDIF format: 94 // The following references are useful for understanding the UDIF format:
42 // - http://newosxbook.com/DMG.html 95 // - http://newosxbook.com/DMG.html
43 // - http://www.macdisk.com/dmgen.php 96 // - http://www.macdisk.com/dmgen.php
44 class UDIFParser { 97 class UDIFParser {
45 public: 98 public:
46 // Constructs an instance from a stream. 99 // Constructs an instance from a stream.
47 explicit UDIFParser(ReadStream* stream); 100 explicit UDIFParser(ReadStream* stream);
48 ~UDIFParser(); 101 ~UDIFParser();
49 102
103 // If the trailer was successfully parsed by the constructor, returns a
104 // pointer to the private member struct containing the contents of the
105 // trailer. Otherwise returns NULL.
Jialiu Lin 2017/06/09 18:48:49 s/NULL/nullptr
mortonm 2017/06/09 19:47:21 Done.
106 UDIFResourceFile* GetTrailer();
107
50 // Parses the UDIF file. This method must be called before any other method. 108 // 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. 109 // If this returns false, it is not legal to call any other methods.
52 bool Parse(); 110 bool Parse();
53 111
54 // Returns the number of partitions in this UDIF image. 112 // Returns the number of partitions in this UDIF image.
55 size_t GetNumberOfPartitions(); 113 size_t GetNumberOfPartitions();
56 114
57 // Returns the partition name for a given partition number, in the range of 115 // Returns the partition name for a given partition number, in the range of
58 // [0,GetNumberOfPartitions()). This will include the number, name, and type 116 // [0,GetNumberOfPartitions()). This will include the number, name, and type
59 // of partition. E.g., "disk image (Apple_HFS : 2)". 117 // of partition. E.g., "disk image (Apple_HFS : 2)".
60 std::string GetPartitionName(size_t part_number); 118 std::string GetPartitionName(size_t part_number);
61 119
62 // Returns the partition type as a string for the given partition number. 120 // Returns the partition type as a string for the given partition number.
63 // E.g., "Apple_HFS" and "Apple_Free". 121 // E.g., "Apple_HFS" and "Apple_Free".
64 std::string GetPartitionType(size_t part_number); 122 std::string GetPartitionType(size_t part_number);
65 123
66 // Returns the size of the partition in bytes. 124 // Returns the size of the partition in bytes.
67 size_t GetPartitionSize(size_t part_number); 125 size_t GetPartitionSize(size_t part_number);
68 126
69 // Returns a stream of the raw partition data for the given partition 127 // Returns a stream of the raw partition data for the given partition
70 // number. 128 // number.
71 std::unique_ptr<ReadStream> GetPartitionReadStream(size_t part_number); 129 std::unique_ptr<ReadStream> GetPartitionReadStream(size_t part_number);
72 130
73 private: 131 private:
74 // Parses the blkx plist trailer structure. 132 // Parses the trailer structure.
133 bool ParseTrailer();
134
135 // Parses the blkx plist structure.
75 bool ParseBlkx(); 136 bool ParseBlkx();
76 137
77 ReadStream* const stream_; // The stream backing the UDIF image. Weak. 138 ReadStream* const stream_; // The stream backing the UDIF image. Weak.
78 std::vector<std::string> partition_names_; // The names of all partitions. 139 std::vector<std::string> partition_names_; // The names of all partitions.
79 // All blocks in the UDIF image. 140 // All blocks in the UDIF image.
80 std::vector<std::unique_ptr<const UDIFBlock>> blocks_; 141 std::vector<std::unique_ptr<const UDIFBlock>> blocks_;
81 uint16_t block_size_; // The image's block size, in bytes. 142 uint16_t block_size_; // The image's block size, in bytes.
143 UDIFResourceFile trailer_;
144 bool trailer_successfully_parsed_;
82 145
83 DISALLOW_COPY_AND_ASSIGN(UDIFParser); 146 DISALLOW_COPY_AND_ASSIGN(UDIFParser);
84 }; 147 };
85 148
86 } // namespace dmg 149 } // namespace dmg
87 } // namespace safe_browsing 150 } // namespace safe_browsing
88 151
89 #endif // CHROME_UTILITY_SAFE_BROWSING_MAC_UDIF_H_ 152 #endif // CHROME_UTILITY_SAFE_BROWSING_MAC_UDIF_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698