2015-07-22 20:32:54 +02:00
|
|
|
|
#ifndef INCLUDE_DLL_H
|
|
|
|
|
#define INCLUDE_DLL_H
|
2015-08-04 17:48:36 +02:00
|
|
|
|
#print "[include <dll.h>]\n"
|
2015-07-22 20:32:54 +02:00
|
|
|
|
|
2015-07-30 17:49:09 +02:00
|
|
|
|
#ifndef INCLUDE_FILESYSTEM_H
|
2018-04-03 15:57:56 +02:00
|
|
|
|
#include "../lib/fs.h"
|
2015-07-29 22:43:05 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
2015-07-30 17:49:09 +02:00
|
|
|
|
#ifdef LANG_RUS
|
|
|
|
|
#define _TEXT_ERROR_ADD "'<27>訡<EFBFBD><E8A8A1> <20><><EFBFBD> <20><><EFBFBD><EFBFBD>㧪<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⥪<EFBFBD>"
|
|
|
|
|
#elif LANG_EST
|
|
|
|
|
#define _TEXT_ERROR_ADD "'Viga teegi laadimisel"
|
|
|
|
|
#else
|
|
|
|
|
#define _TEXT_ERROR_ADD "'Error while loading library"
|
|
|
|
|
#endif
|
2015-07-29 22:43:05 +02:00
|
|
|
|
|
2012-11-26 16:26:15 +01:00
|
|
|
|
char a_libdir[43] = "/sys/lib/\0";
|
|
|
|
|
|
2015-11-05 21:49:42 +01:00
|
|
|
|
dword GLOBAL_FUNC_LIB = 0;
|
|
|
|
|
dword G_FUNC_LOAD = 0;
|
|
|
|
|
dword G_FUNC_GET = 0;
|
|
|
|
|
dword PATH_LIBRARY_LOADING = "/rd/1/lib/library.obj";
|
|
|
|
|
|
2015-07-16 03:44:30 +02:00
|
|
|
|
:inline void error_init(dword text)
|
|
|
|
|
{
|
|
|
|
|
dword TEXT_ERROR = malloc(1024);
|
2015-08-07 00:37:34 +02:00
|
|
|
|
sprintf(TEXT_ERROR, "%s `%s`' -E",_TEXT_ERROR_ADD,text);
|
2015-07-16 03:44:30 +02:00
|
|
|
|
notify(TEXT_ERROR);
|
|
|
|
|
free(TEXT_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-09 20:10:04 +01:00
|
|
|
|
// stdcall with 1 parameter
|
2015-11-05 21:49:42 +01:00
|
|
|
|
:void dll_Load() {
|
2014-02-09 20:10:04 +01:00
|
|
|
|
asm {
|
|
|
|
|
push ebp
|
|
|
|
|
mov ebp, esp
|
|
|
|
|
mov esi, SSDWORD[EBP+8]
|
2015-07-16 03:44:30 +02:00
|
|
|
|
@next_lib:
|
2014-02-09 20:10:04 +01:00
|
|
|
|
mov edx, DSDWORD[ESI]
|
|
|
|
|
or edx, edx
|
|
|
|
|
jz exit
|
|
|
|
|
push esi
|
|
|
|
|
mov esi, DSDWORD[ESI+4]
|
|
|
|
|
mov edi, #a_libdir+9
|
2012-11-26 16:26:15 +01:00
|
|
|
|
|
2014-02-09 20:10:04 +01:00
|
|
|
|
@loc01:
|
|
|
|
|
lodsb
|
|
|
|
|
stosb
|
|
|
|
|
or al, al
|
|
|
|
|
jnz loc01
|
|
|
|
|
|
|
|
|
|
mov eax, 68
|
|
|
|
|
mov ebx, 19
|
|
|
|
|
mov ecx, #a_libdir
|
|
|
|
|
int 0x40
|
|
|
|
|
or eax, eax
|
|
|
|
|
jz fail
|
|
|
|
|
|
|
|
|
|
push edx
|
|
|
|
|
push eax
|
|
|
|
|
call dll_Link
|
|
|
|
|
|
|
|
|
|
push eax
|
|
|
|
|
mov eax, DSDWORD[eax]
|
|
|
|
|
cmp DSDWORD[EAX], '_bil' // somehow this needs to be reversed..
|
|
|
|
|
pop eax
|
|
|
|
|
jnz loc02
|
|
|
|
|
|
|
|
|
|
push DSDWORD[EAX+4]
|
|
|
|
|
call dll_Init
|
|
|
|
|
@loc02:
|
|
|
|
|
|
|
|
|
|
pop esi
|
|
|
|
|
add esi, 8
|
|
|
|
|
jmp next_lib
|
|
|
|
|
@exit:
|
2015-07-22 20:32:54 +02:00
|
|
|
|
xor eax, eax
|
2014-02-09 20:10:04 +01:00
|
|
|
|
leave
|
|
|
|
|
ret 4
|
|
|
|
|
|
|
|
|
|
@fail:
|
|
|
|
|
add esp, 4
|
|
|
|
|
xor eax, eax
|
|
|
|
|
inc eax
|
|
|
|
|
leave
|
|
|
|
|
ret 4
|
|
|
|
|
}
|
2012-11-26 16:26:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-09 20:10:04 +01:00
|
|
|
|
//stdcall with 2 parameters
|
2015-11-05 21:49:42 +01:00
|
|
|
|
:void dll_Link() {
|
2014-02-09 20:10:04 +01:00
|
|
|
|
asm {
|
|
|
|
|
push ebp
|
|
|
|
|
mov ebp, esp
|
|
|
|
|
push eax
|
|
|
|
|
mov esi, SSDWORD[EBP+12]
|
|
|
|
|
test esi, esi
|
|
|
|
|
jz done
|
|
|
|
|
@next:
|
|
|
|
|
lodsd
|
|
|
|
|
test eax, eax
|
|
|
|
|
jz done
|
|
|
|
|
push eax
|
|
|
|
|
push SSDWORD[EBP+8]
|
|
|
|
|
call dll_GetProcAddress
|
|
|
|
|
or eax, eax
|
|
|
|
|
jz loc03
|
|
|
|
|
mov DSDWORD[esi-4], eax
|
|
|
|
|
jmp next
|
|
|
|
|
@loc03:
|
|
|
|
|
mov SSDWORD[esp], 0
|
|
|
|
|
@done:
|
|
|
|
|
pop eax
|
|
|
|
|
leave
|
|
|
|
|
ret 8
|
|
|
|
|
}
|
2012-11-26 16:26:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-09 20:10:04 +01:00
|
|
|
|
|
|
|
|
|
//stdcall with 1 parameter
|
2015-11-05 21:49:42 +01:00
|
|
|
|
:void dll_Init() {
|
2014-02-09 20:10:04 +01:00
|
|
|
|
asm {
|
|
|
|
|
push ebp
|
|
|
|
|
mov ebp, esp
|
|
|
|
|
pushad
|
2015-07-30 17:49:09 +02:00
|
|
|
|
mov eax, #malloc
|
|
|
|
|
mov ebx, #free;
|
|
|
|
|
mov ecx, #realloc;
|
2014-02-09 20:10:04 +01:00
|
|
|
|
mov edx, #dll_Load;
|
|
|
|
|
call SSDWORD[EBP+8]
|
|
|
|
|
popad
|
|
|
|
|
leave
|
|
|
|
|
ret 4
|
|
|
|
|
}
|
2012-11-26 16:26:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-09 20:10:04 +01:00
|
|
|
|
|
|
|
|
|
// stdcall with 2 parameters
|
2015-11-05 21:49:42 +01:00
|
|
|
|
:void dll_GetProcAddress(){
|
2014-02-09 20:10:04 +01:00
|
|
|
|
asm {
|
|
|
|
|
push ebp
|
|
|
|
|
mov ebp, esp
|
|
|
|
|
mov edx, CSDWORD[EBP+8]
|
|
|
|
|
xor eax, eax
|
|
|
|
|
|
|
|
|
|
@next:
|
|
|
|
|
or edx, edx
|
|
|
|
|
jz end
|
|
|
|
|
cmp CSDWORD[edx], 0
|
|
|
|
|
jz end
|
|
|
|
|
|
|
|
|
|
push CSDWORD[EBP+12]
|
|
|
|
|
push CSDWORD[EDX]
|
|
|
|
|
call dll_StrCmp
|
|
|
|
|
test eax, eax
|
|
|
|
|
jz ok
|
|
|
|
|
add edx, 8
|
|
|
|
|
jmp next
|
|
|
|
|
@ok:
|
|
|
|
|
mov eax, DSDWORD[EDX+4]
|
|
|
|
|
@end:
|
|
|
|
|
leave
|
|
|
|
|
ret 8
|
|
|
|
|
}
|
2012-11-26 16:26:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-02-09 20:10:04 +01:00
|
|
|
|
// stdcall with 2 parameters
|
2015-11-05 21:49:42 +01:00
|
|
|
|
:void dll_StrCmp() {
|
2014-02-09 20:10:04 +01:00
|
|
|
|
asm {
|
|
|
|
|
push ebp
|
|
|
|
|
mov ebp, esp
|
|
|
|
|
push esi
|
|
|
|
|
push edi
|
|
|
|
|
mov esi, CSDWORD[EBP+8]
|
|
|
|
|
mov edi, CSDWORD[EBP+12]
|
|
|
|
|
xor eax, eax
|
|
|
|
|
@label1:
|
|
|
|
|
lodsb
|
|
|
|
|
scasb
|
|
|
|
|
jne fail
|
|
|
|
|
or al, al
|
|
|
|
|
jnz label1
|
|
|
|
|
jmp label_ok
|
|
|
|
|
@fail:
|
|
|
|
|
or eax, -1
|
|
|
|
|
@label_ok:
|
|
|
|
|
pop edi
|
|
|
|
|
pop esi
|
|
|
|
|
leave
|
|
|
|
|
ret 8
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-05 21:49:42 +01:00
|
|
|
|
:int load_dll2(dword dllname, import_table, byte need_init)
|
2012-11-26 16:26:15 +01:00
|
|
|
|
{
|
|
|
|
|
//dword dllentry=0;
|
|
|
|
|
// load DLL
|
|
|
|
|
$mov eax, 68
|
|
|
|
|
$mov ebx, 19
|
|
|
|
|
ECX=dllname;
|
|
|
|
|
$int 0x40
|
|
|
|
|
$test eax, eax
|
|
|
|
|
$jz exit01
|
|
|
|
|
|
|
|
|
|
// initialize import
|
|
|
|
|
$mov edx,eax
|
|
|
|
|
ESI=import_table;
|
|
|
|
|
|
|
|
|
|
@import_loop01:
|
|
|
|
|
$lodsd
|
|
|
|
|
$test eax,eax
|
|
|
|
|
$jz import_done01
|
|
|
|
|
$push edx
|
|
|
|
|
@import_find01:
|
|
|
|
|
$mov ebx,DSDWORD[EDX]
|
|
|
|
|
$test ebx, ebx
|
|
|
|
|
$jz exit01
|
|
|
|
|
$push eax
|
|
|
|
|
@nex101:
|
|
|
|
|
$mov cl,DSBYTE[EAX];
|
|
|
|
|
$cmp cl,DSBYTE[EBX];
|
|
|
|
|
$jnz import_find_next01
|
|
|
|
|
$test cl,cl
|
|
|
|
|
$jz import_found01
|
|
|
|
|
$inc eax
|
|
|
|
|
$inc ebx
|
|
|
|
|
$jmp nex101
|
|
|
|
|
@import_find_next01:
|
|
|
|
|
$pop eax
|
|
|
|
|
$add edx, 8
|
|
|
|
|
$jmp import_find01
|
|
|
|
|
@import_found01:
|
|
|
|
|
$pop eax
|
|
|
|
|
$mov eax,DSDWORD[edx+4]
|
|
|
|
|
$mov DSDWORD[esi-4],eax
|
|
|
|
|
$pop edx
|
|
|
|
|
|
|
|
|
|
$jmp import_loop01
|
|
|
|
|
@import_done01:
|
2014-02-09 20:10:04 +01:00
|
|
|
|
IF (need_init) dll_Init (DSDWORD[EDX+4]);
|
2012-11-26 16:26:15 +01:00
|
|
|
|
return 0;
|
|
|
|
|
@exit01:
|
|
|
|
|
return -1;
|
2015-07-29 22:43:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-05 21:49:42 +01:00
|
|
|
|
:byte load_dll(dword dllname, import_table, byte need_init)
|
2015-07-29 22:43:05 +02:00
|
|
|
|
{
|
2015-07-30 17:49:09 +02:00
|
|
|
|
if (load_dll2(dllname, import_table, need_init))
|
|
|
|
|
{
|
|
|
|
|
error_init(dllname);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2015-07-22 20:32:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-05 21:49:42 +01:00
|
|
|
|
:struct OBJECT
|
|
|
|
|
{
|
|
|
|
|
void load(dword dllname);
|
|
|
|
|
dword get(dword fname);
|
|
|
|
|
} library;
|
|
|
|
|
|
|
|
|
|
:void OBJECT::load(dword dllname)
|
|
|
|
|
{
|
|
|
|
|
dword tmp;
|
|
|
|
|
IF(!GLOBAL_FUNC_LIB)
|
|
|
|
|
{
|
|
|
|
|
$mov eax, 68
|
|
|
|
|
$mov ebx, 19
|
|
|
|
|
ECX=#PATH_LIBRARY_LOADING;
|
|
|
|
|
$int 0x40
|
|
|
|
|
tmp = EAX;
|
|
|
|
|
GLOBAL_FUNC_LIB = tmp;
|
|
|
|
|
tmp+=4;
|
|
|
|
|
G_FUNC_LOAD = DSDWORD[tmp];
|
|
|
|
|
tmp+=8;
|
|
|
|
|
G_FUNC_GET = DSDWORD[tmp];
|
|
|
|
|
}
|
|
|
|
|
G_FUNC_LOAD stdcall(dllname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
:dword OBJECT::get(dword fname)
|
|
|
|
|
{
|
|
|
|
|
dword tmp=fname;
|
|
|
|
|
G_FUNC_GET stdcall(tmp);
|
|
|
|
|
return EAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
#define INIT_(name) byte init_#name(){object.load(name);
|
|
|
|
|
#define IMPORT(name) name = object.get(name);
|
|
|
|
|
#define _INIT return 1;}
|
|
|
|
|
*/
|
2015-07-22 20:32:54 +02:00
|
|
|
|
|
|
|
|
|
#endif
|