Release 940815
[wine/multimedia.git] / if1632 / relay.c
blob3122d8bd3f82773082db0c21ec8e7b00eb4780c5
1 static char RCSId[] = "$Id: relay.c,v 1.2 1993/07/04 04:04:21 root Exp root $";
2 static char Copyright[] = "Copyright Robert J. Amstadt, 1993";
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #ifdef linux
12 #include <linux/unistd.h>
13 #include <linux/head.h>
14 #include <linux/ldt.h>
15 #include <linux/segment.h>
16 #endif
17 #include <errno.h>
19 #include "neexe.h"
20 #include "segmem.h"
21 #include "prototypes.h"
22 #include "dlls.h"
23 #include "options.h"
25 #define DEBUG_RELAY /* */
27 #ifdef WINELIB
28 #define WineLibSkip(x) 0
29 #else
30 #define WineLibSkip(x) x
31 #endif
33 struct dll_name_table_entry_s dll_builtin_table[N_BUILTINS] =
35 { "KERNEL", WineLibSkip(KERNEL_table), 410, 1 },
36 { "USER", WineLibSkip(USER_table), 540, 2 },
37 { "GDI", WineLibSkip(GDI_table), 490, 3 },
38 { "UNIXLIB", WineLibSkip(UNIXLIB_table), 10, 4 },
39 { "WIN87EM", WineLibSkip(WIN87EM_table), 10, 5 },
40 { "SHELL", WineLibSkip(SHELL_table), 103, 6 },
41 { "SOUND", WineLibSkip(SOUND_table), 20, 7 },
42 { "KEYBOARD",WineLibSkip(KEYBOARD_table),137, 8 },
43 { "WINSOCK", WineLibSkip(WINSOCK_table), 155, 9 },
44 { "STRESS", WineLibSkip(STRESS_table), 15, 10},
45 { "MMSYSTEM",WineLibSkip(MMSYSTEM_table),1226,11},
46 { "SYSTEM", WineLibSkip(SYSTEM_table), 20 ,12},
47 { "TOOLHELP",WineLibSkip(TOOLHELP_table), 83, 13},
48 { "MOUSE", WineLibSkip(MOUSE_table), 8, 14},
50 /* don't forget to increase N_BUILTINS in dll.h if you add a dll */
52 #ifndef WINELIB
53 unsigned short *Stack16Frame;
55 extern unsigned long IF1632_Saved16_esp;
56 extern unsigned long IF1632_Saved16_ebp;
57 extern unsigned short IF1632_Saved16_ss;
59 /**********************************************************************
60 * DLLRelay
62 * We get a stack frame pointer to data that looks like this:
64 * Hex Offset Contents
65 * ---------- -------
66 * +00 previous saved_16ss
67 * +02 previous saved_16ebp
68 * +06 previous saved_16esp
69 * +0A 16-bit es
70 * +0C 16-bit ds
71 * +0E 16-bit ebp
72 * +12 length of 16-bit arguments
73 * +14 16-bit ip
74 * +16 16-bit cs
75 * +18 arguments
77 int
78 DLLRelay(unsigned int func_num, unsigned int seg_off)
80 struct dll_table_entry_s *dll_p;
81 unsigned short *saved_Stack16Frame;
82 unsigned int segment;
83 unsigned int offset;
84 unsigned int dll_id;
85 unsigned int ordinal;
86 int arg_table[DLL_MAX_ARGS];
87 void *arg_ptr;
88 int (*func_ptr)();
89 int i;
90 int ret_val;
93 * Determine address of arguments.
95 saved_Stack16Frame = Stack16Frame;
96 Stack16Frame = (unsigned short *) seg_off;
97 arg_ptr = (void *) (seg_off + 0x18);
100 * Extract the DLL number and ordinal number.
102 dll_id = ((func_num >> 16) & 0xffff) - 1;
103 ordinal = func_num & 0xffff;
104 dll_p = &dll_builtin_table[dll_id].dll_table[ordinal];
106 #ifdef DEBUG_RELAY
107 if (Options.relay_debug)
109 unsigned int *ret_addr;
110 unsigned short *stack_p;
112 ret_addr = (unsigned int *) ((char *) seg_off + 0x14);
113 printf("Calling %s (%s.%d), 16-bit stack at %04x:%04x, ",
114 dll_p->export_name,
115 dll_builtin_table[dll_id].dll_name, ordinal,
116 seg_off >> 16, seg_off & 0xffff);
117 printf("return to %08x\n", *ret_addr);
118 printf(" ESP %08x, EBP %08x, SS %04x\n",
119 IF1632_Saved16_esp, IF1632_Saved16_ebp,
120 IF1632_Saved16_ss);
122 #ifdef DEBUG_STACK
123 stack_p = (unsigned short *) seg_off;
124 for (i = 0; i < 24; i++, stack_p++)
126 printf("%04x ", *stack_p);
127 if ((i & 7) == 7)
128 printf("\n");
130 printf("\n");
131 #endif /* DEBUG_STACK */
133 #endif /* DEBUG_RELAY */
136 * Make sure we have a handler defined for this call.
138 if (dll_p->handler == NULL)
140 char buffer[100];
142 sprintf(buffer, "No handler for routine %s.%d",
143 dll_builtin_table[dll_id].dll_name, ordinal);
144 myerror(buffer);
146 func_ptr = dll_p->handler;
149 * OK, special case. If the handler is define as taking no arguments
150 * then pass the address of the arguments on the 16-bit stack to the
151 * handler. It will just ignore the pointer if it really takes no
152 * arguments. This allows us to write slightly faster library routines
153 * if we choose.
155 if (dll_p->n_args == 0)
157 ret_val = (*func_ptr)(arg_ptr);
158 Stack16Frame = saved_Stack16Frame;
159 return ret_val;
163 * Getting this far means we need to convert the 16-bit argument stack.
165 for (i = 0; i < dll_p->n_args; i++)
167 short *sp;
168 int *ip;
170 offset = dll_p->args[i].dst_arg;
172 switch (dll_p->args[i].src_type)
174 case DLL_ARGTYPE_SIGNEDWORD:
175 sp = (short *) ((char *) arg_ptr + offset);
176 arg_table[i] = *sp;
177 break;
179 case DLL_ARGTYPE_WORD:
180 sp = (short *) ((char *) arg_ptr + offset);
181 arg_table[i] = (int) *sp & 0xffff;
182 break;
184 case DLL_ARGTYPE_LONG:
185 ip = (int *) ((char *) arg_ptr + offset);
186 arg_table[i] = *ip;
187 break;
189 case DLL_ARGTYPE_FARPTR:
190 ip = (int *) ((char *) arg_ptr + offset);
191 if (*ip & 0xffff0000)
192 arg_table[i] = FIXPTR(*ip);
193 else
194 arg_table[i] = *ip;
195 break;
200 * Call the handler
202 ret_val = (*func_ptr)(arg_table[0], arg_table[1], arg_table[2],
203 arg_table[3], arg_table[4], arg_table[5],
204 arg_table[6], arg_table[7], arg_table[8],
205 arg_table[9], arg_table[10], arg_table[11],
206 arg_table[12], arg_table[13], arg_table[14],
207 arg_table[15]);
209 #ifdef DEBUG_RELAY
210 if (Options.relay_debug)
212 printf("Returning %08.8x from %s (%s.%d)\n",
213 ret_val,
214 dll_p->export_name,
215 dll_builtin_table[dll_id].dll_name, ordinal);
217 #endif
219 Stack16Frame = saved_Stack16Frame;
220 return ret_val;
222 #endif
224 /**********************************************************************
225 * FindDLLTable
227 struct dll_table_entry_s *
228 FindDLLTable(char *dll_name)
230 int i;
232 for (i = 0; i < N_BUILTINS; i++)
233 if (strcasecmp(dll_builtin_table[i].dll_name, dll_name) == 0)
234 #ifdef WINELIB
235 return dll_builtin_table[i].dll_number;
236 #else
237 return dll_builtin_table[i].dll_table;
238 #endif
239 return NULL;
242 /**********************************************************************
243 * FindOrdinalFromName
246 FindOrdinalFromName(struct dll_table_entry_s *dll_table, char *func_name)
248 int i, limit;
250 for (i = 0; i < N_BUILTINS; i++)
251 if (dll_table == dll_builtin_table[i].dll_table)
252 break;
254 if (i == N_BUILTINS)
255 return 0;
257 limit = dll_builtin_table[i].dll_table_length;
258 for (i = 0; i < limit; i++)
259 if (strcasecmp(dll_table[i].export_name, func_name) == 0)
260 return i;
262 return 0;
264 /**********************************************************************
265 * ReturnArg
268 ReturnArg(int arg)
270 return arg;
273 #ifndef WINELIB
274 #ifdef WINESTAT
275 void winestat(){
276 int i, j;
277 double perc;
278 int used, implemented;
279 int tused, timplemented;
280 struct dll_table_entry_s *table;
282 tused = 0;
283 timplemented = 0;
284 for (i = 0; i < N_BUILTINS; i++) {
285 table = dll_builtin_table[i].dll_table;
286 used = 0;
287 implemented = 0;
288 for(j=0; j < dll_builtin_table[i].dll_table_length; j++) {
289 if(table[j].used){
290 used++;
291 if (table[j].handler) implemented++;
292 else
293 printf("%s.%d not implemented\n",
294 dll_builtin_table[i].dll_name,
298 tused += used;
299 timplemented += implemented;
300 if(used)
301 perc = implemented * 100.00 / used;
302 else
303 perc = 0.0;
304 if (used)
305 printf("%s: %d of %d (%3.1f %%)\n", dll_builtin_table[i].dll_name, implemented, used, perc);
307 perc = timplemented * 100.00 / tused;
308 printf("TOTAL: %d of %d winapi functions implemented (%3.1f %%)\n",timplemented, tused, perc);
310 #endif /* WINESTAT */
311 #endif /* !WINELIB */