BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
ByteBuffer.cpp
Go to the documentation of this file.
1#include "ByteBuffer.h"
2#include <iostream>
3
5 : content(nullptr), contentSize(0), padding(v_padding),
7{
8 if (v_size == 0) throw BufferException("Zero size requested");
9
10 this->content = allocContent(v_size, v_padding);
11 this->contentSize = v_size;
12 this->originalSize = v_size;
13}
14
15ByteBuffer::ByteBuffer(BYTE *v_content, bufsize_t v_size, bufsize_t v_padding)
16 : content(nullptr), contentSize(0), padding(v_padding),
17 originalSize(0), m_refs(0)
18{
19 if (v_size == 0) throw BufferException("Zero size requested");
20
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 }
27}
28
30 : content(NULL), contentSize(0), padding(0),
31 originalSize(0), m_refs(0)
32{
33 if (!v_parent) throw BufferException("Cannot make subBuffer for NULL buffer!");
34 if (!v_size) throw BufferException("Cannot make 0 size buffer!");
35
36 bufsize_t parentSize = v_parent->getContentSize();
37
38 bufsize_t copySize = v_size < parentSize ? v_size : parentSize;
39 bufsize_t allocSize = v_size > parentSize ? v_size : parentSize;
40
41 BYTE *bContent = v_parent->getContentAt(v_offset, copySize);
42 if (!bContent) throw BufferException("Cannot make Buffer for NULL content!");
43
44 this->content = allocContent(allocSize, v_padding);
45 if (this->content) {
46 this->contentSize = allocSize;
47 this->originalSize = this->contentSize;
48 ::memcpy(this->content, bContent, copySize);
49 }
50}
51
53{
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;
64 throw BufferException("Cannot allocate buffer of size: 0x" + QString::number(allocSize, 16));
65 }
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 }*/
76 return content;
77}
78
80{
81 if (newSize == this->contentSize) {
82 return true;
83 }
84 BYTE *newContent = nullptr;
85 bool isOk = true;
86 try {
87 newContent = allocContent(newSize, this->padding);
88 if (newContent) {
89 this->content = newContent;
90 this->contentSize = newSize;
91 }
92 } catch (const BufferException&) {
93 isOk = false;
94 }
95 return isOk;
96}
97
99{
100 std::cerr << "Ptr: " << std::hex << (void*)content << " BaseContentSize: " << this->contentSize << " : " << __FUNCTION__ << std::endl;
101 ::free(this->content);
102}
uint32_t bufsize_t
uint64_t offset_t
virtual bufsize_t getContentSize()=0
virtual BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
bufsize_t originalSize
Definition ByteBuffer.h:44
BYTE * content
Definition ByteBuffer.h:40
virtual ~ByteBuffer()
bufsize_t contentSize
Definition ByteBuffer.h:41
ByteBuffer(bufsize_t v_size, bufsize_t padding=DEFAULT_PADDING)
Definition ByteBuffer.cpp:4
size_t m_refs
Definition ByteBuffer.h:46
BYTE * allocContent(bufsize_t v_size, bufsize_t padding)
bufsize_t padding
Definition ByteBuffer.h:42
virtual bool resize(bufsize_t newSize)