BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
AbstractByteBuffer.h
Go to the documentation of this file.
1#pragma once
2
3#include "win_hdrs/win_types.h"
4#include "CustomException.h"
5#include "Util.h"
6
7#include <QtCore>
8
9#include <stdlib.h>
10//------------------------------------------------
11
12#define MAX_DWORD 0xffffffff
13#define MAX_WORD 0xffff
14#define MASK_TO_DWORD(val) ((val < MAX_DWORD) ? (val & MAX_DWORD) : MAX_DWORD)
15#define MASK_TO_WORD(val) ((val < MAX_WORD) ? (val & MAX_WORD) : MAX_WORD)
16
17typedef size_t bufsize_t;
19
20typedef uint64_t offset_t;
23
24
26{
27public:
28 BufferException(const QString info) : CustomException(info) {}
29};
30
31namespace buf_util {
33};
34
36{
37public:
38 static bool isValid(AbstractByteBuffer *buf);
39
41 virtual ~AbstractByteBuffer() { }
42
44 virtual BYTE* getContent() = 0;
45 virtual bool isTruncated() { return false; }
46 virtual bool isResized() { return false; }
47
48 BYTE operator[](size_t idx);
49
50 virtual offset_t getOffset(void *ptr, bool allowExceptions = false); // validates
51 virtual BYTE* getContentAt(offset_t offset, bufsize_t size, bool allowExceptions = false);
52 virtual BYTE* getContentAtPtr(BYTE *ptr, bufsize_t size, bool allowExceptions = false);
53
54 virtual bool setBufferedValue(BYTE *dstPtr, BYTE *srcPtr, bufsize_t srcSize, bufsize_t paddingSize, bool allowExceptions = false);
55 bool setStringValue(offset_t rawOffset, QString newText);
56
57 QString getStringValue(offset_t rawOffset, bufsize_t len = BUFSIZE_MAX, bool acceptNonTerminated = false);
58 QString getWStringValue(offset_t rawOffset, bufsize_t len);
59 QString getWAsciiStringValue(offset_t rawOffset, bufsize_t len, bool acceptNonTerminated = false);
60
63
64 bool isAreaEmpty(offset_t rawOffset, bufsize_t size);
65 bool fillContent(BYTE filling);
66 bool pasteBuffer(offset_t rawOffset, AbstractByteBuffer *buf, bool allowTrunc);
67
68 bool containsBlock(offset_t rawOffset, bufsize_t size);
69 bool intersectsBlock(offset_t rawOffset, bufsize_t size);
70
71 uint64_t getNumValue(offset_t offset, bufsize_t size, bool* isOk);
72 bool setNumValue(offset_t offset, bufsize_t size, uint64_t newVal);
73 bool setTextValue(char* textPtr, std::string newText, size_t fieldLimitLen = 0);
74
75 //TODO
76 virtual bool resize(bufsize_t newSize) { return false; }
77 offset_t substFragmentByFile(offset_t offset, bufsize_t contentSize, QFile &fIn);
78};
79
80//--------------------------------------------
81
83{
84public:
86 virtual ~BufferView() { }
87
88 virtual bufsize_t getContentSize();
89 virtual BYTE* getContent();
90
91 bufsize_t getRequestedSize() const { return size; }
92
93protected:
97};
98
const bufsize_t BUFSIZE_MAX
const offset_t OFFSET_MAX
const offset_t INVALID_ADDR
uint64_t offset_t
size_t bufsize_t
virtual bufsize_t getContentSize()=0
bufsize_t getMaxSizeFromOffset(offset_t startOffset)
virtual bool isTruncated()
bool intersectsBlock(offset_t rawOffset, bufsize_t size)
QString getStringValue(offset_t rawOffset, bufsize_t len=BUFSIZE_MAX, bool acceptNonTerminated=false)
virtual BYTE * getContentAtPtr(BYTE *ptr, bufsize_t size, bool allowExceptions=false)
virtual bool isResized()
virtual BYTE * getContent()=0
bool setTextValue(char *textPtr, std::string newText, size_t fieldLimitLen=0)
bufsize_t getMaxSizeFromPtr(BYTE *ptr)
bool setNumValue(offset_t offset, bufsize_t size, uint64_t newVal)
virtual bool setBufferedValue(BYTE *dstPtr, BYTE *srcPtr, bufsize_t srcSize, bufsize_t paddingSize, bool allowExceptions=false)
virtual BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
offset_t substFragmentByFile(offset_t offset, bufsize_t contentSize, QFile &fIn)
bool pasteBuffer(offset_t rawOffset, AbstractByteBuffer *buf, bool allowTrunc)
bool setStringValue(offset_t rawOffset, QString newText)
uint64_t getNumValue(offset_t offset, bufsize_t size, bool *isOk)
bool containsBlock(offset_t rawOffset, bufsize_t size)
BYTE operator[](size_t idx)
virtual bool resize(bufsize_t newSize)
static bool isValid(AbstractByteBuffer *buf)
QString getWStringValue(offset_t rawOffset, bufsize_t len)
bool isAreaEmpty(offset_t rawOffset, bufsize_t size)
virtual offset_t getOffset(void *ptr, bool allowExceptions=false)
QString getWAsciiStringValue(offset_t rawOffset, bufsize_t len, bool acceptNonTerminated=false)
bool fillContent(BYTE filling)
BufferException(const QString info)
virtual ~BufferView()
bufsize_t getRequestedSize() const
BufferView(AbstractByteBuffer *parent, offset_t offset, bufsize_t size)
AbstractByteBuffer * parent
virtual BYTE * getContent()
virtual bufsize_t getContentSize()
CustomException(const QString info, const int32_t code=UNKNOWN_EXCEPTION)
bufsize_t roundupToUnit(bufsize_t size, bufsize_t unit)