/
Shymer123
/
Messenger
Обзор
Документация
Войти
/
Shymer123
/
Messenger
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Server/SourceFiles/Utils/Utils.cpp
138 строк
4 KB
Shymer123
added support for chunk acceptance of files on the server
23 мар 2025, 15:25
23 мар 2025, 15:25
36381b9
Код
Авторство
О чём код?
#include "Utils.h" #include "Logger/Logger.h" #include <openssl/bio.h> #include <openssl/evp.h> #include <openssl/buffer.h> #include <cstring> #include <random> #include <zlib.h> namespace Utils { std::vector<char> compressData(const std::vector<char>& data) { uint64_t compressedSize = compressBound(data.size()); std::vector<char> compressedData(compressedSize); int result = compress(reinterpret_cast<Bytef*>(compressedData.data()), &compressedSize, reinterpret_cast<const Bytef*>(data.data()), data.size()); if(result != Z_OK) { Logger::getInstance().logError("Compression failed", Logger::logLevel::Error); return std::vector<char>(); } compressedData.resize(compressedSize); return compressedData; } std::vector<char> decompressData(const std::vector<char>& data) { if (data.empty()) { Logger::getInstance().logError("Decompression failed: input data is empty", Logger::logLevel::Error); return {}; } uLongf decompressedSize = data.size() * 4; std::vector<char> decompressedData(decompressedSize); int result; while ((result = uncompress(reinterpret_cast<Bytef*>(decompressedData.data()), &decompressedSize, reinterpret_cast<const Bytef*>(data.data()), data.size())) == Z_BUF_ERROR) { decompressedSize *= 2; decompressedData.resize(decompressedSize); } if(result != Z_OK) { Logger::getInstance().logError("Decompression failed: error code " + std::to_string(result), Logger::logLevel::Error); return {}; } decompressedData.resize(decompressedSize); return decompressedData; } std::string encodeBase64(const std::string& input) { BIO* bio, *b64; BUF_MEM* bufferPtr; bio = BIO_new(BIO_s_mem()); b64 = BIO_new(BIO_f_base64()); bio = BIO_push(b64, bio); BIO_write(bio, input.c_str(), input.length()); BIO_flush(bio); BIO_get_mem_ptr(bio, &bufferPtr); std::string result(bufferPtr->data, bufferPtr->length); BIO_free_all(bio); return result; } std::vector<char> decodeBase64(const std::vector<char>& input) { BIO* bio, *b64; BUF_MEM* bufferPtr; b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); bio = BIO_new_mem_buf(input.data(), input.size()); bio = BIO_push(b64, bio); BIO_get_mem_ptr(bio, &bufferPtr); std::vector<char> buffer(bufferPtr->length); int length = BIO_read(bio, buffer.data(), buffer.size()); BIO_free_all(bio); if (length <= 0) { Logger::getInstance().logError("BIO_read failed in Base64 decoding", Logger::logLevel::Error); return {}; } return buffer; } std::string generateUniqueID(size_t length) { static thread_local std::mt19937 gen(std::random_device{}()); const std::string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; std::uniform_int_distribution<> distrib(0, chars.size() - 1); std::string id; id.reserve(length); for(size_t i = 0; i < length; ++i) { id += chars[distrib(gen)]; } return id; } std::vector<std::string> splitByDelimiter(const std::string& data, const std::string& delimiter) { std::vector<std::string> result; size_t start = 0; size_t end = data.find(delimiter); while (end != std::string::npos) { if (end > start) { result.push_back(data.substr(start, end - start)); } start = end + delimiter.length(); end = data.find(delimiter, start); } if (start < data.length()) { result.push_back(data.substr(start)); } return result; } } // namespace Utils