mirror of
https://github.com/QuasarApp/pe-parse.git
synced 2025-04-27 12:54:31 +00:00
now uses mmap instead of malloc
This commit is contained in:
parent
a24fa560c0
commit
10bdaac799
@ -26,9 +26,18 @@ THE SOFTWARE.
|
||||
#include <string.h>
|
||||
#include "parse.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
using namespace boost;
|
||||
using namespace std;
|
||||
|
||||
struct buffer_detail {
|
||||
int fd;
|
||||
};
|
||||
|
||||
bool readByte(bounded_buffer *b, ::uint32_t offset, ::uint8_t &out) {
|
||||
if(offset >= b->bufLen) {
|
||||
return false;
|
||||
@ -66,6 +75,12 @@ bool readDword(bounded_buffer *b, ::uint32_t offset, ::uint32_t &out) {
|
||||
|
||||
|
||||
bounded_buffer *readFileToFileBuffer(const char *filePath) {
|
||||
int fd = open(filePath, O_RDONLY);
|
||||
|
||||
if(fd == -1) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//make a buffer object
|
||||
bounded_buffer *p = new bounded_buffer();
|
||||
|
||||
@ -73,6 +88,38 @@ bounded_buffer *readFileToFileBuffer(const char *filePath) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(p, 0, sizeof(bounded_buffer));
|
||||
buffer_detail *d = new buffer_detail();
|
||||
|
||||
if(d == NULL) {
|
||||
delete p;
|
||||
return NULL;
|
||||
}
|
||||
memset(d, 0, sizeof(buffer_detail));
|
||||
p->detail = d;
|
||||
|
||||
p->detail->fd = fd;
|
||||
|
||||
struct stat s = {0};
|
||||
|
||||
/* should work since fd is open */
|
||||
fstat(fd, &s);
|
||||
|
||||
void *maddr = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
||||
|
||||
if(maddr == MAP_FAILED) {
|
||||
perror("bla\n");
|
||||
close(fd);
|
||||
delete d;
|
||||
delete p;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p->buf = (::uint8_t *)maddr;
|
||||
p->bufLen = s.st_size;
|
||||
p->copy = false;
|
||||
|
||||
#ifdef BUF_RAW
|
||||
//open the file in binary mode
|
||||
ifstream inFile(filePath, ios::in|ios::binary|ios::ate);
|
||||
|
||||
@ -103,6 +150,8 @@ bounded_buffer *readFileToFileBuffer(const char *filePath) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
@ -133,7 +182,9 @@ void deleteBuffer(bounded_buffer *b) {
|
||||
}
|
||||
|
||||
if(b->copy == false) {
|
||||
#ifdef BUF_RAW
|
||||
free(b->buf);
|
||||
#endif
|
||||
}
|
||||
|
||||
delete b;
|
||||
|
@ -32,10 +32,13 @@ THE SOFTWARE.
|
||||
typedef boost::uint32_t RVA;
|
||||
typedef boost::uint32_t VA;
|
||||
|
||||
struct buffer_detail;
|
||||
|
||||
typedef struct _bounded_buffer {
|
||||
boost::uint8_t *buf;
|
||||
boost::uint32_t bufLen;
|
||||
bool copy;
|
||||
buffer_detail *detail;
|
||||
} bounded_buffer;
|
||||
|
||||
bool readByte(bounded_buffer *b, boost::uint32_t offset, boost::uint8_t &out);
|
||||
|
Loading…
x
Reference in New Issue
Block a user