BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
FileBuffer.cpp
Go to the documentation of this file.
1#include "FileBuffer.h"
2
3
4FileView::FileView(QString &path, bufsize_t maxSize)
5 : AbstractFileBuffer(path), fIn (path)
6{
7 if (fIn.open(QIODevice::ReadOnly) == false) {
8 throw FileBufferException("Cannot open the file: " + path);
9 }
10 this->fileSize = fIn.size();
11 if (fileSize == 0) {
12 std::cerr << fIn.errorString().toStdString() << std::endl;
13 throw FileBufferException("The file is empty");
14 }
15 bufsize_t readableSize = getMappableSize(fIn);
16 this->mappedSize = (readableSize > maxSize) ? maxSize : readableSize;
17
18 uchar *pData = fIn.map(0, this->mappedSize);
19 if (!pData) {
20 throw BufferException("Cannot map the file: " + path + " of size: 0x" + QString::number(this->mappedSize, 16));
21 }
22 this->mappedContent = (BYTE*) pData;
23}
24
26{
27 fIn.unmap((uchar*)this->mappedContent);
28 fIn.close();
29}
30
32{
34 if (size > FILEVIEW_MAXSIZE) {
35 size = FILEVIEW_MAXSIZE;
36 }
37 return size;
38}
39//----------------------------------------------------------------
40
41ByteBuffer* AbstractFileBuffer::read(QString &path, bufsize_t minBufSize, const bool allowTruncate)
42{
43 QFile fIn(path);
44 if (fIn.open(QIODevice::ReadOnly) == false) {
45 throw FileBufferException("Cannot open the file: " + path);
46 }
47 ByteBuffer *bufferedFile = read(fIn, minBufSize, allowTruncate);
48 fIn.close();
49 return bufferedFile;
50}
51
52ByteBuffer* AbstractFileBuffer::read(QFile &fIn, bufsize_t minBufSize, const bool allowTruncate) //throws exceptions
53{
54 bufsize_t readableSize = getReadableSize(fIn);
55 bufsize_t allocSize = (readableSize < minBufSize) ? minBufSize : readableSize;
56
57 ByteBuffer *bufferedFile = NULL;
58 do {
59 try {
60 bufferedFile = new ByteBuffer(allocSize); //throws exceptions
61 }
62 catch (CustomException)
63 {
64 if (!allowTruncate) throw;
65 allocSize /= 2;
66 }
67 } while(!bufferedFile && allocSize);
68
69 char *content = (char*) bufferedFile->getContent();
70 bufsize_t contentSize = bufferedFile->getContentSize();
71
72 if (!content || !contentSize) throw FileBufferException("Cannot allocate buffer");
73 //printf("Reading...%lx , BUFSIZE_MAX = %lx\n", allocSize, BUFSIZE_MAX);
74
75 bufsize_t readSize = 0;
76 offset_t prevOffset = 0;
77 offset_t maxOffset = contentSize - 1;
78
79 while (offset_t(fIn.pos()) < maxOffset) {
80
81 bufsize_t maxSize = contentSize - readSize;
82 if (maxSize > FILEVIEW_MAXSIZE) maxSize = FILEVIEW_MAXSIZE;
83
84 readSize += fIn.read(content + readSize, maxSize);
85 if (prevOffset == fIn.pos()) break; //cannot read more!
86 prevOffset = fIn.pos();
87 }
89 "Read size: %lX",
90 static_cast<unsigned long>(readSize)
91 );
92 return bufferedFile;
93}
94
96{
97 qint64 fileSize = fIn.size();
98 if (fileSize > qint64(FILE_MAXSIZE)) {
99 fileSize = qint64(FILE_MAXSIZE);
100 }
101 return static_cast<bufsize_t>(fileSize);
102}
103
105{
106 if (!path.length()) return 0;
107 QFile fIn(path);
108 if (!fIn.open(QIODevice::ReadOnly)) return 0;
109 const bufsize_t size = getReadableSize(fIn);
110 fIn.close();
111 return size;
112}
113
114bufsize_t AbstractFileBuffer::dump(const QString &path, AbstractByteBuffer &bBuf, bool allowExceptions)
115{
116 BYTE* buf = bBuf.getContent();
117 bufsize_t bufSize = bBuf.getContentSize();
118 if (buf == NULL) {
119 if (allowExceptions) throw FileBufferException("Buffer is empty");
120 return 0;
121 }
122 QFile fOut(path);
123 if (fOut.open(QFile::WriteOnly) == false) {
124 if (allowExceptions) throw FileBufferException("Cannot open the file: " + path + " for writing");
125 return 0;
126 }
127 bufsize_t wrote = static_cast<bufsize_t> (fOut.write((char*)buf, bufSize));
128 fOut.close();
129 return wrote;
130}
131
uint32_t bufsize_t
uint64_t offset_t
const bufsize_t FILEVIEW_MAXSIZE
Definition FileBuffer.h:7
const bufsize_t FILE_MAXSIZE
Definition FileBuffer.h:6
virtual bufsize_t getContentSize()=0
virtual BYTE * getContent()=0
static bufsize_t getReadableSize(QFile &fIn)
static bufsize_t dump(const QString &fileName, AbstractByteBuffer &buf, bool allowExceptions=false)
static ByteBuffer * read(QString &file, bufsize_t minBufSize, const bool allowTruncate)
virtual BYTE * getContent()
Definition ByteBuffer.h:16
virtual bufsize_t getContentSize()
Definition ByteBuffer.h:15
virtual ~FileView()
bufsize_t mappedSize
Definition FileBuffer.h:50
QFile fIn
Definition FileBuffer.h:51
bufsize_t getMappableSize()
Definition FileBuffer.h:45
FileView(QString &fileName, bufsize_t maxSize=FILE_MAXSIZE)
Definition FileBuffer.cpp:4
BYTE * mappedContent
Definition FileBuffer.h:49
bool append(dbg_level lvl, const char *format,...)
Definition Util.cpp:8
@ D_INFO
Definition Util.h:26