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 const bufsize_t sizeDiff = (allocSize > this->contentSize) ? (allocSize - this->contentSize) : 0;
58
59 BYTE* content = reinterpret_cast<BYTE*>(::realloc((void*)this->content, allocSize));
60 if (!content) {
61 throw BufferException("Cannot allocate buffer of size: 0x" + QString::number(allocSize, 16));
62 }
63 if (sizeDiff) {
64 ::memset(content + this->contentSize, 0, sizeDiff);
65 }
66 return content;
67}
68
70{
71 if (newSize == this->contentSize) {
72 return true;
73 }
74 BYTE *newContent = nullptr;
75 bool isOk = true;
76 try {
77 newContent = allocContent(newSize, this->padding);
78 if (newContent) {
79 this->content = newContent;
80 this->contentSize = newSize;
81 }
82 } catch (const BufferException&) {
83 isOk = false;
84 }
85 return isOk;
86}
87
89{
90 ::free(this->content);
91}
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)