Skip to content

Commit

Permalink
Move colors to dict instead of switch statement
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekSelander committed Oct 13, 2019
1 parent e02e464 commit 919b3fc
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 85 deletions.
84 changes: 39 additions & 45 deletions dsdump/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@
@import MachO;

/*******************************************************************************
*******************************************************************************/
Declarations
*******************************************************************************/


static NSArray <NSString *>* exc_rpaths = nil;
//static int analyzeFD = -1;
static void handle_args(int argc, const char * argv[]);

/*******************************************************************************
LC_MAIN fun starts haaaaaaaaaaaaaaa
*******************************************************************************/

int main(int argc, const char * argv[], const char*envp[]) {
handle_args(argc, argv);
Expand Down Expand Up @@ -61,27 +66,9 @@ int main(int argc, const char * argv[], const char*envp[]) {
}
DumpProcessesContainingLibrary(basename(resolved_path));
}

// if (xref_options.analyze) {
// NSString *savePath = [image analysisSavePath];
// analyzeFD = open(savePath.UTF8String, O_RDWR | O_CREAT| O_TRUNC, 0644);
// if (analyzeFD <= 0) {
// printf("Couldn't open %s\n", savePath.UTF8String);
// exit(1);
// }
//
//
// for (NSNumber *key in image.symbolEntry) {
// XRSymbolEntry *entry = image.symbolEntry[key];
// if (entry.visited) {
// continue;
// }
// printf("0x%011llx %s<stripped>%s\n", entry.address, dcolor(DSCOLOR_RED), color_end());
// }
// }

// Go through the options
if (! ( xref_options.file_offset || xref_options.library || xref_options.dump)) {

// Go through the options and pick a default if nothing is set
if (! ( xref_options.file_offset || xref_options.library)) {
[image dumpSymbols];
}

Expand All @@ -92,33 +79,35 @@ int main(int argc, const char * argv[], const char*envp[]) {


static void handle_args(int argc, const char * argv[]) {
int c;

while (1) {
int option_index = 0;
static struct option long_options[] = {
{"library", no_argument, &xref_options.library, 1 },
{"arch", required_argument, 0, 0 },
{"regex", no_argument, &xref_options.use_regex, 1},
{"verbose", optional_argument, &xref_options.verbose, 1 },
{"color", no_argument, &xref_options.color, 1 },
{"defined", no_argument, &xref_options.defined, 1 },
{"undefined", no_argument, &xref_options.undefined, 1 },
{"objc", no_argument, &xref_options.objectiveC_mode, 1 },
{"swift", no_argument, &xref_options.swift_mode, 1 },
{"all", no_argument, &xref_options.all_symbols, 1 },
{"analyze", no_argument, &xref_options.analyze, 1 },
{"debug", no_argument, &xref_options.debug, 1 },
{"help", no_argument, &xref_options.help, 1 },
{"arch", required_argument, 0, 0},
{"verbose", optional_argument, &xref_options.verbose, 1},
{"library", no_argument, &xref_options.library, 1},
{"regex", no_argument, &xref_options.use_regex, 1},
{"color", no_argument, &xref_options.color, 1},
{"defined", no_argument, &xref_options.defined, 1},
{"undefined", no_argument, &xref_options.undefined, 1},
{"objc", no_argument, &xref_options.objectiveC_mode, 1},
{"swift", no_argument, &xref_options.swift_mode, 1},
{"all", no_argument, &xref_options.all_symbols, 1},
{"analyze", no_argument, &xref_options.analyze, 1},
{"debug", no_argument, &xref_options.debug, 1},
{"help", no_argument, &xref_options.help, 1},
{"opcs", no_argument, &xref_options.opcodes, 1},
{"sym", no_argument, &xref_options.symbol_mode, 1},
{0, 0, 0, 0 }
};


c = getopt_long(argc, (char * const *)argv, "f:a:A:uUxcvSlZh",
int c = getopt_long(argc, (char * const *)argv, "f:a:A:uUOxcvSlZh",
long_options, &option_index);
if (c == -1) { break; }
struct host_basic_info;
if (c == -1) {
break;
}

switch (c) {
// Case for long getopts, that can't use ints
case 0:
if (strcmp(long_options[option_index].name, "file_offset") == 0) {
xref_options.file_offset = strtol(optarg, 0, 0);
Expand All @@ -138,6 +127,7 @@ static void handle_args(int argc, const char * argv[]) {
break;
case 'Z':
xref_options.analyze = 1;
break;
case 'c':
xref_options.color = 1;
break;
Expand All @@ -156,6 +146,9 @@ static void handle_args(int argc, const char * argv[]) {
case 'f':
AddFilter(optarg);
break;
case 'O':
xref_options.opcodes = 1;
break;
case 'h':
print_manpage();
exit(0);
Expand All @@ -182,7 +175,7 @@ static void handle_args(int argc, const char * argv[]) {
}

}

break;
} case '?':
break;

Expand All @@ -199,6 +192,7 @@ static void handle_args(int argc, const char * argv[]) {
xref_options.arch = getenv("ARCH");
}

if (!(xref_options.symbol_mode || xref_options.swift_mode || xref_options.objectiveC_mode || xref_options.virtual_address || xref_options.library)) {
xref_options.symbol_mode = 1;
}
}


34 changes: 17 additions & 17 deletions dsdump/miscellaneous.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ __attribute__((weak)) unsigned char __manpage_deets;
/// Globals for exploring multiple references
********************************************************************************/

//extern NSMutableSet <NSString*> *pathsSet;
//extern NSMutableSet <NSString*> *exploredSet;
//extern NSMutableSet <NSString*> *rpathSet;


typedef NS_OPTIONS(NSUInteger, DSCOLOR) {
DSCOLOR_CYAN,
DSCOLOR_YELLOW,
Expand All @@ -43,12 +38,15 @@ typedef NS_OPTIONS(NSUInteger, DSCOLOR) {
DSCOLOR_GRAY,
DSCOLOR_GREEN,
DSCOLOR_BOLD,
DSCOLOR_CYANISH
DSCOLOR_CYAN_UNDERLINE,
DSCOLOR_PURPLE_BOLD,
DSCOLOR_CYAN_LIGHT,
DSCOLOR_YELLOW_LIGHT
};
char const* dcolor(DSCOLOR c);

/// Ends the color option if the DSCOLOR env var is set
char const*color_end(void);
char const* color_end(void);

/// Different levels to logging, opted for a "codesign -vvvv" style
#define VERBOSE_NONE 0
Expand All @@ -58,11 +56,11 @@ char const*color_end(void);
#define VERBOSE_4 4

typedef struct {
int showLibReferences;
int verbose;
int undefined;
int objectiveC_mode;
int swift_mode;
int symbol_mode;
int all_symbols;
int defined;
int color;
Expand All @@ -74,24 +72,29 @@ typedef struct {
int virtual_address_count;
int library;
char * arch;
char * showSymbolReferences;
int debug;
int help;
char *dump;
int opcodes;
} xref_options_t;

extern xref_options_t xref_options;



//uint64_t read_uleb128 (const uint8_t ** offset, const uint8_t * end);
/// Read unsigned leb128 coding
const uint8_t *r_uleb128_decode(uint8_t *data, int *datalen, uint64_t *v);

/// Read signed leb128 coding
const uintptr_t r_sleb128_decode(uint8_t *byte, uintptr_t* shift, uint64_t *v);

/// Used to check for class filtering
BOOL ContainsFilteredWords(const char *word);

void warn_debug(const char *format, ...);
/// Used to input filters for checking for filtering
void AddFilter(char * filter);

/// Only print when DEBUG env var is set
void warn_debug(const char *format, ...);

#ifdef __cplusplus
}
#endif
Expand All @@ -100,9 +103,6 @@ void AddFilter(char * filter);
#define PTR_SIZE sizeof(void*)

/// Only print if DEBUG flag is set
#define DEBUG_PRINT(fmt, args...) if (xref_options.debug) printf(fmt, ## args)
#define DEBUG_PRINT(fmt, args...) if (xref_options.opcodes) { printf(fmt, ## args); }

#endif // MISCELLANEOUS_H



44 changes: 21 additions & 23 deletions dsdump/miscellaneous.mm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ void print_usage() {
// Colors!
********************************************************************************/

static const std::map<DSCOLOR, const char*> colorMap = {
{DSCOLOR_CYAN, "\e[36m"},
{DSCOLOR_GREEN, "\e[92m"},
{DSCOLOR_YELLOW, "\e[33m"},
{DSCOLOR_YELLOW_LIGHT, "\e[93m"},
{DSCOLOR_MAGENTA, "\e[95m"},
{DSCOLOR_PURPLE, "\e[35m"},
{DSCOLOR_RED, "\e[91m"},
{DSCOLOR_BLUE, "\e[34m"},
{DSCOLOR_GRAY, "\e[90m"},
{DSCOLOR_PURPLE_BOLD, "\e[35;1m"},
{DSCOLOR_CYAN_LIGHT,"\e[96m"},
{DSCOLOR_BOLD, "\e[1m"},
{DSCOLOR_CYAN_UNDERLINE, "\033[36;1;4m"}
};

char const* dcolor(DSCOLOR c) {
static BOOL useColor = NO;
static dispatch_once_t onceToken;
Expand All @@ -58,30 +74,11 @@ void print_usage() {
return "";
}

switch (c) {
case DSCOLOR_CYAN:
return "\e[36m";
case DSCOLOR_GREEN:
return "\e[92m";
case DSCOLOR_YELLOW:
return "\e[33m";
case DSCOLOR_MAGENTA:
return "\e[95m";
case DSCOLOR_PURPLE:
return "\e[35m";
case DSCOLOR_RED:
return "\e[91m";
case DSCOLOR_BLUE:
return "\e[34m";
case DSCOLOR_GRAY:
return "\e[90m";
case DSCOLOR_BOLD:
return "\e[1m";
case DSCOLOR_CYANISH:
return "\033[36;1;4m";
default:
return "";
auto color = colorMap.at(c);
if (color) {
return color;
}
return "";
}

char const* color_end() {
Expand Down Expand Up @@ -202,6 +199,7 @@ BOOL ContainsFilteredWords(const char *word) {
return NO;
}


void AddFilter(char * filter) {
payload::filters.insert(filter);
}
Expand Down

0 comments on commit 919b3fc

Please sign in to comment.