Skip to content

Commit

Permalink
update not finished
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroAir committed Apr 22, 2024
1 parent 8bbb26f commit 45f50da
Show file tree
Hide file tree
Showing 24 changed files with 2,099 additions and 1,390 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ set(Lithium_module
# Main

add_library(lithium_server-library STATIC ${component_module} ${config_module} ${debug_module} ${module_module} ${device_module} ${task_module} ${script_module} ${Lithium_module})
target_link_libraries(lithium_server-library loguru)
add_executable(lithium_server ${lithium_src_dir}/app.cpp)
target_link_directories(lithium_server PUBLIC ${CMAKE_BINARY_DIR}/libs)

Expand All @@ -249,7 +250,7 @@ target_link_libraries(lithium_server loguru)
target_link_libraries(lithium_server libzippp)
target_link_libraries(lithium_server atomstatic)
target_link_libraries(lithium_server carbon)
target_link_libraries(lithium_server-library fmt::fmt)
target_link_libraries(lithium_server fmt::fmt)

target_compile_definitions(lithium_server PRIVATE LOGURU_DEBUG_LOGGING)

Expand Down
7 changes: 3 additions & 4 deletions src/LithiumApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ namespace Lithium {
std::shared_ptr<LithiumApp> MyApp = nullptr;

LithiumApp::LithiumApp() {
DLOG_SCOPE_FUNCTION(INFO);
DLOG_F(INFO, "LithiumApp Constructor");
try {
// Specialized Managers and Threads
Expand Down Expand Up @@ -112,10 +111,10 @@ LithiumApp::LithiumApp() {
// message
// to the right type to process.
// All of the messages are based on the Message class.
DLOG_SCOPE_F(INFO, "Start Message Processing Thread");
DLOG_F(INFO, "Start Message Processing Thread");
m_MessageBus.lock()->StartProcessingThread<Message>();

DLOG_SCOPE_F(INFO, "Register LithiumApp Member Functions");
DLOG_F(INFO, "Register LithiumApp Member Functions");

LiRegisterMemberFunc("GetConfig", &LithiumApp::GetConfig);
LiRegisterMemberFunc("SetConfig", &LithiumApp::SetConfig);
Expand All @@ -125,7 +124,7 @@ LithiumApp::LithiumApp() {
LOG_F(ERROR, "Failed to load Lithium App , error : {}", e.what());
throw std::runtime_error("Failed to load Lithium App");
}
DLOG_SCOPE_F(INFO, "Lithium App Initialized");
DLOG_F(INFO, "Lithium App Initialized");
}

LithiumApp::~LithiumApp() {
Expand Down
183 changes: 45 additions & 138 deletions src/atom/algorithm/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,176 +106,83 @@ std::string base32Decode(std::string_view encoded) {
return result;
}

std::string base64Encode(const std::vector<unsigned char> &bytes_to_encode) {
static const std::string kBase64Chars =
std::string base64Encode(std::string_view bytes_to_encode) {
static constexpr std::string_view kBase64Chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
for (const auto &byte : bytes_to_encode) {
char_array_3[i++] = byte;
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (i = 0; i < 4; i++) {
ret += kBase64Chars[char_array_4[i]];
}
i = 0;
}
}
if (i) {
for (j = i; j < 3; j++) {
char_array_3[j] = '\0';
ret.reserve((bytes_to_encode.size() + 2) / 3 * 4);

std::array<unsigned char, 3> char_array_3{};
std::array<unsigned char, 4> char_array_4{};

auto it = bytes_to_encode.begin();
auto end = bytes_to_encode.end();

while (it != end) {
int i = 0;
for (; i < 3 && it != end; ++i, ++it) {
char_array_3[i] = static_cast<unsigned char>(*it);
}

char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] =
((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] =
((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; j < i + 1; j++) {
ret += kBase64Chars[char_array_4[j]];
}
while ((i++ < 3)) {
ret += '=';
}
}
return ret;
}

std::vector<unsigned char> base64Decode(const std::string &encoded_string) {
static const std::string kBase64Chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
for (int j = 0; j < i + 1; ++j) {
ret.push_back(kBase64Chars[char_array_4[j]]);
}

int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::vector<unsigned char> ret;

while (in_len-- && (encoded_string[in_] != '=') &&
(kBase64Chars.find(encoded_string[in_]) != std::string::npos)) {
char_array_4[i++] = encoded_string[in_];
in_++;
if (i == 4) {
for (i = 0; i < 4; i++) {
char_array_4[i] = static_cast<unsigned char>(
kBase64Chars.find(char_array_4[i]));
}
char_array_3[0] =
(char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) +
((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; i < 3; i++) {
ret.push_back(char_array_3[i]);
if (i < 3) {
for (int j = i; j < 3; ++j) {
ret.push_back('=');
}
i = 0;
}
}
if (i) {
for (j = i; j < 4; j++) {
char_array_4[j] = 0;
}
for (j = 0; j < 4; j++) {
char_array_4[j] =
static_cast<unsigned char>(kBase64Chars.find(char_array_4[j]));
}
char_array_3[0] =
(char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] =
((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; j < i - 1; j++) {
ret.push_back(char_array_3[j]);
break;
}
}

return ret;
}

std::string base64EncodeEnhance(const std::vector<uint8_t> &bytes_to_encode) {
const std::string base64_chars =
std::string base64Decode(std::string_view encoded_string) {
static constexpr std::string_view kBase64Chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

std::string encoded_data;
size_t input_length = bytes_to_encode.size();
std::string ret;
ret.reserve(encoded_string.size() / 4 * 3);

for (size_t i = 0; i < input_length; i += 3) {
uint32_t padded_value = 0;
int padding_count = 0;
std::array<unsigned char, 4> char_array_4{};
std::array<unsigned char, 3> char_array_3{};

for (size_t j = 0; j < 3; ++j) {
if (i + j < input_length) {
padded_value <<= 8;
padded_value |= bytes_to_encode[i + j];
} else {
padded_value <<= 8;
++padding_count;
}
}
auto it = encoded_string.begin();
auto end = encoded_string.end();

for (int k = 0; k < 4 - padding_count; ++k) {
uint8_t index = (padded_value >> (6 * (3 - k))) & 0x3F;
encoded_data += base64_chars[index];
while (it != end) {
int i = 0;
for (; i < 4 && it != end && *it != '='; ++i, ++it) {
char_array_4[i] =
static_cast<unsigned char>(kBase64Chars.find(*it));
}

for (int k = 0; k < padding_count; ++k) {
encoded_data += '=';
}
}

return encoded_data;
}

std::vector<uint8_t> base64DecodeEnhance(const std::string &encoded_string) {
const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

std::vector<uint8_t> decoded_data;
size_t input_length = encoded_string.length();
size_t padding_count =
std::count(encoded_string.begin(), encoded_string.end(), '=');
size_t output_length = (3 * input_length) / 4 - padding_count;

uint32_t padded_value = 0;
int padding_index = 0;

for (size_t i = 0; i < input_length; ++i) {
if (encoded_string[i] == '=') {
padded_value <<= 6;
++padding_index;
} else {
uint8_t value = base64_chars.find(encoded_string[i]);
padded_value <<= 6;
padded_value |= value;
}
char_array_3[0] =
(char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] =
((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

if ((i + 1) % 4 == 0 && padding_index < padding_count) {
for (int j = 0; j < 3 - padding_index; ++j) {
uint8_t byte = (padded_value >> (16 - (j + 1) * 8)) & 0xFF;
decoded_data.push_back(byte);
}
padding_index = 0;
padded_value = 0;
for (int j = 0; j < i - 1; ++j) {
ret.push_back(static_cast<char>(char_array_3[j]));
}
}

return decoded_data;
return ret;
}

const std::string base85_chars =
Expand Down
26 changes: 3 additions & 23 deletions src/atom/algorithm/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,36 +72,16 @@ base32Decode(std::string_view encoded);
* @return std::string 编码后的字符串
*/
[[nodiscard("The result of base64Encode is not used.")]] std::string
base64Encode(const std::vector<unsigned char> &bytes_to_encode);
base64Encode(std::string_view bytes_to_encode);

/**
* @brief Base64解码函数
*
* @param encoded_string 待解码字符串
* @return std::vector<unsigned char> 解码后的数据
*/
[[nodiscard(
"The result of base64Decode is not used.")]] std::vector<unsigned char>
base64Decode(const std::string &encoded_string);

/**
* @brief Base64编码函数
*
* @param bytes_to_encode 待编码数据
* @return std::string 编码后的字符串
*/
[[nodiscard("The result of base64EncodeEnhance is not used.")]] std::string
base64EncodeEnhance(const std::vector<uint8_t> &bytes_to_encode);

/**
* @brief Base64解码函数
*
* @param encoded_string 待解码字符串
* @return std::vector<unsigned char> 解码后的数据
*/
[[nodiscard(
"The result of base64DecodeEnhance is not used.")]] std::vector<uint8_t>
base64DecodeEnhance(const std::string &encoded_string);
[[nodiscard("The result of base64Decode is not used.")]] std::string
base64Decode(std::string_view encoded_string);

/**
* @brief Encodes a vector of unsigned characters into a Base85 string.
Expand Down
Loading

0 comments on commit 45f50da

Please sign in to comment.