From 5b41545749c2c8da64badc74aff265ae8a2b1e36 Mon Sep 17 00:00:00 2001 From: hasherezade Date: Sat, 5 Nov 2016 03:06:34 +0100 Subject: [PATCH] [FEATURE] Log everything via Logger --- parser/AbstractByteBuffer.cpp | 11 +++++------ parser/AbstractByteBuffer.h | 4 +--- parser/ByteBuffer.cpp | 2 -- parser/ExeElementWrapper.cpp | 4 ++-- parser/ExeFactory.cpp | 1 - parser/ExeNodeWrapper.cpp | 4 ++-- parser/Executable.cpp | 2 +- parser/FileBuffer.cpp | 8 ++++---- parser/Util.cpp | 12 +++++++++--- parser/Util.h | 11 +++++++++-- parser/pe/ExceptionDirWrapper.cpp | 9 ++++----- parser/pe/PEFile.cpp | 12 ++++++------ parser/pe/PEFile.h | 2 +- 13 files changed, 44 insertions(+), 38 deletions(-) diff --git a/parser/AbstractByteBuffer.cpp b/parser/AbstractByteBuffer.cpp index 4cbc5c93..d85d83d3 100644 --- a/parser/AbstractByteBuffer.cpp +++ b/parser/AbstractByteBuffer.cpp @@ -1,5 +1,4 @@ #include "AbstractByteBuffer.h" -#include "Util.h" bufsize_t buf_util::roundupToUnit(bufsize_t size, bufsize_t unit) { @@ -213,7 +212,7 @@ bool AbstractByteBuffer::pasteBuffer(offset_t rawOffset, AbstractByteBuffer *buf bufsize_t mySize = this->getContentSize(); if (static_cast(mySize) <= rawOffset) { - if (DBG_LVL) printf("Too far offset requested: %lX while mySize: %X\n", rawOffset, mySize); + Logger::append(Logger::ERROR, "Too far offset requested: %lX while mySize: %X\n", rawOffset, mySize); return false; } BYTE *target = this->getContentAt(rawOffset, sizeToFill); @@ -261,11 +260,11 @@ bool AbstractByteBuffer::intersectsBlock(offset_t rawOffset, bufsize_t size) offset_t srchdEnd = rawOffset + size; if (rawOffset >= startOffset && rawOffset <= endOffset) { - //printf("Fount in bounds: %x - %x start: %x\n", startOffset, endOffset, rawOffset); + Logger::append(Logger::INFO, "Found in bounds: %lx - %lx end: %lx\n", startOffset, endOffset, rawOffset); return true; } if (srchdEnd >= startOffset && srchdEnd <= endOffset) { - //printf("Fount in bounds: %x - %x end: %x\n", startOffset, endOffset, endOffset); + Logger::append(Logger::INFO, "Found in bounds: %lx - %lx end: %lx\n", startOffset, endOffset, endOffset); return true; } return false; @@ -298,7 +297,7 @@ bool AbstractByteBuffer::setNumValue(offset_t offset, bufsize_t size, uint64_t n if (size == 0 || offset == INVALID_ADDR) return false; void* ptr = this->getContentAt(offset, size); if (ptr == NULL) { - if (DBG_LVL) printf("Cannot get Ptr at: %lX of size: %x!\n", offset, size); + Logger::append(Logger::ERROR,"Cannot get Ptr at: %lX of size: %x!\n", offset, size); return false; } @@ -326,7 +325,7 @@ bool AbstractByteBuffer::setNumValue(offset_t offset, bufsize_t size, uint64_t n if ((*valPtr) == nVal) return false; (*valPtr) = nVal; } else { - if (DBG_LVL) printf("Wrong size!\n"); + Logger::append(Logger::ERROR, "Wrong size!\n"); return false; } return true; diff --git a/parser/AbstractByteBuffer.h b/parser/AbstractByteBuffer.h index e4c91c96..e4d5626a 100644 --- a/parser/AbstractByteBuffer.h +++ b/parser/AbstractByteBuffer.h @@ -2,15 +2,13 @@ #include "win_hdrs/win_types.h" #include "CustomException.h" +#include "Util.h" #include #include #include -#define DBG_LVL 0 -#define TRACE() if (DBG_LVL) printf(">%s line: %d [%s]\n", __FUNCTION__, __LINE__, __FILE__); -#define LOG(msg) if (DBG_LVL) printf("%s: %s\n", __FUNCTION__,msg); //------------------------------------------------ typedef uint32_t bufsize_t; diff --git a/parser/ByteBuffer.cpp b/parser/ByteBuffer.cpp index 3dbdb2b6..2c8f0ec9 100644 --- a/parser/ByteBuffer.cpp +++ b/parser/ByteBuffer.cpp @@ -36,7 +36,6 @@ ByteBuffer::ByteBuffer(AbstractByteBuffer *v_parent, offset_t v_offset, bufsize_ this->contentSize = allocSize; memcpy(this->content, bContent, copySize); - TRACE(); } BYTE* ByteBuffer::allocContent(bufsize_t v_size, bufsize_t v_padding) @@ -79,7 +78,6 @@ bool ByteBuffer::resize(bufsize_t newSize) ByteBuffer::~ByteBuffer() { delete []this->content; - TRACE(); } diff --git a/parser/ExeElementWrapper.cpp b/parser/ExeElementWrapper.cpp index 7a891c46..19962842 100644 --- a/parser/ExeElementWrapper.cpp +++ b/parser/ExeElementWrapper.cpp @@ -105,11 +105,11 @@ bool ExeElementWrapper::copyToOffset(offset_t rawOffset) if (this->m_Exe == NULL) return false; if (canCopyToOffset(rawOffset) == false) { - if (DBG_LVL) printf("The area is not empty!\n"); + Logger::append(Logger::ERROR,"The area is not empty!\n"); return false; } if (m_Exe->pasteBuffer(rawOffset, this, false) == false) { - if (DBG_LVL) printf("Cannot paste the buffer!\n"); + Logger::append(Logger::ERROR,"Cannot paste the buffer!\n"); return false; } return true; diff --git a/parser/ExeFactory.cpp b/parser/ExeFactory.cpp index f659848e..e1e3b6a2 100644 --- a/parser/ExeFactory.cpp +++ b/parser/ExeFactory.cpp @@ -24,7 +24,6 @@ void ExeFactory::destroy() builders.clear(); } - ExeFactory::exe_type ExeFactory::findMatching(AbstractByteBuffer *buf) { std::map::iterator itr; diff --git a/parser/ExeNodeWrapper.cpp b/parser/ExeNodeWrapper.cpp index 787cc371..847899b0 100644 --- a/parser/ExeNodeWrapper.cpp +++ b/parser/ExeNodeWrapper.cpp @@ -58,7 +58,7 @@ bool ExeNodeWrapper::canAddEntry() bufsize_t paddedSize = entrySize * 2; bool haveSpace = this->m_Exe->isAreaEmpty(nextOffset, paddedSize); - if (DBG_LVL) printf("nextOffset = %lX size = %X, canAdd: %d\n", nextOffset, entrySize, haveSpace); + Logger::append(Logger::INFO,"NextOffset = %lX size = %X, canAdd: %d\n", nextOffset, entrySize, haveSpace); return haveSpace; } @@ -115,7 +115,7 @@ ExeNodeWrapper* ExeNodeWrapper::addEntryAt(ExeNodeWrapper *entry, offset_t nextO } if (loadNextEntry(entryNum) == false) return NULL; reloadMapping(); -// printf("ENTRIES NUM %d\n", this->getEntriesCount()); + Logger::append(Logger::INFO,"Entries count: %lu\n", this->getEntriesCount()); return getLastEntry(); } diff --git a/parser/Executable.cpp b/parser/Executable.cpp index 2074609a..818e88a4 100644 --- a/parser/Executable.cpp +++ b/parser/Executable.cpp @@ -86,7 +86,7 @@ offset_t Executable::toRaw(offset_t offset, addr_type aT, bool allowExceptions) if (offset == INVALID_ADDR) return INVALID_ADDR; if (this->isValidAddr(offset, aT) == false) { - if (DBG_LVL) printf("Address out of bounds: offset = %lX addrType = %d\n", offset, aT); + Logger::append(Logger::ERROR, "Address out of bounds: offset = %lX addrType = %d\n", offset, aT); if (allowExceptions) throw CustomException("Address out of bounds!"); return INVALID_ADDR; } diff --git a/parser/FileBuffer.cpp b/parser/FileBuffer.cpp index 05144862..65fb72ff 100644 --- a/parser/FileBuffer.cpp +++ b/parser/FileBuffer.cpp @@ -59,20 +59,20 @@ ByteBuffer* AbstractFileBuffer::read(QFile &fIn, bufsize_t minBufSize) //throws if (content == NULL) throw FileBufferException("Cannot allocate buffer"); //printf("Reading...%lx , BUFSIZE_MAX = %lx\n", allocSize, BUFSIZE_MAX); - bufsize_t redSize = 0; + bufsize_t readSize = 0; offset_t prevOffset = 0; offset_t maxOffset = contentSize - 1; while (fIn.pos() < maxOffset) { - bufsize_t maxSize = contentSize - redSize; + bufsize_t maxSize = contentSize - readSize; if (maxSize > FILEVIEW_MAXSIZE) maxSize = FILEVIEW_MAXSIZE; - redSize += fIn.read(content + redSize, maxSize); + readSize += fIn.read(content + readSize, maxSize); if (prevOffset == fIn.pos()) break; //cannot read more! prevOffset = fIn.pos(); } - //printf("Red size...%lx\n", redSize); + Logger::append(Logger::INFO, "Read size...%lx\n", readSize); return bufferedFile; } diff --git a/parser/Util.cpp b/parser/Util.cpp index 56052b49..62b3dddf 100644 --- a/parser/Util.cpp +++ b/parser/Util.cpp @@ -5,8 +5,11 @@ using namespace pe_util; #define MAX_LINE 255 -bool Logger::append(const char* format, ...) +bool Logger::append(dbg_level lvl, const char* format, ...) { + if (lvl > DBG_LVL) { + return false; + } if (format == NULL) { return false; } @@ -23,9 +26,12 @@ bool Logger::append(const char* format, ...) va_end(argptr); if (printed <= 0) return false; - const char prefix[] = "*"; + const char *prefixes[LVL_COUNT] = {"ERROR", "WARNING", "INFO"}; + if (static_cast(lvl) > static_cast(LVL_COUNT)) { + lvl = ERROR; + } - fprintf(stderr, "[%s] %s", prefix, line); + fprintf(stderr, "[%s] %s\n", prefixes[lvl], line); return true; } diff --git a/parser/Util.h b/parser/Util.h index cb248bd2..2f666433 100644 --- a/parser/Util.h +++ b/parser/Util.h @@ -11,11 +11,18 @@ #define IS_PRINTABLE(c) (c >= 0x20 && c < 0x7f) #define IS_ENDLINE(c) (c == 0x0A || c == 0xD) +//---- +#define DBG_LVL 2 +#define TRACE() if (DBG_LVL) printf(">%s line: %d [%s]\n", __FUNCTION__, __LINE__, __FILE__); +#define LOG(msg) if (DBG_LVL) printf("%s: %s\n", __FUNCTION__,msg); namespace Logger { - bool append(const char* format, ...); + enum dbg_level{ + ERROR = 0, WARNING, INFO, LVL_COUNT + }; + bool append(dbg_level lvl, const char* format, ...); }; - +//---- namespace pe_util { inline bool isPrintable(char c) { return IS_PRINTABLE(c); } diff --git a/parser/pe/ExceptionDirWrapper.cpp b/parser/pe/ExceptionDirWrapper.cpp index 7bd5b32c..0738d9b5 100644 --- a/parser/pe/ExceptionDirWrapper.cpp +++ b/parser/pe/ExceptionDirWrapper.cpp @@ -37,9 +37,8 @@ bool ExceptionDirWrapper::wrap() entry->getNumValue(ExceptionEntryWrapper::PAGE_VA, &isOk), entry->getNumValue(ExceptionEntryWrapper::BLOCK_SIZE, &isOk) );*/ - } - //printf("entries num = %d, parsedSize = %x\n", entries.size(), parsedSize); + Logger::append(Logger::INFO, "Entries num = %lu, parsedSize = %x\n", entries.size(), parsedSize); return true; } @@ -104,9 +103,9 @@ QString ExceptionEntryWrapper::getFieldName(size_t fieldId) Executable::addr_type ExceptionEntryWrapper::containsAddrType(size_t fieldId, size_t subField) { switch (fieldId) { - case BEGIN_ADDR : - case END_ADDR : - case UNWIND_INFO_ADDR : + case BEGIN_ADDR : + case END_ADDR : + case UNWIND_INFO_ADDR : return Executable::RVA; } return Executable::NOT_ADDR; diff --git a/parser/pe/PEFile.cpp b/parser/pe/PEFile.cpp index 19b3ede5..3776e2cc 100644 --- a/parser/pe/PEFile.cpp +++ b/parser/pe/PEFile.cpp @@ -48,13 +48,13 @@ PEFile::PEFile(AbstractByteBuffer *v_buf) { album = new ResourcesAlbum(this); wrap(v_buf); + Logger::append(Logger::INFO,"Wrapped"); } void PEFile::clearWrappers() { initDirEntries(); MappedExe::clearWrappers(); - TRACE(); } void PEFile::initDirEntries() @@ -177,7 +177,7 @@ bool PEFile::setHdrSectionsNum(size_t newNum) uint64_t count = newNum; bool canSet = fHdr->setNumValue(FileHdrWrapper::SEC_NUM , count); if (canSet == false) { - if (DBG_LVL) printf("Can not change FileHdr!\n"); + Logger::append(Logger::ERROR,"Can not change FileHdr!\n"); return false; } return true; @@ -188,7 +188,7 @@ bool PEFile::setVitualSize(bufsize_t newSize) uint64_t size = newSize; bool canSet = optHdr->setNumValue(OptHdrWrapper::IMAGE_SIZE, 0, size); if (canSet == false) { - if (DBG_LVL) printf("Can not change OptHdr!\n"); + Logger::append(Logger::ERROR, "Can not change OptHdr!\n"); return false; } return true; @@ -260,7 +260,7 @@ BufferView* PEFile::createSectionView(size_t secId) { SectionHdrWrapper *sec = this->getSecHdr(secId); if (sec == NULL) { - printf("No such section\n"); + Logger::append(Logger::WARNING, "No such section\n"); return NULL; } Executable::addr_type aType = Executable::RAW; @@ -333,12 +333,12 @@ SectionHdrWrapper* PEFile::addNewSection(QString name, bufsize_t size) bufsize_t newVirtualSize = roundedVirtualEnd + size; if (setVitualSize(newVirtualSize) == false) { - printf("Failed to change virtual size"); + Logger::append(Logger::ERROR, "Failed to change virtual size"); return NULL; } if (resize(newSize) == false) { - printf("Failed to resize"); + Logger::append(Logger::ERROR, "Failed to resize"); return NULL; } // fetch again after resize: diff --git a/parser/pe/PEFile.h b/parser/pe/PEFile.h index 14fd76ad..e3901b7a 100644 --- a/parser/pe/PEFile.h +++ b/parser/pe/PEFile.h @@ -51,7 +51,7 @@ public: }; PEFile(AbstractByteBuffer *v_buf); - virtual ~PEFile() { TRACE(); clearWrappers(); delete album; } + virtual ~PEFile() { clearWrappers(); delete album; } //--- // inherited from Executable: //