Skip to content

Commit c0ac0f6

Browse files
committed
Merge branch 'use_realloc'
2 parents 7c9ab10 + a610f3b commit c0ac0f6

8 files changed

Lines changed: 71 additions & 53 deletions

File tree

parser/ByteBuffer.cpp

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,102 @@
11
#include "ByteBuffer.h"
2+
#include <iostream>
23

34
ByteBuffer::ByteBuffer(bufsize_t v_size, bufsize_t v_padding)
4-
: content(NULL), contentSize(v_size), padding(v_padding),
5-
originalSize(v_size)
5+
: content(nullptr), contentSize(0), padding(v_padding),
6+
originalSize(0)
67
{
78
if (v_size == 0) throw BufferException("Zero size requested");
89

9-
this->content = allocContent(v_size, v_padding);
10+
this->content = allocContent(v_size, v_padding);
1011
this->contentSize = v_size;
12+
this->originalSize = v_size;
1113
}
1214

1315
ByteBuffer::ByteBuffer(BYTE *v_content, bufsize_t v_size, bufsize_t v_padding)
14-
: content(NULL), contentSize(v_size), padding(v_padding),
15-
originalSize(v_size)
16+
: content(nullptr), contentSize(0), padding(v_padding),
17+
originalSize(0)
1618
{
1719
if (v_size == 0) throw BufferException("Zero size requested");
1820

19-
this->content = allocContent(v_size, v_padding);
20-
this->contentSize = v_size;
21-
::memcpy(this->content, v_content, v_size);
21+
this->content = allocContent(v_size, v_padding);
22+
if (this->content) {
23+
this->contentSize = v_size;
24+
this->originalSize = v_size;
25+
::memcpy(this->content, v_content, v_size);
26+
}
2227
}
2328

2429
ByteBuffer::ByteBuffer(AbstractByteBuffer *v_parent, offset_t v_offset, bufsize_t v_size, bufsize_t v_padding)
2530
: content(NULL), contentSize(0), padding(0),
2631
originalSize(0)
2732
{
28-
if (v_parent == NULL) throw BufferException("Cannot make subBuffer for NULL buffer!");
29-
if (v_size == 0) throw BufferException("Cannot make 0 size buffer!");
33+
if (!v_parent) throw BufferException("Cannot make subBuffer for NULL buffer!");
34+
if (!v_size) throw BufferException("Cannot make 0 size buffer!");
3035

3136
bufsize_t parentSize = v_parent->getContentSize();
3237

3338
bufsize_t copySize = v_size < parentSize ? v_size : parentSize;
3439
bufsize_t allocSize = v_size > parentSize ? v_size : parentSize;
3540

3641
BYTE *bContent = v_parent->getContentAt(v_offset, copySize);
37-
if (bContent == NULL) throw BufferException("Cannot make Buffer for NULL content!");
42+
if (!bContent) throw BufferException("Cannot make Buffer for NULL content!");
3843

3944
this->content = allocContent(allocSize, v_padding);
40-
this->contentSize = allocSize;
41-
this->originalSize = this->contentSize;
42-
::memcpy(this->content, bContent, copySize);
45+
if (this->content) {
46+
this->contentSize = allocSize;
47+
this->originalSize = this->contentSize;
48+
::memcpy(this->content, bContent, copySize);
49+
}
4350
}
4451

4552
BYTE* ByteBuffer::allocContent(bufsize_t v_size, bufsize_t v_padding)
4653
{
47-
if (v_size == 0) throw BufferException("Zero size requested");
48-
bufsize_t allocSize = v_size + v_padding;
49-
BYTE* content = new (std::nothrow) BYTE[allocSize];
50-
if (content == NULL) {
54+
if (!v_size) throw BufferException("Zero size requested");
55+
56+
const bufsize_t allocSize = v_size + v_padding;
57+
std::cerr << "Trying to resize. Ptr: " << std::hex << (void*)content << " New size: " << allocSize << std::endl;
58+
59+
const bufsize_t sizeDiff = (allocSize > this->contentSize) ? (allocSize - this->contentSize) : 0;
60+
BYTE* content = reinterpret_cast<BYTE*>(::realloc((void*)this->content, allocSize));
61+
62+
if (!content) {
63+
std::cerr << "Error!" << std::endl;
5164
throw BufferException("Cannot allocate buffer of size: 0x" + QString::number(allocSize, 16));
5265
}
53-
::memset(content, 0, allocSize);
66+
if (sizeDiff) {
67+
std::cerr << "Ptr: " << std::hex << (void*)content << " Additional size: " << sizeDiff << " BaseContentSize: " << this->contentSize << std::endl;
68+
::memset(content + this->contentSize, 0, sizeDiff);
69+
} else {
70+
std::cerr << "Ptr: " << std::hex << (void*)content << " New size: " << allocSize << std::endl;
71+
}
72+
/*if (sizeDiff) {
73+
// if some memory has been added to the existing buffer, initialize it with 0
74+
::memset(content + this->contentSize, 0, sizeDiff);
75+
}*/
5476
return content;
5577
}
5678

5779
bool ByteBuffer::resize(bufsize_t newSize)
5880
{
59-
if (newSize == this->contentSize) return true;
60-
61-
BYTE *newContent = NULL;
81+
if (newSize == this->contentSize) {
82+
return true;
83+
}
84+
BYTE *newContent = nullptr;
85+
bool isOk = true;
6286
try {
6387
newContent = allocContent(newSize, this->padding);
64-
} catch(BufferException) {
65-
newContent = NULL;
88+
if (newContent) {
89+
this->content = newContent;
90+
this->contentSize = newSize;
91+
}
92+
} catch (BufferException &e) {
93+
isOk = false;
6694
}
67-
if (!newContent) return false;
68-
69-
BYTE *oldContent = this->content;
70-
bufsize_t oldSize = this->contentSize;
71-
bufsize_t copySize = newSize < oldSize ? newSize : oldSize;
72-
73-
::memcpy(newContent, oldContent, copySize);
74-
75-
this->content = newContent;
76-
this->contentSize = newSize;
77-
78-
delete []oldContent;
79-
80-
return true;
95+
return isOk;
8196
}
8297

8398
ByteBuffer::~ByteBuffer()
8499
{
85-
delete []this->content;
100+
std::cerr << "Ptr: " << std::hex << (void*)content << " BaseContentSize: " << this->contentSize << " : " << __FUNCTION__ << std::endl;
101+
::free(this->content);
86102
}
87-
88-
89-

parser/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ else()
2020
get_target_property(QtCore_location Qt${QT_VERSION_MAJOR}::Core LOCATION)
2121
endif()
2222

23+
# multi-processor compilation
24+
if(MSVC)
25+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
26+
endif()
27+
2328
include_directories (include/bearparser)
2429

2530
set (win_hdrs

parser/include/bearparser/pe/LdConfigDirWrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class LdConfigDirWrapper : public DataDirEntryWrapper
180180

181181
std::vector<ExeNodeWrapper*>* getSubEntriesList(size_t parentType)
182182
{
183-
std::map<uint32_t, std::vector<ExeNodeWrapper*> >::iterator itr = subEntriesMap.find(parentType);
183+
auto itr = subEntriesMap.find((uint32_t)parentType);
184184
if (itr == subEntriesMap.end()){
185185
return NULL;
186186
}

parser/include/bearparser/pe/PEFile.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ class PEFile : public MappedExe
324324

325325
/* wrappers:
326326
*/
327-
bool hasDirectory(pe::dir_entry dirNum)
327+
bool hasDirectory(const pe::dir_entry &dirNum)
328328
{
329329
return this->getDataDirEntry(dirNum) ? true : false;
330330
}
@@ -339,8 +339,7 @@ class PEFile : public MappedExe
339339
return this->getAlignment(Executable::RVA);
340340
}
341341

342-
343-
void setImageSize(size_t newSize)
342+
void setImageSize(bufsize_t newSize)
344343
{
345344
this->setVirtualSize(newSize);
346345
}

parser/include/bearparser/pe/rsrc/ResourceStringsWrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ResString : public ExeNodeWrapper {
3636
WORD *content = (WORD*) this->m_Exe->getContentAt(offset,Executable::RAW, size);
3737
if (content == NULL) return "";
3838

39-
return QString::fromUtf16(content, size);
39+
return QString::fromUtf16(reinterpret_cast<const char16_t*>(content), size);
4040
}
4141

4242
virtual size_t getStrLen() { return (sizePtr == NULL) ? 0 : static_cast<size_t>(*sizePtr); }

parser/include/bearparser/pe/rsrc/ResourceVersionWrapper.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,17 @@ class ResourceVersionWrapper : public ResourceContentWrapper
4646
virtual WrappedValue::data_type containsDataType(size_t fieldId, size_t subField = FIELD_NONE);
4747

4848
pe::version_info *getVersionInfo();
49+
4950
QString getVersionText()
5051
{
5152
pe::version_info *info = getVersionInfo();
52-
if (info == NULL) return ""; //ERROR
53+
if (!info) return ""; //ERROR
5354

5455
int size = INFOTEXT_LEN;
55-
WORD *content = info->key;
56-
if (content == NULL) return "";
56+
const WORD *content = info->key;
57+
if (!content) return "";
5758

58-
return QString::fromUtf16(content, size);
59+
return QString::fromUtf16(reinterpret_cast<const char16_t*>(content), size);
5960
}
6061

6162

parser/pe/RichHdrWrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ DWORD RichHdrWrapper::calcChecksum()
164164
if (!data || dansOffset == INVALID_ADDR) return 0;
165165

166166
DWORD cksum = dansOffset;
167-
for (size_t i = 0; i < dansOffset && i < dataSize; i++) {
167+
for (size_t i = 0; i < (size_t)dansOffset && i < dataSize; i++) {
168168
//skip e_lfanew
169169
if (i >= 0x3c && i < 0x40) {
170170
continue;

parser/pe/SectHdrsWrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ bufsize_t SectionHdrWrapper::getMappedVirtualSize()
327327
}
328328

329329
// trim to next section
330-
int secCounter = m_PE->_getSectionsCount(true);
330+
size_t secCounter = m_PE->_getSectionsCount(true);
331331
for (size_t i = 0; i < secCounter; i++) {
332332
SectionHdrWrapper *sec = m_PE->_getSecHdr(i);
333333
if (!sec) continue;

0 commit comments

Comments
 (0)