BearParser
Portable Executable parsing library (from PE-bear)
ByteBuffer.cpp
Go to the documentation of this file.
1 #include "ByteBuffer.h"
2 
4  : content(NULL), contentSize(v_size), padding(v_padding)
5 {
6  if (v_size == 0) throw BufferException("Zero size requested");
7 
8  this->content = allocContent(v_size, v_padding);
9  this->contentSize = v_size;
10 }
11 
12 ByteBuffer::ByteBuffer(BYTE *v_content, bufsize_t v_size, bufsize_t v_padding)
13  : content(NULL), contentSize(v_size), padding(v_padding)
14 {
15  if (v_size == 0) throw BufferException("Zero size requested");
16 
17  this->content = allocContent(v_size, v_padding);
18  this->contentSize = v_size;
19  memcpy(this->content, v_content, v_size);
20 }
21 
22 ByteBuffer::ByteBuffer(AbstractByteBuffer *v_parent, offset_t v_offset, bufsize_t v_size, bufsize_t v_padding)
23 {
24  if (v_parent == NULL) throw BufferException("Cannot make subBuffer for NULL buffer!");
25  if (v_size == 0) throw BufferException("Cannot make 0 size buffer!");
26 
27  bufsize_t parentSize = v_parent->getContentSize();
28 
29  bufsize_t copySize = v_size < parentSize ? v_size : parentSize;
30  bufsize_t allocSize = v_size > parentSize ? v_size : parentSize;
31 
32  BYTE *bContent = v_parent->getContentAt(v_offset, copySize);
33  if (bContent == NULL) throw BufferException("Cannot make Buffer for NULL content!");
34 
35  this->content = allocContent(allocSize, v_padding);
36  this->contentSize = allocSize;
37 
38  memcpy(this->content, bContent, copySize);
39 }
40 
42 {
43  if (v_size == 0) throw BufferException("Zero size requested");
44  bufsize_t allocSize = v_size + v_padding;
45  BYTE* content = new (std::nothrow) BYTE[allocSize];
46  if (content == NULL) {
47  throw BufferException("Cannot allocate buffer of size: 0x" + QString::number(allocSize, 16));
48  }
49  memset(content, 0, allocSize);
50  return content;
51 }
52 
54 {
55  if (newSize == this->contentSize) return true;
56 
57  BYTE *newContent = NULL;
58  try {
59  newContent = allocContent(newSize, this->padding);
60  } catch(BufferException &e) {
61  newContent = NULL;
62  }
63  if (newContent == NULL) return false;
64 
65  BYTE *oldContent = this->content;
66  bufsize_t oldSize = this->contentSize;
67  bufsize_t copySize = newSize < oldSize ? newSize : oldSize;
68 
69  memcpy(newContent, oldContent, copySize);
70 
71  this->content = newContent;
72  this->contentSize = newSize;
73 
74  delete []oldContent;
75  return true;
76 }
77 
79 {
80  delete []this->content;
81 }
82 
83 
84 
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)
BYTE * content
Definition: ByteBuffer.h:21
virtual ~ByteBuffer()
Definition: ByteBuffer.cpp:78
bufsize_t contentSize
Definition: ByteBuffer.h:22
ByteBuffer(bufsize_t v_size, bufsize_t padding=DEFAULT_PADDING)
Definition: ByteBuffer.cpp:3
BYTE * allocContent(bufsize_t v_size, bufsize_t padding)
Definition: ByteBuffer.cpp:41
bufsize_t padding
Definition: ByteBuffer.h:23
virtual bool resize(bufsize_t newSize)
Definition: ByteBuffer.cpp:53