File size: 7,816 Bytes
17f5b5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4ae1c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "Utilities.h"
#include "Constant.h"
#include <Windows.h>
#include <ShlObj.h>
#include <filesystem>
#include <fstream>
#include <iostream>

// 静态成员初始化
const std::string Utilities::WeChatDefaultPath = "C:\\Program Files\\Tencent\\WeChat";
const std::string Utilities::MmMojoDll = "mmmojo.dll";
const std::string Utilities::MmMojo64Dll = "mmmojo_64.dll";

std::string Utilities::GetWeChatDir(const std::string& path)
{
    std::string wechatPath = path.empty() ? WeChatDefaultPath : path;

    // 移除路径末尾的反斜杠
    if (!wechatPath.empty() && wechatPath.back() == '\\')
        wechatPath.pop_back();

    // 根据给定路径下 mmmojo.dll 的存在与否,判断是否为微信完整路径
    std::string dllPath = wechatPath + "\\" + MmMojoDll;
    if (std::filesystem::exists(dllPath))
        return wechatPath;

    // 从注册表中获取微信安装版本并根据参数path拼接微信安装目录
    HKEY hKey;
    if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\WeChat", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
    {
        char displayVersion[256] = { 0 };
        DWORD bufferSize = sizeof(displayVersion);
        DWORD type = REG_SZ;

        if (RegQueryValueExA(hKey, "DisplayVersion", NULL, &type, (LPBYTE)displayVersion, &bufferSize) == ERROR_SUCCESS)
        {
            std::string versionDir = wechatPath + "\\[" + displayVersion + "]";
            RegCloseKey(hKey);

            if (std::filesystem::exists(versionDir))
                return versionDir;
        }
        RegCloseKey(hKey);
    }

    return "";
}

std::string Utilities::GetWeChatOcrExePath()
{
    char appDataPath[MAX_PATH];
    if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_APPDATA, NULL, 0, appDataPath)))
    {
        std::string searchPath = std::string(appDataPath) + "\\Tencent\\WeChat\\XPlugin\\Plugins\\WeChatOCR";
        
        // 使用 Windows API 查找文件
        WIN32_FIND_DATAA findData;
        std::string searchPattern = searchPath + "\\*";
        HANDLE hFind = FindFirstFileA(searchPattern.c_str(), &findData);
        
        if (hFind != INVALID_HANDLE_VALUE)
        {
            do
            {
                if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                    if (strcmp(findData.cFileName, ".") != 0 && strcmp(findData.cFileName, "..") != 0)
                    {
                        std::string subDir = searchPath + "\\" + findData.cFileName;
                        std::string exePath = subDir + "\\WeChatOCR.exe";
                        
                        if (std::filesystem::exists(exePath))
                        {
                            FindClose(hFind);
                            return exePath;
                        }
                    }
                }
            } while (FindNextFileA(hFind, &findData));
            
            FindClose(hFind);
        }
    }
    
    return "";
}

void Utilities::WriteBytesToFile(const std::string& filePath, const unsigned char* bytes, size_t length)
{
    std::ofstream fileStream(filePath, std::ios::binary);
    if (fileStream.is_open())
    {
        fileStream.write(reinterpret_cast<const char*>(bytes), length);
        fileStream.close();
    }
}

void Utilities::CopyMmmojoDll(const std::string& wechatFullDir)
{
    char currentDir[MAX_PATH];
    GetCurrentDirectoryA(MAX_PATH, currentDir);
    
    std::string targetPath = std::string(currentDir) + "\\" + Constant::WeChatOcrData;
    
    // 创建目标目录(如果不存在)
    if (!std::filesystem::exists(targetPath))
        std::filesystem::create_directories(targetPath);
    
    std::string mmMojoFullPath = wechatFullDir + "\\" + MmMojoDll;
    std::string mmMojo64FullPath = wechatFullDir + "\\" + MmMojo64Dll;
    std::string targetMmMojoFullPath = targetPath + "\\" + MmMojoDll;
    std::string targetMmMojo64FullPath = targetPath + "\\" + MmMojo64Dll;
    
    // 复制文件(如果目标文件不存在)
    if (!std::filesystem::exists(targetMmMojoFullPath) && std::filesystem::exists(mmMojoFullPath))
        std::filesystem::copy_file(mmMojoFullPath, targetMmMojoFullPath);
    
    if (!std::filesystem::exists(targetMmMojo64FullPath) && std::filesystem::exists(mmMojo64FullPath))
        std::filesystem::copy_file(mmMojo64FullPath, targetMmMojo64FullPath);
}

bool Utilities::RemoveMmmojoDll(std::string& error)
{
    bool ret = true;
    error = "";
    
    char currentDir[MAX_PATH];
    GetCurrentDirectoryA(MAX_PATH, currentDir);
    
    std::string targetMmMojoFullPath = std::string(currentDir) + "\\" + MmMojoDll;
    std::string targetMmMojo64FullPath = std::string(currentDir) + "\\" + MmMojo64Dll;
    
    try
    {
        if (std::filesystem::exists(targetMmMojoFullPath))
            std::filesystem::remove(targetMmMojoFullPath);
        
        if (std::filesystem::exists(targetMmMojo64FullPath))
            std::filesystem::remove(targetMmMojo64FullPath);
    }
    catch (const std::exception& ex)
    {
        error = ex.what();
        ret = false;
    }
    
    return ret;
}

// Base64解码函数
std::string Utilities::Base64Decode(const std::string& encoded_string) {
    if (encoded_string.empty()) return "";

    // Base64编码表
    static const std::string base64_chars =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz"
        "0123456789+/";

    // 解码过程
    size_t in_len = encoded_string.size();
    size_t i = 0;
    size_t j = 0;
    size_t in_ = 0;
    unsigned char char_array_4[4], char_array_3[3];
    std::string ret;

    while (in_len-- && (encoded_string[in_] != '=') &&
        (isalnum(encoded_string[in_]) || (encoded_string[in_] == '+') || (encoded_string[in_] == '/'))) {
        char_array_4[i++] = encoded_string[in_]; in_++;
        if (i == 4) {
            for (i = 0; i < 4; i++)
                char_array_4[i] = base64_chars.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 += char_array_3[i];
            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] = base64_chars.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 += char_array_3[j];
    }

    return ret;
}

std::vector<unsigned char> Utilities::Base64Decode_Raw(const std::string& input) {

    const std::string BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    std::vector<int> decode_table(256, -1);
    for (int i = 0; i < 64; i++)
        decode_table[BASE64_CHARS[i]] = i; // 构建解码表[6,8](@ref)

    std::vector<unsigned char> decoded;
    int val = 0, bits = -8; // val:24位缓冲区, bits:当前有效位数
    for (unsigned char c : input) {
        if (c == '=') break;               // 遇填充符终止
        if (decode_table[c] == -1) continue; // 跳过非法字符

        val = (val << 6) + decode_table[c]; // 合并6位数据[6](@ref)
        bits += 6;
        if (bits >= 0) {                   // 每凑够8位输出1字节
            decoded.push_back((val >> bits) & 0xFF);
            bits -= 8;
        }
    }
    return decoded;
}