| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file implements PEImage, a generic class to manipulate PE files. | 5 // This file implements PEImage, a generic class to manipulate PE files. |
| 6 // This file was adapted from GreenBorder's Code. | 6 // This file was adapted from GreenBorder's Code. |
| 7 | 7 |
| 8 #include "base/win/pe_image.h" | 8 #include "base/win/pe_image.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| (...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 bool PEImage::ImageAddrToOnDiskOffset(LPVOID address, | 561 bool PEImage::ImageAddrToOnDiskOffset(LPVOID address, |
| 562 DWORD *on_disk_offset) const { | 562 DWORD *on_disk_offset) const { |
| 563 if (NULL == address) | 563 if (NULL == address) |
| 564 return false; | 564 return false; |
| 565 | 565 |
| 566 // Get the section that this address belongs to. | 566 // Get the section that this address belongs to. |
| 567 PIMAGE_SECTION_HEADER section_header = GetImageSectionFromAddr(address); | 567 PIMAGE_SECTION_HEADER section_header = GetImageSectionFromAddr(address); |
| 568 if (NULL == section_header) | 568 if (NULL == section_header) |
| 569 return false; | 569 return false; |
| 570 | 570 |
| 571 #pragma warning(push) | 571 #pragma warning(suppress : 4302) // pointer truncation |
| 572 #pragma warning(disable: 4311) | |
| 573 // These casts generate warnings because they are 32 bit specific. | 572 // These casts generate warnings because they are 32 bit specific. |
| 574 // Don't follow the virtual RVAToAddr, use the one on the base. | 573 // Don't follow the virtual RVAToAddr, use the one on the base. |
| 575 DWORD offset_within_section = reinterpret_cast<DWORD>(address) - | 574 DWORD offset_within_section = reinterpret_cast<DWORD>(address) - |
| 576 reinterpret_cast<DWORD>(PEImage::RVAToAddr( | 575 reinterpret_cast<DWORD>(PEImage::RVAToAddr( |
| 577 section_header->VirtualAddress)); | 576 section_header->VirtualAddress)); |
| 578 #pragma warning(pop) | |
| 579 | 577 |
| 580 *on_disk_offset = section_header->PointerToRawData + offset_within_section; | 578 *on_disk_offset = section_header->PointerToRawData + offset_within_section; |
| 581 return true; | 579 return true; |
| 582 } | 580 } |
| 583 | 581 |
| 584 PVOID PEImage::RVAToAddr(DWORD rva) const { | 582 PVOID PEImage::RVAToAddr(DWORD rva) const { |
| 585 if (rva == 0) | 583 if (rva == 0) |
| 586 return NULL; | 584 return NULL; |
| 587 | 585 |
| 588 return reinterpret_cast<char*>(module_) + rva; | 586 return reinterpret_cast<char*>(module_) + rva; |
| 589 } | 587 } |
| 590 | 588 |
| 591 PVOID PEImageAsData::RVAToAddr(DWORD rva) const { | 589 PVOID PEImageAsData::RVAToAddr(DWORD rva) const { |
| 592 if (rva == 0) | 590 if (rva == 0) |
| 593 return NULL; | 591 return NULL; |
| 594 | 592 |
| 595 PVOID in_memory = PEImage::RVAToAddr(rva); | 593 PVOID in_memory = PEImage::RVAToAddr(rva); |
| 596 DWORD disk_offset; | 594 DWORD disk_offset; |
| 597 | 595 |
| 598 if (!ImageAddrToOnDiskOffset(in_memory, &disk_offset)) | 596 if (!ImageAddrToOnDiskOffset(in_memory, &disk_offset)) |
| 599 return NULL; | 597 return NULL; |
| 600 | 598 |
| 601 return PEImage::RVAToAddr(disk_offset); | 599 return PEImage::RVAToAddr(disk_offset); |
| 602 } | 600 } |
| 603 | 601 |
| 604 } // namespace win | 602 } // namespace win |
| 605 } // namespace base | 603 } // namespace base |
| OLD | NEW |