FLEXAddressExplorerCoordinator.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // FLEXAddressExplorerCoordinator.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 7/10/19.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXAddressExplorerCoordinator.h"
  9. #import "FLEXGlobalsViewController.h"
  10. #import "FLEXObjectExplorerFactory.h"
  11. #import "FLEXObjectExplorerViewController.h"
  12. #import "FLEXRuntimeUtility.h"
  13. #import "FLEXUtility.h"
  14. @interface UITableViewController (FLEXAddressExploration)
  15. - (void)deselectSelectedRow;
  16. - (void)tryExploreAddress:(NSString *)addressString safely:(BOOL)safely;
  17. @end
  18. @implementation FLEXAddressExplorerCoordinator
  19. #pragma mark - FLEXGlobalsEntry
  20. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row {
  21. return @"🔎 Address Explorer";
  22. }
  23. + (FLEXGlobalsEntryRowAction)globalsEntryRowAction:(FLEXGlobalsRow)row {
  24. return ^(UITableViewController *host) {
  25. NSString *title = @"Explore Object at Address";
  26. NSString *message = @"Paste a hexadecimal address below, starting with '0x'. "
  27. "Use the unsafe option if you need to bypass pointer validation, "
  28. "but know that it may crash the app if the address is invalid.";
  29. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  30. make.title(title).message(message);
  31. make.configuredTextField(^(UITextField *textField) {
  32. #if !TARGET_OS_TV
  33. NSString *copied = UIPasteboard.generalPasteboard.string;
  34. #endif
  35. textField.placeholder = @"0x00000070deadbeef";
  36. // Go ahead and paste our clipboard if we have an address copied
  37. #if !TARGET_OS_TV
  38. if ([copied hasPrefix:@"0x"]) {
  39. textField.text = copied;
  40. [textField selectAll:nil];
  41. }
  42. #endif
  43. });
  44. make.button(@"Explore").handler(^(NSArray<NSString *> *strings) {
  45. [host tryExploreAddress:strings.firstObject safely:YES];
  46. });
  47. make.button(@"Unsafe Explore").destructiveStyle().handler(^(NSArray *strings) {
  48. [host tryExploreAddress:strings.firstObject safely:NO];
  49. });
  50. make.button(@"Cancel").cancelStyle();
  51. } showFrom:host];
  52. };
  53. }
  54. @end
  55. @implementation UITableViewController (FLEXAddressExploration)
  56. - (void)deselectSelectedRow {
  57. NSIndexPath *selected = self.tableView.indexPathForSelectedRow;
  58. [self.tableView deselectRowAtIndexPath:selected animated:YES];
  59. }
  60. - (void)tryExploreAddress:(NSString *)addressString safely:(BOOL)safely {
  61. NSScanner *scanner = [NSScanner scannerWithString:addressString];
  62. unsigned long long hexValue = 0;
  63. BOOL didParseAddress = [scanner scanHexLongLong:&hexValue];
  64. const void *pointerValue = (void *)hexValue;
  65. NSString *error = nil;
  66. if (didParseAddress) {
  67. if (safely && ![FLEXRuntimeUtility pointerIsValidObjcObject:pointerValue]) {
  68. error = @"The given address is unlikely to be a valid object.";
  69. }
  70. } else {
  71. error = @"Malformed address. Make sure it's not too long and starts with '0x'.";
  72. }
  73. if (!error) {
  74. id object = (__bridge id)pointerValue;
  75. FLEXObjectExplorerViewController *explorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:object];
  76. [self.navigationController pushViewController:explorer animated:YES];
  77. } else {
  78. [FLEXAlert showAlert:@"Uh-oh" message:error from:self];
  79. [self deselectSelectedRow];
  80. }
  81. }
  82. @end