Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/coff_parser.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ CoffParser::CoffParser(CoffReaderWriter* coff_reader)
*/
bool CoffParser::Parse() {
if (!this->coffStream_->Open()) {
std::cerr << "Unable to open coff file for reading: "
<< reportLastError() << "\n";
std::cerr << "Unable to open coff file: "
<< this->coffStream_->get_file()
<< " for reading: " << reportLastError() << "\n";
return false;
}
int const invalid_valid_sig =
Expand Down Expand Up @@ -578,6 +579,40 @@ void CoffParser::ReportLongName(const char* data) {
std::cout << "DLL: " << data << "\n";
}

std::string CoffParser::GetLongName() const {
// TODO(johnwparent): I think we can access the
// 2nd index of the members vec to get the long
// name
for (auto mem : this->coff_.members) {
if (mem.member->is_longname) {
return std::string(mem.member->data);
}
}
return std::string();
}

std::string CoffParser::GetShortName() const {
for (auto mem : this->coff_.members) {
if (mem.header->Name[0] != '/') {
int i = 0;
while (mem.header->Name[i] != '/') {
++i;
}
return std::string(reinterpret_cast<const char*>(mem.header->Name),
i);
}
}
return std::string();
}

std::string CoffParser::GetName() const {
std::string maybe_name = this->GetLongName();
if (maybe_name.empty()) {
maybe_name = this->GetShortName();
}
return maybe_name;
}

void CoffParser::Report() {
for (auto mem : this->coff_.members) {
if (mem.member->is_longname) {
Expand Down
3 changes: 3 additions & 0 deletions src/coff_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class CoffParser {
bool NormalizeName(std::string& name);
void Report();
int Verify();
std::string GetLongName() const;
std::string GetShortName() const;
std::string GetName() const;
static int Validate(std::string& coff);
};

Expand Down
22 changes: 19 additions & 3 deletions src/coff_reader_writer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,39 @@

#include "coff_reader_writer.h"
#include "coff.h"
#include "utils.h"

#include <winnt.h>
#include <cstdlib>
#include <cstring>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <stdexcept>
#include <string>
#include <system_error>
#include <utility>

CoffReaderWriter::CoffReaderWriter(std::string const& file)
: file_(std::move(file)) {}

bool CoffReaderWriter::Open() {
this->pe_stream_.open(this->file_,
std::ios::in | std::ios::out | std::ios::binary);
return this->pe_stream_.is_open();
std::wstring coff_file;
try {
coff_file = ConvertASCIIToWide(this->file_);
} catch (std::overflow_error& e) {
return false;
}
try {
ScopedFileAccess const obtain_write(coff_file, GENERIC_ALL);
this->pe_stream_.open(this->file_,
std::ios::in | std::ios::out | std::ios::binary);
return this->pe_stream_.is_open();
} catch (std::system_error& e) {
std::cerr << "Could not obtain write access: " << e.what()
<< " (Error Code: " << e.code().value() << ")" << '\n';
return false;
}
}

bool CoffReaderWriter::Close() {
Expand Down
Loading