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