xref: /aosp_15_r20/external/google-breakpad/src/common/mac/file_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 // file_id.h: Return a unique identifier for a file
30 //
31 // Author: Dan Waylonis
32 
33 #ifndef COMMON_MAC_FILE_ID_H__
34 #define COMMON_MAC_FILE_ID_H__
35 
36 #include <limits.h>
37 #include <mach/machine.h>
38 #include <stddef.h>
39 
40 namespace google_breakpad {
41 namespace mach_o {
42 
43 class FileID {
44  public:
45   // Constructs a FileID given a path to a file
46   FileID(const char* path);
47 
48   // Constructs a FileID given the contents of a file and its size.
49   FileID(void* memory, size_t size);
~FileID()50   ~FileID() {}
51 
52   // Treat the file as a mach-o file that will contain one or more archicture.
53   // Accepted values for |cpu_type| and |cpu_subtype| (e.g., CPU_TYPE_X86 or
54   // CPU_TYPE_POWERPC) are listed in /usr/include/mach/machine.h.
55   // If |cpu_type| is 0, then the native cpu type is used. If |cpu_subtype| is
56   // CPU_SUBTYPE_MULTIPLE, the match is only done on |cpu_type|.
57   // Returns false if opening the file failed or if the |cpu_type|/|cpu_subtype|
58   // is not present in the file.
59   // Return the unique identifier in |identifier|.
60   // The current implementation will look for the (in order of priority):
61   // LC_UUID, LC_ID_DYLIB, or MD5 hash of the given |cpu_type|.
62   bool MachoIdentifier(cpu_type_t cpu_type,
63                        cpu_subtype_t cpu_subtype,
64                        unsigned char identifier[16]);
65 
66   // Convert the |identifier| data to a NULL terminated string.  The string will
67   // be formatted as a UUID (e.g., 22F065BB-FC9C-49F7-80FE-26A7CEBD7BCE).
68   // The |buffer| should be at least 37 bytes long to receive all of the data
69   // and termination.  Shorter buffers will contain truncated data.
70   static void ConvertIdentifierToString(const unsigned char identifier[16],
71                                         char *buffer, int buffer_length);
72 
73  private:
74   // Storage for the path specified
75   char path_[PATH_MAX];
76 
77   // Storage for contents of a file if this instance is used to operate on in
78   // memory file data rather than directly from a filesystem. If memory_ is
79   // null, the file represented by path_ will be opened/read. If memory_ is
80   // non-null, it is assumed to contain valid data, and no file operations will
81   // occur.
82   void* memory_;
83 
84   // Size of memory_
85   size_t size_;
86 };
87 
88 }  // namespace mach_o
89 }  // namespace google_breakpad
90 
91 #endif  // COMMON_MAC_FILE_ID_H__
92