Added check for illegal pipe names.
[wine/multimedia.git] / loader / loadorder.c
blob0e3eafb5b9b1ad820755f448003087564bffdd59
1 /*
2 * Module/Library loadorder
4 * Copyright 1999 Bertho Stultiens
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
28 #include "windef.h"
29 #include "winerror.h"
30 #include "winternl.h"
31 #include "file.h"
32 #include "module.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(module);
39 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
41 typedef struct module_loadorder
43 const char *modulename;
44 enum loadorder_type loadorder[LOADORDER_NTYPES];
45 } module_loadorder_t;
47 struct loadorder_list
49 int count;
50 int alloc;
51 module_loadorder_t *order;
54 /* default load-order if nothing specified */
55 /* the list must remain sorted by dll name */
56 static module_loadorder_t default_order_list[] =
58 { "display", { LOADORDER_BI, 0, 0, 0 } },
59 { "gdi.exe", { LOADORDER_BI, 0, 0, 0 } },
60 { "gdi32", { LOADORDER_BI, 0, 0, 0 } },
61 { "glide2x", { LOADORDER_SO, LOADORDER_DLL, 0, 0 } },
62 { "glide3x", { LOADORDER_SO, LOADORDER_DLL, 0, 0 } },
63 { "icmp", { LOADORDER_BI, 0, 0, 0 } },
64 { "kernel", { LOADORDER_BI, 0, 0, 0 } },
65 { "kernel32", { LOADORDER_BI, 0, 0, 0 } },
66 { "keyboard", { LOADORDER_BI, 0, 0, 0 } },
67 { "krnl386.exe", { LOADORDER_BI, 0, 0, 0 } },
68 { "mmsystem", { LOADORDER_BI, 0, 0, 0 } },
69 { "mouse", { LOADORDER_BI, 0, 0, 0 } },
70 { "ntdll", { LOADORDER_BI, 0, 0, 0 } },
71 { "odbc32", { LOADORDER_BI, 0, 0, 0 } },
72 { "system", { LOADORDER_BI, 0, 0, 0 } },
73 { "toolhelp", { LOADORDER_BI, 0, 0, 0 } },
74 { "ttydrv", { LOADORDER_BI, 0, 0, 0 } },
75 { "user.exe", { LOADORDER_BI, 0, 0, 0 } },
76 { "user32", { LOADORDER_BI, 0, 0, 0 } },
77 { "w32skrnl", { LOADORDER_BI, 0, 0, 0 } },
78 { "winaspi", { LOADORDER_BI, 0, 0, 0 } },
79 { "windebug", { LOADORDER_DLL, LOADORDER_BI, 0, 0 } },
80 { "winedos", { LOADORDER_BI, 0, 0, 0 } },
81 { "wineps", { LOADORDER_BI, 0, 0, 0 } },
82 { "wing", { LOADORDER_BI, 0, 0, 0 } },
83 { "winmm", { LOADORDER_BI, 0, 0, 0 } },
84 { "winsock", { LOADORDER_BI, 0, 0, 0 } },
85 { "wnaspi32", { LOADORDER_BI, 0, 0, 0 } },
86 { "wow32", { LOADORDER_BI, 0, 0, 0 } },
87 { "wprocs", { LOADORDER_BI, 0, 0, 0 } },
88 { "ws2_32", { LOADORDER_BI, 0, 0, 0 } },
89 { "wsock32", { LOADORDER_BI, 0, 0, 0 } },
90 { "x11drv", { LOADORDER_BI, 0, 0, 0 } }
93 static const struct loadorder_list default_list =
95 sizeof(default_order_list)/sizeof(default_order_list[0]),
96 sizeof(default_order_list)/sizeof(default_order_list[0]),
97 default_order_list
100 /* default if nothing else specified */
101 static const enum loadorder_type default_loadorder[LOADORDER_NTYPES] =
103 LOADORDER_BI, LOADORDER_DLL, 0, 0
106 /* default for modules with an explicit path */
107 static const enum loadorder_type default_path_loadorder[LOADORDER_NTYPES] =
109 LOADORDER_DLL, LOADORDER_BI, 0, 0
112 static struct loadorder_list cmdline_list;
115 /***************************************************************************
116 * cmp_sort_func (internal, static)
118 * Sorting and comparing function used in sort and search of loadorder
119 * entries.
121 static int cmp_sort_func(const void *s1, const void *s2)
123 return FILE_strcasecmp(((module_loadorder_t *)s1)->modulename,
124 ((module_loadorder_t *)s2)->modulename);
128 /***************************************************************************
129 * get_tok (internal, static)
131 * strtok wrapper for non-destructive buffer writing.
132 * NOTE: strtok is not reentrant and therefore this code is neither.
134 static char *get_tok(const char *str, const char *delim)
136 static char *buf = NULL;
137 char *cptr;
139 if(!str && !buf)
140 return NULL;
142 if(str && buf)
144 HeapFree(GetProcessHeap(), 0, buf);
145 buf = NULL;
148 if(str && !buf)
150 buf = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
151 strcpy( buf, str );
152 cptr = strtok(buf, delim);
154 else
156 cptr = strtok(NULL, delim);
159 if(!cptr)
161 HeapFree(GetProcessHeap(), 0, buf);
162 buf = NULL;
164 return cptr;
168 /***************************************************************************
169 * get_basename
171 * Return the base name of a file name (i.e. remove the path components).
173 static const char *get_basename( const char *name )
175 const char *ptr;
177 if (name[0] && name[1] == ':') name += 2; /* strip drive specification */
178 if ((ptr = strrchr( name, '\\' ))) name = ptr + 1;
179 if ((ptr = strrchr( name, '/' ))) name = ptr + 1;
180 return name;
184 /***************************************************************************
185 * debugstr_loadorder
187 * Return a loadorder in printable form.
189 static const char *debugstr_loadorder( enum loadorder_type lo[] )
191 int i;
192 char buffer[LOADORDER_NTYPES*3+1];
194 buffer[0] = 0;
195 for(i = 0; i < LOADORDER_NTYPES; i++)
197 if (lo[i] == LOADORDER_INVALID) break;
198 switch(lo[i])
200 case LOADORDER_DLL: strcat( buffer, "n," ); break;
201 case LOADORDER_SO: strcat( buffer, "s," ); break;
202 case LOADORDER_BI: strcat( buffer, "b," ); break;
203 default: strcat( buffer, "?," ); break;
206 if (buffer[0]) buffer[strlen(buffer)-1] = 0;
207 return debugstr_a(buffer);
211 /***************************************************************************
212 * ParseLoadOrder (internal, static)
214 * Parses the loadorder options from the configuration and puts it into
215 * a structure.
217 static BOOL ParseLoadOrder(char *order, enum loadorder_type lo[])
219 static int warn;
220 char *cptr;
221 int n = 0;
223 cptr = get_tok(order, ", \t");
224 while(cptr)
226 enum loadorder_type type = LOADORDER_INVALID;
228 if(n >= LOADORDER_NTYPES-1)
230 ERR("More than existing %d module-types specified, rest ignored\n", LOADORDER_NTYPES-1);
231 break;
234 switch(*cptr)
236 case 'N': /* Native */
237 case 'n': type = LOADORDER_DLL; break;
239 case 'E': /* Elfdll */
240 case 'e':
241 if (!warn++) MESSAGE("Load order 'elfdll' no longer supported, ignored\n");
242 break;
243 case 'S': /* So */
244 case 's': type = LOADORDER_SO; break;
246 case 'B': /* Builtin */
247 case 'b': type = LOADORDER_BI; break;
249 default:
250 ERR("Invalid load order module-type '%s', ignored\n", cptr);
253 if(type != LOADORDER_INVALID) lo[n++] = type;
254 cptr = get_tok(NULL, ", \t");
256 lo[n] = LOADORDER_INVALID;
257 return TRUE;
261 /***************************************************************************
262 * AddLoadOrder (internal, static)
264 * Adds an entry in the list of command-line overrides.
266 static BOOL AddLoadOrder(module_loadorder_t *plo)
268 int i;
270 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
272 for(i = 0; i < cmdline_list.count; i++)
274 if(!cmp_sort_func(plo, &cmdline_list.order[i] ))
276 /* replace existing option */
277 memcpy( cmdline_list.order[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
278 return TRUE;
282 if (i >= cmdline_list.alloc)
284 /* No space in current array, make it larger */
285 cmdline_list.alloc += LOADORDER_ALLOC_CLUSTER;
286 cmdline_list.order = HeapReAlloc(GetProcessHeap(), 0, cmdline_list.order,
287 cmdline_list.alloc * sizeof(module_loadorder_t));
288 if(!cmdline_list.order)
290 MESSAGE("Virtual memory exhausted\n");
291 exit(1);
294 memcpy(cmdline_list.order[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
295 cmdline_list.order[i].modulename = HeapAlloc(GetProcessHeap(), 0, strlen(plo->modulename)+1);
296 strcpy( (char *)cmdline_list.order[i].modulename, plo->modulename );
297 cmdline_list.count++;
298 return TRUE;
302 /***************************************************************************
303 * AddLoadOrderSet (internal, static)
305 * Adds a set of entries in the list of command-line overrides from the key parameter.
307 static BOOL AddLoadOrderSet(char *key, char *order)
309 module_loadorder_t ldo;
310 char *cptr;
312 /* Parse the loadorder before the rest because strtok is not reentrant */
313 if(!ParseLoadOrder(order, ldo.loadorder))
314 return FALSE;
316 cptr = get_tok(key, ", \t");
317 while(cptr)
319 char *ext = strrchr(cptr, '.');
320 if(ext && !FILE_strcasecmp( ext, ".dll" )) *ext = 0;
321 ldo.modulename = cptr;
322 if(!AddLoadOrder(&ldo)) return FALSE;
323 cptr = get_tok(NULL, ", \t");
325 return TRUE;
329 /***************************************************************************
330 * MODULE_AddLoadOrderOption
332 * The commandline option is in the form:
333 * name[,name,...]=native[,b,...]
335 void MODULE_AddLoadOrderOption( const char *option )
337 char *value, *key = HeapAlloc(GetProcessHeap(), 0, strlen(option)+1);
339 strcpy( key, option );
340 if (!(value = strchr(key, '='))) goto error;
341 *value++ = '\0';
343 TRACE("Commandline override '%s' = '%s'\n", key, value);
345 if (!AddLoadOrderSet(key, value)) goto error;
346 HeapFree(GetProcessHeap(), 0, key);
348 /* sort the array for quick lookup */
349 qsort(cmdline_list.order, cmdline_list.count, sizeof(cmdline_list.order[0]), cmp_sort_func);
350 return;
352 error:
353 MESSAGE( "Syntax: -dll name[,name[,...]]={native|so|builtin}[,{n|s|b}[,...]]\n"
354 " - 'name' is the name of any dll without extension\n"
355 " - the order of loading (native, so and builtin) can be abbreviated\n"
356 " with the first letter\n"
357 " - the option can be specified multiple times\n"
358 " Example:\n"
359 " -dll comdlg32,commdlg=n -dll shell,shell32=b\n" );
360 ExitProcess(1);
364 /***************************************************************************
365 * get_list_load_order
367 * Get the load order for a given module from the command-line or
368 * default lists.
370 static BOOL get_list_load_order( const char *module, const struct loadorder_list *list,
371 enum loadorder_type lo[] )
373 module_loadorder_t tmp, *res = NULL;
375 tmp.modulename = module;
376 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
377 if (list->count && (res = bsearch(&tmp, list->order, list->count, sizeof(list->order[0]), cmp_sort_func)))
378 memcpy( lo, res->loadorder, sizeof(res->loadorder) );
379 return (res != NULL);
383 /***************************************************************************
384 * open_app_key
386 * Open the registry key to the app-specific DllOverrides list.
388 static HKEY open_app_key( const char *module )
390 OBJECT_ATTRIBUTES attr;
391 UNICODE_STRING nameW;
392 HKEY hkey, appkey;
393 char buffer[MAX_PATH+16], *appname;
394 static const WCHAR AppDefaultsW[] = {'M','a','c','h','i','n','e','\\',
395 'S','o','f','t','w','a','r','e','\\',
396 'W','i','n','e','\\',
397 'W','i','n','e','\\',
398 'C','o','n','f','i','g','\\',
399 'A','p','p','D','e','f','a','u','l','t','s',0};
401 if (!GetModuleFileNameA( 0, buffer, MAX_PATH ))
403 WARN( "could not get module file name loading %s\n", module );
404 return 0;
406 appname = (char *)get_basename( buffer );
408 TRACE( "searching '%s' in AppDefaults\\%s\\DllOverrides\n", module, appname );
410 attr.Length = sizeof(attr);
411 attr.RootDirectory = 0;
412 attr.ObjectName = &nameW;
413 attr.Attributes = 0;
414 attr.SecurityDescriptor = NULL;
415 attr.SecurityQualityOfService = NULL;
416 RtlInitUnicodeString( &nameW, AppDefaultsW );
418 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) return 0;
419 attr.RootDirectory = hkey;
421 /* open AppDefaults\\appname\\DllOverrides key */
422 strcat( appname, "\\DllOverrides" );
423 RtlCreateUnicodeStringFromAsciiz( &nameW, appname );
424 if (NtOpenKey( &appkey, KEY_ALL_ACCESS, &attr )) appkey = 0;
425 RtlFreeUnicodeString( &nameW );
426 NtClose( hkey );
427 return appkey;
431 /***************************************************************************
432 * get_registry_value
434 * Load the registry loadorder value for a given module.
436 static BOOL get_registry_value( HKEY hkey, const char *module, enum loadorder_type lo[] )
438 UNICODE_STRING valueW;
439 char buffer[80];
440 DWORD count;
441 BOOL ret;
443 RtlCreateUnicodeStringFromAsciiz( &valueW, module );
445 if ((ret = !NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation,
446 buffer, sizeof(buffer), &count )))
448 int i, n = 0;
449 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
451 while (*str)
453 enum loadorder_type type = LOADORDER_INVALID;
455 while (*str == ',' || isspaceW(*str)) str++;
456 if (!*str) break;
458 switch(tolowerW(*str))
460 case 'n': type = LOADORDER_DLL; break;
461 case 's': type = LOADORDER_SO; break;
462 case 'b': type = LOADORDER_BI; break;
463 case 0: break; /* end of string */
464 default:
465 ERR("Invalid load order module-type %s, ignored\n", debugstr_w(str));
466 break;
468 if (type != LOADORDER_INVALID)
470 for (i = 0; i < n; i++) if (lo[i] == type) break; /* already specified */
471 if (i == n) lo[n++] = type;
473 while (*str && *str != ',' && !isspaceW(*str)) str++;
475 lo[n] = LOADORDER_INVALID;
477 RtlFreeUnicodeString( &valueW );
478 return ret;
482 /***************************************************************************
483 * MODULE_GetBuiltinPath
485 * Get the path of a builtin module when the native file does not exist.
487 BOOL MODULE_GetBuiltinPath( const char *libname, const char *ext, char *filename, UINT size )
489 char *p;
490 BOOL ret = FALSE;
491 UINT len = GetSystemDirectoryA( filename, size );
493 if (FILE_contains_path( libname ))
495 char *tmp;
497 /* if the library name contains a path and can not be found,
498 * return an error.
499 * exception: if the path is the system directory, proceed,
500 * so that modules which are not PE modules can be loaded.
501 * If the library name does not contain a path and can not
502 * be found, assume the system directory is meant */
504 if (strlen(libname) >= size) return FALSE; /* too long */
505 if (strchr( libname, '/' )) /* need to convert slashes */
507 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, strlen(libname)+1 ))) return FALSE;
508 strcpy( tmp, libname );
509 for (p = tmp; *p; p++) if (*p == '/') *p = '\\';
511 else tmp = (char *)libname;
513 if (!FILE_strncasecmp( filename, tmp, len ) && tmp[len] == '\\')
515 strcpy( filename, tmp );
516 ret = TRUE;
518 if (tmp != libname) HeapFree( GetProcessHeap(), 0, tmp );
519 if (!ret) return FALSE;
521 else
523 if (strlen(libname) >= size - len - 1) return FALSE;
524 filename[len] = '\\';
525 strcpy( filename+len+1, libname );
528 /* if the filename doesn't have an extension, append the default */
529 if (!(p = strrchr( filename, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
531 if (strlen(filename) + strlen(ext) >= size) return FALSE;
532 strcat( filename, ext );
534 return TRUE;
538 /***************************************************************************
539 * MODULE_GetLoadOrder (internal)
541 * Locate the loadorder of a module.
542 * Any path is stripped from the path-argument and so are the extension
543 * '.dll' and '.exe'. A lookup in the table can yield an override for
544 * the specific dll. Otherwise the default load order is returned.
546 void MODULE_GetLoadOrder( enum loadorder_type loadorder[], const char *path, BOOL win32 )
548 static const WCHAR DllOverridesW[] = {'M','a','c','h','i','n','e','\\',
549 'S','o','f','t','w','a','r','e','\\',
550 'W','i','n','e','\\',
551 'W','i','n','e','\\',
552 'C','o','n','f','i','g','\\',
553 'D','l','l','O','v','e','r','r','i','d','e','s',0};
555 static HKEY std_key = (HKEY)-1; /* key to standard section, cached */
556 HKEY app_key = 0;
557 char *module, *basename;
558 int len;
560 TRACE("looking for %s\n", path);
562 loadorder[0] = LOADORDER_INVALID; /* in case something bad happens below */
564 /* Strip path information for 16 bit modules or if the module
565 * resides in the system directory */
566 if (!win32)
568 path = get_basename( path );
569 if (BUILTIN_IsPresent(path))
571 TRACE( "forcing loadorder to builtin for %s\n", debugstr_a(path) );
572 /* force builtin loadorder since the dll is already in memory */
573 loadorder[0] = LOADORDER_BI;
574 loadorder[1] = LOADORDER_INVALID;
575 return;
578 else
580 char sysdir[MAX_PATH+1];
581 if (!GetSystemDirectoryA( sysdir, MAX_PATH )) return;
582 if (!FILE_strncasecmp( sysdir, path, strlen (sysdir) ))
584 path += strlen(sysdir);
585 while (*path == '\\' || *path == '/') path++;
589 if (!(len = strlen(path))) return;
590 if (!(module = HeapAlloc( GetProcessHeap(), 0, len + 2 ))) return;
591 strcpy( module+1, path ); /* reserve module[0] for the wildcard char */
593 if (len >= 4)
595 char *ext = module + 1 + len - 4;
596 if (!FILE_strcasecmp( ext, ".dll" )) *ext = 0;
599 /* check command-line first */
600 if (get_list_load_order( module+1, &cmdline_list, loadorder ))
602 TRACE( "got cmdline %s for %s\n",
603 debugstr_loadorder(loadorder), debugstr_a(path) );
604 goto done;
607 /* then explicit module name in AppDefaults */
608 app_key = open_app_key( module+1 );
609 if (app_key && get_registry_value( app_key, module+1, loadorder ))
611 TRACE( "got app defaults %s for %s\n",
612 debugstr_loadorder(loadorder), debugstr_a(path) );
613 goto done;
616 /* then explicit module name in standard section */
617 if (std_key == (HKEY)-1)
619 OBJECT_ATTRIBUTES attr;
620 UNICODE_STRING nameW;
622 attr.Length = sizeof(attr);
623 attr.RootDirectory = 0;
624 attr.ObjectName = &nameW;
625 attr.Attributes = 0;
626 attr.SecurityDescriptor = NULL;
627 attr.SecurityQualityOfService = NULL;
628 RtlInitUnicodeString( &nameW, DllOverridesW );
630 if (NtOpenKey( &std_key, KEY_ALL_ACCESS, &attr )) std_key = 0;
633 if (std_key && get_registry_value( std_key, module+1, loadorder ))
635 TRACE( "got standard entry %s for %s\n",
636 debugstr_loadorder(loadorder), debugstr_a(path) );
637 goto done;
640 /* then module basename preceded by '*' in AppDefaults */
641 basename = (char *)get_basename( module+1 );
642 basename[-1] = '*';
643 if (app_key && get_registry_value( app_key, basename-1, loadorder ))
645 TRACE( "got app defaults basename %s for %s\n",
646 debugstr_loadorder(loadorder), debugstr_a(path) );
647 goto done;
650 /* then module name preceded by '*' in standard section */
651 if (std_key && get_registry_value( std_key, basename-1, loadorder ))
653 TRACE( "got standard base name %s for %s\n",
654 debugstr_loadorder(loadorder), debugstr_a(path) );
655 goto done;
658 /* then base name matching compiled-in defaults */
659 if (get_list_load_order( basename, &default_list, loadorder ))
661 TRACE( "got compiled-in default %s for %s\n",
662 debugstr_loadorder(loadorder), debugstr_a(path) );
663 goto done;
666 if (basename == module+1)
668 /* then wildcard entry in AppDefaults (only if no explicit path) */
669 if (app_key && get_registry_value( app_key, "*", loadorder ))
671 TRACE( "got app defaults wildcard %s for %s\n",
672 debugstr_loadorder(loadorder), debugstr_a(path) );
673 goto done;
676 /* then wildcard entry in standard section (only if no explicit path) */
677 if (std_key && get_registry_value( std_key, "*", loadorder ))
679 TRACE( "got standard wildcard %s for %s\n",
680 debugstr_loadorder(loadorder), debugstr_a(path) );
681 goto done;
684 /* and last the hard-coded default */
685 memcpy( loadorder, default_loadorder, sizeof(default_loadorder) );
686 TRACE( "got hardcoded default %s for %s\n",
687 debugstr_loadorder(loadorder), debugstr_a(path) );
689 else /* module contains an explicit path */
691 memcpy( loadorder, default_path_loadorder, sizeof(default_path_loadorder) );
692 TRACE( "got hardcoded path default %s for %s\n",
693 debugstr_loadorder(loadorder), debugstr_a(path) );
696 done:
697 if (app_key) NtClose( app_key );
698 HeapFree( GetProcessHeap(), 0, module );