1 /*
2 * Activation contexts
3 *
4 * Copyright 2004 Jon Griffiths
5 * Copyright 2007 Eric Pouech
6 * Copyright 2007 Jacek Caban for CodeWeavers
7 * Copyright 2007 Alexandre Julliard
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdarg.h>
28 #include <stdio.h>
29
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "winternl.h"
35 #include "ddk/wdm.h"
36 #include "ntdll_misc.h"
37 #include "wine/exception.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(actctx);
42
43 #define ACTCTX_FLAGS_ALL (\
44 ACTCTX_FLAG_PROCESSOR_ARCHITECTURE_VALID |\
45 ACTCTX_FLAG_LANGID_VALID |\
46 ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID |\
47 ACTCTX_FLAG_RESOURCE_NAME_VALID |\
48 ACTCTX_FLAG_SET_PROCESS_DEFAULT |\
49 ACTCTX_FLAG_APPLICATION_NAME_VALID |\
50 ACTCTX_FLAG_SOURCE_IS_ASSEMBLYREF |\
51 ACTCTX_FLAG_HMODULE_VALID )
52
53 #define ACTCTX_MAGIC 0xC07E3E11
54
55 /* we don't want to include winuser.h */
56 #define RT_MANIFEST ((ULONG_PTR)24)
57 #define CREATEPROCESS_MANIFEST_RESOURCE_ID ((ULONG_PTR)1)
58
59 typedef struct
60 {
61 const WCHAR *ptr;
62 unsigned int len;
63 } xmlstr_t;
64
65 typedef struct
66 {
67 const WCHAR *ptr;
68 const WCHAR *end;
69 } xmlbuf_t;
70
71 struct file_info
72 {
73 ULONG type;
74 WCHAR *info;
75 };
76
77 struct assembly_version
78 {
79 USHORT major;
80 USHORT minor;
81 USHORT build;
82 USHORT revision;
83 };
84
85 struct assembly_identity
86 {
87 WCHAR *name;
88 WCHAR *arch;
89 WCHAR *public_key;
90 WCHAR *language;
91 WCHAR *type;
92 struct assembly_version version;
93 BOOL optional;
94 };
95
96 struct entity
97 {
98 DWORD kind;
99 union
100 {
101 struct
102 {
103 WCHAR *tlbid;
104 WCHAR *version;
105 WCHAR *helpdir;
106 } typelib;
107 struct
108 {
109 WCHAR *clsid;
110 } comclass;
111 struct {
112 WCHAR *iid;
113 WCHAR *name;
114 } proxy;
115 struct
116 {
117 WCHAR *name;
118 } class;
119 struct
120 {
121 WCHAR *name;
122 WCHAR *clsid;
123 } clrclass;
124 struct
125 {
126 WCHAR *name;
127 WCHAR *clsid;
128 } clrsurrogate;
129 } u;
130 };
131
132 struct entity_array
133 {
134 struct entity *base;
135 unsigned int num;
136 unsigned int allocated;
137 };
138
139 struct dll_redirect
140 {
141 WCHAR *name;
142 WCHAR *hash;
143 struct entity_array entities;
144 };
145
146 enum assembly_type
147 {
148 APPLICATION_MANIFEST,
149 ASSEMBLY_MANIFEST,
150 ASSEMBLY_SHARED_MANIFEST,
151 };
152
153 struct assembly
154 {
155 enum assembly_type type;
156 struct assembly_identity id;
157 struct file_info manifest;
158 WCHAR *directory;
159 BOOL no_inherit;
160 struct dll_redirect *dlls;
161 unsigned int num_dlls;
162 unsigned int allocated_dlls;
163 struct entity_array entities;
164 };
165
166 typedef struct _ACTIVATION_CONTEXT
167 {
168 ULONG magic;
169 int ref_count;
170 struct file_info config;
171 struct file_info appdir;
172 struct assembly *assemblies;
173 unsigned int num_assemblies;
174 unsigned int allocated_assemblies;
175 } ACTIVATION_CONTEXT;
176
177 struct actctx_loader
178 {
179 ACTIVATION_CONTEXT *actctx;
180 struct assembly_identity *dependencies;
181 unsigned int num_dependencies;
182 unsigned int allocated_dependencies;
183 };
184
185 static const WCHAR asmv1W[] = {'a','s','m','v','1',':',0};
186 static const WCHAR asmv2W[] = {'a','s','m','v','2',':',0};
187 static const WCHAR assemblyW[] = {'a','s','s','e','m','b','l','y',0};
188 static const WCHAR assemblyIdentityW[] = {'a','s','s','e','m','b','l','y','I','d','e','n','t','i','t','y',0};
189 static const WCHAR bindingRedirectW[] = {'b','i','n','d','i','n','g','R','e','d','i','r','e','c','t',0};
190 static const WCHAR clrClassW[] = {'c','l','r','C','l','a','s','s',0};
191 static const WCHAR clrSurrogateW[] = {'c','l','r','S','u','r','r','o','g','a','t','e',0};
192 static const WCHAR comClassW[] = {'c','o','m','C','l','a','s','s',0};
193 static const WCHAR comInterfaceExternalProxyStubW[] = {'c','o','m','I','n','t','e','r','f','a','c','e','E','x','t','e','r','n','a','l','P','r','o','x','y','S','t','u','b',0};
194 static const WCHAR comInterfaceProxyStubW[] = {'c','o','m','I','n','t','e','r','f','a','c','e','P','r','o','x','y','S','t','u','b',0};
195 static const WCHAR dependencyW[] = {'d','e','p','e','n','d','e','n','c','y',0};
196 static const WCHAR dependentAssemblyW[] = {'d','e','p','e','n','d','e','n','t','A','s','s','e','m','b','l','y',0};
197 static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
198 static const WCHAR fileW[] = {'f','i','l','e',0};
199 static const WCHAR hashW[] = {'h','a','s','h',0};
200 static const WCHAR noInheritW[] = {'n','o','I','n','h','e','r','i','t',0};
201 static const WCHAR noInheritableW[] = {'n','o','I','n','h','e','r','i','t','a','b','l','e',0};
202 static const WCHAR typelibW[] = {'t','y','p','e','l','i','b',0};
203 static const WCHAR windowClassW[] = {'w','i','n','d','o','w','C','l','a','s','s',0};
204
205 static const WCHAR clsidW[] = {'c','l','s','i','d',0};
206 static const WCHAR hashalgW[] = {'h','a','s','h','a','l','g',0};
207 static const WCHAR helpdirW[] = {'h','e','l','p','d','i','r',0};
208 static const WCHAR iidW[] = {'i','i','d',0};
209 static const WCHAR languageW[] = {'l','a','n','g','u','a','g','e',0};
210 static const WCHAR manifestVersionW[] = {'m','a','n','i','f','e','s','t','V','e','r','s','i','o','n',0};
211 static const WCHAR nameW[] = {'n','a','m','e',0};
212 static const WCHAR neutralW[] = {'n','e','u','t','r','a','l',0};
213 static const WCHAR newVersionW[] = {'n','e','w','V','e','r','s','i','o','n',0};
214 static const WCHAR oldVersionW[] = {'o','l','d','V','e','r','s','i','o','n',0};
215 static const WCHAR optionalW[] = {'o','p','t','i','o','n','a','l',0};
216 static const WCHAR processorArchitectureW[] = {'p','r','o','c','e','s','s','o','r','A','r','c','h','i','t','e','c','t','u','r','e',0};
217 static const WCHAR publicKeyTokenW[] = {'p','u','b','l','i','c','K','e','y','T','o','k','e','n',0};
218 static const WCHAR tlbidW[] = {'t','l','b','i','d',0};
219 static const WCHAR typeW[] = {'t','y','p','e',0};
220 static const WCHAR versionW[] = {'v','e','r','s','i','o','n',0};
221 static const WCHAR xmlnsW[] = {'x','m','l','n','s',0};
222
223 static const WCHAR xmlW[] = {'?','x','m','l',0};
224 static const WCHAR manifestv1W[] = {'u','r','n',':','s','c','h','e','m','a','s','-','m','i','c','r','o','s','o','f','t','-','c','o','m',':','a','s','m','.','v','1',0};
225 static const WCHAR manifestv3W[] = {'u','r','n',':','s','c','h','e','m','a','s','-','m','i','c','r','o','s','o','f','t','-','c','o','m',':','a','s','m','.','v','3',0};
226
227 static const WCHAR dotManifestW[] = {'.','m','a','n','i','f','e','s','t',0};
228 static const WCHAR version_formatW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
229 static const WCHAR wildcardW[] = {'*',0};
230
231 static ACTIVATION_CONTEXT system_actctx = { ACTCTX_MAGIC, 1 };
232 static ACTIVATION_CONTEXT *process_actctx = &system_actctx;
233
234 static WCHAR *strdupW(const WCHAR* str)
235 {
236 WCHAR* ptr;
237
238 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(WCHAR))))
239 return NULL;
240 return strcpyW(ptr, str);
241 }
242
243 static WCHAR *xmlstrdupW(const xmlstr_t* str)
244 {
245 WCHAR *strW;
246
247 if ((strW = RtlAllocateHeap(GetProcessHeap(), 0, (str->len + 1) * sizeof(WCHAR))))
248 {
249 memcpy( strW, str->ptr, str->len * sizeof(WCHAR) );
250 strW[str->len] = 0;
251 }
252 return strW;
253 }
254
255 static inline BOOL xmlstr_cmp(const xmlstr_t* xmlstr, const WCHAR *str)
256 {
257 return !strncmpW(xmlstr->ptr, str, xmlstr->len) && !str[xmlstr->len];
258 }
259
260 static inline BOOL xmlstr_cmpi(const xmlstr_t* xmlstr, const WCHAR *str)
261 {
262 return !strncmpiW(xmlstr->ptr, str, xmlstr->len) && !str[xmlstr->len];
263 }
264
265 static inline BOOL xmlstr_cmp_end(const xmlstr_t* xmlstr, const WCHAR *str)
266 {
267 return (xmlstr->len && xmlstr->ptr[0] == '/' &&
268 !strncmpW(xmlstr->ptr + 1, str, xmlstr->len - 1) && !str[xmlstr->len - 1]);
269 }
270
271 static inline BOOL xml_elem_cmp(const xmlstr_t *elem, const WCHAR *str, const WCHAR *namespace)
272 {
273 UINT len = strlenW( namespace );
274
275 if (!strncmpW(elem->ptr, str, elem->len) && !str[elem->len]) return TRUE;
276 return (elem->len > len && !strncmpW(elem->ptr, namespace, len) &&
277 !strncmpW(elem->ptr + len, str, elem->len - len) && !str[elem->len - len]);
278 }
279
280 static inline BOOL xml_elem_cmp_end(const xmlstr_t *elem, const WCHAR *str, const WCHAR *namespace)
281 {
282 if (elem->len && elem->ptr[0] == '/')
283 {
284 xmlstr_t elem_end;
285 elem_end.ptr = elem->ptr + 1;
286 elem_end.len = elem->len - 1;
287 return xml_elem_cmp( &elem_end, str, namespace );
288 }
289 return FALSE;
290 }
291
292 static inline BOOL isxmlspace( WCHAR ch )
293 {
294 return (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t');
295 }
296
297 static inline const char* debugstr_xmlstr(const xmlstr_t* str)
298 {
299 return debugstr_wn(str->ptr, str->len);
300 }
301
302 static inline const char* debugstr_version(const struct assembly_version *ver)
303 {
304 return wine_dbg_sprintf("%u.%u.%u.%u", ver->major, ver->minor, ver->build, ver->revision);
305 }
306
307 static struct assembly *add_assembly(ACTIVATION_CONTEXT *actctx, enum assembly_type at)
308 {
309 struct assembly *assembly;
310
311 if (actctx->num_assemblies == actctx->allocated_assemblies)
312 {
313 void *ptr;
314 unsigned int new_count;
315 if (actctx->assemblies)
316 {
317 new_count = actctx->allocated_assemblies * 2;
318 ptr = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
319 actctx->assemblies, new_count * sizeof(*assembly) );
320 }
321 else
322 {
323 new_count = 4;
324 ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*assembly) );
325 }
326 if (!ptr) return NULL;
327 actctx->assemblies = ptr;
328 actctx->allocated_assemblies = new_count;
329 }
330
331 assembly = &actctx->assemblies[actctx->num_assemblies++];
332 assembly->type = at;
333 return assembly;
334 }
335
336 static struct dll_redirect* add_dll_redirect(struct assembly* assembly)
337 {
338 if (assembly->num_dlls == assembly->allocated_dlls)
339 {
340 void *ptr;
341 unsigned int new_count;
342 if (assembly->dlls)
343 {
344 new_count = assembly->allocated_dlls * 2;
345 ptr = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
346 assembly->dlls, new_count * sizeof(*assembly->dlls) );
347 }
348 else
349 {
350 new_count = 4;
351 ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*assembly->dlls) );
352 }
353 if (!ptr) return NULL;
354 assembly->dlls = ptr;
355 assembly->allocated_dlls = new_count;
356 }
357 return &assembly->dlls[assembly->num_dlls++];
358 }
359
360 static void free_assembly_identity(struct assembly_identity *ai)
361 {
362 RtlFreeHeap( GetProcessHeap(), 0, ai->name );
363 RtlFreeHeap( GetProcessHeap(), 0, ai->arch );
364 RtlFreeHeap( GetProcessHeap(), 0, ai->public_key );
365 RtlFreeHeap( GetProcessHeap(), 0, ai->language );
366 RtlFreeHeap( GetProcessHeap(), 0, ai->type );
367 }
368
369 static struct entity* add_entity(struct entity_array *array, DWORD kind)
370 {
371 struct entity* entity;
372
373 if (array->num == array->allocated)
374 {
375 void *ptr;
376 unsigned int new_count;
377 if (array->base)
378 {
379 new_count = array->allocated * 2;
380 ptr = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
381 array->base, new_count * sizeof(*array->base) );
382 }
383 else
384 {
385 new_count = 4;
386 ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*array->base) );
387 }
388 if (!ptr) return NULL;
389 array->base = ptr;
390 array->allocated = new_count;
391 }
392 entity = &array->base[array->num++];
393 entity->kind = kind;
394 return entity;
395 }
396
397 static void free_entity_array(struct entity_array *array)
398 {
399 unsigned int i;
400 for (i = 0; i < array->num; i++)
401 {
402 struct entity *entity = &array->base[i];
403 switch (entity->kind)
404 {
405 case ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION:
406 RtlFreeHeap(GetProcessHeap(), 0, entity->u.comclass.clsid);
407 break;
408 case ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION:
409 RtlFreeHeap(GetProcessHeap(), 0, entity->u.proxy.iid);
410 RtlFreeHeap(GetProcessHeap(), 0, entity->u.proxy.name);
411 break;
412 case ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION:
413 RtlFreeHeap(GetProcessHeap(), 0, entity->u.typelib.tlbid);
414 RtlFreeHeap(GetProcessHeap(), 0, entity->u.typelib.version);
415 RtlFreeHeap(GetProcessHeap(), 0, entity->u.typelib.helpdir);
416 break;
417 case ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION:
418 RtlFreeHeap(GetProcessHeap(), 0, entity->u.class.name);
419 break;
420 case ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION:
421 RtlFreeHeap(GetProcessHeap(), 0, entity->u.clrclass.name);
422 RtlFreeHeap(GetProcessHeap(), 0, entity->u.clrclass.clsid);
423 break;
424 case ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES:
425 RtlFreeHeap(GetProcessHeap(), 0, entity->u.clrsurrogate.name);
426 RtlFreeHeap(GetProcessHeap(), 0, entity->u.clrsurrogate.clsid);
427 break;
428 default:
429 FIXME("Unknown entity kind %d\n", entity->kind);
430 }
431 }
432 RtlFreeHeap( GetProcessHeap(), 0, array->base );
433 }
434
435 static BOOL is_matching_string( const WCHAR *str1, const WCHAR *str2 )
436 {
437 if (!str1) return !str2;
438 return str2 && !strcmpiW( str1, str2 );
439 }
440
441 static BOOL is_matching_identity( const struct assembly_identity *id1,
442 const struct assembly_identity *id2 )
443 {
444 if (!is_matching_string( id1->name, id2->name )) return FALSE;
445 if (!is_matching_string( id1->arch, id2->arch )) return FALSE;
446 if (!is_matching_string( id1->public_key, id2->public_key )) return FALSE;
447
448 if (id1->language && id2->language && strcmpiW( id1->language, id2->language ))
449 {
450 if (strcmpW( wildcardW, id1->language ) && strcmpW( wildcardW, id2->language ))
451 return FALSE;
452 }
453 if (id1->version.major != id2->version.major) return FALSE;
454 if (id1->version.minor != id2->version.minor) return FALSE;
455 if (id1->version.build > id2->version.build) return FALSE;
456 if (id1->version.build == id2->version.build &&
457 id1->version.revision > id2->version.revision) return FALSE;
458 return TRUE;
459 }
460
461 static BOOL add_dependent_assembly_id(struct actctx_loader* acl,
462 struct assembly_identity* ai)
463 {
464 unsigned int i;
465
466 /* check if we already have that assembly */
467
468 for (i = 0; i < acl->actctx->num_assemblies; i++)
469 if (is_matching_identity( ai, &acl->actctx->assemblies[i].id ))
470 {
471 TRACE( "reusing existing assembly for %s arch %s version %u.%u.%u.%u\n",
472 debugstr_w(ai->name), debugstr_w(ai->arch), ai->version.major, ai->version.minor,
473 ai->version.build, ai->version.revision );
474 return TRUE;
475 }
476
477 for (i = 0; i < acl->num_dependencies; i++)
478 if (is_matching_identity( ai, &acl->dependencies[i] ))
479 {
480 TRACE( "reusing existing dependency for %s arch %s version %u.%u.%u.%u\n",
481 debugstr_w(ai->name), debugstr_w(ai->arch), ai->version.major, ai->version.minor,
482 ai->version.build, ai->version.revision );
483 return TRUE;
484 }
485
486 if (acl->num_dependencies == acl->allocated_dependencies)
487 {
488 void *ptr;
489 unsigned int new_count;
490 if (acl->dependencies)
491 {
492 new_count = acl->allocated_dependencies * 2;
493 ptr = RtlReAllocateHeap(GetProcessHeap(), 0, acl->dependencies,
494 new_count * sizeof(acl->dependencies[0]));
495 }
496 else
497 {
498 new_count = 4;
499 ptr = RtlAllocateHeap(GetProcessHeap(), 0, new_count * sizeof(acl->dependencies[0]));
500 }
501 if (!ptr) return FALSE;
502 acl->dependencies = ptr;
503 acl->allocated_dependencies = new_count;
504 }
505 acl->dependencies[acl->num_dependencies++] = *ai;
506
507 return TRUE;
508 }
509
510 static void free_depend_manifests(struct actctx_loader* acl)
511 {
512 unsigned int i;
513 for (i = 0; i < acl->num_dependencies; i++)
514 free_assembly_identity(&acl->dependencies[i]);
515 RtlFreeHeap(GetProcessHeap(), 0, acl->dependencies);
516 }
517
518 static WCHAR *build_assembly_dir(struct assembly_identity* ai)
519 {
520 static const WCHAR undW[] = {'_',0};
521 static const WCHAR noneW[] = {'n','o','n','e',0};
522 static const WCHAR mskeyW[] = {'d','e','a','d','b','e','e','f',0};
523
524 const WCHAR *arch = ai->arch ? ai->arch : noneW;
525 const WCHAR *key = ai->public_key ? ai->public_key : noneW;
526 const WCHAR *lang = ai->language ? ai->language : noneW;
527 const WCHAR *name = ai->name ? ai->name : noneW;
528 SIZE_T size = (strlenW(arch) + 1 + strlenW(name) + 1 + strlenW(key) + 24 + 1 +
529 strlenW(lang) + 1) * sizeof(WCHAR) + sizeof(mskeyW);
530 WCHAR *ret;
531
532 if (!(ret = RtlAllocateHeap( GetProcessHeap(), 0, size ))) return NULL;
533
534 strcpyW( ret, arch );
535 strcatW( ret, undW );
536 strcatW( ret, name );
537 strcatW( ret, undW );
538 strcatW( ret, key );
539 strcatW( ret, undW );
540 sprintfW( ret + strlenW(ret), version_formatW,
541 ai->version.major, ai->version.minor, ai->version.build, ai->version.revision );
542 strcatW( ret, undW );
543 strcatW( ret, lang );
544 strcatW( ret, undW );
545 strcatW( ret, mskeyW );
546 return ret;
547 }
548
549 static inline void append_string( WCHAR *buffer, const WCHAR *prefix, const WCHAR *str )
550 {
551 WCHAR *p = buffer;
552
553 if (!str) return;
554 strcatW( buffer, prefix );
555 p += strlenW(p);
556 *p++ = '"';
557 strcpyW( p, str );
558 p += strlenW(p);
559 *p++ = '"';
560 *p = 0;
561 }
562
563 static WCHAR *build_assembly_id( const struct assembly_identity *ai )
564 {
565 static const WCHAR archW[] =
566 {',','p','r','o','c','e','s','s','o','r','A','r','c','h','i','t','e','c','t','u','r','e','=',0};
567 static const WCHAR public_keyW[] =
568 {',','p','u','b','l','i','c','K','e','y','T','o','k','e','n','=',0};
569 static const WCHAR typeW[] =
570 {',','t','y','p','e','=',0};
571 static const WCHAR versionW[] =
572 {',','v','e','r','s','i','o','n','=',0};
573
574 WCHAR version[64], *ret;
575 SIZE_T size = 0;
576
577 sprintfW( version, version_formatW,
578 ai->version.major, ai->version.minor, ai->version.build, ai->version.revision );
579 if (ai->name) size += strlenW(ai->name) * sizeof(WCHAR);
580 if (ai->arch) size += strlenW(archW) + strlenW(ai->arch) + 2;
581 if (ai->public_key) size += strlenW(public_keyW) + strlenW(ai->public_key) + 2;
582 if (ai->type) size += strlenW(typeW) + strlenW(ai->type) + 2;
583 size += strlenW(versionW) + strlenW(version) + 2;
584
585 if (!(ret = RtlAllocateHeap( GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR) )))
586 return NULL;
587
588 if (ai->name) strcpyW( ret, ai->name );
589 else *ret = 0;
590 append_string( ret, archW, ai->arch );
591 append_string( ret, public_keyW, ai->public_key );
592 append_string( ret, typeW, ai->type );
593 append_string( ret, versionW, version );
594 return ret;
595 }
596
597 static ACTIVATION_CONTEXT *check_actctx( HANDLE h )
598 {
599 ACTIVATION_CONTEXT *ret = NULL, *actctx = h;
600
601 if (!h || h == INVALID_HANDLE_VALUE) return NULL;
602 __TRY
603 {
604 if (actctx->magic == ACTCTX_MAGIC) ret = actctx;
605 }
606 __EXCEPT_PAGE_FAULT
607 {
608 }
609 __ENDTRY
610 return ret;
611 }
612
613 static inline void actctx_addref( ACTIVATION_CONTEXT *actctx )
614 {
615 interlocked_xchg_add( &actctx->ref_count, 1 );
616 }
617
618 static void actctx_release( ACTIVATION_CONTEXT *actctx )
619 {
620 if (interlocked_xchg_add( &actctx->ref_count, -1 ) == 1)
621 {
622 unsigned int i, j;
623
624 for (i = 0; i < actctx->num_assemblies; i++)
625 {
626 struct assembly *assembly = &actctx->assemblies[i];
627 for (j = 0; j < assembly->num_dlls; j++)
628 {
629 struct dll_redirect *dll = &assembly->dlls[j];
630 free_entity_array( &dll->entities );
631 RtlFreeHeap( GetProcessHeap(), 0, dll->name );
632 RtlFreeHeap( GetProcessHeap(), 0, dll->hash );
633 }
634 RtlFreeHeap( GetProcessHeap(), 0, assembly->dlls );
635 RtlFreeHeap( GetProcessHeap(), 0, assembly->manifest.info );
636 RtlFreeHeap( GetProcessHeap(), 0, assembly->directory );
637 free_entity_array( &assembly->entities );
638 free_assembly_identity(&assembly->id);
639 }
640 RtlFreeHeap( GetProcessHeap(), 0, actctx->config.info );
641 RtlFreeHeap( GetProcessHeap(), 0, actctx->appdir.info );
642 RtlFreeHeap( GetProcessHeap(), 0, actctx->assemblies );
643 actctx->magic = 0;
644 RtlFreeHeap( GetProcessHeap(), 0, actctx );
645 }
646 }
647
648 static BOOL next_xml_attr(xmlbuf_t* xmlbuf, xmlstr_t* name, xmlstr_t* value,
649 BOOL* error, BOOL* end)
650 {
651 const WCHAR* ptr;
652
653 *error = TRUE;
654
655 while (xmlbuf->ptr < xmlbuf->end && isxmlspace(*xmlbuf->ptr))
656 xmlbuf->ptr++;
657
658 if (xmlbuf->ptr == xmlbuf->end) return FALSE;
659
660 if (*xmlbuf->ptr == '/')
661 {
662 xmlbuf->ptr++;
663 if (xmlbuf->ptr == xmlbuf->end || *xmlbuf->ptr != '>')
664 return FALSE;
665
666 xmlbuf->ptr++;
667 *end = TRUE;
668 *error = FALSE;
669 return FALSE;
670 }
671
672 if (*xmlbuf->ptr == '>')
673 {
674 xmlbuf->ptr++;
675 *error = FALSE;
676 return FALSE;
677 }
678
679 ptr = xmlbuf->ptr;
680 while (ptr < xmlbuf->end && *ptr != '=' && *ptr != '>' && !isxmlspace(*ptr)) ptr++;
681
682 if (ptr == xmlbuf->end || *ptr != '=') return FALSE;
683
684 name->ptr = xmlbuf->ptr;
685 name->len = ptr-xmlbuf->ptr;
686 xmlbuf->ptr = ptr;
687
688 ptr++;
689 if (ptr == xmlbuf->end || (*ptr != '"' && *ptr != '\'')) return FALSE;
690
691 value->ptr = ++ptr;
692 if (ptr == xmlbuf->end) return FALSE;
693
694 ptr = memchrW(ptr, ptr[-1], xmlbuf->end - ptr);
695 if (!ptr)
696 {
697 xmlbuf->ptr = xmlbuf->end;
698 return FALSE;
699 }
700
701 value->len = ptr - value->ptr;
702 xmlbuf->ptr = ptr + 1;
703
704 if (xmlbuf->ptr == xmlbuf->end) return FALSE;
705
706 *error = FALSE;
707 return TRUE;
708 }
709
710 static BOOL next_xml_elem(xmlbuf_t* xmlbuf, xmlstr_t* elem)
711 {
712 const WCHAR* ptr;
713
714 for (;;)
715 {
716 ptr = memchrW(xmlbuf->ptr, '<', xmlbuf->end - xmlbuf->ptr);
717 if (!ptr)
718 {
719 xmlbuf->ptr = xmlbuf->end;
720 return FALSE;
721 }
722 ptr++;
723 if (ptr + 3 < xmlbuf->end && ptr[0] == '!' && ptr[1] == '-' && ptr[2] == '-') /* skip comment */
724 {
725 for (ptr += 3; ptr + 3 <= xmlbuf->end; ptr++)
726 if (ptr[0] == '-' && ptr[1] == '-' && ptr[2] == '>') break;
727
728 if (ptr + 3 > xmlbuf->end)
729 {
730 xmlbuf->ptr = xmlbuf->end;
731 return FALSE;
732 }
733 xmlbuf->ptr = ptr + 3;
734 }
735 else break;
736 }
737
738 xmlbuf->ptr = ptr;
739 while (ptr < xmlbuf->end && !isxmlspace(*ptr) && *ptr != '>' && (*ptr != '/' || ptr == xmlbuf->ptr))
740 ptr++;
741
742 elem->ptr = xmlbuf->ptr;
743 elem->len = ptr - xmlbuf->ptr;
744 xmlbuf->ptr = ptr;
745 return xmlbuf->ptr != xmlbuf->end;
746 }
747
748 static BOOL parse_xml_header(xmlbuf_t* xmlbuf)
749 {
750 /* FIXME: parse attributes */
751 const WCHAR *ptr;
752
753 for (ptr = xmlbuf->ptr; ptr < xmlbuf->end - 1; ptr++)
754 {
755 if (ptr[0] == '?' && ptr[1] == '>')
756 {
757 xmlbuf->ptr = ptr + 2;
758 return TRUE;
759 }
760 }
761 return FALSE;
762 }
763
764 static BOOL parse_text_content(xmlbuf_t* xmlbuf, xmlstr_t* content)
765 {
766 const WCHAR *ptr = memchrW(xmlbuf->ptr, '<', xmlbuf->end - xmlbuf->ptr);
767
768 if (!ptr) return FALSE;
769
770 content->ptr = xmlbuf->ptr;
771 content->len = ptr - xmlbuf->ptr;
772 xmlbuf->ptr = ptr;
773
774 return TRUE;
775 }
776
777 static BOOL parse_version(const xmlstr_t *str, struct assembly_version *version)
778 {
779 unsigned int ver[4];
780 unsigned int pos;
781 const WCHAR *curr;
782
783 /* major.minor.build.revision */
784 ver[0] = ver[1] = ver[2] = ver[3] = pos = 0;
785 for (curr = str->ptr; curr < str->ptr + str->len; curr++)
786 {
787 if (*curr >= '' && *curr <= '9')
788 {
789 ver[pos] = ver[pos] * 10 + *curr - '';
790 if (ver[pos] >= 0x10000) goto error;
791 }
792 else if (*curr == '.')
793 {
794 if (++pos >= 4) goto error;
795 }
796 else goto error;
797 }
798 version->major = ver[0];
799 version->minor = ver[1];
800 version->build = ver[2];
801 version->revision = ver[3];
802 return TRUE;
803
804 error:
805 FIXME( "Wrong version definition in manifest file (%s)\n", debugstr_xmlstr(str) );
806 return FALSE;
807 }
808
809 static BOOL parse_expect_elem(xmlbuf_t* xmlbuf, const WCHAR* name, const WCHAR *namespace)
810 {
811 xmlstr_t elem;
812 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
813 if (xml_elem_cmp(&elem, name, namespace)) return TRUE;
814 FIXME( "unexpected element %s\n", debugstr_xmlstr(&elem) );
815 return FALSE;
816 }
817
818 static BOOL parse_expect_no_attr(xmlbuf_t* xmlbuf, BOOL* end)
819 {
820 xmlstr_t attr_name, attr_value;
821 BOOL error;
822
823 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, end))
824 {
825 WARN("unexpected attr %s=%s\n", debugstr_xmlstr(&attr_name),
826 debugstr_xmlstr(&attr_value));
827 }
828 return !error;
829 }
830
831 static BOOL parse_end_element(xmlbuf_t *xmlbuf)
832 {
833 BOOL end = FALSE;
834 return parse_expect_no_attr(xmlbuf, &end) && !end;
835 }
836
837 static BOOL parse_expect_end_elem(xmlbuf_t *xmlbuf, const WCHAR *name, const WCHAR *namespace)
838 {
839 xmlstr_t elem;
840 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
841 if (!xml_elem_cmp_end(&elem, name, namespace))
842 {
843 FIXME( "unexpected element %s\n", debugstr_xmlstr(&elem) );
844 return FALSE;
845 }
846 return parse_end_element(xmlbuf);
847 }
848
849 static BOOL parse_unknown_elem(xmlbuf_t *xmlbuf, const xmlstr_t *unknown_elem)
850 {
851 xmlstr_t attr_name, attr_value, elem;
852 BOOL end = FALSE, error, ret = TRUE;
853
854 while(next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end));
855 if(error || end) return end;
856
857 while(ret && (ret = next_xml_elem(xmlbuf, &elem)))
858 {
859 if(*elem.ptr == '/' && elem.len - 1 == unknown_elem->len &&
860 !strncmpW(elem.ptr+1, unknown_elem->ptr, unknown_elem->len))
861 break;
862 else
863 ret = parse_unknown_elem(xmlbuf, &elem);
864 }
865
866 return ret && parse_end_element(xmlbuf);
867 }
868
869 static BOOL parse_assembly_identity_elem(xmlbuf_t* xmlbuf, ACTIVATION_CONTEXT* actctx,
870 struct assembly_identity* ai)
871 {
872 xmlstr_t attr_name, attr_value;
873 BOOL end = FALSE, error;
874
875 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
876 {
877 if (xmlstr_cmp(&attr_name, nameW))
878 {
879 if (!(ai->name = xmlstrdupW(&attr_value))) return FALSE;
880 }
881 else if (xmlstr_cmp(&attr_name, typeW))
882 {
883 if (!(ai->type = xmlstrdupW(&attr_value))) return FALSE;
884 }
885 else if (xmlstr_cmp(&attr_name, versionW))
886 {
887 if (!parse_version(&attr_value, &ai->version)) return FALSE;
888 }
889 else if (xmlstr_cmp(&attr_name, processorArchitectureW))
890 {
891 if (!(ai->arch = xmlstrdupW(&attr_value))) return FALSE;
892 }
893 else if (xmlstr_cmp(&attr_name, publicKeyTokenW))
894 {
895 if (!(ai->public_key = xmlstrdupW(&attr_value))) return FALSE;
896 }
897 else if (xmlstr_cmp(&attr_name, languageW))
898 {
899 WARN("Unsupported yet language attribute (%s)\n",
900 debugstr_xmlstr(&attr_value));
901 if (!(ai->language = xmlstrdupW(&attr_value))) return FALSE;
902 }
903 else
904 {
905 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name),
906 debugstr_xmlstr(&attr_value));
907 }
908 }
909
910 TRACE( "name=%s version=%s arch=%s\n",
911 debugstr_w(ai->name), debugstr_version(&ai->version), debugstr_w(ai->arch) );
912
913 if (error || end) return end;
914 return parse_expect_end_elem(xmlbuf, assemblyIdentityW, asmv1W);
915 }
916
917 static BOOL parse_com_class_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
918 {
919 xmlstr_t elem, attr_name, attr_value;
920 BOOL ret, end = FALSE, error;
921 struct entity* entity;
922
923 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION)))
924 return FALSE;
925
926 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
927 {
928 if (xmlstr_cmp(&attr_name, clsidW))
929 {
930 if (!(entity->u.comclass.clsid = xmlstrdupW(&attr_value))) return FALSE;
931 }
932 else
933 {
934 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
935 }
936 }
937
938 if (error || end) return end;
939
940 while ((ret = next_xml_elem(xmlbuf, &elem)))
941 {
942 if (xmlstr_cmp_end(&elem, comClassW))
943 {
944 ret = parse_end_element(xmlbuf);
945 break;
946 }
947 else
948 {
949 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
950 ret = parse_unknown_elem(xmlbuf, &elem);
951 }
952 }
953 return ret;
954 }
955
956 static BOOL parse_cominterface_proxy_stub_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
957 {
958 xmlstr_t attr_name, attr_value;
959 BOOL end = FALSE, error;
960 struct entity* entity;
961
962 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION)))
963 return FALSE;
964
965 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
966 {
967 if (xmlstr_cmp(&attr_name, iidW))
968 {
969 if (!(entity->u.proxy.iid = xmlstrdupW(&attr_value))) return FALSE;
970 }
971 if (xmlstr_cmp(&attr_name, nameW))
972 {
973 if (!(entity->u.proxy.name = xmlstrdupW(&attr_value))) return FALSE;
974 }
975 else
976 {
977 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
978 }
979 }
980
981 if (error || end) return end;
982 return parse_expect_end_elem(xmlbuf, comInterfaceProxyStubW, asmv1W);
983 }
984
985 static BOOL parse_typelib_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
986 {
987 xmlstr_t attr_name, attr_value;
988 BOOL end = FALSE, error;
989 struct entity* entity;
990
991 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION)))
992 return FALSE;
993
994 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
995 {
996 if (xmlstr_cmp(&attr_name, tlbidW))
997 {
998 if (!(entity->u.typelib.tlbid = xmlstrdupW(&attr_value))) return FALSE;
999 }
1000 if (xmlstr_cmp(&attr_name, versionW))
1001 {
1002 if (!(entity->u.typelib.version = xmlstrdupW(&attr_value))) return FALSE;
1003 }
1004 if (xmlstr_cmp(&attr_name, helpdirW))
1005 {
1006 if (!(entity->u.typelib.helpdir = xmlstrdupW(&attr_value))) return FALSE;
1007 }
1008 else
1009 {
1010 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1011 }
1012 }
1013
1014 if (error || end) return end;
1015 return parse_expect_end_elem(xmlbuf, typelibW, asmv1W);
1016 }
1017
1018 static BOOL parse_window_class_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
1019 {
1020 xmlstr_t elem, content;
1021 BOOL end = FALSE, ret = TRUE;
1022 struct entity* entity;
1023
1024 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION)))
1025 return FALSE;
1026
1027 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1028 if (end) return FALSE;
1029
1030 if (!parse_text_content(xmlbuf, &content)) return FALSE;
1031
1032 if (!(entity->u.class.name = xmlstrdupW(&content))) return FALSE;
1033
1034 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1035 {
1036 if (xmlstr_cmp_end(&elem, windowClassW))
1037 {
1038 ret = parse_end_element(xmlbuf);
1039 break;
1040 }
1041 else
1042 {
1043 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1044 ret = parse_unknown_elem(xmlbuf, &elem);
1045 }
1046 }
1047
1048 return ret;
1049 }
1050
1051 static BOOL parse_binding_redirect_elem(xmlbuf_t* xmlbuf)
1052 {
1053 xmlstr_t attr_name, attr_value;
1054 BOOL end = FALSE, error;
1055
1056 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1057 {
1058 if (xmlstr_cmp(&attr_name, oldVersionW))
1059 {
1060 FIXME("Not stored yet oldVersion=%s\n", debugstr_xmlstr(&attr_value));
1061 }
1062 else if (xmlstr_cmp(&attr_name, newVersionW))
1063 {
1064 FIXME("Not stored yet newVersion=%s\n", debugstr_xmlstr(&attr_value));
1065 }
1066 else
1067 {
1068 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1069 }
1070 }
1071
1072 if (error || end) return end;
1073 return parse_expect_end_elem(xmlbuf, bindingRedirectW, asmv1W);
1074 }
1075
1076 static BOOL parse_description_elem(xmlbuf_t* xmlbuf)
1077 {
1078 xmlstr_t elem, content;
1079 BOOL end = FALSE, ret = TRUE;
1080
1081 if (!parse_expect_no_attr(xmlbuf, &end) || end ||
1082 !parse_text_content(xmlbuf, &content))
1083 return FALSE;
1084
1085 TRACE("Got description %s\n", debugstr_xmlstr(&content));
1086
1087 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1088 {
1089 if (xmlstr_cmp_end(&elem, descriptionW))
1090 {
1091 ret = parse_end_element(xmlbuf);
1092 break;
1093 }
1094 else
1095 {
1096 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1097 ret = parse_unknown_elem(xmlbuf, &elem);
1098 }
1099 }
1100
1101 return ret;
1102 }
1103
1104 static BOOL parse_com_interface_external_proxy_stub_elem(xmlbuf_t* xmlbuf,
1105 struct assembly* assembly)
1106 {
1107 xmlstr_t attr_name, attr_value;
1108 BOOL end = FALSE, error;
1109 struct entity* entity;
1110
1111 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION);
1112 if (!entity) return FALSE;
1113
1114 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1115 {
1116 if (xmlstr_cmp(&attr_name, iidW))
1117 {
1118 if (!(entity->u.proxy.iid = xmlstrdupW(&attr_value))) return FALSE;
1119 }
1120 if (xmlstr_cmp(&attr_name, nameW))
1121 {
1122 if (!(entity->u.proxy.name = xmlstrdupW(&attr_value))) return FALSE;
1123 }
1124 else
1125 {
1126 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1127 }
1128 }
1129
1130 if (error || end) return end;
1131 return parse_expect_end_elem(xmlbuf, comInterfaceExternalProxyStubW, asmv1W);
1132 }
1133
1134 static BOOL parse_clr_class_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1135 {
1136 xmlstr_t attr_name, attr_value;
1137 BOOL end = FALSE, error;
1138 struct entity* entity;
1139
1140 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION);
1141 if (!entity) return FALSE;
1142
1143 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1144 {
1145 if (xmlstr_cmp(&attr_name, nameW))
1146 {
1147 if (!(entity->u.clrclass.name = xmlstrdupW(&attr_value))) return FALSE;
1148 }
1149 else if (xmlstr_cmp(&attr_name, clsidW))
1150 {
1151 if (!(entity->u.clrclass.clsid = xmlstrdupW(&attr_value))) return FALSE;
1152 }
1153 else
1154 {
1155 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1156 }
1157 }
1158
1159 if (error || end) return end;
1160 return parse_expect_end_elem(xmlbuf, clrClassW, asmv1W);
1161 }
1162
1163 static BOOL parse_clr_surrogate_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1164 {
1165 xmlstr_t attr_name, attr_value;
1166 BOOL end = FALSE, error;
1167 struct entity* entity;
1168
1169 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES);
1170 if (!entity) return FALSE;
1171
1172 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1173 {
1174 if (xmlstr_cmp(&attr_name, nameW))
1175 {
1176 if (!(entity->u.clrsurrogate.name = xmlstrdupW(&attr_value))) return FALSE;
1177 }
1178 else if (xmlstr_cmp(&attr_name, clsidW))
1179 {
1180 if (!(entity->u.clrsurrogate.clsid = xmlstrdupW(&attr_value))) return FALSE;
1181 }
1182 else
1183 {
1184 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1185 }
1186 }
1187
1188 if (error || end) return end;
1189 return parse_expect_end_elem(xmlbuf, clrSurrogateW, asmv1W);
1190 }
1191
1192 static BOOL parse_dependent_assembly_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl, BOOL optional)
1193 {
1194 struct assembly_identity ai;
1195 xmlstr_t elem;
1196 BOOL end = FALSE, ret = TRUE;
1197
1198 if (!parse_expect_no_attr(xmlbuf, &end) || end) return end;
1199
1200 memset(&ai, 0, sizeof(ai));
1201 ai.optional = optional;
1202
1203 if (!parse_expect_elem(xmlbuf, assemblyIdentityW, asmv1W) ||
1204 !parse_assembly_identity_elem(xmlbuf, acl->actctx, &ai))
1205 return FALSE;
1206
1207 TRACE( "adding name=%s version=%s arch=%s\n",
1208 debugstr_w(ai.name), debugstr_version(&ai.version), debugstr_w(ai.arch) );
1209
1210 /* store the newly found identity for later loading */
1211 if (!add_dependent_assembly_id(acl, &ai)) return FALSE;
1212
1213 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1214 {
1215 if (xmlstr_cmp_end(&elem, dependentAssemblyW))
1216 {
1217 ret = parse_end_element(xmlbuf);
1218 break;
1219 }
1220 else if (xmlstr_cmp(&elem, bindingRedirectW))
1221 {
1222 ret = parse_binding_redirect_elem(xmlbuf);
1223 }
1224 else
1225 {
1226 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1227 ret = parse_unknown_elem(xmlbuf, &elem);
1228 }
1229 }
1230
1231 return ret;
1232 }
1233
1234 static BOOL parse_dependency_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl)
1235 {
1236 xmlstr_t attr_name, attr_value, elem;
1237 BOOL end = FALSE, ret = TRUE, error, optional = FALSE;
1238
1239 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1240 {
1241 if (xmlstr_cmp(&attr_name, optionalW))
1242 {
1243 static const WCHAR yesW[] = {'y','e','s',0};
1244 optional = xmlstr_cmpi( &attr_value, yesW );
1245 TRACE("optional=%s\n", debugstr_xmlstr(&attr_value));
1246 }
1247 else
1248 {
1249 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1250 }
1251 }
1252
1253 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1254 {
1255 if (xmlstr_cmp_end(&elem, dependencyW))
1256 {
1257 ret = parse_end_element(xmlbuf);
1258 break;
1259 }
1260 else if (xmlstr_cmp(&elem, dependentAssemblyW))
1261 {
1262 ret = parse_dependent_assembly_elem(xmlbuf, acl, optional);
1263 }
1264 else
1265 {
1266 WARN("unknown element %s\n", debugstr_xmlstr(&elem));
1267 ret = parse_unknown_elem(xmlbuf, &elem);
1268 }
1269 }
1270
1271 return ret;
1272 }
1273
1274 static BOOL parse_noinherit_elem(xmlbuf_t* xmlbuf)
1275 {
1276 BOOL end = FALSE;
1277
1278 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1279 return end || parse_expect_end_elem(xmlbuf, noInheritW, asmv1W);
1280 }
1281
1282 static BOOL parse_noinheritable_elem(xmlbuf_t* xmlbuf)
1283 {
1284 BOOL end = FALSE;
1285
1286 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1287 return end || parse_expect_end_elem(xmlbuf, noInheritableW, asmv1W);
1288 }
1289
1290 static BOOL parse_file_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1291 {
1292 xmlstr_t attr_name, attr_value, elem;
1293 BOOL end = FALSE, error, ret = TRUE;
1294 struct dll_redirect* dll;
1295
1296 if (!(dll = add_dll_redirect(assembly))) return FALSE;
1297
1298 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1299 {
1300 if (xmlstr_cmp(&attr_name, nameW))
1301 {
1302 if (!(dll->name = xmlstrdupW(&attr_value))) return FALSE;
1303 TRACE("name=%s\n", debugstr_xmlstr(&attr_value));
1304 }
1305 else if (xmlstr_cmp(&attr_name, hashW))
1306 {
1307 if (!(dll->hash = xmlstrdupW(&attr_value))) return FALSE;
1308 }
1309 else if (xmlstr_cmp(&attr_name, hashalgW))
1310 {
1311 static const WCHAR sha1W[] = {'S','H','A','1',0};
1312 if (!xmlstr_cmpi(&attr_value, sha1W))
1313 FIXME("hashalg should be SHA1, got %s\n", debugstr_xmlstr(&attr_value));
1314 }
1315 else
1316 {
1317 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1318 }
1319 }
1320
1321 if (error || !dll->name) return FALSE;
1322 if (end) return TRUE;
1323
1324 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1325 {
1326 if (xmlstr_cmp_end(&elem, fileW))
1327 {
1328 ret = parse_end_element(xmlbuf);
1329 break;
1330 }
1331 else if (xmlstr_cmp(&elem, comClassW))
1332 {
1333 ret = parse_com_class_elem(xmlbuf, dll);
1334 }
1335 else if (xmlstr_cmp(&elem, comInterfaceProxyStubW))
1336 {
1337 ret = parse_cominterface_proxy_stub_elem(xmlbuf, dll);
1338 }
1339 else if (xml_elem_cmp(&elem, hashW, asmv2W))
1340 {
1341 WARN("asmv2:hash (undocumented) not supported\n");
1342 ret = parse_unknown_elem(xmlbuf, &elem);
1343 }
1344 else if (xmlstr_cmp(&elem, typelibW))
1345 {
1346 ret = parse_typelib_elem(xmlbuf, dll);
1347 }
1348 else if (xmlstr_cmp(&elem, windowClassW))
1349 {
1350 ret = parse_window_class_elem(xmlbuf, dll);
1351 }
1352 else
1353 {
1354 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1355 ret = parse_unknown_elem( xmlbuf, &elem );
1356 }
1357 }
1358
1359 return ret;
1360 }
1361
1362 static BOOL parse_assembly_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl,
1363 struct assembly* assembly,
1364 struct assembly_identity* expected_ai)
1365 {
1366 xmlstr_t attr_name, attr_value, elem;
1367 BOOL end = FALSE, error, version = FALSE, xmlns = FALSE, ret = TRUE;
1368
1369 TRACE("(%p)\n", xmlbuf);
1370
1371 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1372 {
1373 if (xmlstr_cmp(&attr_name, manifestVersionW))
1374 {
1375 static const WCHAR v10W[] = {'1','.','',0};
1376 if (!xmlstr_cmp(&attr_value, v10W))
1377 {
1378 FIXME("wrong version %s\n", debugstr_xmlstr(&attr_value));
1379 return FALSE;
1380 }
1381 version = TRUE;
1382 }
1383 else if (xmlstr_cmp(&attr_name, xmlnsW))
1384 {
1385 if (!xmlstr_cmp(&attr_value, manifestv1W) && !xmlstr_cmp(&attr_value, manifestv3W))
1386 {
1387 FIXME("wrong namespace %s\n", debugstr_xmlstr(&attr_value));
1388 return FALSE;
1389 }
1390 xmlns = TRUE;
1391 }
1392 else
1393 {
1394 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1395 }
1396 }
1397
1398 if (error || end || !xmlns || !version) return FALSE;
1399 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
1400
1401 if (assembly->type == APPLICATION_MANIFEST && xmlstr_cmp(&elem, noInheritW))
1402 {
1403 if (!parse_noinherit_elem(xmlbuf) || !next_xml_elem(xmlbuf, &elem))
1404 return FALSE;
1405 assembly->no_inherit = TRUE;
1406 }
1407
1408 if (xml_elem_cmp(&elem, noInheritableW, asmv1W))
1409 {
1410 if (!parse_noinheritable_elem(xmlbuf) || !next_xml_elem(xmlbuf, &elem))
1411 return FALSE;
1412 }
1413 else if ((assembly->type == ASSEMBLY_MANIFEST || assembly->type == ASSEMBLY_SHARED_MANIFEST) &&
1414 assembly->no_inherit)
1415 return FALSE;
1416
1417 while (ret)
1418 {
1419 if (xml_elem_cmp_end(&elem, assemblyW, asmv1W))
1420 {
1421 ret = parse_end_element(xmlbuf);
1422 break;
1423 }
1424 else if (xml_elem_cmp(&elem, descriptionW, asmv1W))
1425 {
1426 ret = parse_description_elem(xmlbuf);
1427 }
1428 else if (xml_elem_cmp(&elem, comInterfaceExternalProxyStubW, asmv1W))
1429 {
1430 ret = parse_com_interface_external_proxy_stub_elem(xmlbuf, assembly);
1431 }
1432 else if (xml_elem_cmp(&elem, dependencyW, asmv1W))
1433 {
1434 ret = parse_dependency_elem(xmlbuf, acl);
1435 }
1436 else if (xml_elem_cmp(&elem, fileW, asmv1W))
1437 {
1438 ret = parse_file_elem(xmlbuf, assembly);
1439 }
1440 else if (xml_elem_cmp(&elem, clrClassW, asmv1W))
1441 {
1442 ret = parse_clr_class_elem(xmlbuf, assembly);
1443 }
1444 else if (xml_elem_cmp(&elem, clrSurrogateW, asmv1W))
1445 {
1446 ret = parse_clr_surrogate_elem(xmlbuf, assembly);
1447 }
1448 else if (xml_elem_cmp(&elem, assemblyIdentityW, asmv1W))
1449 {
1450 if (!parse_assembly_identity_elem(xmlbuf, acl->actctx, &assembly->id)) return FALSE;
1451
1452 if (expected_ai)
1453 {
1454 /* FIXME: more tests */
1455 if (assembly->type == ASSEMBLY_MANIFEST &&
1456 memcmp(&assembly->id.version, &expected_ai->version, sizeof(assembly->id.version)))
1457 {
1458 FIXME("wrong version for assembly manifest: %u.%u.%u.%u / %u.%u.%u.%u\n",
1459 expected_ai->version.major, expected_ai->version.minor,
1460 expected_ai->version.build, expected_ai->version.revision,
1461 assembly->id.version.major, assembly->id.version.minor,
1462 assembly->id.version.build, assembly->id.version.revision);
1463 ret = FALSE;
1464 }
1465 else if (assembly->type == ASSEMBLY_SHARED_MANIFEST &&
1466 (assembly->id.version.major != expected_ai->version.major ||
1467 assembly->id.version.minor != expected_ai->version.minor ||
1468 assembly->id.version.build < expected_ai->version.build ||
1469 (assembly->id.version.build == expected_ai->version.build &&
1470 assembly->id.version.revision < expected_ai->version.revision)))
1471 {
1472 FIXME("wrong version for shared assembly manifest\n");
1473 ret = FALSE;
1474 }
1475 }
1476 }
1477 else
1478 {
1479 WARN("unknown element %s\n", debugstr_xmlstr(&elem));
1480 ret = parse_unknown_elem(xmlbuf, &elem);
1481 }
1482 if (ret) ret = next_xml_elem(xmlbuf, &elem);
1483 }
1484
1485 return ret;
1486 }
1487
1488 static NTSTATUS parse_manifest_buffer( struct actctx_loader* acl, struct assembly *assembly,
1489 struct assembly_identity* ai, xmlbuf_t *xmlbuf )
1490 {
1491 xmlstr_t elem;
1492
1493 if (!next_xml_elem(xmlbuf, &elem)) return STATUS_SXS_CANT_GEN_ACTCTX;
1494
1495 if (xmlstr_cmp(&elem, xmlW) &&
1496 (!parse_xml_header(xmlbuf) || !next_xml_elem(xmlbuf, &elem)))
1497 return STATUS_SXS_CANT_GEN_ACTCTX;
1498
1499 if (!xml_elem_cmp(&elem, assemblyW, asmv1W))
1500 {
1501 FIXME("root element is %s, not <assembly>\n", debugstr_xmlstr(&elem));
1502 return STATUS_SXS_CANT_GEN_ACTCTX;
1503 }
1504
1505 if (!parse_assembly_elem(xmlbuf, acl, assembly, ai))
1506 {
1507 FIXME("failed to parse manifest %s\n", debugstr_w(assembly->manifest.info) );
1508 return STATUS_SXS_CANT_GEN_ACTCTX;
1509 }
1510
1511 if (next_xml_elem(xmlbuf, &elem))
1512 {
1513 FIXME("unexpected element %s\n", debugstr_xmlstr(&elem));
1514 return STATUS_SXS_CANT_GEN_ACTCTX;
1515 }
1516
1517 if (xmlbuf->ptr != xmlbuf->end)
1518 {
1519 FIXME("parse error\n");
1520 return STATUS_SXS_CANT_GEN_ACTCTX;
1521 }
1522 return STATUS_SUCCESS;
1523 }
1524
1525 static NTSTATUS parse_manifest( struct actctx_loader* acl, struct assembly_identity* ai,
1526 LPCWSTR filename, LPCWSTR directory, BOOL shared,
1527 const void *buffer, SIZE_T size )
1528 {
1529 xmlbuf_t xmlbuf;
1530 NTSTATUS status;
1531 struct assembly *assembly;
1532 int unicode_tests;
1533
1534 TRACE( "parsing manifest loaded from %s base dir %s\n", debugstr_w(filename), debugstr_w(directory) );
1535
1536 if (!(assembly = add_assembly(acl->actctx, shared ? ASSEMBLY_SHARED_MANIFEST : ASSEMBLY_MANIFEST)))
1537 return STATUS_SXS_CANT_GEN_ACTCTX;
1538
1539 if (directory && !(assembly->directory = strdupW(directory)))
1540 return STATUS_NO_MEMORY;
1541
1542 if (filename) assembly->manifest.info = strdupW( filename + 4 /* skip \??\ prefix */ );
1543 assembly->manifest.type = assembly->manifest.info ? ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE
1544 : ACTIVATION_CONTEXT_PATH_TYPE_NONE;
1545
1546 unicode_tests = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1547 if (RtlIsTextUnicode( buffer, size, &unicode_tests ))
1548 {
1549 xmlbuf.ptr = buffer;
1550 xmlbuf.end = xmlbuf.ptr + size / sizeof(WCHAR);
1551 status = parse_manifest_buffer( acl, assembly, ai, &xmlbuf );
1552 }
1553 else if (unicode_tests & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
1554 {
1555 const WCHAR *buf = buffer;
1556 WCHAR *new_buff;
1557 unsigned int i;
1558
1559 if (!(new_buff = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1560 return STATUS_NO_MEMORY;
1561 for (i = 0; i < size / sizeof(WCHAR); i++)
1562 new_buff[i] = RtlUshortByteSwap( buf[i] );
1563 xmlbuf.ptr = new_buff;
1564 xmlbuf.end = xmlbuf.ptr + size / sizeof(WCHAR);
1565 status = parse_manifest_buffer( acl, assembly, ai, &xmlbuf );
1566 RtlFreeHeap( GetProcessHeap(), 0, new_buff );
1567 }
1568 else
1569 {
1570 /* let's assume utf-8 for now */
1571 int len = wine_utf8_mbstowcs( 0, buffer, size, NULL, 0 );
1572 WCHAR *new_buff;
1573
1574 if (len == -1)
1575 {
1576 FIXME( "utf-8 conversion failed\n" );
1577 return STATUS_SXS_CANT_GEN_ACTCTX;
1578 }
1579 if (!(new_buff = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1580 return STATUS_NO_MEMORY;
1581 wine_utf8_mbstowcs( 0, buffer, size, new_buff, len );
1582 xmlbuf.ptr = new_buff;
1583 xmlbuf.end = xmlbuf.ptr + len;
1584 status = parse_manifest_buffer( acl, assembly, ai, &xmlbuf );
1585 RtlFreeHeap( GetProcessHeap(), 0, new_buff );
1586 }
1587 return status;
1588 }
1589
1590 static NTSTATUS open_nt_file( HANDLE *handle, UNICODE_STRING *name )
1591 {
1592 OBJECT_ATTRIBUTES attr;
1593 IO_STATUS_BLOCK io;
1594
1595 attr.Length = sizeof(attr);
1596 attr.RootDirectory = 0;
1597 attr.Attributes = OBJ_CASE_INSENSITIVE;
1598 attr.ObjectName = name;
1599 attr.SecurityDescriptor = NULL;
1600 attr.SecurityQualityOfService = NULL;
1601 return NtOpenFile( handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_ALERT );
1602 }
1603
1604 static NTSTATUS get_module_filename( HMODULE module, UNICODE_STRING *str, unsigned int extra_len )
1605 {
1606 NTSTATUS status;
1607 ULONG magic;
1608 LDR_MODULE *pldr;
1609
1610 LdrLockLoaderLock(0, NULL, &magic);
1611 status = LdrFindEntryForAddress( module, &pldr );
1612 if (status == STATUS_SUCCESS)
1613 {
1614 if ((str->Buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1615 pldr->FullDllName.Length + extra_len + sizeof(WCHAR) )))
1616 {
1617 memcpy( str->Buffer, pldr->FullDllName.Buffer, pldr->FullDllName.Length + sizeof(WCHAR) );
1618 str->Length = pldr->FullDllName.Length;
1619 str->MaximumLength = pldr->FullDllName.Length + extra_len + sizeof(WCHAR);
1620 }
1621 else status = STATUS_NO_MEMORY;
1622 }
1623 LdrUnlockLoaderLock(0, magic);
1624 return status;
1625 }
1626
1627 static NTSTATUS get_manifest_in_module( struct actctx_loader* acl, struct assembly_identity* ai,
1628 LPCWSTR filename, LPCWSTR directory, BOOL shared,
1629 HANDLE hModule, LPCWSTR resname, ULONG lang )
1630 {
1631 NTSTATUS status;
1632 UNICODE_STRING nameW;
1633 LDR_RESOURCE_INFO info;
1634 const IMAGE_RESOURCE_DATA_ENTRY* entry = NULL;
1635 void *ptr;
1636
1637 if (TRACE_ON(actctx))
1638 {
1639 if (!filename && !get_module_filename( hModule, &nameW, 0 ))
1640 {
1641 TRACE( "looking for res %s in module %p %s\n", debugstr_w(resname),
1642 hModule, debugstr_w(nameW.Buffer) );
1643 RtlFreeUnicodeString( &nameW );
1644 }
1645 else TRACE( "looking for res %s in module %p %s\n", debugstr_w(resname),
1646 hModule, debugstr_w(filename) );
1647 }
1648
1649 if (!resname) return STATUS_INVALID_PARAMETER;
1650
1651 info.Type = RT_MANIFEST;
1652 info.Language = lang;
1653 if (!((ULONG_PTR)resname >> 16))
1654 {
1655 info.Name = (ULONG_PTR)resname;
1656 status = LdrFindResource_U(hModule, &info, 3, &entry);
1657 }
1658 else if (resname[0] == '#')
1659 {
1660 ULONG value;
1661 RtlInitUnicodeString(&nameW, resname + 1);
1662 if (RtlUnicodeStringToInteger(&nameW, 10, &value) != STATUS_SUCCESS || HIWORD(value))
1663 return STATUS_INVALID_PARAMETER;
1664 info.Name = value;
1665 status = LdrFindResource_U(hModule, &info, 3, &entry);
1666 }
1667 else
1668 {
1669 RtlCreateUnicodeString(&nameW, resname);
1670 RtlUpcaseUnicodeString(&nameW, &nameW, FALSE);
1671 info.Name = (ULONG_PTR)nameW.Buffer;
1672 status = LdrFindResource_U(hModule, &info, 3, &entry);
1673 RtlFreeUnicodeString(&nameW);
1674 }
1675 if (status == STATUS_SUCCESS) status = LdrAccessResource(hModule, entry, &ptr, NULL);
1676
1677 if (status == STATUS_SUCCESS)
1678 status = parse_manifest(acl, ai, filename, directory, shared, ptr, entry->Size);
1679
1680 return status;
1681 }
1682
1683 static NTSTATUS get_manifest_in_pe_file( struct actctx_loader* acl, struct assembly_identity* ai,
1684 LPCWSTR filename, LPCWSTR directory, BOOL shared,
1685 HANDLE file, LPCWSTR resname, ULONG lang )
1686 {
1687 HANDLE mapping;
1688 OBJECT_ATTRIBUTES attr;
1689 LARGE_INTEGER size;
1690 LARGE_INTEGER offset;
1691 NTSTATUS status;
1692 SIZE_T count;
1693 void *base;
1694
1695 TRACE( "looking for res %s in %s\n", debugstr_w(resname), debugstr_w(filename) );
1696
1697 attr.Length = sizeof(attr);
1698 attr.RootDirectory = 0;
1699 attr.ObjectName = NULL;
1700 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1701 attr.SecurityDescriptor = NULL;
1702 attr.SecurityQualityOfService = NULL;
1703
1704 size.QuadPart = 0;
1705 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
1706 &attr, &size, PAGE_READONLY, SEC_COMMIT, file );
1707 if (status != STATUS_SUCCESS) return status;
1708
1709 offset.QuadPart = 0;
1710 count = 0;
1711 base = NULL;
1712 status = NtMapViewOfSection( mapping, GetCurrentProcess(), &base, 0, 0, &offset,
1713 &count, ViewShare, 0, PAGE_READONLY );
1714 NtClose( mapping );
1715 if (status != STATUS_SUCCESS) return status;
1716
1717 if (RtlImageNtHeader(base)) /* we got a PE file */
1718 {
1719 HANDLE module = (HMODULE)((ULONG_PTR)base | 1); /* make it a LOAD_LIBRARY_AS_DATAFILE handle */
1720 status = get_manifest_in_module( acl, ai, filename, directory, shared, module, resname, lang );
1721 }
1722 else status = STATUS_INVALID_IMAGE_FORMAT;
1723
1724 NtUnmapViewOfSection( GetCurrentProcess(), base );
1725 return status;
1726 }
1727
1728 static NTSTATUS get_manifest_in_manifest_file( struct actctx_loader* acl, struct assembly_identity* ai,
1729 LPCWSTR filename, LPCWSTR directory, BOOL shared, HANDLE file )
1730 {
1731 FILE_END_OF_FILE_INFORMATION info;
1732 IO_STATUS_BLOCK io;
1733 HANDLE mapping;
1734 OBJECT_ATTRIBUTES attr;
1735 LARGE_INTEGER size;
1736 LARGE_INTEGER offset;
1737 NTSTATUS status;
1738 SIZE_T count;
1739 void *base;
1740
1741 TRACE( "loading manifest file %s\n", debugstr_w(filename) );
1742
1743 attr.Length = sizeof(attr);
1744 attr.RootDirectory = 0;
1745 attr.ObjectName = NULL;
1746 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1747 attr.SecurityDescriptor = NULL;
1748 attr.SecurityQualityOfService = NULL;
1749
1750 size.QuadPart = 0;
1751 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
1752 &attr, &size, PAGE_READONLY, SEC_COMMIT, file );
1753 if (status != STATUS_SUCCESS) return status;
1754
1755 offset.QuadPart = 0;
1756 count = 0;
1757 base = NULL;
1758 status = NtMapViewOfSection( mapping, GetCurrentProcess(), &base, 0, 0, &offset,
1759 &count, ViewShare, 0, PAGE_READONLY );
1760 NtClose( mapping );
1761 if (status != STATUS_SUCCESS) return status;
1762
1763 status = NtQueryInformationFile( file, &io, &info, sizeof(info), FileEndOfFileInformation );
1764 if (status == STATUS_SUCCESS)
1765 status = parse_manifest(acl, ai, filename, directory, shared, base, info.EndOfFile.QuadPart);
1766
1767 NtUnmapViewOfSection( GetCurrentProcess(), base );
1768 return status;
1769 }
1770
1771 /* try to load the .manifest file associated to the file */
1772 static NTSTATUS get_manifest_in_associated_manifest( struct actctx_loader* acl, struct assembly_identity* ai,
1773 LPCWSTR filename, LPCWSTR directory, HMODULE module, LPCWSTR resname )
1774 {
1775 static const WCHAR fmtW[] = { '.','%','l','u',0 };
1776 WCHAR *buffer;
1777 NTSTATUS status;
1778 UNICODE_STRING nameW;
1779 HANDLE file;
1780 ULONG_PTR resid = CREATEPROCESS_MANIFEST_RESOURCE_ID;
1781
1782 if (!((ULONG_PTR)resname >> 16)) resid = (ULONG_PTR)resname & 0xffff;
1783
1784 TRACE( "looking for manifest associated with %s id %lu\n", debugstr_w(filename), resid );
1785
1786 if (module) /* use the module filename */
1787 {
1788 UNICODE_STRING name;
1789
1790 if (!(status = get_module_filename( module, &name, sizeof(dotManifestW) + 10*sizeof(WCHAR) )))
1791 {
1792 if (resid != 1) sprintfW( name.Buffer + strlenW(name.Buffer), fmtW, resid );
1793 strcatW( name.Buffer, dotManifestW );
1794 if (!RtlDosPathNameToNtPathName_U( name.Buffer, &nameW, NULL, NULL ))
1795 status = STATUS_RESOURCE_DATA_NOT_FOUND;
1796 RtlFreeUnicodeString( &name );
1797 }
1798 if (status) return status;
1799 }
1800 else
1801 {
1802 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1803 (strlenW(filename) + 10) * sizeof(WCHAR) + sizeof(dotManifestW) )))
1804 return STATUS_NO_MEMORY;
1805 strcpyW( buffer, filename );
1806 if (resid != 1) sprintfW( buffer + strlenW(buffer), fmtW, resid );
1807 strcatW( buffer, dotManifestW );
1808 RtlInitUnicodeString( &nameW, buffer );
1809 }
1810
1811 if (!open_nt_file( &file, &nameW ))
1812 {
1813 status = get_manifest_in_manifest_file( acl, ai, nameW.Buffer, directory, FALSE, file );
1814 NtClose( file );
1815 }
1816 else status = STATUS_RESOURCE_DATA_NOT_FOUND;
1817 RtlFreeUnicodeString( &nameW );
1818 return status;
1819 }
1820
1821 static WCHAR *lookup_manifest_file( HANDLE dir, struct assembly_identity *ai )
1822 {
1823 static const WCHAR lookup_fmtW[] =
1824 {'%','s','_','%','s','_','%','s','_','%','u','.','%','u','.','*','.','*','_',
1825 '%','s','_','*','.','m','a','n','i','f','e','s','t',0};
1826 static const WCHAR wine_trailerW[] = {'d','e','a','d','b','e','e','f','.','m','a','n','i','f','e','s','t'};
1827
1828 WCHAR *lookup, *ret = NULL;
1829 UNICODE_STRING lookup_us;
1830 IO_STATUS_BLOCK io;
1831 const WCHAR *lang = ai->language;
1832 unsigned int data_pos = 0, data_len;
1833 char buffer[8192];
1834
1835 if (!(lookup = RtlAllocateHeap( GetProcessHeap(), 0,
1836 (strlenW(ai->arch) + strlenW(ai->name)
1837 + strlenW(ai->public_key) + 20) * sizeof(WCHAR)
1838 + sizeof(lookup_fmtW) )))
1839 return NULL;
1840
1841 if (!lang || !strcmpiW( lang, neutralW )) lang = wildcardW;
1842 sprintfW( lookup, lookup_fmtW, ai->arch, ai->name, ai->public_key,
1843 ai->version.major, ai->version.minor, lang );
1844 RtlInitUnicodeString( &lookup_us, lookup );
1845
1846 NtQueryDirectoryFile( dir, 0, NULL, NULL, &io, buffer, sizeof(buffer),
1847 FileBothDirectoryInformation, FALSE, &lookup_us, TRUE );
1848 if (io.u.Status == STATUS_SUCCESS)
1849 {
1850 ULONG min_build = ai->version.build, min_revision = ai->version.revision;
1851 FILE_BOTH_DIR_INFORMATION *dir_info;
1852 WCHAR *tmp;
1853 ULONG build, revision;
1854
1855 data_len = io.Information;
1856
1857 for (;;)
1858 {
1859 if (data_pos >= data_len)
1860 {
1861 NtQueryDirectoryFile( dir, 0, NULL, NULL, &io, buffer, sizeof(buffer),
1862 FileBothDirectoryInformation, FALSE, &lookup_us, FALSE );
1863 if (io.u.Status != STATUS_SUCCESS) break;
1864 data_len = io.Information;
1865 data_pos = 0;
1866 }
1867 dir_info = (FILE_BOTH_DIR_INFORMATION*)(buffer + data_pos);
1868
1869 if (dir_info->NextEntryOffset) data_pos += dir_info->NextEntryOffset;
1870 else data_pos = data_len;
1871
1872 tmp = dir_info->FileName + (strchrW(lookup, '*') - lookup);
1873 build = atoiW(tmp);
1874 if (build < min_build) continue;
1875 tmp = strchrW(tmp, '.') + 1;
1876 revision = atoiW(tmp);
1877 if (build == min_build && revision < min_revision) continue;
1878 tmp = strchrW(tmp, '_') + 1;
1879 tmp = strchrW(tmp, '_') + 1;
1880 if (dir_info->FileNameLength - (tmp - dir_info->FileName) * sizeof(WCHAR) == sizeof(wine_trailerW) &&
1881 !memicmpW( tmp, wine_trailerW, sizeof(wine_trailerW) / sizeof(WCHAR) ))
1882 {
1883 /* prefer a non-Wine manifest if we already have one */
1884 /* we'll still load the builtin dll if specified through DllOverrides */
1885 if (ret) continue;
1886 }
1887 else
1888 {
1889 min_build = build;
1890 min_revision = revision;
1891 }
1892 ai->version.build = build;
1893 ai->version.revision = revision;
1894 RtlFreeHeap( GetProcessHeap(), 0, ret );
1895 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, dir_info->FileNameLength + sizeof(WCHAR) )))
1896 {
1897 memcpy( ret, dir_info->FileName, dir_info->FileNameLength );
1898 ret[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
1899 }
1900 }
1901 }
1902 else WARN("no matching file for %s\n", debugstr_w(lookup));
1903 RtlFreeHeap( GetProcessHeap(), 0, lookup );
1904 return ret;
1905 }
1906
1907 static NTSTATUS lookup_winsxs(struct actctx_loader* acl, struct assembly_identity* ai)
1908 {
1909 struct assembly_identity sxs_ai;
1910 UNICODE_STRING path_us;
1911 OBJECT_ATTRIBUTES attr;
1912 IO_STATUS_BLOCK io;
1913 WCHAR *path, *file = NULL;
1914 HANDLE handle;
1915
1916 static const WCHAR manifest_dirW[] =
1917 {'\\','w','i','n','s','x','s','\\','m','a','n','i','f','e','s','t','s',0};
1918
1919 if (!ai->arch || !ai->name || !ai->public_key) return STATUS_NO_SUCH_FILE;
1920
1921 if (!(path = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(manifest_dirW) +
1922 strlenW(user_shared_data->NtSystemRoot) * sizeof(WCHAR) )))
1923 return STATUS_NO_MEMORY;
1924
1925 strcpyW( path, user_shared_data->NtSystemRoot );
1926 memcpy( path + strlenW(path), manifest_dirW, sizeof(manifest_dirW) );
1927
1928 if (!RtlDosPathNameToNtPathName_U( path, &path_us, NULL, NULL ))
1929 {
1930 RtlFreeHeap( GetProcessHeap(), 0, path );
1931 return STATUS_NO_SUCH_FILE;
1932 }
1933 RtlFreeHeap( GetProcessHeap(), 0, path );
1934
1935 attr.Length = sizeof(attr);
1936 attr.RootDirectory = 0;
1937 attr.Attributes = OBJ_CASE_INSENSITIVE;
1938 attr.ObjectName = &path_us;
1939 attr.SecurityDescriptor = NULL;
1940 attr.SecurityQualityOfService = NULL;
1941
1942 if (!NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
1943 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT ))
1944 {
1945 sxs_ai = *ai;
1946 file = lookup_manifest_file( handle, &sxs_ai );
1947 NtClose( handle );
1948 }
1949 if (!file)
1950 {
1951 RtlFreeUnicodeString( &path_us );
1952 return STATUS_NO_SUCH_FILE;
1953 }
1954
1955 /* append file name to directory path */
1956 if (!(path = RtlReAllocateHeap( GetProcessHeap(), 0, path_us.Buffer,
1957 path_us.Length + (strlenW(file) + 2) * sizeof(WCHAR) )))
1958 {
1959 RtlFreeHeap( GetProcessHeap(), 0, file );
1960 RtlFreeUnicodeString( &path_us );
1961 return STATUS_NO_MEMORY;
1962 }
1963
1964 path[path_us.Length/sizeof(WCHAR)] = '\\';
1965 strcpyW( path + path_us.Length/sizeof(WCHAR) + 1, file );
1966 RtlInitUnicodeString( &path_us, path );
1967 *strrchrW(file, '.') = 0; /* remove .manifest extension */
1968
1969 if (!open_nt_file( &handle, &path_us ))
1970 {
1971 io.u.Status = get_manifest_in_manifest_file(acl, &sxs_ai, path_us.Buffer, file, TRUE, handle);
1972 NtClose( handle );
1973 }
1974 else io.u.Status = STATUS_NO_SUCH_FILE;
1975
1976 RtlFreeHeap( GetProcessHeap(), 0, file );
1977 RtlFreeUnicodeString( &path_us );
1978 return io.u.Status;
1979 }
1980
1981 static NTSTATUS lookup_assembly(struct actctx_loader* acl,
1982 struct assembly_identity* ai)
1983 {
1984 static const WCHAR dotDllW[] = {'.','d','l','l',0};
1985 unsigned int i;
1986 WCHAR *buffer, *p, *directory;
1987 NTSTATUS status;
1988 UNICODE_STRING nameW;
1989 HANDLE file;
1990
1991 TRACE( "looking for name=%s version=%s arch=%s\n",
1992 debugstr_w(ai->name), debugstr_version(&ai->version), debugstr_w(ai->arch) );
1993
1994 if ((status = lookup_winsxs(acl, ai)) != STATUS_NO_SUCH_FILE) return status;
1995
1996 /* FIXME: add support for language specific lookup */
1997
1998 nameW.Buffer = NULL;
1999 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0,
2000 (strlenW(acl->actctx->appdir.info) + 2 * strlenW(ai->name) + 2) * sizeof(WCHAR) + sizeof(dotManifestW) )))
2001 return STATUS_NO_MEMORY;
2002
2003 if (!(directory = build_assembly_dir( ai )))
2004 {
2005 RtlFreeHeap( GetProcessHeap(), 0, buffer );
2006 return STATUS_NO_MEMORY;
2007 }
2008
2009 /* lookup in appdir\name.dll
2010 * appdir\name.manifest
2011 * appdir\name\name.dll
2012 * appdir\name\name.manifest
2013 */
2014 strcpyW( buffer, acl->actctx->appdir.info );
2015 p = buffer + strlenW(buffer);
2016 for (i = 0; i < 2; i++)
2017 {
2018 *p++ = '\\';
2019 strcpyW( p, ai->name );
2020 p += strlenW(p);
2021
2022 strcpyW( p, dotDllW );
2023 if (RtlDosPathNameToNtPathName_U( buffer, &nameW, NULL, NULL ))
2024 {
2025 status = open_nt_file( &file, &nameW );
2026 if (!status)
2027 {
2028 status = get_manifest_in_pe_file( acl, ai, nameW.Buffer, directory, FALSE, file,
2029 (LPCWSTR)CREATEPROCESS_MANIFEST_RESOURCE_ID, 0 );
2030 NtClose( file );
2031 break;
2032 }
2033 RtlFreeUnicodeString( &nameW );
2034 }
2035
2036 strcpyW( p, dotManifestW );
2037 if (RtlDosPathNameToNtPathName_U( buffer, &nameW, NULL, NULL ))
2038 {
2039 status = open_nt_file( &file, &nameW );
2040 if (!status)
2041 {
2042 status = get_manifest_in_manifest_file( acl, ai, nameW.Buffer, directory, FALSE, file );
2043 NtClose( file );
2044 break;
2045 }
2046 RtlFreeUnicodeString( &nameW );
2047 }
2048 status = STATUS_SXS_ASSEMBLY_NOT_FOUND;
2049 }
2050 RtlFreeUnicodeString( &nameW );
2051 RtlFreeHeap( GetProcessHeap(), 0, directory );
2052 RtlFreeHeap( GetProcessHeap(), 0, buffer );
2053 return status;
2054 }
2055
2056 static NTSTATUS parse_depend_manifests(struct actctx_loader* acl)
2057 {
2058 NTSTATUS status = STATUS_SUCCESS;
2059 unsigned int i;
2060
2061 for (i = 0; i < acl->num_dependencies; i++)
2062 {
2063 if (lookup_assembly(acl, &acl->dependencies[i]) != STATUS_SUCCESS)
2064 {
2065 if (!acl->dependencies[i].optional)
2066 {
2067 FIXME( "Could not find dependent assembly %s (%s)\n",
2068 debugstr_w(acl->dependencies[i].name),
2069 debugstr_version(&acl->dependencies[i].version) );
2070 status = STATUS_SXS_CANT_GEN_ACTCTX;
2071 break;
2072 }
2073 }
2074 }
2075 /* FIXME should now iterate through all refs */
2076 return status;
2077 }
2078
2079 /* find the appropriate activation context for RtlQueryInformationActivationContext */
2080 static NTSTATUS find_query_actctx( HANDLE *handle, DWORD flags, ULONG class )
2081 {
2082 NTSTATUS status = STATUS_SUCCESS;
2083
2084 if (flags & QUERY_ACTCTX_FLAG_USE_ACTIVE_ACTCTX)
2085 {
2086 if (*handle) return STATUS_INVALID_PARAMETER;
2087
2088 if (NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2089 *handle = NtCurrentTeb()->ActivationContextStack.ActiveFrame->ActivationContext;
2090 }
2091 else if (flags & (QUERY_ACTCTX_FLAG_ACTCTX_IS_ADDRESS|QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE))
2092 {
2093 ULONG magic;
2094 LDR_MODULE *pldr;
2095
2096 if (!*handle) return STATUS_INVALID_PARAMETER;
2097
2098 LdrLockLoaderLock( 0, NULL, &magic );
2099 if (!LdrFindEntryForAddress( *handle, &pldr ))
2100 {
2101 if ((flags & QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE) && *handle != pldr->BaseAddress)
2102 status = STATUS_DLL_NOT_FOUND;
2103 else
2104 *handle = pldr->ActivationContext;
2105 }
2106 else status = STATUS_DLL_NOT_FOUND;
2107 LdrUnlockLoaderLock( 0, magic );
2108 }
2109 else if (!*handle && (class != ActivationContextBasicInformation))
2110 *handle = process_actctx;
2111
2112 return status;
2113 }
2114
2115 static NTSTATUS fill_keyed_data(PACTCTX_SECTION_KEYED_DATA data, PVOID v1, PVOID v2, unsigned int i)
2116 {
2117 data->ulDataFormatVersion = 1;
2118 data->lpData = v1;
2119 data->ulLength = 20; /* FIXME */
2120 data->lpSectionGlobalData = NULL; /* FIXME */
2121 data->ulSectionGlobalDataLength = 0; /* FIXME */
2122 data->lpSectionBase = v2;
2123 data->ulSectionTotalLength = 0; /* FIXME */
2124 data->hActCtx = NULL;
2125 if (data->cbSize >= offsetof(ACTCTX_SECTION_KEYED_DATA, ulAssemblyRosterIndex) + sizeof(ULONG))
2126 data->ulAssemblyRosterIndex = i + 1;
2127
2128 return STATUS_SUCCESS;
2129 }
2130
2131 static NTSTATUS find_dll_redirection(ACTIVATION_CONTEXT* actctx, const UNICODE_STRING *section_name,
2132 PACTCTX_SECTION_KEYED_DATA data)
2133 {
2134 unsigned int i, j, snlen = section_name->Length / sizeof(WCHAR);
2135
2136 for (i = 0; i < actctx->num_assemblies; i++)
2137 {
2138 struct assembly *assembly = &actctx->assemblies[i];
2139 for (j = 0; j < assembly->num_dlls; j++)
2140 {
2141 struct dll_redirect *dll = &assembly->dlls[j];
2142 if (!strncmpiW(section_name->Buffer, dll->name, snlen) && !dll->name[snlen])
2143 return fill_keyed_data(data, dll, assembly, i);
2144 }
2145 }
2146 return STATUS_SXS_KEY_NOT_FOUND;
2147 }
2148
2149 static NTSTATUS find_window_class(ACTIVATION_CONTEXT* actctx, const UNICODE_STRING *section_name,
2150 PACTCTX_SECTION_KEYED_DATA data)
2151 {
2152 unsigned int i, j, k, snlen = section_name->Length / sizeof(WCHAR);
2153
2154 for (i = 0; i < actctx->num_assemblies; i++)
2155 {
2156 struct assembly *assembly = &actctx->assemblies[i];
2157 for (j = 0; j < assembly->num_dlls; j++)
2158 {
2159 struct dll_redirect *dll = &assembly->dlls[j];
2160 for (k = 0; k < dll->entities.num; k++)
2161 {
2162 struct entity *entity = &dll->entities.base[k];
2163 if (entity->kind == ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION)
2164 {
2165 if (!strncmpiW(section_name->Buffer, entity->u.class.name, snlen) && !entity->u.class.name[snlen])
2166 return fill_keyed_data(data, entity, dll, i);
2167 }
2168 }
2169 }
2170 }
2171 return STATUS_SXS_KEY_NOT_FOUND;
2172 }
2173
2174 static NTSTATUS find_string(ACTIVATION_CONTEXT* actctx, ULONG section_kind,
2175 const UNICODE_STRING *section_name,
2176 DWORD flags, PACTCTX_SECTION_KEYED_DATA data)
2177 {
2178 NTSTATUS status;
2179
2180 switch (section_kind)
2181 {
2182 case ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION:
2183 status = find_dll_redirection(actctx, section_name, data);
2184 break;
2185 case ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION:
2186 status = find_window_class(actctx, section_name, data);
2187 break;
2188 case ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION:
2189 case ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION:
2190 case ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION:
2191 case ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION:
2192 case ACTIVATION_CONTEXT_SECTION_GLOBAL_OBJECT_RENAME_TABLE:
2193 case ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES:
2194 FIXME("Unsupported yet section_kind %x\n", section_kind);
2195 return STATUS_SXS_SECTION_NOT_FOUND;
2196 default:
2197 WARN("Unknown section_kind %x\n", section_kind);
2198 return STATUS_SXS_SECTION_NOT_FOUND;
2199 }
2200
2201 if (status != STATUS_SUCCESS) return status;
2202
2203 if (flags & FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX)
2204 {
2205 actctx_addref(actctx);
2206 data->hActCtx = actctx;
2207 }
2208 return STATUS_SUCCESS;
2209 }
2210
2211 /* initialize the activation context for the current process */
2212 void actctx_init(void)
2213 {
2214 ACTCTXW ctx;
2215 HANDLE handle;
2216
2217 ctx.cbSize = sizeof(ctx);
2218 ctx.lpSource = NULL;
2219 ctx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID | ACTCTX_FLAG_HMODULE_VALID;
2220 ctx.hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
2221 ctx.lpResourceName = (LPCWSTR)CREATEPROCESS_MANIFEST_RESOURCE_ID;
2222
2223 if (!RtlCreateActivationContext( &handle, &ctx )) process_actctx = check_actctx(handle);
2224 }
2225
2226
2227 /***********************************************************************
2228 * RtlCreateActivationContext (NTDLL.@)
2229 *
2230 * Create an activation context.
2231 *
2232 * FIXME: function signature/prototype is wrong
2233 */
2234 NTSTATUS WINAPI RtlCreateActivationContext( HANDLE *handle, const void *ptr )
2235 {
2236 const ACTCTXW *pActCtx = ptr; /* FIXME: not the right structure */
2237 const WCHAR *directory = NULL;
2238 ACTIVATION_CONTEXT *actctx;
2239 UNICODE_STRING nameW;
2240 ULONG lang = 0;
2241 NTSTATUS status = STATUS_NO_MEMORY;
2242 HANDLE file = 0;
2243 struct actctx_loader acl;
2244
2245 TRACE("%p %08x\n", pActCtx, pActCtx ? pActCtx->dwFlags : 0);
2246
2247 if (!pActCtx || pActCtx->cbSize < sizeof(*pActCtx) ||
2248 (pActCtx->dwFlags & ~ACTCTX_FLAGS_ALL))
2249 return STATUS_INVALID_PARAMETER;
2250
2251 if (!(actctx = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*actctx) )))
2252 return STATUS_NO_MEMORY;
2253
2254 actctx->magic = ACTCTX_MAGIC;
2255 actctx->ref_count = 1;
2256 actctx->config.type = ACTIVATION_CONTEXT_PATH_TYPE_NONE;
2257 actctx->config.info = NULL;
2258 actctx->appdir.type = ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE;
2259 if (pActCtx->dwFlags & ACTCTX_FLAG_APPLICATION_NAME_VALID)
2260 {
2261 if (!(actctx->appdir.info = strdupW( pActCtx->lpApplicationName ))) goto error;
2262 }
2263 else
2264 {
2265 UNICODE_STRING dir;
2266 WCHAR *p;
2267 HMODULE module;
2268
2269 if (pActCtx->dwFlags & ACTCTX_FLAG_HMODULE_VALID) module = pActCtx->hModule;
2270 else module = NtCurrentTeb()->Peb->ImageBaseAddress;
2271
2272 if ((status = get_module_filename( module, &dir, 0 ))) goto error;
2273 if ((p = strrchrW( dir.Buffer, '\\' ))) p[1] = 0;
2274 actctx->appdir.info = dir.Buffer;
2275 }
2276
2277 nameW.Buffer = NULL;
2278 if (pActCtx->lpSource)
2279 {
2280 if (!RtlDosPathNameToNtPathName_U(pActCtx->lpSource, &nameW, NULL, NULL))
2281 {
2282 status = STATUS_NO_SUCH_FILE;
2283 goto error;
2284 }
2285 status = open_nt_file( &file, &nameW );
2286 if (status)
2287 {
2288 RtlFreeUnicodeString( &nameW );
2289 goto error;
2290 }
2291 }
2292
2293 acl.actctx = actctx;
2294 acl.dependencies = NULL;
2295 acl.num_dependencies = 0;
2296 acl.allocated_dependencies = 0;
2297
2298 if (pActCtx->dwFlags & ACTCTX_FLAG_LANGID_VALID) lang = pActCtx->wLangId;
2299 if (pActCtx->dwFlags & ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID) directory = pActCtx->lpAssemblyDirectory;
2300
2301 if (pActCtx->dwFlags & ACTCTX_FLAG_RESOURCE_NAME_VALID)
2302 {
2303 /* if we have a resource it's a PE file */
2304 if (pActCtx->dwFlags & ACTCTX_FLAG_HMODULE_VALID)
2305 {
2306 status = get_manifest_in_module( &acl, NULL, NULL, directory, FALSE, pActCtx->hModule,
2307 pActCtx->lpResourceName, lang );
2308 if (status && status != STATUS_SXS_CANT_GEN_ACTCTX)
2309 /* FIXME: what to do if pActCtx->lpSource is set */
2310 status = get_manifest_in_associated_manifest( &acl, NULL, NULL, directory,
2311 pActCtx->hModule, pActCtx->lpResourceName );
2312 }
2313 else if (pActCtx->lpSource)
2314 {
2315 status = get_manifest_in_pe_file( &acl, NULL, nameW.Buffer, directory, FALSE,
2316 file, pActCtx->lpResourceName, lang );
2317 if (status && status != STATUS_SXS_CANT_GEN_ACTCTX)
2318 status = get_manifest_in_associated_manifest( &acl, NULL, nameW.Buffer, directory,
2319 NULL, pActCtx->lpResourceName );
2320 }
2321 else status = STATUS_INVALID_PARAMETER;
2322 }
2323 else
2324 {
2325 status = get_manifest_in_manifest_file( &acl, NULL, nameW.Buffer, directory, FALSE, file );
2326 }
2327
2328 if (file) NtClose( file );
2329 RtlFreeUnicodeString( &nameW );
2330
2331 if (status == STATUS_SUCCESS) status = parse_depend_manifests(&acl);
2332 free_depend_manifests( &acl );
2333
2334 if (status == STATUS_SUCCESS) *handle = actctx;
2335 else actctx_release( actctx );
2336 return status;
2337
2338 error:
2339 if (file) NtClose( file );
2340 actctx_release( actctx );
2341 return status;
2342 }
2343
2344
2345 /***********************************************************************
2346 * RtlAddRefActivationContext (NTDLL.@)
2347 */
2348 void WINAPI RtlAddRefActivationContext( HANDLE handle )
2349 {
2350 ACTIVATION_CONTEXT *actctx;
2351
2352 if ((actctx = check_actctx( handle ))) actctx_addref( actctx );
2353 }
2354
2355
2356 /******************************************************************
2357 * RtlReleaseActivationContext (NTDLL.@)
2358 */
2359 void WINAPI RtlReleaseActivationContext( HANDLE handle )
2360 {
2361 ACTIVATION_CONTEXT *actctx;
2362
2363 if ((actctx = check_actctx( handle ))) actctx_release( actctx );
2364 }
2365
2366
2367 /******************************************************************
2368 * RtlActivateActivationContext (NTDLL.@)
2369 */
2370 NTSTATUS WINAPI RtlActivateActivationContext( ULONG unknown, HANDLE handle, PULONG_PTR cookie )
2371 {
2372 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2373
2374 if (!(frame = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*frame) )))
2375 return STATUS_NO_MEMORY;
2376
2377 frame->Previous = NtCurrentTeb()->ActivationContextStack.ActiveFrame;
2378 frame->ActivationContext = handle;
2379 frame->Flags = 0;
2380 NtCurrentTeb()->ActivationContextStack.ActiveFrame = frame;
2381 RtlAddRefActivationContext( handle );
2382
2383 *cookie = (ULONG_PTR)frame;
2384 TRACE( "%p cookie=%lx\n", handle, *cookie );
2385 return STATUS_SUCCESS;
2386 }
2387
2388
2389 /***********************************************************************
2390 * RtlDeactivateActivationContext (NTDLL.@)
2391 */
2392 void WINAPI RtlDeactivateActivationContext( ULONG flags, ULONG_PTR cookie )
2393 {
2394 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame, *top;
2395
2396 TRACE( "%x cookie=%lx\n", flags, cookie );
2397
2398 /* find the right frame */
2399 top = NtCurrentTeb()->ActivationContextStack.ActiveFrame;
2400 for (frame = top; frame; frame = frame->Previous)
2401 if ((ULONG_PTR)frame == cookie) break;
2402
2403 if (!frame)
2404 RtlRaiseStatus( STATUS_SXS_INVALID_DEACTIVATION );
2405
2406 if (frame != top && !(flags & DEACTIVATE_ACTCTX_FLAG_FORCE_EARLY_DEACTIVATION))
2407 RtlRaiseStatus( STATUS_SXS_EARLY_DEACTIVATION );
2408
2409 /* pop everything up to and including frame */
2410 NtCurrentTeb()->ActivationContextStack.ActiveFrame = frame->Previous;
2411
2412 while (top != NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2413 {
2414 frame = top->Previous;
2415 RtlReleaseActivationContext( top->ActivationContext );
2416 RtlFreeHeap( GetProcessHeap(), 0, top );
2417 top = frame;
2418 }
2419 }
2420
2421
2422 /******************************************************************
2423 * RtlFreeThreadActivationContextStack (NTDLL.@)
2424 */
2425 void WINAPI RtlFreeThreadActivationContextStack(void)
2426 {
2427 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2428
2429 frame = NtCurrentTeb()->ActivationContextStack.ActiveFrame;
2430 while (frame)
2431 {
2432 RTL_ACTIVATION_CONTEXT_STACK_FRAME *prev = frame->Previous;
2433 RtlReleaseActivationContext( frame->ActivationContext );
2434 RtlFreeHeap( GetProcessHeap(), 0, frame );
2435 frame = prev;
2436 }
2437 NtCurrentTeb()->ActivationContextStack.ActiveFrame = NULL;
2438 }
2439
2440
2441 /******************************************************************
2442 * RtlGetActiveActivationContext (NTDLL.@)
2443 */
2444 NTSTATUS WINAPI RtlGetActiveActivationContext( HANDLE *handle )
2445 {
2446 if (NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2447 {
2448 *handle = NtCurrentTeb()->ActivationContextStack.ActiveFrame->ActivationContext;
2449 RtlAddRefActivationContext( *handle );
2450 }
2451 else
2452 *handle = 0;
2453
2454 return STATUS_SUCCESS;
2455 }
2456
2457
2458 /******************************************************************
2459 * RtlIsActivationContextActive (NTDLL.@)
2460 */
2461 BOOLEAN WINAPI RtlIsActivationContextActive( HANDLE handle )
2462 {
2463 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2464
2465 for (frame = NtCurrentTeb()->ActivationContextStack.ActiveFrame; frame; frame = frame->Previous)
2466 if (frame->ActivationContext == handle) return TRUE;
2467 return FALSE;
2468 }
2469
2470
2471 /***********************************************************************
2472 * RtlQueryInformationActivationContext (NTDLL.@)
2473 *
2474 * Get information about an activation context.
2475 * FIXME: function signature/prototype may be wrong
2476 */
2477 NTSTATUS WINAPI RtlQueryInformationActivationContext( ULONG flags, HANDLE handle, PVOID subinst,
2478 ULONG class, PVOID buffer,
2479 SIZE_T bufsize, SIZE_T *retlen )
2480 {
2481 ACTIVATION_CONTEXT *actctx;
2482 NTSTATUS status;
2483
2484 TRACE("%08x %p %p %u %p %ld %p\n", flags, handle,
2485 subinst, class, buffer, bufsize, retlen);
2486
2487 if (retlen) *retlen = 0;
2488 if ((status = find_query_actctx( &handle, flags, class ))) return status;
2489
2490 switch (class)
2491 {
2492 case ActivationContextBasicInformation:
2493 {
2494 ACTIVATION_CONTEXT_BASIC_INFORMATION *info = buffer;
2495
2496 if (retlen) *retlen = sizeof(*info);
2497 if (!info || bufsize < sizeof(*info)) return STATUS_BUFFER_TOO_SMALL;
2498
2499 info->hActCtx = handle;
2500 info->dwFlags = 0; /* FIXME */
2501 if (!(flags & QUERY_ACTCTX_FLAG_NO_ADDREF)) RtlAddRefActivationContext( handle );
2502 }
2503 break;
2504
2505 case ActivationContextDetailedInformation:
2506 {
2507 ACTIVATION_CONTEXT_DETAILED_INFORMATION *acdi = buffer;
2508 struct assembly *assembly = NULL;
2509 SIZE_T len, manifest_len = 0, config_len = 0, appdir_len = 0;
2510 LPWSTR ptr;
2511
2512 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2513
2514 if (actctx->num_assemblies) assembly = actctx->assemblies;
2515
2516 if (assembly && assembly->manifest.info)
2517 manifest_len = strlenW(assembly->manifest.info) + 1;
2518 if (actctx->config.info) config_len = strlenW(actctx->config.info) + 1;
2519 if (actctx->appdir.info) appdir_len = strlenW(actctx->appdir.info) + 1;
2520 len = sizeof(*acdi) + (manifest_len + config_len + appdir_len) * sizeof(WCHAR);
2521
2522 if (retlen) *retlen = len;
2523 if (!buffer || bufsize < len) return STATUS_BUFFER_TOO_SMALL;
2524
2525 acdi->dwFlags = 0;
2526 acdi->ulFormatVersion = assembly ? 1 : 0; /* FIXME */
2527 acdi->ulAssemblyCount = actctx->num_assemblies;
2528 acdi->ulRootManifestPathType = assembly ? assembly->manifest.type : 0 /* FIXME */;
2529 acdi->ulRootManifestPathChars = assembly && assembly->manifest.info ? manifest_len - 1 : 0;
2530 acdi->ulRootConfigurationPathType = actctx->config.type;
2531 acdi->ulRootConfigurationPathChars = actctx->config.info ? config_len - 1 : 0;
2532 acdi->ulAppDirPathType = actctx->appdir.type;
2533 acdi->ulAppDirPathChars = actctx->appdir.info ? appdir_len - 1 : 0;
2534 ptr = (LPWSTR)(acdi + 1);
2535 if (manifest_len)
2536 {
2537 acdi->lpRootManifestPath = ptr;
2538 memcpy(ptr, assembly->manifest.info, manifest_len * sizeof(WCHAR));
2539 ptr += manifest_len;
2540 }
2541 else acdi->lpRootManifestPath = NULL;
2542 if (config_len)
2543 {
2544 acdi->lpRootConfigurationPath = ptr;
2545 memcpy(ptr, actctx->config.info, config_len * sizeof(WCHAR));
2546 ptr += config_len;
2547 }
2548 else acdi->lpRootConfigurationPath = NULL;
2549 if (appdir_len)
2550 {
2551 acdi->lpAppDirPath = ptr;
2552 memcpy(ptr, actctx->appdir.info, appdir_len * sizeof(WCHAR));
2553 }
2554 else acdi->lpAppDirPath = NULL;
2555 }
2556 break;
2557
2558 case AssemblyDetailedInformationInActivationContext:
2559 {
2560 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION *afdi = buffer;
2561 struct assembly *assembly;
2562 WCHAR *assembly_id;
2563 DWORD index;
2564 SIZE_T len, id_len = 0, ad_len = 0, path_len = 0;
2565 LPWSTR ptr;
2566
2567 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2568 if (!subinst) return STATUS_INVALID_PARAMETER;
2569
2570 index = *(DWORD*)subinst;
2571 if (!index || index > actctx->num_assemblies) return STATUS_INVALID_PARAMETER;
2572
2573 assembly = &actctx->assemblies[index - 1];
2574
2575 if (!(assembly_id = build_assembly_id( &assembly->id ))) return STATUS_NO_MEMORY;
2576 id_len = strlenW(assembly_id) + 1;
2577 if (assembly->directory) ad_len = strlenW(assembly->directory) + 1;
2578
2579 if (assembly->manifest.info &&
2580 (assembly->type == ASSEMBLY_MANIFEST || assembly->type == ASSEMBLY_SHARED_MANIFEST))
2581 path_len = strlenW(assembly->manifest.info) + 1;
2582
2583 len = sizeof(*afdi) + (id_len + ad_len + path_len) * sizeof(WCHAR);
2584
2585 if (retlen) *retlen = len;
2586 if (!buffer || bufsize < len)
2587 {
2588 RtlFreeHeap( GetProcessHeap(), 0, assembly_id );
2589 return STATUS_BUFFER_TOO_SMALL;
2590 }
2591
2592 afdi->ulFlags = 0; /* FIXME */
2593 afdi->ulEncodedAssemblyIdentityLength = (id_len - 1) * sizeof(WCHAR);
2594 afdi->ulManifestPathType = assembly->manifest.type;
2595 afdi->ulManifestPathLength = assembly->manifest.info ? (path_len - 1) * sizeof(WCHAR) : 0;
2596 /* FIXME afdi->liManifestLastWriteTime = 0; */
2597 afdi->ulPolicyPathType = ACTIVATION_CONTEXT_PATH_TYPE_NONE; /* FIXME */
2598 afdi->ulPolicyPathLength = 0;
2599 /* FIXME afdi->liPolicyLastWriteTime = 0; */
2600 afdi->ulMetadataSatelliteRosterIndex = 0; /* FIXME */
2601 afdi->ulManifestVersionMajor = 1;
2602 afdi->ulManifestVersionMinor = 0;
2603 afdi->ulPolicyVersionMajor = 0; /* FIXME */
2604 afdi->ulPolicyVersionMinor = 0; /* FIXME */
2605 afdi->ulAssemblyDirectoryNameLength = ad_len ? (ad_len - 1) * sizeof(WCHAR) : 0;
2606 ptr = (LPWSTR)(afdi + 1);
2607 afdi->lpAssemblyEncodedAssemblyIdentity = ptr;
2608 memcpy( ptr, assembly_id, id_len * sizeof(WCHAR) );
2609 ptr += id_len;
2610 if (path_len)
2611 {
2612 afdi->lpAssemblyManifestPath = ptr;
2613 memcpy(ptr, assembly->manifest.info, path_len * sizeof(WCHAR));
2614 ptr += path_len;
2615 } else afdi->lpAssemblyManifestPath = NULL;
2616 afdi->lpAssemblyPolicyPath = NULL; /* FIXME */
2617 if (ad_len)
2618 {
2619 afdi->lpAssemblyDirectoryName = ptr;
2620 memcpy(ptr, assembly->directory, ad_len * sizeof(WCHAR));
2621 }
2622 else afdi->lpAssemblyDirectoryName = NULL;
2623 RtlFreeHeap( GetProcessHeap(), 0, assembly_id );
2624 }
2625 break;
2626
2627 case FileInformationInAssemblyOfAssemblyInActivationContext:
2628 {
2629 const ACTIVATION_CONTEXT_QUERY_INDEX *acqi = subinst;
2630 ASSEMBLY_FILE_DETAILED_INFORMATION *afdi = buffer;
2631 struct assembly *assembly;
2632 struct dll_redirect *dll;
2633 SIZE_T len, dll_len = 0;
2634 LPWSTR ptr;
2635
2636 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2637 if (!acqi) return STATUS_INVALID_PARAMETER;
2638
2639 if (acqi->ulAssemblyIndex >= actctx->num_assemblies)
2640 return STATUS_INVALID_PARAMETER;
2641 assembly = &actctx->assemblies[acqi->ulAssemblyIndex];
2642
2643 if (acqi->ulFileIndexInAssembly >= assembly->num_dlls)
2644 return STATUS_INVALID_PARAMETER;
2645 dll = &assembly->dlls[acqi->ulFileIndexInAssembly];
2646
2647 if (dll->name) dll_len = strlenW(dll->name) + 1;
2648 len = sizeof(*afdi) + dll_len * sizeof(WCHAR);
2649
2650 if (!buffer || bufsize < len)
2651 {
2652 if (retlen) *retlen = len;
2653 return STATUS_BUFFER_TOO_SMALL;
2654 }
2655 if (retlen) *retlen = 0; /* yes that's what native does !! */
2656 afdi->ulFlags = ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION;
2657 afdi->ulFilenameLength = dll_len ? (dll_len - 1) * sizeof(WCHAR) : 0;
2658 afdi->ulPathLength = 0; /* FIXME */
2659 ptr = (LPWSTR)(afdi + 1);
2660 if (dll_len)
2661 {
2662 afdi->lpFileName = ptr;
2663 memcpy( ptr, dll->name, dll_len * sizeof(WCHAR) );
2664 } else afdi->lpFileName = NULL;
2665 afdi->lpFilePath = NULL; /* FIXME */
2666 }
2667 break;
2668
2669 default:
2670 FIXME( "class %u not implemented\n", class );
2671 return STATUS_NOT_IMPLEMENTED;
2672 }
2673 return STATUS_SUCCESS;
2674 }
2675
2676 /***********************************************************************
2677 * RtlFindActivationContextSectionString (NTDLL.@)
2678 *
2679 * Find information about a string in an activation context.
2680 * FIXME: function signature/prototype may be wrong
2681 */
2682 NTSTATUS WINAPI RtlFindActivationContextSectionString( ULONG flags, const GUID *guid, ULONG section_kind,
2683 const UNICODE_STRING *section_name, PVOID ptr )
2684 {
2685 PACTCTX_SECTION_KEYED_DATA data = ptr;
2686 NTSTATUS status = STATUS_SXS_KEY_NOT_FOUND;
2687
2688 TRACE("%08x %s %u %s %p\n", flags, debugstr_guid(guid), section_kind,
2689 debugstr_us(section_name), data);
2690
2691 if (guid)
2692 {
2693 FIXME("expected guid == NULL\n");
2694 return STATUS_INVALID_PARAMETER;
2695 }
2696 if (flags & ~FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX)
2697 {
2698 FIXME("unknown flags %08x\n", flags);
2699 return STATUS_INVALID_PARAMETER;
2700 }
2701 if (!data || data->cbSize < offsetof(ACTCTX_SECTION_KEYED_DATA, ulAssemblyRosterIndex) ||
2702 !section_name || !section_name->Buffer)
2703 {
2704 WARN("invalid parameter\n");
2705 return STATUS_INVALID_PARAMETER;
2706 }
2707
2708 if (NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2709 {
2710 ACTIVATION_CONTEXT *actctx = check_actctx(NtCurrentTeb()->ActivationContextStack.ActiveFrame->ActivationContext);
2711 if (actctx) status = find_string( actctx, section_kind, section_name, flags, data );
2712 }
2713
2714 if (status != STATUS_SUCCESS)
2715 status = find_string( process_actctx, section_kind, section_name, flags, data );
2716
2717 return status;
2718 }
2719
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.