diff options
author | Szczepan Zalega <szczepan@nitrokey.com> | 2020-07-28 12:29:43 +0200 |
---|---|---|
committer | Szczepan Zalega <szczepan@nitrokey.com> | 2020-07-28 12:29:56 +0200 |
commit | dc1cfa6252073ac345412e7df9c5cc0365bb7f11 (patch) | |
tree | a62eccae0fbc7317c3bf67db4cb8ceb79dfd96e7 /NK_C_API_helpers.h | |
parent | 145149d65915e3f2eb068032b428ee5cfc0295c9 (diff) | |
download | libnitrokey-dc1cfa6252073ac345412e7df9c5cc0365bb7f11.tar.gz libnitrokey-dc1cfa6252073ac345412e7df9c5cc0365bb7f11.tar.bz2 |
Extract Nitrokey Storage only features to separate unit
Diffstat (limited to 'NK_C_API_helpers.h')
-rw-r--r-- | NK_C_API_helpers.h | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/NK_C_API_helpers.h b/NK_C_API_helpers.h new file mode 100644 index 0000000..e9ae4d1 --- /dev/null +++ b/NK_C_API_helpers.h @@ -0,0 +1,88 @@ +#ifndef LIBNITROKEY_NK_C_API_HELPERS_H +#define LIBNITROKEY_NK_C_API_HELPERS_H + + +#include "NK_C_API.h" +#include <iostream> +#include <tuple> +#include "libnitrokey/NitrokeyManager.h" +#include <cstring> +#include "libnitrokey/LibraryException.h" +#include "libnitrokey/cxx_semantics.h" +#include "libnitrokey/stick20_commands.h" +#include "libnitrokey/device_proto.h" +#include "libnitrokey/version.h" + + +extern uint8_t NK_last_command_status; + + +template <typename T> +T* duplicate_vector_and_clear(std::vector<T> &v){ + auto d = new T[v.size()]; + std::copy(v.begin(), v.end(), d); + std::fill(v.begin(), v.end(), 0); + return d; +} + +template <typename R, typename T> +std::tuple<int, R> get_with_status(T func, R fallback) { + NK_last_command_status = 0; + try { + return std::make_tuple(0, func()); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + } + catch (LibraryException & libraryException){ + NK_last_command_status = libraryException.exception_id(); + } + catch (const DeviceCommunicationException &deviceException){ + NK_last_command_status = 256-deviceException.getType(); + } + return std::make_tuple(NK_last_command_status, fallback); +} + +template <typename T> +uint8_t * get_with_array_result(T func){ + return std::get<1>(get_with_status<uint8_t*>(func, nullptr)); +} + +template <typename T> +char* get_with_string_result(T func){ + auto result = std::get<1>(get_with_status<char*>(func, nullptr)); + if (result == nullptr) { + return strndup("", MAXIMUM_STR_REPLY_LENGTH); + } + return result; +} + +template <typename T> +auto get_with_result(T func){ + return std::get<1>(get_with_status(func, static_cast<decltype(func())>(0))); +} + +template <typename T> +uint8_t get_without_result(T func){ + NK_last_command_status = 0; + try { + func(); + return 0; + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + } + catch (LibraryException & libraryException){ + NK_last_command_status = libraryException.exception_id(); + } + catch (const InvalidCRCReceived &invalidCRCException){ + ; + } + catch (const DeviceCommunicationException &deviceException){ + NK_last_command_status = 256-deviceException.getType(); + } + return NK_last_command_status; +} + + +#endif // LIBNITROKEY_NK_C_API_HELPERS_H |