OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006, 2009, 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 |
| 35 #include "breakpad/linux/file_id.h" |
| 36 |
| 37 #include <elf.h> |
| 38 #include <fcntl.h> |
| 39 #include <link.h> |
| 40 #include <string.h> |
| 41 #include <sys/mman.h> |
| 42 #include <unistd.h> |
| 43 |
| 44 #include <cassert> |
| 45 #include <cstdio> |
| 46 |
| 47 namespace google_breakpad { |
| 48 |
| 49 FileID::FileID(const char* path) { |
| 50 strncpy(path_, path, sizeof(path_)); |
| 51 } |
| 52 |
| 53 bool FileID::ElfFileIdentifier(uint8_t identifier[kMDGUIDSize]) { |
| 54 const size_t mapped_len = 4096; // Page size (matches WriteMappings()) |
| 55 int fd = open(path_, O_RDONLY); |
| 56 if (fd < 0) |
| 57 return false; |
| 58 struct stat st; |
| 59 if (fstat(fd, &st) != 0 || st.st_size <= mapped_len) { |
| 60 close(fd); |
| 61 return false; |
| 62 } |
| 63 void* base = mmap(NULL, mapped_len, |
| 64 PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 65 close(fd); |
| 66 if (base == MAP_FAILED) |
| 67 return false; |
| 68 |
| 69 memset(identifier, 0, kMDGUIDSize); |
| 70 uint8_t* ptr = reinterpret_cast<uint8_t*>(base); |
| 71 uint8_t* ptr_end = ptr + mapped_len; |
| 72 while (ptr < ptr_end) { |
| 73 for (unsigned i = 0; i < kMDGUIDSize; i++) |
| 74 identifier[i] ^= ptr[i]; |
| 75 ptr += kMDGUIDSize; |
| 76 } |
| 77 |
| 78 munmap(base, mapped_len); |
| 79 return true; |
| 80 } |
| 81 |
| 82 // static |
| 83 void FileID::ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize], |
| 84 char* buffer, int buffer_length) { |
| 85 int buffer_idx = 0; |
| 86 for (int idx = 0; |
| 87 (buffer_idx < buffer_length) && (idx < kMDGUIDSize); |
| 88 ++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++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi; |
| 96 buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo; |
| 97 } |
| 98 |
| 99 // NULL terminate |
| 100 buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0; |
| 101 } |
| 102 |
| 103 } // namespace google_breakpad |
OLD | NEW |