⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbproviderbase.cpp

📁 SecurityLib一个user-rights系统控件
💻 CPP
字号:
//---------------------------------------------------------------------------
#pragma inline

#include <vcl.h>
#pragma hdrstop

#include "DBProviderBase.h"
#include "SecurityEncryption.h"





namespace Security {

PswdString DBProviderBase::AddrKey;


void __fastcall DBProviderBase::Destruct(TSecObjData *od) { delete od->Object; }


DBProviderBase *__stdcall DBProviderBase::Handle(const char *dbeng, SecOFCmd cmd, TSecObjData *od) {
	if (cmd != ofcCheck && !CheckAddress()) return 0;
	if (cmd == ofcCheck) {
		AddrKey = od->Key();

		if (CheckAddress(od->Key()))
			return (DBProviderBase*)1;
		else
			return 0;
	}
	if (cmd == ofcGetDBProvider)
		for(word i=0; i<DBEnginesCount; ++i)
			if (!strcmp(dbeng, DBEnginesNames[i]) ) {
				TClass c = DBEnginesClasses[i];
				DBProviderBase *p;
				// explicit call to virtual constructor  - I assume it's always at the beggining of the vtbl
				asm {  // has to preserve EDI, ESI, ESP, EBP, and EBX
					push ebx;

					mov dl, 1;    // constructor should allocate memory
					mov ecx, dbeng;  // parameter
					mov eax, dword ptr [c];  // address of constructor
					mov ebx, dword ptr [eax]; // ^
					mov eax, c; // pass vtable pointer
					call ebx;
					mov p, eax;   // get the pointer to new object

					pop ebx;
				}

				p->Security = od->Security;

				return p;

			}
	if (cmd == ofcDelDBProvider)
		delete od->Object;


	return 0;
}


inline void DBProviderBase::SetPath() {
	char drive[_MAX_DRIVE];
	char dir[_MAX_DIR];
	char file[_MAX_FNAME];
	char ext[_MAX_EXT];
	 _splitpath(ParamStr(0).c_str() ,drive,dir,file,ext);

	 Path = AnsiString(drive) + dir;
}



DBProviderBase *DBProviderBase::LoadDBProvider(const char *filename, const char * dbeng, void *data) {
	if (Path == ":") SetPath();

	if (!Counter) {

		ChildProv = LoadLibraryEx((Path + filename).c_str(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
		if (ChildProv) {
			void * __stdcall (*tmpf)() = ( void * __stdcall (*)() )GetProcAddress(ChildProv, "_SecObjectFactory");
			if (tmpf) {
				TSecObjectFactory ObjectF = (TSecObjectFactory)(tmpf());
				if (ObjectF && ObjectF(dbeng, ofcCheck, &TSecObjData(Security, EncodeWithDBProviderKey(FillUpPointer(ObjectF))) )) {
					// now lib knows, that can give away the address of DBProvider object
					DBProviderBase *p = (DBProviderBase *)ObjectF(dbeng, ofcGetDBProvider, &TSecObjData(Security));

					PswdString rk = RandomKey();
					if (p && DecodeWithDBProviderKey(p->Validate(rk)) == rk) {
						(DBProv = p)->Init(data);
						++Counter;
						return p;
					} else
						return DBProv =0;

				}
			}
		}
		return 0;
	} else
		return DBProv;
}


void __fastcall DBProviderBase::UnloadDBProvider(DBProviderBase *) {
	if (!--Counter)
		FreeLibrary(ChildProv);

}




} // namespace Security;


//---------------------------------------------------------------------------

#pragma package(smart_init)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -