From bcfc7f44632717855ed75a3e643278a3ddee7309 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 9 Aug 2016 13:25:19 +0200 Subject: Return a vector to C API to duplicate there Signed-off-by: Szczepan Zalega --- NK_C_API.cc | 21 ++++++++------------- NitrokeyManager.cc | 12 ++++++------ include/NitrokeyManager.h | 2 +- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/NK_C_API.cc b/NK_C_API.cc index 556ec65..577f2d6 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -5,9 +5,10 @@ using namespace nitrokey; static uint8_t NK_last_command_status = 0; template -T* array_dup(std::vector& v){ +T* duplicate_vector_and_clear(std::vector &v){ auto d = new T[v.size()]; std::copy(v.begin(), v.end(), d); + std::fill(v.begin(), v.end(), 0); return d; } @@ -141,7 +142,7 @@ extern uint8_t* NK_read_config(){ auto m = NitrokeyManager::instance(); return get_with_array_result( [&](){ auto v = m->read_config(); - return array_dup(v); + return duplicate_vector_and_clear(v); }); } @@ -274,18 +275,12 @@ extern int NK_enable_password_safe(const char *user_pin){ }); } extern uint8_t * NK_get_password_safe_slot_status(){ - NK_last_command_status = 0; auto m = NitrokeyManager::instance(); - auto null_result = new uint8_t[16]; - memset(null_result, 0, 16); - try { - const auto slot_status = m->get_password_safe_slot_status(); - return slot_status; //TODO FIXME - } - catch (CommandFailedException & commandFailedException){ - NK_last_command_status = commandFailedException.last_command_status; - } - return null_result; + return get_with_array_result( [&](){ + auto slot_status = m->get_password_safe_slot_status(); + return duplicate_vector_and_clear(slot_status); + }); + } extern uint8_t NK_get_user_retry_count(){ diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 14e31b2..4b7f591 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -282,12 +282,12 @@ namespace nitrokey{ EnablePasswordSafe::CommandTransaction::run(*device, p); } - uint8_t * NitrokeyManager::get_password_safe_slot_status() { - auto responsePayload = GetPasswordSafeSlotStatus::CommandTransaction::run(*device); //TODO FIXME - auto res = new uint8_t[16]; - memcpy(res, responsePayload.data().password_safe_status, 16*sizeof (uint8_t)); - //FIXME return vector and do copy on C_API side - return res; + vector NitrokeyManager::get_password_safe_slot_status() { + auto responsePayload = GetPasswordSafeSlotStatus::CommandTransaction::run(*device); + vector v = vector(responsePayload.data().password_safe_status, + responsePayload.data().password_safe_status + + sizeof(responsePayload.data().password_safe_status)); + return v; } uint8_t NitrokeyManager::get_user_retry_count() { diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 19b54bd..d2ea991 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -49,7 +49,7 @@ namespace nitrokey { void enable_password_safe(const char *user_pin); - uint8_t * get_password_safe_slot_status(); + vector get_password_safe_slot_status(); uint8_t get_admin_retry_count(); uint8_t get_user_retry_count(); -- cgit v1.2.3