Subversion Repositories Kolibri OS

Rev

Rev 5676 | Rev 5883 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5598 pavelyakov 1
#ifndef INCLUDE_DLL_H
2
#define INCLUDE_DLL_H
5676 pavelyakov 3
#print "[include ]\n"
5598 pavelyakov 4
 
5631 pavelyakov 5
#ifndef INCLUDE_FILESYSTEM_H
5626 leency 6
#include "../lib/file_system.h"
7
#endif
8
 
5631 pavelyakov 9
#ifdef LANG_RUS
10
	#define _TEXT_ERROR_ADD "'Ошибка при загрузке библиотеки"
11
#elif LANG_EST
12
	#define _TEXT_ERROR_ADD "'Viga teegi laadimisel"
13
#else
14
	#define _TEXT_ERROR_ADD "'Error while loading library"
15
#endif
5626 leency 16
 
3067 leency 17
char a_libdir[43]  = "/sys/lib/\0";
18
 
5576 pavelyakov 19
:inline void error_init(dword text)
20
{
21
	dword TEXT_ERROR = malloc(1024);
5690 leency 22
	sprintf(TEXT_ERROR, "%s `%s`' -E",_TEXT_ERROR_ADD,text);
5576 pavelyakov 23
	notify(TEXT_ERROR);
24
	free(TEXT_ERROR);
25
}
26
 
4567 hidnplayr 27
// stdcall with 1 parameter
28
void dll_Load() {
29
asm {
30
        push    ebp
31
        mov     ebp, esp
32
        mov     esi, SSDWORD[EBP+8]
5576 pavelyakov 33
		@next_lib:
4567 hidnplayr 34
        mov     edx, DSDWORD[ESI]
35
        or      edx, edx
36
        jz      exit
37
        push    esi
38
        mov     esi, DSDWORD[ESI+4]
39
        mov     edi, #a_libdir+9
3067 leency 40
 
4567 hidnplayr 41
@loc01:
42
        lodsb
43
        stosb
44
        or      al, al
45
        jnz     loc01
3067 leency 46
 
4567 hidnplayr 47
        mov     eax, 68
48
        mov     ebx, 19
49
        mov     ecx, #a_libdir
50
        int     0x40
51
        or      eax, eax
52
        jz      fail
53
 
54
        push    edx
55
        push    eax
56
        call    dll_Link
57
 
58
        push    eax
59
        mov     eax, DSDWORD[eax]
60
        cmp     DSDWORD[EAX], '_bil'    // somehow this needs to be reversed..
61
        pop     eax
62
        jnz     loc02
63
 
64
        push    DSDWORD[EAX+4]
65
        call    dll_Init
66
@loc02:
67
 
68
        pop     esi
69
        add     esi, 8
70
        jmp     next_lib
71
@exit:
5598 pavelyakov 72
	xor     eax, eax
4567 hidnplayr 73
        leave
74
        ret     4
75
 
76
@fail:
77
        add     esp, 4
78
        xor     eax, eax
79
        inc     eax
80
        leave
81
        ret     4
82
    }
3067 leency 83
}
84
 
4567 hidnplayr 85
//stdcall with 2 parameters
86
void dll_Link() {
87
asm {
88
        push    ebp
89
        mov     ebp, esp
90
        push    eax
91
        mov     esi, SSDWORD[EBP+12]
92
        test    esi, esi
93
        jz      done
94
@next:
95
        lodsd
96
        test    eax, eax
97
        jz      done
98
        push    eax
99
        push    SSDWORD[EBP+8]
100
        call    dll_GetProcAddress
101
        or      eax, eax
102
        jz      loc03
103
        mov     DSDWORD[esi-4], eax
104
        jmp     next
105
@loc03:
106
        mov     SSDWORD[esp], 0
107
@done:
108
        pop     eax
109
        leave
110
        ret     8
111
    }
112
}
3067 leency 113
 
114
 
4567 hidnplayr 115
//stdcall with 1 parameter
116
void dll_Init() {
117
asm {
118
        push    ebp
119
        mov     ebp, esp
120
        pushad
5631 pavelyakov 121
        mov     eax, #malloc
122
        mov     ebx, #free;
123
        mov     ecx, #realloc;
4567 hidnplayr 124
        mov     edx, #dll_Load;
125
        call    SSDWORD[EBP+8]
126
        popad
127
        leave
128
        ret     4
129
    }
3067 leency 130
}
131
 
4567 hidnplayr 132
 
133
// stdcall with 2 parameters
134
void dll_GetProcAddress(){
135
asm {
136
        push    ebp
137
        mov     ebp, esp
138
        mov     edx, CSDWORD[EBP+8]
139
        xor     eax, eax
140
 
141
@next:
142
        or      edx, edx
143
        jz      end
144
        cmp     CSDWORD[edx], 0
145
        jz      end
146
 
147
        push    CSDWORD[EBP+12]
148
        push    CSDWORD[EDX]
149
        call    dll_StrCmp
150
        test    eax, eax
151
        jz      ok
152
        add     edx, 8
153
        jmp     next
154
@ok:
155
        mov     eax, DSDWORD[EDX+4]
156
@end:
157
        leave
158
        ret     8
159
    }
3067 leency 160
}
161
 
4567 hidnplayr 162
 
163
// stdcall with 2 parameters
164
void dll_StrCmp() {
165
asm {
166
        push    ebp
167
        mov     ebp, esp
168
        push    esi
169
        push    edi
170
        mov     esi, CSDWORD[EBP+8]
171
        mov     edi, CSDWORD[EBP+12]
172
        xor     eax, eax
173
@label1:
174
        lodsb
175
        scasb
176
        jne     fail
177
        or      al, al
178
        jnz     label1
179
        jmp     label_ok
180
@fail:
181
        or      eax, -1
182
@label_ok:
183
        pop     edi
184
        pop     esi
185
        leave
186
        ret     8
187
    }
3067 leency 188
}
189
 
190
int load_dll2(dword dllname, import_table, byte need_init)
191
{
192
   //dword dllentry=0;
193
// load DLL
194
        $mov     eax, 68
195
        $mov     ebx, 19
196
        ECX=dllname;
197
        $int     0x40
198
        $test    eax, eax
199
        $jz      exit01
200
 
201
// initialize import
202
        $mov     edx,eax
203
        ESI=import_table;
204
 
205
@import_loop01:
206
        $lodsd
207
        $test    eax,eax
208
        $jz      import_done01
209
        $push    edx
210
@import_find01:
211
        $mov     ebx,DSDWORD[EDX]
212
        $test    ebx, ebx
213
        $jz      exit01
214
        $push    eax
215
@nex101:
216
        $mov     cl,DSBYTE[EAX];
217
        $cmp     cl,DSBYTE[EBX];
218
        $jnz     import_find_next01
219
        $test    cl,cl
220
        $jz      import_found01
221
        $inc     eax
222
        $inc     ebx
223
        $jmp     nex101
224
@import_find_next01:
225
        $pop     eax
226
        $add     edx, 8
227
        $jmp     import_find01
228
@import_found01:
229
        $pop     eax
230
        $mov     eax,DSDWORD[edx+4]
231
        $mov     DSDWORD[esi-4],eax
232
        $pop     edx
233
 
234
        $jmp     import_loop01
235
@import_done01:
4567 hidnplayr 236
        IF (need_init) dll_Init (DSDWORD[EDX+4]);
3067 leency 237
        return 0;
238
@exit01:
239
        return -1;
5598 pavelyakov 240
}
241
 
5631 pavelyakov 242
byte load_dll(dword dllname, import_table, byte need_init)
5626 leency 243
{
5631 pavelyakov 244
	if (load_dll2(dllname, import_table, need_init))
245
	{
246
		error_init(dllname);
247
		return false;
248
	}
249
	return true;
5626 leency 250
}
5598 pavelyakov 251
 
5626 leency 252
 
5598 pavelyakov 253
#endif