1bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// Copyright (c) 2006, Google Inc.
2bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// All rights reserved.
3bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//
4bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// Redistribution and use in source and binary forms, with or without
5bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// modification, are permitted provided that the following conditions are
6bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// met:
7bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//
8bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//     * Redistributions of source code must retain the above copyright
9bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// notice, this list of conditions and the following disclaimer.
10bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//     * Redistributions in binary form must reproduce the above
11bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// copyright notice, this list of conditions and the following disclaimer
12bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// in the documentation and/or other materials provided with the
13bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// distribution.
14bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//     * Neither the name of Google Inc. nor the names of its
15bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// contributors may be used to endorse or promote products derived from
16bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// this software without specific prior written permission.
17bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//
18bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//
30bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly// file_id.h: Return a unique identifier for a file
31bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly//
32bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
33bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly#ifndef COMMON_LINUX_FILE_ID_H__
34bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly#define COMMON_LINUX_FILE_ID_H__
35bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
36bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly#include <limits.h>
37ce2d9075df0d7ce8b7e5de935387cef773b9bc47thestig@chromium.org#include <string>
38bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
39b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid#include "common/linux/guid_creator.h"
40b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
41bcd46f007919b5063164c8c5c6c2bd4dfb62681elulynamespace google_breakpad {
42bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
43b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsidstatic const size_t kMDGUIDSize = sizeof(MDGUID);
44b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid
45bcd46f007919b5063164c8c5c6c2bd4dfb62681elulyclass FileID {
46bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly public:
47b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  explicit FileID(const char* path);
48b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  ~FileID() {}
49bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
50bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly  // Load the identifier for the elf file path specified in the constructor into
51bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly  // |identifier|.  Return false if the identifier could not be created for the
52bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly  // file.
538ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  // The current implementation will look for a .note.gnu.build-id
548ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  // section and use that as the file id, otherwise it falls back to
558ade75f9558e31d25aec862a552fe78f9ba98c82ted.mielczarek  // XORing the first 4096 bytes of the .text section to generate an identifier.
56b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  bool ElfFileIdentifier(uint8_t identifier[kMDGUIDSize]);
57bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
580a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  // Load the identifier for the elf file mapped into memory at |base| into
590a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  // |identifier|.  Return false if the identifier could not be created for the
600a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek  // file.
61f058b4faa7e16838d2120564fdb627076c3137f9benchan@chromium.org  static bool ElfFileIdentifierFromMappedFile(const void* base,
620a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek                                              uint8_t identifier[kMDGUIDSize]);
630a5fc5d663054eb836eafc258cc2f6792358e2c9ted.mielczarek
64bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly  // Convert the |identifier| data to a NULL terminated string.  The string will
65bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly  // be formatted as a UUID (e.g., 22F065BB-FC9C-49F7-80FE-26A7CEBD7BCE).
66bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly  // The |buffer| should be at least 37 bytes long to receive all of the data
67bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly  // and termination.  Shorter buffers will contain truncated data.
68b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid  static void ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize],
69b0baafc4da1f3ffb84e267dd19d176db3de1c14enealsid                                        char* buffer, int buffer_length);
70bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
71bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly private:
72bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly  // Storage for the path specified
73ce2d9075df0d7ce8b7e5de935387cef773b9bc47thestig@chromium.org  std::string path_;
74bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly};
75bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
76bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly}  // namespace google_breakpad
77bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly
78bcd46f007919b5063164c8c5c6c2bd4dfb62681eluly#endif  // COMMON_LINUX_FILE_ID_H__
79