Added stubs for msrle32.dll and implemented RLE8 decoder.
[wine] / if1632 / thunk.c
1 /*
2  * Emulator thunks
3  *
4  * Copyright 1996, 1997 Alexandre Julliard
5  * Copyright 1998       Ulrich Weigand
6  */
7
8 #include <string.h>
9 #include "wine/winbase16.h"
10 #include "callback.h"
11 #include "builtin16.h"
12 #include "module.h"
13 #include "stackframe.h"
14 #include "debugtools.h"
15
16 DEFAULT_DEBUG_CHANNEL(thunk);
17
18
19 /* List of the 16-bit callback functions. This list is used  */
20 /* by the build program to generate the file if1632/callto16.S */
21
22 /* ### start build ### */
23 extern WORD CALLBACK THUNK_CallTo16_word_lllw (FARPROC16,LONG,LONG,LONG,WORD);
24 extern WORD CALLBACK THUNK_CallTo16_word_ww   (FARPROC16,WORD,WORD);
25 /* ### stop build ### */
26
27 static THUNK *firstThunk = NULL;
28
29 CALLOUT_TABLE Callout = {
30     /* UserSignalProc */ NULL,
31     /* DestroyIcon32 */ NULL
32 };
33
34 /***********************************************************************
35  *           THUNK_Alloc
36  */
37 FARPROC THUNK_Alloc( FARPROC16 func, RELAY relay )
38 {
39     HANDLE16 hSeg;
40     NE_MODULE *pModule;
41     THUNK *thunk;
42
43     /* NULL maps to NULL */
44     if ( !func ) return NULL;
45
46     /* 
47      * If we got an 16-bit built-in API entry point, retrieve the Wine
48      * 32-bit handler for that API routine.
49      *
50      * NOTE: For efficiency reasons, we only check whether the selector
51      *       of 'func' points to the code segment of a built-in module.
52      *       It might be theoretically possible that the offset is such
53      *       that 'func' does not point, in fact, to an API entry point.
54      *       In this case, however, the pointer is corrupt anyway.
55      */
56     hSeg = GlobalHandle16( SELECTOROF( func ) );
57     pModule = NE_GetPtr( FarGetOwner16( hSeg ) );
58
59     if ( pModule && (pModule->flags & NE_FFLAGS_BUILTIN) 
60                  && NE_SEG_TABLE(pModule)[0].hSeg == hSeg )
61     {
62         FARPROC proc = (FARPROC)((ENTRYPOINT16 *)MapSL( (SEGPTR)func ))->target;
63
64         TRACE( "(%04x:%04x, %p) -> built-in API %p\n",
65                SELECTOROF( func ), OFFSETOF( func ), relay, proc );
66         return proc;
67     }
68
69     /* Otherwise, we need to alloc a thunk */
70     thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
71     if (thunk)
72     {
73         thunk->popl_eax   = 0x58;
74         thunk->pushl_func = 0x68;
75         thunk->proc       = func;
76         thunk->pushl_eax  = 0x50;
77         thunk->jmp        = 0xe9;
78         thunk->relay      = (RELAY)((char *)relay - (char *)(&thunk->next));
79         thunk->magic      = CALLTO16_THUNK_MAGIC;
80         thunk->next       = firstThunk;
81         firstThunk = thunk;
82     }
83
84     TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
85            SELECTOROF( func ), OFFSETOF( func ), relay, thunk );
86     return (FARPROC)thunk;
87 }
88
89 /***********************************************************************
90  *           THUNK_Free
91  */
92 void THUNK_Free( FARPROC thunk )
93 {
94     THUNK *t = (THUNK*)thunk;
95     if ( !t || IsBadReadPtr( t, sizeof(*t) ) 
96             || t->magic != CALLTO16_THUNK_MAGIC )
97          return;
98
99     if (HeapValidate( GetProcessHeap(), 0, t ))
100     {
101         THUNK **prev = &firstThunk;
102         while (*prev && (*prev != t)) prev = &(*prev)->next;
103         if (*prev)
104         {
105             *prev = t->next;
106             HeapFree( GetProcessHeap(), 0, t );
107             return;
108         }
109     }
110     ERR("invalid thunk addr %p\n", thunk );
111     return;
112 }
113
114
115 /***********************************************************************
116  *           THUNK_GetCalloutThunk
117  *
118  * Retrieve API entry point with given name from given module.
119  * If module is builtin, return the 32-bit entry point, otherwise
120  * create a 32->16 thunk to the 16-bit entry point, using the 
121  * given relay code.
122  *
123  */
124 static FARPROC THUNK_GetCalloutThunk( NE_MODULE *pModule, LPSTR name, RELAY relay )
125 {
126     FARPROC16 proc = GetProcAddress16( pModule->self, name );
127     if ( !proc ) return 0;
128
129     if ( pModule->flags & NE_FFLAGS_BUILTIN )
130         return (FARPROC)((ENTRYPOINT16 *)MapSL( (SEGPTR)proc ))->target;
131     else
132         return (FARPROC)THUNK_Alloc( proc, relay );
133 }
134
135 /***********************************************************************
136  *           THUNK_InitCallout
137  */
138 void THUNK_InitCallout(void)
139 {
140     NE_MODULE *pModule = NE_GetPtr( GetModuleHandle16( "USER.EXE" ) );
141     if ( pModule )
142     {
143 #define GETADDR( var, name, thk )  \
144         *(FARPROC *)&Callout.var = THUNK_GetCalloutThunk( pModule, name, \
145                                                (RELAY)THUNK_CallTo16_##thk )
146
147         GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
148         GETADDR( UserSignalProc, "SignalProc32", word_lllw );
149 #undef GETADDR
150     }
151     else WARN("no 16-bit USER\n");
152 }