Release 950109
[wine/multimedia.git] / if1632 / relay.c
blobe286bb82e689cd39111a64fcb8468d013b636308
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 /* FIXME: Is there an end-of-stack-pointer somewhere ? */
133 int n = min(24, (0x10000 - (seg_off & 0xffff)) / sizeof(*stack_p));
134 for (i = 0; i < n; i++, stack_p++)
136 printf("%04x ", *stack_p);
137 if ((i & 7) == 7)
138 printf("\n");
140 printf("\n");
142 } /* DEBUG_RELAY */
145 * Make sure we have a handler defined for this call.
147 if (dll_p->handler == NULL)
149 char buffer[100];
151 sprintf(buffer, "No handler for routine %s.%d",
152 dll_builtin_table[dll_id].dll_name, ordinal);
153 myerror(buffer);
155 func_ptr = dll_p->handler;
158 * OK, special case. If the handler is define as taking no arguments
159 * then pass the address of the arguments on the 16-bit stack to the
160 * handler. It will just ignore the pointer if it really takes no
161 * arguments. This allows us to write slightly faster library routines
162 * if we choose.
164 if (dll_p->n_args == 0)
166 ret_val = (*func_ptr)(arg_ptr);
167 Stack16Frame = saved_Stack16Frame;
168 return ret_val;
172 * Getting this far means we need to convert the 16-bit argument stack.
174 for (i = 0; i < dll_p->n_args; i++)
176 short *sp;
177 int *ip;
179 offset = dll_p->args[i].dst_arg;
181 switch (dll_p->args[i].src_type)
183 case DLL_ARGTYPE_SIGNEDWORD:
184 sp = (short *) ((char *) arg_ptr + offset);
185 arg_table[i] = *sp;
186 break;
188 case DLL_ARGTYPE_WORD:
189 sp = (short *) ((char *) arg_ptr + offset);
190 arg_table[i] = (int) *sp & 0xffff;
191 break;
193 case DLL_ARGTYPE_LONG:
194 ip = (int *) ((char *) arg_ptr + offset);
195 arg_table[i] = *ip;
196 break;
198 case DLL_ARGTYPE_FARPTR:
199 ip = (int *) ((char *) arg_ptr + offset);
200 if (*ip & 0xffff0000)
201 arg_table[i] = FIXPTR(*ip);
202 else
203 arg_table[i] = *ip;
204 break;
209 * Call the handler
211 ret_val = (*func_ptr)(arg_table[0], arg_table[1], arg_table[2],
212 arg_table[3], arg_table[4], arg_table[5],
213 arg_table[6], arg_table[7], arg_table[8],
214 arg_table[9], arg_table[10], arg_table[11],
215 arg_table[12], arg_table[13], arg_table[14],
216 arg_table[15]);
218 if (debugging_relay)
220 printf("Returning %08x from %s (%s.%d)\n",
221 ret_val,
222 dll_p->export_name,
223 dll_builtin_table[dll_id].dll_name, ordinal);
226 Stack16Frame = saved_Stack16Frame;
227 return ret_val;
229 #endif
231 /**********************************************************************
232 * FindDLLTable
234 struct dll_table_entry_s *
235 FindDLLTable(char *dll_name)
237 int i;
239 for (i = 0; i < N_BUILTINS; i++)
240 if (strcasecmp(dll_builtin_table[i].dll_name, dll_name) == 0)
241 #ifdef WINELIB
242 return dll_builtin_table[i].dll_number;
243 #else
244 return dll_builtin_table[i].dll_table;
245 #endif
246 return NULL;
249 /**********************************************************************
250 * FindOrdinalFromName
253 FindOrdinalFromName(struct dll_table_entry_s *dll_table, char *func_name)
255 int i, limit;
257 for (i = 0; i < N_BUILTINS; i++)
258 if (dll_table == dll_builtin_table[i].dll_table)
259 break;
261 if (i == N_BUILTINS)
262 return 0;
264 limit = dll_builtin_table[i].dll_table_length;
265 for (i = 0; i < limit; i++)
266 if (strcasecmp(dll_table[i].export_name, func_name) == 0)
267 return i;
269 return 0;
271 /**********************************************************************
272 * ReturnArg
275 ReturnArg(int arg)
277 return arg;
280 #ifndef WINELIB
281 #ifdef WINESTAT
282 void winestat(){
283 int i, j;
284 double perc;
285 int used, implemented;
286 int tused, timplemented;
287 struct dll_table_entry_s *table;
289 tused = 0;
290 timplemented = 0;
291 for (i = 0; i < N_BUILTINS; i++) {
292 table = dll_builtin_table[i].dll_table;
293 used = 0;
294 implemented = 0;
295 for(j=0; j < dll_builtin_table[i].dll_table_length; j++) {
296 if(table[j].used){
297 used++;
298 if (table[j].handler) implemented++;
299 else
300 printf("%s.%d not implemented\n",
301 dll_builtin_table[i].dll_name,
305 tused += used;
306 timplemented += implemented;
307 if(used)
308 perc = implemented * 100.00 / used;
309 else
310 perc = 0.0;
311 if (used)
312 printf("%s: %d of %d (%3.1f %%)\n", dll_builtin_table[i].dll_name, implemented, used, perc);
314 perc = timplemented * 100.00 / tused;
315 printf("TOTAL: %d of %d winapi functions implemented (%3.1f %%)\n",timplemented, tused, perc);
317 #endif /* WINESTAT */
318 #endif /* !WINELIB */