BearParser
Portable Executable parsing library (from PE-bear)
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 
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 
41 ByteBuffer* AbstractFileBuffer::read(QString &path, bufsize_t minBufSize)
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);
48  fIn.close();
49  return bufferedFile;
50 }
51 
52 ByteBuffer* AbstractFileBuffer::read(QFile &fIn, bufsize_t minBufSize) //throws exceptions
53 {
54  bufsize_t readableSize = getReadableSize(fIn);
55  bufsize_t allocSize = (readableSize < minBufSize) ? minBufSize : readableSize;
56  //printf("Alloc size: %lx\n", allocSize);
57 
58  ByteBuffer *bufferedFile = new ByteBuffer(allocSize); //throws exceptions
59  char *content = (char*) bufferedFile->getContent();
60  bufsize_t contentSize = bufferedFile->getContentSize();
61 
62  if (content == NULL) throw FileBufferException("Cannot allocate buffer");
63  //printf("Reading...%lx , BUFSIZE_MAX = %lx\n", allocSize, BUFSIZE_MAX);
64 
65  bufsize_t readSize = 0;
66  offset_t prevOffset = 0;
67  offset_t maxOffset = contentSize - 1;
68 
69  while (offset_t(fIn.pos()) < maxOffset) {
70 
71  bufsize_t maxSize = contentSize - readSize;
72  if (maxSize > FILEVIEW_MAXSIZE) maxSize = FILEVIEW_MAXSIZE;
73 
74  readSize += fIn.read(content + readSize, maxSize);
75  if (prevOffset == fIn.pos()) break; //cannot read more!
76  prevOffset = fIn.pos();
77  }
79  "Read size: %lX",
80  static_cast<unsigned long>(readSize)
81  );
82  return bufferedFile;
83 }
84 
86 {
87  qint64 fileSize = fIn.size();
88  bufsize_t size = static_cast<bufsize_t> (fileSize);
89 
90  if (size > FILE_MAXSIZE) {
91  size = FILE_MAXSIZE;
92  }
93  return size;
94 }
95 
97 {
98  if (path.length() == 0) return 0;
99  QFile fIn(path);
100  if (fIn.open(QIODevice::ReadOnly) == false) return 0;
101  bufsize_t size = getReadableSize(fIn);
102  fIn.close();
103  return size;
104 }
105 
106 bufsize_t AbstractFileBuffer::dump(QString path, AbstractByteBuffer &bBuf, bool allowExceptions)
107 {
108  BYTE* buf = bBuf.getContent();
109  bufsize_t bufSize = bBuf.getContentSize();
110  if (buf == NULL) {
111  if (allowExceptions) throw FileBufferException("Buffer is empty");
112  return 0;
113  }
114  QFile fOut(path);
115  if (fOut.open(QFile::WriteOnly) == false) {
116  if (allowExceptions) throw FileBufferException("Cannot open the file: " + path + " for writing");
117  return 0;
118  }
119  bufsize_t wrote = static_cast<bufsize_t> (fOut.write((char*)buf, bufSize));
120  fOut.close();
121  return wrote;
122 }
123 
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)
Definition: FileBuffer.cpp:85
static bufsize_t dump(QString fileName, AbstractByteBuffer &buf, bool allowExceptions=false)
Definition: FileBuffer.cpp:106
static ByteBuffer * read(QString &file, bufsize_t minBufSize)
Definition: FileBuffer.cpp:41
virtual bufsize_t getContentSize()
Definition: ByteBuffer.h:15
virtual BYTE * getContent()
Definition: ByteBuffer.h:16
virtual ~FileView()
Definition: FileBuffer.cpp:25
bufsize_t mappedSize
Definition: FileBuffer.h:48
bufsize_t getMappableSize(QFile &fIn)
Definition: FileBuffer.cpp:31
QFile fIn
Definition: FileBuffer.h:49
FileView(QString &fileName, bufsize_t maxSize=FILE_MAXSIZE)
Definition: FileBuffer.cpp:4
BYTE * mappedContent
Definition: FileBuffer.h:47
bool append(dbg_level lvl, const char *format,...)
Definition: Util.cpp:8
@ D_INFO
Definition: Util.h:26