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#include <limits.h>
4
5
6void ByteBuffer::_init(const bufsize_t& v_size, const bufsize_t& v_padding)
7{
8 if (v_size == 0) throw BufferException("Zero size requested");
9
10 this->content = allocContent(v_size, v_padding);
11 if (this->content) {
12 this->padding = v_padding;
13 this->contentSize = v_size;
14 this->originalSize = v_size;
15 }
16}
17
19 : content(nullptr), contentSize(0), padding(0), originalSize(0)
20{
21 _init(v_size, v_padding);
22}
23
24ByteBuffer::ByteBuffer(BYTE* v_content, bufsize_t v_size, bufsize_t v_padding)
25 : content(nullptr), contentSize(0), padding(0), originalSize(0)
26{
27 _init(v_size, v_padding);
28 if (this->content && v_content) {
29 ::memcpy(this->content, v_content, v_size);
30 }
31}
32
34 : content(nullptr), contentSize(0), padding(0), originalSize(0)
35{
36 if (!v_parent) throw BufferException("Cannot make subBuffer for NULL buffer!");
37 if (!v_size) throw BufferException("Cannot make 0 size buffer!");
38
39 bufsize_t parentSize = v_parent->getContentSize();
40
41 bufsize_t copySize = v_size < parentSize ? v_size : parentSize;
42 bufsize_t allocSize = v_size > parentSize ? v_size : parentSize;
43
44 BYTE *bContent = v_parent->getContentAt(v_offset, copySize);
45 if (!bContent) throw BufferException("Cannot make Buffer for NULL content!");
46
47 _init(v_size, v_padding);
48 if (this->content && bContent) {
49 ::memcpy(this->content, bContent, v_size);
50 }
51}
52
54{
55 if (!v_size) throw BufferException("Zero size requested");
56 if (v_size >= BUFSIZE_MAX || v_size >= LLONG_MAX) throw BufferException("Too big size requested");
57
58 const bufsize_t allocSize = v_size + v_padding;
59 const bufsize_t sizeDiff = (allocSize > this->contentSize) ? (allocSize - this->contentSize) : 0;
60
61 BYTE* content = reinterpret_cast<BYTE*>(::realloc((void*)this->content, allocSize));
62 if (!content) {
63 throw BufferException("Cannot allocate buffer of size: 0x" + QString::number(allocSize, 16));
64 }
65 if (sizeDiff) {
66 ::memset(content + this->contentSize, 0, sizeDiff);
67 }
68 return content;
69}
70
72{
73 if (newSize == this->contentSize) {
74 return true;
75 }
76 BYTE *newContent = nullptr;
77 bool isOk = true;
78 try {
79 newContent = allocContent(newSize, this->padding);
80 if (newContent) {
81 this->content = newContent;
82 this->contentSize = newSize;
83 }
84 } catch (const BufferException&) {
85 isOk = false;
86 }
87 return isOk;
88}
89
91{
92 ::free(this->content);
93}
const bufsize_t BUFSIZE_MAX
uint64_t offset_t
size_t bufsize_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)
BYTE * allocContent(bufsize_t v_size, bufsize_t padding)
bufsize_t padding
Definition ByteBuffer.h:26
virtual bool resize(bufsize_t newSize)