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