xref: /aosp_15_r20/external/google-breakpad/src/common/mac/file_id.h (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1*9712c20fSFrederick Mayle // Copyright 2006 Google LLC
2*9712c20fSFrederick Mayle //
3*9712c20fSFrederick Mayle // Redistribution and use in source and binary forms, with or without
4*9712c20fSFrederick Mayle // modification, are permitted provided that the following conditions are
5*9712c20fSFrederick Mayle // met:
6*9712c20fSFrederick Mayle //
7*9712c20fSFrederick Mayle //     * Redistributions of source code must retain the above copyright
8*9712c20fSFrederick Mayle // notice, this list of conditions and the following disclaimer.
9*9712c20fSFrederick Mayle //     * Redistributions in binary form must reproduce the above
10*9712c20fSFrederick Mayle // copyright notice, this list of conditions and the following disclaimer
11*9712c20fSFrederick Mayle // in the documentation and/or other materials provided with the
12*9712c20fSFrederick Mayle // distribution.
13*9712c20fSFrederick Mayle //     * Neither the name of Google LLC nor the names of its
14*9712c20fSFrederick Mayle // contributors may be used to endorse or promote products derived from
15*9712c20fSFrederick Mayle // this software without specific prior written permission.
16*9712c20fSFrederick Mayle //
17*9712c20fSFrederick Mayle // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*9712c20fSFrederick Mayle // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*9712c20fSFrederick Mayle // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*9712c20fSFrederick Mayle // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*9712c20fSFrederick Mayle // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*9712c20fSFrederick Mayle // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*9712c20fSFrederick Mayle // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*9712c20fSFrederick Mayle // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*9712c20fSFrederick Mayle // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*9712c20fSFrederick Mayle // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*9712c20fSFrederick Mayle // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*9712c20fSFrederick Mayle 
29*9712c20fSFrederick Mayle // file_id.h: Return a unique identifier for a file
30*9712c20fSFrederick Mayle //
31*9712c20fSFrederick Mayle // Author: Dan Waylonis
32*9712c20fSFrederick Mayle 
33*9712c20fSFrederick Mayle #ifndef COMMON_MAC_FILE_ID_H__
34*9712c20fSFrederick Mayle #define COMMON_MAC_FILE_ID_H__
35*9712c20fSFrederick Mayle 
36*9712c20fSFrederick Mayle #include <limits.h>
37*9712c20fSFrederick Mayle #include <mach/machine.h>
38*9712c20fSFrederick Mayle #include <stddef.h>
39*9712c20fSFrederick Mayle 
40*9712c20fSFrederick Mayle namespace google_breakpad {
41*9712c20fSFrederick Mayle namespace mach_o {
42*9712c20fSFrederick Mayle 
43*9712c20fSFrederick Mayle class FileID {
44*9712c20fSFrederick Mayle  public:
45*9712c20fSFrederick Mayle   // Constructs a FileID given a path to a file
46*9712c20fSFrederick Mayle   FileID(const char* path);
47*9712c20fSFrederick Mayle 
48*9712c20fSFrederick Mayle   // Constructs a FileID given the contents of a file and its size.
49*9712c20fSFrederick Mayle   FileID(void* memory, size_t size);
~FileID()50*9712c20fSFrederick Mayle   ~FileID() {}
51*9712c20fSFrederick Mayle 
52*9712c20fSFrederick Mayle   // Treat the file as a mach-o file that will contain one or more archicture.
53*9712c20fSFrederick Mayle   // Accepted values for |cpu_type| and |cpu_subtype| (e.g., CPU_TYPE_X86 or
54*9712c20fSFrederick Mayle   // CPU_TYPE_POWERPC) are listed in /usr/include/mach/machine.h.
55*9712c20fSFrederick Mayle   // If |cpu_type| is 0, then the native cpu type is used. If |cpu_subtype| is
56*9712c20fSFrederick Mayle   // CPU_SUBTYPE_MULTIPLE, the match is only done on |cpu_type|.
57*9712c20fSFrederick Mayle   // Returns false if opening the file failed or if the |cpu_type|/|cpu_subtype|
58*9712c20fSFrederick Mayle   // is not present in the file.
59*9712c20fSFrederick Mayle   // Return the unique identifier in |identifier|.
60*9712c20fSFrederick Mayle   // The current implementation will look for the (in order of priority):
61*9712c20fSFrederick Mayle   // LC_UUID, LC_ID_DYLIB, or MD5 hash of the given |cpu_type|.
62*9712c20fSFrederick Mayle   bool MachoIdentifier(cpu_type_t cpu_type,
63*9712c20fSFrederick Mayle                        cpu_subtype_t cpu_subtype,
64*9712c20fSFrederick Mayle                        unsigned char identifier[16]);
65*9712c20fSFrederick Mayle 
66*9712c20fSFrederick Mayle   // Convert the |identifier| data to a NULL terminated string.  The string will
67*9712c20fSFrederick Mayle   // be formatted as a UUID (e.g., 22F065BB-FC9C-49F7-80FE-26A7CEBD7BCE).
68*9712c20fSFrederick Mayle   // The |buffer| should be at least 37 bytes long to receive all of the data
69*9712c20fSFrederick Mayle   // and termination.  Shorter buffers will contain truncated data.
70*9712c20fSFrederick Mayle   static void ConvertIdentifierToString(const unsigned char identifier[16],
71*9712c20fSFrederick Mayle                                         char *buffer, int buffer_length);
72*9712c20fSFrederick Mayle 
73*9712c20fSFrederick Mayle  private:
74*9712c20fSFrederick Mayle   // Storage for the path specified
75*9712c20fSFrederick Mayle   char path_[PATH_MAX];
76*9712c20fSFrederick Mayle 
77*9712c20fSFrederick Mayle   // Storage for contents of a file if this instance is used to operate on in
78*9712c20fSFrederick Mayle   // memory file data rather than directly from a filesystem. If memory_ is
79*9712c20fSFrederick Mayle   // null, the file represented by path_ will be opened/read. If memory_ is
80*9712c20fSFrederick Mayle   // non-null, it is assumed to contain valid data, and no file operations will
81*9712c20fSFrederick Mayle   // occur.
82*9712c20fSFrederick Mayle   void* memory_;
83*9712c20fSFrederick Mayle 
84*9712c20fSFrederick Mayle   // Size of memory_
85*9712c20fSFrederick Mayle   size_t size_;
86*9712c20fSFrederick Mayle };
87*9712c20fSFrederick Mayle 
88*9712c20fSFrederick Mayle }  // namespace mach_o
89*9712c20fSFrederick Mayle }  // namespace google_breakpad
90*9712c20fSFrederick Mayle 
91*9712c20fSFrederick Mayle #endif  // COMMON_MAC_FILE_ID_H__
92