6 printf(
"Invalid roundup unit!\n");
11 if (roundDown < size) unitsNum ++;
12 return unitsNum * unit;
18 if (buf == NULL)
return false;
41 if (buf == NULL || bufSize == 0) {
46 if (allowExceptions)
throw BufferException(
"Pointer before buffer begining!");
49 offset_t offset =
static_cast<BYTE*
>(ptr) - buf;
50 if (offset >= bufSize) {
51 if (allowExceptions)
throw BufferException(
"Pointer does not belong to buffer!");
60 if (allowExceptions)
throw BufferException(
"Invalid address requested!");
70 if (buf == NULL)
return NULL;
72 if (offset >= fileSize ) {
73 if (allowExceptions)
throw BufferException(
"Too far offset requested! Buffer size: "
74 + QString::number(fileSize) +
" vs reguested Offset: 0x" + QString::number(offset, 16));
77 const offset_t endOffset = offset + size;
78 if (endOffset > fileSize) {
79 if (allowExceptions)
throw BufferException(
"Too big size requested! Buffer size: "
80 + QString::number(fileSize) +
" vs end of the requested area: 0x" + QString::number(endOffset, 16));
83 BYTE *cntnt = buf + offset;
92 if (contentSize < startOffset)
return 0;
109 if (dstPtr == srcPtr)
return false;
110 if (dstPtr == NULL || srcPtr == NULL)
return false;
114 printf(
"Invalid copy destination!");
115 if (allowExceptions)
throw BufferException(
"Invalid copy destination!");
121 if (dstMaxSize < size) {
125 if (memcmp(dstPtr, srcPtr, size) == 0) {
128 if (paddingSize != 0) {
129 memset(dstPtr, 0, size);
131 memcpy(dstPtr, srcPtr, srcSize);
137 std::string newTextStr = newText.toStdString();
140 BYTE *dstPtr = this->
getContentAt(rawOffset, newTextLen + 1);
145 const char* newTextC = newTextStr.c_str();
159 return QString::fromUtf8(ptr,
static_cast<int>(asciiLen));
167 size = len * unitSize;
169 WORD* ptr = (WORD*) this->
getContentAt(rawOffset, size);
170 if (ptr == NULL)
return "";
171 return QString::fromUtf16(ptr,
static_cast<int>(len));
179 size = len * unitSize;
185 return QString::fromUtf16(ptr,
static_cast<int>(asciiLen));
191 if (area == NULL)
return false;
194 if (area[i] != 0)
return false;
204 if (buf == NULL)
return false;
206 memset(buf, filling, bufSize);
212 if (
isValid(buf) ==
false ||
isValid(
this) ==
false)
return false;
213 if (buf == NULL || buf->
getContent() == NULL)
return false;
218 if (
static_cast<offset_t>(mySize) <= rawOffset) {
220 "Too far offset requested: %llX while mySize: %lX",
221 static_cast<unsigned long long>(rawOffset),
222 static_cast<unsigned long>(mySize)
226 BYTE *target = this->
getContentAt(rawOffset, sizeToFill);
227 if (target == NULL) {
228 if (allowTrunc ==
false)
return false;
229 sizeToFill = mySize - rawOffset;
232 if (target == NULL)
return false;
233 memcpy(target, source, sizeToFill);
239 if (rawOffset ==
INVALID_ADDR || size == 0)
return false;
242 if (ptr == NULL)
return false;
249 offset_t srchdEnd = rawOffset + size;
250 if (rawOffset >= startOffset && srchdEnd <= endOffset) {
259 if (rawOffset ==
INVALID_ADDR || size == 0)
return false;
262 if (ptr == NULL)
return false;
269 offset_t srchdEnd = rawOffset + size;
270 if (rawOffset >= startOffset && rawOffset <= endOffset) {
272 "Found in bounds: %llX - %llX end: %llX",
273 static_cast<unsigned long long>(startOffset),
274 static_cast<unsigned long long>(endOffset),
275 static_cast<unsigned long long>(rawOffset)
279 if (srchdEnd >= startOffset && srchdEnd <= endOffset) {
281 "Found in bounds: %llX - %llX",
282 static_cast<unsigned long long>(startOffset),
283 static_cast<unsigned long long>(endOffset)
290template <
typename INT_TYPE>
293 INT_TYPE val = *((INT_TYPE*)ptr);
299 if (isOk) (*isOk) =
false;
314 if (isOk) (*isOk) =
true;
318template <
typename INT_TYPE>
321 INT_TYPE* valPtr = (INT_TYPE*)ptr;
322 if ((*valPtr) == nVal)
return false;
333 "Cannot get Ptr at: %llX of size: %lX!",
334 static_cast<unsigned long long>(offset),
335 static_cast<unsigned long>(size)
340 if (size ==
sizeof(uint8_t)) {
344 else if (size ==
sizeof(uint16_t)) {
348 else if (size ==
sizeof(uint32_t)) {
352 else if (size ==
sizeof(uint64_t)) {
364 if (!textPtr)
return false;
366 size_t newLen = newText.length() + 1;
376 const char* newTextC = newText.c_str();
377 if (!strcmp(newTextC, textPtr)) {
381 if (fieldLimitLen != 0) {
384 memset(textPtr, 0, fieldLimitLen);
385 if (newLen > fieldLimitLen) {
386 newLen = fieldLimitLen;
390 memcpy(textPtr, newTextC, newLen);
391 textPtr[newLen] =
'\0';
397 BYTE *contentPart = this->
getContentAt(offset, contentSize);
398 if (!contentPart)
return 0;
400 if (!fIn.isReadable())
return 0;
402 size_t toLoad = fIn.size() < contentSize ? fIn.size() : contentSize;
403 BYTE *fBuf = (BYTE*)::calloc(toLoad, 1);
407 size_t loaded = fIn.read((
char*)fBuf, toLoad);
408 memset(contentPart, 0, contentSize);
409 memcpy(contentPart, fBuf, loaded);
410 free(fBuf); fBuf = NULL;
418 : parent(v_parent), offset(v_offset), size(v_size)
420 if (v_parent == NULL)
throw BufferException(
"Cannot make subBuffer for NULL buffer!");
INT_TYPE _getNumValue(void *ptr)
bool _setNumValue(void *ptr, INT_TYPE nVal)
const bufsize_t BUFSIZE_MAX
const offset_t INVALID_ADDR
virtual bufsize_t getContentSize()=0
bufsize_t getMaxSizeFromOffset(offset_t startOffset)
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 BYTE * getContent()=0
bool setTextValue(char *textPtr, std::string newText, size_t fieldLimitLen=0)
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)
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)
BufferView(AbstractByteBuffer *parent, offset_t offset, bufsize_t size)
AbstractByteBuffer * parent
virtual BYTE * getContent()
virtual bufsize_t getContentSize()
bool append(dbg_level lvl, const char *format,...)
bufsize_t roundupToUnit(bufsize_t size, bufsize_t unit)
size_t getAsciiLen(const char *ptr, size_t maxCount, bool acceptNotTerminated=false)
size_t getAsciiLenW(const WORD *ptr, size_t maxCount, bool acceptNotTerminated=false)