BearParser
Portable Executable parsing library (from PE-bear)
Toggle main menu visibility
Loading...
Searching...
No Matches
parser
FileBuffer.cpp
Go to the documentation of this file.
1
#include "
FileBuffer.h
"
2
3
4
FileView::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
25
FileView::~FileView
()
26
{
27
fIn
.unmap((uchar*)this->
mappedContent
);
28
fIn
.close();
29
}
30
31
bufsize_t
FileView::getMappableSize
(QFile &
fIn
)
32
{
33
bufsize_t
size =
getReadableSize
(
fIn
);
34
if
(size >
FILEVIEW_MAXSIZE
) {
35
size =
FILEVIEW_MAXSIZE
;
36
}
37
return
size;
38
}
39
//----------------------------------------------------------------
40
41
ByteBuffer
*
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
52
ByteBuffer
*
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
}
88
Logger::append
(
Logger::D_INFO
,
89
"Read size: %lX"
,
90
static_cast<
unsigned
long
>
(readSize)
91
);
92
return
bufferedFile;
93
}
94
95
bufsize_t
AbstractFileBuffer::getReadableSize
(QFile &fIn)
96
{
97
qint64
fileSize
= fIn.size();
98
if
(!
fileSize
)
return
0;
99
if
(
fileSize
> qint64(
FILE_MAXSIZE
)) {
100
fileSize
= qint64(
FILE_MAXSIZE
);
101
}
102
return
static_cast<
bufsize_t
>
(
fileSize
);
103
}
104
105
bufsize_t
AbstractFileBuffer::getReadableSize
(
const
QString &path)
106
{
107
if
(!path.length())
return
0;
108
QFile fIn(path);
109
if
(!fIn.open(QIODevice::ReadOnly))
return
0;
110
const
bufsize_t
size =
getReadableSize
(fIn);
111
fIn.close();
112
return
size;
113
}
114
115
bufsize_t
AbstractFileBuffer::dump
(
const
QString &path,
AbstractByteBuffer
&bBuf,
bool
allowExceptions)
116
{
117
BYTE* buf = bBuf.
getContent
();
118
bufsize_t
bufSize = bBuf.
getContentSize
();
119
if
(buf == NULL) {
120
if
(allowExceptions)
throw
FileBufferException
(
"Buffer is empty"
);
121
return
0;
122
}
123
QFile fOut(path);
124
if
(fOut.open(QFile::WriteOnly) ==
false
) {
125
if
(allowExceptions)
throw
FileBufferException
(
"Cannot open the file: "
+ path +
" for writing"
);
126
return
0;
127
}
128
bufsize_t
wrote =
static_cast<
bufsize_t
>
(fOut.write((
char
*)buf, bufSize));
129
fOut.close();
130
return
wrote;
131
}
132
offset_t
uint64_t offset_t
Definition
AbstractByteBuffer.h:20
bufsize_t
size_t bufsize_t
Definition
AbstractByteBuffer.h:17
FileBuffer.h
FILEVIEW_MAXSIZE
const bufsize_t FILEVIEW_MAXSIZE
Definition
FileBuffer.h:7
FILE_MAXSIZE
const bufsize_t FILE_MAXSIZE
Definition
FileBuffer.h:6
AbstractByteBuffer
Definition
AbstractByteBuffer.h:36
AbstractByteBuffer::getContentSize
virtual bufsize_t getContentSize()=0
AbstractByteBuffer::getContent
virtual BYTE * getContent()=0
AbstractFileBuffer::getReadableSize
static bufsize_t getReadableSize(QFile &fIn)
Definition
FileBuffer.cpp:95
AbstractFileBuffer::dump
static bufsize_t dump(const QString &fileName, AbstractByteBuffer &buf, bool allowExceptions=false)
Definition
FileBuffer.cpp:115
AbstractFileBuffer::read
static ByteBuffer * read(QString &file, bufsize_t minBufSize, const bool allowTruncate)
Definition
FileBuffer.cpp:41
AbstractFileBuffer::AbstractFileBuffer
AbstractFileBuffer(QString v_fileName)
Definition
FileBuffer.h:28
AbstractFileBuffer::fileSize
qint64 fileSize
Definition
FileBuffer.h:31
BufferException
Definition
AbstractByteBuffer.h:26
ByteBuffer
Definition
ByteBuffer.h:7
ByteBuffer::getContent
virtual BYTE * getContent()
Definition
ByteBuffer.h:16
ByteBuffer::getContentSize
virtual bufsize_t getContentSize()
Definition
ByteBuffer.h:15
CustomException
Definition
CustomException.h:12
FileBufferException
Definition
FileBuffer.h:10
FileView::~FileView
virtual ~FileView()
Definition
FileBuffer.cpp:25
FileView::mappedSize
bufsize_t mappedSize
Definition
FileBuffer.h:50
FileView::fIn
QFile fIn
Definition
FileBuffer.h:51
FileView::getMappableSize
bufsize_t getMappableSize()
Definition
FileBuffer.h:45
FileView::FileView
FileView(QString &fileName, bufsize_t maxSize=FILE_MAXSIZE)
Definition
FileBuffer.cpp:4
FileView::mappedContent
BYTE * mappedContent
Definition
FileBuffer.h:49
Logger::append
bool append(dbg_level lvl, const char *format,...)
Definition
Util.cpp:8
Logger::D_INFO
@ D_INFO
Definition
Util.h:26
Generated by
1.17.0