OLD | NEW |
1 // Copyright (c) 2006, Google Inc. | 1 // Copyright (c) 2006, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 #include "common/mac/byteswap.h" | 46 #include "common/mac/byteswap.h" |
47 #include "common/mac/macho_walker.h" | 47 #include "common/mac/macho_walker.h" |
48 #include "common/mac/macho_utilities.h" | 48 #include "common/mac/macho_utilities.h" |
49 | 49 |
50 namespace MacFileUtilities { | 50 namespace MacFileUtilities { |
51 | 51 |
52 MachoWalker::MachoWalker(const char *path, LoadCommandCallback callback, | 52 MachoWalker::MachoWalker(const char *path, LoadCommandCallback callback, |
53 void *context) | 53 void *context) |
54 : file_(0), | 54 : file_(0), |
| 55 memory_(NULL), |
| 56 memory_size_(0), |
55 callback_(callback), | 57 callback_(callback), |
56 callback_context_(context), | 58 callback_context_(context), |
57 current_header_(NULL), | 59 current_header_(NULL), |
58 current_header_size_(0), | 60 current_header_size_(0), |
59 current_header_offset_(0) { | 61 current_header_offset_(0) { |
60 file_ = open(path, O_RDONLY); | 62 file_ = open(path, O_RDONLY); |
61 } | 63 } |
62 | 64 |
| 65 MachoWalker::MachoWalker(void *memory, size_t size, |
| 66 LoadCommandCallback callback, void *context) |
| 67 : file_(0), |
| 68 memory_(memory), |
| 69 memory_size_(size), |
| 70 callback_(callback), |
| 71 callback_context_(context), |
| 72 current_header_(NULL), |
| 73 current_header_size_(0), |
| 74 current_header_offset_(0) { |
| 75 } |
| 76 |
63 MachoWalker::~MachoWalker() { | 77 MachoWalker::~MachoWalker() { |
64 if (file_ != -1) | 78 if (file_ != -1) |
65 close(file_); | 79 close(file_); |
66 } | 80 } |
67 | 81 |
68 int MachoWalker::ValidateCPUType(int cpu_type) { | 82 int MachoWalker::ValidateCPUType(int cpu_type) { |
69 // If the user didn't specify, use the local architecture. | 83 // If the user didn't specify, use the local architecture. |
70 if (cpu_type == 0) { | 84 if (cpu_type == 0) { |
71 const NXArchInfo *arch = NXGetLocalArchInfo(); | 85 const NXArchInfo *arch = NXGetLocalArchInfo(); |
72 assert(arch); | 86 assert(arch); |
(...skipping 10 matching lines...) Expand all Loading... |
83 if (cpu_type & CPU_ARCH_ABI64) | 97 if (cpu_type & CPU_ARCH_ABI64) |
84 return WalkHeader64AtOffset(offset); | 98 return WalkHeader64AtOffset(offset); |
85 | 99 |
86 return WalkHeaderAtOffset(offset); | 100 return WalkHeaderAtOffset(offset); |
87 } | 101 } |
88 | 102 |
89 return false; | 103 return false; |
90 } | 104 } |
91 | 105 |
92 bool MachoWalker::ReadBytes(void *buffer, size_t size, off_t offset) { | 106 bool MachoWalker::ReadBytes(void *buffer, size_t size, off_t offset) { |
93 return pread(file_, buffer, size, offset) == (ssize_t)size; | 107 if (memory_) { |
| 108 bool result = true; |
| 109 if (offset + size > memory_size_) { |
| 110 size = memory_size_ - offset; |
| 111 result = false; |
| 112 } |
| 113 if (size < 0) |
| 114 return false; |
| 115 memcpy(buffer, static_cast<char *>(memory_) + offset, size); |
| 116 return result; |
| 117 } else { |
| 118 return pread(file_, buffer, size, offset) == (ssize_t)size; |
| 119 } |
94 } | 120 } |
95 | 121 |
96 bool MachoWalker::CurrentHeader(struct mach_header_64 *header, off_t *offset) { | 122 bool MachoWalker::CurrentHeader(struct mach_header_64 *header, off_t *offset) { |
97 if (current_header_) { | 123 if (current_header_) { |
98 memcpy(header, current_header_, sizeof(mach_header_64)); | 124 memcpy(header, current_header_, sizeof(mach_header_64)); |
99 *offset = current_header_offset_; | 125 *offset = current_header_offset_; |
100 return true; | 126 return true; |
101 } | 127 } |
102 | 128 |
103 return false; | 129 return false; |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 if (callback_ && !callback_(this, &cmd, offset, swap, callback_context_)) | 256 if (callback_ && !callback_(this, &cmd, offset, swap, callback_context_)) |
231 break; | 257 break; |
232 | 258 |
233 offset += cmd.cmdsize; | 259 offset += cmd.cmdsize; |
234 } | 260 } |
235 | 261 |
236 return true; | 262 return true; |
237 } | 263 } |
238 | 264 |
239 } // namespace MacFileUtilities | 265 } // namespace MacFileUtilities |
OLD | NEW |