From e13d3fdf4d4754c85e7e05e98592afb71d22be3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 22 Aug 2016 21:29:22 +0200 Subject: Move get_first_non_empty to louloulibs/utils --- louloulibs/utils/get_first_non_empty.cpp | 11 +++++++++++ louloulibs/utils/get_first_non_empty.hpp | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 louloulibs/utils/get_first_non_empty.cpp create mode 100644 louloulibs/utils/get_first_non_empty.hpp (limited to 'louloulibs/utils') diff --git a/louloulibs/utils/get_first_non_empty.cpp b/louloulibs/utils/get_first_non_empty.cpp new file mode 100644 index 0000000..5b3bedb --- /dev/null +++ b/louloulibs/utils/get_first_non_empty.cpp @@ -0,0 +1,11 @@ +#include + +bool is_empty(const std::string& val) +{ + return val.empty(); +} + +bool is_empty(const int& val) +{ + return val == 0; +} diff --git a/louloulibs/utils/get_first_non_empty.hpp b/louloulibs/utils/get_first_non_empty.hpp new file mode 100644 index 0000000..a38f5fb --- /dev/null +++ b/louloulibs/utils/get_first_non_empty.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include + +bool is_empty(const std::string& val); +bool is_empty(const int& val); + +template +T get_first_non_empty(T&& last) +{ + return last; +} + +template +T get_first_non_empty(T&& first, Args&&... args) +{ + if (!is_empty(first)) + return first; + return get_first_non_empty(std::forward(args)...); +} -- cgit v1.2.3 From 7536a1b3f38fbf093c1629b0db209754ada0c906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 25 Aug 2016 19:43:51 +0200 Subject: Respond to MAM requests on a channel JID At the moment, result-set-management is not implemented, the whole history (well, at most 1024 messages) is returned. --- louloulibs/utils/time.cpp | 12 ++++++++++++ louloulibs/utils/time.hpp | 9 +++++++++ 2 files changed, 21 insertions(+) create mode 100644 louloulibs/utils/time.cpp create mode 100644 louloulibs/utils/time.hpp (limited to 'louloulibs/utils') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp new file mode 100644 index 0000000..e23def4 --- /dev/null +++ b/louloulibs/utils/time.cpp @@ -0,0 +1,12 @@ +#include + +namespace utils +{ +std::string to_string(const std::time_t& timestamp) +{ + constexpr std::size_t stamp_size = 20; + char date_buf[stamp_size]; + std::strftime(date_buf, stamp_size, "%FT%TZ", std::gmtime(×tamp)); + return {std::begin(date_buf), std::end(date_buf)}; +} +} \ No newline at end of file diff --git a/louloulibs/utils/time.hpp b/louloulibs/utils/time.hpp new file mode 100644 index 0000000..dff1250 --- /dev/null +++ b/louloulibs/utils/time.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include +#include + +namespace utils +{ +std::string to_string(const std::time_t& timestamp); +} \ No newline at end of file -- cgit v1.2.3 From 2a4905df0153c47656555a21f3d57bbad6f3ffe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 31 Aug 2016 01:17:45 +0200 Subject: Fix to_string(time_t) and write a unit test for it --- louloulibs/utils/time.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'louloulibs/utils') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp index e23def4..b85d764 100644 --- a/louloulibs/utils/time.cpp +++ b/louloulibs/utils/time.cpp @@ -4,9 +4,10 @@ namespace utils { std::string to_string(const std::time_t& timestamp) { - constexpr std::size_t stamp_size = 20; - char date_buf[stamp_size]; - std::strftime(date_buf, stamp_size, "%FT%TZ", std::gmtime(×tamp)); - return {std::begin(date_buf), std::end(date_buf)}; + constexpr std::size_t stamp_size = 21; + char date_buf[stamp_size]; + if (std::strftime(date_buf, stamp_size, "%FT%TZ", std::gmtime(×tamp)) != stamp_size - 1) + return ""; + return {std::begin(date_buf), std::end(date_buf) - 1}; } } \ No newline at end of file -- cgit v1.2.3 From 1140db3b88bb70cbcab044efa729707bd5a442f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 31 Aug 2016 01:58:13 +0200 Subject: Add parse_datetime --- louloulibs/utils/time.cpp | 11 +++++++++++ louloulibs/utils/time.hpp | 1 + 2 files changed, 12 insertions(+) (limited to 'louloulibs/utils') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp index b85d764..305b2ad 100644 --- a/louloulibs/utils/time.cpp +++ b/louloulibs/utils/time.cpp @@ -1,4 +1,5 @@ #include +#include namespace utils { @@ -10,4 +11,14 @@ std::string to_string(const std::time_t& timestamp) return ""; return {std::begin(date_buf), std::end(date_buf) - 1}; } + +std::time_t parse_datetime(const std::string& stamp) +{ + struct tm tm; + if (!::strptime(stamp.data(), "%FT%T%z", &tm)) + return -1; + auto res = ::timegm(&tm); + return res; +} + } \ No newline at end of file diff --git a/louloulibs/utils/time.hpp b/louloulibs/utils/time.hpp index dff1250..c71cd9c 100644 --- a/louloulibs/utils/time.hpp +++ b/louloulibs/utils/time.hpp @@ -6,4 +6,5 @@ namespace utils { std::string to_string(const std::time_t& timestamp); +std::time_t parse_datetime(const std::string& stamp); } \ No newline at end of file -- cgit v1.2.3 From aaa2ca670ab5f19390e77a1f5ea88017b312ceaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Sat, 17 Sep 2016 00:34:28 +0200 Subject: Fix the parse_datetime by using %Z instead of %z If anybody knows why fedora accepts both, but it only works with %z on debian, please tell me. --- louloulibs/utils/time.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'louloulibs/utils') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp index 305b2ad..bc8b3f8 100644 --- a/louloulibs/utils/time.cpp +++ b/louloulibs/utils/time.cpp @@ -15,10 +15,10 @@ std::string to_string(const std::time_t& timestamp) std::time_t parse_datetime(const std::string& stamp) { struct tm tm; - if (!::strptime(stamp.data(), "%FT%T%z", &tm)) + if (!::strptime(stamp.data(), "%FT%T%Z", &tm)) return -1; auto res = ::timegm(&tm); return res; } -} \ No newline at end of file +} -- cgit v1.2.3 From 985da8f2b2fbd2119a15e88677ab23c0f74f4100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 28 Sep 2016 20:14:41 +0200 Subject: Remove unused code in sha1 module --- louloulibs/utils/sha1.cpp | 33 --------------------------------- louloulibs/utils/sha1.hpp | 2 -- 2 files changed, 35 deletions(-) (limited to 'louloulibs/utils') diff --git a/louloulibs/utils/sha1.cpp b/louloulibs/utils/sha1.cpp index 76476df..f75bc2a 100644 --- a/louloulibs/utils/sha1.cpp +++ b/louloulibs/utils/sha1.cpp @@ -119,36 +119,3 @@ uint8_t* sha1_result(sha1nfo *s) { // Return pointer to hash (20 characters) return s->state.b; } - -#define HMAC_IPAD 0x36 -#define HMAC_OPAD 0x5c - -void sha1_initHmac(sha1nfo *s, const uint8_t* key, int keyLength) { - uint8_t i; - memset(s->keyBuffer, 0, BLOCK_LENGTH); - if (keyLength > BLOCK_LENGTH) { - // Hash long keys - sha1_init(s); - for (;keyLength--;) sha1_writebyte(s, *key++); - memcpy(s->keyBuffer, sha1_result(s), HASH_LENGTH); - } else { - // Block length keys are used as is - memcpy(s->keyBuffer, key, keyLength); - } - // Start inner hash - sha1_init(s); - for (i=0; ikeyBuffer[i] ^ HMAC_IPAD); - } -} - -uint8_t* sha1_resultHmac(sha1nfo *s) { - uint8_t i; - // Complete inner hash - memcpy(s->innerHash,sha1_result(s),HASH_LENGTH); - // Calculate outer hash - sha1_init(s); - for (i=0; ikeyBuffer[i] ^ HMAC_OPAD); - for (i=0; iinnerHash[i]); - return sha1_result(s); -} diff --git a/louloulibs/utils/sha1.hpp b/louloulibs/utils/sha1.hpp index d02de75..d436782 100644 --- a/louloulibs/utils/sha1.hpp +++ b/louloulibs/utils/sha1.hpp @@ -31,5 +31,3 @@ void sha1_init(sha1nfo *s); void sha1_writebyte(sha1nfo *s, uint8_t data); void sha1_write(sha1nfo *s, const char *data, size_t len); uint8_t* sha1_result(sha1nfo *s); -void sha1_initHmac(sha1nfo *s, const uint8_t* key, int keyLength); -uint8_t* sha1_resultHmac(sha1nfo *s); -- cgit v1.2.3 From e5b392ece8c90605b86d0d93f0ca6989048bc1c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 3 Oct 2016 23:30:20 +0200 Subject: Fix parse_datetime by always using a 'z' as the timezone MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Because some plateform accept Z and z, but some only accept z… --- louloulibs/utils/time.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'louloulibs/utils') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp index bc8b3f8..abf0a84 100644 --- a/louloulibs/utils/time.cpp +++ b/louloulibs/utils/time.cpp @@ -14,8 +14,9 @@ std::string to_string(const std::time_t& timestamp) std::time_t parse_datetime(const std::string& stamp) { + auto stamp2 = stamp.substr(0, stamp.size() - 1) + "z"; struct tm tm; - if (!::strptime(stamp.data(), "%FT%T%Z", &tm)) + if (!::strptime(stamp2.data(), "%FT%T%Z", &tm)) return -1; auto res = ::timegm(&tm); return res; -- cgit v1.2.3 From 548e4ad473e7be22f971184312cc5ce9b8fe56b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 11 Oct 2016 00:18:48 +0200 Subject: Parse the timezone myself, instead of using the broken strptime See https://lab.louiz.org/louiz/biboumi/issues/3215 https://github.com/andikleen/glibc/blob/master/time/strptime_l.c#L746-L747 for why strptime() sucks We use std::get_time now, to parse the date and time. And we parse the timezone by hand. fix #3215 --- louloulibs/utils/time.cpp | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to 'louloulibs/utils') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp index abf0a84..7ad6663 100644 --- a/louloulibs/utils/time.cpp +++ b/louloulibs/utils/time.cpp @@ -1,6 +1,10 @@ #include #include +#include +#include +#include + namespace utils { std::string to_string(const std::time_t& timestamp) @@ -14,12 +18,42 @@ std::string to_string(const std::time_t& timestamp) std::time_t parse_datetime(const std::string& stamp) { - auto stamp2 = stamp.substr(0, stamp.size() - 1) + "z"; - struct tm tm; - if (!::strptime(stamp2.data(), "%FT%T%Z", &tm)) + static const char* format = "%Y-%m-%dT%H:%M:%S"; + std::tm t = {}; + std::istringstream ss(stamp); + ss.imbue(std::locale("en_US.utf-8")); + + std::string timezone; + ss >> std::get_time(&t, format) >> timezone; + if (ss.fail()) + return -1; + + if (timezone.empty()) return -1; - auto res = ::timegm(&tm); - return res; + + if (timezone.compare(0, 1, "Z") != 0) + { + std::stringstream tz_ss; + tz_ss << timezone; + int multiplier = -1; + char prefix; + int hours; + char sep; + int minutes; + tz_ss >> prefix >> hours >> sep >> minutes; + if (tz_ss.fail()) + return -1; + if (prefix == '-') + multiplier = +1; + else if (prefix != '+') + return -1; + + t.tm_hour += multiplier * hours; + t.tm_min += multiplier * minutes; + } + return ::timegm(&t); } } + + -- cgit v1.2.3 From c54f28d29d5f1d7a2bb973609beffbe5ad56d422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 11 Oct 2016 21:00:56 +0200 Subject: =?UTF-8?q?Conditionally=20use=20strptime=20if=20we=20don=E2=80=99?= =?UTF-8?q?t=20have=20std::get=5Ftime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- louloulibs/utils/time.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'louloulibs/utils') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp index 7ad6663..afd6117 100644 --- a/louloulibs/utils/time.cpp +++ b/louloulibs/utils/time.cpp @@ -5,6 +5,8 @@ #include #include +#include "louloulibs.h" + namespace utils { std::string to_string(const std::time_t& timestamp) @@ -20,6 +22,7 @@ std::time_t parse_datetime(const std::string& stamp) { static const char* format = "%Y-%m-%dT%H:%M:%S"; std::tm t = {}; +#ifdef HAS_GET_TIME std::istringstream ss(stamp); ss.imbue(std::locale("en_US.utf-8")); @@ -27,6 +30,14 @@ std::time_t parse_datetime(const std::string& stamp) ss >> std::get_time(&t, format) >> timezone; if (ss.fail()) return -1; +#else + /* Y - m - d T H : M : S */ + constexpr std::size_t stamp_size_without_tz = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2; + if (!strptime(stamp.data(), format, &t)) { + return -1; + } + const std::string timezone(stamp.data() + stamp_size_without_tz); +#endif if (timezone.empty()) return -1; -- cgit v1.2.3 From ce06c25e93183282be42ab79bfed2ab7c02791ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 20 Oct 2016 19:32:20 +0200 Subject: Very little optimization by using a simpler scope_guard when possible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The version with the vector, that can be disabled etc, is “very” slow, so we use unique_ptr when we don’t need to disable it, and when it only contains one function --- louloulibs/utils/encoding.cpp | 8 ++++---- louloulibs/utils/scopeguard.hpp | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'louloulibs/utils') diff --git a/louloulibs/utils/encoding.cpp b/louloulibs/utils/encoding.cpp index 507f38a..4b20797 100644 --- a/louloulibs/utils/encoding.cpp +++ b/louloulibs/utils/encoding.cpp @@ -76,7 +76,7 @@ namespace utils { // The given string MUST be a valid utf-8 string unsigned char* res = new unsigned char[original.size()]; - ScopeGuard sg([&res]() { delete[] res;}); + const auto sg = utils::make_scope_guard([&res](auto&&) { delete[] res;}); // pointer where we write valid chars unsigned char* r = res; @@ -140,7 +140,7 @@ namespace utils else throw std::runtime_error("Invalid UTF-8 passed to remove_invalid_xml_chars"); } - return std::string(reinterpret_cast(res), r-res); + return {reinterpret_cast(res), static_cast(r-res)}; } std::string convert_to_utf8(const std::string& str, const char* charset) @@ -152,7 +152,7 @@ namespace utils throw std::runtime_error("Cannot convert into UTF-8"); // Make sure cd is always closed when we leave this function - ScopeGuard sg([&]{ iconv_close(cd); }); + const auto sg = utils::make_scope_guard([&](auto&&){ iconv_close(cd); }); size_t inbytesleft = str.size(); @@ -169,7 +169,7 @@ namespace utils char* outbuf_ptr = outbuf; // Make sure outbuf is always deleted when we leave this function - sg.add_callback([&]{ delete[] outbuf; }); + const auto sg2 = utils::make_scope_guard([&](auto&&){ delete[] outbuf; }); bool done = false; while (done == false) diff --git a/louloulibs/utils/scopeguard.hpp b/louloulibs/utils/scopeguard.hpp index ee1e2ef..cd0e89e 100644 --- a/louloulibs/utils/scopeguard.hpp +++ b/louloulibs/utils/scopeguard.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include /** @@ -85,5 +86,11 @@ private: }; +template +auto make_scope_guard(F&& f) +{ + return std::unique_ptr>{(void*)1, std::forward(f)}; +} + } -- cgit v1.2.3 From ac61450184112ccb22971cff6cfa6117b4ddfbb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 27 Oct 2016 01:37:55 +0200 Subject: Refactor remove_invalid_xml_chars to use correct types directly --- louloulibs/utils/encoding.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'louloulibs/utils') diff --git a/louloulibs/utils/encoding.cpp b/louloulibs/utils/encoding.cpp index 4b20797..712028e 100644 --- a/louloulibs/utils/encoding.cpp +++ b/louloulibs/utils/encoding.cpp @@ -75,13 +75,12 @@ namespace utils std::string remove_invalid_xml_chars(const std::string& original) { // The given string MUST be a valid utf-8 string - unsigned char* res = new unsigned char[original.size()]; - const auto sg = utils::make_scope_guard([&res](auto&&) { delete[] res;}); + std::vector res(original.size(), '\0'); // pointer where we write valid chars - unsigned char* r = res; + char* r = res.data(); - const unsigned char* str = reinterpret_cast(original.c_str()); + const char* str = original.c_str(); std::bitset<20> codepoint; while (*str) @@ -140,7 +139,7 @@ namespace utils else throw std::runtime_error("Invalid UTF-8 passed to remove_invalid_xml_chars"); } - return {reinterpret_cast(res), static_cast(r-res)}; + return {res.data(), static_cast(r - res.data())}; } std::string convert_to_utf8(const std::string& str, const char* charset) -- cgit v1.2.3 From ae02e58b9dc276b247be84e1d708ca50a1f5bbd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 31 Oct 2016 13:54:25 +0100 Subject: Some cleanups --- louloulibs/utils/encoding.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'louloulibs/utils') diff --git a/louloulibs/utils/encoding.cpp b/louloulibs/utils/encoding.cpp index 712028e..cb953c0 100644 --- a/louloulibs/utils/encoding.cpp +++ b/louloulibs/utils/encoding.cpp @@ -196,12 +196,8 @@ namespace utils outbuf_ptr++; done = true; break; - case E2BIG: - // This should never happen - done = true; - break; - default: - // This should happen even neverer + case E2BIG: // This should never happen + default: // This should happen even neverer done = true; break; } -- cgit v1.2.3 From f50f50653dc064575e4730c31b5615301f00e057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 1 Nov 2016 19:43:56 +0100 Subject: Refactor load_certs() --- louloulibs/utils/encoding.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'louloulibs/utils') diff --git a/louloulibs/utils/encoding.cpp b/louloulibs/utils/encoding.cpp index cb953c0..60f2212 100644 --- a/louloulibs/utils/encoding.cpp +++ b/louloulibs/utils/encoding.cpp @@ -151,7 +151,7 @@ namespace utils throw std::runtime_error("Cannot convert into UTF-8"); // Make sure cd is always closed when we leave this function - const auto sg = utils::make_scope_guard([&](auto&&){ iconv_close(cd); }); + const auto sg = utils::make_scope_guard([&cd](auto&&){ iconv_close(cd); }); size_t inbytesleft = str.size(); @@ -168,7 +168,7 @@ namespace utils char* outbuf_ptr = outbuf; // Make sure outbuf is always deleted when we leave this function - const auto sg2 = utils::make_scope_guard([&](auto&&){ delete[] outbuf; }); + const auto sg2 = utils::make_scope_guard([outbuf](auto&&){ delete[] outbuf; }); bool done = false; while (done == false) -- cgit v1.2.3