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
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
13ByteBuffer::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
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
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
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
84{
85 delete []this->content;
86}
87
88
89
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:28
BYTE * content
Definition ByteBuffer.h:24
virtual ~ByteBuffer()
bufsize_t contentSize
Definition ByteBuffer.h:25
ByteBuffer(bufsize_t v_size, bufsize_t padding=DEFAULT_PADDING)
Definition ByteBuffer.cpp:3
BYTE * allocContent(bufsize_t v_size, bufsize_t padding)
bufsize_t padding
Definition ByteBuffer.h:26
virtual bool resize(bufsize_t newSize)