/thirdparty/breakpad/common/mac/macho_id.h

http://github.com/tomahawk-player/tomahawk · C Header · 120 lines · 38 code · 26 blank · 56 comment · 0 complexity · 278c23cb7f1188ba3dfd6587e5a0565b 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.h: Functions to gather identifying information from a macho file
  30. //
  31. // Author: Dan Waylonis
  32. #ifndef COMMON_MAC_MACHO_ID_H__
  33. #define COMMON_MAC_MACHO_ID_H__
  34. #include <limits.h>
  35. #include <mach-o/loader.h>
  36. #include "common/mac/macho_walker.h"
  37. #include "common/md5.h"
  38. namespace MacFileUtilities {
  39. class MachoID {
  40. public:
  41. MachoID(const char *path);
  42. MachoID(const char *path, void *memory, size_t size);
  43. ~MachoID();
  44. // For the given |cpu_type|, return a UUID from the LC_UUID command.
  45. // Return false if there isn't a LC_UUID command.
  46. bool UUIDCommand(int cpu_type, unsigned char identifier[16]);
  47. // For the given |cpu_type|, return a UUID from the LC_ID_DYLIB command.
  48. // Return false if there isn't a LC_ID_DYLIB command.
  49. bool IDCommand(int cpu_type, unsigned char identifier[16]);
  50. // For the given |cpu_type|, return the Adler32 CRC for the mach-o data
  51. // segment(s).
  52. // Return 0 on error (e.g., if the file is not a mach-o file)
  53. uint32_t Adler32(int cpu_type);
  54. // For the given |cpu_type|, return the MD5 for the mach-o data segment(s).
  55. // Return true on success, false otherwise
  56. bool MD5(int cpu_type, unsigned char identifier[16]);
  57. private:
  58. // Signature of class member function to be called with data read from file
  59. typedef void (MachoID::*UpdateFunction)(unsigned char *bytes, size_t size);
  60. // Update the CRC value by examining |size| |bytes| and applying the algorithm
  61. // to each byte.
  62. void UpdateCRC(unsigned char *bytes, size_t size);
  63. // Update the MD5 value by examining |size| |bytes| and applying the algorithm
  64. // to each byte.
  65. void UpdateMD5(unsigned char *bytes, size_t size);
  66. // Bottleneck for update routines
  67. void Update(MachoWalker *walker, off_t offset, size_t size);
  68. // Factory for the MachoWalker
  69. bool WalkHeader(int cpu_type, MachoWalker::LoadCommandCallback callback,
  70. void *context);
  71. // The callback from the MachoWalker for CRC and MD5
  72. static bool WalkerCB(MachoWalker *walker, load_command *cmd, off_t offset,
  73. bool swap, void *context);
  74. // The callback from the MachoWalker for LC_UUID
  75. static bool UUIDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset,
  76. bool swap, void *context);
  77. // The callback from the MachoWalker for LC_ID_DYLIB
  78. static bool IDWalkerCB(MachoWalker *walker, load_command *cmd, off_t offset,
  79. bool swap, void *context);
  80. // File path
  81. char path_[PATH_MAX];
  82. // Memory region to read from
  83. void *memory_;
  84. // Size of the memory region
  85. size_t memory_size_;
  86. // The current crc value
  87. uint32_t crc_;
  88. // The MD5 context
  89. google_breakpad::MD5Context md5_context_;
  90. // The current update to call from the Update callback
  91. UpdateFunction update_function_;
  92. };
  93. } // namespace MacFileUtilities
  94. #endif // COMMON_MAC_MACHO_ID_H__