From 7878f91cdda79b4860ed6564fbe5086740abb7c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Mon, 21 Nov 2022 17:56:29 +0100 Subject: [PATCH] Add support for MacOS and old compilers (not supporting C++11). Main patch is from Samuel Marks. --- README.md | 1 - include/Base64.h | 12 +++++++-- include/libgourou_common.h | 14 ++++++++-- src/device.cpp | 53 +++++++++++++++++++++++++++++++++++--- src/fulfillment_item.cpp | 5 ++++ 5 files changed, 77 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0cffdd7..d09f731 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,6 @@ _externals_ : _internals_ : * PugiXML - * Base64 * uPDFParser For utils : diff --git a/include/Base64.h b/include/Base64.h index cdfdc04..e5b4930 100644 --- a/include/Base64.h +++ b/include/Base64.h @@ -33,7 +33,11 @@ class Base64 { public: static std::string Encode(const std::string data) { - static constexpr char sEncodingTable[] = { + static +#if __STDC_VERSION__ >= 201112L + constexpr +#endif /* __STDC_VERSION__ >= 201112L */ + char sEncodingTable[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', @@ -73,7 +77,11 @@ class Base64 { } static std::string Decode(const std::string& input, std::string& out) { - static constexpr unsigned char kDecodingTable[] = { + static +#if __STDC_VERSION__ >= 201112L + constexpr +#endif /* __STDC_VERSION__ >= 201112L */ + unsigned char kDecodingTable[] = { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, diff --git a/include/libgourou_common.h b/include/libgourou_common.h index 826a1a3..22bbd2d 100644 --- a/include/libgourou_common.h +++ b/include/libgourou_common.h @@ -131,7 +131,17 @@ namespace gourou DRM_INVALID_KEY_SIZE, DRM_ERR_ENCRYPTION_KEY_FP }; - + + #ifndef _NOEXCEPT + #if __STDC_VERSION__ >= 201112L + # define _NOEXCEPT noexcept + # define _NOEXCEPT_(x) noexcept(x) + #else + # define _NOEXCEPT throw() + # define _NOEXCEPT_(x) + #endif + #endif /* !_NOEXCEPT */ + /** * Generic exception class */ @@ -157,7 +167,7 @@ namespace gourou this->fullmessage = strdup(other.fullmessage); } - ~Exception() + ~Exception() _NOEXCEPT { free(fullmessage); } diff --git a/src/device.cpp b/src/device.cpp index 481a99b..1418d47 100644 --- a/src/device.cpp +++ b/src/device.cpp @@ -29,13 +29,23 @@ #include #include -// From https://stackoverflow.com/questions/1779715/how-to-get-mac-address-of-your-machine-using-a-c-program/35242525 +#include +#if defined(__linux__) || defined(linux) || defined(__linux) #include -#include +#include #include #include -#include +#elif (defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \ + || defined(__bsdi__) || defined(__DragonFly__) || defined(__APPLE__)) +#include +#include +#include +#define BSD_HEADERS 1 +#endif + +#if defined(__linux__) || defined(linux) || defined(__linux) +// From https://stackoverflow.com/questions/1779715/how-to-get-mac-address-of-your-machine-using-a-c-program/35242525 int get_mac_address(unsigned char* mac_address) { struct ifreq ifr; @@ -74,6 +84,43 @@ int get_mac_address(unsigned char* mac_address) return 1; } +#elif BSD_HEADERS +// https://stackoverflow.com/a/3978293 +int get_mac_address(unsigned char* mac_address, const char* if_name = "en0") +{ + ifaddrs* iflist; + int found = 0; + if (getifaddrs(&iflist) == 0) { + for (ifaddrs* cur = iflist; cur; cur = cur->ifa_next) { + if ((cur->ifa_addr->sa_family == AF_LINK) && + (strcmp(cur->ifa_name, if_name) == 0) && + cur->ifa_addr) { + sockaddr_dl* sdl = (sockaddr_dl*)cur->ifa_addr; + memcpy(mac_address, LLADDR(sdl), sdl->sdl_alen); + found = 1; + break; + } + } + + freeifaddrs(iflist); + } + return found; +} +#else +int get_mac_address(unsigned char* mac_address) +{ + GOUROU_LOG(INFO, "get_mac_address() not implemented for your platform, using a static address"); + + mac_address[0] = 0x8D; + mac_address[1] = 0x70; + mac_address[2] = 0x13; + mac_address[3] = 0x8D; + mac_address[4] = 0x43; + mac_address[5] = 0x27; + + return 1; +} +#endif /* defined(__linux__) || defined(linux) || defined(__linux) */ namespace gourou diff --git a/src/fulfillment_item.cpp b/src/fulfillment_item.cpp index 924c63f..dbd8d31 100644 --- a/src/fulfillment_item.cpp +++ b/src/fulfillment_item.cpp @@ -17,6 +17,7 @@ along with libgourou. If not, see . */ +#include #include #include #include "user.h" @@ -93,8 +94,12 @@ namespace gourou std::string FulfillmentItem::getMetadata(std::string name) { // https://stackoverflow.com/questions/313970/how-to-convert-an-instance-of-stdstring-to-lower-case + #if __STDC_VERSION__ >= 201112L std::transform(name.begin(), name.end(), name.begin(), [](unsigned char c){ return std::tolower(c); }); + #else + std::transform(name.begin(), name.end(), name.begin(), tolower); + #endif name = std::string("dc:") + name; pugi::xpath_node path = metadatas.select_node(name.c_str());