Converted to the new debug interface.
[wine] / win32 / kernel32.c
1 /*
2  * KERNEL32 thunks and other undocumented stuff
3  *
4  * Copyright 1997-1998 Marcus Meissner
5  * Copyright 1998      Ulrich Weigand
6  *
7  */
8
9 #include <string.h>
10
11 #include "windef.h"
12 #include "winbase.h"
13 #include "wine/winbase16.h"
14 #include "callback.h"
15 #include "task.h"
16 #include "user.h"
17 #include "heap.h"
18 #include "module.h"
19 #include "neexe.h"
20 #include "process.h"
21 #include "stackframe.h"
22 #include "heap.h"
23 #include "selectors.h"
24 #include "task.h"
25 #include "file.h"
26 #include "debugtools.h"
27 #include "flatthunk.h"
28 #include "syslevel.h"
29 #include "winerror.h"
30
31 DECLARE_DEBUG_CHANNEL(dosmem)
32 DECLARE_DEBUG_CHANNEL(thunk)
33 DECLARE_DEBUG_CHANNEL(win32)
34
35
36 /***********************************************************************
37  *                                                                     *
38  *                 Win95 internal thunks                               *
39  *                                                                     *
40  ***********************************************************************/
41
42 /***********************************************************************
43  *           LogApiThk    (KERNEL.423)
44  */
45 void WINAPI LogApiThk( LPSTR func )
46 {
47     TRACE_(thunk)( "%s\n", debugstr_a(func) );
48 }
49
50 /***********************************************************************
51  *           LogApiThkLSF    (KERNEL32.42)
52  * 
53  * NOTE: needs to preserve all registers!
54  */
55 void WINAPI REGS_FUNC(LogApiThkLSF)( LPSTR func, CONTEXT *context )
56 {
57     TRACE_(thunk)( "%s\n", debugstr_a(func) );
58 }
59
60 /***********************************************************************
61  *           LogApiThkSL    (KERNEL32.44)
62  * 
63  * NOTE: needs to preserve all registers!
64  */
65 void WINAPI REGS_FUNC(LogApiThkSL)( LPSTR func, CONTEXT *context )
66 {
67     TRACE_(thunk)( "%s\n", debugstr_a(func) );
68 }
69
70 /***********************************************************************
71  *           LogCBThkSL    (KERNEL32.47)
72  * 
73  * NOTE: needs to preserve all registers!
74  */
75 void WINAPI REGS_FUNC(LogCBThkSL)( LPSTR func, CONTEXT *context )
76 {
77     TRACE_(thunk)( "%s\n", debugstr_a(func) );
78 }
79
80 /***********************************************************************
81  * Generates a FT_Prolog call.
82  *      
83  *  0FB6D1                  movzbl edx,cl
84  *  8B1495xxxxxxxx          mov edx,[4*edx + targetTable]
85  *  68xxxxxxxx              push FT_Prolog
86  *  C3                      lret
87  */
88 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
89         LPBYTE  x;
90
91         x       = relayCode;
92         *x++    = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
93         *x++    = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
94         x+=4;   /* mov edx, [4*edx + targetTable] */
95         *x++    = 0x68; *(DWORD*)x = (DWORD)GetProcAddress(GetModuleHandleA("KERNEL32"),"FT_Prolog");
96         x+=4;   /* push FT_Prolog */
97         *x++    = 0xC3;         /* lret */
98         /* fill rest with 0xCC / int 3 */
99 }
100
101 /***********************************************************************
102  *      _write_qtthunk                                  (internal)
103  * Generates a QT_Thunk style call.
104  *
105  *  33C9                    xor ecx, ecx
106  *  8A4DFC                  mov cl , [ebp-04]
107  *  8B148Dxxxxxxxx          mov edx, [4*ecx + targetTable]
108  *  B8yyyyyyyy              mov eax, QT_Thunk
109  *  FFE0                    jmp eax
110  */
111 static void _write_qtthunk(
112         LPBYTE relayCode,       /* [in] start of QT_Thunk stub */
113         DWORD *targetTable      /* [in] start of thunk (for index lookup) */
114 ) {
115         LPBYTE  x;
116
117         x       = relayCode;
118         *x++    = 0x33;*x++=0xC9; /* xor ecx,ecx */
119         *x++    = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
120         *x++    = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
121         x+=4;   /* mov edx, [4*ecx + targetTable */
122         *x++    = 0xB8; *(DWORD*)x = (DWORD)GetProcAddress(GetModuleHandleA("KERNEL32"),"QT_Thunk");
123         x+=4;   /* mov eax , QT_Thunk */
124         *x++    = 0xFF; *x++ = 0xE0;    /* jmp eax */
125         /* should fill the rest of the 32 bytes with 0xCC */
126 }
127
128 /***********************************************************************
129  *           _loadthunk
130  */
131 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32, 
132                          struct ThunkDataCommon *TD32, DWORD checksum)
133 {
134     struct ThunkDataCommon *TD16;
135     HMODULE hmod;
136     int ordinal;
137
138     if ((hmod = LoadLibrary16(module)) <= 32) 
139     {
140         ERR_(thunk)("(%s, %s, %s): Unable to load '%s', error %d\n",
141                    module, func, module32, module, hmod);
142         return 0;
143     }
144
145     if (   !(ordinal = NE_GetOrdinal(hmod, func))
146         || !(TD16 = PTR_SEG_TO_LIN(NE_GetEntryPointEx(hmod, ordinal, FALSE))))
147     {
148         ERR_(thunk)("(%s, %s, %s): Unable to find '%s'\n",
149                    module, func, module32, func);
150         return 0;
151     }
152
153     if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
154     {
155         ERR_(thunk)("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
156                    module, func, module32, 
157                    TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
158                    TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
159         return 0;
160     }
161
162     if (TD32 && TD16->checksum != TD32->checksum)
163     {
164         ERR_(thunk)("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
165                    module, func, module32, TD16->checksum, TD32->checksum);
166         return 0;
167     }
168
169     if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
170     {
171         ERR_(thunk)("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
172                    module, func, module32, *(LPDWORD)TD16, checksum);
173         return 0;
174     }
175
176     return TD16;
177 }
178
179 /***********************************************************************
180  *           GetThunkStuff    (KERNEL32.53)
181  */
182 LPVOID WINAPI GetThunkStuff(LPSTR module, LPSTR func)
183 {
184     return _loadthunk(module, func, "<kernel>", NULL, 0L);
185 }
186
187 /***********************************************************************
188  *           GetThunkBuff    (KERNEL32.52)
189  * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
190  */
191 LPVOID WINAPI GetThunkBuff(void)
192 {
193     return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
194 }
195
196 /***********************************************************************
197  *              ThunkConnect32          (KERNEL32)
198  * Connects a 32bit and a 16bit thunkbuffer.
199  */
200 UINT WINAPI ThunkConnect32( 
201         struct ThunkDataCommon *TD,  /* [in/out] thunkbuffer */
202         LPSTR thunkfun16,            /* [in] win16 thunkfunction */
203         LPSTR module16,              /* [in] name of win16 dll */
204         LPSTR module32,              /* [in] name of win32 dll */
205         HMODULE hmod32,            /* [in] hmodule of win32 dll */
206         DWORD dwReason               /* [in] initialisation argument */
207 ) {
208     BOOL directionSL;
209
210     if (!lstrncmpA(TD->magic, "SL01", 4))
211     {
212         directionSL = TRUE;
213
214         TRACE_(thunk)("SL01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
215                      module32, (DWORD)TD, module16, thunkfun16, dwReason);
216     }
217     else if (!lstrncmpA(TD->magic, "LS01", 4))
218     {
219         directionSL = FALSE;
220
221         TRACE_(thunk)("LS01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
222                      module32, (DWORD)TD, module16, thunkfun16, dwReason);
223     }
224     else
225     {
226         ERR_(thunk)("Invalid magic %c%c%c%c\n", 
227                    TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
228         return 0;
229     }
230     
231     switch (dwReason)
232     {
233         case DLL_PROCESS_ATTACH:
234         {
235             struct ThunkDataCommon *TD16;
236             if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
237                 return 0;
238
239             if (directionSL)
240             {
241                 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
242                 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
243                 struct SLTargetDB *tdb;
244
245                 if (SL16->fpData == NULL)
246                 {
247                     ERR_(thunk)("ThunkConnect16 was not called!\n");
248                     return 0;
249                 }
250
251                 SL32->data = SL16->fpData;
252
253                 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
254                 tdb->process = PROCESS_Current();
255                 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
256
257                 tdb->next = SL32->data->targetDB;   /* FIXME: not thread-safe! */
258                 SL32->data->targetDB = tdb;
259
260                 TRACE_(thunk)("Process %08lx allocated TargetDB entry for ThunkDataSL %08lx\n", 
261                              (DWORD)PROCESS_Current(), (DWORD)SL32->data);
262             }
263             else
264             {
265                 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
266                 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
267
268                 LS32->targetTable = PTR_SEG_TO_LIN(LS16->targetTable);
269
270                 /* write QT_Thunk and FT_Prolog stubs */
271                 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk,  LS32->targetTable);
272                 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
273             }
274             break;
275         }
276
277         case DLL_PROCESS_DETACH:
278             /* FIXME: cleanup */
279             break;
280     }
281
282     return 1;
283 }
284
285 /**********************************************************************
286  *              QT_Thunk                        (KERNEL32)
287  *
288  * The target address is in EDX.
289  * The 16 bit arguments start at ESP.
290  * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
291  * [ok]
292  */
293 void WINAPI REGS_FUNC(QT_Thunk)( CONTEXT *context )
294 {
295 #ifdef __i386__
296     CONTEXT86 context16;
297     DWORD argsize;
298
299     memcpy(&context16,context,sizeof(context16));
300
301     CS_reg(&context16)  = HIWORD(EDX_reg(context));
302     IP_reg(&context16)  = LOWORD(EDX_reg(context));
303     EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
304                            + (WORD)&((STACK16FRAME*)0)->bp;
305
306     argsize = EBP_reg(context)-ESP_reg(context)-0x40;
307
308     memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
309             (LPBYTE)ESP_reg(context), argsize );
310
311     EAX_reg(context) = Callbacks->CallRegisterShortProc( &context16, argsize );
312     EDX_reg(context) = HIWORD(EAX_reg(context));
313     EAX_reg(context) = LOWORD(EAX_reg(context));
314 #endif
315 }
316
317
318 /**********************************************************************
319  *              FT_Prolog                       (KERNEL32.233)
320  * 
321  * The set of FT_... thunk routines is used instead of QT_Thunk,
322  * if structures have to be converted from 32-bit to 16-bit
323  * (change of member alignment, conversion of members).
324  *
325  * The thunk function (as created by the thunk compiler) calls
326  * FT_Prolog at the beginning, to set up a stack frame and
327  * allocate a 64 byte buffer on the stack.
328  * The input parameters (target address and some flags) are
329  * saved for later use by FT_Thunk.
330  *
331  * Input:  EDX  16-bit target address (SEGPTR)
332  *         CX   bits  0..7   target number (in target table)
333  *              bits  8..9   some flags (unclear???)
334  *              bits 10..15  number of DWORD arguments
335  *
336  * Output: A new stackframe is created, and a 64 byte buffer
337  *         allocated on the stack. The layout of the stack 
338  *         on return is as follows:
339  *
340  *  (ebp+4)  return address to caller of thunk function
341  *  (ebp)    old EBP
342  *  (ebp-4)  saved EBX register of caller
343  *  (ebp-8)  saved ESI register of caller
344  *  (ebp-12) saved EDI register of caller
345  *  (ebp-16) saved ECX register, containing flags
346  *  (ebp-20) bitmap containing parameters that are to be converted
347  *           by FT_Thunk; it is initialized to 0 by FT_Prolog and
348  *           filled in by the thunk code before calling FT_Thunk
349  *  (ebp-24)
350  *    ...    (unclear)
351  *  (ebp-44)
352  *  (ebp-48) saved EAX register of caller (unclear, never restored???)
353  *  (ebp-52) saved EDX register, containing 16-bit thunk target
354  *  (ebp-56)
355  *    ...    (unclear)
356  *  (ebp-64)
357  *
358  *  ESP is EBP-64 after return.
359  *         
360  */
361
362 void WINAPI REGS_FUNC(FT_Prolog)( CONTEXT *context )
363 {
364 #ifdef __i386__
365     /* Build stack frame */
366     STACK32_PUSH(context, EBP_reg(context));
367     EBP_reg(context) = ESP_reg(context);
368
369     /* Allocate 64-byte Thunk Buffer */
370     ESP_reg(context) -= 64;
371     memset((char *)ESP_reg(context), '\0', 64);
372
373     /* Store Flags (ECX) and Target Address (EDX) */
374     /* Save other registers to be restored later */
375     *(DWORD *)(EBP_reg(context) -  4) = EBX_reg(context);
376     *(DWORD *)(EBP_reg(context) -  8) = ESI_reg(context);
377     *(DWORD *)(EBP_reg(context) - 12) = EDI_reg(context);
378     *(DWORD *)(EBP_reg(context) - 16) = ECX_reg(context);
379
380     *(DWORD *)(EBP_reg(context) - 48) = EAX_reg(context);
381     *(DWORD *)(EBP_reg(context) - 52) = EDX_reg(context);
382 #endif
383 }
384
385 /**********************************************************************
386  *              FT_Thunk                        (KERNEL32.234)
387  *
388  * This routine performs the actual call to 16-bit code, 
389  * similar to QT_Thunk. The differences are:
390  *  - The call target is taken from the buffer created by FT_Prolog
391  *  - Those arguments requested by the thunk code (by setting the
392  *    corresponding bit in the bitmap at EBP-20) are converted
393  *    from 32-bit pointers to segmented pointers (those pointers
394  *    are guaranteed to point to structures copied to the stack
395  *    by the thunk code, so we always use the 16-bit stack selector
396  *    for those addresses).
397  * 
398  *    The bit #i of EBP-20 corresponds here to the DWORD starting at
399  *    ESP+4 + 2*i.
400  * 
401  * FIXME: It is unclear what happens if there are more than 32 WORDs 
402  *        of arguments, so that the single DWORD bitmap is no longer
403  *        sufficient ...
404  */
405
406 void WINAPI REGS_FUNC(FT_Thunk)( CONTEXT *context )
407 {
408 #ifdef __i386__
409     DWORD mapESPrelative = *(DWORD *)(EBP_reg(context) - 20);
410     DWORD callTarget     = *(DWORD *)(EBP_reg(context) - 52);
411
412     CONTEXT86 context16;
413     DWORD i, argsize;
414     LPBYTE newstack, oldstack;
415
416     memcpy(&context16,context,sizeof(context16));
417
418     CS_reg(&context16)  = HIWORD(callTarget);
419     IP_reg(&context16)  = LOWORD(callTarget);
420     EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
421                            + (WORD)&((STACK16FRAME*)0)->bp;
422
423     argsize  = EBP_reg(context)-ESP_reg(context)-0x40;
424     newstack = (LPBYTE)CURRENT_STACK16 - argsize;
425     oldstack = (LPBYTE)ESP_reg(context);
426
427     memcpy( newstack, oldstack, argsize );
428
429     for (i = 0; i < 32; i++)    /* NOTE: What about > 32 arguments? */
430         if (mapESPrelative & (1 << i))
431         {
432             SEGPTR *arg = (SEGPTR *)(newstack + 2*i);
433             *arg = PTR_SEG_OFF_TO_SEGPTR(SELECTOROF(NtCurrentTeb()->cur_stack), 
434                                          OFFSETOF(NtCurrentTeb()->cur_stack) - argsize
435                                          + (*(LPBYTE *)arg - oldstack));
436         }
437
438     EAX_reg(context) = Callbacks->CallRegisterShortProc( &context16, argsize );
439     EDX_reg(context) = HIWORD(EAX_reg(context));
440     EAX_reg(context) = LOWORD(EAX_reg(context));
441
442     /* Copy modified buffers back to 32-bit stack */
443     memcpy( oldstack, newstack, argsize );
444 #endif
445 }
446
447 /**********************************************************************
448  *              FT_ExitNN               (KERNEL32.218 - 232)
449  *
450  * One of the FT_ExitNN functions is called at the end of the thunk code.
451  * It removes the stack frame created by FT_Prolog, moves the function
452  * return from EBX to EAX (yes, FT_Thunk did use EAX for the return 
453  * value, but the thunk code has moved it from EAX to EBX in the 
454  * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
455  * and perform a return to the CALLER of the thunk code (while removing
456  * the given number of arguments from the caller's stack).
457  */
458
459 static void FT_Exit(CONTEXT *context, int nPopArgs)
460 {
461 #ifdef __i386__
462     /* Return value is in EBX */
463     EAX_reg(context) = EBX_reg(context);
464
465     /* Restore EBX, ESI, and EDI registers */
466     EBX_reg(context) = *(DWORD *)(EBP_reg(context) -  4);
467     ESI_reg(context) = *(DWORD *)(EBP_reg(context) -  8);
468     EDI_reg(context) = *(DWORD *)(EBP_reg(context) - 12);
469
470     /* Clean up stack frame */
471     ESP_reg(context) = EBP_reg(context);
472     EBP_reg(context) = STACK32_POP(context);
473
474     /* Pop return address to CALLER of thunk code */
475     EIP_reg(context) = STACK32_POP(context);
476     /* Remove arguments */
477     ESP_reg(context) += nPopArgs;
478 #endif
479 }
480
481 void WINAPI REGS_FUNC(FT_Exit0)(CONTEXT *context)  { FT_Exit(context,  0); }
482 void WINAPI REGS_FUNC(FT_Exit4)(CONTEXT *context)  { FT_Exit(context,  4); }
483 void WINAPI REGS_FUNC(FT_Exit8)(CONTEXT *context)  { FT_Exit(context,  8); }
484 void WINAPI REGS_FUNC(FT_Exit12)(CONTEXT *context) { FT_Exit(context, 12); }
485 void WINAPI REGS_FUNC(FT_Exit16)(CONTEXT *context) { FT_Exit(context, 16); }
486 void WINAPI REGS_FUNC(FT_Exit20)(CONTEXT *context) { FT_Exit(context, 20); }
487 void WINAPI REGS_FUNC(FT_Exit24)(CONTEXT *context) { FT_Exit(context, 24); }
488 void WINAPI REGS_FUNC(FT_Exit28)(CONTEXT *context) { FT_Exit(context, 28); }
489 void WINAPI REGS_FUNC(FT_Exit32)(CONTEXT *context) { FT_Exit(context, 32); }
490 void WINAPI REGS_FUNC(FT_Exit36)(CONTEXT *context) { FT_Exit(context, 36); }
491 void WINAPI REGS_FUNC(FT_Exit40)(CONTEXT *context) { FT_Exit(context, 40); }
492 void WINAPI REGS_FUNC(FT_Exit44)(CONTEXT *context) { FT_Exit(context, 44); }
493 void WINAPI REGS_FUNC(FT_Exit48)(CONTEXT *context) { FT_Exit(context, 48); }
494 void WINAPI REGS_FUNC(FT_Exit52)(CONTEXT *context) { FT_Exit(context, 52); }
495 void WINAPI REGS_FUNC(FT_Exit56)(CONTEXT *context) { FT_Exit(context, 56); }
496
497
498 /**********************************************************************
499  *           WOWCallback16 (KERNEL32.62)(WOW32.2)
500  * Calls a win16 function with a single DWORD argument.
501  * RETURNS
502  *      the return value
503  */
504 DWORD WINAPI WOWCallback16(
505         FARPROC16 fproc,        /* [in] win16 function to call */
506         DWORD arg               /* [in] single DWORD argument to function */
507 ) {
508         DWORD   ret;
509         TRACE_(thunk)("(%p,0x%08lx)...\n",fproc,arg);
510         ret =  Callbacks->CallWOWCallbackProc(fproc,arg);
511         TRACE_(thunk)("... returns %ld\n",ret);
512         return ret;
513 }
514
515 /**********************************************************************
516  *           WOWCallback16Ex (KERNEL32.55)(WOW32.3)
517  * Calls a function in 16bit code.
518  * RETURNS
519  *      TRUE for success
520  */
521 BOOL WINAPI WOWCallback16Ex(
522         FARPROC16 vpfn16,       /* [in] win16 function to call */
523         DWORD dwFlags,          /* [in] flags */
524         DWORD cbArgs,           /* [in] nr of arguments */
525         LPVOID pArgs,           /* [in] pointer to arguments (LPDWORD) */
526         LPDWORD pdwRetCode      /* [out] return value of win16 function */
527 ) {
528         return Callbacks->CallWOWCallback16Ex(vpfn16,dwFlags,cbArgs,pArgs,pdwRetCode);
529 }
530
531 /***********************************************************************
532  *              ThunkInitLS     (KERNEL32.43)
533  * A thunkbuffer link routine 
534  * The thunkbuf looks like:
535  *
536  *      00: DWORD       length          ? don't know exactly
537  *      04: SEGPTR      ptr             ? where does it point to?
538  * The pointer ptr is written into the first DWORD of 'thunk'.
539  * (probably correct implemented)
540  * [ok probably]
541  * RETURNS
542  *      segmented pointer to thunk?
543  */
544 DWORD WINAPI ThunkInitLS(
545         LPDWORD thunk,  /* [in] win32 thunk */
546         LPCSTR thkbuf,  /* [in] thkbuffer name in win16 dll */
547         DWORD len,      /* [in] thkbuffer length */
548         LPCSTR dll16,   /* [in] name of win16 dll */
549         LPCSTR dll32    /* [in] name of win32 dll (FIXME: not used?) */
550 ) {
551         LPDWORD         addr;
552
553         if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
554                 return 0;
555
556         if (!addr[1])
557                 return 0;
558         *(DWORD*)thunk = addr[1];
559
560         return addr[1];
561 }
562
563 /***********************************************************************
564  *              Common32ThkLS   (KERNEL32.45)
565  * 
566  * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
567  * style thunks. The basic difference is that the parameter conversion 
568  * is done completely on the *16-bit* side here. Thus we do not call
569  * the 16-bit target directly, but call a common entry point instead.
570  * This entry function then calls the target according to the target
571  * number passed in the DI register.
572  * 
573  * Input:  EAX    SEGPTR to the common 16-bit entry point
574  *         CX     offset in thunk table (target number * 4)
575  *         DX     error return value if execution fails (unclear???)
576  *         EDX.HI number of DWORD parameters
577  *
578  * (Note that we need to move the thunk table offset from CX to DI !)
579  *
580  * The called 16-bit stub expects its stack to look like this:
581  *     ...
582  *   (esp+40)  32-bit arguments
583  *     ...
584  *   (esp+8)   32 byte of stack space available as buffer
585  *   (esp)     8 byte return address for use with 0x66 lret 
586  * 
587  * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
588  * and uses the EAX register to return a DWORD return value.
589  * Thus we need to use a special assembly glue routine 
590  * (CallRegisterLongProc instead of CallRegisterShortProc).
591  *
592  * Finally, we return to the caller, popping the arguments off 
593  * the stack.
594  *
595  * FIXME: The called function uses EBX to return the number of 
596  *        arguments that are to be popped off the caller's stack.
597  *        This is clobbered by the assembly glue, so we simply use
598  *        the original EDX.HI to get the number of arguments.
599  *        (Those two values should be equal anyway ...?)
600  * 
601  */
602 void WINAPI REGS_FUNC(Common32ThkLS)( CONTEXT *context )
603 {
604 #ifdef __i386__
605     CONTEXT86 context16;
606     DWORD argsize;
607
608     memcpy(&context16,context,sizeof(context16));
609
610     DI_reg(&context16)  = CX_reg(context);
611     CS_reg(&context16)  = HIWORD(EAX_reg(context));
612     IP_reg(&context16)  = LOWORD(EAX_reg(context));
613     EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
614                            + (WORD)&((STACK16FRAME*)0)->bp;
615
616     argsize = HIWORD(EDX_reg(context)) * 4;
617
618     /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
619     if (EDX_reg(context) == EIP_reg(context))
620         argsize = 6 * 4;
621
622     memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
623             (LPBYTE)ESP_reg(context), argsize );
624
625     EAX_reg(context) = Callbacks->CallRegisterLongProc(&context16, argsize + 32);
626
627     /* Clean up caller's stack frame */
628     ESP_reg(context) += argsize;
629 #endif
630 }
631
632 /***********************************************************************
633  *              OT_32ThkLSF     (KERNEL32.40)
634  *
635  * YET Another 32->16 thunk. The difference to Common32ThkLS is that
636  * argument processing is done on both the 32-bit and the 16-bit side:
637  * The 32-bit side prepares arguments, copying them onto the stack.
638  * 
639  * When this routine is called, the first word on the stack is the 
640  * number of argument bytes prepared by the 32-bit code, and EDX
641  * contains the 16-bit target address.
642  *
643  * The called 16-bit routine is another relaycode, doing further 
644  * argument processing and then calling the real 16-bit target
645  * whose address is stored at [bp-04].
646  *
647  * The call proceeds using a normal CallRegisterShortProc.
648  * After return from the 16-bit relaycode, the arguments need
649  * to be copied *back* to the 32-bit stack, since the 32-bit
650  * relaycode processes output parameters.
651  * 
652  * Note that we copy twice the number of arguments, since some of the
653  * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
654  * arguments of the caller!
655  *
656  * (Note that this function seems only to be used for
657  *  OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
658  */
659 void WINAPI REGS_FUNC(OT_32ThkLSF)( CONTEXT *context )
660 {
661 #ifdef __i386__
662     CONTEXT86 context16;
663     DWORD argsize;
664
665     memcpy(&context16,context,sizeof(context16));
666
667     CS_reg(&context16)  = HIWORD(EDX_reg(context));
668     IP_reg(&context16)  = LOWORD(EDX_reg(context));
669     EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
670                            + (WORD)&((STACK16FRAME*)0)->bp;
671
672     argsize = 2 * *(WORD *)ESP_reg(context) + 2;
673
674     memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
675             (LPBYTE)ESP_reg(context), argsize );
676
677     EAX_reg(context) = Callbacks->CallRegisterShortProc(&context16, argsize);
678
679     memcpy( (LPBYTE)ESP_reg(context), 
680             (LPBYTE)CURRENT_STACK16 - argsize, argsize );
681 #endif
682 }
683
684 /***********************************************************************
685  *              ThunkInitLSF            (KERNEL32.41)
686  * A thunk setup routine.
687  * Expects a pointer to a preinitialized thunkbuffer in the first argument
688  * looking like:
689  *      00..03:         unknown (pointer, check _41, _43, _46)
690  *      04: EB1E                jmp +0x20
691  *
692  *      06..23:         unknown (space for replacement code, check .90)
693  *
694  *      24:>E800000000          call offset 29
695  *      29:>58                  pop eax            ( target of call )
696  *      2A: 2D25000000          sub eax,0x00000025 ( now points to offset 4 )
697  *      2F: BAxxxxxxxx          mov edx,xxxxxxxx
698  *      34: 68yyyyyyyy          push KERNEL32.90
699  *      39: C3                  ret
700  *
701  *      3A: EB1E                jmp +0x20
702  *      3E ... 59:      unknown (space for replacement code?)
703  *      5A: E8xxxxxxxx          call <32bitoffset xxxxxxxx>
704  *      5F: 5A                  pop edx
705  *      60: 81EA25xxxxxx        sub edx, 0x25xxxxxx
706  *      66: 52                  push edx
707  *      67: 68xxxxxxxx          push xxxxxxxx
708  *      6C: 68yyyyyyyy          push KERNEL32.89
709  *      71: C3                  ret
710  *      72: end?
711  * This function checks if the code is there, and replaces the yyyyyyyy entries
712  * by the functionpointers.
713  * The thunkbuf looks like:
714  *
715  *      00: DWORD       length          ? don't know exactly
716  *      04: SEGPTR      ptr             ? where does it point to?
717  * The segpointer ptr is written into the first DWORD of 'thunk'.
718  * [ok probably]
719  * RETURNS
720  *      unclear, pointer to win16 thkbuffer?
721  */
722 LPVOID WINAPI ThunkInitLSF(
723         LPBYTE thunk,   /* [in] win32 thunk */
724         LPCSTR thkbuf,  /* [in] thkbuffer name in win16 dll */
725         DWORD len,      /* [in] length of thkbuffer */
726         LPCSTR dll16,   /* [in] name of win16 dll */
727         LPCSTR dll32    /* [in] name of win32 dll */
728 ) {
729         HMODULE hkrnl32 = GetModuleHandleA("KERNEL32");
730         LPDWORD         addr,addr2;
731
732         /* FIXME: add checks for valid code ... */
733         /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
734         *(DWORD*)(thunk+0x35) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)90);
735         *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)89);
736
737         
738         if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
739                 return 0;
740
741         addr2 = PTR_SEG_TO_LIN(addr[1]);
742         if (HIWORD(addr2))
743                 *(DWORD*)thunk = (DWORD)addr2;
744
745         return addr2;
746 }
747
748 /***********************************************************************
749  *              FT_PrologPrime                  (KERNEL32.89)
750  * 
751  * This function is called from the relay code installed by
752  * ThunkInitLSF. It replaces the location from where it was 
753  * called by a standard FT_Prolog call stub (which is 'primed'
754  * by inserting the correct target table pointer).
755  * Finally, it calls that stub.
756  * 
757  * Input:  ECX    target number + flags (passed through to FT_Prolog)
758  *        (ESP)   offset of location where target table pointer 
759  *                is stored, relative to the start of the relay code
760  *        (ESP+4) pointer to start of relay code
761  *                (this is where the FT_Prolog call stub gets written to)
762  * 
763  * Note: The two DWORD arguments get popped off the stack.
764  *        
765  */
766 void WINAPI REGS_FUNC(FT_PrologPrime)( CONTEXT *context )
767 {
768 #ifdef __i386__
769     DWORD  targetTableOffset;
770     LPBYTE relayCode;
771
772     /* Compensate for the fact that the Wine register relay code thought
773        we were being called, although we were in fact jumped to */
774     ESP_reg(context) -= 4;
775
776     /* Write FT_Prolog call stub */
777     targetTableOffset = STACK32_POP(context);
778     relayCode = (LPBYTE)STACK32_POP(context);
779     _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
780
781     /* Jump to the call stub just created */
782     EIP_reg(context) = (DWORD)relayCode;
783 #endif
784 }
785
786 /***********************************************************************
787  *              QT_ThunkPrime                   (KERNEL32.90)
788  *
789  * This function corresponds to FT_PrologPrime, but installs a 
790  * call stub for QT_Thunk instead.
791  *
792  * Input: (EBP-4) target number (passed through to QT_Thunk)
793  *         EDX    target table pointer location offset
794  *         EAX    start of relay code
795  *      
796  */
797 void WINAPI REGS_FUNC(QT_ThunkPrime)( CONTEXT *context )
798 {
799 #ifdef __i386__
800     DWORD  targetTableOffset;
801     LPBYTE relayCode;
802
803     /* Compensate for the fact that the Wine register relay code thought
804        we were being called, although we were in fact jumped to */
805     ESP_reg(context) -= 4;
806
807     /* Write QT_Thunk call stub */
808     targetTableOffset = EDX_reg(context);
809     relayCode = (LPBYTE)EAX_reg(context);
810     _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
811
812     /* Jump to the call stub just created */
813     EIP_reg(context) = (DWORD)relayCode;
814 #endif
815 }
816
817 /***********************************************************************
818  *                                                      (KERNEL32.46)
819  * Another thunkbuf link routine.
820  * The start of the thunkbuf looks like this:
821  *      00: DWORD       length
822  *      04: SEGPTR      address for thunkbuffer pointer
823  * [ok probably]
824  */
825 VOID WINAPI ThunkInitSL(
826         LPBYTE thunk,           /* [in] start of thunkbuffer */
827         LPCSTR thkbuf,          /* [in] name/ordinal of thunkbuffer in win16 dll */
828         DWORD len,              /* [in] length of thunkbuffer */
829         LPCSTR dll16,           /* [in] name of win16 dll containing the thkbuf */
830         LPCSTR dll32            /* [in] win32 dll. FIXME: strange, unused */
831 ) {
832         LPDWORD         addr;
833
834         if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
835                 return;
836
837         *(DWORD*)PTR_SEG_TO_LIN(addr[1]) = (DWORD)thunk;
838 }
839
840 /**********************************************************************
841  *           SSInit             KERNEL.700
842  * RETURNS
843  *      TRUE for success.
844  */
845 BOOL WINAPI SSInit16()
846 {
847     return TRUE;
848 }
849
850 /**********************************************************************
851  *           SSOnBigStack       KERNEL32.87
852  * Check if thunking is initialized (ss selector set up etc.)
853  * We do that differently, so just return TRUE.
854  * [ok]
855  * RETURNS
856  *      TRUE for success.
857  */
858 BOOL WINAPI SSOnBigStack()
859 {
860     TRACE_(thunk)("Yes, thunking is initialized\n");
861     return TRUE;
862 }
863
864 /**********************************************************************
865  *           SSConfirmSmallStack     KERNEL.704
866  *
867  * Abort if not on small stack.
868  *
869  * This must be a register routine as it has to preserve *all* registers.
870  */
871 void WINAPI SSConfirmSmallStack( CONTEXT86 *context )
872 {
873     /* We are always on the small stack while in 16-bit code ... */
874 }
875
876 /**********************************************************************
877  *           SSCall
878  * One of the real thunking functions. This one seems to be for 32<->32
879  * thunks. It should probably be capable of crossing processboundaries.
880  *
881  * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
882  * [ok]
883  */
884 DWORD WINAPIV SSCall(
885         DWORD nr,       /* [in] number of argument bytes */
886         DWORD flags,    /* [in] FIXME: flags ? */
887         FARPROC fun,    /* [in] function to call */
888         ...             /* [in/out] arguments */
889 ) {
890     DWORD i,ret;
891     DWORD *args = ((DWORD *)&fun) + 1;
892
893     if(TRACE_ON(thunk)){
894       dbg_decl_str(thunk, 256);
895       for (i=0;i<nr/4;i++) 
896         dsprintf(thunk,"0x%08lx,",args[i]);
897       TRACE_(thunk)("(%ld,0x%08lx,%p,[%s])\n",
898                     nr,flags,fun,dbg_str(thunk));
899     }
900     switch (nr) {
901     case 0:     ret = fun();
902                 break;
903     case 4:     ret = fun(args[0]);
904                 break;
905     case 8:     ret = fun(args[0],args[1]);
906                 break;
907     case 12:    ret = fun(args[0],args[1],args[2]);
908                 break;
909     case 16:    ret = fun(args[0],args[1],args[2],args[3]);
910                 break;
911     case 20:    ret = fun(args[0],args[1],args[2],args[3],args[4]);
912                 break;
913     case 24:    ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
914                 break;
915     case 28:    ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
916                 break;
917     case 32:    ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
918                 break;
919     case 36:    ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
920                 break;
921     case 40:    ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
922                 break;
923     case 44:    ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]);
924                 break;
925     case 48:    ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11]);
926                 break;
927     default:
928         WARN_(thunk)("Unsupported nr of arguments, %ld\n",nr);
929         ret = 0;
930         break;
931
932     }
933     TRACE_(thunk)(" returning %ld ...\n",ret);
934     return ret;
935 }
936
937 /**********************************************************************
938  *           W32S_BackTo32                      (KERNEL32.51)
939  */
940 void WINAPI REGS_FUNC(W32S_BackTo32)( CONTEXT *context )
941 {
942 #ifdef __i386__
943     LPDWORD stack = (LPDWORD)ESP_reg( context );
944     FARPROC proc = (FARPROC)EIP_reg(context);
945
946     EAX_reg( context ) = proc( stack[1], stack[2], stack[3], stack[4], stack[5],
947                                stack[6], stack[7], stack[8], stack[9], stack[10] );
948
949     EIP_reg( context ) = STACK32_POP(context);
950 #endif
951 }
952
953 /**********************************************************************
954  *                      AllocSLCallback         (KERNEL32)
955  *
956  * Win95 uses some structchains for callbacks. It allocates them
957  * in blocks of 100 entries, size 32 bytes each, layout:
958  * blockstart:
959  *      0:      PTR     nextblockstart
960  *      4:      entry   *first;
961  *      8:      WORD    sel ( start points to blockstart)
962  *      A:      WORD    unknown
963  * 100xentry:
964  *      00..17:         Code
965  *      18:     PDB     *owning_process;
966  *      1C:     PTR     blockstart
967  *
968  * We ignore this for now. (Just a note for further developers)
969  * FIXME: use this method, so we don't waste selectors...
970  *
971  * Following code is then generated by AllocSLCallback. The code is 16 bit, so
972  * the 0x66 prefix switches from word->long registers.
973  *
974  *      665A            pop     edx 
975  *      6668x arg2 x    pushl   <arg2>
976  *      6652            push    edx
977  *      EAx arg1 x      jmpf    <arg1>
978  *
979  * returns the startaddress of this thunk.
980  *
981  * Note, that they look very similair to the ones allocates by THUNK_Alloc.
982  * RETURNS
983  *      segmented pointer to the start of the thunk
984  */
985 DWORD WINAPI
986 AllocSLCallback(
987         DWORD finalizer,        /* [in] finalizer function */
988         DWORD callback          /* [in] callback function */
989 ) {
990         LPBYTE  x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
991         WORD    sel;
992
993         x=thunk;
994         *x++=0x66;*x++=0x5a;                            /* popl edx */
995         *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4;  /* pushl finalizer */
996         *x++=0x66;*x++=0x52;                            /* pushl edx */
997         *x++=0xea;*(DWORD*)x=callback;x+=4;             /* jmpf callback */
998
999         *(PDB**)(thunk+18) = PROCESS_Current();
1000
1001         sel = SELECTOR_AllocBlock( thunk , 32, SEGMENT_CODE, FALSE, FALSE );
1002         return (sel<<16)|0;
1003 }
1004
1005 /**********************************************************************
1006  *              FreeSLCallback          (KERNEL32.274)
1007  * Frees the specified 16->32 callback
1008  */
1009 void WINAPI
1010 FreeSLCallback(
1011         DWORD x /* [in] 16 bit callback (segmented pointer?) */
1012 ) {
1013         FIXME_(win32)("(0x%08lx): stub\n",x);
1014 }
1015
1016
1017 /**********************************************************************
1018  *              GetTEBSelectorFS        (KERNEL.475)
1019  *      Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1020  */
1021 VOID WINAPI GetTEBSelectorFS16( CONTEXT86 *context ) 
1022 {
1023     GET_FS( FS_reg(context) );
1024 }
1025
1026 /**********************************************************************
1027  *              KERNEL_431              (KERNEL.431)
1028  *              IsPeFormat              (W32SYS.2)
1029  * Checks the passed filename if it is a PE format executeable
1030  * RETURNS
1031  *  TRUE, if it is.
1032  *  FALSE if not.
1033  */
1034 BOOL16 WINAPI IsPeFormat16(
1035         LPSTR   fn,     /* [in] filename to executeable */
1036         HFILE16 hf16    /* [in] open file, if filename is NULL */
1037 ) {
1038         IMAGE_DOS_HEADER        mzh;
1039         HFILE                 hf=FILE_GetHandle(hf16);
1040         OFSTRUCT                ofs;
1041         DWORD                   xmagic;
1042
1043         if (fn) {
1044                 hf = OpenFile(fn,&ofs,OF_READ);
1045                 if (hf==HFILE_ERROR)
1046                         return FALSE;
1047         }
1048         _llseek(hf,0,SEEK_SET);
1049         if (sizeof(mzh)!=_lread(hf,&mzh,sizeof(mzh))) {
1050                 _lclose(hf);
1051                 return FALSE;
1052         }
1053         if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) {
1054                 WARN_(dosmem)("File has not got dos signature!\n");
1055                 _lclose(hf);
1056                 return FALSE;
1057         }
1058         _llseek(hf,mzh.e_lfanew,SEEK_SET);
1059         if (sizeof(DWORD)!=_lread(hf,&xmagic,sizeof(DWORD))) {
1060                 _lclose(hf);
1061                 return FALSE;
1062         }
1063         _lclose(hf);
1064         return (xmagic == IMAGE_NT_SIGNATURE);
1065 }
1066
1067 /***********************************************************************
1068  *           WOWHandle32                        (KERNEL32.57)(WOW32.16)
1069  * Converts a win16 handle of type into the respective win32 handle.
1070  * We currently just return this handle, since most handles are the same
1071  * for win16 and win32.
1072  * RETURNS
1073  *      The new handle
1074  */
1075 HANDLE WINAPI WOWHandle32(
1076         WORD handle,            /* [in] win16 handle */
1077         WOW_HANDLE_TYPE type    /* [in] handle type */
1078 ) {
1079         TRACE_(win32)("(0x%04x,%d)\n",handle,type);
1080         return (HANDLE)handle;
1081 }
1082
1083 /***********************************************************************
1084  *           K32Thk1632Prolog                   (KERNEL32.492)
1085  */
1086 void WINAPI REGS_FUNC(K32Thk1632Prolog)( CONTEXT *context )
1087 {
1088 #ifdef __i386__
1089    LPBYTE code = (LPBYTE)EIP_reg(context) - 5;
1090
1091    /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1092       of 16->32 thunks instead of using one of the standard methods!
1093       This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1094       and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1095       Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1096       bypassed, which means it will crash the next time the 32-bit OLE 
1097       code thunks down again to 16-bit (this *will* happen!).
1098
1099       The following hack tries to recognize this situation.
1100       This is possible since the called stubs in OLECLI32/OLESVR32 all
1101       look exactly the same:
1102         00   E8xxxxxxxx    call K32Thk1632Prolog
1103         05   FF55FC        call [ebp-04]
1104         08   E8xxxxxxxx    call K32Thk1632Epilog
1105         0D   66CB          retf
1106
1107       If we recognize this situation, we try to simulate the actions
1108       of our CallTo/CallFrom mechanism by copying the 16-bit stack
1109       to our 32-bit stack, creating a proper STACK16FRAME and 
1110       updating cur_stack. */ 
1111
1112    if (   code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1113        && code[13] == 0x66 && code[14] == 0xCB)
1114    {
1115       WORD  stackSel  = NtCurrentTeb()->stack_sel;
1116       DWORD stackBase = GetSelectorBase(stackSel);
1117
1118       DWORD argSize = EBP_reg(context) - ESP_reg(context);
1119       char *stack16 = (char *)ESP_reg(context) - 4;
1120       char *stack32 = (char *)NtCurrentTeb()->cur_stack - argSize;
1121       STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1122
1123       TRACE_(thunk)("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1124                    EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1125
1126       memset(frame16, '\0', sizeof(STACK16FRAME));
1127       frame16->frame32 = (STACK32FRAME *)NtCurrentTeb()->cur_stack;
1128       frame16->ebp = EBP_reg(context);
1129
1130       memcpy(stack32, stack16, argSize);
1131       NtCurrentTeb()->cur_stack = PTR_SEG_OFF_TO_SEGPTR(stackSel, (DWORD)frame16 - stackBase);
1132
1133       ESP_reg(context) = (DWORD)stack32 + 4;
1134       EBP_reg(context) = ESP_reg(context) + argSize;
1135
1136       TRACE_(thunk)("after  SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1137                    EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1138    }
1139
1140    SYSLEVEL_ReleaseWin16Lock();
1141 #endif
1142 }
1143
1144 /***********************************************************************
1145  *           K32Thk1632Epilog                   (KERNEL32.491)
1146  */
1147 void WINAPI REGS_FUNC(K32Thk1632Epilog)( CONTEXT *context )
1148 {
1149 #ifdef __i386__
1150    LPBYTE code = (LPBYTE)EIP_reg(context) - 13;
1151
1152    SYSLEVEL_RestoreWin16Lock();
1153
1154    /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1155
1156    if (   code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1157        && code[13] == 0x66 && code[14] == 0xCB)
1158    {
1159       STACK16FRAME *frame16 = (STACK16FRAME *)PTR_SEG_TO_LIN(NtCurrentTeb()->cur_stack);
1160       char *stack16 = (char *)(frame16 + 1);
1161       DWORD argSize = frame16->ebp - (DWORD)stack16;
1162       char *stack32 = (char *)frame16->frame32 - argSize;
1163
1164       DWORD nArgsPopped = ESP_reg(context) - (DWORD)stack32;
1165
1166       TRACE_(thunk)("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1167                    EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1168
1169       NtCurrentTeb()->cur_stack = (DWORD)frame16->frame32;
1170
1171       ESP_reg(context) = (DWORD)stack16 + nArgsPopped;
1172       EBP_reg(context) = frame16->ebp;
1173
1174       TRACE_(thunk)("after  SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1175                    EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1176    }
1177 #endif
1178 }
1179
1180 /***********************************************************************
1181  *           UpdateResource32A                 (KERNEL32.707)
1182  */
1183 BOOL WINAPI UpdateResourceA(
1184   HANDLE  hUpdate,
1185   LPCSTR  lpType,
1186   LPCSTR  lpName,
1187   WORD    wLanguage,
1188   LPVOID  lpData,
1189   DWORD   cbData) {
1190
1191   FIXME_(win32)(": stub\n");
1192   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1193   return FALSE;
1194 }
1195
1196 /***********************************************************************
1197  *           UpdateResource32W                 (KERNEL32.708)
1198  */
1199 BOOL WINAPI UpdateResourceW(
1200   HANDLE  hUpdate,
1201   LPCWSTR lpType,
1202   LPCWSTR lpName,
1203   WORD    wLanguage,
1204   LPVOID  lpData,
1205   DWORD   cbData) {
1206
1207   FIXME_(win32)(": stub\n");
1208   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1209   return FALSE;
1210 }
1211
1212
1213 /***********************************************************************
1214  *           WaitNamedPipe32A                 [KERNEL32.725]
1215  */
1216 BOOL WINAPI WaitNamedPipeA (LPCSTR lpNamedPipeName, DWORD nTimeOut)
1217 {       FIXME_(win32)("%s 0x%08lx\n",lpNamedPipeName,nTimeOut);
1218         SetLastError(ERROR_PIPE_NOT_CONNECTED);
1219         return FALSE;
1220 }
1221 /***********************************************************************
1222  *           WaitNamedPipe32W                 [KERNEL32.726]
1223  */
1224 BOOL WINAPI WaitNamedPipeW (LPCWSTR lpNamedPipeName, DWORD nTimeOut)
1225 {       FIXME_(win32)("%s 0x%08lx\n",debugstr_w(lpNamedPipeName),nTimeOut);
1226         SetLastError(ERROR_PIPE_NOT_CONNECTED);
1227         return FALSE;
1228 }
1229
1230 /*********************************************************************
1231  *                   PK16FNF [KERNEL32.91]
1232  *
1233  *  This routine fills in the supplied 13-byte (8.3 plus terminator)
1234  *  string buffer with the 8.3 filename of a recently loaded 16-bit
1235  *  module.  It is unknown exactly what modules trigger this
1236  *  mechanism or what purpose this serves.  Win98 Explorer (and
1237  *  probably also Win95 with IE 4 shell integration) calls this
1238  *  several times during initialization.
1239  *
1240  *  FIXME: find out what this really does and make it work.
1241  */
1242 void WINAPI PK16FNF(LPSTR strPtr)
1243 {
1244        FIXME_(win32)("(%p): stub\n", strPtr);
1245
1246        /* fill in a fake filename that'll be easy to recognize */
1247        lstrcpyA(strPtr, "WINESTUB.FIX");
1248 }