From 98a58e0294c45b7432e6e5a69db36c91cede4800 Mon Sep 17 00:00:00 2001 From: Leon Lynch Date: Wed, 15 Mar 2023 21:31:17 +0100 Subject: [PATCH] Add high level EMV library interface and refactor generated headers * Generate emv_utils_config.h for build configuration to contain project version string, platform checks, and paths required by iso-codes and mcc-codes. This replaces isocodes_config.h and mcc_config.h. * Add emv.h, emv.c, implement emv_lib_version_string(), and update emv-decode and emv-tool accordingly. --- src/CMakeLists.txt | 21 +++++++++++------ src/{mcc_config.h.in => emv.c} | 16 ++++++------- src/{isocodes_config.h.in => emv.h} | 23 ++++++++++++------ src/emv_strings.c | 1 - src/emv_utils_config.h.in | 36 +++++++++++++++++++++++++++++ src/isocodes_lookup.cpp | 2 +- src/mcc_lookup.cpp | 2 +- tests/mcc_test.c | 2 +- tools/CMakeLists.txt | 8 ------- tools/emv-decode.c | 10 +++++++- tools/emv-tool.c | 10 +++++++- 11 files changed, 95 insertions(+), 36 deletions(-) rename src/{mcc_config.h.in => emv.c} (74%) rename src/{isocodes_config.h.in => emv.h} (68%) create mode 100644 src/emv_utils_config.h.in diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 77a45e4..6e5396a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,8 +14,8 @@ if(NOT HAVE_TIME_H) message(FATAL_ERROR "Failed to find time.h") endif() include(CheckSymbolExists) -check_symbol_exists(clock_gettime time.h HAVE_clock_gettime) -if(NOT HAVE_clock_gettime) +check_symbol_exists(clock_gettime time.h HAVE_CLOCK_GETTIME) +if(NOT HAVE_CLOCK_GETTIME) message(FATAL_ERROR "Failed to find clock_gettime") endif() @@ -63,6 +63,10 @@ else() message(FATAL_ERROR "mcc-codes/mcc_codes.json not found") endif() +# Generate emv-utils build configuration for internal use only +# This file should NOT be installed or used by an installed header +configure_file(emv_utils_config.h.in emv_utils_config.h) + # ISO 7816 library add_library(iso7816 iso7816.c @@ -175,6 +179,7 @@ install( # EMV library add_library(emv + emv.c emv_fields.c emv_tlv.c emv_dol.c @@ -184,6 +189,7 @@ add_library(emv emv_tal.c ) set(emv_HEADERS # PUBLIC_HEADER property requires a list instead of individual entries + emv.h emv_tags.h emv_fields.h emv_tlv.h @@ -208,9 +214,12 @@ set_target_properties(emv VERSION ${CMAKE_PROJECT_VERSION} SOVERSION ${CMAKE_PROJECT_VERSION_MAJOR}.${CMAKE_PROJECT_VERSION_MINOR} ) -target_include_directories(emv INTERFACE - $ - $ +target_include_directories(emv + PRIVATE + "${CMAKE_CURRENT_BINARY_DIR}" # For generated headers + INTERFACE + $ + $ ) install( TARGETS @@ -251,8 +260,6 @@ set_target_properties(emv_strings VERSION ${CMAKE_PROJECT_VERSION} SOVERSION ${CMAKE_PROJECT_VERSION_MAJOR}.${CMAKE_PROJECT_VERSION_MINOR} ) -configure_file(isocodes_config.h.in isocodes_config.h) -configure_file(mcc_config.h.in mcc_config.h) target_include_directories(emv_strings PRIVATE "${CMAKE_CURRENT_BINARY_DIR}" # For generated headers diff --git a/src/mcc_config.h.in b/src/emv.c similarity index 74% rename from src/mcc_config.h.in rename to src/emv.c index febaf3c..5aa7250 100644 --- a/src/mcc_config.h.in +++ b/src/emv.c @@ -1,6 +1,6 @@ /** - * @file mcc_config.h - * @brief Definitions related to the configuration of mcc-codes + * @file emv.c + * @brief High level EMV library interface * * Copyright (c) 2023 Leon Lynch * @@ -19,10 +19,10 @@ * . */ -#ifndef MCC_CONFIG_H -#define MCC_CONFIG_H +#include "emv.h" +#include "emv_utils_config.h" -#define MCC_JSON_BUILD_PATH "@MCC_JSON_BUILD_PATH@" -#define MCC_JSON_INSTALL_PATH "@MCC_JSON_INSTALL_PATH@" - -#endif +const char* emv_lib_version_string(void) +{ + return EMV_UTILS_VERSION_STRING; +} diff --git a/src/isocodes_config.h.in b/src/emv.h similarity index 68% rename from src/isocodes_config.h.in rename to src/emv.h index e90ce91..a75f157 100644 --- a/src/isocodes_config.h.in +++ b/src/emv.h @@ -1,8 +1,8 @@ /** - * @file isocodes_config.h - * @brief Definitions related to the configuration of the iso-codes package + * @file emv.h + * @brief High level EMV library interface * - * Copyright (c) 2021 Leon Lynch + * Copyright (c) 2023 Leon Lynch * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -19,10 +19,19 @@ * . */ -#ifndef ISOCODES_CONFIG_H -#define ISOCODES_CONFIG_H +#ifndef EMV_H +#define EMV_H -#define ISOCODES_PREFIX "@IsoCodes_PREFIX@" -#define ISOCODES_JSON_PATH "@IsoCodes_JSON_PATH@" +#include + +__BEGIN_DECLS + +/** + * Retrieve EMV library version string + * @return Pointer to null-terminated string. Do not free. + */ +const char* emv_lib_version_string(void); + +__END_DECLS #endif diff --git a/src/emv_strings.c b/src/emv_strings.c index 4b534f5..15ac8c3 100644 --- a/src/emv_strings.c +++ b/src/emv_strings.c @@ -25,7 +25,6 @@ #include "emv_fields.h" #include "isocodes_lookup.h" #include "mcc_lookup.h" -#include "mcc_config.h" #include #include diff --git a/src/emv_utils_config.h.in b/src/emv_utils_config.h.in new file mode 100644 index 0000000..5161c15 --- /dev/null +++ b/src/emv_utils_config.h.in @@ -0,0 +1,36 @@ +/** + * @file emv_utils_config.h + * @brief Definitions related to emv-utils build configuration + * + * Copyright (c) 2023 Leon Lynch + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#ifndef EMV_UTILS_CONFIG_H +#define EMV_UTILS_CONFIG_H + +#cmakedefine EMV_UTILS_VERSION_STRING "@EMV_UTILS_VERSION_STRING@" +#cmakedefine HAVE_TIME_H +#cmakedefine HAVE_CLOCK_GETTIME + +// For iso-codes +#define ISOCODES_JSON_PATH "@IsoCodes_JSON_PATH@" + +// For mcc-codes +#cmakedefine MCC_JSON_BUILD_PATH "@MCC_JSON_BUILD_PATH@" +#cmakedefine MCC_JSON_INSTALL_PATH "@MCC_JSON_INSTALL_PATH@" + +#endif diff --git a/src/isocodes_lookup.cpp b/src/isocodes_lookup.cpp index 6542c66..532be88 100644 --- a/src/isocodes_lookup.cpp +++ b/src/isocodes_lookup.cpp @@ -20,7 +20,7 @@ */ #include "isocodes_lookup.h" -#include "isocodes_config.h" +#include "emv_utils_config.h" #include #include diff --git a/src/mcc_lookup.cpp b/src/mcc_lookup.cpp index cb3f386..f0231c3 100644 --- a/src/mcc_lookup.cpp +++ b/src/mcc_lookup.cpp @@ -20,7 +20,7 @@ */ #include "mcc_lookup.h" -#include "mcc_config.h" +#include "emv_utils_config.h" #include #include diff --git a/tests/mcc_test.c b/tests/mcc_test.c index dbaa8ef..c389507 100644 --- a/tests/mcc_test.c +++ b/tests/mcc_test.c @@ -20,7 +20,7 @@ */ #include "mcc_lookup.h" -#include "mcc_config.h" +#include "emv_utils_config.h" #include #include diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index d89fe09..cabdd2b 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -57,10 +57,6 @@ target_link_libraries(print_helpers PRIVATE iso7816 emv emv_strings) # EMV decode command line tool if(BUILD_EMV_DECODE) add_executable(emv-decode emv-decode.c) - set_source_files_properties(emv-decode.c - PROPERTIES - COMPILE_DEFINITIONS EMV_UTILS_VERSION_STRING="${EMV_UTILS_VERSION_STRING}" - ) target_link_libraries(emv-decode PRIVATE print_helpers iso7816 emv emv_strings iso8859) if(TARGET libargp::argp) target_link_libraries(emv-decode PRIVATE libargp::argp) @@ -85,10 +81,6 @@ if(BUILD_EMV_TOOL) endif() add_executable(emv-tool emv-tool.c ../src/pcsc.c) - set_source_files_properties(emv-tool.c - PROPERTIES - COMPILE_DEFINITIONS EMV_UTILS_VERSION_STRING="${EMV_UTILS_VERSION_STRING}" - ) target_link_libraries(emv-tool PRIVATE print_helpers iso7816 emv emv_strings) if(TARGET libargp::argp) target_link_libraries(emv-tool PRIVATE libargp::argp) diff --git a/tools/emv-decode.c b/tools/emv-decode.c index 9238e3c..5e7d1fc 100644 --- a/tools/emv-decode.c +++ b/tools/emv-decode.c @@ -19,6 +19,7 @@ * . */ +#include "emv.h" #include "iso7816.h" #include "print_helpers.h" #include "emv_strings.h" @@ -286,7 +287,14 @@ static error_t argp_parser_helper(int key, char* arg, struct argp_state* state) } case EMV_DECODE_VERSION: { - printf("%s\n", EMV_UTILS_VERSION_STRING); + const char* version; + + version = emv_lib_version_string(); + if (version) { + printf("%s\n", version); + } else { + printf("Unknown\n"); + } exit(EXIT_SUCCESS); return 0; } diff --git a/tools/emv-tool.c b/tools/emv-tool.c index a2ffa3a..7ee5440 100644 --- a/tools/emv-tool.c +++ b/tools/emv-tool.c @@ -19,6 +19,7 @@ * . */ +#include "emv.h" #include "pcsc.h" #include "iso7816.h" #include "print_helpers.h" @@ -255,7 +256,14 @@ static error_t argp_parser_helper(int key, char* arg, struct argp_state* state) } case EMV_TOOL_VERSION: { - printf("%s\n", EMV_UTILS_VERSION_STRING); + const char* version; + + version = emv_lib_version_string(); + if (version) { + printf("%s\n", version); + } else { + printf("Unknown\n"); + } exit(EXIT_SUCCESS); return 0; }