From 094dff7d3846debbc80cb6385e950e36983a5cc7 Mon Sep 17 00:00:00 2001 From: Georg Sauthoff Date: Sun, 28 Feb 2016 00:01:01 +0100 Subject: [PATCH] Speedup copying of C-strings Also, the copy functionality is now inside an extra function, thus easier to maintain. --- parser-library/parse.cpp | 49 ++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/parser-library/parse.cpp b/parser-library/parse.cpp index 06e0567..f157203 100644 --- a/parser-library/parse.cpp +++ b/parser-library/parse.cpp @@ -24,6 +24,9 @@ THE SOFTWARE. #include #include +#include +#include +#include #include "parse.h" #include "nt-headers.h" #include "to_string.h" @@ -91,6 +94,23 @@ string GetPEErrLoc() { return err_loc; } +static bool readCString(const bounded_buffer &buffer, ::uint32_t off, + string &result) +{ + if (off < buffer.bufLen) { + ::uint8_t *p = buffer.buf; + ::uint32_t n = buffer.bufLen; + ::uint8_t *b = p + off; + ::uint8_t *x = std::find(b, p + n, 0); + if (x == p + n) + return false; + result.insert(result.end(), b, x); + return true; + } else { + return false; + } +} + bool getSecForVA(list
&secs, VA v, section &sec) { for(list
::iterator it = secs.begin(), e = secs.end(); it != e; @@ -728,23 +748,13 @@ parsed_pe *ParsePEFromFile(const char *filePath) { ::uint32_t nameOff = nameVA - nameSec.sectionBase; string modName; - ::uint8_t c; - do { - if(readByte(nameSec.sectionData, nameOff, c) == false) { + if (readCString(*nameSec.sectionData, nameOff, modName) == false) { deleteBuffer(remaining); deleteBuffer(p->fileBuffer); delete p; PE_ERR(PEERR_READ); return NULL; - } - - if(c == 0) { - break; - } - - modName.push_back(c); - nameOff++; - }while(true); + } //now, get all the named export symbols ::uint32_t numNames; @@ -1190,23 +1200,14 @@ parsed_pe *ParsePEFromFile(const char *filePath) { ::uint32_t nameOff = name - nameSec.sectionBase; string modName; - ::uint8_t c; - do { - if(readByte(nameSec.sectionData, nameOff, c) == false) { + if (readCString(*nameSec.sectionData, nameOff, modName) == false ) { deleteBuffer(remaining); deleteBuffer(p->fileBuffer); delete p; PE_ERR(PEERR_READ); return NULL; - } - - if(c == 0) { - break; - } - - modName.push_back(toupper(c)); - nameOff++; - }while(true); + } + boost::to_upper(modName); //then, try and get all of the sub-symbols VA lookupVA;