Release 941227
[wine/multimedia.git] / if1632 / relay.c
blob5a5b6cb961a691d558714876615863ab602c561d
1 /*
2 static char RCSId[] = "$Id: relay.c,v 1.2 1993/07/04 04:04:21 root Exp root $";
3 static char Copyright[] = "Copyright Robert J. Amstadt, 1993";
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #ifdef linux
15 #include <linux/unistd.h>
16 #include <linux/head.h>
17 #include <linux/ldt.h>
18 #endif
20 #include "neexe.h"
21 #include "segmem.h"
22 #include "prototypes.h"
23 #include "dlls.h"
24 #include "options.h"
25 #include "stddebug.h"
26 /* #define DEBUG_RELAY /* */
27 /* #define DEBUG_STACK /* */
28 #include "debug.h"
30 #if 0
31 /* Make make_debug think these were really used */
32 dprintf_relay
33 dprintf_stack
34 #endif
36 #ifdef WINELIB
37 #define WineLibSkip(x) 0
38 #else
39 #define WineLibSkip(x) x
40 #endif
42 struct dll_name_table_entry_s dll_builtin_table[N_BUILTINS] =
44 { "KERNEL", WineLibSkip(KERNEL_table), 410, 1 },
45 { "USER", WineLibSkip(USER_table), 540, 2 },
46 { "GDI", WineLibSkip(GDI_table), 490, 3 },
47 { "UNIXLIB", WineLibSkip(UNIXLIB_table), 10, 4 },
48 { "WIN87EM", WineLibSkip(WIN87EM_table), 10, 5 },
49 { "SHELL", WineLibSkip(SHELL_table), 103, 6 },
50 { "SOUND", WineLibSkip(SOUND_table), 20, 7 },
51 { "KEYBOARD",WineLibSkip(KEYBOARD_table),137, 8 },
52 { "WINSOCK", WineLibSkip(WINSOCK_table), 155, 9 },
53 { "STRESS", WineLibSkip(STRESS_table), 15, 10},
54 { "MMSYSTEM",WineLibSkip(MMSYSTEM_table),1226,11},
55 { "SYSTEM", WineLibSkip(SYSTEM_table), 20 ,12},
56 { "TOOLHELP",WineLibSkip(TOOLHELP_table), 83, 13},
57 { "MOUSE", WineLibSkip(MOUSE_table), 8, 14},
58 { "EMUCOMMDLG", WineLibSkip(COMMDLG_table), 31, 15},
60 /* don't forget to increase N_BUILTINS in dll.h if you add a dll */
62 #ifndef WINELIB
63 unsigned short *Stack16Frame;
65 extern unsigned long IF1632_Saved16_esp;
66 extern unsigned long IF1632_Saved16_ebp;
67 extern unsigned short IF1632_Saved16_ss;
69 /**********************************************************************
70 * DLLRelay
72 * We get a stack frame pointer to data that looks like this:
74 * Hex Offset Contents
75 * ---------- -------
76 * +00 previous saved_16ss
77 * +02 previous saved_16ebp
78 * +06 previous saved_16esp
79 * +0A 16-bit es
80 * +0C 16-bit ds
81 * +0E 16-bit ebp
82 * +12 length of 16-bit arguments
83 * +14 16-bit ip
84 * +16 16-bit cs
85 * +18 arguments
87 int
88 DLLRelay(unsigned int func_num, unsigned int seg_off)
90 struct dll_table_entry_s *dll_p;
91 unsigned short *saved_Stack16Frame;
92 unsigned int offset;
93 unsigned int dll_id;
94 unsigned int ordinal;
95 int arg_table[DLL_MAX_ARGS];
96 void *arg_ptr;
97 int (*func_ptr)();
98 int i;
99 int ret_val;
102 * Determine address of arguments.
104 saved_Stack16Frame = Stack16Frame;
105 Stack16Frame = (unsigned short *) seg_off;
106 arg_ptr = (void *) (seg_off + 0x18);
109 * Extract the DLL number and ordinal number.
111 dll_id = ((func_num >> 16) & 0xffff) - 1;
112 ordinal = func_num & 0xffff;
113 dll_p = &dll_builtin_table[dll_id].dll_table[ordinal];
115 if (debugging_relay)
117 unsigned int *ret_addr;
119 ret_addr = (unsigned int *) ((char *) seg_off + 0x14);
120 printf("Call %s (%s.%d), stack=%04x:%04x, ",
121 dll_p->export_name,
122 dll_builtin_table[dll_id].dll_name, ordinal,
123 seg_off >> 16, seg_off & 0xffff);
124 printf("ret=%08x", *ret_addr);
125 printf(" ESP=%08lx, EBP=%08lx, SS=%04x\n",
126 IF1632_Saved16_esp, IF1632_Saved16_ebp,
127 IF1632_Saved16_ss);
129 if(debugging_stack)
131 unsigned short *stack_p = (unsigned short *) seg_off;
132 for (i = 0; i < 24; i++, stack_p++)
134 printf("%04x ", *stack_p);
135 if ((i & 7) == 7)
136 printf("\n");
138 printf("\n");
140 } /* DEBUG_RELAY */
143 * Make sure we have a handler defined for this call.
145 if (dll_p->handler == NULL)
147 char buffer[100];
149 sprintf(buffer, "No handler for routine %s.%d",
150 dll_builtin_table[dll_id].dll_name, ordinal);
151 myerror(buffer);
153 func_ptr = dll_p->handler;
156 * OK, special case. If the handler is define as taking no arguments
157 * then pass the address of the arguments on the 16-bit stack to the
158 * handler. It will just ignore the pointer if it really takes no
159 * arguments. This allows us to write slightly faster library routines
160 * if we choose.
162 if (dll_p->n_args == 0)
164 ret_val = (*func_ptr)(arg_ptr);
165 Stack16Frame = saved_Stack16Frame;
166 return ret_val;
170 * Getting this far means we need to convert the 16-bit argument stack.
172 for (i = 0; i < dll_p->n_args; i++)
174 short *sp;
175 int *ip;
177 offset = dll_p->args[i].dst_arg;
179 switch (dll_p->args[i].src_type)
181 case DLL_ARGTYPE_SIGNEDWORD:
182 sp = (short *) ((char *) arg_ptr + offset);
183 arg_table[i] = *sp;
184 break;
186 case DLL_ARGTYPE_WORD:
187 sp = (short *) ((char *) arg_ptr + offset);
188 arg_table[i] = (int) *sp & 0xffff;
189 break;
191 case DLL_ARGTYPE_LONG:
192 ip = (int *) ((char *) arg_ptr + offset);
193 arg_table[i] = *ip;
194 break;
196 case DLL_ARGTYPE_FARPTR:
197 ip = (int *) ((char *) arg_ptr + offset);
198 if (*ip & 0xffff0000)
199 arg_table[i] = FIXPTR(*ip);
200 else
201 arg_table[i] = *ip;
202 break;
207 * Call the handler
209 ret_val = (*func_ptr)(arg_table[0], arg_table[1], arg_table[2],
210 arg_table[3], arg_table[4], arg_table[5],
211 arg_table[6], arg_table[7], arg_table[8],
212 arg_table[9], arg_table[10], arg_table[11],
213 arg_table[12], arg_table[13], arg_table[14],
214 arg_table[15]);
216 if (debugging_relay)
218 printf("Returning %08x from %s (%s.%d)\n",
219 ret_val,
220 dll_p->export_name,
221 dll_builtin_table[dll_id].dll_name, ordinal);
224 Stack16Frame = saved_Stack16Frame;
225 return ret_val;
227 #endif
229 /**********************************************************************
230 * FindDLLTable
232 struct dll_table_entry_s *
233 FindDLLTable(char *dll_name)
235 int i;
237 for (i = 0; i < N_BUILTINS; i++)
238 if (strcasecmp(dll_builtin_table[i].dll_name, dll_name) == 0)
239 #ifdef WINELIB
240 return dll_builtin_table[i].dll_number;
241 #else
242 return dll_builtin_table[i].dll_table;
243 #endif
244 return NULL;
247 /**********************************************************************
248 * FindOrdinalFromName
251 FindOrdinalFromName(struct dll_table_entry_s *dll_table, char *func_name)
253 int i, limit;
255 for (i = 0; i < N_BUILTINS; i++)
256 if (dll_table == dll_builtin_table[i].dll_table)
257 break;
259 if (i == N_BUILTINS)
260 return 0;
262 limit = dll_builtin_table[i].dll_table_length;
263 for (i = 0; i < limit; i++)
264 if (strcasecmp(dll_table[i].export_name, func_name) == 0)
265 return i;
267 return 0;
269 /**********************************************************************
270 * ReturnArg
273 ReturnArg(int arg)
275 return arg;
278 #ifndef WINELIB
279 #ifdef WINESTAT
280 void winestat(){
281 int i, j;
282 double perc;
283 int used, implemented;
284 int tused, timplemented;
285 struct dll_table_entry_s *table;
287 tused = 0;
288 timplemented = 0;
289 for (i = 0; i < N_BUILTINS; i++) {
290 table = dll_builtin_table[i].dll_table;
291 used = 0;
292 implemented = 0;
293 for(j=0; j < dll_builtin_table[i].dll_table_length; j++) {
294 if(table[j].used){
295 used++;
296 if (table[j].handler) implemented++;
297 else
298 printf("%s.%d not implemented\n",
299 dll_builtin_table[i].dll_name,
303 tused += used;
304 timplemented += implemented;
305 if(used)
306 perc = implemented * 100.00 / used;
307 else
308 perc = 0.0;
309 if (used)
310 printf("%s: %d of %d (%3.1f %%)\n", dll_builtin_table[i].dll_name, implemented, used, perc);
312 perc = timplemented * 100.00 / tused;
313 printf("TOTAL: %d of %d winapi functions implemented (%3.1f %%)\n",timplemented, tused, perc);
315 #endif /* WINESTAT */
316 #endif /* !WINELIB */