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

View File

@ -1,7 +1,6 @@
#include "pch.h" #include "utils.h"
#include "utils.h"
#include "pch.h"
namespace wxhelper { namespace wxhelper {
std::wstring Utils::UTF8ToWstring(const std::string &str) { std::wstring Utils::UTF8ToWstring(const std::string &str) {
@ -102,8 +101,6 @@ void Utils::Hex2Bytes(const std::string &hex, BYTE *bytes) {
} }
} }
bool Utils::IsDigit(std::string str) { bool Utils::IsDigit(std::string str) {
if (str.length() == 0) { if (str.length() == 0) {
return false; return false;
@ -119,7 +116,8 @@ bool Utils::IsDigit(std::string str) {
bool Utils::FindOrCreateDirectoryW(const wchar_t *path) { bool Utils::FindOrCreateDirectoryW(const wchar_t *path) {
WIN32_FIND_DATAW fd; WIN32_FIND_DATAW fd;
HANDLE hFind = ::FindFirstFileW(path, &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); FindClose(hFind);
return true; return true;
} }
@ -174,4 +172,19 @@ std::string Utils::WCharToUTF8(wchar_t *wstr) {
return std::string(); 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 } // namespace wxhelper

View File

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