substrate2.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. substrate2.h ... Convenient wrapper for MobileSubstrate.
  3. Copyright (c) 2009 KennyTM~ <kennytm@gmail.com>
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright notice, this
  8. list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. * Neither the name of the KennyTM~ nor the names of its contributors may be
  13. used to endorse or promote products derived from this software without
  14. specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  22. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef SUBSTRATE2_H
  27. #define SUBSTRATE2_H
  28. // Use substrate.h on iPhoneOS, and APELite on x86/ppc for debugging.
  29. #ifdef __arm__
  30. #import <substrate.h>
  31. #define REGPARM3
  32. #elif __i386__ || __ppc__
  33. #import <objc/runtime.h>
  34. #import <objc/message.h>
  35. extern
  36. #if __cplusplus
  37. "C"
  38. #endif
  39. void* APEPatchCreate(const void* original, const void* replacement);
  40. #define MSHookFunction(original, replacement, result) (*(result) = APEPatchCreate((original), (replacement)))
  41. #define MSHookMessageEx(class, selector, replacement, result) (*(result) = method_setImplementation(class_getInstanceMethod((class), (selector)), (replacement)))
  42. #define REGPARM3 __attribute__((regparm(3)))
  43. #else
  44. #error Not supported in non-ARM/i386/PPC system.
  45. #endif
  46. #define Original(funcname) original_##funcname
  47. #define DefineHook(rettype, funcname, ...) \
  48. rettype funcname (__VA_ARGS__); \
  49. static rettype (*original_##funcname) (__VA_ARGS__); \
  50. static rettype replaced_##funcname (__VA_ARGS__)
  51. #define DefineObjCHook(rettype, funcname, ...) \
  52. static rettype (*original_##funcname) (__VA_ARGS__); \
  53. static rettype replaced_##funcname (__VA_ARGS__)
  54. #define DefineHiddenHook(rettype, funcname, ...) \
  55. static rettype (*funcname) (__VA_ARGS__); \
  56. REGPARM3 static rettype (*original_##funcname) (__VA_ARGS__); \
  57. REGPARM3 static rettype replaced_##funcname (__VA_ARGS__)
  58. #define InstallHook(funcname) MSHookFunction(funcname, replaced_##funcname, (void**)&original_##funcname)
  59. #define InstallObjCInstanceHook(cls, sel, delimited_name) MSHookMessageEx((cls), (sel), (IMP)replaced_##delimited_name, (IMP*)&original_##delimited_name)
  60. #define InstallObjCClassHook(cls, sel, delimited_name) MSHookMessageEx(object_getClass(cls), (sel), (IMP)replaced_##delimited_name, (IMP*)&original_##delimited_name)
  61. #endif