ole32: Added IMoniker::BindToStorage proxy/stub implementation.
[wine.git] / dlls / ntdll / actctx.c
blob8e4085d18512aff989c93f62a7cf6dbbc17d1e82
1 /*
2 * Activation contexts
4 * Copyright 2004 Jon Griffiths
5 * Copyright 2007 Eric Pouech
6 * Copyright 2007 Jacek Caban for CodeWeavers
7 * Copyright 2007 Alexandre Julliard
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.
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.
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
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
28 #include <stdio.h>
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"
41 WINE_DEFAULT_DEBUG_CHANNEL(actctx);
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 )
53 #define ACTCTX_MAGIC 0xC07E3E11
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)
59 typedef struct
61 const WCHAR *ptr;
62 unsigned int len;
63 } xmlstr_t;
65 typedef struct
67 const WCHAR *ptr;
68 const WCHAR *end;
69 } xmlbuf_t;
71 struct file_info
73 ULONG type;
74 WCHAR *info;
77 struct assembly_version
79 USHORT major;
80 USHORT minor;
81 USHORT build;
82 USHORT revision;
85 struct assembly_identity
87 WCHAR *name;
88 WCHAR *arch;
89 WCHAR *public_key;
90 WCHAR *language;
91 WCHAR *type;
92 struct assembly_version version;
93 BOOL optional;
96 struct entity
98 DWORD kind;
99 union
101 struct
103 WCHAR *tlbid;
104 WCHAR *version;
105 WCHAR *helpdir;
106 } typelib;
107 struct
109 WCHAR *clsid;
110 } comclass;
111 struct {
112 WCHAR *iid;
113 WCHAR *name;
114 } proxy;
115 struct
117 WCHAR *name;
118 } class;
119 struct
121 WCHAR *name;
122 WCHAR *clsid;
123 } clrclass;
124 struct
126 WCHAR *name;
127 WCHAR *clsid;
128 } clrsurrogate;
129 } u;
132 struct entity_array
134 struct entity *base;
135 unsigned int num;
136 unsigned int allocated;
139 struct dll_redirect
141 WCHAR *name;
142 WCHAR *hash;
143 struct entity_array entities;
146 enum assembly_type
148 APPLICATION_MANIFEST,
149 ASSEMBLY_MANIFEST,
150 ASSEMBLY_SHARED_MANIFEST,
153 struct assembly
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;
166 typedef struct _ACTIVATION_CONTEXT
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;
177 struct actctx_loader
179 ACTIVATION_CONTEXT *actctx;
180 struct assembly_identity *dependencies;
181 unsigned int num_dependencies;
182 unsigned int allocated_dependencies;
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};
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};
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};
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};
231 static ACTIVATION_CONTEXT system_actctx = { ACTCTX_MAGIC, 1 };
232 static ACTIVATION_CONTEXT *process_actctx = &system_actctx;
234 static WCHAR *strdupW(const WCHAR* str)
236 WCHAR* ptr;
238 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(WCHAR))))
239 return NULL;
240 return strcpyW(ptr, str);
243 static WCHAR *xmlstrdupW(const xmlstr_t* str)
245 WCHAR *strW;
247 if ((strW = RtlAllocateHeap(GetProcessHeap(), 0, (str->len + 1) * sizeof(WCHAR))))
249 memcpy( strW, str->ptr, str->len * sizeof(WCHAR) );
250 strW[str->len] = 0;
252 return strW;
255 static inline BOOL xmlstr_cmp(const xmlstr_t* xmlstr, const WCHAR *str)
257 return !strncmpW(xmlstr->ptr, str, xmlstr->len) && !str[xmlstr->len];
260 static inline BOOL xmlstr_cmpi(const xmlstr_t* xmlstr, const WCHAR *str)
262 return !strncmpiW(xmlstr->ptr, str, xmlstr->len) && !str[xmlstr->len];
265 static inline BOOL xmlstr_cmp_end(const xmlstr_t* xmlstr, const WCHAR *str)
267 return (xmlstr->len && xmlstr->ptr[0] == '/' &&
268 !strncmpW(xmlstr->ptr + 1, str, xmlstr->len - 1) && !str[xmlstr->len - 1]);
271 static inline BOOL xml_elem_cmp(const xmlstr_t *elem, const WCHAR *str, const WCHAR *namespace)
273 UINT len = strlenW( namespace );
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]);
280 static inline BOOL xml_elem_cmp_end(const xmlstr_t *elem, const WCHAR *str, const WCHAR *namespace)
282 if (elem->len && elem->ptr[0] == '/')
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 );
289 return FALSE;
292 static inline BOOL isxmlspace( WCHAR ch )
294 return (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t');
297 static inline const char* debugstr_xmlstr(const xmlstr_t* str)
299 return debugstr_wn(str->ptr, str->len);
302 static inline const char* debugstr_version(const struct assembly_version *ver)
304 return wine_dbg_sprintf("%u.%u.%u.%u", ver->major, ver->minor, ver->build, ver->revision);
307 static struct assembly *add_assembly(ACTIVATION_CONTEXT *actctx, enum assembly_type at)
309 struct assembly *assembly;
311 if (actctx->num_assemblies == actctx->allocated_assemblies)
313 void *ptr;
314 unsigned int new_count;
315 if (actctx->assemblies)
317 new_count = actctx->allocated_assemblies * 2;
318 ptr = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
319 actctx->assemblies, new_count * sizeof(*assembly) );
321 else
323 new_count = 4;
324 ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*assembly) );
326 if (!ptr) return NULL;
327 actctx->assemblies = ptr;
328 actctx->allocated_assemblies = new_count;
331 assembly = &actctx->assemblies[actctx->num_assemblies++];
332 assembly->type = at;
333 return assembly;
336 static struct dll_redirect* add_dll_redirect(struct assembly* assembly)
338 if (assembly->num_dlls == assembly->allocated_dlls)
340 void *ptr;
341 unsigned int new_count;
342 if (assembly->dlls)
344 new_count = assembly->allocated_dlls * 2;
345 ptr = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
346 assembly->dlls, new_count * sizeof(*assembly->dlls) );
348 else
350 new_count = 4;
351 ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*assembly->dlls) );
353 if (!ptr) return NULL;
354 assembly->dlls = ptr;
355 assembly->allocated_dlls = new_count;
357 return &assembly->dlls[assembly->num_dlls++];
360 static void free_assembly_identity(struct assembly_identity *ai)
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 );
369 static struct entity* add_entity(struct entity_array *array, DWORD kind)
371 struct entity* entity;
373 if (array->num == array->allocated)
375 void *ptr;
376 unsigned int new_count;
377 if (array->base)
379 new_count = array->allocated * 2;
380 ptr = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
381 array->base, new_count * sizeof(*array->base) );
383 else
385 new_count = 4;
386 ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*array->base) );
388 if (!ptr) return NULL;
389 array->base = ptr;
390 array->allocated = new_count;
392 entity = &array->base[array->num++];
393 entity->kind = kind;
394 return entity;
397 static void free_entity_array(struct entity_array *array)
399 unsigned int i;
400 for (i = 0; i < array->num; i++)
402 struct entity *entity = &array->base[i];
403 switch (entity->kind)
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);
432 RtlFreeHeap( GetProcessHeap(), 0, array->base );
435 static BOOL is_matching_string( const WCHAR *str1, const WCHAR *str2 )
437 if (!str1) return !str2;
438 return str2 && !strcmpiW( str1, str2 );
441 static BOOL is_matching_identity( const struct assembly_identity *id1,
442 const struct assembly_identity *id2 )
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;
448 if (id1->language && id2->language && strcmpiW( id1->language, id2->language ))
450 if (strcmpW( wildcardW, id1->language ) && strcmpW( wildcardW, id2->language ))
451 return FALSE;
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;
461 static BOOL add_dependent_assembly_id(struct actctx_loader* acl,
462 struct assembly_identity* ai)
464 unsigned int i;
466 /* check if we already have that assembly */
468 for (i = 0; i < acl->actctx->num_assemblies; i++)
469 if (is_matching_identity( ai, &acl->actctx->assemblies[i].id ))
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;
477 for (i = 0; i < acl->num_dependencies; i++)
478 if (is_matching_identity( ai, &acl->dependencies[i] ))
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;
486 if (acl->num_dependencies == acl->allocated_dependencies)
488 void *ptr;
489 unsigned int new_count;
490 if (acl->dependencies)
492 new_count = acl->allocated_dependencies * 2;
493 ptr = RtlReAllocateHeap(GetProcessHeap(), 0, acl->dependencies,
494 new_count * sizeof(acl->dependencies[0]));
496 else
498 new_count = 4;
499 ptr = RtlAllocateHeap(GetProcessHeap(), 0, new_count * sizeof(acl->dependencies[0]));
501 if (!ptr) return FALSE;
502 acl->dependencies = ptr;
503 acl->allocated_dependencies = new_count;
505 acl->dependencies[acl->num_dependencies++] = *ai;
507 return TRUE;
510 static void free_depend_manifests(struct actctx_loader* acl)
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);
518 static WCHAR *build_assembly_dir(struct assembly_identity* ai)
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};
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;
532 if (!(ret = RtlAllocateHeap( GetProcessHeap(), 0, size ))) return NULL;
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;
549 static inline void append_string( WCHAR *buffer, const WCHAR *prefix, const WCHAR *str )
551 WCHAR *p = buffer;
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;
563 static WCHAR *build_assembly_id( const struct assembly_identity *ai )
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};
574 WCHAR version[64], *ret;
575 SIZE_T size = 0;
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;
585 if (!(ret = RtlAllocateHeap( GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR) )))
586 return NULL;
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;
597 static ACTIVATION_CONTEXT *check_actctx( HANDLE h )
599 ACTIVATION_CONTEXT *ret = NULL, *actctx = h;
601 if (!h || h == INVALID_HANDLE_VALUE) return NULL;
602 __TRY
604 if (actctx->magic == ACTCTX_MAGIC) ret = actctx;
606 __EXCEPT_PAGE_FAULT
609 __ENDTRY
610 return ret;
613 static inline void actctx_addref( ACTIVATION_CONTEXT *actctx )
615 interlocked_xchg_add( &actctx->ref_count, 1 );
618 static void actctx_release( ACTIVATION_CONTEXT *actctx )
620 if (interlocked_xchg_add( &actctx->ref_count, -1 ) == 1)
622 unsigned int i, j;
624 for (i = 0; i < actctx->num_assemblies; i++)
626 struct assembly *assembly = &actctx->assemblies[i];
627 for (j = 0; j < assembly->num_dlls; j++)
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 );
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);
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 );
648 static BOOL next_xml_attr(xmlbuf_t* xmlbuf, xmlstr_t* name, xmlstr_t* value,
649 BOOL* error, BOOL* end)
651 const WCHAR* ptr;
653 *error = TRUE;
655 while (xmlbuf->ptr < xmlbuf->end && isxmlspace(*xmlbuf->ptr))
656 xmlbuf->ptr++;
658 if (xmlbuf->ptr == xmlbuf->end) return FALSE;
660 if (*xmlbuf->ptr == '/')
662 xmlbuf->ptr++;
663 if (xmlbuf->ptr == xmlbuf->end || *xmlbuf->ptr != '>')
664 return FALSE;
666 xmlbuf->ptr++;
667 *end = TRUE;
668 *error = FALSE;
669 return FALSE;
672 if (*xmlbuf->ptr == '>')
674 xmlbuf->ptr++;
675 *error = FALSE;
676 return FALSE;
679 ptr = xmlbuf->ptr;
680 while (ptr < xmlbuf->end && *ptr != '=' && *ptr != '>' && !isxmlspace(*ptr)) ptr++;
682 if (ptr == xmlbuf->end || *ptr != '=') return FALSE;
684 name->ptr = xmlbuf->ptr;
685 name->len = ptr-xmlbuf->ptr;
686 xmlbuf->ptr = ptr;
688 ptr++;
689 if (ptr == xmlbuf->end || (*ptr != '"' && *ptr != '\'')) return FALSE;
691 value->ptr = ++ptr;
692 if (ptr == xmlbuf->end) return FALSE;
694 ptr = memchrW(ptr, ptr[-1], xmlbuf->end - ptr);
695 if (!ptr)
697 xmlbuf->ptr = xmlbuf->end;
698 return FALSE;
701 value->len = ptr - value->ptr;
702 xmlbuf->ptr = ptr + 1;
704 if (xmlbuf->ptr == xmlbuf->end) return FALSE;
706 *error = FALSE;
707 return TRUE;
710 static BOOL next_xml_elem(xmlbuf_t* xmlbuf, xmlstr_t* elem)
712 const WCHAR* ptr;
714 for (;;)
716 ptr = memchrW(xmlbuf->ptr, '<', xmlbuf->end - xmlbuf->ptr);
717 if (!ptr)
719 xmlbuf->ptr = xmlbuf->end;
720 return FALSE;
722 ptr++;
723 if (ptr + 3 < xmlbuf->end && ptr[0] == '!' && ptr[1] == '-' && ptr[2] == '-') /* skip comment */
725 for (ptr += 3; ptr + 3 <= xmlbuf->end; ptr++)
726 if (ptr[0] == '-' && ptr[1] == '-' && ptr[2] == '>') break;
728 if (ptr + 3 > xmlbuf->end)
730 xmlbuf->ptr = xmlbuf->end;
731 return FALSE;
733 xmlbuf->ptr = ptr + 3;
735 else break;
738 xmlbuf->ptr = ptr;
739 while (ptr < xmlbuf->end && !isxmlspace(*ptr) && *ptr != '>' && (*ptr != '/' || ptr == xmlbuf->ptr))
740 ptr++;
742 elem->ptr = xmlbuf->ptr;
743 elem->len = ptr - xmlbuf->ptr;
744 xmlbuf->ptr = ptr;
745 return xmlbuf->ptr != xmlbuf->end;
748 static BOOL parse_xml_header(xmlbuf_t* xmlbuf)
750 /* FIXME: parse attributes */
751 const WCHAR *ptr;
753 for (ptr = xmlbuf->ptr; ptr < xmlbuf->end - 1; ptr++)
755 if (ptr[0] == '?' && ptr[1] == '>')
757 xmlbuf->ptr = ptr + 2;
758 return TRUE;
761 return FALSE;
764 static BOOL parse_text_content(xmlbuf_t* xmlbuf, xmlstr_t* content)
766 const WCHAR *ptr = memchrW(xmlbuf->ptr, '<', xmlbuf->end - xmlbuf->ptr);
768 if (!ptr) return FALSE;
770 content->ptr = xmlbuf->ptr;
771 content->len = ptr - xmlbuf->ptr;
772 xmlbuf->ptr = ptr;
774 return TRUE;
777 static BOOL parse_version(const xmlstr_t *str, struct assembly_version *version)
779 unsigned int ver[4];
780 unsigned int pos;
781 const WCHAR *curr;
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++)
787 if (*curr >= '0' && *curr <= '9')
789 ver[pos] = ver[pos] * 10 + *curr - '0';
790 if (ver[pos] >= 0x10000) goto error;
792 else if (*curr == '.')
794 if (++pos >= 4) goto error;
796 else goto error;
798 version->major = ver[0];
799 version->minor = ver[1];
800 version->build = ver[2];
801 version->revision = ver[3];
802 return TRUE;
804 error:
805 FIXME( "Wrong version definition in manifest file (%s)\n", debugstr_xmlstr(str) );
806 return FALSE;
809 static BOOL parse_expect_elem(xmlbuf_t* xmlbuf, const WCHAR* name, const WCHAR *namespace)
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;
818 static BOOL parse_expect_no_attr(xmlbuf_t* xmlbuf, BOOL* end)
820 xmlstr_t attr_name, attr_value;
821 BOOL error;
823 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, end))
825 WARN("unexpected attr %s=%s\n", debugstr_xmlstr(&attr_name),
826 debugstr_xmlstr(&attr_value));
828 return !error;
831 static BOOL parse_end_element(xmlbuf_t *xmlbuf)
833 BOOL end = FALSE;
834 return parse_expect_no_attr(xmlbuf, &end) && !end;
837 static BOOL parse_expect_end_elem(xmlbuf_t *xmlbuf, const WCHAR *name, const WCHAR *namespace)
839 xmlstr_t elem;
840 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
841 if (!xml_elem_cmp_end(&elem, name, namespace))
843 FIXME( "unexpected element %s\n", debugstr_xmlstr(&elem) );
844 return FALSE;
846 return parse_end_element(xmlbuf);
849 static BOOL parse_unknown_elem(xmlbuf_t *xmlbuf, const xmlstr_t *unknown_elem)
851 xmlstr_t attr_name, attr_value, elem;
852 BOOL end = FALSE, error, ret = TRUE;
854 while(next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end));
855 if(error || end) return end;
857 while(ret && (ret = next_xml_elem(xmlbuf, &elem)))
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);
866 return ret && parse_end_element(xmlbuf);
869 static BOOL parse_assembly_identity_elem(xmlbuf_t* xmlbuf, ACTIVATION_CONTEXT* actctx,
870 struct assembly_identity* ai)
872 xmlstr_t attr_name, attr_value;
873 BOOL end = FALSE, error;
875 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
877 if (xmlstr_cmp(&attr_name, nameW))
879 if (!(ai->name = xmlstrdupW(&attr_value))) return FALSE;
881 else if (xmlstr_cmp(&attr_name, typeW))
883 if (!(ai->type = xmlstrdupW(&attr_value))) return FALSE;
885 else if (xmlstr_cmp(&attr_name, versionW))
887 if (!parse_version(&attr_value, &ai->version)) return FALSE;
889 else if (xmlstr_cmp(&attr_name, processorArchitectureW))
891 if (!(ai->arch = xmlstrdupW(&attr_value))) return FALSE;
893 else if (xmlstr_cmp(&attr_name, publicKeyTokenW))
895 if (!(ai->public_key = xmlstrdupW(&attr_value))) return FALSE;
897 else if (xmlstr_cmp(&attr_name, languageW))
899 WARN("Unsupported yet language attribute (%s)\n",
900 debugstr_xmlstr(&attr_value));
901 if (!(ai->language = xmlstrdupW(&attr_value))) return FALSE;
903 else
905 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name),
906 debugstr_xmlstr(&attr_value));
910 TRACE( "name=%s version=%s arch=%s\n",
911 debugstr_w(ai->name), debugstr_version(&ai->version), debugstr_w(ai->arch) );
913 if (error || end) return end;
914 return parse_expect_end_elem(xmlbuf, assemblyIdentityW, asmv1W);
917 static BOOL parse_com_class_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
919 xmlstr_t elem, attr_name, attr_value;
920 BOOL ret, end = FALSE, error;
921 struct entity* entity;
923 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION)))
924 return FALSE;
926 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
928 if (xmlstr_cmp(&attr_name, clsidW))
930 if (!(entity->u.comclass.clsid = xmlstrdupW(&attr_value))) return FALSE;
932 else
934 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
938 if (error || end) return end;
940 while ((ret = next_xml_elem(xmlbuf, &elem)))
942 if (xmlstr_cmp_end(&elem, comClassW))
944 ret = parse_end_element(xmlbuf);
945 break;
947 else
949 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
950 ret = parse_unknown_elem(xmlbuf, &elem);
953 return ret;
956 static BOOL parse_cominterface_proxy_stub_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
958 xmlstr_t attr_name, attr_value;
959 BOOL end = FALSE, error;
960 struct entity* entity;
962 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION)))
963 return FALSE;
965 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
967 if (xmlstr_cmp(&attr_name, iidW))
969 if (!(entity->u.proxy.iid = xmlstrdupW(&attr_value))) return FALSE;
971 if (xmlstr_cmp(&attr_name, nameW))
973 if (!(entity->u.proxy.name = xmlstrdupW(&attr_value))) return FALSE;
975 else
977 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
981 if (error || end) return end;
982 return parse_expect_end_elem(xmlbuf, comInterfaceProxyStubW, asmv1W);
985 static BOOL parse_typelib_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
987 xmlstr_t attr_name, attr_value;
988 BOOL end = FALSE, error;
989 struct entity* entity;
991 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION)))
992 return FALSE;
994 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
996 if (xmlstr_cmp(&attr_name, tlbidW))
998 if (!(entity->u.typelib.tlbid = xmlstrdupW(&attr_value))) return FALSE;
1000 if (xmlstr_cmp(&attr_name, versionW))
1002 if (!(entity->u.typelib.version = xmlstrdupW(&attr_value))) return FALSE;
1004 if (xmlstr_cmp(&attr_name, helpdirW))
1006 if (!(entity->u.typelib.helpdir = xmlstrdupW(&attr_value))) return FALSE;
1008 else
1010 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1014 if (error || end) return end;
1015 return parse_expect_end_elem(xmlbuf, typelibW, asmv1W);
1018 static BOOL parse_window_class_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
1020 xmlstr_t elem, content;
1021 BOOL end = FALSE, ret = TRUE;
1022 struct entity* entity;
1024 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION)))
1025 return FALSE;
1027 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1028 if (end) return FALSE;
1030 if (!parse_text_content(xmlbuf, &content)) return FALSE;
1032 if (!(entity->u.class.name = xmlstrdupW(&content))) return FALSE;
1034 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1036 if (xmlstr_cmp_end(&elem, windowClassW))
1038 ret = parse_end_element(xmlbuf);
1039 break;
1041 else
1043 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1044 ret = parse_unknown_elem(xmlbuf, &elem);
1048 return ret;
1051 static BOOL parse_binding_redirect_elem(xmlbuf_t* xmlbuf)
1053 xmlstr_t attr_name, attr_value;
1054 BOOL end = FALSE, error;
1056 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1058 if (xmlstr_cmp(&attr_name, oldVersionW))
1060 FIXME("Not stored yet oldVersion=%s\n", debugstr_xmlstr(&attr_value));
1062 else if (xmlstr_cmp(&attr_name, newVersionW))
1064 FIXME("Not stored yet newVersion=%s\n", debugstr_xmlstr(&attr_value));
1066 else
1068 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1072 if (error || end) return end;
1073 return parse_expect_end_elem(xmlbuf, bindingRedirectW, asmv1W);
1076 static BOOL parse_description_elem(xmlbuf_t* xmlbuf)
1078 xmlstr_t elem, content;
1079 BOOL end = FALSE, ret = TRUE;
1081 if (!parse_expect_no_attr(xmlbuf, &end) || end ||
1082 !parse_text_content(xmlbuf, &content))
1083 return FALSE;
1085 TRACE("Got description %s\n", debugstr_xmlstr(&content));
1087 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1089 if (xmlstr_cmp_end(&elem, descriptionW))
1091 ret = parse_end_element(xmlbuf);
1092 break;
1094 else
1096 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1097 ret = parse_unknown_elem(xmlbuf, &elem);
1101 return ret;
1104 static BOOL parse_com_interface_external_proxy_stub_elem(xmlbuf_t* xmlbuf,
1105 struct assembly* assembly)
1107 xmlstr_t attr_name, attr_value;
1108 BOOL end = FALSE, error;
1109 struct entity* entity;
1111 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION);
1112 if (!entity) return FALSE;
1114 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1116 if (xmlstr_cmp(&attr_name, iidW))
1118 if (!(entity->u.proxy.iid = xmlstrdupW(&attr_value))) return FALSE;
1120 if (xmlstr_cmp(&attr_name, nameW))
1122 if (!(entity->u.proxy.name = xmlstrdupW(&attr_value))) return FALSE;
1124 else
1126 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1130 if (error || end) return end;
1131 return parse_expect_end_elem(xmlbuf, comInterfaceExternalProxyStubW, asmv1W);
1134 static BOOL parse_clr_class_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1136 xmlstr_t attr_name, attr_value;
1137 BOOL end = FALSE, error;
1138 struct entity* entity;
1140 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION);
1141 if (!entity) return FALSE;
1143 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1145 if (xmlstr_cmp(&attr_name, nameW))
1147 if (!(entity->u.clrclass.name = xmlstrdupW(&attr_value))) return FALSE;
1149 else if (xmlstr_cmp(&attr_name, clsidW))
1151 if (!(entity->u.clrclass.clsid = xmlstrdupW(&attr_value))) return FALSE;
1153 else
1155 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1159 if (error || end) return end;
1160 return parse_expect_end_elem(xmlbuf, clrClassW, asmv1W);
1163 static BOOL parse_clr_surrogate_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1165 xmlstr_t attr_name, attr_value;
1166 BOOL end = FALSE, error;
1167 struct entity* entity;
1169 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES);
1170 if (!entity) return FALSE;
1172 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1174 if (xmlstr_cmp(&attr_name, nameW))
1176 if (!(entity->u.clrsurrogate.name = xmlstrdupW(&attr_value))) return FALSE;
1178 else if (xmlstr_cmp(&attr_name, clsidW))
1180 if (!(entity->u.clrsurrogate.clsid = xmlstrdupW(&attr_value))) return FALSE;
1182 else
1184 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1188 if (error || end) return end;
1189 return parse_expect_end_elem(xmlbuf, clrSurrogateW, asmv1W);
1192 static BOOL parse_dependent_assembly_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl, BOOL optional)
1194 struct assembly_identity ai;
1195 xmlstr_t elem;
1196 BOOL end = FALSE, ret = TRUE;
1198 if (!parse_expect_no_attr(xmlbuf, &end) || end) return end;
1200 memset(&ai, 0, sizeof(ai));
1201 ai.optional = optional;
1203 if (!parse_expect_elem(xmlbuf, assemblyIdentityW, asmv1W) ||
1204 !parse_assembly_identity_elem(xmlbuf, acl->actctx, &ai))
1205 return FALSE;
1207 TRACE( "adding name=%s version=%s arch=%s\n",
1208 debugstr_w(ai.name), debugstr_version(&ai.version), debugstr_w(ai.arch) );
1210 /* store the newly found identity for later loading */
1211 if (!add_dependent_assembly_id(acl, &ai)) return FALSE;
1213 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1215 if (xmlstr_cmp_end(&elem, dependentAssemblyW))
1217 ret = parse_end_element(xmlbuf);
1218 break;
1220 else if (xmlstr_cmp(&elem, bindingRedirectW))
1222 ret = parse_binding_redirect_elem(xmlbuf);
1224 else
1226 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1227 ret = parse_unknown_elem(xmlbuf, &elem);
1231 return ret;
1234 static BOOL parse_dependency_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl)
1236 xmlstr_t attr_name, attr_value, elem;
1237 BOOL end = FALSE, ret = TRUE, error, optional = FALSE;
1239 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1241 if (xmlstr_cmp(&attr_name, optionalW))
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));
1247 else
1249 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1253 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1255 if (xmlstr_cmp_end(&elem, dependencyW))
1257 ret = parse_end_element(xmlbuf);
1258 break;
1260 else if (xmlstr_cmp(&elem, dependentAssemblyW))
1262 ret = parse_dependent_assembly_elem(xmlbuf, acl, optional);
1264 else
1266 WARN("unknown element %s\n", debugstr_xmlstr(&elem));
1267 ret = parse_unknown_elem(xmlbuf, &elem);
1271 return ret;
1274 static BOOL parse_noinherit_elem(xmlbuf_t* xmlbuf)
1276 BOOL end = FALSE;
1278 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1279 return end || parse_expect_end_elem(xmlbuf, noInheritW, asmv1W);
1282 static BOOL parse_noinheritable_elem(xmlbuf_t* xmlbuf)
1284 BOOL end = FALSE;
1286 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1287 return end || parse_expect_end_elem(xmlbuf, noInheritableW, asmv1W);
1290 static BOOL parse_file_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1292 xmlstr_t attr_name, attr_value, elem;
1293 BOOL end = FALSE, error, ret = TRUE;
1294 struct dll_redirect* dll;
1296 if (!(dll = add_dll_redirect(assembly))) return FALSE;
1298 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1300 if (xmlstr_cmp(&attr_name, nameW))
1302 if (!(dll->name = xmlstrdupW(&attr_value))) return FALSE;
1303 TRACE("name=%s\n", debugstr_xmlstr(&attr_value));
1305 else if (xmlstr_cmp(&attr_name, hashW))
1307 if (!(dll->hash = xmlstrdupW(&attr_value))) return FALSE;
1309 else if (xmlstr_cmp(&attr_name, hashalgW))
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));
1315 else
1317 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1321 if (error || !dll->name) return FALSE;
1322 if (end) return TRUE;
1324 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1326 if (xmlstr_cmp_end(&elem, fileW))
1328 ret = parse_end_element(xmlbuf);
1329 break;
1331 else if (xmlstr_cmp(&elem, comClassW))
1333 ret = parse_com_class_elem(xmlbuf, dll);
1335 else if (xmlstr_cmp(&elem, comInterfaceProxyStubW))
1337 ret = parse_cominterface_proxy_stub_elem(xmlbuf, dll);
1339 else if (xml_elem_cmp(&elem, hashW, asmv2W))
1341 WARN("asmv2:hash (undocumented) not supported\n");
1342 ret = parse_unknown_elem(xmlbuf, &elem);
1344 else if (xmlstr_cmp(&elem, typelibW))
1346 ret = parse_typelib_elem(xmlbuf, dll);
1348 else if (xmlstr_cmp(&elem, windowClassW))
1350 ret = parse_window_class_elem(xmlbuf, dll);
1352 else
1354 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1355 ret = parse_unknown_elem( xmlbuf, &elem );
1359 return ret;
1362 static BOOL parse_assembly_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl,
1363 struct assembly* assembly,
1364 struct assembly_identity* expected_ai)
1366 xmlstr_t attr_name, attr_value, elem;
1367 BOOL end = FALSE, error, version = FALSE, xmlns = FALSE, ret = TRUE;
1369 TRACE("(%p)\n", xmlbuf);
1371 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1373 if (xmlstr_cmp(&attr_name, manifestVersionW))
1375 static const WCHAR v10W[] = {'1','.','0',0};
1376 if (!xmlstr_cmp(&attr_value, v10W))
1378 FIXME("wrong version %s\n", debugstr_xmlstr(&attr_value));
1379 return FALSE;
1381 version = TRUE;
1383 else if (xmlstr_cmp(&attr_name, xmlnsW))
1385 if (!xmlstr_cmp(&attr_value, manifestv1W) && !xmlstr_cmp(&attr_value, manifestv3W))
1387 FIXME("wrong namespace %s\n", debugstr_xmlstr(&attr_value));
1388 return FALSE;
1390 xmlns = TRUE;
1392 else
1394 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1398 if (error || end || !xmlns || !version) return FALSE;
1399 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
1401 if (assembly->type == APPLICATION_MANIFEST && xmlstr_cmp(&elem, noInheritW))
1403 if (!parse_noinherit_elem(xmlbuf) || !next_xml_elem(xmlbuf, &elem))
1404 return FALSE;
1405 assembly->no_inherit = TRUE;
1408 if (xml_elem_cmp(&elem, noInheritableW, asmv1W))
1410 if (!parse_noinheritable_elem(xmlbuf) || !next_xml_elem(xmlbuf, &elem))
1411 return FALSE;
1413 else if ((assembly->type == ASSEMBLY_MANIFEST || assembly->type == ASSEMBLY_SHARED_MANIFEST) &&
1414 assembly->no_inherit)
1415 return FALSE;
1417 while (ret)
1419 if (xml_elem_cmp_end(&elem, assemblyW, asmv1W))
1421 ret = parse_end_element(xmlbuf);
1422 break;
1424 else if (xml_elem_cmp(&elem, descriptionW, asmv1W))
1426 ret = parse_description_elem(xmlbuf);
1428 else if (xml_elem_cmp(&elem, comInterfaceExternalProxyStubW, asmv1W))
1430 ret = parse_com_interface_external_proxy_stub_elem(xmlbuf, assembly);
1432 else if (xml_elem_cmp(&elem, dependencyW, asmv1W))
1434 ret = parse_dependency_elem(xmlbuf, acl);
1436 else if (xml_elem_cmp(&elem, fileW, asmv1W))
1438 ret = parse_file_elem(xmlbuf, assembly);
1440 else if (xml_elem_cmp(&elem, clrClassW, asmv1W))
1442 ret = parse_clr_class_elem(xmlbuf, assembly);
1444 else if (xml_elem_cmp(&elem, clrSurrogateW, asmv1W))
1446 ret = parse_clr_surrogate_elem(xmlbuf, assembly);
1448 else if (xml_elem_cmp(&elem, assemblyIdentityW, asmv1W))
1450 if (!parse_assembly_identity_elem(xmlbuf, acl->actctx, &assembly->id)) return FALSE;
1452 if (expected_ai)
1454 /* FIXME: more tests */
1455 if (assembly->type == ASSEMBLY_MANIFEST &&
1456 memcmp(&assembly->id.version, &expected_ai->version, sizeof(assembly->id.version)))
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;
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)))
1472 FIXME("wrong version for shared assembly manifest\n");
1473 ret = FALSE;
1477 else
1479 WARN("unknown element %s\n", debugstr_xmlstr(&elem));
1480 ret = parse_unknown_elem(xmlbuf, &elem);
1482 if (ret) ret = next_xml_elem(xmlbuf, &elem);
1485 return ret;
1488 static NTSTATUS parse_manifest_buffer( struct actctx_loader* acl, struct assembly *assembly,
1489 struct assembly_identity* ai, xmlbuf_t *xmlbuf )
1491 xmlstr_t elem;
1493 if (!next_xml_elem(xmlbuf, &elem)) return STATUS_SXS_CANT_GEN_ACTCTX;
1495 if (xmlstr_cmp(&elem, xmlW) &&
1496 (!parse_xml_header(xmlbuf) || !next_xml_elem(xmlbuf, &elem)))
1497 return STATUS_SXS_CANT_GEN_ACTCTX;
1499 if (!xml_elem_cmp(&elem, assemblyW, asmv1W))
1501 FIXME("root element is %s, not <assembly>\n", debugstr_xmlstr(&elem));
1502 return STATUS_SXS_CANT_GEN_ACTCTX;
1505 if (!parse_assembly_elem(xmlbuf, acl, assembly, ai))
1507 FIXME("failed to parse manifest %s\n", debugstr_w(assembly->manifest.info) );
1508 return STATUS_SXS_CANT_GEN_ACTCTX;
1511 if (next_xml_elem(xmlbuf, &elem))
1513 FIXME("unexpected element %s\n", debugstr_xmlstr(&elem));
1514 return STATUS_SXS_CANT_GEN_ACTCTX;
1517 if (xmlbuf->ptr != xmlbuf->end)
1519 FIXME("parse error\n");
1520 return STATUS_SXS_CANT_GEN_ACTCTX;
1522 return STATUS_SUCCESS;
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 )
1529 xmlbuf_t xmlbuf;
1530 NTSTATUS status;
1531 struct assembly *assembly;
1532 int unicode_tests;
1534 TRACE( "parsing manifest loaded from %s base dir %s\n", debugstr_w(filename), debugstr_w(directory) );
1536 if (!(assembly = add_assembly(acl->actctx, shared ? ASSEMBLY_SHARED_MANIFEST : ASSEMBLY_MANIFEST)))
1537 return STATUS_SXS_CANT_GEN_ACTCTX;
1539 if (directory && !(assembly->directory = strdupW(directory)))
1540 return STATUS_NO_MEMORY;
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;
1546 unicode_tests = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1547 if (RtlIsTextUnicode( buffer, size, &unicode_tests ))
1549 xmlbuf.ptr = buffer;
1550 xmlbuf.end = xmlbuf.ptr + size / sizeof(WCHAR);
1551 status = parse_manifest_buffer( acl, assembly, ai, &xmlbuf );
1553 else if (unicode_tests & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
1555 const WCHAR *buf = buffer;
1556 WCHAR *new_buff;
1557 unsigned int i;
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 );
1568 else
1570 /* let's assume utf-8 for now */
1571 int len = wine_utf8_mbstowcs( 0, buffer, size, NULL, 0 );
1572 WCHAR *new_buff;
1574 if (len == -1)
1576 FIXME( "utf-8 conversion failed\n" );
1577 return STATUS_SXS_CANT_GEN_ACTCTX;
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 );
1587 return status;
1590 static NTSTATUS open_nt_file( HANDLE *handle, UNICODE_STRING *name )
1592 OBJECT_ATTRIBUTES attr;
1593 IO_STATUS_BLOCK io;
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 );
1604 static NTSTATUS get_module_filename( HMODULE module, UNICODE_STRING *str, unsigned int extra_len )
1606 NTSTATUS status;
1607 ULONG magic;
1608 LDR_MODULE *pldr;
1610 LdrLockLoaderLock(0, NULL, &magic);
1611 status = LdrFindEntryForAddress( module, &pldr );
1612 if (status == STATUS_SUCCESS)
1614 if ((str->Buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1615 pldr->FullDllName.Length + extra_len + sizeof(WCHAR) )))
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);
1621 else status = STATUS_NO_MEMORY;
1623 LdrUnlockLoaderLock(0, magic);
1624 return status;
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 )
1631 NTSTATUS status;
1632 UNICODE_STRING nameW;
1633 LDR_RESOURCE_INFO info;
1634 const IMAGE_RESOURCE_DATA_ENTRY* entry = NULL;
1635 void *ptr;
1637 if (TRACE_ON(actctx))
1639 if (!filename && !get_module_filename( hModule, &nameW, 0 ))
1641 TRACE( "looking for res %s in module %p %s\n", debugstr_w(resname),
1642 hModule, debugstr_w(nameW.Buffer) );
1643 RtlFreeUnicodeString( &nameW );
1645 else TRACE( "looking for res %s in module %p %s\n", debugstr_w(resname),
1646 hModule, debugstr_w(filename) );
1649 if (!resname) return STATUS_INVALID_PARAMETER;
1651 info.Type = RT_MANIFEST;
1652 info.Language = lang;
1653 if (!((ULONG_PTR)resname >> 16))
1655 info.Name = (ULONG_PTR)resname;
1656 status = LdrFindResource_U(hModule, &info, 3, &entry);
1658 else if (resname[0] == '#')
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);
1667 else
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);
1675 if (status == STATUS_SUCCESS) status = LdrAccessResource(hModule, entry, &ptr, NULL);
1677 if (status == STATUS_SUCCESS)
1678 status = parse_manifest(acl, ai, filename, directory, shared, ptr, entry->Size);
1680 return status;
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 )
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;
1695 TRACE( "looking for res %s in %s\n", debugstr_w(resname), debugstr_w(filename) );
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;
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;
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;
1717 if (RtlImageNtHeader(base)) /* we got a PE file */
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 );
1722 else status = STATUS_INVALID_IMAGE_FORMAT;
1724 NtUnmapViewOfSection( GetCurrentProcess(), base );
1725 return status;
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 )
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;
1741 TRACE( "loading manifest file %s\n", debugstr_w(filename) );
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;
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;
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;
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);
1767 NtUnmapViewOfSection( GetCurrentProcess(), base );
1768 return status;
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 )
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;
1782 if (!((ULONG_PTR)resname >> 16)) resid = (ULONG_PTR)resname & 0xffff;
1784 TRACE( "looking for manifest associated with %s id %lu\n", debugstr_w(filename), resid );
1786 if (module) /* use the module filename */
1788 UNICODE_STRING name;
1790 if (!(status = get_module_filename( module, &name, sizeof(dotManifestW) + 10*sizeof(WCHAR) )))
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 );
1798 if (status) return status;
1800 else
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 );
1811 if (!open_nt_file( &file, &nameW ))
1813 status = get_manifest_in_manifest_file( acl, ai, nameW.Buffer, directory, FALSE, file );
1814 NtClose( file );
1816 else status = STATUS_RESOURCE_DATA_NOT_FOUND;
1817 RtlFreeUnicodeString( &nameW );
1818 return status;
1821 static WCHAR *lookup_manifest_file( HANDLE dir, struct assembly_identity *ai )
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'};
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];
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;
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 );
1846 NtQueryDirectoryFile( dir, 0, NULL, NULL, &io, buffer, sizeof(buffer),
1847 FileBothDirectoryInformation, FALSE, &lookup_us, TRUE );
1848 if (io.u.Status == STATUS_SUCCESS)
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;
1855 data_len = io.Information;
1857 for (;;)
1859 if (data_pos >= data_len)
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;
1867 dir_info = (FILE_BOTH_DIR_INFORMATION*)(buffer + data_pos);
1869 if (dir_info->NextEntryOffset) data_pos += dir_info->NextEntryOffset;
1870 else data_pos = data_len;
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) ))
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;
1887 else
1889 min_build = build;
1890 min_revision = revision;
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) )))
1897 memcpy( ret, dir_info->FileName, dir_info->FileNameLength );
1898 ret[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
1902 else WARN("no matching file for %s\n", debugstr_w(lookup));
1903 RtlFreeHeap( GetProcessHeap(), 0, lookup );
1904 return ret;
1907 static NTSTATUS lookup_winsxs(struct actctx_loader* acl, struct assembly_identity* ai)
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;
1916 static const WCHAR manifest_dirW[] =
1917 {'\\','w','i','n','s','x','s','\\','m','a','n','i','f','e','s','t','s',0};
1919 if (!ai->arch || !ai->name || !ai->public_key) return STATUS_NO_SUCH_FILE;
1921 if (!(path = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(manifest_dirW) +
1922 strlenW(user_shared_data->NtSystemRoot) * sizeof(WCHAR) )))
1923 return STATUS_NO_MEMORY;
1925 strcpyW( path, user_shared_data->NtSystemRoot );
1926 memcpy( path + strlenW(path), manifest_dirW, sizeof(manifest_dirW) );
1928 if (!RtlDosPathNameToNtPathName_U( path, &path_us, NULL, NULL ))
1930 RtlFreeHeap( GetProcessHeap(), 0, path );
1931 return STATUS_NO_SUCH_FILE;
1933 RtlFreeHeap( GetProcessHeap(), 0, path );
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;
1942 if (!NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
1943 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT ))
1945 sxs_ai = *ai;
1946 file = lookup_manifest_file( handle, &sxs_ai );
1947 NtClose( handle );
1949 if (!file)
1951 RtlFreeUnicodeString( &path_us );
1952 return STATUS_NO_SUCH_FILE;
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) )))
1959 RtlFreeHeap( GetProcessHeap(), 0, file );
1960 RtlFreeUnicodeString( &path_us );
1961 return STATUS_NO_MEMORY;
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 */
1969 if (!open_nt_file( &handle, &path_us ))
1971 io.u.Status = get_manifest_in_manifest_file(acl, &sxs_ai, path_us.Buffer, file, TRUE, handle);
1972 NtClose( handle );
1974 else io.u.Status = STATUS_NO_SUCH_FILE;
1976 RtlFreeHeap( GetProcessHeap(), 0, file );
1977 RtlFreeUnicodeString( &path_us );
1978 return io.u.Status;
1981 static NTSTATUS lookup_assembly(struct actctx_loader* acl,
1982 struct assembly_identity* ai)
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;
1991 TRACE( "looking for name=%s version=%s arch=%s\n",
1992 debugstr_w(ai->name), debugstr_version(&ai->version), debugstr_w(ai->arch) );
1994 if ((status = lookup_winsxs(acl, ai)) != STATUS_NO_SUCH_FILE) return status;
1996 /* FIXME: add support for language specific lookup */
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;
2003 if (!(directory = build_assembly_dir( ai )))
2005 RtlFreeHeap( GetProcessHeap(), 0, buffer );
2006 return STATUS_NO_MEMORY;
2009 /* lookup in appdir\name.dll
2010 * appdir\name.manifest
2011 * appdir\name\name.dll
2012 * appdir\name\name.manifest
2014 strcpyW( buffer, acl->actctx->appdir.info );
2015 p = buffer + strlenW(buffer);
2016 for (i = 0; i < 2; i++)
2018 *p++ = '\\';
2019 strcpyW( p, ai->name );
2020 p += strlenW(p);
2022 strcpyW( p, dotDllW );
2023 if (RtlDosPathNameToNtPathName_U( buffer, &nameW, NULL, NULL ))
2025 status = open_nt_file( &file, &nameW );
2026 if (!status)
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;
2033 RtlFreeUnicodeString( &nameW );
2036 strcpyW( p, dotManifestW );
2037 if (RtlDosPathNameToNtPathName_U( buffer, &nameW, NULL, NULL ))
2039 status = open_nt_file( &file, &nameW );
2040 if (!status)
2042 status = get_manifest_in_manifest_file( acl, ai, nameW.Buffer, directory, FALSE, file );
2043 NtClose( file );
2044 break;
2046 RtlFreeUnicodeString( &nameW );
2048 status = STATUS_SXS_ASSEMBLY_NOT_FOUND;
2050 RtlFreeUnicodeString( &nameW );
2051 RtlFreeHeap( GetProcessHeap(), 0, directory );
2052 RtlFreeHeap( GetProcessHeap(), 0, buffer );
2053 return status;
2056 static NTSTATUS parse_depend_manifests(struct actctx_loader* acl)
2058 NTSTATUS status = STATUS_SUCCESS;
2059 unsigned int i;
2061 for (i = 0; i < acl->num_dependencies; i++)
2063 if (lookup_assembly(acl, &acl->dependencies[i]) != STATUS_SUCCESS)
2065 if (!acl->dependencies[i].optional)
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;
2075 /* FIXME should now iterate through all refs */
2076 return status;
2079 /* find the appropriate activation context for RtlQueryInformationActivationContext */
2080 static NTSTATUS find_query_actctx( HANDLE *handle, DWORD flags, ULONG class )
2082 NTSTATUS status = STATUS_SUCCESS;
2084 if (flags & QUERY_ACTCTX_FLAG_USE_ACTIVE_ACTCTX)
2086 if (*handle) return STATUS_INVALID_PARAMETER;
2088 if (NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2089 *handle = NtCurrentTeb()->ActivationContextStack.ActiveFrame->ActivationContext;
2091 else if (flags & (QUERY_ACTCTX_FLAG_ACTCTX_IS_ADDRESS|QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE))
2093 ULONG magic;
2094 LDR_MODULE *pldr;
2096 if (!*handle) return STATUS_INVALID_PARAMETER;
2098 LdrLockLoaderLock( 0, NULL, &magic );
2099 if (!LdrFindEntryForAddress( *handle, &pldr ))
2101 if ((flags & QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE) && *handle != pldr->BaseAddress)
2102 status = STATUS_DLL_NOT_FOUND;
2103 else
2104 *handle = pldr->ActivationContext;
2106 else status = STATUS_DLL_NOT_FOUND;
2107 LdrUnlockLoaderLock( 0, magic );
2109 else if (!*handle && (class != ActivationContextBasicInformation))
2110 *handle = process_actctx;
2112 return status;
2115 static NTSTATUS fill_keyed_data(PACTCTX_SECTION_KEYED_DATA data, PVOID v1, PVOID v2, unsigned int i)
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;
2128 return STATUS_SUCCESS;
2131 static NTSTATUS find_dll_redirection(ACTIVATION_CONTEXT* actctx, const UNICODE_STRING *section_name,
2132 PACTCTX_SECTION_KEYED_DATA data)
2134 unsigned int i, j, snlen = section_name->Length / sizeof(WCHAR);
2136 for (i = 0; i < actctx->num_assemblies; i++)
2138 struct assembly *assembly = &actctx->assemblies[i];
2139 for (j = 0; j < assembly->num_dlls; j++)
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);
2146 return STATUS_SXS_KEY_NOT_FOUND;
2149 static NTSTATUS find_window_class(ACTIVATION_CONTEXT* actctx, const UNICODE_STRING *section_name,
2150 PACTCTX_SECTION_KEYED_DATA data)
2152 unsigned int i, j, k, snlen = section_name->Length / sizeof(WCHAR);
2154 for (i = 0; i < actctx->num_assemblies; i++)
2156 struct assembly *assembly = &actctx->assemblies[i];
2157 for (j = 0; j < assembly->num_dlls; j++)
2159 struct dll_redirect *dll = &assembly->dlls[j];
2160 for (k = 0; k < dll->entities.num; k++)
2162 struct entity *entity = &dll->entities.base[k];
2163 if (entity->kind == ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION)
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);
2171 return STATUS_SXS_KEY_NOT_FOUND;
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)
2178 NTSTATUS status;
2180 switch (section_kind)
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;
2201 if (status != STATUS_SUCCESS) return status;
2203 if (flags & FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX)
2205 actctx_addref(actctx);
2206 data->hActCtx = actctx;
2208 return STATUS_SUCCESS;
2211 /* initialize the activation context for the current process */
2212 void actctx_init(void)
2214 ACTCTXW ctx;
2215 HANDLE handle;
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;
2223 if (!RtlCreateActivationContext( &handle, &ctx )) process_actctx = check_actctx(handle);
2227 /***********************************************************************
2228 * RtlCreateActivationContext (NTDLL.@)
2230 * Create an activation context.
2232 * FIXME: function signature/prototype is wrong
2234 NTSTATUS WINAPI RtlCreateActivationContext( HANDLE *handle, const void *ptr )
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;
2245 TRACE("%p %08x\n", pActCtx, pActCtx ? pActCtx->dwFlags : 0);
2247 if (!pActCtx || pActCtx->cbSize < sizeof(*pActCtx) ||
2248 (pActCtx->dwFlags & ~ACTCTX_FLAGS_ALL))
2249 return STATUS_INVALID_PARAMETER;
2251 if (!(actctx = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*actctx) )))
2252 return STATUS_NO_MEMORY;
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)
2261 if (!(actctx->appdir.info = strdupW( pActCtx->lpApplicationName ))) goto error;
2263 else
2265 UNICODE_STRING dir;
2266 WCHAR *p;
2267 HMODULE module;
2269 if (pActCtx->dwFlags & ACTCTX_FLAG_HMODULE_VALID) module = pActCtx->hModule;
2270 else module = NtCurrentTeb()->Peb->ImageBaseAddress;
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;
2277 nameW.Buffer = NULL;
2278 if (pActCtx->lpSource)
2280 if (!RtlDosPathNameToNtPathName_U(pActCtx->lpSource, &nameW, NULL, NULL))
2282 status = STATUS_NO_SUCH_FILE;
2283 goto error;
2285 status = open_nt_file( &file, &nameW );
2286 if (status)
2288 RtlFreeUnicodeString( &nameW );
2289 goto error;
2293 acl.actctx = actctx;
2294 acl.dependencies = NULL;
2295 acl.num_dependencies = 0;
2296 acl.allocated_dependencies = 0;
2298 if (pActCtx->dwFlags & ACTCTX_FLAG_LANGID_VALID) lang = pActCtx->wLangId;
2299 if (pActCtx->dwFlags & ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID) directory = pActCtx->lpAssemblyDirectory;
2301 if (pActCtx->dwFlags & ACTCTX_FLAG_RESOURCE_NAME_VALID)
2303 /* if we have a resource it's a PE file */
2304 if (pActCtx->dwFlags & ACTCTX_FLAG_HMODULE_VALID)
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 );
2313 else if (pActCtx->lpSource)
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 );
2321 else status = STATUS_INVALID_PARAMETER;
2323 else
2325 status = get_manifest_in_manifest_file( &acl, NULL, nameW.Buffer, directory, FALSE, file );
2328 if (file) NtClose( file );
2329 RtlFreeUnicodeString( &nameW );
2331 if (status == STATUS_SUCCESS) status = parse_depend_manifests(&acl);
2332 free_depend_manifests( &acl );
2334 if (status == STATUS_SUCCESS) *handle = actctx;
2335 else actctx_release( actctx );
2336 return status;
2338 error:
2339 if (file) NtClose( file );
2340 actctx_release( actctx );
2341 return status;
2345 /***********************************************************************
2346 * RtlAddRefActivationContext (NTDLL.@)
2348 void WINAPI RtlAddRefActivationContext( HANDLE handle )
2350 ACTIVATION_CONTEXT *actctx;
2352 if ((actctx = check_actctx( handle ))) actctx_addref( actctx );
2356 /******************************************************************
2357 * RtlReleaseActivationContext (NTDLL.@)
2359 void WINAPI RtlReleaseActivationContext( HANDLE handle )
2361 ACTIVATION_CONTEXT *actctx;
2363 if ((actctx = check_actctx( handle ))) actctx_release( actctx );
2367 /******************************************************************
2368 * RtlActivateActivationContext (NTDLL.@)
2370 NTSTATUS WINAPI RtlActivateActivationContext( ULONG unknown, HANDLE handle, PULONG_PTR cookie )
2372 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2374 if (!(frame = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*frame) )))
2375 return STATUS_NO_MEMORY;
2377 frame->Previous = NtCurrentTeb()->ActivationContextStack.ActiveFrame;
2378 frame->ActivationContext = handle;
2379 frame->Flags = 0;
2380 NtCurrentTeb()->ActivationContextStack.ActiveFrame = frame;
2381 RtlAddRefActivationContext( handle );
2383 *cookie = (ULONG_PTR)frame;
2384 TRACE( "%p cookie=%lx\n", handle, *cookie );
2385 return STATUS_SUCCESS;
2389 /***********************************************************************
2390 * RtlDeactivateActivationContext (NTDLL.@)
2392 void WINAPI RtlDeactivateActivationContext( ULONG flags, ULONG_PTR cookie )
2394 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame, *top;
2396 TRACE( "%x cookie=%lx\n", flags, cookie );
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;
2403 if (!frame)
2404 RtlRaiseStatus( STATUS_SXS_INVALID_DEACTIVATION );
2406 if (frame != top && !(flags & DEACTIVATE_ACTCTX_FLAG_FORCE_EARLY_DEACTIVATION))
2407 RtlRaiseStatus( STATUS_SXS_EARLY_DEACTIVATION );
2409 /* pop everything up to and including frame */
2410 NtCurrentTeb()->ActivationContextStack.ActiveFrame = frame->Previous;
2412 while (top != NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2414 frame = top->Previous;
2415 RtlReleaseActivationContext( top->ActivationContext );
2416 RtlFreeHeap( GetProcessHeap(), 0, top );
2417 top = frame;
2422 /******************************************************************
2423 * RtlFreeThreadActivationContextStack (NTDLL.@)
2425 void WINAPI RtlFreeThreadActivationContextStack(void)
2427 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2429 frame = NtCurrentTeb()->ActivationContextStack.ActiveFrame;
2430 while (frame)
2432 RTL_ACTIVATION_CONTEXT_STACK_FRAME *prev = frame->Previous;
2433 RtlReleaseActivationContext( frame->ActivationContext );
2434 RtlFreeHeap( GetProcessHeap(), 0, frame );
2435 frame = prev;
2437 NtCurrentTeb()->ActivationContextStack.ActiveFrame = NULL;
2441 /******************************************************************
2442 * RtlGetActiveActivationContext (NTDLL.@)
2444 NTSTATUS WINAPI RtlGetActiveActivationContext( HANDLE *handle )
2446 if (NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2448 *handle = NtCurrentTeb()->ActivationContextStack.ActiveFrame->ActivationContext;
2449 RtlAddRefActivationContext( *handle );
2451 else
2452 *handle = 0;
2454 return STATUS_SUCCESS;
2458 /******************************************************************
2459 * RtlIsActivationContextActive (NTDLL.@)
2461 BOOLEAN WINAPI RtlIsActivationContextActive( HANDLE handle )
2463 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2465 for (frame = NtCurrentTeb()->ActivationContextStack.ActiveFrame; frame; frame = frame->Previous)
2466 if (frame->ActivationContext == handle) return TRUE;
2467 return FALSE;
2471 /***********************************************************************
2472 * RtlQueryInformationActivationContext (NTDLL.@)
2474 * Get information about an activation context.
2475 * FIXME: function signature/prototype may be wrong
2477 NTSTATUS WINAPI RtlQueryInformationActivationContext( ULONG flags, HANDLE handle, PVOID subinst,
2478 ULONG class, PVOID buffer,
2479 SIZE_T bufsize, SIZE_T *retlen )
2481 ACTIVATION_CONTEXT *actctx;
2482 NTSTATUS status;
2484 TRACE("%08x %p %p %u %p %ld %p\n", flags, handle,
2485 subinst, class, buffer, bufsize, retlen);
2487 if (retlen) *retlen = 0;
2488 if ((status = find_query_actctx( &handle, flags, class ))) return status;
2490 switch (class)
2492 case ActivationContextBasicInformation:
2494 ACTIVATION_CONTEXT_BASIC_INFORMATION *info = buffer;
2496 if (retlen) *retlen = sizeof(*info);
2497 if (!info || bufsize < sizeof(*info)) return STATUS_BUFFER_TOO_SMALL;
2499 info->hActCtx = handle;
2500 info->dwFlags = 0; /* FIXME */
2501 if (!(flags & QUERY_ACTCTX_FLAG_NO_ADDREF)) RtlAddRefActivationContext( handle );
2503 break;
2505 case ActivationContextDetailedInformation:
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;
2512 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2514 if (actctx->num_assemblies) assembly = actctx->assemblies;
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);
2522 if (retlen) *retlen = len;
2523 if (!buffer || bufsize < len) return STATUS_BUFFER_TOO_SMALL;
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)
2537 acdi->lpRootManifestPath = ptr;
2538 memcpy(ptr, assembly->manifest.info, manifest_len * sizeof(WCHAR));
2539 ptr += manifest_len;
2541 else acdi->lpRootManifestPath = NULL;
2542 if (config_len)
2544 acdi->lpRootConfigurationPath = ptr;
2545 memcpy(ptr, actctx->config.info, config_len * sizeof(WCHAR));
2546 ptr += config_len;
2548 else acdi->lpRootConfigurationPath = NULL;
2549 if (appdir_len)
2551 acdi->lpAppDirPath = ptr;
2552 memcpy(ptr, actctx->appdir.info, appdir_len * sizeof(WCHAR));
2554 else acdi->lpAppDirPath = NULL;
2556 break;
2558 case AssemblyDetailedInformationInActivationContext:
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;
2567 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2568 if (!subinst) return STATUS_INVALID_PARAMETER;
2570 index = *(DWORD*)subinst;
2571 if (!index || index > actctx->num_assemblies) return STATUS_INVALID_PARAMETER;
2573 assembly = &actctx->assemblies[index - 1];
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;
2579 if (assembly->manifest.info &&
2580 (assembly->type == ASSEMBLY_MANIFEST || assembly->type == ASSEMBLY_SHARED_MANIFEST))
2581 path_len = strlenW(assembly->manifest.info) + 1;
2583 len = sizeof(*afdi) + (id_len + ad_len + path_len) * sizeof(WCHAR);
2585 if (retlen) *retlen = len;
2586 if (!buffer || bufsize < len)
2588 RtlFreeHeap( GetProcessHeap(), 0, assembly_id );
2589 return STATUS_BUFFER_TOO_SMALL;
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)
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)
2619 afdi->lpAssemblyDirectoryName = ptr;
2620 memcpy(ptr, assembly->directory, ad_len * sizeof(WCHAR));
2622 else afdi->lpAssemblyDirectoryName = NULL;
2623 RtlFreeHeap( GetProcessHeap(), 0, assembly_id );
2625 break;
2627 case FileInformationInAssemblyOfAssemblyInActivationContext:
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;
2636 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2637 if (!acqi) return STATUS_INVALID_PARAMETER;
2639 if (acqi->ulAssemblyIndex >= actctx->num_assemblies)
2640 return STATUS_INVALID_PARAMETER;
2641 assembly = &actctx->assemblies[acqi->ulAssemblyIndex];
2643 if (acqi->ulFileIndexInAssembly >= assembly->num_dlls)
2644 return STATUS_INVALID_PARAMETER;
2645 dll = &assembly->dlls[acqi->ulFileIndexInAssembly];
2647 if (dll->name) dll_len = strlenW(dll->name) + 1;
2648 len = sizeof(*afdi) + dll_len * sizeof(WCHAR);
2650 if (!buffer || bufsize < len)
2652 if (retlen) *retlen = len;
2653 return STATUS_BUFFER_TOO_SMALL;
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)
2662 afdi->lpFileName = ptr;
2663 memcpy( ptr, dll->name, dll_len * sizeof(WCHAR) );
2664 } else afdi->lpFileName = NULL;
2665 afdi->lpFilePath = NULL; /* FIXME */
2667 break;
2669 default:
2670 FIXME( "class %u not implemented\n", class );
2671 return STATUS_NOT_IMPLEMENTED;
2673 return STATUS_SUCCESS;
2676 /***********************************************************************
2677 * RtlFindActivationContextSectionString (NTDLL.@)
2679 * Find information about a string in an activation context.
2680 * FIXME: function signature/prototype may be wrong
2682 NTSTATUS WINAPI RtlFindActivationContextSectionString( ULONG flags, const GUID *guid, ULONG section_kind,
2683 const UNICODE_STRING *section_name, PVOID ptr )
2685 PACTCTX_SECTION_KEYED_DATA data = ptr;
2686 NTSTATUS status = STATUS_SXS_KEY_NOT_FOUND;
2688 TRACE("%08x %s %u %s %p\n", flags, debugstr_guid(guid), section_kind,
2689 debugstr_us(section_name), data);
2691 if (guid)
2693 FIXME("expected guid == NULL\n");
2694 return STATUS_INVALID_PARAMETER;
2696 if (flags & ~FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX)
2698 FIXME("unknown flags %08x\n", flags);
2699 return STATUS_INVALID_PARAMETER;
2701 if (!data || data->cbSize < offsetof(ACTCTX_SECTION_KEYED_DATA, ulAssemblyRosterIndex) ||
2702 !section_name || !section_name->Buffer)
2704 WARN("invalid parameter\n");
2705 return STATUS_INVALID_PARAMETER;
2708 if (NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2710 ACTIVATION_CONTEXT *actctx = check_actctx(NtCurrentTeb()->ActivationContextStack.ActiveFrame->ActivationContext);
2711 if (actctx) status = find_string( actctx, section_kind, section_name, flags, data );
2714 if (status != STATUS_SUCCESS)
2715 status = find_string( process_actctx, section_kind, section_name, flags, data );
2717 return status;