fix: 修复部分字段未知的编码方式,转换成base64 close #51

This commit is contained in:
hugy 2023-04-24 20:37:41 +08:00
parent dffdbfb9d7
commit 6105789fcc
3 changed files with 33 additions and 9 deletions

View File

@ -5,6 +5,7 @@
#include "easylogging++.h"
#include "wechat_function.h"
#include "utils.h"
using namespace std;
namespace wxhelper {
@ -117,8 +118,16 @@ int DB::Select(DWORD db_hanle, const char *sql,
vector<string> item;
for (size_t i = 0; i < it.size(); i++) {
if (!it[i].is_blob) {
string content(it[i].content);
item.push_back(content);
bool is_utf8 = Utils::IsTextUtf8(it[i].content, it[i].content_len);
if (is_utf8) {
string content(it[i].content);
item.push_back(content);
} else {
string b64_str =
base64_encode((BYTE *)it[i].content, it[i].content_len);
item.push_back(b64_str);
}
} else {
string b64_str =
base64_encode((BYTE *)it[i].content, it[i].content_len);

View File

@ -1,7 +1,6 @@
#include "pch.h"
#include "utils.h"
#include "utils.h"
#include "pch.h"
namespace wxhelper {
std::wstring Utils::UTF8ToWstring(const std::string &str) {
@ -19,7 +18,7 @@ std::wstring Utils::AnsiToWstring(const std::string &input, DWORD locale) {
MultiByteToWideChar(CP_UTF8, 0, input.c_str(), -1, &temp[0], wchar_len);
return std::wstring(&temp[0]);
}
return std::wstring();
}
@ -102,8 +101,6 @@ void Utils::Hex2Bytes(const std::string &hex, BYTE *bytes) {
}
}
bool Utils::IsDigit(std::string str) {
if (str.length() == 0) {
return false;
@ -119,7 +116,8 @@ bool Utils::IsDigit(std::string str) {
bool Utils::FindOrCreateDirectoryW(const wchar_t *path) {
WIN32_FIND_DATAW fd;
HANDLE hFind = ::FindFirstFileW(path, &fd);
if (hFind != INVALID_HANDLE_VALUE && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
if (hFind != INVALID_HANDLE_VALUE &&
(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
FindClose(hFind);
return true;
}
@ -174,4 +172,19 @@ std::string Utils::WCharToUTF8(wchar_t *wstr) {
return std::string();
}
bool Utils::IsTextUtf8(const char *str, int len) {
int needed = MultiByteToWideChar(CP_UTF8, 0, str, len, NULL, 0);
wchar_t *wide_str = new wchar_t[needed];
MultiByteToWideChar(CP_UTF8, 0, str, len, wide_str, needed);
char *new_str = new char[len + 1];
WideCharToMultiByte(CP_ACP, 0, wide_str, -1, new_str, len + 1, NULL, NULL);
bool isUtf8 = strcmp(str, new_str) == 0;
delete[] wide_str;
delete[] new_str;
return isUtf8;
}
} // namespace wxhelper

View File

@ -47,6 +47,8 @@ class Utils {
static std::string WCharToUTF8(wchar_t *wstr);
static bool IsTextUtf8(const char *str, int len);
template <typename T1, typename T2>
static std::vector<T1> split(T1 str, T2 letter) {
std::vector<T1> arr;