From 729381ec7c2fe8a079f40ce3863b4198a9581dc0 Mon Sep 17 00:00:00 2001 From: invertego Date: Sun, 7 Jan 2024 06:32:57 -0800 Subject: unidasm: Allow -count to be specified in hexadecimal or octal. (#11912) --- src/tools/unidasm.cpp | 50 +++++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/src/tools/unidasm.cpp b/src/tools/unidasm.cpp index d84393be307..50c5e5ba7e8 100644 --- a/src/tools/unidasm.cpp +++ b/src/tools/unidasm.cpp @@ -1147,6 +1147,24 @@ void unidasm_data_buffer::decrypt(const unidasm_data_buffer &buffer, bool opcode abort(); } +static int parse_number(const char *curarg, const char *default_format, u32 *value) +{ + int result; + if(curarg[0] == '0') { + if(tolower((uint8_t)curarg[1]) == 'x') + result = sscanf(&curarg[2], "%x", value); + else if(tolower((uint8_t)curarg[1]) == 'o') + result = sscanf(&curarg[2], "%o", value); + else + result = sscanf(&curarg[1], "%o", value); + } + else if(curarg[0] == '$') + result = sscanf(&curarg[1], "%x", value); + else + result = sscanf(&curarg[0], default_format, value); + return result; +} + static int parse_options(int argc, char *argv[], options *opts) { bool pending_base = false; @@ -1190,20 +1208,7 @@ static int parse_options(int argc, char *argv[], options *opts) } else if(pending_base) { // base PC - int result; - if(curarg[0] == '0') { - if(tolower((uint8_t)curarg[1]) == 'x') - result = sscanf(&curarg[2], "%x", &opts->basepc); - else if(tolower((uint8_t)curarg[1]) == 'o') - result = sscanf(&curarg[2], "%o", &opts->basepc); - else - result = sscanf(&curarg[1], "%o", &opts->basepc); - } - else if(curarg[0] == '$') - result = sscanf(&curarg[1], "%x", &opts->basepc); - else - result = sscanf(&curarg[0], "%x", &opts->basepc); - if(result != 1) + if(parse_number(curarg, "%x", &opts->basepc) != 1) goto usage; pending_base = false; @@ -1220,26 +1225,13 @@ static int parse_options(int argc, char *argv[], options *opts) } else if(pending_skip) { // skip bytes - int result; - if(curarg[0] == '0') { - if(tolower((uint8_t)curarg[1]) == 'x') - result = sscanf(&curarg[2], "%x", &opts->skip); - else if(tolower((uint8_t)curarg[1]) == 'o') - result = sscanf(&curarg[2], "%o", &opts->skip); - else - result = sscanf(&curarg[1], "%o", &opts->skip); - } - else if(curarg[0] == '$') - result = sscanf(&curarg[1], "%x", &opts->skip); - else - result = sscanf(curarg, "%d", &opts->skip); - if(result != 1) + if(parse_number(curarg, "%d", &opts->skip) != 1) goto usage; pending_skip = false; } else if(pending_count) { // size - if(sscanf(curarg, "%d", &opts->count) != 1) + if(parse_number(curarg, "%d", &opts->count) != 1) goto usage; pending_count = false; -- cgit v1.2.3