aboutsummaryrefslogtreecommitdiff
path: root/src/common/mac/macho_walker.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/mac/macho_walker.cc')
-rw-r--r--src/common/mac/macho_walker.cc28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/common/mac/macho_walker.cc b/src/common/mac/macho_walker.cc
index 3e8fe211..00fde03e 100644
--- a/src/common/mac/macho_walker.cc
+++ b/src/common/mac/macho_walker.cc
@@ -52,6 +52,8 @@ namespace MacFileUtilities {
MachoWalker::MachoWalker(const char *path, LoadCommandCallback callback,
void *context)
: file_(0),
+ memory_(NULL),
+ memory_size_(0),
callback_(callback),
callback_context_(context),
current_header_(NULL),
@@ -60,6 +62,18 @@ MachoWalker::MachoWalker(const char *path, LoadCommandCallback callback,
file_ = open(path, O_RDONLY);
}
+MachoWalker::MachoWalker(void *memory, size_t size,
+ LoadCommandCallback callback, void *context)
+ : file_(0),
+ memory_(memory),
+ memory_size_(size),
+ callback_(callback),
+ callback_context_(context),
+ current_header_(NULL),
+ current_header_size_(0),
+ current_header_offset_(0) {
+}
+
MachoWalker::~MachoWalker() {
if (file_ != -1)
close(file_);
@@ -90,7 +104,19 @@ bool MachoWalker::WalkHeader(int cpu_type) {
}
bool MachoWalker::ReadBytes(void *buffer, size_t size, off_t offset) {
- return pread(file_, buffer, size, offset) == (ssize_t)size;
+ if (memory_) {
+ bool result = true;
+ if (offset + size > memory_size_) {
+ size = memory_size_ - offset;
+ result = false;
+ }
+ if (size < 0)
+ return false;
+ memcpy(buffer, static_cast<char *>(memory_) + offset, size);
+ return result;
+ } else {
+ return pread(file_, buffer, size, offset) == (ssize_t)size;
+ }
}
bool MachoWalker::CurrentHeader(struct mach_header_64 *header, off_t *offset) {