OLD | NEW |
| (Empty) |
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-o/loader.h> | |
39 #include <openssl/md5.h> | |
40 #include <openssl/sha.h> | |
41 | |
42 namespace MacFileUtilities { | |
43 | |
44 class MachoWalker; | |
45 | |
46 class MachoID { | |
47 public: | |
48 MachoID(const char *path); | |
49 ~MachoID(); | |
50 | |
51 // For the given |cpu_type|, return a UUID from the LC_UUID command. | |
52 // Return false if there isn't a LC_UUID command. | |
53 bool UUIDCommand(int cpu_type, unsigned char identifier[16]); | |
54 | |
55 // For the given |cpu_type|, return a UUID from the LC_ID_DYLIB command. | |
56 // Return false if there isn't a LC_ID_DYLIB command. | |
57 bool IDCommand(int cpu_type, unsigned char identifier[16]); | |
58 | |
59 // For the given |cpu_type|, return the Adler32 CRC for the mach-o data | |
60 // segment(s). | |
61 // Return 0 on error (e.g., if the file is not a mach-o file) | |
62 uint32_t Adler32(int cpu_type); | |
63 | |
64 // For the given |cpu_type|, return the MD5 for the mach-o data segment(s). | |
65 // Return true on success, false otherwise | |
66 bool MD5(int cpu_type, unsigned char identifier[16]); | |
67 | |
68 // For the given |cpu_type|, return the SHA1 for the mach-o data segment(s). | |
69 // Return true on success, false otherwise | |
70 bool SHA1(int cpu_type, unsigned char identifier[16]); | |
71 | |
72 private: | |
73 // Signature of class member function to be called with data read from file | |
74 typedef void (MachoID::*UpdateFunction)(unsigned char *bytes, size_t size); | |
75 | |
76 // Update the CRC value by examining |size| |bytes| and applying the algorithm | |
77 // to each byte. | |
78 void UpdateCRC(unsigned char *bytes, size_t size); | |
79 | |
80 // Update the MD5 value by examining |size| |bytes| and applying the algorithm | |
81 // to each byte. | |
82 void UpdateMD5(unsigned char *bytes, size_t size); | |
83 | |
84 // Update the SHA1 value by examining |size| |bytes| and applying the | |
85 // algorithm to each byte. | |
86 void UpdateSHA1(unsigned char *bytes, size_t size); | |
87 | |
88 // Bottleneck for update routines | |
89 void Update(MachoWalker *walker, unsigned long offset, size_t size); | |
90 | |
91 // The callback from the MachoWalker for CRC, MD5, and SHA1 | |
92 static bool WalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, | |
93 bool swap, void *context); | |
94 | |
95 // The callback from the MachoWalker for LC_UUID | |
96 static bool UUIDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, | |
97 bool swap, void *context); | |
98 | |
99 // The callback from the MachoWalker for LC_ID_DYLIB | |
100 static bool IDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, | |
101 bool swap, void *context); | |
102 | |
103 // File path | |
104 char path_[PATH_MAX]; | |
105 | |
106 // File descriptor | |
107 int file_; | |
108 | |
109 // The current crc value | |
110 uint32_t crc_; | |
111 | |
112 // The MD5 context | |
113 MD5_CTX md5_context_; | |
114 | |
115 // The SHA1 context | |
116 SHA_CTX sha1_context_; | |
117 | |
118 // The current update to call from the Update callback | |
119 UpdateFunction update_function_; | |
120 }; | |
121 | |
122 } // namespace MacFileUtilities | |
123 | |
124 #endif // COMMON_MAC_MACHO_ID_H__ | |
OLD | NEW |