From b0baafc4da1f3ffb84e267dd19d176db3de1c14e Mon Sep 17 00:00:00 2001 From: nealsid Date: Mon, 17 Aug 2009 23:12:53 +0000 Subject: Merge of Breakpad Chrome Linux fork A=agl, Lei Zhang R=nealsid, agl git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@384 4c0a9323-5329-0410-9bdc-e9ce6186880e --- src/common/linux/file_id.cc | 112 ++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 71 deletions(-) (limited to 'src/common/linux/file_id.cc') diff --git a/src/common/linux/file_id.cc b/src/common/linux/file_id.cc index 1adf2a13..34c9e508 100644 --- a/src/common/linux/file_id.cc +++ b/src/common/linux/file_id.cc @@ -32,104 +32,74 @@ // See file_id.h for documentation // -#include -#include +#include "common/linux/file_id.h" + +#include #include #include #include -#include #include +#include #include -#include "common/linux/file_id.h" -#include "common/md5.h" +#include +#include namespace google_breakpad { -static bool FindElfTextSection(const void *elf_mapped_base, - const void **text_start, - int *text_size) { - assert(elf_mapped_base); - assert(text_start); - assert(text_size); - - const unsigned char *elf_base = - static_cast(elf_mapped_base); - const ElfW(Ehdr) *elf_header = - reinterpret_cast(elf_base); - if (memcmp(elf_header, ELFMAG, SELFMAG) != 0) - return false; - *text_start = NULL; - *text_size = 0; - const ElfW(Shdr) *sections = - reinterpret_cast(elf_base + elf_header->e_shoff); - const char *text_section_name = ".text"; - int name_len = strlen(text_section_name); - const ElfW(Shdr) *string_section = sections + elf_header->e_shstrndx; - const ElfW(Shdr) *text_section = NULL; - for (int i = 0; i < elf_header->e_shnum; ++i) { - if (sections[i].sh_type == SHT_PROGBITS) { - const char *section_name = (char*)(elf_base + - string_section->sh_offset + - sections[i].sh_name); - if (!strncmp(section_name, text_section_name, name_len)) { - text_section = §ions[i]; - break; - } - } - } - if (text_section != NULL && text_section->sh_size > 0) { - int text_section_size = text_section->sh_size; - *text_start = elf_base + text_section->sh_offset; - *text_size = text_section_size; - } - return true; -} - -FileID::FileID(const char *path) { +FileID::FileID(const char* path) { strncpy(path_, path, sizeof(path_)); } -bool FileID::ElfFileIdentifier(unsigned char identifier[16]) { +bool FileID::ElfFileIdentifier(uint8_t identifier[kMDGUIDSize]) { + const ssize_t mapped_len = 4096; // Page size (matches WriteMappings()) int fd = open(path_, O_RDONLY); if (fd < 0) return false; struct stat st; - if (fstat(fd, &st) != 0 && st.st_size <= 0) { + if (fstat(fd, &st) != 0 || st.st_size <= mapped_len) { close(fd); return false; } - void *base = mmap(NULL, st.st_size, + void* base = mmap(NULL, mapped_len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); - if (base == MAP_FAILED) { - close(fd); + close(fd); + if (base == MAP_FAILED) return false; - } - bool success = false; - const void *text_section = NULL; - int text_size = 0; - if (FindElfTextSection(base, &text_section, &text_size) && (text_size > 0)) { - struct MD5Context md5; - MD5Init(&md5); - MD5Update(&md5, - static_cast(text_section), - text_size); - MD5Final(identifier, &md5); - success = true; + + memset(identifier, 0, kMDGUIDSize); + uint8_t* ptr = reinterpret_cast(base); + uint8_t* ptr_end = ptr + mapped_len; + while (ptr < ptr_end) { + for (unsigned i = 0; i < kMDGUIDSize; i++) + identifier[i] ^= ptr[i]; + ptr += kMDGUIDSize; } - close(fd); - munmap(base, st.st_size); - return success; + munmap(base, mapped_len); + return true; } // static -void FileID::ConvertIdentifierToString(const unsigned char identifier[16], - char *buffer, int buffer_length) { +void FileID::ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize], + char* buffer, int buffer_length) { + uint8_t identifier_swapped[kMDGUIDSize]; + + // Endian-ness swap to match dump processor expectation. + memcpy(identifier_swapped, identifier, kMDGUIDSize); + uint32_t* data1 = reinterpret_cast(identifier_swapped); + *data1 = htonl(*data1); + uint16_t* data2 = reinterpret_cast(identifier_swapped + 4); + *data2 = htons(*data2); + uint16_t* data3 = reinterpret_cast(identifier_swapped + 6); + *data3 = htons(*data3); + int buffer_idx = 0; - for (int idx = 0; (buffer_idx < buffer_length) && (idx < 16); ++idx) { - int hi = (identifier[idx] >> 4) & 0x0F; - int lo = (identifier[idx]) & 0x0F; + for (unsigned int idx = 0; + (buffer_idx < buffer_length) && (idx < kMDGUIDSize); + ++idx) { + int hi = (identifier_swapped[idx] >> 4) & 0x0F; + int lo = (identifier_swapped[idx]) & 0x0F; if (idx == 4 || idx == 6 || idx == 8 || idx == 10) buffer[buffer_idx++] = '-'; -- cgit v1.2.1