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
25 #include "wine/port.h"
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
33 #define WIN32_NO_STATUS
35 #include "ntdll_misc.h"
36 #include "wine/exception.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(actctx);
42 #define ACTCTX_FLAGS_ALL (\
43 ACTCTX_FLAG_PROCESSOR_ARCHITECTURE_VALID |\
44 ACTCTX_FLAG_LANGID_VALID |\
45 ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID |\
46 ACTCTX_FLAG_RESOURCE_NAME_VALID |\
47 ACTCTX_FLAG_SET_PROCESS_DEFAULT |\
48 ACTCTX_FLAG_APPLICATION_NAME_VALID |\
49 ACTCTX_FLAG_SOURCE_IS_ASSEMBLYREF |\
50 ACTCTX_FLAG_HMODULE_VALID )
52 #define ACTCTX_MAGIC 0xC07E3E11
54 /* we don't want to include winuser.h */
55 #define RT_MANIFEST ((ULONG_PTR)24)
56 #define CREATEPROCESS_MANIFEST_RESOURCE_ID ((ULONG_PTR)1)
76 struct assembly_version
84 struct assembly_identity
91 struct assembly_version version;
135 unsigned int allocated;
142 struct entity_array entities;
147 APPLICATION_MANIFEST,
149 ASSEMBLY_SHARED_MANIFEST,
154 enum assembly_type type;
155 struct assembly_identity id;
156 struct file_info manifest;
159 struct dll_redirect *dlls;
160 unsigned int num_dlls;
161 unsigned int allocated_dlls;
162 struct entity_array entities;
165 typedef struct _ACTIVATION_CONTEXT
169 struct file_info config;
170 struct file_info appdir;
171 struct assembly *assemblies;
172 unsigned int num_assemblies;
173 unsigned int allocated_assemblies;
174 } ACTIVATION_CONTEXT;
178 ACTIVATION_CONTEXT *actctx;
179 struct assembly_identity *dependencies;
180 unsigned int num_dependencies;
181 unsigned int allocated_dependencies;
184 static const WCHAR assemblyW[] = {'a','s','s','e','m','b','l','y',0};
185 static const WCHAR assemblyIdentityW[] = {'a','s','s','e','m','b','l','y','I','d','e','n','t','i','t','y',0};
186 static const WCHAR bindingRedirectW[] = {'b','i','n','d','i','n','g','R','e','d','i','r','e','c','t',0};
187 static const WCHAR clrClassW[] = {'c','l','r','C','l','a','s','s',0};
188 static const WCHAR clrSurrogateW[] = {'c','l','r','S','u','r','r','o','g','a','t','e',0};
189 static const WCHAR comClassW[] = {'c','o','m','C','l','a','s','s',0};
190 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};
191 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};
192 static const WCHAR dependencyW[] = {'d','e','p','e','n','d','e','n','c','y',0};
193 static const WCHAR dependentAssemblyW[] = {'d','e','p','e','n','d','e','n','t','A','s','s','e','m','b','l','y',0};
194 static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
195 static const WCHAR fileW[] = {'f','i','l','e',0};
196 static const WCHAR asmv2hashW[] = {'a','s','m','v','2',':','h','a','s','h',0};
197 static const WCHAR noInheritW[] = {'n','o','I','n','h','e','r','i','t',0};
198 static const WCHAR noInheritableW[] = {'n','o','I','n','h','e','r','i','t','a','b','l','e',0};
199 static const WCHAR typelibW[] = {'t','y','p','e','l','i','b',0};
200 static const WCHAR windowClassW[] = {'w','i','n','d','o','w','C','l','a','s','s',0};
202 static const WCHAR clsidW[] = {'c','l','s','i','d',0};
203 static const WCHAR hashW[] = {'h','a','s','h',0};
204 static const WCHAR hashalgW[] = {'h','a','s','h','a','l','g',0};
205 static const WCHAR helpdirW[] = {'h','e','l','p','d','i','r',0};
206 static const WCHAR iidW[] = {'i','i','d',0};
207 static const WCHAR languageW[] = {'l','a','n','g','u','a','g','e',0};
208 static const WCHAR manifestVersionW[] = {'m','a','n','i','f','e','s','t','V','e','r','s','i','o','n',0};
209 static const WCHAR nameW[] = {'n','a','m','e',0};
210 static const WCHAR newVersionW[] = {'n','e','w','V','e','r','s','i','o','n',0};
211 static const WCHAR oldVersionW[] = {'o','l','d','V','e','r','s','i','o','n',0};
212 static const WCHAR optionalW[] = {'o','p','t','i','o','n','a','l',0};
213 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};
214 static const WCHAR publicKeyTokenW[] = {'p','u','b','l','i','c','K','e','y','T','o','k','e','n',0};
215 static const WCHAR tlbidW[] = {'t','l','b','i','d',0};
216 static const WCHAR typeW[] = {'t','y','p','e',0};
217 static const WCHAR versionW[] = {'v','e','r','s','i','o','n',0};
218 static const WCHAR xmlnsW[] = {'x','m','l','n','s',0};
220 static const WCHAR xmlW[] = {'?','x','m','l',0};
221 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};
222 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};
224 static const WCHAR dotManifestW[] = {'.','m','a','n','i','f','e','s','t',0};
225 static const WCHAR version_formatW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
227 static ACTIVATION_CONTEXT system_actctx = { ACTCTX_MAGIC, 1 };
228 static ACTIVATION_CONTEXT *process_actctx = &system_actctx;
230 static WCHAR *strdupW(const WCHAR* str)
234 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(WCHAR))))
236 return strcpyW(ptr, str);
239 static WCHAR *xmlstrdupW(const xmlstr_t* str)
243 if ((strW = RtlAllocateHeap(GetProcessHeap(), 0, (str->len + 1) * sizeof(WCHAR))))
245 memcpy( strW, str->ptr, str->len * sizeof(WCHAR) );
251 static inline BOOL xmlstr_cmp(const xmlstr_t* xmlstr, const WCHAR *str)
253 return !strncmpW(xmlstr->ptr, str, xmlstr->len) && !str[xmlstr->len];
256 static inline BOOL xmlstr_cmpi(const xmlstr_t* xmlstr, const WCHAR *str)
258 return !strncmpiW(xmlstr->ptr, str, xmlstr->len) && !str[xmlstr->len];
261 static inline BOOL xmlstr_cmp_end(const xmlstr_t* xmlstr, const WCHAR *str)
263 return (xmlstr->len && xmlstr->ptr[0] == '/' &&
264 !strncmpW(xmlstr->ptr + 1, str, xmlstr->len - 1) && !str[xmlstr->len - 1]);
267 static inline BOOL isxmlspace( WCHAR ch )
269 return (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t');
272 static inline const char* debugstr_xmlstr(const xmlstr_t* str)
274 return debugstr_wn(str->ptr, str->len);
277 static inline const char* debugstr_version(const struct assembly_version *ver)
279 return wine_dbg_sprintf("%u.%u.%u.%u", ver->major, ver->minor, ver->build, ver->revision);
282 static struct assembly *add_assembly(ACTIVATION_CONTEXT *actctx, enum assembly_type at)
284 struct assembly *assembly;
286 if (actctx->num_assemblies == actctx->allocated_assemblies)
289 unsigned int new_count;
290 if (actctx->assemblies)
292 new_count = actctx->allocated_assemblies * 2;
293 ptr = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
294 actctx->assemblies, new_count * sizeof(*assembly) );
299 ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*assembly) );
301 if (!ptr) return NULL;
302 actctx->assemblies = ptr;
303 actctx->allocated_assemblies = new_count;
306 assembly = &actctx->assemblies[actctx->num_assemblies++];
311 static struct dll_redirect* add_dll_redirect(struct assembly* assembly)
313 if (assembly->num_dlls == assembly->allocated_dlls)
316 unsigned int new_count;
319 new_count = assembly->allocated_dlls * 2;
320 ptr = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
321 assembly->dlls, new_count * sizeof(*assembly->dlls) );
326 ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*assembly->dlls) );
328 if (!ptr) return NULL;
329 assembly->dlls = ptr;
330 assembly->allocated_dlls = new_count;
332 return &assembly->dlls[assembly->num_dlls++];
335 static void free_assembly_identity(struct assembly_identity *ai)
337 RtlFreeHeap( GetProcessHeap(), 0, ai->name );
338 RtlFreeHeap( GetProcessHeap(), 0, ai->arch );
339 RtlFreeHeap( GetProcessHeap(), 0, ai->public_key );
340 RtlFreeHeap( GetProcessHeap(), 0, ai->language );
343 static struct entity* add_entity(struct entity_array *array, DWORD kind)
345 struct entity* entity;
347 if (array->num == array->allocated)
350 unsigned int new_count;
353 new_count = array->allocated * 2;
354 ptr = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
355 array->base, new_count * sizeof(*array->base) );
360 ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*array->base) );
362 if (!ptr) return NULL;
364 array->allocated = new_count;
366 entity = &array->base[array->num++];
371 static void free_entity_array(struct entity_array *array)
374 for (i = 0; i < array->num; i++)
376 struct entity *entity = &array->base[i];
377 switch (entity->kind)
379 case ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION:
380 RtlFreeHeap(GetProcessHeap(), 0, entity->u.comclass.clsid);
382 case ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION:
383 RtlFreeHeap(GetProcessHeap(), 0, entity->u.proxy.iid);
384 RtlFreeHeap(GetProcessHeap(), 0, entity->u.proxy.name);
386 case ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION:
387 RtlFreeHeap(GetProcessHeap(), 0, entity->u.typelib.tlbid);
388 RtlFreeHeap(GetProcessHeap(), 0, entity->u.typelib.version);
389 RtlFreeHeap(GetProcessHeap(), 0, entity->u.typelib.helpdir);
391 case ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION:
392 RtlFreeHeap(GetProcessHeap(), 0, entity->u.class.name);
394 case ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION:
395 RtlFreeHeap(GetProcessHeap(), 0, entity->u.clrclass.name);
396 RtlFreeHeap(GetProcessHeap(), 0, entity->u.clrclass.clsid);
398 case ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES:
399 RtlFreeHeap(GetProcessHeap(), 0, entity->u.clrsurrogate.name);
400 RtlFreeHeap(GetProcessHeap(), 0, entity->u.clrsurrogate.clsid);
403 FIXME("Unknown entity kind %d\n", entity->kind);
406 RtlFreeHeap( GetProcessHeap(), 0, array->base );
409 static BOOL is_matching_string( const WCHAR *str1, const WCHAR *str2 )
411 if (!str1) return !str2;
412 return str2 && !strcmpiW( str1, str2 );
415 static BOOL is_matching_identity( const struct assembly_identity *id1,
416 const struct assembly_identity *id2 )
418 if (!is_matching_string( id1->name, id2->name )) return FALSE;
419 if (!is_matching_string( id1->arch, id2->arch )) return FALSE;
420 if (!is_matching_string( id1->public_key, id2->public_key )) return FALSE;
422 if (id1->language && id2->language && strcmpiW( id1->language, id2->language ))
424 static const WCHAR wildcardW[] = {'*',0};
425 if (strcmpW( wildcardW, id1->language ) && strcmpW( wildcardW, id2->language ))
428 if (id1->version.major != id2->version.major) return FALSE;
429 if (id1->version.minor != id2->version.minor) return FALSE;
430 if (id1->version.build > id2->version.build) return FALSE;
431 if (id1->version.build == id2->version.build &&
432 id1->version.revision > id2->version.revision) return FALSE;
436 static BOOL add_dependent_assembly_id(struct actctx_loader* acl,
437 struct assembly_identity* ai)
441 /* check if we already have that assembly */
443 for (i = 0; i < acl->actctx->num_assemblies; i++)
444 if (is_matching_identity( ai, &acl->actctx->assemblies[i].id ))
446 TRACE( "reusing existing assembly for %s arch %s version %u.%u.%u.%u\n",
447 debugstr_w(ai->name), debugstr_w(ai->arch), ai->version.major, ai->version.minor,
448 ai->version.build, ai->version.revision );
452 for (i = 0; i < acl->num_dependencies; i++)
453 if (is_matching_identity( ai, &acl->dependencies[i] ))
455 TRACE( "reusing existing dependency for %s arch %s version %u.%u.%u.%u\n",
456 debugstr_w(ai->name), debugstr_w(ai->arch), ai->version.major, ai->version.minor,
457 ai->version.build, ai->version.revision );
461 if (acl->num_dependencies == acl->allocated_dependencies)
464 unsigned int new_count;
465 if (acl->dependencies)
467 new_count = acl->allocated_dependencies * 2;
468 ptr = RtlReAllocateHeap(GetProcessHeap(), 0, acl->dependencies,
469 new_count * sizeof(acl->dependencies[0]));
474 ptr = RtlAllocateHeap(GetProcessHeap(), 0, new_count * sizeof(acl->dependencies[0]));
476 if (!ptr) return FALSE;
477 acl->dependencies = ptr;
478 acl->allocated_dependencies = new_count;
480 acl->dependencies[acl->num_dependencies++] = *ai;
485 static void free_depend_manifests(struct actctx_loader* acl)
488 for (i = 0; i < acl->num_dependencies; i++)
489 free_assembly_identity(&acl->dependencies[i]);
490 RtlFreeHeap(GetProcessHeap(), 0, acl->dependencies);
493 static WCHAR *build_assembly_dir(struct assembly_identity* ai)
495 static const WCHAR undW[] = {'_',0};
496 static const WCHAR noneW[] = {'n','o','n','e',0};
497 static const WCHAR mskeyW[] = {'d','e','a','d','b','e','e','f',0};
499 const WCHAR *key = ai->public_key ? ai->public_key : noneW;
500 const WCHAR *lang = ai->language ? ai->language : noneW;
501 SIZE_T size = (strlenW(ai->arch) + 1 + strlenW(ai->name) + 1 + strlenW(key) + 24 + 1 +
502 strlenW(lang) + 1) * sizeof(WCHAR) + sizeof(mskeyW);
505 if (!(ret = RtlAllocateHeap( GetProcessHeap(), 0, size ))) return NULL;
507 strcpyW( ret, ai->arch );
508 strcatW( ret, undW );
509 strcatW( ret, ai->name );
510 strcatW( ret, undW );
512 strcatW( ret, undW );
513 sprintfW( ret + strlenW(ret), version_formatW,
514 ai->version.major, ai->version.minor, ai->version.build, ai->version.revision );
515 strcatW( ret, undW );
516 strcatW( ret, lang );
517 strcatW( ret, undW );
518 strcatW( ret, mskeyW );
522 static inline void append_string( WCHAR *buffer, const WCHAR *prefix, const WCHAR *str )
527 strcatW( buffer, prefix );
536 static WCHAR *build_assembly_id( const struct assembly_identity *ai )
538 static const WCHAR archW[] =
539 {',','p','r','o','c','e','s','s','o','r','A','r','c','h','i','t','e','c','t','u','r','e','=',0};
540 static const WCHAR public_keyW[] =
541 {',','p','u','b','l','i','c','K','e','y','T','o','k','e','n','=',0};
542 static const WCHAR typeW[] =
543 {',','t','y','p','e','=',0};
544 static const WCHAR versionW[] =
545 {',','v','e','r','s','i','o','n','=',0};
547 WCHAR version[64], *ret;
550 sprintfW( version, version_formatW,
551 ai->version.major, ai->version.minor, ai->version.build, ai->version.revision );
552 if (ai->name) size += strlenW(ai->name) * sizeof(WCHAR);
553 if (ai->arch) size += strlenW(archW) + strlenW(ai->arch) + 2;
554 if (ai->public_key) size += strlenW(public_keyW) + strlenW(ai->public_key) + 2;
555 if (ai->type) size += strlenW(typeW) + strlenW(ai->type) + 2;
556 size += strlenW(versionW) + strlenW(version) + 2;
558 if (!(ret = RtlAllocateHeap( GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR) )))
561 if (ai->name) strcpyW( ret, ai->name );
563 append_string( ret, archW, ai->arch );
564 append_string( ret, public_keyW, ai->public_key );
565 append_string( ret, typeW, ai->type );
566 append_string( ret, versionW, version );
570 static ACTIVATION_CONTEXT *check_actctx( HANDLE h )
572 ACTIVATION_CONTEXT *ret = NULL, *actctx = h;
574 if (!h || h == INVALID_HANDLE_VALUE) return NULL;
577 if (actctx->magic == ACTCTX_MAGIC) ret = actctx;
586 static inline void actctx_addref( ACTIVATION_CONTEXT *actctx )
588 interlocked_xchg_add( &actctx->ref_count, 1 );
591 static void actctx_release( ACTIVATION_CONTEXT *actctx )
593 if (interlocked_xchg_add( &actctx->ref_count, -1 ) == 1)
597 for (i = 0; i < actctx->num_assemblies; i++)
599 struct assembly *assembly = &actctx->assemblies[i];
600 for (j = 0; j < assembly->num_dlls; j++)
602 struct dll_redirect *dll = &assembly->dlls[j];
603 free_entity_array( &dll->entities );
604 RtlFreeHeap( GetProcessHeap(), 0, dll->name );
605 RtlFreeHeap( GetProcessHeap(), 0, dll->hash );
607 RtlFreeHeap( GetProcessHeap(), 0, assembly->dlls );
608 RtlFreeHeap( GetProcessHeap(), 0, assembly->manifest.info );
609 RtlFreeHeap( GetProcessHeap(), 0, assembly->directory );
610 free_entity_array( &assembly->entities );
611 free_assembly_identity(&assembly->id);
613 RtlFreeHeap( GetProcessHeap(), 0, actctx->config.info );
614 RtlFreeHeap( GetProcessHeap(), 0, actctx->appdir.info );
615 RtlFreeHeap( GetProcessHeap(), 0, actctx->assemblies );
617 RtlFreeHeap( GetProcessHeap(), 0, actctx );
621 static BOOL next_xml_attr(xmlbuf_t* xmlbuf, xmlstr_t* name, xmlstr_t* value,
622 BOOL* error, BOOL* end)
628 while (xmlbuf->ptr < xmlbuf->end && isxmlspace(*xmlbuf->ptr))
631 if (xmlbuf->ptr == xmlbuf->end) return FALSE;
633 if (*xmlbuf->ptr == '/')
636 if (xmlbuf->ptr == xmlbuf->end || *xmlbuf->ptr != '>')
645 if (*xmlbuf->ptr == '>')
653 while (ptr < xmlbuf->end && *ptr != '=' && *ptr != '>' && !isxmlspace(*ptr)) ptr++;
655 if (ptr == xmlbuf->end || *ptr != '=') return FALSE;
657 name->ptr = xmlbuf->ptr;
658 name->len = ptr-xmlbuf->ptr;
662 if (ptr == xmlbuf->end || (*ptr != '"' && *ptr != '\'')) return FALSE;
665 if (ptr == xmlbuf->end) return FALSE;
667 ptr = memchrW(ptr, ptr[-1], xmlbuf->end - ptr);
670 xmlbuf->ptr = xmlbuf->end;
674 value->len = ptr - value->ptr;
675 xmlbuf->ptr = ptr + 1;
677 if (xmlbuf->ptr == xmlbuf->end) return FALSE;
683 static BOOL next_xml_elem(xmlbuf_t* xmlbuf, xmlstr_t* elem)
689 ptr = memchrW(xmlbuf->ptr, '<', xmlbuf->end - xmlbuf->ptr);
692 xmlbuf->ptr = xmlbuf->end;
696 if (ptr + 3 < xmlbuf->end && ptr[0] == '!' && ptr[1] == '-' && ptr[2] == '-') /* skip comment */
698 for (ptr += 3; ptr + 3 <= xmlbuf->end; ptr++)
699 if (ptr[0] == '-' && ptr[1] == '-' && ptr[2] == '>') break;
701 if (ptr + 3 > xmlbuf->end)
703 xmlbuf->ptr = xmlbuf->end;
706 xmlbuf->ptr = ptr + 3;
712 while (ptr < xmlbuf->end && !isxmlspace(*ptr) && *ptr != '>' && (*ptr != '/' || ptr == xmlbuf->ptr))
715 elem->ptr = xmlbuf->ptr;
716 elem->len = ptr - xmlbuf->ptr;
718 return xmlbuf->ptr != xmlbuf->end;
721 static BOOL parse_xml_header(xmlbuf_t* xmlbuf)
723 /* FIXME: parse attributes */
726 for (ptr = xmlbuf->ptr; ptr < xmlbuf->end - 1; ptr++)
728 if (ptr[0] == '?' && ptr[1] == '>')
730 xmlbuf->ptr = ptr + 2;
737 static BOOL parse_text_content(xmlbuf_t* xmlbuf, xmlstr_t* content)
739 const WCHAR *ptr = memchrW(xmlbuf->ptr, '<', xmlbuf->end - xmlbuf->ptr);
741 if (!ptr) return FALSE;
743 content->ptr = xmlbuf->ptr;
744 content->len = ptr - xmlbuf->ptr;
750 static BOOL parse_version(const xmlstr_t *str, struct assembly_version *version)
756 /* major.minor.build.revision */
757 ver[0] = ver[1] = ver[2] = ver[3] = pos = 0;
758 for (curr = str->ptr; curr < str->ptr + str->len; curr++)
760 if (*curr >= '0' && *curr <= '9')
762 ver[pos] = ver[pos] * 10 + *curr - '0';
763 if (ver[pos] >= 0x10000) goto error;
765 else if (*curr == '.')
767 if (++pos >= 4) goto error;
771 version->major = ver[0];
772 version->minor = ver[1];
773 version->build = ver[2];
774 version->revision = ver[3];
778 FIXME( "Wrong version definition in manifest file (%s)\n", debugstr_xmlstr(str) );
782 static BOOL parse_expect_elem(xmlbuf_t* xmlbuf, const WCHAR* name)
785 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
786 if (xmlstr_cmp(&elem, name)) return TRUE;
787 FIXME( "unexpected element %s\n", debugstr_xmlstr(&elem) );
791 static BOOL parse_expect_no_attr(xmlbuf_t* xmlbuf, BOOL* end)
793 xmlstr_t attr_name, attr_value;
796 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, end))
798 WARN("unexpected attr %s=%s\n", debugstr_xmlstr(&attr_name),
799 debugstr_xmlstr(&attr_value));
804 static BOOL parse_end_element(xmlbuf_t *xmlbuf)
807 return parse_expect_no_attr(xmlbuf, &end) && !end;
810 static BOOL parse_expect_end_elem(xmlbuf_t *xmlbuf, const WCHAR *name)
813 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
814 if (!xmlstr_cmp_end(&elem, name))
816 FIXME( "unexpected element %s\n", debugstr_xmlstr(&elem) );
819 return parse_end_element(xmlbuf);
822 static BOOL parse_unknown_elem(xmlbuf_t *xmlbuf, const xmlstr_t *unknown_elem)
824 xmlstr_t attr_name, attr_value, elem;
825 BOOL end = FALSE, error, ret = TRUE;
827 while(next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end));
828 if(error || end) return end;
830 while(ret && (ret = next_xml_elem(xmlbuf, &elem)))
832 if(*elem.ptr == '/' && elem.len - 1 == unknown_elem->len &&
833 !strncmpW(elem.ptr+1, unknown_elem->ptr, unknown_elem->len))
836 ret = parse_unknown_elem(xmlbuf, &elem);
839 return ret && parse_end_element(xmlbuf);
842 static BOOL parse_assembly_identity_elem(xmlbuf_t* xmlbuf, ACTIVATION_CONTEXT* actctx,
843 struct assembly_identity* ai)
845 xmlstr_t attr_name, attr_value;
846 BOOL end = FALSE, error;
848 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
850 if (xmlstr_cmp(&attr_name, nameW))
852 if (!(ai->name = xmlstrdupW(&attr_value))) return FALSE;
854 else if (xmlstr_cmp(&attr_name, typeW))
856 if (!(ai->type = xmlstrdupW(&attr_value))) return FALSE;
858 else if (xmlstr_cmp(&attr_name, versionW))
860 if (!parse_version(&attr_value, &ai->version)) return FALSE;
862 else if (xmlstr_cmp(&attr_name, processorArchitectureW))
864 if (!(ai->arch = xmlstrdupW(&attr_value))) return FALSE;
866 else if (xmlstr_cmp(&attr_name, publicKeyTokenW))
868 if (!(ai->public_key = xmlstrdupW(&attr_value))) return FALSE;
870 else if (xmlstr_cmp(&attr_name, languageW))
872 WARN("Unsupported yet language attribute (%s)\n",
873 debugstr_xmlstr(&attr_value));
874 if (!(ai->language = xmlstrdupW(&attr_value))) return FALSE;
878 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name),
879 debugstr_xmlstr(&attr_value));
883 TRACE( "name=%s version=%s arch=%s\n",
884 debugstr_w(ai->name), debugstr_version(&ai->version), debugstr_w(ai->arch) );
886 if (error || end) return end;
887 return parse_expect_end_elem(xmlbuf, assemblyIdentityW);
890 static BOOL parse_com_class_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
892 xmlstr_t elem, attr_name, attr_value;
893 BOOL ret, end = FALSE, error;
894 struct entity* entity;
896 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION)))
899 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
901 if (xmlstr_cmp(&attr_name, clsidW))
903 if (!(entity->u.comclass.clsid = xmlstrdupW(&attr_value))) return FALSE;
907 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
911 if (error || end) return end;
913 while ((ret = next_xml_elem(xmlbuf, &elem)))
915 if (xmlstr_cmp_end(&elem, comClassW))
917 ret = parse_end_element(xmlbuf);
922 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
923 ret = parse_unknown_elem(xmlbuf, &elem);
929 static BOOL parse_cominterface_proxy_stub_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
931 xmlstr_t attr_name, attr_value;
932 BOOL end = FALSE, error;
933 struct entity* entity;
935 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION)))
938 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
940 if (xmlstr_cmp(&attr_name, iidW))
942 if (!(entity->u.proxy.iid = xmlstrdupW(&attr_value))) return FALSE;
944 if (xmlstr_cmp(&attr_name, nameW))
946 if (!(entity->u.proxy.name = xmlstrdupW(&attr_value))) return FALSE;
950 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
954 if (error || end) return end;
955 return parse_expect_end_elem(xmlbuf, comInterfaceProxyStubW);
958 static BOOL parse_typelib_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
960 xmlstr_t attr_name, attr_value;
961 BOOL end = FALSE, error;
962 struct entity* entity;
964 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION)))
967 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
969 if (xmlstr_cmp(&attr_name, tlbidW))
971 if (!(entity->u.typelib.tlbid = xmlstrdupW(&attr_value))) return FALSE;
973 if (xmlstr_cmp(&attr_name, versionW))
975 if (!(entity->u.typelib.version = xmlstrdupW(&attr_value))) return FALSE;
977 if (xmlstr_cmp(&attr_name, helpdirW))
979 if (!(entity->u.typelib.helpdir = xmlstrdupW(&attr_value))) return FALSE;
983 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
987 if (error || end) return end;
988 return parse_expect_end_elem(xmlbuf, typelibW);
991 static BOOL parse_window_class_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
993 xmlstr_t elem, content;
994 BOOL end = FALSE, ret = TRUE;
995 struct entity* entity;
997 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION)))
1000 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1001 if (end) return FALSE;
1003 if (!parse_text_content(xmlbuf, &content)) return FALSE;
1005 if (!(entity->u.class.name = xmlstrdupW(&content))) return FALSE;
1007 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1009 if (xmlstr_cmp_end(&elem, windowClassW))
1011 ret = parse_end_element(xmlbuf);
1016 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1017 ret = parse_unknown_elem(xmlbuf, &elem);
1024 static BOOL parse_binding_redirect_elem(xmlbuf_t* xmlbuf)
1026 xmlstr_t attr_name, attr_value;
1027 BOOL end = FALSE, error;
1029 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1031 if (xmlstr_cmp(&attr_name, oldVersionW))
1033 FIXME("Not stored yet oldVersion=%s\n", debugstr_xmlstr(&attr_value));
1035 else if (xmlstr_cmp(&attr_name, newVersionW))
1037 FIXME("Not stored yet newVersion=%s\n", debugstr_xmlstr(&attr_value));
1041 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1045 if (error || end) return end;
1046 return parse_expect_end_elem(xmlbuf, bindingRedirectW);
1049 static BOOL parse_description_elem(xmlbuf_t* xmlbuf)
1051 xmlstr_t elem, content;
1052 BOOL end = FALSE, ret = TRUE;
1054 if (!parse_expect_no_attr(xmlbuf, &end) || end ||
1055 !parse_text_content(xmlbuf, &content))
1058 TRACE("Got description %s\n", debugstr_xmlstr(&content));
1060 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1062 if (xmlstr_cmp_end(&elem, descriptionW))
1064 ret = parse_end_element(xmlbuf);
1069 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1070 ret = parse_unknown_elem(xmlbuf, &elem);
1077 static BOOL parse_com_interface_external_proxy_stub_elem(xmlbuf_t* xmlbuf,
1078 struct assembly* assembly)
1080 xmlstr_t attr_name, attr_value;
1081 BOOL end = FALSE, error;
1082 struct entity* entity;
1084 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION);
1085 if (!entity) return FALSE;
1087 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1089 if (xmlstr_cmp(&attr_name, iidW))
1091 if (!(entity->u.proxy.iid = xmlstrdupW(&attr_value))) return FALSE;
1093 if (xmlstr_cmp(&attr_name, nameW))
1095 if (!(entity->u.proxy.name = xmlstrdupW(&attr_value))) return FALSE;
1099 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1103 if (error || end) return end;
1104 return parse_expect_end_elem(xmlbuf, comInterfaceExternalProxyStubW);
1107 static BOOL parse_clr_class_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1109 xmlstr_t attr_name, attr_value;
1110 BOOL end = FALSE, error;
1111 struct entity* entity;
1113 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION);
1114 if (!entity) return FALSE;
1116 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1118 if (xmlstr_cmp(&attr_name, nameW))
1120 if (!(entity->u.clrclass.name = xmlstrdupW(&attr_value))) return FALSE;
1122 else if (xmlstr_cmp(&attr_name, clsidW))
1124 if (!(entity->u.clrclass.clsid = xmlstrdupW(&attr_value))) return FALSE;
1128 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1132 if (error || end) return end;
1133 return parse_expect_end_elem(xmlbuf, clrClassW);
1136 static BOOL parse_clr_surrogate_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1138 xmlstr_t attr_name, attr_value;
1139 BOOL end = FALSE, error;
1140 struct entity* entity;
1142 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES);
1143 if (!entity) return FALSE;
1145 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1147 if (xmlstr_cmp(&attr_name, nameW))
1149 if (!(entity->u.clrsurrogate.name = xmlstrdupW(&attr_value))) return FALSE;
1151 else if (xmlstr_cmp(&attr_name, clsidW))
1153 if (!(entity->u.clrsurrogate.clsid = xmlstrdupW(&attr_value))) return FALSE;
1157 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1161 if (error || end) return end;
1162 return parse_expect_end_elem(xmlbuf, clrSurrogateW);
1165 static BOOL parse_dependent_assembly_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl, BOOL optional)
1167 struct assembly_identity ai;
1169 BOOL end = FALSE, ret = TRUE;
1171 if (!parse_expect_no_attr(xmlbuf, &end) || end) return end;
1173 memset(&ai, 0, sizeof(ai));
1174 ai.optional = optional;
1176 if (!parse_expect_elem(xmlbuf, assemblyIdentityW) ||
1177 !parse_assembly_identity_elem(xmlbuf, acl->actctx, &ai))
1180 TRACE( "adding name=%s version=%s arch=%s\n",
1181 debugstr_w(ai.name), debugstr_version(&ai.version), debugstr_w(ai.arch) );
1183 /* store the newly found identity for later loading */
1184 if (!add_dependent_assembly_id(acl, &ai)) return FALSE;
1186 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1188 if (xmlstr_cmp_end(&elem, dependentAssemblyW))
1190 ret = parse_end_element(xmlbuf);
1193 else if (xmlstr_cmp(&elem, bindingRedirectW))
1195 ret = parse_binding_redirect_elem(xmlbuf);
1199 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1200 ret = parse_unknown_elem(xmlbuf, &elem);
1207 static BOOL parse_dependency_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl)
1209 xmlstr_t attr_name, attr_value, elem;
1210 BOOL end = FALSE, ret = TRUE, error, optional = FALSE;
1212 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1214 if (xmlstr_cmp(&attr_name, optionalW))
1216 static const WCHAR yesW[] = {'y','e','s',0};
1217 optional = xmlstr_cmpi( &attr_value, yesW );
1218 TRACE("optional=%s\n", debugstr_xmlstr(&attr_value));
1222 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1226 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1228 if (xmlstr_cmp_end(&elem, dependencyW))
1230 ret = parse_end_element(xmlbuf);
1233 else if (xmlstr_cmp(&elem, dependentAssemblyW))
1235 ret = parse_dependent_assembly_elem(xmlbuf, acl, optional);
1239 WARN("unknown element %s\n", debugstr_xmlstr(&elem));
1240 ret = parse_unknown_elem(xmlbuf, &elem);
1247 static BOOL parse_noinherit_elem(xmlbuf_t* xmlbuf)
1251 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1252 return end || parse_expect_end_elem(xmlbuf, noInheritW);
1255 static BOOL parse_noinheritable_elem(xmlbuf_t* xmlbuf)
1259 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1260 return end || parse_expect_end_elem(xmlbuf, noInheritableW);
1263 static BOOL parse_file_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1265 xmlstr_t attr_name, attr_value, elem;
1266 BOOL end = FALSE, error, ret = TRUE;
1267 struct dll_redirect* dll;
1269 if (!(dll = add_dll_redirect(assembly))) return FALSE;
1271 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1273 if (xmlstr_cmp(&attr_name, nameW))
1275 if (!(dll->name = xmlstrdupW(&attr_value))) return FALSE;
1276 TRACE("name=%s\n", debugstr_xmlstr(&attr_value));
1278 else if (xmlstr_cmp(&attr_name, hashW))
1280 if (!(dll->hash = xmlstrdupW(&attr_value))) return FALSE;
1282 else if (xmlstr_cmp(&attr_name, hashalgW))
1284 static const WCHAR sha1W[] = {'S','H','A','1',0};
1285 if (!xmlstr_cmpi(&attr_value, sha1W))
1286 FIXME("hashalg should be SHA1, got %s\n", debugstr_xmlstr(&attr_value));
1290 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1294 if (error || !dll->name) return FALSE;
1295 if (end) return TRUE;
1297 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1299 if (xmlstr_cmp_end(&elem, fileW))
1301 ret = parse_end_element(xmlbuf);
1304 else if (xmlstr_cmp(&elem, comClassW))
1306 ret = parse_com_class_elem(xmlbuf, dll);
1308 else if (xmlstr_cmp(&elem, comInterfaceProxyStubW))
1310 ret = parse_cominterface_proxy_stub_elem(xmlbuf, dll);
1312 else if (xmlstr_cmp(&elem, asmv2hashW))
1314 WARN("asmv2hash (undocumented) not supported\n");
1315 ret = parse_unknown_elem(xmlbuf, &elem);
1317 else if (xmlstr_cmp(&elem, typelibW))
1319 ret = parse_typelib_elem(xmlbuf, dll);
1321 else if (xmlstr_cmp(&elem, windowClassW))
1323 ret = parse_window_class_elem(xmlbuf, dll);
1327 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1328 ret = parse_unknown_elem( xmlbuf, &elem );
1335 static BOOL parse_assembly_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl,
1336 struct assembly* assembly,
1337 struct assembly_identity* expected_ai)
1339 xmlstr_t attr_name, attr_value, elem;
1340 BOOL end = FALSE, error, version = FALSE, xmlns = FALSE, ret = TRUE;
1342 TRACE("(%p)\n", xmlbuf);
1344 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1346 if (xmlstr_cmp(&attr_name, manifestVersionW))
1348 static const WCHAR v10W[] = {'1','.','0',0};
1349 if (!xmlstr_cmp(&attr_value, v10W))
1351 FIXME("wrong version %s\n", debugstr_xmlstr(&attr_value));
1356 else if (xmlstr_cmp(&attr_name, xmlnsW))
1358 if (!xmlstr_cmp(&attr_value, manifestv1W) && !xmlstr_cmp(&attr_value, manifestv3W))
1360 FIXME("wrong namespace %s\n", debugstr_xmlstr(&attr_value));
1367 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1371 if (error || end || !xmlns || !version) return FALSE;
1372 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
1374 if (assembly->type == APPLICATION_MANIFEST && xmlstr_cmp(&elem, noInheritW))
1376 if (!parse_noinherit_elem(xmlbuf) || !next_xml_elem(xmlbuf, &elem))
1378 assembly->no_inherit = TRUE;
1381 if (xmlstr_cmp(&elem, noInheritableW))
1383 if (!parse_noinheritable_elem(xmlbuf) || !next_xml_elem(xmlbuf, &elem))
1386 else if ((assembly->type == ASSEMBLY_MANIFEST || assembly->type == ASSEMBLY_SHARED_MANIFEST) &&
1387 assembly->no_inherit)
1390 if (xmlstr_cmp(&elem, assemblyIdentityW))
1392 if (!parse_assembly_identity_elem(xmlbuf, acl->actctx, &assembly->id)) return FALSE;
1393 ret = next_xml_elem(xmlbuf, &elem);
1397 /* FIXME: more tests */
1398 if (assembly->type == ASSEMBLY_MANIFEST &&
1399 memcmp(&assembly->id.version, &expected_ai->version, sizeof(assembly->id.version)))
1401 FIXME("wrong version for assembly manifest\n");
1404 else if (assembly->type == ASSEMBLY_SHARED_MANIFEST &&
1405 (assembly->id.version.major != expected_ai->version.major ||
1406 assembly->id.version.minor != expected_ai->version.minor ||
1407 assembly->id.version.build < expected_ai->version.build ||
1408 (assembly->id.version.build == expected_ai->version.build &&
1409 assembly->id.version.revision < expected_ai->version.revision)))
1411 FIXME("wrong version for shared assembly manifest\n");
1419 if (xmlstr_cmp_end(&elem, assemblyW))
1421 ret = parse_end_element(xmlbuf);
1424 else if (xmlstr_cmp(&elem, descriptionW))
1426 ret = parse_description_elem(xmlbuf);
1428 else if (xmlstr_cmp(&elem, comInterfaceExternalProxyStubW))
1430 ret = parse_com_interface_external_proxy_stub_elem(xmlbuf, assembly);
1432 else if (xmlstr_cmp(&elem, dependencyW))
1434 ret = parse_dependency_elem(xmlbuf, acl);
1436 else if (xmlstr_cmp(&elem, fileW))
1438 ret = parse_file_elem(xmlbuf, assembly);
1440 else if (xmlstr_cmp(&elem, clrClassW))
1442 ret = parse_clr_class_elem(xmlbuf, assembly);
1444 else if (xmlstr_cmp(&elem, clrSurrogateW))
1446 ret = parse_clr_surrogate_elem(xmlbuf, assembly);
1450 WARN("unknown element %s\n", debugstr_xmlstr(&elem));
1451 ret = parse_unknown_elem(xmlbuf, &elem);
1453 if (ret) ret = next_xml_elem(xmlbuf, &elem);
1459 static NTSTATUS parse_manifest_buffer( struct actctx_loader* acl, struct assembly *assembly,
1460 struct assembly_identity* ai, xmlbuf_t *xmlbuf )
1464 if (!next_xml_elem(xmlbuf, &elem)) return STATUS_SXS_CANT_GEN_ACTCTX;
1466 if (xmlstr_cmp(&elem, xmlW) &&
1467 (!parse_xml_header(xmlbuf) || !next_xml_elem(xmlbuf, &elem)))
1468 return STATUS_SXS_CANT_GEN_ACTCTX;
1470 if (!xmlstr_cmp(&elem, assemblyW))
1472 FIXME("root element is %s, not <assembly>\n", debugstr_xmlstr(&elem));
1473 return STATUS_SXS_CANT_GEN_ACTCTX;
1476 if (!parse_assembly_elem(xmlbuf, acl, assembly, ai))
1478 FIXME("failed to parse manifest %s\n", debugstr_w(assembly->manifest.info) );
1479 return STATUS_SXS_CANT_GEN_ACTCTX;
1482 if (next_xml_elem(xmlbuf, &elem))
1484 FIXME("unexpected element %s\n", debugstr_xmlstr(&elem));
1485 return STATUS_SXS_CANT_GEN_ACTCTX;
1488 if (xmlbuf->ptr != xmlbuf->end)
1490 FIXME("parse error\n");
1491 return STATUS_SXS_CANT_GEN_ACTCTX;
1493 return STATUS_SUCCESS;
1496 static NTSTATUS parse_manifest( struct actctx_loader* acl, struct assembly_identity* ai,
1497 LPCWSTR filename, LPCWSTR directory, BOOL shared,
1498 const void *buffer, SIZE_T size )
1502 struct assembly *assembly;
1505 TRACE( "parsing manifest loaded from %s base dir %s\n", debugstr_w(filename), debugstr_w(directory) );
1507 if (!(assembly = add_assembly(acl->actctx, shared ? ASSEMBLY_SHARED_MANIFEST : ASSEMBLY_MANIFEST)))
1508 return STATUS_SXS_CANT_GEN_ACTCTX;
1510 if (directory && !(assembly->directory = strdupW(directory)))
1511 return STATUS_NO_MEMORY;
1513 if (filename) assembly->manifest.info = strdupW( filename + 4 /* skip \??\ prefix */ );
1514 assembly->manifest.type = assembly->manifest.info ? ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE
1515 : ACTIVATION_CONTEXT_PATH_TYPE_NONE;
1517 unicode_tests = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1518 if (RtlIsTextUnicode( buffer, size, &unicode_tests ))
1520 xmlbuf.ptr = buffer;
1521 xmlbuf.end = xmlbuf.ptr + size / sizeof(WCHAR);
1522 status = parse_manifest_buffer( acl, assembly, ai, &xmlbuf );
1524 else if (unicode_tests & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
1526 const WCHAR *buf = buffer;
1530 if (!(new_buff = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1531 return STATUS_NO_MEMORY;
1532 for (i = 0; i < size / sizeof(WCHAR); i++)
1533 new_buff[i] = RtlUshortByteSwap( buf[i] );
1534 xmlbuf.ptr = new_buff;
1535 xmlbuf.end = xmlbuf.ptr + size / sizeof(WCHAR);
1536 status = parse_manifest_buffer( acl, assembly, ai, &xmlbuf );
1537 RtlFreeHeap( GetProcessHeap(), 0, new_buff );
1541 /* let's assume utf-8 for now */
1542 int len = wine_utf8_mbstowcs( 0, buffer, size, NULL, 0 );
1547 FIXME( "utf-8 conversion failed\n" );
1548 return STATUS_SXS_CANT_GEN_ACTCTX;
1550 if (!(new_buff = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1551 return STATUS_NO_MEMORY;
1552 wine_utf8_mbstowcs( 0, buffer, size, new_buff, len );
1553 xmlbuf.ptr = new_buff;
1554 xmlbuf.end = xmlbuf.ptr + len;
1555 status = parse_manifest_buffer( acl, assembly, ai, &xmlbuf );
1556 RtlFreeHeap( GetProcessHeap(), 0, new_buff );
1561 static NTSTATUS open_nt_file( HANDLE *handle, UNICODE_STRING *name )
1563 OBJECT_ATTRIBUTES attr;
1566 attr.Length = sizeof(attr);
1567 attr.RootDirectory = 0;
1568 attr.Attributes = OBJ_CASE_INSENSITIVE;
1569 attr.ObjectName = name;
1570 attr.SecurityDescriptor = NULL;
1571 attr.SecurityQualityOfService = NULL;
1572 return NtOpenFile( handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_ALERT );
1575 static NTSTATUS get_module_filename( HMODULE module, UNICODE_STRING *str, unsigned int extra_len )
1581 LdrLockLoaderLock(0, NULL, &magic);
1582 status = LdrFindEntryForAddress( module, &pldr );
1583 if (status == STATUS_SUCCESS)
1585 if ((str->Buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1586 pldr->FullDllName.Length + extra_len + sizeof(WCHAR) )))
1588 memcpy( str->Buffer, pldr->FullDllName.Buffer, pldr->FullDllName.Length + sizeof(WCHAR) );
1589 str->Length = pldr->FullDllName.Length;
1590 str->MaximumLength = pldr->FullDllName.Length + extra_len + sizeof(WCHAR);
1592 else status = STATUS_NO_MEMORY;
1594 LdrUnlockLoaderLock(0, magic);
1598 static NTSTATUS get_manifest_in_module( struct actctx_loader* acl, struct assembly_identity* ai,
1599 LPCWSTR filename, LPCWSTR directory, BOOL shared,
1600 HANDLE hModule, LPCWSTR resname, ULONG lang )
1603 UNICODE_STRING nameW;
1604 LDR_RESOURCE_INFO info;
1605 const IMAGE_RESOURCE_DATA_ENTRY* entry = NULL;
1608 if (TRACE_ON(actctx))
1610 if (!filename && !get_module_filename( hModule, &nameW, 0 ))
1612 TRACE( "looking for res %s in module %p %s\n", debugstr_w(resname),
1613 hModule, debugstr_w(nameW.Buffer) );
1614 RtlFreeUnicodeString( &nameW );
1616 else TRACE( "looking for res %s in module %p %s\n", debugstr_w(resname),
1617 hModule, debugstr_w(filename) );
1620 if (!resname) return STATUS_INVALID_PARAMETER;
1622 info.Type = RT_MANIFEST;
1623 info.Language = lang;
1624 if (!((ULONG_PTR)resname >> 16))
1626 info.Name = (ULONG_PTR)resname;
1627 status = LdrFindResource_U(hModule, &info, 3, &entry);
1629 else if (resname[0] == '#')
1632 RtlInitUnicodeString(&nameW, resname + 1);
1633 if (RtlUnicodeStringToInteger(&nameW, 10, &value) != STATUS_SUCCESS || HIWORD(value))
1634 return STATUS_INVALID_PARAMETER;
1636 status = LdrFindResource_U(hModule, &info, 3, &entry);
1640 RtlCreateUnicodeString(&nameW, resname);
1641 RtlUpcaseUnicodeString(&nameW, &nameW, FALSE);
1642 info.Name = (ULONG_PTR)nameW.Buffer;
1643 status = LdrFindResource_U(hModule, &info, 3, &entry);
1644 RtlFreeUnicodeString(&nameW);
1646 if (status == STATUS_SUCCESS) status = LdrAccessResource(hModule, entry, &ptr, NULL);
1648 if (status == STATUS_SUCCESS)
1649 status = parse_manifest(acl, ai, filename, directory, shared, ptr, entry->Size);
1654 static NTSTATUS get_manifest_in_pe_file( struct actctx_loader* acl, struct assembly_identity* ai,
1655 LPCWSTR filename, LPCWSTR directory, BOOL shared,
1656 HANDLE file, LPCWSTR resname, ULONG lang )
1659 OBJECT_ATTRIBUTES attr;
1661 LARGE_INTEGER offset;
1666 TRACE( "looking for res %s in %s\n", debugstr_w(resname), debugstr_w(filename) );
1668 attr.Length = sizeof(attr);
1669 attr.RootDirectory = 0;
1670 attr.ObjectName = NULL;
1671 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1672 attr.SecurityDescriptor = NULL;
1673 attr.SecurityQualityOfService = NULL;
1676 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
1677 &attr, &size, PAGE_READONLY, SEC_COMMIT, file );
1678 if (status != STATUS_SUCCESS) return status;
1680 offset.QuadPart = 0;
1683 status = NtMapViewOfSection( mapping, GetCurrentProcess(), &base, 0, 0, &offset,
1684 &count, ViewShare, 0, PAGE_READONLY );
1686 if (status != STATUS_SUCCESS) return status;
1688 if (RtlImageNtHeader(base)) /* we got a PE file */
1690 HANDLE module = (HMODULE)((ULONG_PTR)base | 1); /* make it a LOAD_LIBRARY_AS_DATAFILE handle */
1691 status = get_manifest_in_module( acl, ai, filename, directory, shared, module, resname, lang );
1693 else status = STATUS_INVALID_IMAGE_FORMAT;
1695 NtUnmapViewOfSection( GetCurrentProcess(), base );
1699 static NTSTATUS get_manifest_in_manifest_file( struct actctx_loader* acl, struct assembly_identity* ai,
1700 LPCWSTR filename, LPCWSTR directory, BOOL shared, HANDLE file )
1702 FILE_END_OF_FILE_INFORMATION info;
1705 OBJECT_ATTRIBUTES attr;
1707 LARGE_INTEGER offset;
1712 TRACE( "loading manifest file %s\n", debugstr_w(filename) );
1714 attr.Length = sizeof(attr);
1715 attr.RootDirectory = 0;
1716 attr.ObjectName = NULL;
1717 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1718 attr.SecurityDescriptor = NULL;
1719 attr.SecurityQualityOfService = NULL;
1722 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
1723 &attr, &size, PAGE_READONLY, SEC_COMMIT, file );
1724 if (status != STATUS_SUCCESS) return status;
1726 offset.QuadPart = 0;
1729 status = NtMapViewOfSection( mapping, GetCurrentProcess(), &base, 0, 0, &offset,
1730 &count, ViewShare, 0, PAGE_READONLY );
1732 if (status != STATUS_SUCCESS) return status;
1734 status = NtQueryInformationFile( file, &io, &info, sizeof(info), FileEndOfFileInformation );
1735 if (status == STATUS_SUCCESS)
1736 status = parse_manifest(acl, ai, filename, directory, shared, base, info.EndOfFile.QuadPart);
1738 NtUnmapViewOfSection( GetCurrentProcess(), base );
1742 /* try to load the .manifest file associated to the file */
1743 static NTSTATUS get_manifest_in_associated_manifest( struct actctx_loader* acl, struct assembly_identity* ai,
1744 LPCWSTR filename, LPCWSTR directory, HMODULE module, LPCWSTR resname )
1746 static const WCHAR fmtW[] = { '.','%','l','u',0 };
1749 UNICODE_STRING nameW;
1751 ULONG_PTR resid = CREATEPROCESS_MANIFEST_RESOURCE_ID;
1753 if (!((ULONG_PTR)resname >> 16)) resid = (ULONG_PTR)resname & 0xffff;
1755 TRACE( "looking for manifest associated with %s id %lu\n", debugstr_w(filename), resid );
1757 if (module) /* use the module filename */
1759 UNICODE_STRING name;
1761 if (!(status = get_module_filename( module, &name, sizeof(dotManifestW) + 10*sizeof(WCHAR) )))
1763 if (resid != 1) sprintfW( name.Buffer + strlenW(name.Buffer), fmtW, resid );
1764 strcatW( name.Buffer, dotManifestW );
1765 if (!RtlDosPathNameToNtPathName_U( name.Buffer, &nameW, NULL, NULL ))
1766 status = STATUS_RESOURCE_DATA_NOT_FOUND;
1767 RtlFreeUnicodeString( &name );
1769 if (status) return status;
1773 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1774 (strlenW(filename) + 10) * sizeof(WCHAR) + sizeof(dotManifestW) )))
1775 return STATUS_NO_MEMORY;
1776 strcpyW( buffer, filename );
1777 if (resid != 1) sprintfW( buffer + strlenW(buffer), fmtW, resid );
1778 strcatW( buffer, dotManifestW );
1779 RtlInitUnicodeString( &nameW, buffer );
1782 if (!open_nt_file( &file, &nameW ))
1784 status = get_manifest_in_manifest_file( acl, ai, nameW.Buffer, directory, FALSE, file );
1787 else status = STATUS_RESOURCE_DATA_NOT_FOUND;
1788 RtlFreeUnicodeString( &nameW );
1792 static WCHAR *lookup_manifest_file( HANDLE dir, struct assembly_identity *ai )
1794 static const WCHAR lookup_fmtW[] =
1795 {'%','s','_','%','s','_','%','s','_','%','u','.','%','u','.','*','.','*','_',
1797 '.','m','a','n','i','f','e','s','t',0};
1799 WCHAR *lookup, *ret = NULL;
1800 UNICODE_STRING lookup_us;
1802 unsigned int data_pos = 0, data_len;
1805 if (!(lookup = RtlAllocateHeap( GetProcessHeap(), 0,
1806 (strlenW(ai->arch) + strlenW(ai->name)
1807 + strlenW(ai->public_key) + 20) * sizeof(WCHAR)
1808 + sizeof(lookup_fmtW) )))
1811 sprintfW( lookup, lookup_fmtW, ai->arch, ai->name, ai->public_key, ai->version.major, ai->version.minor);
1812 RtlInitUnicodeString( &lookup_us, lookup );
1814 NtQueryDirectoryFile( dir, 0, NULL, NULL, &io, buffer, sizeof(buffer),
1815 FileBothDirectoryInformation, FALSE, &lookup_us, TRUE );
1816 if (io.u.Status == STATUS_SUCCESS)
1818 FILE_BOTH_DIR_INFORMATION *dir_info;
1820 ULONG build, revision;
1822 data_len = io.Information;
1826 if (data_pos >= data_len)
1828 NtQueryDirectoryFile( dir, 0, NULL, NULL, &io, buffer, sizeof(buffer),
1829 FileBothDirectoryInformation, FALSE, &lookup_us, FALSE );
1830 if (io.u.Status != STATUS_SUCCESS) break;
1831 data_len = io.Information;
1834 dir_info = (FILE_BOTH_DIR_INFORMATION*)(buffer + data_pos);
1836 if (dir_info->NextEntryOffset) data_pos += dir_info->NextEntryOffset;
1837 else data_pos = data_len;
1839 tmp = (WCHAR *)dir_info->FileName + (strchrW(lookup, '*') - lookup);
1841 if (build < ai->version.build) continue;
1842 tmp = strchrW(tmp, '.') + 1;
1843 revision = atoiW(tmp);
1844 if (build == ai->version.build && revision < ai->version.revision)
1846 ai->version.build = build;
1847 ai->version.revision = revision;
1848 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, dir_info->FileNameLength + sizeof(WCHAR) )))
1850 memcpy( ret, dir_info->FileName, dir_info->FileNameLength );
1851 ret[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
1856 else WARN("no matching file for %s\n", debugstr_w(lookup));
1857 RtlFreeHeap( GetProcessHeap(), 0, lookup );
1861 static NTSTATUS lookup_winsxs(struct actctx_loader* acl, struct assembly_identity* ai)
1863 struct assembly_identity sxs_ai;
1864 UNICODE_STRING path_us;
1865 OBJECT_ATTRIBUTES attr;
1867 WCHAR *path, *file = NULL;
1870 static const WCHAR manifest_dirW[] =
1871 {'\\','w','i','n','s','x','s','\\','m','a','n','i','f','e','s','t','s',0};
1873 if (!ai->arch || !ai->name || !ai->public_key) return STATUS_NO_SUCH_FILE;
1875 if (!(path = RtlAllocateHeap( GetProcessHeap(), 0, windows_dir.Length + sizeof(manifest_dirW) )))
1876 return STATUS_NO_MEMORY;
1878 memcpy( path, windows_dir.Buffer, windows_dir.Length );
1879 memcpy( path + windows_dir.Length/sizeof(WCHAR), manifest_dirW, sizeof(manifest_dirW) );
1881 if (!RtlDosPathNameToNtPathName_U( path, &path_us, NULL, NULL ))
1883 RtlFreeHeap( GetProcessHeap(), 0, path );
1884 return STATUS_NO_SUCH_FILE;
1886 RtlFreeHeap( GetProcessHeap(), 0, path );
1888 attr.Length = sizeof(attr);
1889 attr.RootDirectory = 0;
1890 attr.Attributes = OBJ_CASE_INSENSITIVE;
1891 attr.ObjectName = &path_us;
1892 attr.SecurityDescriptor = NULL;
1893 attr.SecurityQualityOfService = NULL;
1895 if (!NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
1896 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT ))
1899 file = lookup_manifest_file( handle, &sxs_ai );
1904 RtlFreeUnicodeString( &path_us );
1905 return STATUS_NO_SUCH_FILE;
1908 /* append file name to directory path */
1909 if (!(path = RtlReAllocateHeap( GetProcessHeap(), 0, path_us.Buffer,
1910 path_us.Length + (strlenW(file) + 2) * sizeof(WCHAR) )))
1912 RtlFreeHeap( GetProcessHeap(), 0, file );
1913 RtlFreeUnicodeString( &path_us );
1914 return STATUS_NO_MEMORY;
1917 path[path_us.Length/sizeof(WCHAR)] = '\\';
1918 strcpyW( path + path_us.Length/sizeof(WCHAR) + 1, file );
1919 RtlInitUnicodeString( &path_us, path );
1920 *strrchrW(file, '.') = 0; /* remove .manifest extension */
1922 if (!open_nt_file( &handle, &path_us ))
1924 io.u.Status = get_manifest_in_manifest_file(acl, &sxs_ai, path_us.Buffer, file, TRUE, handle);
1927 else io.u.Status = STATUS_NO_SUCH_FILE;
1929 RtlFreeHeap( GetProcessHeap(), 0, file );
1930 RtlFreeUnicodeString( &path_us );
1934 static NTSTATUS lookup_assembly(struct actctx_loader* acl,
1935 struct assembly_identity* ai)
1937 static const WCHAR dotDllW[] = {'.','d','l','l',0};
1939 WCHAR *buffer, *p, *directory;
1941 UNICODE_STRING nameW;
1944 TRACE( "looking for name=%s version=%s arch=%s\n",
1945 debugstr_w(ai->name), debugstr_version(&ai->version), debugstr_w(ai->arch) );
1947 if ((status = lookup_winsxs(acl, ai)) != STATUS_NO_SUCH_FILE) return status;
1949 /* FIXME: add support for language specific lookup */
1951 nameW.Buffer = NULL;
1952 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1953 (strlenW(acl->actctx->appdir.info) + 2 * strlenW(ai->name) + 2) * sizeof(WCHAR) + sizeof(dotManifestW) )))
1954 return STATUS_NO_MEMORY;
1956 if (!(directory = build_assembly_dir( ai )))
1958 RtlFreeHeap( GetProcessHeap(), 0, buffer );
1959 return STATUS_NO_MEMORY;
1962 /* lookup in appdir\name.dll
1963 * appdir\name.manifest
1964 * appdir\name\name.dll
1965 * appdir\name\name.manifest
1967 strcpyW( buffer, acl->actctx->appdir.info );
1968 p = buffer + strlenW(buffer);
1969 for (i = 0; i < 2; i++)
1972 strcpyW( p, ai->name );
1975 strcpyW( p, dotDllW );
1976 if (RtlDosPathNameToNtPathName_U( buffer, &nameW, NULL, NULL ))
1978 status = open_nt_file( &file, &nameW );
1981 status = get_manifest_in_pe_file( acl, ai, nameW.Buffer, directory, FALSE, file,
1982 (LPCWSTR)CREATEPROCESS_MANIFEST_RESOURCE_ID, 0 );
1986 RtlFreeUnicodeString( &nameW );
1989 strcpyW( p, dotManifestW );
1990 if (RtlDosPathNameToNtPathName_U( buffer, &nameW, NULL, NULL ))
1992 status = open_nt_file( &file, &nameW );
1995 status = get_manifest_in_manifest_file( acl, ai, nameW.Buffer, directory, FALSE, file );
1999 RtlFreeUnicodeString( &nameW );
2001 status = STATUS_SXS_ASSEMBLY_NOT_FOUND;
2003 RtlFreeUnicodeString( &nameW );
2004 RtlFreeHeap( GetProcessHeap(), 0, directory );
2005 RtlFreeHeap( GetProcessHeap(), 0, buffer );
2009 static NTSTATUS parse_depend_manifests(struct actctx_loader* acl)
2011 NTSTATUS status = STATUS_SUCCESS;
2014 for (i = 0; i < acl->num_dependencies; i++)
2016 if (lookup_assembly(acl, &acl->dependencies[i]) != STATUS_SUCCESS)
2018 if (!acl->dependencies[i].optional)
2020 FIXME( "Could not find dependent assembly %s\n", debugstr_w(acl->dependencies[i].name) );
2021 status = STATUS_SXS_CANT_GEN_ACTCTX;
2026 /* FIXME should now iterate through all refs */
2030 /* find the appropriate activation context for RtlQueryInformationActivationContext */
2031 static NTSTATUS find_query_actctx( HANDLE *handle, DWORD flags, ULONG class )
2033 NTSTATUS status = STATUS_SUCCESS;
2035 if (flags & QUERY_ACTCTX_FLAG_USE_ACTIVE_ACTCTX)
2037 if (*handle) return STATUS_INVALID_PARAMETER;
2039 if (NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2040 *handle = NtCurrentTeb()->ActivationContextStack.ActiveFrame->ActivationContext;
2042 else if (flags & (QUERY_ACTCTX_FLAG_ACTCTX_IS_ADDRESS|QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE))
2047 if (!*handle) return STATUS_INVALID_PARAMETER;
2049 LdrLockLoaderLock( 0, NULL, &magic );
2050 if (!LdrFindEntryForAddress( *handle, &pldr ))
2052 if ((flags & QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE) && *handle != pldr->BaseAddress)
2053 status = STATUS_DLL_NOT_FOUND;
2055 *handle = pldr->ActivationContext;
2057 else status = STATUS_DLL_NOT_FOUND;
2058 LdrUnlockLoaderLock( 0, magic );
2060 else if (!*handle && (class != ActivationContextBasicInformation))
2061 *handle = process_actctx;
2066 static NTSTATUS fill_keyed_data(PACTCTX_SECTION_KEYED_DATA data, PVOID v1, PVOID v2, unsigned int i)
2068 data->ulDataFormatVersion = 1;
2070 data->ulLength = 20; /* FIXME */
2071 data->lpSectionGlobalData = NULL; /* FIXME */
2072 data->ulSectionGlobalDataLength = 0; /* FIXME */
2073 data->lpSectionBase = v2;
2074 data->ulSectionTotalLength = 0; /* FIXME */
2075 data->hActCtx = NULL;
2076 if (data->cbSize >= offsetof(ACTCTX_SECTION_KEYED_DATA, ulAssemblyRosterIndex) + sizeof(ULONG))
2077 data->ulAssemblyRosterIndex = i + 1;
2079 return STATUS_SUCCESS;
2082 static NTSTATUS find_dll_redirection(ACTIVATION_CONTEXT* actctx, const UNICODE_STRING *section_name,
2083 PACTCTX_SECTION_KEYED_DATA data)
2085 unsigned int i, j, snlen = section_name->Length / sizeof(WCHAR);
2087 for (i = 0; i < actctx->num_assemblies; i++)
2089 struct assembly *assembly = &actctx->assemblies[i];
2090 for (j = 0; j < assembly->num_dlls; j++)
2092 struct dll_redirect *dll = &assembly->dlls[j];
2093 if (!strncmpiW(section_name->Buffer, dll->name, snlen) && !dll->name[snlen])
2094 return fill_keyed_data(data, dll, assembly, i);
2097 return STATUS_SXS_KEY_NOT_FOUND;
2100 static NTSTATUS find_window_class(ACTIVATION_CONTEXT* actctx, const UNICODE_STRING *section_name,
2101 PACTCTX_SECTION_KEYED_DATA data)
2103 unsigned int i, j, k, snlen = section_name->Length / sizeof(WCHAR);
2105 for (i = 0; i < actctx->num_assemblies; i++)
2107 struct assembly *assembly = &actctx->assemblies[i];
2108 for (j = 0; j < assembly->num_dlls; j++)
2110 struct dll_redirect *dll = &assembly->dlls[j];
2111 for (k = 0; k < dll->entities.num; k++)
2113 struct entity *entity = &dll->entities.base[k];
2114 if (entity->kind == ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION)
2116 if (!strncmpiW(section_name->Buffer, entity->u.class.name, snlen) && !entity->u.class.name[snlen])
2117 return fill_keyed_data(data, entity, dll, i);
2122 return STATUS_SXS_KEY_NOT_FOUND;
2125 static NTSTATUS find_string(ACTIVATION_CONTEXT* actctx, ULONG section_kind,
2126 const UNICODE_STRING *section_name,
2127 DWORD flags, PACTCTX_SECTION_KEYED_DATA data)
2131 switch (section_kind)
2133 case ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION:
2134 status = find_dll_redirection(actctx, section_name, data);
2136 case ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION:
2137 status = find_window_class(actctx, section_name, data);
2139 case ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION:
2140 case ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION:
2141 case ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION:
2142 case ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION:
2143 case ACTIVATION_CONTEXT_SECTION_GLOBAL_OBJECT_RENAME_TABLE:
2144 case ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES:
2145 FIXME("Unsupported yet section_kind %x\n", section_kind);
2146 return STATUS_SXS_SECTION_NOT_FOUND;
2148 WARN("Unknown section_kind %x\n", section_kind);
2149 return STATUS_SXS_SECTION_NOT_FOUND;
2152 if (status != STATUS_SUCCESS) return status;
2154 if (flags & FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX)
2156 actctx_addref(actctx);
2157 data->hActCtx = actctx;
2159 return STATUS_SUCCESS;
2162 /* initialize the activation context for the current process */
2163 void actctx_init(void)
2168 ctx.cbSize = sizeof(ctx);
2169 ctx.lpSource = NULL;
2170 ctx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID | ACTCTX_FLAG_HMODULE_VALID;
2171 ctx.hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
2172 ctx.lpResourceName = (LPCWSTR)CREATEPROCESS_MANIFEST_RESOURCE_ID;
2174 if (!RtlCreateActivationContext( &handle, &ctx )) process_actctx = check_actctx(handle);
2178 /***********************************************************************
2179 * RtlCreateActivationContext (NTDLL.@)
2181 * Create an activation context.
2183 * FIXME: function signature/prototype is wrong
2185 NTSTATUS WINAPI RtlCreateActivationContext( HANDLE *handle, const void *ptr )
2187 const ACTCTXW *pActCtx = ptr; /* FIXME: not the right structure */
2188 const WCHAR *directory = NULL;
2189 ACTIVATION_CONTEXT *actctx;
2190 UNICODE_STRING nameW;
2192 NTSTATUS status = STATUS_NO_MEMORY;
2194 struct actctx_loader acl;
2196 TRACE("%p %08x\n", pActCtx, pActCtx ? pActCtx->dwFlags : 0);
2198 if (!pActCtx || pActCtx->cbSize < sizeof(*pActCtx) ||
2199 (pActCtx->dwFlags & ~ACTCTX_FLAGS_ALL))
2200 return STATUS_INVALID_PARAMETER;
2202 if (!(actctx = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*actctx) )))
2203 return STATUS_NO_MEMORY;
2205 actctx->magic = ACTCTX_MAGIC;
2206 actctx->ref_count = 1;
2207 actctx->config.type = ACTIVATION_CONTEXT_PATH_TYPE_NONE;
2208 actctx->config.info = NULL;
2209 actctx->appdir.type = ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE;
2210 if (pActCtx->dwFlags & ACTCTX_FLAG_APPLICATION_NAME_VALID)
2212 if (!(actctx->appdir.info = strdupW( pActCtx->lpApplicationName ))) goto error;
2220 if (pActCtx->dwFlags & ACTCTX_FLAG_HMODULE_VALID) module = pActCtx->hModule;
2221 else module = NtCurrentTeb()->Peb->ImageBaseAddress;
2223 if ((status = get_module_filename( module, &dir, 0 ))) goto error;
2224 if ((p = strrchrW( dir.Buffer, '\\' ))) p[1] = 0;
2225 actctx->appdir.info = dir.Buffer;
2228 nameW.Buffer = NULL;
2229 if (pActCtx->lpSource)
2231 if (!RtlDosPathNameToNtPathName_U(pActCtx->lpSource, &nameW, NULL, NULL))
2233 status = STATUS_NO_SUCH_FILE;
2236 status = open_nt_file( &file, &nameW );
2239 RtlFreeUnicodeString( &nameW );
2244 acl.actctx = actctx;
2245 acl.dependencies = NULL;
2246 acl.num_dependencies = 0;
2247 acl.allocated_dependencies = 0;
2249 if (pActCtx->dwFlags & ACTCTX_FLAG_LANGID_VALID) lang = pActCtx->wLangId;
2250 if (pActCtx->dwFlags & ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID) directory = pActCtx->lpAssemblyDirectory;
2252 if (pActCtx->dwFlags & ACTCTX_FLAG_RESOURCE_NAME_VALID)
2254 /* if we have a resource it's a PE file */
2255 if (pActCtx->dwFlags & ACTCTX_FLAG_HMODULE_VALID)
2257 status = get_manifest_in_module( &acl, NULL, NULL, directory, FALSE, pActCtx->hModule,
2258 pActCtx->lpResourceName, lang );
2259 if (status && status != STATUS_SXS_CANT_GEN_ACTCTX)
2260 /* FIXME: what to do if pActCtx->lpSource is set */
2261 status = get_manifest_in_associated_manifest( &acl, NULL, NULL, directory,
2262 pActCtx->hModule, pActCtx->lpResourceName );
2264 else if (pActCtx->lpSource)
2266 status = get_manifest_in_pe_file( &acl, NULL, nameW.Buffer, directory, FALSE,
2267 file, pActCtx->lpResourceName, lang );
2268 if (status && status != STATUS_SXS_CANT_GEN_ACTCTX)
2269 status = get_manifest_in_associated_manifest( &acl, NULL, nameW.Buffer, directory,
2270 NULL, pActCtx->lpResourceName );
2272 else status = STATUS_INVALID_PARAMETER;
2276 status = get_manifest_in_manifest_file( &acl, NULL, nameW.Buffer, directory, FALSE, file );
2279 if (file) NtClose( file );
2280 RtlFreeUnicodeString( &nameW );
2282 if (status == STATUS_SUCCESS) status = parse_depend_manifests(&acl);
2283 free_depend_manifests( &acl );
2285 if (status == STATUS_SUCCESS) *handle = actctx;
2286 else actctx_release( actctx );
2290 if (file) NtClose( file );
2291 actctx_release( actctx );
2296 /***********************************************************************
2297 * RtlAddRefActivationContext (NTDLL.@)
2299 void WINAPI RtlAddRefActivationContext( HANDLE handle )
2301 ACTIVATION_CONTEXT *actctx;
2303 if ((actctx = check_actctx( handle ))) actctx_addref( actctx );
2307 /******************************************************************
2308 * RtlReleaseActivationContext (NTDLL.@)
2310 void WINAPI RtlReleaseActivationContext( HANDLE handle )
2312 ACTIVATION_CONTEXT *actctx;
2314 if ((actctx = check_actctx( handle ))) actctx_release( actctx );
2318 /******************************************************************
2319 * RtlActivateActivationContext (NTDLL.@)
2321 NTSTATUS WINAPI RtlActivateActivationContext( ULONG unknown, HANDLE handle, PULONG_PTR cookie )
2323 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2325 if (!(frame = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*frame) )))
2326 return STATUS_NO_MEMORY;
2328 frame->Previous = NtCurrentTeb()->ActivationContextStack.ActiveFrame;
2329 frame->ActivationContext = handle;
2331 NtCurrentTeb()->ActivationContextStack.ActiveFrame = frame;
2332 RtlAddRefActivationContext( handle );
2334 *cookie = (ULONG_PTR)frame;
2335 TRACE( "%p cookie=%lx\n", handle, *cookie );
2336 return STATUS_SUCCESS;
2340 /***********************************************************************
2341 * RtlDeactivateActivationContext (NTDLL.@)
2343 void WINAPI RtlDeactivateActivationContext( ULONG flags, ULONG_PTR cookie )
2345 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame, *top;
2347 TRACE( "%x cookie=%lx\n", flags, cookie );
2349 /* find the right frame */
2350 top = NtCurrentTeb()->ActivationContextStack.ActiveFrame;
2351 for (frame = top; frame; frame = frame->Previous)
2352 if ((ULONG_PTR)frame == cookie) break;
2355 RtlRaiseStatus( STATUS_SXS_INVALID_DEACTIVATION );
2357 if (frame != top && !(flags & DEACTIVATE_ACTCTX_FLAG_FORCE_EARLY_DEACTIVATION))
2358 RtlRaiseStatus( STATUS_SXS_EARLY_DEACTIVATION );
2360 /* pop everything up to and including frame */
2361 NtCurrentTeb()->ActivationContextStack.ActiveFrame = frame->Previous;
2363 while (top != NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2365 frame = top->Previous;
2366 RtlReleaseActivationContext( top->ActivationContext );
2367 RtlFreeHeap( GetProcessHeap(), 0, top );
2373 /******************************************************************
2374 * RtlFreeThreadActivationContextStack (NTDLL.@)
2376 void WINAPI RtlFreeThreadActivationContextStack(void)
2378 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2380 frame = NtCurrentTeb()->ActivationContextStack.ActiveFrame;
2383 RTL_ACTIVATION_CONTEXT_STACK_FRAME *prev = frame->Previous;
2384 RtlReleaseActivationContext( frame->ActivationContext );
2385 RtlFreeHeap( GetProcessHeap(), 0, frame );
2388 NtCurrentTeb()->ActivationContextStack.ActiveFrame = NULL;
2392 /******************************************************************
2393 * RtlGetActiveActivationContext (NTDLL.@)
2395 NTSTATUS WINAPI RtlGetActiveActivationContext( HANDLE *handle )
2397 if (NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2399 *handle = NtCurrentTeb()->ActivationContextStack.ActiveFrame->ActivationContext;
2400 RtlAddRefActivationContext( *handle );
2405 return STATUS_SUCCESS;
2409 /******************************************************************
2410 * RtlIsActivationContextActive (NTDLL.@)
2412 BOOLEAN WINAPI RtlIsActivationContextActive( HANDLE handle )
2414 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2416 for (frame = NtCurrentTeb()->ActivationContextStack.ActiveFrame; frame; frame = frame->Previous)
2417 if (frame->ActivationContext == handle) return TRUE;
2422 /***********************************************************************
2423 * RtlQueryInformationActivationContext (NTDLL.@)
2425 * Get information about an activation context.
2426 * FIXME: function signature/prototype may be wrong
2428 NTSTATUS WINAPI RtlQueryInformationActivationContext( ULONG flags, HANDLE handle, PVOID subinst,
2429 ULONG class, PVOID buffer,
2430 SIZE_T bufsize, SIZE_T *retlen )
2432 ACTIVATION_CONTEXT *actctx;
2435 TRACE("%08x %p %p %u %p %ld %p\n", flags, handle,
2436 subinst, class, buffer, bufsize, retlen);
2438 if (retlen) *retlen = 0;
2439 if ((status = find_query_actctx( &handle, flags, class ))) return status;
2443 case ActivationContextBasicInformation:
2445 ACTIVATION_CONTEXT_BASIC_INFORMATION *info = buffer;
2447 if (retlen) *retlen = sizeof(*info);
2448 if (!info || bufsize < sizeof(*info)) return STATUS_BUFFER_TOO_SMALL;
2450 info->hActCtx = handle;
2451 info->dwFlags = 0; /* FIXME */
2452 if (!(flags & QUERY_ACTCTX_FLAG_NO_ADDREF)) RtlAddRefActivationContext( handle );
2456 case ActivationContextDetailedInformation:
2458 ACTIVATION_CONTEXT_DETAILED_INFORMATION *acdi = buffer;
2459 struct assembly *assembly = NULL;
2460 SIZE_T len, manifest_len = 0, config_len = 0, appdir_len = 0;
2463 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2465 if (actctx->num_assemblies) assembly = actctx->assemblies;
2467 if (assembly && assembly->manifest.info)
2468 manifest_len = strlenW(assembly->manifest.info) + 1;
2469 if (actctx->config.info) config_len = strlenW(actctx->config.info) + 1;
2470 if (actctx->appdir.info) appdir_len = strlenW(actctx->appdir.info) + 1;
2471 len = sizeof(*acdi) + (manifest_len + config_len + appdir_len) * sizeof(WCHAR);
2473 if (retlen) *retlen = len;
2474 if (!buffer || bufsize < len) return STATUS_BUFFER_TOO_SMALL;
2477 acdi->ulFormatVersion = assembly ? 1 : 0; /* FIXME */
2478 acdi->ulAssemblyCount = actctx->num_assemblies;
2479 acdi->ulRootManifestPathType = assembly ? assembly->manifest.type : 0 /* FIXME */;
2480 acdi->ulRootManifestPathChars = assembly && assembly->manifest.info ? manifest_len - 1 : 0;
2481 acdi->ulRootConfigurationPathType = actctx->config.type;
2482 acdi->ulRootConfigurationPathChars = actctx->config.info ? config_len - 1 : 0;
2483 acdi->ulAppDirPathType = actctx->appdir.type;
2484 acdi->ulAppDirPathChars = actctx->appdir.info ? appdir_len - 1 : 0;
2485 ptr = (LPWSTR)(acdi + 1);
2488 acdi->lpRootManifestPath = ptr;
2489 memcpy(ptr, assembly->manifest.info, manifest_len * sizeof(WCHAR));
2490 ptr += manifest_len;
2492 else acdi->lpRootManifestPath = NULL;
2495 acdi->lpRootConfigurationPath = ptr;
2496 memcpy(ptr, actctx->config.info, config_len * sizeof(WCHAR));
2499 else acdi->lpRootConfigurationPath = NULL;
2502 acdi->lpAppDirPath = ptr;
2503 memcpy(ptr, actctx->appdir.info, appdir_len * sizeof(WCHAR));
2505 else acdi->lpAppDirPath = NULL;
2509 case AssemblyDetailedInformationInActivationContext:
2511 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION *afdi = buffer;
2512 struct assembly *assembly;
2515 SIZE_T len, id_len = 0, ad_len = 0, path_len = 0;
2518 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2519 if (!subinst) return STATUS_INVALID_PARAMETER;
2521 index = *(DWORD*)subinst;
2522 if (!index || index > actctx->num_assemblies) return STATUS_INVALID_PARAMETER;
2524 assembly = &actctx->assemblies[index - 1];
2526 if (!(assembly_id = build_assembly_id( &assembly->id ))) return STATUS_NO_MEMORY;
2527 id_len = strlenW(assembly_id) + 1;
2528 if (assembly->directory) ad_len = strlenW(assembly->directory) + 1;
2530 if (assembly->manifest.info &&
2531 (assembly->type == ASSEMBLY_MANIFEST || assembly->type == ASSEMBLY_SHARED_MANIFEST))
2532 path_len = strlenW(assembly->manifest.info) + 1;
2534 len = sizeof(*afdi) + (id_len + ad_len + path_len) * sizeof(WCHAR);
2536 if (retlen) *retlen = len;
2537 if (!buffer || bufsize < len)
2539 RtlFreeHeap( GetProcessHeap(), 0, assembly_id );
2540 return STATUS_BUFFER_TOO_SMALL;
2543 afdi->ulFlags = 0; /* FIXME */
2544 afdi->ulEncodedAssemblyIdentityLength = (id_len - 1) * sizeof(WCHAR);
2545 afdi->ulManifestPathType = assembly->manifest.type;
2546 afdi->ulManifestPathLength = assembly->manifest.info ? (path_len - 1) * sizeof(WCHAR) : 0;
2547 /* FIXME afdi->liManifestLastWriteTime = 0; */
2548 afdi->ulPolicyPathType = ACTIVATION_CONTEXT_PATH_TYPE_NONE; /* FIXME */
2549 afdi->ulPolicyPathLength = 0;
2550 /* FIXME afdi->liPolicyLastWriteTime = 0; */
2551 afdi->ulMetadataSatelliteRosterIndex = 0; /* FIXME */
2552 afdi->ulManifestVersionMajor = 1;
2553 afdi->ulManifestVersionMinor = 0;
2554 afdi->ulPolicyVersionMajor = 0; /* FIXME */
2555 afdi->ulPolicyVersionMinor = 0; /* FIXME */
2556 afdi->ulAssemblyDirectoryNameLength = ad_len ? (ad_len - 1) * sizeof(WCHAR) : 0;
2557 ptr = (LPWSTR)(afdi + 1);
2558 afdi->lpAssemblyEncodedAssemblyIdentity = ptr;
2559 memcpy( ptr, assembly_id, id_len * sizeof(WCHAR) );
2563 afdi->lpAssemblyManifestPath = ptr;
2564 memcpy(ptr, assembly->manifest.info, path_len * sizeof(WCHAR));
2566 } else afdi->lpAssemblyManifestPath = NULL;
2567 afdi->lpAssemblyPolicyPath = NULL; /* FIXME */
2570 afdi->lpAssemblyDirectoryName = ptr;
2571 memcpy(ptr, assembly->directory, ad_len * sizeof(WCHAR));
2574 else afdi->lpAssemblyDirectoryName = NULL;
2575 RtlFreeHeap( GetProcessHeap(), 0, assembly_id );
2579 case FileInformationInAssemblyOfAssemblyInActivationContext:
2581 const ACTIVATION_CONTEXT_QUERY_INDEX *acqi = subinst;
2582 ASSEMBLY_FILE_DETAILED_INFORMATION *afdi = buffer;
2583 struct assembly *assembly;
2584 struct dll_redirect *dll;
2585 SIZE_T len, dll_len = 0;
2588 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2589 if (!acqi) return STATUS_INVALID_PARAMETER;
2591 if (acqi->ulAssemblyIndex >= actctx->num_assemblies)
2592 return STATUS_INVALID_PARAMETER;
2593 assembly = &actctx->assemblies[acqi->ulAssemblyIndex];
2595 if (acqi->ulFileIndexInAssembly >= assembly->num_dlls)
2596 return STATUS_INVALID_PARAMETER;
2597 dll = &assembly->dlls[acqi->ulFileIndexInAssembly];
2599 if (dll->name) dll_len = strlenW(dll->name) + 1;
2600 len = sizeof(*afdi) + dll_len * sizeof(WCHAR);
2602 if (!buffer || bufsize < len)
2604 if (retlen) *retlen = len;
2605 return STATUS_BUFFER_TOO_SMALL;
2607 if (retlen) *retlen = 0; /* yes that's what native does !! */
2608 afdi->ulFlags = ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION;
2609 afdi->ulFilenameLength = dll_len ? (dll_len - 1) * sizeof(WCHAR) : 0;
2610 afdi->ulPathLength = 0; /* FIXME */
2611 ptr = (LPWSTR)(afdi + 1);
2614 afdi->lpFileName = ptr;
2615 memcpy( ptr, dll->name, dll_len * sizeof(WCHAR) );
2616 } else afdi->lpFileName = NULL;
2617 afdi->lpFilePath = NULL; /* FIXME */
2622 FIXME( "class %u not implemented\n", class );
2623 return STATUS_NOT_IMPLEMENTED;
2625 return STATUS_SUCCESS;
2628 /***********************************************************************
2629 * RtlFindActivationContextSectionString (NTDLL.@)
2631 * Find information about a string in an activation context.
2632 * FIXME: function signature/prototype may be wrong
2634 NTSTATUS WINAPI RtlFindActivationContextSectionString( ULONG flags, const GUID *guid, ULONG section_kind,
2635 const UNICODE_STRING *section_name, PVOID ptr )
2637 PACTCTX_SECTION_KEYED_DATA data = ptr;
2638 NTSTATUS status = STATUS_SXS_KEY_NOT_FOUND;
2640 TRACE("%08x %s %u %s %p\n", flags, debugstr_guid(guid), section_kind,
2641 debugstr_us(section_name), data);
2645 FIXME("expected guid == NULL\n");
2646 return STATUS_INVALID_PARAMETER;
2648 if (flags & ~FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX)
2650 FIXME("unknown flags %08x\n", flags);
2651 return STATUS_INVALID_PARAMETER;
2653 if (!data || data->cbSize < offsetof(ACTCTX_SECTION_KEYED_DATA, ulAssemblyRosterIndex) ||
2654 !section_name || !section_name->Buffer)
2656 WARN("invalid parameter\n");
2657 return STATUS_INVALID_PARAMETER;
2660 if (NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2662 ACTIVATION_CONTEXT *actctx = check_actctx(NtCurrentTeb()->ActivationContextStack.ActiveFrame->ActivationContext);
2663 if (actctx) status = find_string( actctx, section_kind, section_name, flags, data );
2666 if (status != STATUS_SUCCESS)
2667 status = find_string( process_actctx, section_kind, section_name, flags, data );