From adbc664125142c434294bfa795666c90c7608429 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 9 Mar 2017 18:49:24 +0100 Subject: Adjust for compilation on Visual Studio 2017 Building works however tests are not. Possibly linking with original hidapi solution would work. --- .gitignore | 1 + CMakeLists.txt | 4 ++-- CMakeSettings.json | 37 +++++++++++++++++++++++++++++++++++++ NK_C_API.cc | 4 ++-- NitrokeyManager.cc | 12 ++++++------ device.cc | 12 ++++++------ include/LibraryException.h | 2 +- include/command.h | 7 +++++-- include/cxx_semantics.h | 4 ++++ include/device_proto.h | 8 +++++--- include/misc.h | 2 +- include/stick10_commands.h | 8 ++++++++ include/stick20_commands.h | 5 +++++ misc.cc | 11 ++++++----- 14 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 CMakeSettings.json diff --git a/.gitignore b/.gitignore index 47b7703..c98c3a9 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ core .cache/ .idea/ CMakeFiles/ +/.vs diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b9f580..e77de6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,7 +65,7 @@ set(SOURCE_FILES # add_library(hidapi-libusb STATIC hidapi/libusb/hid.c ) #ELSE() include_directories(hidapi/hidapi) - add_library(hidapi-libusb STATIC hidapi/windows/hid.c ) + add_library(hidapi-libusb SHARED hidapi/windows/hid.c ) target_link_libraries(hidapi-libusb setupapi setupapi kernel32 user32 gdi32 winspool comdlg32 advapi32 shell32 ole32 oleaut32 uuid odbc32 odbccp32) #ENDIF() @@ -85,7 +85,7 @@ install (FILES ${LIB_INCLUDES} DESTINATION "include") IF (COMPILE_TESTS) include_directories(unittest/Catch/include) - add_library(catch STATIC unittest/catch_main.cpp ) + add_library(catch SHARED unittest/catch_main.cpp ) add_executable (test_C_API unittest/test_C_API.cpp) target_link_libraries (test_C_API ${EXTRA_LIBS} ${LIBNAME} catch) diff --git a/CMakeSettings.json b/CMakeSettings.json new file mode 100644 index 0000000..e8c1f1d --- /dev/null +++ b/CMakeSettings.json @@ -0,0 +1,37 @@ +{ + // See https://go.microsoft.com//fwlink//?linkid=834763 for more information about this file. + "configurations": [ + { + "name": "x86-Debug", + "generator": "Visual Studio 15 2017", + "configurationType" : "Debug", + "buildRoot": "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}", + "cmakeCommandArgs": "", + "buildCommandArgs": "-m -v:minimal" + }, + { + "name": "x86-Release", + "generator": "Visual Studio 15 2017", + "configurationType" : "Release", + "buildRoot": "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}", + "cmakeCommandArgs": "", + "buildCommandArgs": "-m -v:minimal" + }, + { + "name": "x64-Debug", + "generator": "Visual Studio 15 2017 Win64", + "configurationType" : "Debug", + "buildRoot": "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}", + "cmakeCommandArgs": "", + "buildCommandArgs": "-m -v:minimal" + }, + { + "name": "x64-Release", + "generator": "Visual Studio 15 2017 Win64", + "configurationType" : "Release", + "buildRoot": "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}", + "cmakeCommandArgs": "", + "buildCommandArgs": "-m -v:minimal" + } + ] +} \ No newline at end of file diff --git a/NK_C_API.cc b/NK_C_API.cc index 6d18e52..4e53cd1 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -169,7 +169,7 @@ extern const char * NK_status() { auto m = NitrokeyManager::instance(); return get_with_string_result([&](){ string && s = m->get_status_as_string(); - char * rs = strdup(s.c_str()); + char * rs = _strdup(s.c_str()); clear_string(s); return rs; }); @@ -179,7 +179,7 @@ extern const char * NK_device_serial_number(){ auto m = NitrokeyManager::instance(); return get_with_string_result([&](){ string && s = m->get_serial_number(); - char * rs = strdup(s.c_str()); + char * rs = _strdup(s.c_str()); clear_string(s); return rs; }); diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 6ce9910..a120085 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -397,7 +397,7 @@ namespace nitrokey{ auto payload = get_payload(); payload.slot_number = slot_number; auto resp = GetSlotName::CommandTransaction::run(device, payload); - return strdup((const char *) resp.data().slot_name); + return _strdup((const char *) resp.data().slot_name); } bool NitrokeyManager::first_authenticate(const char *pin, const char *temporary_password) { @@ -504,7 +504,7 @@ namespace nitrokey{ auto p = get_payload(); p.slot_number = slot_number; auto response = GetPasswordSafeSlotName::CommandTransaction::run(device, p); - return strdup((const char *) response.data().slot_name); + return _strdup((const char *) response.data().slot_name); } bool NitrokeyManager::is_valid_password_safe_slot_number(uint8_t slot_number) const { return slot_number < 16; } @@ -514,7 +514,7 @@ namespace nitrokey{ auto p = get_payload(); p.slot_number = slot_number; auto response = GetPasswordSafeSlotLogin::CommandTransaction::run(device, p); - return strdup((const char *) response.data().slot_login); + return _strdup((const char *) response.data().slot_login); } const char *NitrokeyManager::get_password_safe_slot_password(uint8_t slot_number) { @@ -522,7 +522,7 @@ namespace nitrokey{ auto p = get_payload(); p.slot_number = slot_number; auto response = GetPasswordSafeSlotPassword::CommandTransaction::run(device, p); - return strdup((const char *) response.data().slot_password); //FIXME use secure way + return _strdup((const char *) response.data().slot_password); //FIXME use secure way } void NitrokeyManager::write_password_safe_slot(uint8_t slot_number, const char *slot_name, const char *slot_login, @@ -724,7 +724,7 @@ namespace nitrokey{ const char * NitrokeyManager::get_status_storage_as_string(){ auto p = stick20::GetDeviceStatus::CommandTransaction::run(device); - return strdup(p.data().dissect().c_str()); + return _strdup(p.data().dissect().c_str()); } stick20::DeviceConfigurationResponsePacket::ResponsePayload NitrokeyManager::get_status_storage(){ @@ -734,7 +734,7 @@ namespace nitrokey{ const char * NitrokeyManager::get_SD_usage_data_as_string(){ auto p = stick20::GetSDCardOccupancy::CommandTransaction::run(device); - return strdup(p.data().dissect().c_str()); + return _strdup(p.data().dissect().c_str()); } int NitrokeyManager::get_progress_bar_value(){ diff --git a/device.cc b/device.cc index d904fd9..0111572 100644 --- a/device.cc +++ b/device.cc @@ -2,7 +2,7 @@ #include #include #include -#include +#include "hidapi/hidapi.h" #include "include/misc.h" #include "include/device.h" #include "include/log.h" @@ -31,7 +31,7 @@ Device::Device(const uint16_t vid, const uint16_t pid, const DeviceModel model, bool Device::disconnect() { //called in object's destructor - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); Log::instance()(std::string(__FUNCTION__) + std::string(m_model==DeviceModel::PRO?"PRO":"STORAGE"), Loglevel::DEBUG_L2); Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); @@ -45,7 +45,7 @@ bool Device::disconnect() { return true; } bool Device::connect() { - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); @@ -57,7 +57,7 @@ bool Device::connect() { } int Device::send(const void *packet) { - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); @@ -71,7 +71,7 @@ int Device::send(const void *packet) { } int Device::recv(void *packet) { - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); int status; @@ -115,7 +115,7 @@ int Device::recv(void *packet) { } bool Device::is_connected() { - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard lock(mex_dev_com); if (mp_devhandle==nullptr){ return false; diff --git a/include/LibraryException.h b/include/LibraryException.h index e62788d..daf0155 100644 --- a/include/LibraryException.h +++ b/include/LibraryException.h @@ -27,7 +27,7 @@ public: virtual const char *what() const throw() override { std::string s = " "; - auto ts = [](int x){ return std::to_string(x); }; + 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]") +s+ ts(source_size) +s+ ts(target_size); return msg.c_str(); diff --git a/include/command.h b/include/command.h index 0a875e4..fc374f7 100644 --- a/include/command.h +++ b/include/command.h @@ -28,6 +28,7 @@ namespace stick20{ template class PasswordCommand : public Command { + constexpr static CommandID _command_id() { return cmd_id; } public: struct CommandPayload { uint8_t kind; @@ -69,8 +70,10 @@ namespace stick20{ } __packed; - typedef Transaction::command_id(), struct CommandPayload, struct EmptyPayload> - CommandTransaction; + //typedef Transaction::command_id(), struct CommandPayload, struct EmptyPayload> + // CommandTransaction; + using CommandTransaction = Transaction; + //using CommandTransaction = Transaction<_command_id(), CommandPayload, EmptyPayload>; }; } diff --git a/include/cxx_semantics.h b/include/cxx_semantics.h index b846317..29e51c3 100644 --- a/include/cxx_semantics.h +++ b/include/cxx_semantics.h @@ -1,7 +1,11 @@ #ifndef CXX_SEMANTICS_H #define CXX_SEMANTICS_H +#ifndef _WINDOWS #define __packed __attribute__((__packed__)) +#else +#define __packed +#endif /* * There's no need to include Boost for a simple subset this project needs. diff --git a/include/device_proto.h b/include/device_proto.h index cbb74fb..1e381dd 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -6,7 +6,6 @@ #include #include #include -#include // a local version for compatibility with Windows #include #include "cxx_semantics.h" @@ -34,6 +33,7 @@ #include #include "DeviceCommunicationExceptions.h" +#define bzero(b,len) (memset((b), '\0', (len)), (void) 0) namespace nitrokey { namespace proto { @@ -43,7 +43,7 @@ namespace nitrokey { * * TODO (future) support for Big Endian */ - +#pragma pack (push,1) /* * Every packet is a USB HID report (check USB spec) */ @@ -181,8 +181,10 @@ namespace nitrokey { typedef command_payload CommandPayload; typedef response_payload ResponsePayload; + typedef struct HIDReport OutgoingPacket; typedef struct DeviceResponse ResponsePacket; +#pragma pack (pop) static_assert(std::is_pod::value, "outgoingpacket must be a pod type"); @@ -216,7 +218,7 @@ namespace nitrokey { static std::mutex send_receive_mtx; std::lock_guard guard(send_receive_mtx); - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); if (dev == nullptr){ throw DeviceNotConnected("Device not initialized"); diff --git a/include/misc.h b/include/misc.h index 9e4659d..330654a 100644 --- a/include/misc.h +++ b/include/misc.h @@ -27,7 +27,7 @@ namespace misc { strncpy((char*) &dest, src, s_dest); } - +#define bzero(b,len) (memset((b), '\0', (len)), (void) 0) template typename T::CommandPayload get_payload(){ //Create, initialize and return by value command payload diff --git a/include/stick10_commands.h b/include/stick10_commands.h index 8d37dbd..3d9e234 100644 --- a/include/stick10_commands.h +++ b/include/stick10_commands.h @@ -1,5 +1,6 @@ #ifndef STICK10_COMMANDS_H #define STICK10_COMMANDS_H + #include #include #include @@ -8,9 +9,13 @@ #include "command.h" #include "device_proto.h" +#pragma pack (push,1) + namespace nitrokey { namespace proto { + + /* * Stick10 protocol definition */ @@ -844,8 +849,11 @@ class BuildAESKey : Command { typedef Transaction CommandTransaction; + }; + } } } +#pragma pack (pop) #endif diff --git a/include/stick20_commands.h b/include/stick20_commands.h index b887636..e3bea3f 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -1,12 +1,15 @@ #ifndef STICK20_COMMANDS_H #define STICK20_COMMANDS_H + + #include #include "command.h" #include #include #include "device_proto.h" +#pragma pack (push,1) namespace nitrokey { namespace proto { @@ -332,10 +335,12 @@ namespace nitrokey { typedef Transaction CommandTransaction; }; + } } } #undef print_to_ss +#pragma pack (pop) #endif diff --git a/misc.cc b/misc.cc index a76bfb6..f85d486 100644 --- a/misc.cc +++ b/misc.cc @@ -5,18 +5,19 @@ #include #include #include "LibraryException.h" +#include namespace nitrokey { namespace misc { -std::vector hex_string_to_byte(const char* hexString){ +::std::vector hex_string_to_byte(const char* hexString){ const size_t big_string_size = 256; //arbitrary 'big' number const size_t s_size = strlen(hexString); const size_t d_size = s_size/2; if (s_size%2!=0 || s_size>big_string_size){ throw InvalidHexString(0); } - auto data = std::vector(); + auto data = ::std::vector(); data.reserve(d_size); char buf[2]; @@ -36,9 +37,9 @@ std::vector hex_string_to_byte(const char* hexString){ }; #include -std::string hexdump(const char *p, size_t size, bool print_header, +::std::string hexdump(const char *p, size_t size, bool print_header, bool print_ascii, bool print_empty) { - std::stringstream out; + ::std::stringstream out; char formatbuf[128]; const char *pstart = p; @@ -68,7 +69,7 @@ std::string hexdump(const char *p, size_t size, bool print_header, out << '.'; } } - out << std::endl; + out << ::std::endl; } return out.str(); } -- cgit v1.2.1