DbExecuteSql.cpp 7.5 KB
Newer Older
L
ljc545w 已提交
1 2
#include "pch.h"

L
ljc545w 已提交
3
// sqlite3_callback函数指针
L
ljc545w 已提交
4 5 6 7 8 9 10
typedef int(*sqlite3_callback)(
	void*,
	int,
	char**,
	char**
);

L
ljc545w 已提交
11
// sqlite3_exec函数指针
L
ljc545w 已提交
12 13 14 15 16 17 18 19 20
typedef int(__cdecl* Sqlite3_exec)(
	DWORD,					/* The database on which the SQL executes */
	const char*,            /* The SQL to be executed */
	sqlite3_callback,       /* Invoke this callback routine */
	void*,                  /* First argument to xCallback() */
	char**                  /* Write error messages here */
);

DWORD WeChatWinBase = GetWeChatWinBase();
L
ljc545w 已提交
21
// sqlite3_exec函数地址
L
ljc545w 已提交
22
DWORD sqlite3_execAddr = WeChatWinBase + OffsetFromIdaAddr(IDA_SQLITE3_EXEC_ADDRESS);
L
ljc545w 已提交
23

L
ljc545w 已提交
24 25 26 27 28
/*
* 外部调用时传递的参数结构
* ptrDb:数据库句柄
* ptrSql:保存sql的地址
*/
L
ljc545w 已提交
29 30 31 32 33
struct executeParams {
	DWORD ptrDb;
	DWORD ptrSql;
};

L
ljc545w 已提交
34 35 36 37 38
/*
* 保存查询结果的结构
* ColName:字段名;l_ColName:`ColName`字符数
* content:字段值;l_content:`content`字符数
*/
L
ljc545w 已提交
39 40 41 42 43
struct SQLResultStruct {
	char* ColName;
	DWORD l_ColName;
	char* content;
	DWORD l_content;
L
ljc545w 已提交
44
	BOOL  isblob;
L
ljc545w 已提交
45 46
};

L
ljc545w 已提交
47 48 49 50 51
/*
* 外部调用时的返回类型
* SQLResultAddr:`SQLResult`首成员地址
* length:查询结果条数
*/
L
ljc545w 已提交
52 53 54 55 56
struct executeResult {
	DWORD SQLResultAddr;
	DWORD length;
};

L
ljc545w 已提交
57
// 外部调用时的具体返回对象
L
ljc545w 已提交
58
executeResult result = { 0 };
L
ljc545w 已提交
59
// 保存查询结果的二维动态数组
L
ljc545w 已提交
60 61
vector <vector<SQLResultStruct>> SQLResult;

L
ljc545w 已提交
62 63 64
/*
* 获取数据库信息的回调函数
*/
L
ljc545w 已提交
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
int GetDbInfo(void* data,int argc,char** argv,char** azColName) {
	DbInfoStruct* pdata = (DbInfoStruct*)data;
	TableInfoStruct tb = { 0 };
	if (argv[1])
	{
		tb.name = new char[strlen(argv[1]) + 1];
		memcpy(tb.name,argv[1],strlen(argv[1]) + 1);
	}
	else {
		tb.name = (char*)"NULL";
	}
	if (argv[2])
	{
		tb.tbl_name = new char[strlen(argv[2]) + 1];
		memcpy(tb.tbl_name, argv[2], strlen(argv[2]) + 1);
	}
	else {
		tb.tbl_name = (char*)"NULL";
	}
	if (argv[3])
	{
		tb.rootpage = new char[strlen(argv[3]) + 1];
		memcpy(tb.rootpage, argv[3], strlen(argv[3]) + 1);
	}
	else {
		tb.rootpage = (char*)"NULL";
	}
	if (argv[4])
	{
		tb.sql = new char[strlen(argv[4]) + 1];
		memcpy(tb.sql, argv[4], strlen(argv[4]) + 1);
	}
	else {
		tb.sql = (char*)"NULL";
	}
	tb.l_name = strlen(tb.name);
	tb.l_tbl_name = strlen(tb.tbl_name);
	tb.l_sql = strlen(tb.sql);
	tb.l_rootpage = strlen(tb.rootpage);
	pdata->tables.push_back(tb);
	pdata->count = pdata->tables.size();
	return 0;
}

L
ljc545w 已提交
109 110 111
/*
* DLL内部查询用的回调函数,直接显示查询结果,用处不大
*/
L
ljc545w 已提交
112 113 114 115 116 117 118 119 120
int query(void* data, int argc, char** argv, char** azColName) {
	for (int i = 0; i < argc; i++) {
		string content = argv[i] ? UTF8ToGBK(argv[i]) : "NULL";
		cout << azColName[i] << " = " << content << endl;
	}
	printf("\n");
	return 0;
}

L
ljc545w 已提交
121 122 123 124
/*
* 外部调用时使用的回调函数,将结果存入`SQLResult`中
* return:int,执行成功返回`0`,执行失败返回非0值
*/
L
ljc545w 已提交
125
int selectdbinfo(void* data, int argc, char** argv, char** azColName) {
L
ljc545w 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	executeResult* pdata = (executeResult*)data;
	vector<SQLResultStruct> tempStruct;
	for (int i = 0; i < argc; i++) {
		SQLResultStruct temp = { 0 };
		temp.ColName = new char[strlen(azColName[i]) + 1];
		memcpy(temp.ColName, azColName[i], strlen(azColName[i]) + 1);
		temp.l_ColName = strlen(azColName[i]);
		if (argv[i]) {
			temp.content = new char[strlen(argv[i]) + 1];
			memcpy(temp.content, argv[i], strlen(argv[i]) + 1);
			temp.l_content = strlen(argv[i]);
		}
		else {
			temp.content = new char[2];
			ZeroMemory(temp.content, 2);
			temp.l_content = 0;
		}
		tempStruct.push_back(temp);
	}
	SQLResult.push_back(tempStruct);
	pdata->length++;
	return 0;
}

L
ljc545w 已提交
150 151 152 153
/*
* 清空查询结果,释放内存
* return:void
*/
L
ljc545w 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
void ClearResultArray() {
	if (SQLResult.size() == 0)
		return;
	for(unsigned int i = 0; i < SQLResult.size(); i++) {
		for (unsigned j = 0; j < SQLResult[i].size(); j++) {
			SQLResultStruct* sr = (SQLResultStruct*)&SQLResult[i][j];
			if (sr->ColName) {
				delete sr->ColName;
				sr->ColName = NULL;
			}
			if (sr->content) {
				delete sr->content;
				sr->content = NULL;
			}
		}
		SQLResult[i].clear();
	}
	SQLResult.clear();
	result.SQLResultAddr = 0;
	result.length = 0;
}

L
ljc545w 已提交
176 177 178 179 180 181 182 183
/*
* 执行SQL的入口函数
* ptrDb:数据库句柄
* sql:要执行的SQL
* callback:回调函数地址
* data:传递给回调函数的参数
* return:BOOL,执行成功返回`1`,执行失败返回`0`
*/
L
ljc545w 已提交
184 185 186 187 188 189
BOOL ExecuteSQL(DWORD ptrDb,const char* sql,DWORD callback,void* data) {
	Sqlite3_exec p_Sqlite3_exec = (Sqlite3_exec)sqlite3_execAddr;
	int status = p_Sqlite3_exec(ptrDb,sql, (sqlite3_callback)callback,data,0);
	return status == 0;
}

L
ljc545w 已提交
190 191 192 193 194
/*
* 供外部调用的执行SQL接口
* lpParameter:`executeParams`类型结构体指针
* return:DWORD,如果SQL执行成功,返回`SQLResult`首成员地址,否则返回0
*/
L
ljc545w 已提交
195 196 197
DWORD ExecuteSQLRemote(LPVOID lpParameter){
	ClearResultArray();
	executeParams* sqlparam = (executeParams*)lpParameter;
L
ljc545w 已提交
198
	BOOL status = ExecuteSQL(sqlparam->ptrDb, (const char*)sqlparam->ptrSql, (DWORD)selectdbinfo, &result);
L
ljc545w 已提交
199
	
L
ljc545w 已提交
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	if (status) {
		result.SQLResultAddr = (DWORD)SQLResult.data();
		return (DWORD)&result;
	}
	else {
		result.length = 0;
	}
	return 0;
}

static BOOL SelectData(DWORD db,const char* sql,void* data)
{
	executeResult* pdata = (executeResult*)data;
	DWORD wxBaseAddress = GetWeChatWinBase();
	Sqlite3_prepare p_Sqlite3_prepare = (Sqlite3_prepare)(wxBaseAddress + OffsetFromIdaAddr(IDA_SQLITE3_PREPARE_ADDRESS));
	Sqlite3_step p_Sqlite3_step = (Sqlite3_step)(wxBaseAddress + OffsetFromIdaAddr(IDA_SQLITE3_STEP_ADDRESS));
	Sqlite3_column_count p_Sqlite3_column_count = (Sqlite3_column_count)(wxBaseAddress + OffsetFromIdaAddr(IDA_SQLITE3_COLUMN_COUNT_ADDRESS));
	Sqlite3_column_name p_Sqlite3_column_name = (Sqlite3_column_name)(wxBaseAddress + OffsetFromIdaAddr(IDA_SQLITE3_COLUMN_NAME_ADDRESS));
	Sqlite3_column_type p_Sqlite3_column_type = (Sqlite3_column_type)(wxBaseAddress + OffsetFromIdaAddr(IDA_SQLITE3_COLUMN_TYPE_ADDRESS));
	Sqlite3_column_blob p_Sqlite3_column_blob = (Sqlite3_column_blob)(wxBaseAddress + OffsetFromIdaAddr(IDA_SQLITE3_COLUMN_BLOB_ADDRESS));
	Sqlite3_column_bytes p_Sqlite3_column_bytes = (Sqlite3_column_bytes)(wxBaseAddress + OffsetFromIdaAddr(IDA_SQLITE3_COLUMN_BYTES_ADDRESS));
	Sqlite3_finalize p_Sqlite3_finalize = (Sqlite3_finalize)(wxBaseAddress + OffsetFromIdaAddr(IDA_SQLITE3_FINALIZE_ADDRESS));
	DWORD* stmt;
	int rc = p_Sqlite3_prepare(db, sql, -1, &stmt, 0);
	if (rc != SQLITE_OK)
		return rc;
	while (p_Sqlite3_step(stmt) == SQLITE_ROW)
	{
		int col_count = p_Sqlite3_column_count(stmt);
		vector<SQLResultStruct> tempStruct;
		for (int i = 0; i < col_count; i++) {
			SQLResultStruct temp = { 0 };
			const char* ColName = p_Sqlite3_column_name(stmt, i);
			int nType = p_Sqlite3_column_type(stmt, i);
			const void* pReadBlobData = p_Sqlite3_column_blob(stmt, i);
			int nLength = p_Sqlite3_column_bytes(stmt, i);
			temp.ColName = new char[strlen(ColName) + 1];
			memcpy(temp.ColName, ColName, strlen(ColName) + 1);
			temp.l_ColName = strlen(ColName);
			temp.l_content = nLength;
			switch (nType)
			{
			case SQLITE_BLOB: {
				temp.content = new char[nLength];
				memcpy(temp.content, pReadBlobData, nLength);
				temp.isblob = true;
				break;
			}
			default: {
				if (nLength != 0) {
					temp.content = new char[nLength + 1];
					memcpy(temp.content, pReadBlobData, nLength + 1);
				}
				else {
					temp.content = new char[2];
					ZeroMemory(temp.content, 2);
				}
				temp.isblob = false;
				break;
			}
			}
			tempStruct.push_back(temp);
		}
		SQLResult.push_back(tempStruct);
		pdata->length++;
	}
	p_Sqlite3_finalize(stmt);
	return rc == 0;
}

int SelectDataRemote(LPVOID lpParameter) {
	ClearResultArray();
	executeParams* sqlparam = (executeParams*)lpParameter;
	BOOL status = SelectData(sqlparam->ptrDb, (const char*)sqlparam->ptrSql, &result);

L
ljc545w 已提交
275 276 277 278 279 280 281 282 283
	if (status) {
		result.SQLResultAddr = (DWORD)SQLResult.data();
		return (DWORD)&result;
	}
	else {
		result.length = 0;
	}
	return 0;
}