diff --git a/common/include/villas/advio.h b/common/include/villas/advio.h index 46077fd40..53736eae5 100644 --- a/common/include/villas/advio.h +++ b/common/include/villas/advio.h @@ -26,7 +26,7 @@ #include -#include +#include struct advio { CURL *curl; diff --git a/common/include/villas/crypt.h b/common/include/villas/crypt.h deleted file mode 100644 index 94cf5be26..000000000 --- a/common/include/villas/crypt.h +++ /dev/null @@ -1,33 +0,0 @@ -/** Crypto helpers. - * - * @author Steffen Vogel - * @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC - * @license GNU General Public License (version 3) - * - * VILLAScommon - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - *********************************************************************************/ - -#pragma once - -#include -#include - -/** Calculate SHA1 hash of complete file \p f and place it into \p sha1. - * - * @param sha1[out] Must be SHA_DIGEST_LENGTH (20) in size. - * @retval 0 Everything was okay. - */ -int sha1sum(FILE *f, unsigned char *sha1); diff --git a/common/lib/CMakeLists.txt b/common/lib/CMakeLists.txt index 1d875435b..e932c1b41 100644 --- a/common/lib/CMakeLists.txt +++ b/common/lib/CMakeLists.txt @@ -25,7 +25,6 @@ add_library(villas-common SHARED buffer.cpp json_buffer.cpp compat.cpp - crypt.cpp hist.cpp dsp/pid.cpp kernel/kernel.cpp diff --git a/common/lib/advio.cpp b/common/lib/advio.cpp index 9dd2491c1..118a5f363 100644 --- a/common/lib/advio.cpp +++ b/common/lib/advio.cpp @@ -35,7 +35,6 @@ #include #include #include -#include #define BAR_WIDTH 60 /**< How wide you want the progress meter to be. */ diff --git a/common/lib/crypt.cpp b/common/lib/crypt.cpp deleted file mode 100644 index a88897c5e..000000000 --- a/common/lib/crypt.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/** Crypto helpers. - * - * @author Steffen Vogel - * @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC - * @license GNU General Public License (version 3) - * - * VILLAScommon - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - *********************************************************************************/ - -#include - -int sha1sum(FILE *f, unsigned char *sha1) -{ - SHA_CTX c; - char buf[512]; - ssize_t bytes; - long seek; - - seek = ftell(f); - fseek(f, 0, SEEK_SET); - - SHA1_Init(&c); - - bytes = fread(buf, 1, 512, f); - while (bytes > 0) { - SHA1_Update(&c, buf, bytes); - bytes = fread(buf, 1, 512, f); - } - - SHA1_Final(sha1, &c); - - fseek(f, seek, SEEK_SET); - - return 0; -} - diff --git a/common/lib/utils.cpp b/common/lib/utils.cpp index 24569795a..ce72ecaeb 100644 --- a/common/lib/utils.cpp +++ b/common/lib/utils.cpp @@ -36,6 +36,10 @@ #include #include +#include +#include +#include + #include #include #include @@ -47,15 +51,14 @@ static pthread_t main_thread; namespace villas { namespace utils { -std::vector -tokenize(std::string s, std::string delimiter) +std::vector tokenize(std::string s, std::string delimiter) { std::vector tokens; size_t lastPos = 0; size_t curentPos; - while((curentPos = s.find(delimiter, lastPos)) != std::string::npos) { + while ((curentPos = s.find(delimiter, lastPos)) != std::string::npos) { const size_t tokenLength = curentPos - lastPos; tokens.push_back(s.substr(lastPos, tokenLength)); @@ -64,7 +67,7 @@ tokenize(std::string s, std::string delimiter) } /* Check if there's a last token behind the last delimiter. */ - if(lastPos != s.length()) { + if (lastPos != s.length()) { const size_t lastTokenLength = s.length() - lastPos; tokens.push_back(s.substr(lastPos, lastTokenLength)); } @@ -328,5 +331,82 @@ size_t strlenp(const char *str) return sz; } +int sha1sum(FILE *f, unsigned char *sha1) +{ + SHA_CTX c; + char buf[512]; + ssize_t bytes; + long seek; + + seek = ftell(f); + fseek(f, 0, SEEK_SET); + + SHA1_Init(&c); + + bytes = fread(buf, 1, 512, f); + while (bytes > 0) { + SHA1_Update(&c, buf, bytes); + bytes = fread(buf, 1, 512, f); + } + + SHA1_Final(sha1, &c); + + fseek(f, seek, SEEK_SET); + + return 0; +} + +namespace base64 { + +std::string encode(const std::string &str) +{ + return encode((unsigned char *) str.data(), str.size()); +} + +std::string decode(const std::string &str) +{ + return decode((unsigned char *) str.data(), str.size()); +} + +std::string encode(const unsigned char *input, size_t len) +{ + BIO *bmem, *b64; + BUF_MEM *bptr; + + b64 = BIO_new(BIO_f_base64()); + bmem = BIO_new(BIO_s_mem()); + b64 = BIO_push(b64, bmem); + BIO_write(b64, input, len); + BIO_flush(b64); + BIO_get_mem_ptr(b64, &bptr); + + std::string str(bptr->data, bptr->length); + + BIO_free_all(b64); + + return str; +} + +std::string decode(unsigned char *input, size_t len) +{ + BIO *b64, *bmem; + + std::string str(len, 0); + + char *buffer = (char *) malloc(len); + memset(buffer, 0, len); + + b64 = BIO_new(BIO_f_base64()); + bmem = BIO_new_mem_buf(input, len); + bmem = BIO_push(b64, bmem); + + BIO_read(bmem, str.data(), str.capacity()); + + BIO_free_all(bmem); + + return buffer; +} + +} /* namespace base64 */ } /* namespace utils */ } /* namespace villas */ diff --git a/common/tests/unit/utils.cpp b/common/tests/unit/utils.cpp index 4949a23c1..528d7226a 100644 --- a/common/tests/unit/utils.cpp +++ b/common/tests/unit/utils.cpp @@ -22,7 +22,6 @@ #include -#include #include #include #include