Corrected small bug in GetCommState16. Parity check can be disabled
[wine/dcerpc.git] / if1632 / builtin.c
blobd5c6444a60bb4283f420dbba063d60dce7b09520
1 /*
2 * Built-in modules
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include "winbase.h"
11 #include "wine/winbase16.h"
12 #include "wine/winestring.h"
13 #include "builtin32.h"
14 #include "global.h"
15 #include "heap.h"
16 #include "module.h"
17 #include "miscemu.h"
18 #include "neexe.h"
19 #include "stackframe.h"
20 #include "user.h"
21 #include "process.h"
22 #include "snoop.h"
23 #include "task.h"
24 #include "debug.h"
26 /* Built-in modules descriptors */
27 /* Don't change these structures! (see tools/build.c) */
29 typedef struct
31 const char *name; /* DLL name */
32 void *module_start; /* 32-bit address of the module data */
33 int module_size; /* Size of the module data */
34 const BYTE *code_start; /* 32-bit address of DLL code */
35 const BYTE *data_start; /* 32-bit address of DLL data */
36 } WIN16_DESCRIPTOR;
38 typedef struct
40 const WIN16_DESCRIPTOR *descr; /* DLL descriptor */
41 int flags; /* flags (see below) */
42 } BUILTIN16_DLL;
44 /* DLL flags */
45 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
46 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
48 /* 16-bit DLLs */
50 extern const WIN16_DESCRIPTOR AVIFILE_Descriptor;
51 extern const WIN16_DESCRIPTOR COMM_Descriptor;
52 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
53 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
54 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
55 extern const WIN16_DESCRIPTOR DISPDIB_Descriptor;
56 extern const WIN16_DESCRIPTOR DISPLAY_Descriptor;
57 extern const WIN16_DESCRIPTOR GDI_Descriptor;
58 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
59 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
60 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
61 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
62 extern const WIN16_DESCRIPTOR MOUSE_Descriptor;
63 extern const WIN16_DESCRIPTOR MSACM_Descriptor;
64 extern const WIN16_DESCRIPTOR MSVIDEO_Descriptor;
65 extern const WIN16_DESCRIPTOR OLE2CONV_Descriptor;
66 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor;
67 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor;
68 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor;
69 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor;
70 extern const WIN16_DESCRIPTOR OLE2_Descriptor;
71 extern const WIN16_DESCRIPTOR OLECLI_Descriptor;
72 extern const WIN16_DESCRIPTOR OLESVR_Descriptor;
73 extern const WIN16_DESCRIPTOR RASAPI16_Descriptor;
74 extern const WIN16_DESCRIPTOR SHELL_Descriptor;
75 extern const WIN16_DESCRIPTOR SOUND_Descriptor;
76 extern const WIN16_DESCRIPTOR STORAGE_Descriptor;
77 extern const WIN16_DESCRIPTOR STRESS_Descriptor;
78 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor;
79 extern const WIN16_DESCRIPTOR TOOLHELP_Descriptor;
80 extern const WIN16_DESCRIPTOR TYPELIB_Descriptor;
81 extern const WIN16_DESCRIPTOR USER_Descriptor;
82 extern const WIN16_DESCRIPTOR VER_Descriptor;
83 extern const WIN16_DESCRIPTOR W32SYS_Descriptor;
84 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor;
85 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor;
86 extern const WIN16_DESCRIPTOR WINASPI_Descriptor;
87 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor;
88 extern const WIN16_DESCRIPTOR WINEPS_Descriptor;
89 extern const WIN16_DESCRIPTOR WING_Descriptor;
90 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor;
91 extern const WIN16_DESCRIPTOR WPROCS_Descriptor;
93 /* Table of all built-in DLLs */
95 static BUILTIN16_DLL BuiltinDLLs[] =
97 { &KERNEL_Descriptor, 0 },
98 { &USER_Descriptor, 0 },
99 { &GDI_Descriptor, 0 },
100 { &SYSTEM_Descriptor, DLL_FLAG_ALWAYS_USED },
101 { &DISPLAY_Descriptor, DLL_FLAG_ALWAYS_USED },
102 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
103 { &WINDEBUG_Descriptor, DLL_FLAG_NOT_USED },
104 { &AVIFILE_Descriptor, DLL_FLAG_NOT_USED },
105 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
106 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
107 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
108 { &DISPDIB_Descriptor, 0 },
109 { &KEYBOARD_Descriptor, 0 },
110 { &COMM_Descriptor, 0 },
111 { &LZEXPAND_Descriptor, 0 },
112 { &MMSYSTEM_Descriptor, 0 },
113 { &MOUSE_Descriptor, 0 },
114 { &MSACM_Descriptor, 0 },
115 { &MSVIDEO_Descriptor, 0 },
116 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
117 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
118 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
119 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
120 { &OLE2THK_Descriptor, DLL_FLAG_NOT_USED },
121 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
122 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
123 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
124 { &RASAPI16_Descriptor, 0 },
125 { &SHELL_Descriptor, 0 },
126 { &SOUND_Descriptor, 0 },
127 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
128 { &STRESS_Descriptor, 0 },
129 { &TOOLHELP_Descriptor, 0 },
130 { &TYPELIB_Descriptor, DLL_FLAG_NOT_USED },
131 { &VER_Descriptor, 0 },
132 { &W32SYS_Descriptor, DLL_FLAG_NOT_USED },
133 { &WIN32S16_Descriptor, DLL_FLAG_NOT_USED },
134 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
135 { &WINASPI_Descriptor, 0 },
136 { &WINEPS_Descriptor, DLL_FLAG_ALWAYS_USED },
137 { &WING_Descriptor, 0 },
138 { &WINSOCK_Descriptor, 0 },
139 /* Last entry */
140 { NULL, 0 }
143 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
144 #define FIRST_INTERRUPT_ORDINAL 100
147 /***********************************************************************
148 * BUILTIN_DoLoadModule16
150 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
151 * and BUILTIN_Init.
153 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
155 NE_MODULE *pModule;
156 int minsize;
157 SEGTABLEENTRY *pSegTable;
159 HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
160 descr->module_size, 0,
161 FALSE, FALSE, FALSE, NULL );
162 if (!hModule) return 0;
163 FarSetOwner16( hModule, hModule );
165 TRACE(module, "Built-in %s: hmodule=%04x\n",
166 descr->name, hModule );
167 pModule = (NE_MODULE *)GlobalLock16( hModule );
168 pModule->self = hModule;
170 /* Allocate the code segment */
172 pSegTable = NE_SEG_TABLE( pModule );
173 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
174 pSegTable->minsize, hModule,
175 TRUE, TRUE, FALSE, NULL );
176 if (!pSegTable->hSeg) return 0;
177 pSegTable++;
179 /* Allocate the data segment */
181 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
182 minsize += pModule->heap_size;
183 if (minsize > 0x10000) minsize = 0x10000;
184 pSegTable->hSeg = GLOBAL_Alloc( GMEM_FIXED, minsize,
185 hModule, FALSE, FALSE, FALSE );
186 if (!pSegTable->hSeg) return 0;
187 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
188 descr->data_start, pSegTable->minsize);
189 if (pModule->heap_size)
190 LocalInit16( GlobalHandleToSel16(pSegTable->hSeg),
191 pSegTable->minsize, minsize );
193 NE_RegisterModule( pModule );
194 return hModule;
198 /***********************************************************************
199 * BUILTIN_Init
201 * Load all built-in modules marked as 'always used'.
203 BOOL BUILTIN_Init(void)
205 BUILTIN16_DLL *dll;
206 WORD vector;
207 HMODULE16 hModule;
209 fnBUILTIN_LoadModule = BUILTIN_LoadModule;
211 for (dll = BuiltinDLLs; dll->descr; dll++)
213 if (dll->flags & DLL_FLAG_ALWAYS_USED)
214 if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
217 /* Set interrupt vectors from entry points in WPROCS.DLL */
219 hModule = GetModuleHandle16( "WPROCS" );
220 for (vector = 0; vector < 256; vector++)
222 FARPROC16 proc = NE_GetEntryPoint( hModule,
223 FIRST_INTERRUPT_ORDINAL + vector );
224 assert(proc);
225 INT_SetPMHandler( vector, proc );
228 SNOOP16_Init();
230 return TRUE;
234 /***********************************************************************
235 * BUILTIN_LoadModule
237 * Load a built-in module. If the 'force' parameter is FALSE, we only
238 * load the module if it has not been disabled via the -dll option.
240 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL force )
242 BUILTIN16_DLL *table;
243 char dllname[16], *p;
245 /* Fix the name in case we have a full path and extension */
247 if ((p = strrchr( name, '\\' ))) name = p + 1;
248 lstrcpynA( dllname, name, sizeof(dllname) );
249 if ((p = strrchr( dllname, '.' ))) *p = '\0';
251 for (table = BuiltinDLLs; table->descr; table++)
252 if (!lstrcmpiA( table->descr->name, dllname )) break;
253 if (!table->descr) return 0;
254 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
256 return BUILTIN_DoLoadModule16( table->descr );
260 /***********************************************************************
261 * BUILTIN_GetEntryPoint16
263 * Return the ordinal and name corresponding to a CS:IP address.
264 * This is used only by relay debugging.
266 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
268 static char buffer[80];
269 WORD ordinal, i, max_offset;
270 register BYTE *p;
271 NE_MODULE *pModule;
273 if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16(cs) ))))
274 return NULL;
276 /* Search for the ordinal */
278 p = (BYTE *)pModule + pModule->entry_table;
279 max_offset = 0;
280 ordinal = 1;
281 *pOrd = 0;
282 while (*p)
284 switch(p[1])
286 case 0: /* unused */
287 ordinal += *p;
288 p += 2;
289 break;
290 case 1: /* code segment */
291 i = *p;
292 p += 2;
293 while (i-- > 0)
295 p++;
296 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
298 max_offset = *(WORD *)p;
299 *pOrd = ordinal;
301 p += 2;
302 ordinal++;
304 break;
305 case 0xff: /* moveable (should not happen in built-in modules) */
306 TRACE( relay, "Built-in module has moveable entry\n" );
307 ordinal += *p;
308 p += 2 + *p * 6;
309 break;
310 default: /* other segment */
311 ordinal += *p;
312 p += 2 + *p * 3;
313 break;
317 /* Search for the name in the resident names table */
318 /* (built-in modules have no non-resident table) */
320 p = (BYTE *)pModule + pModule->name_table;
321 while (*p)
323 p += *p + 1 + sizeof(WORD);
324 if (*(WORD *)(p + *p + 1) == *pOrd) break;
327 sprintf( buffer, "%.*s.%d: %.*s",
328 *((BYTE *)pModule + pModule->name_table),
329 (char *)pModule + pModule->name_table + 1,
330 *pOrd, *p, (char *)(p + 1) );
331 return buffer;
335 /**********************************************************************
336 * BUILTIN_DefaultIntHandler
338 * Default interrupt handler.
340 void BUILTIN_DefaultIntHandler( CONTEXT *context )
342 WORD ordinal;
343 STACK16FRAME *frame = CURRENT_STACK16;
344 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
345 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
349 /***********************************************************************
350 * BUILTIN_ParseDLLOptions
352 * Set runtime DLL usage flags
354 BOOL BUILTIN_ParseDLLOptions( char *str )
356 BUILTIN16_DLL *dll;
357 char *p,*last;
360 last = str;
361 while (*str)
363 while (*str && (*str==',' || isspace(*str))) str++;
364 if (!*str) {
365 *last = '\0'; /* cut off garbage at end at */
366 return TRUE;
368 if ((*str != '+') && (*str != '-')) return FALSE;
369 str++;
370 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
371 while ((p > str) && isspace(p[-1])) p--;
372 if (p == str) return FALSE;
373 for (dll = BuiltinDLLs; dll->descr; dll++)
375 if (!lstrncmpiA( str, dll->descr->name, (int)(p - str) ))
377 if (dll->descr->name[(int)(p-str)]) /* only partial match */
378 continue;
379 if (str[-1] == '-')
381 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
382 dll->flags |= DLL_FLAG_NOT_USED;
384 else dll->flags &= ~DLL_FLAG_NOT_USED;
385 break;
388 if (!dll->descr) {
389 /* not found, but could get handled by BUILTIN32_, so move last */
390 last = p;
391 str = p;
392 } else {
393 /* handled. cut out the "[+-]DLL," string, so it isn't handled
394 * by BUILTIN32
396 if (*p) {
397 memcpy(last,p,strlen(p)+1);
398 str = last;
399 } else {
400 *last = '\0';
401 break;
405 return TRUE;
409 /***********************************************************************
410 * BUILTIN_PrintDLLs
412 * Print the list of built-in DLLs that can be disabled.
414 void BUILTIN_PrintDLLs(void)
416 int i;
417 BUILTIN16_DLL *dll;
419 MSG("Example: -dll -ole2 Do not use emulated OLE2.DLL\n");
420 MSG("Available Win16 DLLs:\n");
421 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
423 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
424 MSG("%-9s%c", dll->descr->name,
425 ((++i) % 8) ? ' ' : '\n' );
427 MSG("\n");
428 BUILTIN32_PrintDLLs();