1
// Copyright (c) 2006, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
8
// * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above
11
// copyright notice, this list of conditions and the following disclaimer
12
// in the documentation and/or other materials provided with the
14
// * Neither the name of Google Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from
16
// this software without specific prior written permission.
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
// macho_id.h: Functions to gather identifying information from a macho file
32
// Author: Dan Waylonis
34
#ifndef COMMON_MAC_MACHO_ID_H__
35
#define COMMON_MAC_MACHO_ID_H__
38
#include <mach-o/loader.h>
40
#include "common/mac/macho_walker.h"
41
#include "common/md5.h"
43
namespace MacFileUtilities {
47
MachoID(const char *path);
48
MachoID(const char *path, void *memory, size_t size);
51
// For the given |cpu_type|, return a UUID from the LC_UUID command.
52
// Return false if there isn't a LC_UUID command.
53
bool UUIDCommand(int cpu_type, unsigned char identifier[16]);
55
// For the given |cpu_type|, return a UUID from the LC_ID_DYLIB command.
56
// Return false if there isn't a LC_ID_DYLIB command.
57
bool IDCommand(int cpu_type, unsigned char identifier[16]);
59
// For the given |cpu_type|, return the Adler32 CRC for the mach-o data
61
// Return 0 on error (e.g., if the file is not a mach-o file)
62
uint32_t Adler32(int cpu_type);
64
// For the given |cpu_type|, return the MD5 for the mach-o data segment(s).
65
// Return true on success, false otherwise
66
bool MD5(int cpu_type, unsigned char identifier[16]);
69
// Signature of class member function to be called with data read from file
70
typedef void (MachoID::*UpdateFunction)(unsigned char *bytes, size_t size);
72
// Update the CRC value by examining |size| |bytes| and applying the algorithm
74
void UpdateCRC(unsigned char *bytes, size_t size);
76
// Update the MD5 value by examining |size| |bytes| and applying the algorithm
78
void UpdateMD5(unsigned char *bytes, size_t size);
80
// Bottleneck for update routines
81
void Update(MachoWalker *walker, off_t offset, size_t size);
83
// Factory for the MachoWalker
84
bool WalkHeader(int cpu_type, MachoWalker::LoadCommandCallback callback,
87
// The callback from the MachoWalker for CRC and MD5
88
static bool WalkerCB(MachoWalker *walker, load_command *cmd, off_t offset,
89
bool swap, void *context);
91
// The callback from the MachoWalker for LC_UUID
92
static bool UUIDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset,
93
bool swap, void *context);
95
// The callback from the MachoWalker for LC_ID_DYLIB
96
static bool IDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset,
97
bool swap, void *context);
100
char path_[PATH_MAX];
102
// Memory region to read from
105
// Size of the memory region
108
// The current crc value
112
google_breakpad::MD5Context md5_context_;
114
// The current update to call from the Update callback
115
UpdateFunction update_function_;
118
} // namespace MacFileUtilities
120
#endif // COMMON_MAC_MACHO_ID_H__