diff options
Diffstat (limited to 'src/common/mac/macho_id.cc')
-rw-r--r-- | src/common/mac/macho_id.cc | 312 |
1 files changed, 312 insertions, 0 deletions
diff --git a/src/common/mac/macho_id.cc b/src/common/mac/macho_id.cc new file mode 100644 index 00000000..f152d275 --- /dev/null +++ b/src/common/mac/macho_id.cc @@ -0,0 +1,312 @@ +// Copyright (c) 2006, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// macho_id.cc: Functions to gather identifying information from a macho file +// +// See macho_id.h for documentation +// +// Author: Dan Waylonis + +#include <fcntl.h> +#include <mach-o/loader.h> +#include <mach-o/swap.h> +#include <openssl/md5.h> +#include <openssl/sha.h> +#include <stdio.h> +#include <string.h> +#include <sys/time.h> +#include <sys/types.h> +#include <unistd.h> + +#include "common/mac/macho_id.h" +#include "common/mac/macho_walker.h" + +namespace MacFileUtilities { + +MachoID::MachoID(const char *path) { + strlcpy(path_, path, sizeof(path_)); + file_ = open(path, O_RDONLY); +} + +MachoID::~MachoID() { + if (file_ != -1) + close(file_); +} + +// The CRC info is from http://en.wikipedia.org/wiki/Adler-32 +// With optimizations from http://www.zlib.net/ + +// The largest prime smaller than 65536 +#define MOD_ADLER 65521 +// MAX_BLOCK is the largest n such that 255n(n+1)/2 + (n+1)(MAX_BLOCK-1) <= 2^32-1 +#define MAX_BLOCK 5552 + +void MachoID::UpdateCRC(unsigned char *bytes, size_t size) { +// Unrolled loops for summing +#define DO1(buf,i) {sum1 += (buf)[i]; sum2 += sum1;} +#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); +#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); +#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); +#define DO16(buf) DO8(buf,0); DO8(buf,8); + // Split up the crc + uint32_t sum1 = crc_ & 0xFFFF; + uint32_t sum2 = (crc_ >> 16) & 0xFFFF; + + // Do large blocks + while (size >= MAX_BLOCK) { + size -= MAX_BLOCK; + int block_count = MAX_BLOCK / 16; + do { + DO16(bytes); + bytes += 16; + } while (--block_count); + sum1 %= MOD_ADLER; + sum2 %= MOD_ADLER; + } + + // Do remaining bytes + if (size) { + while (size >= 16) { + size -= 16; + DO16(bytes); + bytes += 16; + } + while (size--) { + sum1 += *bytes++; + sum2 += sum1; + } + sum1 %= MOD_ADLER; + sum2 %= MOD_ADLER; + crc_ = (sum2 << 16) | sum1; + } +} + +void MachoID::UpdateMD5(unsigned char *bytes, size_t size) { + MD5_Update(&md5_context_, bytes, size); +} + +void MachoID::UpdateSHA1(unsigned char *bytes, size_t size) { + SHA_Update(&sha1_context_, bytes, size); +} + +void MachoID::Update(unsigned char *bytes, size_t size) { + if (update_function_) + (this->*update_function_)(bytes, size); +} + +bool MachoID::UUIDCommand(int cpu_type, unsigned char bytes[16]) { + struct uuid_command uuid_cmd; + MachoWalker walker(path_, UUIDWalkerCB, &uuid_cmd); + + uuid_cmd.cmd = 0; + if (!walker.WalkHeader(cpu_type)) + return false; + + // If we found the command, we'll have initialized the uuid_command + // structure + if (uuid_cmd.cmd == LC_UUID) { + memcpy(bytes, uuid_cmd.uuid, sizeof(uuid_cmd.uuid)); + return true; + } + + return false; +} + +bool MachoID::IDCommand(int cpu_type, unsigned char identifier[16]) { + struct dylib_command dylib_cmd; + MachoWalker walker(path_, IDWalkerCB, &dylib_cmd); + + dylib_cmd.cmd = 0; + if (!walker.WalkHeader(cpu_type)) + return false; + + // If we found the command, we'll have initialized the dylib_command + // structure + if (dylib_cmd.cmd == LC_ID_DYLIB) { + // Take the timestamp, version, and compatability version bytes to form + // the first 12 bytes, pad the rest with zeros + identifier[0] = (dylib_cmd.dylib.timestamp >> 24) & 0xFF; + identifier[1] = (dylib_cmd.dylib.timestamp >> 16) & 0xFF; + identifier[2] = (dylib_cmd.dylib.timestamp >> 8) & 0xFF; + identifier[3] = dylib_cmd.dylib.timestamp & 0xFF; + identifier[4] = (dylib_cmd.dylib.current_version >> 24) & 0xFF; + identifier[5] = (dylib_cmd.dylib.current_version >> 16) & 0xFF; + identifier[6] = (dylib_cmd.dylib.current_version >> 8) & 0xFF; + identifier[7] = dylib_cmd.dylib.current_version & 0xFF; + identifier[8] = (dylib_cmd.dylib.compatibility_version >> 24) & 0xFF; + identifier[9] = (dylib_cmd.dylib.compatibility_version >> 16) & 0xFF; + identifier[10] = (dylib_cmd.dylib.compatibility_version >> 8) & 0xFF; + identifier[11] = dylib_cmd.dylib.compatibility_version & 0xFF; + identifier[12] = identifier[13] = identifier[14] = identifier[15] = 0; + + return true; + } + + return false; +} + +uint32_t MachoID::Adler32(int cpu_type) { + MachoWalker walker(path_, WalkerCB, this); + update_function_ = &MachoID::UpdateCRC; + crc_ = 0; + + if (!walker.WalkHeader(cpu_type)) + return 0; + + return crc_; +} + +bool MachoID::MD5(int cpu_type, unsigned char identifier[16]) { + MachoWalker walker(path_, WalkerCB, this); + update_function_ = &MachoID::UpdateMD5; + + if (MD5_Init(&md5_context_)) { + if (!walker.WalkHeader(cpu_type)) + return false; + + MD5_Final(identifier, &md5_context_); + return true; + } + + return false; +} + +bool MachoID::SHA1(int cpu_type, unsigned char identifier[16]) { + MachoWalker walker(path_, WalkerCB, this); + update_function_ = &MachoID::UpdateSHA1; + + if (SHA_Init(&sha1_context_)) { + if (!walker.WalkHeader(cpu_type)) + return false; + + SHA_Final(identifier, &sha1_context_); + return true; + } + + return false; +} + +// static +bool MachoID::WalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, + bool swap, void *context) { + MachoID *macho_id = (MachoID *)context; + off_t seg_offset = 0; + size_t seg_size = 0; + + if (cmd->cmd == LC_SEGMENT) { + struct segment_command seg; + + if (!walker->ReadBytes(&seg, sizeof(seg), offset)) + return false; + + if (swap) + swap_segment_command(&seg, NXHostByteOrder()); + + seg_offset = seg.fileoff; + seg_size = seg.filesize; + } else if (cmd->cmd == LC_SEGMENT_64) { + struct segment_command_64 seg64; + + if (!walker->ReadBytes(&seg64, sizeof(seg64), offset)) + return false; + + if (swap) + swap_segment_command_64(&seg64, NXHostByteOrder()); + + seg_offset = seg64.fileoff; + seg_size = seg64.filesize; + } + + // If this was a non-zero segment, read the bytes, update the hash + if (seg_size) { + // Read 4k x 2 bytes at a time. This is faster than just 4k bytes, but + // doesn't seem to be an unreasonable size for the stack. + unsigned char buffer[4096 * 2]; + size_t buffer_size; + off_t file_offset = seg_offset; + while (seg_size > 0) { + if (seg_size > sizeof(buffer)) { + buffer_size = sizeof(buffer); + seg_size -= buffer_size; + } else { + buffer_size = seg_size; + seg_size = 0; + } + + if (!walker->ReadBytes(buffer, buffer_size, file_offset)) + return false; + + macho_id->Update(buffer, buffer_size); + file_offset += buffer_size; + } + } + + // Continue processing + return true; +} + +// static +bool MachoID::UUIDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, + bool swap, void *context) { + if (cmd->cmd == LC_UUID) { + struct uuid_command *uuid_cmd = (struct uuid_command *)context; + + if (!walker->ReadBytes(uuid_cmd, sizeof(struct uuid_command), offset)) + return false; + + if (swap) + swap_uuid_command(uuid_cmd, NXHostByteOrder()); + + return false; + } + + // Continue processing + return true; +} + +// static +bool MachoID::IDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset, + bool swap, void *context) { + if (cmd->cmd == LC_ID_DYLIB) { + struct dylib_command *dylib_cmd = (struct dylib_command *)context; + + if (!walker->ReadBytes(dylib_cmd, sizeof(struct dylib_command), offset)) + return false; + + if (swap) + swap_dylib_command(dylib_cmd, NXHostByteOrder()); + + return false; + } + + // Continue processing + return true; +} + +} // namespace MacFileUtilities |