2
* Former implementations of os_exfld, os_exfil, and os_excall. All
3
* external function operations have been removed as of 2.5.8 - external
4
* functions are now obsolete due to security concerns.
10
* os_exfld - load in an external function from an open file, given the
11
* size of the function (in bytes). Returns a pointer to the newly
12
* allocated memory block containing the function in memory.
14
int (*os_exfld( osfildef *fp, unsigned len ))(void *)
16
return (int (*)(void *)) 0;
20
* Load an external function from a file. This routine assumes that the
21
* file has the same name as the resource.
23
int (*os_exfil(const char *name))(void *)
25
return (int (*)(void *)) 0;
29
* call an external function, passing it an argument (a string pointer),
30
* and passing back the string pointer returned by the external function
32
int os_excall(int (*extfn)(void *), void *arg)
39
/* ------------------------------------------------------------------------ */
41
* Real-mode and 16-bit protected mode external function interfaces
46
static void *canon(void *ptr)
48
unsigned long p = (unsigned long)ptr;
49
unsigned long abs = ((p >> 16) << 4) + (p & 0xffff);
50
if (abs & (unsigned long)0xf)
51
abs = (abs & ~(unsigned long)0xf) + (unsigned long)0x10;
52
return((void *)(abs << 12));
56
* os_exfld - load in an external function from an open file, given the
57
* size of the function (in bytes). Returns a pointer to the newly
58
* allocated memory block containing the function in memory.
60
int (*os_exfld(osfildef *fp, unsigned len))(void *)
66
/* for OS/2, don't load anything, but seek past the resource */
68
if (_osmode == OS2_MODE)
69
# endif /* MICROSOFT */
71
osfseek(fp, (long)len, OSFSK_SET);
72
return((int (*)(void))0);
80
selector = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, len);
81
if (!selector || !(extfn = GlobalLock(selector)))
85
osfrb(fp, extfn, len);
87
/* change the selector to a code segment */
90
mov bx, selector /* get selector */
91
lar ax, bx /* get current access rights */
93
or cl, 8 /* set the CODE bit in the descriptor */
94
mov ax, 9 /* function = set descriptor rights */
99
/* close the file and return the pointer to the function in memory */
100
return((int (*)(void *))extfn);
102
#else /* __DPMI16 __ */
104
/* figure out how much memory is needed and allocate it */
105
alo = ((len + 0xf) & ~0xf) + 16; /* round to mult of 16, plus 1 page */
106
extfn = canon(malloc(alo));/* allocate the memory, canonicalize pointer */
108
return((int (*)(void *))0 );
111
osfrb(fp, extfn, len);
113
/* close the file and return the pointer to the function in memory */
114
return((int (*)(void *))extfn);
116
#endif /* __DPMI16__ */
120
* Load an external function from a file. This routine assumes that the
121
* file has the same name as the resource.
123
int (*os_exfil(const char *name))(void *)
127
int (*extfn)(void *);
130
/* load the function from a DLL of the same name as the function */
132
if (_osmode == OS2_MODE)
133
# endif /* MICROSOFT */
139
if (DosLoadModule(failname, sizeof(failname), (PSZ)name, &hmod)
140
|| DosGetProcAddr(hmod, (PSZ)"_main", &pfn))
142
return((int (*)(void))0);
144
return((int (*)(void))pfn);
152
/* open the file and see how big it is to determine our memory needs */
153
if ( !( fp = fopen( name, "rb" ))) return( (int (*)(void *))0 );
154
(void)fseek( fp, 0L, 2 );
155
len = (unsigned)ftell( fp ); /* total length of file */
157
(void)fseek( fp, 0L, 0 );
158
extfn = os_exfld(fp, len);
165
* call an external function, passing it an argument (a string pointer),
166
* and passing back the string pointer returned by the external function
168
int os_excall(int (*extfn)(void *), void *arg)
170
return((*extfn)(arg));
173
#endif /* !__WIN32__ */
175
/* ------------------------------------------------------------------------ */
177
* Win32 external function interfaces - not implemented at present
182
int (*os_exfld(osfildef *fp, unsigned len))(void *)
184
/* NOT IMPLEMENTED - scan past the resource and fail */
185
osfseek(fp, (long)len, OSFSK_CUR);
189
int (*os_exfil(const char *name))(void *)
195
* load the library of the given name; if we can't load the library, we
196
* can't load the external function
198
hlib = LoadLibrary(name);
202
/* get the address of the "_main" procedure */
203
proc = GetProcAddress(hlib, "main");
205
/* if that failed, unload the library and return failure */
213
* return the procedure address, suitably cast; unfortunately, we have
214
* no provision for freeing the library instance handle, so we'll just
215
* have to count on Windows releasing it when the process terminates
217
return (int (*)(void *))proc;
220
int os_excall(int (*extfn)(void *), void *arg)
222
/* call the function directly */
223
return((*extfn)(arg));
226
#endif /* __WIN32__ */