Skip to content

Commit

Permalink
Merge branch 'code-cleanups'
Browse files Browse the repository at this point in the history
Corrections for the Catch2 v3 release

Fixes #216
  • Loading branch information
szszszsz committed Aug 29, 2022
2 parents d9f4a48 + d56ab10 commit 9bb8552
Show file tree
Hide file tree
Showing 20 changed files with 182 additions and 182 deletions.
8 changes: 4 additions & 4 deletions NK_C_API.cc
Original file line number Diff line number Diff line change
Expand Up @@ -554,15 +554,15 @@ extern "C" {

NK_C_API int NK_is_AES_supported(const char *user_password) {
auto m = NitrokeyManager::instance();
return get_with_result([&]() {
return (uint8_t)m->is_AES_supported(user_password);
return get_with_result([&]() -> uint8_t {
return m->is_AES_supported(user_password);
});
}

NK_C_API int NK_login_auto() {
auto m = NitrokeyManager::instance();
return get_with_result([&]() {
return (uint8_t)m->connect();
return get_with_result([&]() -> uint8_t {
return m->connect();
});
}

Expand Down
18 changes: 9 additions & 9 deletions NitrokeyManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ using nitrokey::misc::strcpyT;

// package type to auth, auth type [Authorize,UserAuthorize]
template <typename S, typename A, typename T>
void NitrokeyManager::authorize_packet(T &package, const char *admin_temporary_password, shared_ptr<Device> device){
void NitrokeyManager::authorize_packet(T &package, const char *admin_temporary_password, shared_ptr<Device> device_){
if (!is_authorization_command_supported()){
LOG("Authorization command not supported, skipping", Loglevel::WARNING);
}
auto auth = get_payload<A>();
strcpyT(auth.temporary_password, admin_temporary_password);
auth.crc_to_authorize = S::CommandTransaction::getCRC(package);
A::CommandTransaction::run(device, auth);
A::CommandTransaction::run(device_, auth);
}

shared_ptr <NitrokeyManager> NitrokeyManager::_instance = nullptr;
Expand Down Expand Up @@ -357,7 +357,7 @@ using nitrokey::misc::strcpyT;
return res;
}

bool NitrokeyManager::is_connected() throw(){
bool NitrokeyManager::is_connected() noexcept {
std::lock_guard<std::mutex> lock(mex_dev_com_manager);
if(device != nullptr){
auto connected = device->could_be_enumerated();
Expand Down Expand Up @@ -479,8 +479,8 @@ using nitrokey::misc::strcpyT;
bool NitrokeyManager::is_internal_hotp_slot_number(uint8_t slot_number) const { return slot_number < 0x20; }
bool NitrokeyManager::is_valid_hotp_slot_number(uint8_t slot_number) const { return slot_number < 3; }
bool NitrokeyManager::is_valid_totp_slot_number(uint8_t slot_number) const { return slot_number < 0x10-1; } //15
uint8_t NitrokeyManager::get_internal_slot_number_for_totp(uint8_t slot_number) const { return (uint8_t) (0x20 + slot_number); }
uint8_t NitrokeyManager::get_internal_slot_number_for_hotp(uint8_t slot_number) const { return (uint8_t) (0x10 + slot_number); }
uint8_t NitrokeyManager::get_internal_slot_number_for_totp(uint8_t slot_number) const { return 0x20 + slot_number; }
uint8_t NitrokeyManager::get_internal_slot_number_for_hotp(uint8_t slot_number) const { return 0x10 + slot_number; }



Expand Down Expand Up @@ -706,7 +706,7 @@ using nitrokey::misc::strcpyT;
auto payload = get_payload<GetSlotName>();
payload.slot_number = slot_number;
auto resp = GetSlotName::CommandTransaction::run(device, payload);
return strndup((const char *) resp.data().slot_name, max_string_field_length);
return strndup(reinterpret_cast<const char *>(resp.data().slot_name), max_string_field_length);
}

bool NitrokeyManager::first_authenticate(const char *pin, const char *temporary_password) {
Expand Down Expand Up @@ -821,7 +821,7 @@ using nitrokey::misc::strcpyT;
auto p = get_payload<GetPasswordSafeSlotName>();
p.slot_number = slot_number;
auto response = GetPasswordSafeSlotName::CommandTransaction::run(device, p);
return strndup((const char *) response.data().slot_name, max_string_field_length);
return strndup(reinterpret_cast<const char *>(response.data().slot_name), max_string_field_length);
}

bool NitrokeyManager::is_valid_password_safe_slot_number(uint8_t slot_number) const { return slot_number < 16; }
Expand All @@ -831,15 +831,15 @@ using nitrokey::misc::strcpyT;
auto p = get_payload<GetPasswordSafeSlotLogin>();
p.slot_number = slot_number;
auto response = GetPasswordSafeSlotLogin::CommandTransaction::run(device, p);
return strndup((const char *) response.data().slot_login, max_string_field_length);
return strndup(reinterpret_cast<const char *>(response.data().slot_login), max_string_field_length);
}

char * NitrokeyManager::get_password_safe_slot_password(uint8_t slot_number) {
if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number);
auto p = get_payload<GetPasswordSafeSlotPassword>();
p.slot_number = slot_number;
auto response = GetPasswordSafeSlotPassword::CommandTransaction::run(device, p);
return strndup((const char *) response.data().slot_password, max_string_field_length); //FIXME use secure way
return strndup(reinterpret_cast<const char *>(response.data().slot_password), max_string_field_length); //FIXME use secure way
}

void NitrokeyManager::write_password_safe_slot(uint8_t slot_number, const char *slot_name, const char *slot_login,
Expand Down
4 changes: 2 additions & 2 deletions device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ int Device::send(const void *packet) {
throw DeviceNotConnected("Attempted HID send on an invalid descriptor.");
}
send_feature_report = hid_send_feature_report(
mp_devhandle, (const unsigned char *)(packet), HID_REPORT_SIZE);
mp_devhandle, static_cast<const unsigned char *>(packet), HID_REPORT_SIZE);
if (send_feature_report < 0) _reconnect();
//add thread sleep?
LOG(std::string("Sending attempt: ")+std::to_string(i+1) + " / 3" , Loglevel::DEBUG_L2);
Expand All @@ -200,7 +200,7 @@ int Device::recv(void *packet) {
throw DeviceNotConnected("Attempted HID receive on an invalid descriptor.");
}

status = (hid_get_feature_report(mp_devhandle, (unsigned char *)(packet),
status = (hid_get_feature_report(mp_devhandle, static_cast<unsigned char *>(packet),
HID_REPORT_SIZE));

auto pwherr = hid_error(mp_devhandle);
Expand Down
20 changes: 10 additions & 10 deletions libnitrokey/CommandFailedException.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,38 @@ class CommandFailedException : public std::exception {
const uint8_t last_command_id;
const uint8_t last_command_status;

CommandFailedException(uint8_t last_command_id, uint8_t last_command_status) :
last_command_id(last_command_id),
last_command_status(last_command_status){
CommandFailedException(uint8_t last_command_id_, uint8_t last_command_status_) :
last_command_id(last_command_id_),
last_command_status(last_command_status_) {
LOG(std::string("CommandFailedException, status: ")+ std::to_string(last_command_status), nitrokey::log::Loglevel::DEBUG);
}

virtual const char *what() const throw() {
virtual const char *what() const noexcept override {
return "Command execution has failed on device";
}


bool reason_timestamp_warning() const throw(){
bool reason_timestamp_warning() const noexcept {
return last_command_status == static_cast<uint8_t>(cs::timestamp_warning);
}

bool reason_AES_not_initialized() const throw(){
bool reason_AES_not_initialized() const noexcept {
return last_command_status == static_cast<uint8_t>(cs::AES_dec_failed);
}

bool reason_not_authorized() const throw(){
bool reason_not_authorized() const noexcept {
return last_command_status == static_cast<uint8_t>(cs::not_authorized);
}

bool reason_slot_not_programmed() const throw(){
bool reason_slot_not_programmed() const noexcept {
return last_command_status == static_cast<uint8_t>(cs::slot_not_programmed);
}

bool reason_wrong_password() const throw(){
bool reason_wrong_password() const noexcept {
return last_command_status == static_cast<uint8_t>(cs::wrong_password);
}

bool reason_smartcard_busy() const throw(){
bool reason_smartcard_busy() const noexcept {
return last_command_status == static_cast<uint8_t>(cs2::smartcard_error);
}

Expand Down
16 changes: 8 additions & 8 deletions libnitrokey/DeviceCommunicationExceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,36 @@ class DeviceCommunicationException: public std::runtime_error
explicit DeviceCommunicationException(const std::string& _msg): std::runtime_error(_msg), message(_msg){
++occurred;
}
uint8_t getType() const {return 1;};
// virtual const char* what() const throw() override {
uint8_t getType() const {return 1;}
// virtual const char* what() const noexcept override {
// return message.c_str();
// }
static bool has_occurred(){ return occurred > 0; };
static void reset_occurred_flag(){ occurred = 0; };
static bool has_occurred(){ return occurred > 0; }
static void reset_occurred_flag(){ occurred = 0; }
};

class DeviceNotConnected: public DeviceCommunicationException {
public:
DeviceNotConnected(std::string msg) : DeviceCommunicationException(msg){}
uint8_t getType() const {return 2;};
uint8_t getType() const {return 2;}
};

class DeviceSendingFailure: public DeviceCommunicationException {
public:
DeviceSendingFailure(std::string msg) : DeviceCommunicationException(msg){}
uint8_t getType() const {return 3;};
uint8_t getType() const {return 3;}
};

class DeviceReceivingFailure: public DeviceCommunicationException {
public:
DeviceReceivingFailure(std::string msg) : DeviceCommunicationException(msg){}
uint8_t getType() const {return 4;};
uint8_t getType() const {return 4;}
};

class InvalidCRCReceived: public DeviceReceivingFailure {
public:
InvalidCRCReceived(std::string msg) : DeviceReceivingFailure(msg){}
uint8_t getType() const {return 5;};
uint8_t getType() const {return 5;}
};


Expand Down
20 changes: 10 additions & 10 deletions libnitrokey/LibraryException.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class TargetBufferSmallerThanSource: public LibraryException {
size_t target_size;

TargetBufferSmallerThanSource(
size_t source_size, size_t target_size
) : source_size(source_size), target_size(target_size) {}
size_t source_size_, size_t target_size_
) : source_size(source_size_), target_size(target_size_) {}

virtual const char *what() const throw() override {
virtual const char *what() const noexcept override {
std::string s = " ";
auto ts = [](size_t x){ return std::to_string(x); };
std::string msg = std::string("Target buffer size is smaller than source: [source size, buffer size]")
Expand All @@ -65,9 +65,9 @@ class InvalidHexString : public LibraryException {
public:
uint8_t invalid_char;

InvalidHexString (uint8_t invalid_char) : invalid_char( invalid_char) {}
InvalidHexString (uint8_t invalid_char_) : invalid_char(invalid_char_) {}

virtual const char *what() const throw() override {
virtual const char *what() const noexcept override {
return "Invalid character in hex string";
}

Expand All @@ -82,9 +82,9 @@ class InvalidSlotException : public LibraryException {
public:
uint8_t slot_selected;

InvalidSlotException(uint8_t slot_selected) : slot_selected(slot_selected) {}
InvalidSlotException(uint8_t slot_selected_) : slot_selected(slot_selected_) {}

virtual const char *what() const throw() override {
virtual const char *what() const noexcept override {
return "Wrong slot selected";
}

Expand All @@ -102,13 +102,13 @@ class TooLongStringException : public LibraryException {
std::size_t size_destination;
std::string message;

TooLongStringException(size_t size_source, size_t size_destination, const std::string &message = "") : size_source(
size_source), size_destination(size_destination), message(message) {
TooLongStringException(size_t size_source_, size_t size_destination_, const std::string &message_ = "") : size_source(
size_source_), size_destination(size_destination_), message(message_) {
LOG(std::string("TooLongStringException, size diff: ")+ std::to_string(size_source-size_destination), nitrokey::log::Loglevel::DEBUG);

}

virtual const char *what() const throw() override {
virtual const char *what() const noexcept override {
//TODO add sizes and message data to final message
return "Too long string has been supplied as an argument";
}
Expand Down
6 changes: 3 additions & 3 deletions libnitrokey/LongOperationInProgressException.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class LongOperationInProgressException : public CommandFailedException {
unsigned char progress_bar_value;

LongOperationInProgressException(
unsigned char _command_id, uint8_t last_command_status, unsigned char _progress_bar_value)
: CommandFailedException(_command_id, last_command_status), progress_bar_value(_progress_bar_value){
unsigned char command_id_, uint8_t last_command_status_, unsigned char progress_bar_value_)
: CommandFailedException(command_id_, last_command_status_), progress_bar_value(progress_bar_value_){
LOG(
std::string("LongOperationInProgressException, progress bar status: ")+
std::to_string(progress_bar_value), nitrokey::log::Loglevel::DEBUG);
}
virtual const char *what() const throw() {
virtual const char *what() const noexcept override {
return "Device returned busy status with long operation in progress";
}
};
Expand Down
2 changes: 1 addition & 1 deletion libnitrokey/NitrokeyManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ char * strndup(const char* str, size_t maxlen);
bool connect(device::DeviceModel device_model);
bool connect();
bool disconnect();
bool is_connected() throw() ;
bool is_connected() noexcept ;
bool could_current_device_be_enumerated();
bool set_default_commands_delay(int delay);

Expand Down
12 changes: 6 additions & 6 deletions libnitrokey/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
#include "cxx_semantics.h"

#define print_to_ss(x) ( ss << " " << (#x) <<":\t" << (x) << std::endl );
#define print_to_ss_int(x) ( ss << " " << (#x) <<":\t" << (int)(x) << std::endl );
#define print_to_ss_int(x) ( ss << " " << (#x) <<":\t" << static_cast<int>(x) << std::endl );
#ifdef LOG_VOLATILE_DATA
#define print_to_ss_volatile(x) print_to_ss(x);
#else
#define print_to_ss_volatile(x) ( ss << " " << (#x) <<":\t" << "***********" << std::endl );
#endif
#define hexdump_to_ss(x) (ss << #x":\n"\
<< ::nitrokey::misc::hexdump((const uint8_t *) (&x), sizeof x, false));
<< ::nitrokey::misc::hexdump(reinterpret_cast<const uint8_t *>(&x), sizeof x, false));

namespace nitrokey {
namespace proto {
Expand Down Expand Up @@ -71,13 +71,13 @@ namespace stick20{
return ss.str();
}
void set_kind_admin() {
kind = (uint8_t) 'A';
kind = 'A';
}
void set_kind_admin_prefixed() {
kind = (uint8_t) 'P';
kind = 'P';
}
void set_kind_user() {
kind = (uint8_t) 'P';
kind = 'P';
}

void set_defaults(){
Expand All @@ -96,7 +96,7 @@ namespace stick20{
set_kind_admin_prefixed();
break;
}
};
}

} __packed;

Expand Down
12 changes: 6 additions & 6 deletions libnitrokey/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ class Device {

void show_stats();
// ErrorCounters get_stats(){ return m_counters; }
int get_retry_receiving_count() const { return m_retry_receiving_count; };
int get_retry_sending_count() const { return m_retry_sending_count; };
std::chrono::milliseconds get_retry_timeout() const { return m_retry_timeout; };
int get_retry_receiving_count() const { return m_retry_receiving_count; }
int get_retry_sending_count() const { return m_retry_sending_count; }
std::chrono::milliseconds get_retry_timeout() const { return m_retry_timeout; }
std::chrono::milliseconds get_send_receive_delay() const {return m_send_receive_delay;}

int get_last_command_status() {int a = std::atomic_exchange(&last_command_status, static_cast<uint8_t>(0)); return a;};
void set_last_command_status(uint8_t _err) { last_command_status = _err;} ;
bool last_command_sucessfull() const {return last_command_status == 0;};
int get_last_command_status() {int a = std::atomic_exchange(&last_command_status, static_cast<uint8_t>(0)); return a;}
void set_last_command_status(uint8_t _err) { last_command_status = _err;}
bool last_command_sucessfull() const {return last_command_status == 0;}
DeviceModel get_device_model() const {return m_model;}
void set_receiving_delay(std::chrono::milliseconds delay);
void set_retry_delay(std::chrono::milliseconds delay);
Expand Down
8 changes: 4 additions & 4 deletions libnitrokey/device_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ namespace nitrokey {
uint32_t calculate_CRC() const {
// w/o leading zero, a part of each HID packet
// w/o 4-byte crc
return misc::stm_crc32((const uint8_t *) (this) + 1,
(size_t) (HID_REPORT_SIZE - 5));
return misc::stm_crc32(reinterpret_cast<const uint8_t *>(this) + 1,
static_cast<size_t>(HID_REPORT_SIZE - 5));
}

void update_CRC() { crc = calculate_CRC(); }
Expand Down Expand Up @@ -156,8 +156,8 @@ namespace nitrokey {
uint32_t calculate_CRC() const {
// w/o leading zero, a part of each HID packet
// w/o 4-byte crc
return misc::stm_crc32((const uint8_t *) (this) + 1,
(size_t) (HID_REPORT_SIZE - 5));
return misc::stm_crc32(reinterpret_cast<const uint8_t *>(this) + 1,
static_cast<size_t>(HID_REPORT_SIZE - 5));
}

void update_CRC() { crc = calculate_CRC(); }
Expand Down
Loading

0 comments on commit 9bb8552

Please sign in to comment.