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.cc: Return a unique identifier for a file
31 //
32 // See file_id.h for documentation
33 //
34 // Author: Dan Waylonis
35
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include "common/mac/file_id.h"
42 #include "common/mac/macho_id.h"
43
44 using MacFileUtilities::MachoID;
45
46 namespace google_breakpad {
47
FileID(const char * path)48 FileID::FileID(const char *path) {
49 snprintf(path_, sizeof(path_), "%s", path);
50 }
51
FileIdentifier(unsigned char identifier[16])52 bool FileID::FileIdentifier(unsigned char identifier[16]) {
53 int fd = open(path_, O_RDONLY);
54 if (fd == -1)
55 return false;
56
57 MD5Context md5;
58 MD5Init(&md5);
59
60 // Read 4k x 2 bytes at a time. This is faster than just 4k bytes, but
61 // doesn't seem to be an unreasonable size for the stack.
62 unsigned char buffer[4096 * 2];
63 size_t buffer_size = sizeof(buffer);
64 while ((buffer_size = read(fd, buffer, buffer_size) > 0)) {
65 MD5Update(&md5, buffer, static_cast<unsigned>(buffer_size));
66 }
67
68 close(fd);
69 MD5Final(identifier, &md5);
70
71 return true;
72 }
73
MachoIdentifier(cpu_type_t cpu_type,cpu_subtype_t cpu_subtype,unsigned char identifier[16])74 bool FileID::MachoIdentifier(cpu_type_t cpu_type,
75 cpu_subtype_t cpu_subtype,
76 unsigned char identifier[16]) {
77 MachoID macho(path_);
78
79 if (macho.UUIDCommand(cpu_type, cpu_subtype, identifier))
80 return true;
81
82 return macho.MD5(cpu_type, cpu_subtype, identifier);
83 }
84
85 // static
ConvertIdentifierToString(const unsigned char identifier[16],char * buffer,int buffer_length)86 void FileID::ConvertIdentifierToString(const unsigned char identifier[16],
87 char *buffer, int buffer_length) {
88 int buffer_idx = 0;
89 for (int idx = 0; (buffer_idx < buffer_length) && (idx < 16); ++idx) {
90 int hi = (identifier[idx] >> 4) & 0x0F;
91 int lo = (identifier[idx]) & 0x0F;
92
93 if (idx == 4 || idx == 6 || idx == 8 || idx == 10)
94 buffer[buffer_idx++] = '-';
95
96 buffer[buffer_idx++] =
97 static_cast<char>((hi >= 10) ? ('A' + hi - 10) : ('0' + hi));
98 buffer[buffer_idx++] =
99 static_cast<char>((lo >= 10) ? ('A' + lo - 10) : ('0' + lo));
100 }
101
102 // NULL terminate
103 buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0;
104 }
105
106 } // namespace google_breakpad
107