Debian Codegraph @

terps/tads/tads2/msdos/osdosex0.c

raw symbols SCIP
1 /*
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.
5 */
6
7 #ifdef DJGPP
8
9 /*
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.
13 */
14 int (*os_exfld( osfildef *fp, unsigned len ))(void *)
15 {
16 return (int (*)(void *)) 0;
17 }
18
19 /*
20 * Load an external function from a file. This routine assumes that the
21 * file has the same name as the resource.
22 */
23 int (*os_exfil(const char *name))(void *)
24 {
25 return (int (*)(void *)) 0;
26 }
27
28 /*
29 * call an external function, passing it an argument (a string pointer),
30 * and passing back the string pointer returned by the external function
31 */
32 int os_excall(int (*extfn)(void *), void *arg)
33 {
34 return 0;
35 }
36
37 #endif /* DJGPP */
38
39 /* ------------------------------------------------------------------------ */
40 /*
41 * Real-mode and 16-bit protected mode external function interfaces
42 */
43
44 #ifndef T_WIN32
45
46 static void *canon(void *ptr)
47 {
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));
53 }
54
55 /*
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.
59 */
60 int (*os_exfld(osfildef *fp, unsigned len))(void *)
61 {
62 void *extfn;
63 unsigned alo;
64
65 #ifdef MSOS2
66 /* for OS/2, don't load anything, but seek past the resource */
67 # ifdef MICROSOFT
68 if (_osmode == OS2_MODE)
69 # endif /* MICROSOFT */
70 {
71 osfseek(fp, (long)len, OSFSK_SET);
72 return((int (*)(void))0);
73 }
74 #endif /* MSOS2 */
75
76 #ifdef __DPMI16__
77
78 HANDLE selector;
79
80 selector = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, len);
81 if (!selector || !(extfn = GlobalLock(selector)))
82 return 0;
83
84 /* read the file */
85 osfrb(fp, extfn, len);
86
87 /* change the selector to a code segment */
88 _asm
89 {
90 mov bx, selector /* get selector */
91 lar ax, bx /* get current access rights */
92 mov cl, ah
93 or cl, 8 /* set the CODE bit in the descriptor */
94 mov ax, 9 /* function = set descriptor rights */
95 mov ch, 0
96 int 31h
97 }
98
99 /* close the file and return the pointer to the function in memory */
100 return((int (*)(void *))extfn);
101
102 #else /* __DPMI16 __ */
103
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 */
107 if (!extfn)
108 return((int (*)(void *))0 );
109
110 /* read the file */
111 osfrb(fp, extfn, len);
112
113 /* close the file and return the pointer to the function in memory */
114 return((int (*)(void *))extfn);
115
116 #endif /* __DPMI16__ */
117 }
118
119 /*
120 * Load an external function from a file. This routine assumes that the
121 * file has the same name as the resource.
122 */
123 int (*os_exfil(const char *name))(void *)
124 {
125 FILE *fp;
126 unsigned len;
127 int (*extfn)(void *);
128
129 #ifdef MSOS2
130 /* load the function from a DLL of the same name as the function */
131 # ifdef MICROSOFT
132 if (_osmode == OS2_MODE)
133 # endif /* MICROSOFT */
134 {
135 CHAR failname[128];
136 HMODULE hmod;
137 PFN pfn;
138
139 if (DosLoadModule(failname, sizeof(failname), (PSZ)name, &hmod)
140 || DosGetProcAddr(hmod, (PSZ)"_main", &pfn))
141 {
142 return((int (*)(void))0);
143 }
144 return((int (*)(void))pfn);
145 }
146 #endif /* MSOS2 */
147
148 #ifdef __DPMI16__
149
150 #endif
151
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 */
156
157 (void)fseek( fp, 0L, 0 );
158 extfn = os_exfld(fp, len);
159
160 fclose( fp );
161 return( extfn );
162 }
163
164 /*
165 * call an external function, passing it an argument (a string pointer),
166 * and passing back the string pointer returned by the external function
167 */
168 int os_excall(int (*extfn)(void *), void *arg)
169 {
170 return((*extfn)(arg));
171 }
172
173 #endif /* !T_WIN32 */
174
175 /* ------------------------------------------------------------------------ */
176 /*
177 * Win32 external function interfaces - not implemented at present
178 */
179
180 #ifdef T_WIN32
181
182 int (*os_exfld(osfildef *fp, unsigned len))(void *)
183 {
184 /* NOT IMPLEMENTED - scan past the resource and fail */
185 osfseek(fp, (long)len, OSFSK_CUR);
186 return 0;
187 }
188
189 int (*os_exfil(const char *name))(void *)
190 {
191 HINSTANCE hlib;
192 FARPROC proc;
193
194 /*
195 * load the library of the given name; if we can't load the library, we
196 * can't load the external function
197 */
198 hlib = LoadLibrary(name);
199 if (hlib == 0)
200 return 0;
201
202 /* get the address of the "_main" procedure */
203 proc = GetProcAddress(hlib, "main");
204
205 /* if that failed, unload the library and return failure */
206 if (proc == 0)
207 {
208 FreeLibrary(hlib);
209 return 0;
210 }
211
212 /*
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
216 */
217 return (int (*)(void *))proc;
218 }
219
220 int os_excall(int (*extfn)(void *), void *arg)
221 {
222 /* call the function directly */
223 return((*extfn)(arg));
224 }
225
226 #endif /* T_WIN32 */
227