| 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 <arpa/inet.h> | |
| 38 #include <elf.h> | |
| 39 #include <fcntl.h> | |
| 40 #include <link.h> | |
| 41 #include <string.h> | |
| 42 #include <sys/mman.h> | |
| 43 #include <unistd.h> | |
| 44 | |
| 45 #include <cassert> | |
| 46 #include <cstdio> | |
| 47 | |
| 48 namespace google_breakpad { | |
| 49 | |
| 50 FileID::FileID(const char* path) { | |
| 51 strncpy(path_, path, sizeof(path_)); | |
| 52 } | |
| 53 | |
| 54 bool FileID::ElfFileIdentifier(uint8_t identifier[kMDGUIDSize]) { | |
| 55 const size_t mapped_len = 4096; // Page size (matches WriteMappings()) | |
| 56 int fd = open(path_, O_RDONLY); | |
| 57 if (fd < 0) | |
| 58 return false; | |
| 59 struct stat st; | |
| 60 if (fstat(fd, &st) != 0 || st.st_size <= mapped_len) { | |
| 61 close(fd); | |
| 62 return false; | |
| 63 } | |
| 64 void* base = mmap(NULL, mapped_len, | |
| 65 PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | |
| 66 close(fd); | |
| 67 if (base == MAP_FAILED) | |
| 68 return false; | |
| 69 | |
| 70 memset(identifier, 0, kMDGUIDSize); | |
| 71 uint8_t* ptr = reinterpret_cast<uint8_t*>(base); | |
| 72 uint8_t* ptr_end = ptr + mapped_len; | |
| 73 while (ptr < ptr_end) { | |
| 74 for (unsigned i = 0; i < kMDGUIDSize; i++) | |
| 75 identifier[i] ^= ptr[i]; | |
| 76 ptr += kMDGUIDSize; | |
| 77 } | |
| 78 | |
| 79 munmap(base, mapped_len); | |
| 80 return true; | |
| 81 } | |
| 82 | |
| 83 // static | |
| 84 void FileID::ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize], | |
| 85 char* buffer, int buffer_length) { | |
| 86 uint8_t identifier_swapped[kMDGUIDSize]; | |
| 87 | |
| 88 // Endian-ness swap to match dump processor expectation. | |
| 89 memcpy(identifier_swapped, identifier, kMDGUIDSize); | |
| 90 uint32_t* data1 = reinterpret_cast<uint32_t*>(identifier_swapped); | |
| 91 *data1 = htonl(*data1); | |
| 92 uint16_t* data2 = reinterpret_cast<uint16_t*>(identifier_swapped + 4); | |
| 93 *data2 = htons(*data2); | |
| 94 uint16_t* data3 = reinterpret_cast<uint16_t*>(identifier_swapped + 6); | |
| 95 *data3 = htons(*data3); | |
| 96 | |
| 97 int buffer_idx = 0; | |
| 98 for (int idx = 0; | |
| 99 (buffer_idx < buffer_length) && (idx < kMDGUIDSize); | |
| 100 ++idx) { | |
| 101 int hi = (identifier_swapped[idx] >> 4) & 0x0F; | |
| 102 int lo = (identifier_swapped[idx]) & 0x0F; | |
| 103 | |
| 104 if (idx == 4 || idx == 6 || idx == 8 || idx == 10) | |
| 105 buffer[buffer_idx++] = '-'; | |
| 106 | |
| 107 buffer[buffer_idx++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi; | |
| 108 buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo; | |
| 109 } | |
| 110 | |
| 111 // NULL terminate | |
| 112 buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0; | |
| 113 } | |
| 114 | |
| 115 } // namespace google_breakpad | |
| OLD | NEW |