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