BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
parser
ByteBuffer.cpp
Go to the documentation of this file.
1
#include "
ByteBuffer.h
"
2
3
ByteBuffer::ByteBuffer
(
bufsize_t
v_size,
bufsize_t
v_padding)
4
: content(NULL), contentSize(v_size), padding(v_padding),
5
originalSize(v_size)
6
{
7
if
(v_size == 0)
throw
BufferException
(
"Zero size requested"
);
8
9
this->
content
=
allocContent
(v_size, v_padding);
10
this->
contentSize
= v_size;
11
}
12
13
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
{
17
if
(v_size == 0)
throw
BufferException
(
"Zero size requested"
);
18
19
this->
content
=
allocContent
(v_size, v_padding);
20
this->
contentSize
= v_size;
21
::memcpy(this->
content
, v_content, v_size);
22
}
23
24
ByteBuffer::ByteBuffer
(
AbstractByteBuffer
*v_parent,
offset_t
v_offset,
bufsize_t
v_size,
bufsize_t
v_padding)
25
: content(NULL), contentSize(0), padding(0),
26
originalSize(0)
27
{
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!"
);
30
31
bufsize_t
parentSize = v_parent->
getContentSize
();
32
33
bufsize_t
copySize = v_size < parentSize ? v_size : parentSize;
34
bufsize_t
allocSize = v_size > parentSize ? v_size : parentSize;
35
36
BYTE *bContent = v_parent->
getContentAt
(v_offset, copySize);
37
if
(bContent == NULL)
throw
BufferException
(
"Cannot make Buffer for NULL content!"
);
38
39
this->
content
=
allocContent
(allocSize, v_padding);
40
this->
contentSize
= allocSize;
41
this->
originalSize
= this->
contentSize
;
42
::memcpy(this->
content
, bContent, copySize);
43
}
44
45
BYTE*
ByteBuffer::allocContent
(
bufsize_t
v_size,
bufsize_t
v_padding)
46
{
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) {
51
throw
BufferException
(
"Cannot allocate buffer of size: 0x"
+ QString::number(allocSize, 16));
52
}
53
::memset(
content
, 0, allocSize);
54
return
content
;
55
}
56
57
bool
ByteBuffer::resize
(
bufsize_t
newSize)
58
{
59
if
(newSize == this->
contentSize
)
return
true
;
60
61
BYTE *newContent = NULL;
62
try
{
63
newContent =
allocContent
(newSize, this->
padding
);
64
}
catch
(
BufferException
) {
65
newContent = NULL;
66
}
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
;
81
}
82
83
ByteBuffer::~ByteBuffer
()
84
{
85
delete
[]this->
content
;
86
}
87
88
89
bufsize_t
uint32_t bufsize_t
Definition
AbstractByteBuffer.h:14
offset_t
uint64_t offset_t
Definition
AbstractByteBuffer.h:17
ByteBuffer.h
AbstractByteBuffer
Definition
AbstractByteBuffer.h:32
AbstractByteBuffer::getContentSize
virtual bufsize_t getContentSize()=0
AbstractByteBuffer::getContentAt
virtual BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
Definition
AbstractByteBuffer.cpp:57
BufferException
Definition
AbstractByteBuffer.h:22
ByteBuffer::originalSize
bufsize_t originalSize
Definition
ByteBuffer.h:28
ByteBuffer::content
BYTE * content
Definition
ByteBuffer.h:24
ByteBuffer::~ByteBuffer
virtual ~ByteBuffer()
Definition
ByteBuffer.cpp:83
ByteBuffer::contentSize
bufsize_t contentSize
Definition
ByteBuffer.h:25
ByteBuffer::ByteBuffer
ByteBuffer(bufsize_t v_size, bufsize_t padding=DEFAULT_PADDING)
Definition
ByteBuffer.cpp:3
ByteBuffer::allocContent
BYTE * allocContent(bufsize_t v_size, bufsize_t padding)
Definition
ByteBuffer.cpp:45
ByteBuffer::padding
bufsize_t padding
Definition
ByteBuffer.h:26
ByteBuffer::resize
virtual bool resize(bufsize_t newSize)
Definition
ByteBuffer.cpp:57
Generated by
1.12.0