1 | // Copyright (c) 2006, Google Inc. |
2 | // All rights reserved. |
3 | // |
4 | // Redistribution and use in source and binary forms, with or without |
5 | // modification, are permitted provided that the following conditions are |
6 | // met: |
7 | // |
8 | // * Redistributions of source code must retain the above copyright |
9 | // notice, this list of conditions and the following disclaimer. |
10 | // * Redistributions in binary form must reproduce the above |
11 | // copyright notice, this list of conditions and the following disclaimer |
12 | // in the documentation and/or other materials provided with the |
13 | // distribution. |
14 | // * Neither the name of Google Inc. nor the names of its |
15 | // contributors may be used to endorse or promote products derived from |
16 | // this software without specific prior written permission. |
17 | // |
18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | |
30 | // macho_id.h: Functions to gather identifying information from a macho file |
31 | // |
32 | // Author: Dan Waylonis |
33 | |
34 | #ifndef COMMON_MAC_MACHO_ID_H__ |
35 | #define COMMON_MAC_MACHO_ID_H__ |
36 | |
37 | #include <limits.h> |
38 | #include <mach/machine.h> |
39 | #include <mach-o/loader.h> |
40 | |
41 | #include "common/mac/macho_walker.h" |
42 | #include "common/md5.h" |
43 | |
44 | namespace MacFileUtilities { |
45 | |
46 | class MachoID { |
47 | public: |
48 | MachoID(const char* path); |
49 | MachoID(void* memory, size_t size); |
50 | ~MachoID(); |
51 | |
52 | // For the given |cpu_type| and |cpu_subtype|, return a UUID from the LC_UUID |
53 | // command. |
54 | // Return false if there isn't a LC_UUID command. |
55 | bool UUIDCommand(cpu_type_t cpu_type, |
56 | cpu_subtype_t cpu_subtype, |
57 | unsigned char identifier[16]); |
58 | |
59 | // For the given |cpu_type|, and |cpu_subtype| return the MD5 for the mach-o |
60 | // data segment(s). |
61 | // Return true on success, false otherwise |
62 | bool MD5(cpu_type_t cpu_type, |
63 | cpu_subtype_t cpu_subtype, |
64 | unsigned char identifier[16]); |
65 | |
66 | private: |
67 | // Signature of class member function to be called with data read from file |
68 | typedef void (MachoID::*UpdateFunction)(unsigned char* bytes, size_t size); |
69 | |
70 | // Update the MD5 value by examining |size| |bytes| and applying the algorithm |
71 | // to each byte. |
72 | void UpdateMD5(unsigned char* bytes, size_t size); |
73 | |
74 | // Bottleneck for update routines |
75 | void Update(MachoWalker* walker, off_t offset, size_t size); |
76 | |
77 | // Factory for the MachoWalker |
78 | bool WalkHeader(cpu_type_t cpu_type, cpu_subtype_t cpu_subtype, |
79 | MachoWalker::LoadCommandCallback callback, void* context); |
80 | |
81 | // The callback from the MachoWalker for CRC and MD5 |
82 | static bool WalkerCB(MachoWalker* walker, load_command* cmd, off_t offset, |
83 | bool swap, void* context); |
84 | |
85 | // The callback from the MachoWalker for LC_UUID |
86 | static bool UUIDWalkerCB(MachoWalker* walker, load_command* cmd, off_t offset, |
87 | bool swap, void* context); |
88 | |
89 | // File path |
90 | char path_[PATH_MAX]; |
91 | |
92 | // Memory region to read from |
93 | void* memory_; |
94 | |
95 | // Size of the memory region |
96 | size_t memory_size_; |
97 | |
98 | // The MD5 context |
99 | google_breakpad::MD5Context md5_context_; |
100 | |
101 | // The current update to call from the Update callback |
102 | UpdateFunction update_function_; |
103 | }; |
104 | |
105 | } // namespace MacFileUtilities |
106 | |
107 | #endif // COMMON_MAC_MACHO_ID_H__ |
108 | |