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.cc: Return a unique identifier for a file
30 //
31 // See file_id.h for documentation
32 //
33 // Author: Dan Waylonis
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h> // Must come first
37 #endif
38
39 #include "common/mac/file_id.h"
40
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <string.h>
44
45 #include "common/mac/macho_id.h"
46 #include "common/scoped_ptr.h"
47
48 using MacFileUtilities::MachoID;
49
50 namespace google_breakpad {
51 namespace mach_o {
52 // Constructs a FileID given a path to a file
FileID(const char * path)53 FileID::FileID(const char* path) : memory_(nullptr), size_(0) {
54 snprintf(path_, sizeof(path_), "%s", path);
55 }
56
57 // Constructs a FileID given the contents of a file and its size
FileID(void * memory,size_t size)58 FileID::FileID(void* memory, size_t size)
59 : path_(), memory_(memory), size_(size) {}
60
MachoIdentifier(cpu_type_t cpu_type,cpu_subtype_t cpu_subtype,unsigned char identifier[16])61 bool FileID::MachoIdentifier(cpu_type_t cpu_type,
62 cpu_subtype_t cpu_subtype,
63 unsigned char identifier[16]) {
64 scoped_ptr<MachoID> macho;
65 if (memory_) {
66 macho.reset(new MachoID(memory_, size_));
67 } else {
68 macho.reset(new MachoID(path_));
69 }
70 if (macho->UUIDCommand(cpu_type, cpu_subtype, identifier))
71 return true;
72
73 return macho->MD5(cpu_type, cpu_subtype, identifier);
74 }
75
76 // static
ConvertIdentifierToString(const unsigned char identifier[16],char * buffer,int buffer_length)77 void FileID::ConvertIdentifierToString(const unsigned char identifier[16],
78 char *buffer, int buffer_length) {
79 int buffer_idx = 0;
80 for (int idx = 0; (buffer_idx < buffer_length) && (idx < 16); ++idx) {
81 int hi = (identifier[idx] >> 4) & 0x0F;
82 int lo = (identifier[idx]) & 0x0F;
83
84 if (idx == 4 || idx == 6 || idx == 8 || idx == 10)
85 buffer[buffer_idx++] = '-';
86
87 buffer[buffer_idx++] =
88 static_cast<char>((hi >= 10) ? ('A' + hi - 10) : ('0' + hi));
89 buffer[buffer_idx++] =
90 static_cast<char>((lo >= 10) ? ('A' + lo - 10) : ('0' + lo));
91 }
92
93 // NULL terminate
94 buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0;
95 }
96
97 } // namespace mach_o
98 } // namespace google_breakpad
99