kernel32: Fix LANGID for Korean resource.
[wine] / dlls / kernel32 / relay16.c
1 /*
2  * Copyright 1993 Robert J. Amstadt
3  * Copyright 1995 Alexandre Julliard
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wine/winbase16.h"
32 #include "winternl.h"
33 #include "kernel_private.h"
34 #include "kernel16_private.h"
35 #include "wine/unicode.h"
36 #include "wine/library.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(relay);
40
41 #ifdef __i386__
42
43 static const WCHAR **debug_relay_excludelist;
44 static const WCHAR **debug_relay_includelist;
45 static const WCHAR **debug_snoop_excludelist;
46 static const WCHAR **debug_snoop_includelist;
47
48 /* compare an ASCII and a Unicode string without depending on the current codepage */
49 inline static int strcmpiAW( const char *strA, const WCHAR *strW )
50 {
51     while (*strA && (toupperW((unsigned char)*strA) == toupperW(*strW))) { strA++; strW++; }
52     return toupperW((unsigned char)*strA) - toupperW(*strW);
53 }
54
55 /* compare an ASCII and a Unicode string without depending on the current codepage */
56 inline static int strncmpiAW( const char *strA, const WCHAR *strW, int n )
57 {
58     int ret = 0;
59     for ( ; n > 0; n--, strA++, strW++)
60         if ((ret = toupperW((unsigned char)*strA) - toupperW(*strW)) || !*strA) break;
61     return ret;
62 }
63
64 /***********************************************************************
65  *           build_list
66  *
67  * Build a function list from a ';'-separated string.
68  */
69 static const WCHAR **build_list( const WCHAR *buffer )
70 {
71     int count = 1;
72     const WCHAR *p = buffer;
73     const WCHAR **ret;
74
75     while ((p = strchrW( p, ';' )))
76     {
77         count++;
78         p++;
79     }
80     /* allocate count+1 pointers, plus the space for a copy of the string */
81     if ((ret = RtlAllocateHeap( GetProcessHeap(), 0,
82                                 (count+1) * sizeof(WCHAR*) + (strlenW(buffer)+1) * sizeof(WCHAR) )))
83     {
84         WCHAR *str = (WCHAR *)(ret + count + 1);
85         WCHAR *p = str;
86
87         strcpyW( str, buffer );
88         count = 0;
89         for (;;)
90         {
91             ret[count++] = p;
92             if (!(p = strchrW( p, ';' ))) break;
93             *p++ = 0;
94         }
95         ret[count++] = NULL;
96     }
97     return ret;
98 }
99
100
101 /***********************************************************************
102  *           RELAY16_InitDebugLists
103  *
104  * Build the relay include/exclude function lists.
105  */
106 void RELAY16_InitDebugLists(void)
107 {
108     OBJECT_ATTRIBUTES attr;
109     UNICODE_STRING name;
110     char buffer[1024];
111     HANDLE root, hkey;
112     DWORD count;
113     WCHAR *str;
114     static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
115                                     'W','i','n','e','\\',
116                                     'D','e','b','u','g',0};
117     static const WCHAR RelayIncludeW[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
118     static const WCHAR RelayExcludeW[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
119     static const WCHAR SnoopIncludeW[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
120     static const WCHAR SnoopExcludeW[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
121
122     RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
123     attr.Length = sizeof(attr);
124     attr.RootDirectory = root;
125     attr.ObjectName = &name;
126     attr.Attributes = 0;
127     attr.SecurityDescriptor = NULL;
128     attr.SecurityQualityOfService = NULL;
129     RtlInitUnicodeString( &name, configW );
130
131     /* @@ Wine registry key: HKCU\Software\Wine\Debug */
132     if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
133     NtClose( root );
134     if (!hkey) return;
135
136     str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
137     RtlInitUnicodeString( &name, RelayIncludeW );
138     if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
139     {
140         debug_relay_includelist = build_list( str );
141     }
142
143     RtlInitUnicodeString( &name, RelayExcludeW );
144     if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
145     {
146         debug_relay_excludelist = build_list( str );
147     }
148
149     RtlInitUnicodeString( &name, SnoopIncludeW );
150     if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
151     {
152         debug_snoop_includelist = build_list( str );
153     }
154
155     RtlInitUnicodeString( &name, SnoopExcludeW );
156     if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
157     {
158         debug_snoop_excludelist = build_list( str );
159     }
160     NtClose( hkey );
161 }
162
163
164 /***********************************************************************
165  *           check_list
166  *
167  * Check if a given module and function is in the list.
168  */
169 static BOOL check_list( const char *module, int ordinal, const char *func, const WCHAR **list )
170 {
171     char ord_str[10];
172
173     sprintf( ord_str, "%d", ordinal );
174     for(; *list; list++)
175     {
176         const WCHAR *p = strrchrW( *list, '.' );
177         if (p && p > *list)  /* check module and function */
178         {
179             int len = p - *list;
180             if (strncmpiAW( module, *list, len-1 ) || module[len]) continue;
181             if (p[1] == '*' && !p[2]) return TRUE;
182             if (!strcmpiAW( ord_str, p + 1 )) return TRUE;
183             if (func && !strcmpiAW( func, p + 1 )) return TRUE;
184         }
185         else  /* function only */
186         {
187             if (func && !strcmpiAW( func, *list )) return TRUE;
188         }
189     }
190     return FALSE;
191 }
192
193
194 /***********************************************************************
195  *           RELAY_ShowDebugmsgRelay
196  *
197  * Simple function to decide if a particular debugging message is
198  * wanted.
199  */
200 static BOOL RELAY_ShowDebugmsgRelay(const char *module, int ordinal, const char *func)
201 {
202     if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
203         return FALSE;
204     if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
205         return FALSE;
206     return TRUE;
207 }
208
209
210 /***********************************************************************
211  *          SNOOP16_ShowDebugmsgSnoop
212  *
213  * Simple function to decide if a particular debugging message is
214  * wanted.
215  */
216 int SNOOP16_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
217 {
218     if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
219         return FALSE;
220     if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
221         return FALSE;
222     return TRUE;
223 }
224
225
226 /***********************************************************************
227  *           get_entry_point
228  *
229  * Return the ordinal, name, and type info corresponding to a CS:IP address.
230  */
231 static const CALLFROM16 *get_entry_point( STACK16FRAME *frame, LPSTR module, LPSTR func, WORD *pOrd )
232 {
233     WORD i, max_offset;
234     register BYTE *p;
235     NE_MODULE *pModule;
236     ET_BUNDLE *bundle;
237     ET_ENTRY *entry;
238
239     *pOrd = 0;
240     if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16( frame->module_cs ) ))))
241         return NULL;
242
243     max_offset = 0;
244     bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
245     do
246     {
247         entry = (ET_ENTRY *)((BYTE *)bundle+6);
248         for (i = bundle->first + 1; i <= bundle->last; i++)
249         {
250             if ((entry->offs < frame->entry_ip)
251             && (entry->segnum == 1) /* code segment ? */
252             && (entry->offs >= max_offset))
253             {
254                 max_offset = entry->offs;
255                 *pOrd = i;
256             }
257             entry++;
258         }
259     } while ( (bundle->next)
260            && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
261
262     /* Search for the name in the resident names table */
263     /* (built-in modules have no non-resident table)   */
264
265     p = (BYTE *)pModule + pModule->ne_restab;
266     memcpy( module, p + 1, *p );
267     module[*p] = 0;
268
269     while (*p)
270     {
271         p += *p + 1 + sizeof(WORD);
272         if (*(WORD *)(p + *p + 1) == *pOrd) break;
273     }
274     memcpy( func, p + 1, *p );
275     func[*p] = 0;
276
277     /* Retrieve entry point call structure */
278     p = MapSL( MAKESEGPTR( frame->module_cs, frame->callfrom_ip ) );
279     /* p now points to lret, get the start of CALLFROM16 structure */
280     return (CALLFROM16 *)(p - (BYTE *)&((CALLFROM16 *)0)->ret);
281 }
282
283
284 extern int call_entry_point( void *func, int nb_args, const int *args );
285 __ASM_GLOBAL_FUNC( call_entry_point,
286                    "\tpushl %ebp\n"
287                    "\tmovl %esp,%ebp\n"
288                    "\tpushl %esi\n"
289                    "\tpushl %edi\n"
290                    "\tmovl 12(%ebp),%edx\n"
291                    "\tshll $2,%edx\n"
292                    "\tjz 1f\n"
293                    "\tsubl %edx,%esp\n"
294                    "\tandl $~15,%esp\n"
295                    "\tmovl 12(%ebp),%ecx\n"
296                    "\tmovl 16(%ebp),%esi\n"
297                    "\tmovl %esp,%edi\n"
298                    "\tcld\n"
299                    "\trep; movsl\n"
300                    "1:\tcall *8(%ebp)\n"
301                    "\tleal -8(%ebp),%esp\n"
302                    "\tpopl %edi\n"
303                    "\tpopl %esi\n"
304                    "\tpopl %ebp\n"
305                    "\tret" )
306
307
308 /***********************************************************************
309  *           relay_call_from_16_no_debug
310  *
311  * Same as relay_call_from_16 but doesn't print any debug information.
312  */
313 static int relay_call_from_16_no_debug( void *entry_point, unsigned char *args16, CONTEXT86 *context,
314                                         const CALLFROM16 *call )
315 {
316     unsigned int i, j, nb_args = 0;
317     int args32[20];
318
319     /* look for the ret instruction */
320     for (j = 0; j < sizeof(call->ret)/sizeof(call->ret[0]); j++)
321         if (call->ret[j] == 0xca66 || call->ret[j] == 0xcb66) break;
322
323     if (call->ret[j] == 0xcb66)  /* cdecl */
324     {
325         for (i = 0; i < 20; i++, nb_args++)
326         {
327             int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
328
329             if (type == ARG_NONE) break;
330             switch(type)
331             {
332             case ARG_WORD:
333                 args32[nb_args] = *(WORD *)args16;
334                 args16 += sizeof(WORD);
335                 break;
336             case ARG_SWORD:
337                 args32[nb_args] = *(short *)args16;
338                 args16 += sizeof(WORD);
339                 break;
340             case ARG_LONG:
341             case ARG_SEGSTR:
342                 args32[nb_args] = *(int *)args16;
343                 args16 += sizeof(int);
344                 break;
345             case ARG_PTR:
346             case ARG_STR:
347                 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
348                 args16 += sizeof(SEGPTR);
349                 break;
350             case ARG_VARARG:
351                 args32[nb_args] = (int)args16;
352                 break;
353             default:
354                 break;
355             }
356         }
357     }
358     else  /* not cdecl */
359     {
360         /* Start with the last arg */
361         args16 += call->ret[j + 1];
362         for (i = 0; i < 20; i++, nb_args++)
363         {
364             int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
365
366             if (type == ARG_NONE) break;
367             switch(type)
368             {
369             case ARG_WORD:
370                 args16 -= sizeof(WORD);
371                 args32[nb_args] = *(WORD *)args16;
372                 break;
373             case ARG_SWORD:
374                 args16 -= sizeof(WORD);
375                 args32[nb_args] = *(short *)args16;
376                 break;
377             case ARG_LONG:
378             case ARG_SEGSTR:
379                 args16 -= sizeof(int);
380                 args32[nb_args] = *(int *)args16;
381                 break;
382             case ARG_PTR:
383             case ARG_STR:
384                 args16 -= sizeof(SEGPTR);
385                 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
386                 break;
387             default:
388                 break;
389             }
390         }
391     }
392
393     if (!j)  /* register function */
394         args32[nb_args++] = (int)context;
395
396     SYSLEVEL_CheckNotLevel( 2 );
397
398     return call_entry_point( entry_point, nb_args, args32 );
399 }
400
401
402 /***********************************************************************
403  *           relay_call_from_16
404  *
405  * Replacement for the 16-bit relay functions when relay debugging is on.
406  */
407 int relay_call_from_16( void *entry_point, unsigned char *args16, CONTEXT86 *context )
408 {
409     STACK16FRAME *frame;
410     WORD ordinal;
411     unsigned int i, j, nb_args = 0;
412     int ret_val, args32[20];
413     char module[10], func[64];
414     const CALLFROM16 *call;
415
416     frame = CURRENT_STACK16;
417     call = get_entry_point( frame, module, func, &ordinal );
418     if (!TRACE_ON(relay) || !RELAY_ShowDebugmsgRelay( module, ordinal, func ))
419         return relay_call_from_16_no_debug( entry_point, args16, context, call );
420
421     DPRINTF( "%04x:Call %s.%d: %s(",GetCurrentThreadId(), module, ordinal, func );
422
423     /* look for the ret instruction */
424     for (j = 0; j < sizeof(call->ret)/sizeof(call->ret[0]); j++)
425         if (call->ret[j] == 0xca66 || call->ret[j] == 0xcb66) break;
426
427     if (call->ret[j] == 0xcb66)  /* cdecl */
428     {
429         for (i = 0; i < 20; i++, nb_args++)
430         {
431             int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
432
433             if (type == ARG_NONE) break;
434             if (i) DPRINTF( "," );
435             switch(type)
436             {
437             case ARG_WORD:
438                 DPRINTF( "%04x", *(WORD *)args16 );
439                 args32[nb_args] = *(WORD *)args16;
440                 args16 += sizeof(WORD);
441                 break;
442             case ARG_SWORD:
443                 DPRINTF( "%04x", *(WORD *)args16 );
444                 args32[nb_args] = *(short *)args16;
445                 args16 += sizeof(WORD);
446                 break;
447             case ARG_LONG:
448                 DPRINTF( "%08x", *(int *)args16 );
449                 args32[nb_args] = *(int *)args16;
450                 args16 += sizeof(int);
451                 break;
452             case ARG_PTR:
453                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
454                 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
455                 args16 += sizeof(SEGPTR);
456                 break;
457             case ARG_STR:
458                 DPRINTF( "%08x %s", *(int *)args16,
459                          debugstr_a( MapSL(*(SEGPTR *)args16 )));
460                 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
461                 args16 += sizeof(int);
462                 break;
463             case ARG_SEGSTR:
464                 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
465                          debugstr_a( MapSL(*(SEGPTR *)args16 )) );
466                 args32[nb_args] = *(SEGPTR *)args16;
467                 args16 += sizeof(SEGPTR);
468                 break;
469             case ARG_VARARG:
470                 DPRINTF( "..." );
471                 args32[nb_args] = (int)args16;
472                 break;
473             default:
474                 break;
475             }
476         }
477     }
478     else  /* not cdecl */
479     {
480         /* Start with the last arg */
481         args16 += call->ret[j + 1];
482         for (i = 0; i < 20; i++, nb_args++)
483         {
484             int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
485
486             if (type == ARG_NONE) break;
487             if (i) DPRINTF( "," );
488             switch(type)
489             {
490             case ARG_WORD:
491                 args16 -= sizeof(WORD);
492                 args32[nb_args] = *(WORD *)args16;
493                 DPRINTF( "%04x", *(WORD *)args16 );
494                 break;
495             case ARG_SWORD:
496                 args16 -= sizeof(WORD);
497                 args32[nb_args] = *(short *)args16;
498                 DPRINTF( "%04x", *(WORD *)args16 );
499                 break;
500             case ARG_LONG:
501                 args16 -= sizeof(int);
502                 args32[nb_args] = *(int *)args16;
503                 DPRINTF( "%08x", *(int *)args16 );
504                 break;
505             case ARG_PTR:
506                 args16 -= sizeof(SEGPTR);
507                 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
508                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
509                 break;
510             case ARG_STR:
511                 args16 -= sizeof(int);
512                 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
513                 DPRINTF( "%08x %s", *(int *)args16,
514                          debugstr_a( MapSL(*(SEGPTR *)args16 )));
515                 break;
516             case ARG_SEGSTR:
517                 args16 -= sizeof(SEGPTR);
518                 args32[nb_args] = *(SEGPTR *)args16;
519                 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
520                          debugstr_a( MapSL(*(SEGPTR *)args16 )) );
521                 break;
522             case ARG_VARARG:
523                 DPRINTF( "..." );
524                 args32[nb_args] = (int)args16;
525                 break;
526             default:
527                 break;
528             }
529         }
530     }
531
532     DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
533
534     if (!j)  /* register function */
535     {
536         args32[nb_args++] = (int)context;
537         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08x\n",
538                 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
539                 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
540                 (WORD)context->SegEs, context->EFlags );
541     }
542
543     SYSLEVEL_CheckNotLevel( 2 );
544
545     ret_val = call_entry_point( entry_point, nb_args, args32 );
546
547     SYSLEVEL_CheckNotLevel( 2 );
548
549     DPRINTF( "%04x:Ret  %s.%d: %s() ",GetCurrentThreadId(), module, ordinal, func );
550     if (!j)  /* register function */
551     {
552         DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
553                 (WORD)context->SegCs, LOWORD(context->Eip), (WORD)context->SegDs);
554         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08x\n",
555                 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
556                 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
557                 (WORD)context->SegEs, context->EFlags );
558     }
559     else
560     {
561         frame = CURRENT_STACK16;  /* might have be changed by the entry point */
562         if (j == 1)  /* 16-bit return sequence */
563             DPRINTF( "retval=%04x ret=%04x:%04x ds=%04x\n",
564                      ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
565         else
566             DPRINTF( "retval=%08x ret=%04x:%04x ds=%04x\n",
567                      ret_val, frame->cs, frame->ip, frame->ds );
568     }
569     return ret_val;
570 }
571
572 #else /* __i386__ */
573
574 /*
575  * Stubs for the CallTo16/CallFrom16 routines on non-Intel architectures
576  * (these will never be called but need to be present to satisfy the linker ...)
577  */
578
579 /***********************************************************************
580  *              __wine_call_from_16_regs (KERNEL32.@)
581  */
582 void __wine_call_from_16_regs()
583 {
584     assert( FALSE );
585 }
586
587 DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi )
588 { assert( FALSE ); }
589
590 DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs )
591 { assert( FALSE ); }
592
593 #endif  /* __i386__ */