Skip to content

Commit

Permalink
优化 代码;
Browse files Browse the repository at this point in the history
  • Loading branch information
RealChuan committed Mar 27, 2024
1 parent 60e17c2 commit 0302cb4
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 62 deletions.
43 changes: 32 additions & 11 deletions Breakpad/breakpad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,42 @@
#include <locale>

#ifdef _WIN32
bool callback(const wchar_t *dump_path,

auto convertWideStringToUTF8(const wchar_t *wstr) -> std::string
{
if (wstr == nullptr) {
return {};
}

// 首先,获取转换后的字符串长度(不包括空终止符)
int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);

// 如果转换失败,返回空字符串
if (len == 0) {
return {};
}

// 分配足够的空间来存储转换后的字符串
std::string utf8String(len, 0);

// 执行转换
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &utf8String[0], len, nullptr, nullptr);

// 去除末尾的空字符
utf8String.resize(len - 1);
return utf8String;
}

auto callback(const wchar_t *dump_path,
const wchar_t *id,
void *context,
EXCEPTION_POINTERS *exinfo,
MDRawAssertionInfo *assertion,
bool succeeded)
bool succeeded) -> bool
{
auto succeeded_str = succeeded ? "succeeded" : "fialed";
auto convert_str = [](const wchar_t *wstr) {
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.to_bytes(wstr);
};
auto dump_path_str = convert_str(dump_path) + convert_str(id);
std::cout << "Create dump file " << succeeded_str << " Dump path: " << dump_path_str
<< std::endl;
const auto *succeeded_str = succeeded ? "succeeded" : "fialed";
auto dump_path_str = convertWideStringToUTF8(dump_path) + convertWideStringToUTF8(id);
std::cout << "Create dump file " << succeeded_str << " Dump path: " << dump_path_str << '\n';
return succeeded;
}
#elif __APPLE__
Expand Down Expand Up @@ -66,4 +87,4 @@ Breakpad::Breakpad(const std::string &dump_path)
#endif
}

Breakpad::~Breakpad() {}
Breakpad::~Breakpad() = default;
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 设定版本号
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.18)

if(CMAKE_HOST_WIN32)
set(CMAKE_TOOLCHAIN_FILE
Expand Down
4 changes: 2 additions & 2 deletions CountDownLatch/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_executable(CountDownLatch_test main.cc countdownlatch.hpp)
add_executable(countdownlatch_test main.cc countdownlatch.hpp)

if(CMAKE_HOST_UNIX)
target_link_libraries(CountDownLatch_test pthread)
target_link_libraries(countdownlatch_test pthread)
endif()
57 changes: 47 additions & 10 deletions CountDownLatch/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,53 @@
#include <iostream>
#include <thread>

struct Job
{
const std::string name;
std::string product{"not worked"};
std::thread action{};
};

void countDownLatchTest()
{
Job jobs[]{{"Annika"}, {"Buru"}, {"Chuck"}};

CountDownLatch work_done{std::size(jobs)};
CountDownLatch start_clean_up{1};

auto work = [&](Job &my_job) {
my_job.product = my_job.name + " worked";
work_done.countDown();
start_clean_up.wait();
my_job.product = my_job.name + " cleaned";
};

std::cout << "Work is starting... ";
for (auto &job : jobs) {
job.action = std::thread{work, std::ref(job)};
}

work_done.wait();
std::cout << "done:\n";
for (auto const &job : jobs) {
std::cout << " " << job.product << '\n';
}

std::cout << "Workers are cleaning up... ";
start_clean_up.countDown();
for (auto &job : jobs) {
job.action.join();
}

std::cout << "done:\n";
for (auto const &job : jobs) {
std::cout << " " << job.product << '\n';
}
}

auto main() -> int
{
CountDownLatch latch(1);
std::thread thread([&]() {
std::cout << "thread start" << std::endl;
latch.wait();
std::cout << "thread end" << std::endl;
});
std::cout << "main start" << std::endl;
latch.countDown();
thread.join();
std::cout << "main end" << std::endl;
countDownLatchTest();

return 0;
}
32 changes: 18 additions & 14 deletions Glog/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,33 @@
auto main(int argc, char **argv) -> int
{
(void) argc;
(void) argv;

const auto *log_dir = "./glog_demo";

// Initialize Google’s logging library.
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
google::SetLogFilenameExtension(".log");
google::EnableLogCleaner(3);
google::EnableLogCleaner(7);
//google::DisableLogCleaner();
FLAGS_alsologtostderr = true; //是否将日志输出到文件和stderr
FLAGS_colorlogtostderr = true; //是否启用不同颜色显示
fLS::FLAGS_log_dir = "./Log";
FLAGS_alsologtostderr = true; // 是否将日志输出到文件和stderr
FLAGS_colorlogtostderr = true; // 是否启用不同颜色显示
FLAGS_max_log_size = 1000; // 最大日志文件大小
fLS::FLAGS_log_dir = log_dir;

std::filesystem::create_directories(log_dir);

std::filesystem::create_directories("./Log");
std::string message("Hello World");

LOG(INFO) << "INFO_LOG Hello, world!";
LOG(WARNING) << "WARNING_LOG Hello, world!";
LOG(ERROR) << "ERROR_LOG Hello, world!";
//LOG(FATAL) << "FATAL_LOG Hello, world!";
LOG(INFO) << message;
LOG(WARNING) << message;
LOG(ERROR) << message;
// LOG(FATAL) << message;

DLOG(INFO) << "DINFO_LOG Hello, world!";
DLOG(WARNING) << "DWARNING_LOG Hello, world!";
DLOG(ERROR) << "DERROR_LOG Hello, world!";
//DLOG(FATAL) << "DFATAL_LOG Hello, world!";
DLOG(INFO) << message;
DLOG(WARNING) << message;
DLOG(ERROR) << message;
// DLOG(FATAL) << message;

google::ShutdownGoogleLogging();
return 0;
Expand Down
77 changes: 53 additions & 24 deletions OpenSSL/openssl_x509.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,94 @@
void x509_generate_certificate(const std::string &x509Path, const std::string &pkeyPath)
{
// 生成私钥
EVP_PKEY *pkey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(pkey, RSA_generate_key(2048, RSA_F4, NULL, NULL));
auto *pKey = EVP_RSA_gen(2048);

// 生成证书
X509 *x509 = X509_new();
X509_set_version(x509, 2);
ASN1_INTEGER_set(X509_get_serialNumber(x509), 1);
X509_gmtime_adj(X509_get_notBefore(x509), 0);
X509_gmtime_adj(X509_get_notAfter(x509), 31536000L);
X509_set_pubkey(x509, pkey);
X509_set_pubkey(x509, pKey);

// 设置证书信息
X509_NAME *name = X509_get_subject_name(x509);
X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (const unsigned char *) "CN", -1, -1, 0);
X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (const unsigned char *) "CN", -1, -1, 0);
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (const unsigned char *) "CN", -1, -1, 0);
X509_NAME_add_entry_by_txt(name,
"C",
MBSTRING_ASC,
reinterpret_cast<const unsigned char *>("CN"),
-1,
-1,
0);
X509_NAME_add_entry_by_txt(name,
"O",
MBSTRING_ASC,
reinterpret_cast<const unsigned char *>("CN"),
-1,
-1,
0);
X509_NAME_add_entry_by_txt(name,
"CN",
MBSTRING_ASC,
reinterpret_cast<const unsigned char *>("CN"),
-1,
-1,
0);
X509_set_issuer_name(x509, name);

// 签名证书
X509_sign(x509, pkey, EVP_sha1());
X509_sign(x509, pKey, EVP_sha1());

// 保存证书
FILE *fp = fopen(x509Path.c_str(), "wb");
PEM_write_X509(fp, x509);
fclose(fp);
auto *out = BIO_new_file(x509Path.c_str(), "w");
if (out != nullptr) {
PEM_write_bio_X509(out, x509);
BIO_free(out);
}

// 保存私钥
fp = fopen(pkeyPath.c_str(), "wb");
PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
fclose(fp);
out = BIO_new_file(pkeyPath.c_str(), "w");
if (out != nullptr) {
PEM_write_bio_PrivateKey(out, pKey, nullptr, nullptr, 0, nullptr, nullptr);
BIO_free(out);
}

// 释放资源
X509_free(x509);
EVP_PKEY_free(pkey);
EVP_PKEY_free(pKey);
}

// x509 读取证书 并验证
void x509_read_certificate(const std::string &x509Path, const std::string &pkeyPath)
{
// 读取证书
FILE *fp = fopen(x509Path.c_str(), "rb");
X509 *x509 = PEM_read_X509(fp, NULL, NULL, NULL);
fclose(fp);
auto *in = BIO_new_file(x509Path.c_str(), "r");
if (in == nullptr) {
std::cerr << "read certificate failed" << '\n';
return;
}
X509 *x509 = PEM_read_bio_X509(in, nullptr, nullptr, nullptr);
BIO_free(in);

// 读取私钥
fp = fopen(pkeyPath.c_str(), "rb");
EVP_PKEY *pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
fclose(fp);
in = BIO_new_file(pkeyPath.c_str(), "r");
if (in == nullptr) {
std::cerr << "read pkey failed" << '\n';
return;
}
EVP_PKEY *pKey = PEM_read_bio_PrivateKey(in, nullptr, nullptr, nullptr);
BIO_free(in);

// 验证证书
if (X509_verify(x509, pkey) == 1) {
std::cout << "verify success" << std::endl;
if (X509_verify(x509, pKey) == 1) {
std::cout << "verify success" << '\n';
} else {
std::cout << "verify failed" << std::endl;
std::cout << "verify failed" << '\n';
}

// 释放资源
X509_free(x509);
EVP_PKEY_free(pkey);
EVP_PKEY_free(pKey);
}

auto main() -> int
Expand Down

0 comments on commit 0302cb4

Please sign in to comment.