From 01a09e81fa1d68f75d72298d25c673cd264fc996 Mon Sep 17 00:00:00 2001 From: Leon Lynch Date: Sun, 21 Jul 2024 21:19:39 +0200 Subject: [PATCH] Implement hidden --isocodes-path option for emv-decode and emv-tool This allows the path of the iso-codes JSON files to be set instead of using the default. This is useful for either using custom JSON files, or for future MacOS or Windows packages where the iso-codes JSON files may be at a different path. --- tools/emv-decode.c | 18 +++++++++++++++--- tools/emv-tool.c | 17 ++++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/tools/emv-decode.c b/tools/emv-decode.c index 7a31d8f..6ccb770 100644 --- a/tools/emv-decode.c +++ b/tools/emv-decode.c @@ -89,11 +89,13 @@ enum emv_decode_mode_t { EMV_DECODE_ISO8859_14, EMV_DECODE_ISO8859_15, EMV_DECODE_VERSION, + EMV_DECODE_OVERRIDE_ISOCODES_PATH, EMV_DECODE_OVERRIDE_MCC_JSON, }; static enum emv_decode_mode_t emv_decode_mode = EMV_DECODE_NONE; // Testing parameters +static char* isocodes_path = NULL; static char* mcc_json = NULL; // argp option structure @@ -166,7 +168,8 @@ static struct argp_option argp_options[] = { { "version", EMV_DECODE_VERSION, NULL, 0, "Display emv-utils version" }, - // Hidden option for testing + // Hidden options for testing + { "isocodes-path", EMV_DECODE_OVERRIDE_ISOCODES_PATH, "path", OPTION_HIDDEN, "Override directory path of iso-codes JSON files" }, { "mcc-json", EMV_DECODE_OVERRIDE_MCC_JSON, "path", OPTION_HIDDEN, "Override path of mcc-codes JSON file" }, { 0 }, @@ -303,6 +306,11 @@ static error_t argp_parser_helper(int key, char* arg, struct argp_state* state) return 0; } + case EMV_DECODE_OVERRIDE_ISOCODES_PATH: { + isocodes_path = strdup(arg); + return 0; + } + case EMV_DECODE_OVERRIDE_MCC_JSON: { mcc_json = strdup(arg); return 0; @@ -405,13 +413,13 @@ int main(int argc, char** argv) return 1; } - r = emv_strings_init(NULL, mcc_json); + r = emv_strings_init(isocodes_path, mcc_json); if (r < 0) { fprintf(stderr, "Failed to initialise EMV strings\n"); return 2; } if (r > 0) { - fprintf(stderr, "Failed to find iso-codes data; currency, country and language lookups will not be possible\n"); + fprintf(stderr, "Failed to load iso-codes data or mcc-codes data; currency, country, language or MCC lookups may not be possible\n"); } switch (emv_decode_mode) { @@ -889,6 +897,7 @@ int main(int argc, char** argv) case EMV_DECODE_ISO8859_X: case EMV_DECODE_VERSION: + case EMV_DECODE_OVERRIDE_ISOCODES_PATH: case EMV_DECODE_OVERRIDE_MCC_JSON: // Implemented in argp_parser_helper() break; @@ -900,6 +909,9 @@ int main(int argc, char** argv) if (arg_str) { free(arg_str); } + if (isocodes_path) { + free(isocodes_path); + } if (mcc_json) { free(mcc_json); } diff --git a/tools/emv-tool.c b/tools/emv-tool.c index cbf6198..b332630 100644 --- a/tools/emv-tool.c +++ b/tools/emv-tool.c @@ -59,6 +59,7 @@ enum emv_tool_param_t { EMV_TOOL_PARAM_DEBUG_SOURCES_MASK, EMV_TOOL_PARAM_DEBUG_LEVEL, EMV_TOOL_VERSION, + EMV_TOOL_OVERRIDE_ISOCODES_PATH, EMV_TOOL_OVERRIDE_MCC_JSON, }; @@ -76,7 +77,8 @@ static struct argp_option argp_options[] = { { "version", EMV_TOOL_VERSION, NULL, 0, "Display emv-utils version" }, - // Hidden option for testing + // Hidden options for testing + { "isocodes-path", EMV_TOOL_OVERRIDE_ISOCODES_PATH, "path", OPTION_HIDDEN, "Override directory path of iso-codes JSON files" }, { "mcc-json", EMV_TOOL_OVERRIDE_MCC_JSON, "path", OPTION_HIDDEN, "Override path of mcc-codes JSON file" }, { 0 }, @@ -116,6 +118,7 @@ static const char* debug_level_str[] = { static enum emv_debug_level_t debug_level = EMV_DEBUG_INFO; // Testing parameters +static char* isocodes_path = NULL; static char* mcc_json = NULL; @@ -245,6 +248,11 @@ static error_t argp_parser_helper(int key, char* arg, struct argp_state* state) return 0; } + case EMV_TOOL_OVERRIDE_ISOCODES_PATH: { + isocodes_path = strdup(arg); + return 0; + } + case EMV_TOOL_OVERRIDE_MCC_JSON: { mcc_json = strdup(arg); return 0; @@ -650,13 +658,13 @@ int main(int argc, char** argv) argp_help(&argp_config, stdout, ARGP_HELP_STD_HELP, argv[0]); } - r = emv_strings_init(NULL, mcc_json); + r = emv_strings_init(isocodes_path, mcc_json); if (r < 0) { fprintf(stderr, "Failed to initialise EMV strings\n"); return 2; } if (r > 0) { - fprintf(stderr, "Failed to find iso-codes data; currency, country and language lookups will not be possible\n"); + fprintf(stderr, "Failed to load iso-codes data or mcc-codes data; currency, country, language or MCC lookups may not be possible\n"); } r = emv_debug_init( @@ -912,6 +920,9 @@ int main(int argc, char** argv) pcsc_exit: pcsc_release(&pcsc); + if (isocodes_path) { + free(isocodes_path); + } if (mcc_json) { free(mcc_json); }