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
43using MacFileUtilities::MachoID;
44
45namespace google_breakpad {
46
47FileID::FileID(const char *path) {
48  strlcpy(path_, path, sizeof(path_));
49}
50
51bool FileID::FileIdentifier(unsigned char identifier[16]) {
52  int fd = open(path_, O_RDONLY);
53  if (fd == -1)
54    return false;
55
56  MD5Context md5;
57  MD5Init(&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    MD5Update(&md5, buffer, static_cast<unsigned>(buffer_size));
65  }
66
67  close(fd);
68  MD5Final(identifier, &md5);
69
70  return true;
71}
72
73bool FileID::MachoIdentifier(cpu_type_t cpu_type,
74                             cpu_subtype_t cpu_subtype,
75                             unsigned char identifier[16]) {
76  MachoID macho(path_);
77
78  if (macho.UUIDCommand(cpu_type, cpu_subtype, identifier))
79    return true;
80
81  return macho.MD5(cpu_type, cpu_subtype, identifier);
82}
83
84// static
85void FileID::ConvertIdentifierToString(const unsigned char identifier[16],
86                                       char *buffer, int buffer_length) {
87  int buffer_idx = 0;
88  for (int idx = 0; (buffer_idx < buffer_length) && (idx < 16); ++idx) {
89    int hi = (identifier[idx] >> 4) & 0x0F;
90    int lo = (identifier[idx]) & 0x0F;
91
92    if (idx == 4 || idx == 6 || idx == 8 || idx == 10)
93      buffer[buffer_idx++] = '-';
94
95    buffer[buffer_idx++] =
96        static_cast<char>((hi >= 10) ? ('A' + hi - 10) : ('0' + hi));
97    buffer[buffer_idx++] =
98        static_cast<char>((lo >= 10) ? ('A' + lo - 10) : ('0' + lo));
99  }
100
101  // NULL terminate
102  buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0;
103}
104
105}  // namespace google_breakpad
106