/thirdparty/breakpad/common/mac/macho_id.cc

http://github.com/tomahawk-player/tomahawk · C++ · 366 lines · 237 code · 66 blank · 63 comment · 56 complexity · 963d551893e62eeaf8910c33cd5d2240 MD5 · raw file

  1. // Copyright (c) 2006, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  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
  13. // distribution.
  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.
  17. //
  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.
  29. // macho_id.cc: Functions to gather identifying information from a macho file
  30. //
  31. // See macho_id.h for documentation
  32. //
  33. // Author: Dan Waylonis
  34. extern "C" { // necessary for Leopard
  35. #include <fcntl.h>
  36. #include <mach-o/loader.h>
  37. #include <mach-o/swap.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <sys/time.h>
  42. #include <sys/types.h>
  43. #include <unistd.h>
  44. }
  45. #include "common/mac/macho_id.h"
  46. #include "common/mac/macho_walker.h"
  47. #include "common/mac/macho_utilities.h"
  48. namespace MacFileUtilities {
  49. using google_breakpad::MD5Init;
  50. using google_breakpad::MD5Update;
  51. using google_breakpad::MD5Final;
  52. MachoID::MachoID(const char *path)
  53. : memory_(0),
  54. memory_size_(0),
  55. crc_(0),
  56. md5_context_(),
  57. update_function_(NULL) {
  58. strlcpy(path_, path, sizeof(path_));
  59. }
  60. MachoID::MachoID(const char *path, void *memory, size_t size)
  61. : memory_(memory),
  62. memory_size_(size),
  63. crc_(0),
  64. md5_context_(),
  65. update_function_(NULL) {
  66. strlcpy(path_, path, sizeof(path_));
  67. }
  68. MachoID::~MachoID() {
  69. }
  70. // The CRC info is from http://en.wikipedia.org/wiki/Adler-32
  71. // With optimizations from http://www.zlib.net/
  72. // The largest prime smaller than 65536
  73. #define MOD_ADLER 65521
  74. // MAX_BLOCK is the largest n such that 255n(n+1)/2 + (n+1)(MAX_BLOCK-1) <= 2^32-1
  75. #define MAX_BLOCK 5552
  76. void MachoID::UpdateCRC(unsigned char *bytes, size_t size) {
  77. // Unrolled loops for summing
  78. #define DO1(buf,i) {sum1 += (buf)[i]; sum2 += sum1;}
  79. #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
  80. #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
  81. #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
  82. #define DO16(buf) DO8(buf,0); DO8(buf,8);
  83. // Split up the crc
  84. uint32_t sum1 = crc_ & 0xFFFF;
  85. uint32_t sum2 = (crc_ >> 16) & 0xFFFF;
  86. // Do large blocks
  87. while (size >= MAX_BLOCK) {
  88. size -= MAX_BLOCK;
  89. int block_count = MAX_BLOCK / 16;
  90. do {
  91. DO16(bytes);
  92. bytes += 16;
  93. } while (--block_count);
  94. sum1 %= MOD_ADLER;
  95. sum2 %= MOD_ADLER;
  96. }
  97. // Do remaining bytes
  98. if (size) {
  99. while (size >= 16) {
  100. size -= 16;
  101. DO16(bytes);
  102. bytes += 16;
  103. }
  104. while (size--) {
  105. sum1 += *bytes++;
  106. sum2 += sum1;
  107. }
  108. sum1 %= MOD_ADLER;
  109. sum2 %= MOD_ADLER;
  110. crc_ = (sum2 << 16) | sum1;
  111. }
  112. }
  113. void MachoID::UpdateMD5(unsigned char *bytes, size_t size) {
  114. MD5Update(&md5_context_, bytes, size);
  115. }
  116. void MachoID::Update(MachoWalker *walker, off_t offset, size_t size) {
  117. if (!update_function_ || !size)
  118. return;
  119. // Read up to 4k bytes at a time
  120. unsigned char buffer[4096];
  121. size_t buffer_size;
  122. off_t file_offset = offset;
  123. while (size > 0) {
  124. if (size > sizeof(buffer)) {
  125. buffer_size = sizeof(buffer);
  126. size -= buffer_size;
  127. } else {
  128. buffer_size = size;
  129. size = 0;
  130. }
  131. if (!walker->ReadBytes(buffer, buffer_size, file_offset))
  132. return;
  133. (this->*update_function_)(buffer, buffer_size);
  134. file_offset += buffer_size;
  135. }
  136. }
  137. bool MachoID::UUIDCommand(int cpu_type, unsigned char bytes[16]) {
  138. struct breakpad_uuid_command uuid_cmd;
  139. uuid_cmd.cmd = 0;
  140. if (!WalkHeader(cpu_type, UUIDWalkerCB, &uuid_cmd))
  141. return false;
  142. // If we found the command, we'll have initialized the uuid_command
  143. // structure
  144. if (uuid_cmd.cmd == LC_UUID) {
  145. memcpy(bytes, uuid_cmd.uuid, sizeof(uuid_cmd.uuid));
  146. return true;
  147. }
  148. return false;
  149. }
  150. bool MachoID::IDCommand(int cpu_type, unsigned char identifier[16]) {
  151. struct dylib_command dylib_cmd;
  152. dylib_cmd.cmd = 0;
  153. if (!WalkHeader(cpu_type, IDWalkerCB, &dylib_cmd))
  154. return false;
  155. // If we found the command, we'll have initialized the dylib_command
  156. // structure
  157. if (dylib_cmd.cmd == LC_ID_DYLIB) {
  158. // Take the hashed filename, version, and compatability version bytes
  159. // to form the first 12 bytes, pad the rest with zeros
  160. // create a crude hash of the filename to generate the first 4 bytes
  161. identifier[0] = 0;
  162. identifier[1] = 0;
  163. identifier[2] = 0;
  164. identifier[3] = 0;
  165. for (int j = 0, i = (int)strlen(path_)-1; i>=0 && path_[i]!='/'; ++j, --i) {
  166. identifier[j%4] += path_[i];
  167. }
  168. identifier[4] = (dylib_cmd.dylib.current_version >> 24) & 0xFF;
  169. identifier[5] = (dylib_cmd.dylib.current_version >> 16) & 0xFF;
  170. identifier[6] = (dylib_cmd.dylib.current_version >> 8) & 0xFF;
  171. identifier[7] = dylib_cmd.dylib.current_version & 0xFF;
  172. identifier[8] = (dylib_cmd.dylib.compatibility_version >> 24) & 0xFF;
  173. identifier[9] = (dylib_cmd.dylib.compatibility_version >> 16) & 0xFF;
  174. identifier[10] = (dylib_cmd.dylib.compatibility_version >> 8) & 0xFF;
  175. identifier[11] = dylib_cmd.dylib.compatibility_version & 0xFF;
  176. identifier[12] = (cpu_type >> 24) & 0xFF;
  177. identifier[13] = (cpu_type >> 16) & 0xFF;
  178. identifier[14] = (cpu_type >> 8) & 0xFF;
  179. identifier[15] = cpu_type & 0xFF;
  180. return true;
  181. }
  182. return false;
  183. }
  184. uint32_t MachoID::Adler32(int cpu_type) {
  185. update_function_ = &MachoID::UpdateCRC;
  186. crc_ = 0;
  187. if (!WalkHeader(cpu_type, WalkerCB, this))
  188. return 0;
  189. return crc_;
  190. }
  191. bool MachoID::MD5(int cpu_type, unsigned char identifier[16]) {
  192. update_function_ = &MachoID::UpdateMD5;
  193. MD5Init(&md5_context_);
  194. if (!WalkHeader(cpu_type, WalkerCB, this))
  195. return false;
  196. MD5Final(identifier, &md5_context_);
  197. return true;
  198. }
  199. bool MachoID::WalkHeader(int cpu_type,
  200. MachoWalker::LoadCommandCallback callback,
  201. void *context) {
  202. if (memory_) {
  203. MachoWalker walker(memory_, memory_size_, callback, context);
  204. return walker.WalkHeader(cpu_type);
  205. } else {
  206. MachoWalker walker(path_, callback, context);
  207. return walker.WalkHeader(cpu_type);
  208. }
  209. }
  210. // static
  211. bool MachoID::WalkerCB(MachoWalker *walker, load_command *cmd, off_t offset,
  212. bool swap, void *context) {
  213. MachoID *macho_id = (MachoID *)context;
  214. if (cmd->cmd == LC_SEGMENT) {
  215. struct segment_command seg;
  216. if (!walker->ReadBytes(&seg, sizeof(seg), offset))
  217. return false;
  218. if (swap)
  219. swap_segment_command(&seg, NXHostByteOrder());
  220. struct mach_header_64 header;
  221. off_t header_offset;
  222. if (!walker->CurrentHeader(&header, &header_offset))
  223. return false;
  224. // Process segments that have sections:
  225. // (e.g., __TEXT, __DATA, __IMPORT, __OBJC)
  226. offset += sizeof(struct segment_command);
  227. struct section sec;
  228. for (unsigned long i = 0; i < seg.nsects; ++i) {
  229. if (!walker->ReadBytes(&sec, sizeof(sec), offset))
  230. return false;
  231. if (swap)
  232. swap_section(&sec, 1, NXHostByteOrder());
  233. // sections of type S_ZEROFILL are "virtual" and contain no data
  234. // in the file itself
  235. if ((sec.flags & SECTION_TYPE) != S_ZEROFILL && sec.offset != 0)
  236. macho_id->Update(walker, header_offset + sec.offset, sec.size);
  237. offset += sizeof(struct section);
  238. }
  239. } else if (cmd->cmd == LC_SEGMENT_64) {
  240. struct segment_command_64 seg64;
  241. if (!walker->ReadBytes(&seg64, sizeof(seg64), offset))
  242. return false;
  243. if (swap)
  244. breakpad_swap_segment_command_64(&seg64, NXHostByteOrder());
  245. struct mach_header_64 header;
  246. off_t header_offset;
  247. if (!walker->CurrentHeader(&header, &header_offset))
  248. return false;
  249. // Process segments that have sections:
  250. // (e.g., __TEXT, __DATA, __IMPORT, __OBJC)
  251. offset += sizeof(struct segment_command_64);
  252. struct section_64 sec64;
  253. for (unsigned long i = 0; i < seg64.nsects; ++i) {
  254. if (!walker->ReadBytes(&sec64, sizeof(sec64), offset))
  255. return false;
  256. if (swap)
  257. breakpad_swap_section_64(&sec64, 1, NXHostByteOrder());
  258. // sections of type S_ZEROFILL are "virtual" and contain no data
  259. // in the file itself
  260. if ((sec64.flags & SECTION_TYPE) != S_ZEROFILL && sec64.offset != 0)
  261. macho_id->Update(walker,
  262. header_offset + sec64.offset,
  263. (size_t)sec64.size);
  264. offset += sizeof(struct section_64);
  265. }
  266. }
  267. // Continue processing
  268. return true;
  269. }
  270. // static
  271. bool MachoID::UUIDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset,
  272. bool swap, void *context) {
  273. if (cmd->cmd == LC_UUID) {
  274. struct breakpad_uuid_command *uuid_cmd =
  275. (struct breakpad_uuid_command *)context;
  276. if (!walker->ReadBytes(uuid_cmd, sizeof(struct breakpad_uuid_command),
  277. offset))
  278. return false;
  279. if (swap)
  280. breakpad_swap_uuid_command(uuid_cmd, NXHostByteOrder());
  281. return false;
  282. }
  283. // Continue processing
  284. return true;
  285. }
  286. // static
  287. bool MachoID::IDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset,
  288. bool swap, void *context) {
  289. if (cmd->cmd == LC_ID_DYLIB) {
  290. struct dylib_command *dylib_cmd = (struct dylib_command *)context;
  291. if (!walker->ReadBytes(dylib_cmd, sizeof(struct dylib_command), offset))
  292. return false;
  293. if (swap)
  294. swap_dylib_command(dylib_cmd, NXHostByteOrder());
  295. return false;
  296. }
  297. // Continue processing
  298. return true;
  299. }
  300. } // namespace MacFileUtilities