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