From 68721bcd82c3cd719b8917803fa08efa0c830cbb Mon Sep 17 00:00:00 2001 From: Oleksandr Brezhniev Date: Thu, 11 Jan 2024 20:22:03 +0000 Subject: [PATCH 1/2] Use mmap buffer instead of regular buffer for BinFile::openExisting --- src/binfile_utils.cpp | 75 +++++++++++++++++++++++++++++++++++++++++-- src/binfile_utils.hpp | 4 +++ 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/src/binfile_utils.cpp b/src/binfile_utils.cpp index de6d0d7..7b1f5d1 100644 --- a/src/binfile_utils.cpp +++ b/src/binfile_utils.cpp @@ -1,3 +1,7 @@ +#include +#include +#include +#include #include #include #include @@ -8,6 +12,64 @@ namespace BinFileUtils { +BinFile::BinFile(std::string fileName, std::string _type, uint32_t maxVersion) { + + is_fd = true; + struct stat sb; + + fd = open(fileName.c_str(), O_RDONLY); + if (fd == -1) + throw std::system_error(errno, std::generic_category(), "open"); + + + if (fstat(fd, &sb) == -1) /* To obtain file size */ + throw std::system_error(errno, std::generic_category(), "fstat"); + + size = sb.st_size; + + addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0); + if (addr == MAP_FAILED) { + close(fd); + throw std::system_error(errno, std::generic_category(), "mmap failed"); + } + + type.assign((const char *)addr, 4); + pos = 4; + + if (type != _type) { + munmap(addr, size); + close(fd); + throw new std::invalid_argument("Invalid file type. It should be " + _type + " and it is " + type + " filename: " + fileName); + } + + version = readU32LE(); + if (version > maxVersion) { + munmap(addr, size); + close(fd); + throw new std::invalid_argument("Invalid version. It should be <=" + std::to_string(maxVersion) + " and it is " + std::to_string(version)); + } + + u_int32_t nSections = readU32LE(); + + + for (u_int32_t i=0; i())); + } + + sections[sType].push_back(Section( (void *)((u_int64_t)addr + pos), sSize)); + + pos += sSize; + } + + pos = 0; + readingSection = NULL; +} + + BinFile::BinFile(const void *fileData, size_t fileSize, std::string _type, uint32_t maxVersion) { size = fileSize; @@ -49,7 +111,12 @@ BinFile::BinFile(const void *fileData, size_t fileSize, std::string _type, uint3 } BinFile::~BinFile() { - free(addr); + if (is_fd) { + munmap(addr, size); + close(fd); + } else { + free(addr); + } } void BinFile::startReadSection(u_int32_t sectionId, u_int32_t sectionPos) { @@ -126,9 +193,11 @@ void *BinFile::read(u_int64_t len) { std::unique_ptr openExisting(std::string filename, std::string type, uint32_t maxVersion) { - FileLoader fileLoader(filename); + // FileLoader fileLoader(filename); + // + // return std::unique_ptr(new BinFile(fileLoader.dataBuffer(), fileLoader.dataSize(), type, maxVersion)); - return std::unique_ptr(new BinFile(fileLoader.dataBuffer(), fileLoader.dataSize(), type, maxVersion)); + return std::unique_ptr(new BinFile(filename, type, maxVersion)); } } // Namespace diff --git a/src/binfile_utils.hpp b/src/binfile_utils.hpp index 44b74d3..87fa00b 100644 --- a/src/binfile_utils.hpp +++ b/src/binfile_utils.hpp @@ -9,6 +9,9 @@ namespace BinFileUtils { class BinFile { + bool is_fd; + int fd; + void *addr; u_int64_t size; u_int64_t pos; @@ -33,6 +36,7 @@ namespace BinFileUtils { public: BinFile(const void *fileData, size_t fileSize, std::string _type, uint32_t maxVersion); + BinFile(std::string fileName, std::string _type, uint32_t maxVersion); ~BinFile(); void *getSetcionData(u_int32_t sectionId, u_int32_t sectionPos = 0); From b1c2e9104d3211c6c022f1b1a549fdb908764389 Mon Sep 17 00:00:00 2001 From: Oleksandr Brezhniev Date: Thu, 11 Jan 2024 20:47:21 +0000 Subject: [PATCH 2/2] Fix build on Mac --- src/binfile_utils.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/binfile_utils.cpp b/src/binfile_utils.cpp index 7b1f5d1..1044d4a 100644 --- a/src/binfile_utils.cpp +++ b/src/binfile_utils.cpp @@ -27,11 +27,12 @@ BinFile::BinFile(std::string fileName, std::string _type, uint32_t maxVersion) { size = sb.st_size; - addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0); + addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (addr == MAP_FAILED) { close(fd); throw std::system_error(errno, std::generic_category(), "mmap failed"); } + madvise(addr, size, MADV_SEQUENTIAL); type.assign((const char *)addr, 4); pos = 4;