Skip to content

Commit

Permalink
Implement hidden --isocodes-path option for emv-decode and emv-tool
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leonlynch committed Jul 21, 2024
1 parent c144b5f commit 01a09e8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
18 changes: 15 additions & 3 deletions tools/emv-decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
17 changes: 14 additions & 3 deletions tools/emv-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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 },
Expand Down Expand Up @@ -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;


Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 01a09e8

Please sign in to comment.