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/debug.h"
37 #include "wine/unicode.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(actctx);
41 #define ACTCTX_FLAGS_ALL (\
42 ACTCTX_FLAG_PROCESSOR_ARCHITECTURE_VALID |\
43 ACTCTX_FLAG_LANGID_VALID |\
44 ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID |\
45 ACTCTX_FLAG_RESOURCE_NAME_VALID |\
46 ACTCTX_FLAG_SET_PROCESS_DEFAULT |\
47 ACTCTX_FLAG_APPLICATION_NAME_VALID |\
48 ACTCTX_FLAG_SOURCE_IS_ASSEMBLYREF |\
49 ACTCTX_FLAG_HMODULE_VALID )
51 #define ACTCTX_MAGIC 0xC07E3E11
53 /* we don't want to include winuser.h */
54 #define RT_MANIFEST ((ULONG_PTR)24)
55 #define CREATEPROCESS_MANIFEST_RESOURCE_ID ((ULONG_PTR)1)
75 struct assembly_version
83 struct assembly_identity
90 struct assembly_version version;
134 unsigned int allocated;
141 struct entity_array entities;
146 APPLICATION_MANIFEST,
148 ASSEMBLY_SHARED_MANIFEST,
153 enum assembly_type type;
154 struct assembly_identity id;
155 struct file_info manifest;
158 struct dll_redirect *dlls;
159 unsigned int num_dlls;
160 unsigned int allocated_dlls;
161 struct entity_array entities;
164 typedef struct _ACTIVATION_CONTEXT
168 struct file_info config;
169 struct file_info appdir;
170 struct assembly *assemblies;
171 unsigned int num_assemblies;
172 unsigned int allocated_assemblies;
173 } ACTIVATION_CONTEXT;
177 ACTIVATION_CONTEXT *actctx;
178 struct assembly_identity *dependencies;
179 unsigned int num_dependencies;
180 unsigned int allocated_dependencies;
183 static const WCHAR assemblyW[] = {'a','s','s','e','m','b','l','y',0};
184 static const WCHAR assemblyIdentityW[] = {'a','s','s','e','m','b','l','y','I','d','e','n','t','i','t','y',0};
185 static const WCHAR bindingRedirectW[] = {'b','i','n','d','i','n','g','R','e','d','i','r','e','c','t',0};
186 static const WCHAR clrClassW[] = {'c','l','r','C','l','a','s','s',0};
187 static const WCHAR clrSurrogateW[] = {'c','l','r','S','u','r','r','o','g','a','t','e',0};
188 static const WCHAR comClassW[] = {'c','o','m','C','l','a','s','s',0};
189 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};
190 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};
191 static const WCHAR dependencyW[] = {'d','e','p','e','n','d','e','n','c','y',0};
192 static const WCHAR dependentAssemblyW[] = {'d','e','p','e','n','d','e','n','t','A','s','s','e','m','b','l','y',0};
193 static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
194 static const WCHAR fileW[] = {'f','i','l','e',0};
195 static const WCHAR asmv2hashW[] = {'a','s','m','v','2',':','h','a','s','h',0};
196 static const WCHAR noInheritW[] = {'n','o','I','n','h','e','r','i','t',0};
197 static const WCHAR noInheritableW[] = {'n','o','I','n','h','e','r','i','t','a','b','l','e',0};
198 static const WCHAR typelibW[] = {'t','y','p','e','l','i','b',0};
199 static const WCHAR windowClassW[] = {'w','i','n','d','o','w','C','l','a','s','s',0};
201 static const WCHAR clsidW[] = {'c','l','s','i','d',0};
202 static const WCHAR hashW[] = {'h','a','s','h',0};
203 static const WCHAR hashalgW[] = {'h','a','s','h','a','l','g',0};
204 static const WCHAR helpdirW[] = {'h','e','l','p','d','i','r',0};
205 static const WCHAR iidW[] = {'i','i','d',0};
206 static const WCHAR languageW[] = {'l','a','n','g','u','a','g','e',0};
207 static const WCHAR manifestVersionW[] = {'m','a','n','i','f','e','s','t','V','e','r','s','i','o','n',0};
208 static const WCHAR nameW[] = {'n','a','m','e',0};
209 static const WCHAR newVersionW[] = {'n','e','w','V','e','r','s','i','o','n',0};
210 static const WCHAR oldVersionW[] = {'o','l','d','V','e','r','s','i','o','n',0};
211 static const WCHAR optionalW[] = {'o','p','t','i','o','n','a','l',0};
212 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};
213 static const WCHAR publicKeyTokenW[] = {'p','u','b','l','i','c','K','e','y','T','o','k','e','n',0};
214 static const WCHAR tlbidW[] = {'t','l','b','i','d',0};
215 static const WCHAR typeW[] = {'t','y','p','e',0};
216 static const WCHAR versionW[] = {'v','e','r','s','i','o','n',0};
217 static const WCHAR xmlnsW[] = {'x','m','l','n','s',0};
219 static const WCHAR xmlW[] = {'?','x','m','l',0};
220 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};
221 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};
223 static const WCHAR dotManifestW[] = {'.','m','a','n','i','f','e','s','t',0};
224 static const WCHAR version_formatW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
226 static ACTIVATION_CONTEXT system_actctx = { ACTCTX_MAGIC, 1 };
227 static ACTIVATION_CONTEXT *process_actctx = &system_actctx;
229 static WCHAR *strdupW(const WCHAR* str)
233 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(WCHAR))))
235 return strcpyW(ptr, str);
238 static WCHAR *xmlstrdupW(const xmlstr_t* str)
242 if ((strW = RtlAllocateHeap(GetProcessHeap(), 0, (str->len + 1) * sizeof(WCHAR))))
244 memcpy( strW, str->ptr, str->len * sizeof(WCHAR) );
250 static inline BOOL xmlstr_cmp(const xmlstr_t* xmlstr, const WCHAR *str)
252 return !strncmpW(xmlstr->ptr, str, xmlstr->len) && !str[xmlstr->len];
255 static inline BOOL xmlstr_cmpi(const xmlstr_t* xmlstr, const WCHAR *str)
257 return !strncmpiW(xmlstr->ptr, str, xmlstr->len) && !str[xmlstr->len];
260 static inline BOOL xmlstr_cmp_end(const xmlstr_t* xmlstr, const WCHAR *str)
262 return (xmlstr->len && xmlstr->ptr[0] == '/' &&
263 !strncmpW(xmlstr->ptr + 1, str, xmlstr->len - 1) && !str[xmlstr->len - 1]);
266 static inline BOOL isxmlspace( WCHAR ch )
268 return (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t');
271 static inline const char* debugstr_xmlstr(const xmlstr_t* str)
273 return debugstr_wn(str->ptr, str->len);
276 static inline const char* debugstr_version(const struct assembly_version *ver)
278 return wine_dbg_sprintf("%u.%u.%u.%u", ver->major, ver->minor, ver->build, ver->revision);
281 static struct assembly *add_assembly(ACTIVATION_CONTEXT *actctx, enum assembly_type at)
283 struct assembly *assembly;
285 if (actctx->num_assemblies == actctx->allocated_assemblies)
288 unsigned int new_count;
289 if (actctx->assemblies)
291 new_count = actctx->allocated_assemblies * 2;
292 ptr = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
293 actctx->assemblies, new_count * sizeof(*assembly) );
298 ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*assembly) );
300 if (!ptr) return NULL;
301 actctx->assemblies = ptr;
302 actctx->allocated_assemblies = new_count;
305 assembly = &actctx->assemblies[actctx->num_assemblies++];
310 static struct dll_redirect* add_dll_redirect(struct assembly* assembly)
312 if (assembly->num_dlls == assembly->allocated_dlls)
315 unsigned int new_count;
318 new_count = assembly->allocated_dlls * 2;
319 ptr = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
320 assembly->dlls, new_count * sizeof(*assembly->dlls) );
325 ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*assembly->dlls) );
327 if (!ptr) return NULL;
328 assembly->dlls = ptr;
329 assembly->allocated_dlls = new_count;
331 return &assembly->dlls[assembly->num_dlls++];
334 static void free_assembly_identity(struct assembly_identity *ai)
336 RtlFreeHeap( GetProcessHeap(), 0, ai->name );
337 RtlFreeHeap( GetProcessHeap(), 0, ai->arch );
338 RtlFreeHeap( GetProcessHeap(), 0, ai->public_key );
339 RtlFreeHeap( GetProcessHeap(), 0, ai->language );
342 static struct entity* add_entity(struct entity_array *array, DWORD kind)
344 struct entity* entity;
346 if (array->num == array->allocated)
349 unsigned int new_count;
352 new_count = array->allocated * 2;
353 ptr = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
354 array->base, new_count * sizeof(*array->base) );
359 ptr = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*array->base) );
361 if (!ptr) return NULL;
363 array->allocated = new_count;
365 entity = &array->base[array->num++];
370 static void free_entity_array(struct entity_array *array)
373 for (i = 0; i < array->num; i++)
375 struct entity *entity = &array->base[i];
376 switch (entity->kind)
378 case ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION:
379 RtlFreeHeap(GetProcessHeap(), 0, entity->u.comclass.clsid);
381 case ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION:
382 RtlFreeHeap(GetProcessHeap(), 0, entity->u.proxy.iid);
383 RtlFreeHeap(GetProcessHeap(), 0, entity->u.proxy.name);
385 case ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION:
386 RtlFreeHeap(GetProcessHeap(), 0, entity->u.typelib.tlbid);
387 RtlFreeHeap(GetProcessHeap(), 0, entity->u.typelib.version);
388 RtlFreeHeap(GetProcessHeap(), 0, entity->u.typelib.helpdir);
390 case ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION:
391 RtlFreeHeap(GetProcessHeap(), 0, entity->u.class.name);
393 case ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION:
394 RtlFreeHeap(GetProcessHeap(), 0, entity->u.clrclass.name);
395 RtlFreeHeap(GetProcessHeap(), 0, entity->u.clrclass.clsid);
397 case ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES:
398 RtlFreeHeap(GetProcessHeap(), 0, entity->u.clrsurrogate.name);
399 RtlFreeHeap(GetProcessHeap(), 0, entity->u.clrsurrogate.clsid);
402 FIXME("Unknown entity kind %d\n", entity->kind);
405 RtlFreeHeap( GetProcessHeap(), 0, array->base );
408 static BOOL is_matching_string( const WCHAR *str1, const WCHAR *str2 )
410 if (!str1) return !str2;
411 return str2 && !strcmpiW( str1, str2 );
414 static BOOL is_matching_identity( const struct assembly_identity *id1,
415 const struct assembly_identity *id2 )
417 if (!is_matching_string( id1->name, id2->name )) return FALSE;
418 if (!is_matching_string( id1->arch, id2->arch )) return FALSE;
419 if (!is_matching_string( id1->public_key, id2->public_key )) return FALSE;
421 if (id1->language && id2->language && strcmpiW( id1->language, id2->language ))
423 static const WCHAR wildcardW[] = {'*',0};
424 if (strcmpW( wildcardW, id1->language ) && strcmpW( wildcardW, id2->language ))
427 if (id1->version.major != id2->version.major) return FALSE;
428 if (id1->version.minor != id2->version.minor) return FALSE;
429 if (id1->version.build > id2->version.build) return FALSE;
430 if (id1->version.build == id2->version.build &&
431 id1->version.revision > id2->version.revision) return FALSE;
435 static BOOL add_dependent_assembly_id(struct actctx_loader* acl,
436 struct assembly_identity* ai)
440 /* check if we already have that assembly */
442 for (i = 0; i < acl->actctx->num_assemblies; i++)
443 if (is_matching_identity( ai, &acl->actctx->assemblies[i].id ))
445 TRACE( "reusing existing assembly for %s arch %s version %u.%u.%u.%u\n",
446 debugstr_w(ai->name), debugstr_w(ai->arch), ai->version.major, ai->version.minor,
447 ai->version.build, ai->version.revision );
451 for (i = 0; i < acl->num_dependencies; i++)
452 if (is_matching_identity( ai, &acl->dependencies[i] ))
454 TRACE( "reusing existing dependency for %s arch %s version %u.%u.%u.%u\n",
455 debugstr_w(ai->name), debugstr_w(ai->arch), ai->version.major, ai->version.minor,
456 ai->version.build, ai->version.revision );
460 if (acl->num_dependencies == acl->allocated_dependencies)
463 unsigned int new_count;
464 if (acl->dependencies)
466 new_count = acl->allocated_dependencies * 2;
467 ptr = RtlReAllocateHeap(GetProcessHeap(), 0, acl->dependencies,
468 new_count * sizeof(acl->dependencies[0]));
473 ptr = RtlAllocateHeap(GetProcessHeap(), 0, new_count * sizeof(acl->dependencies[0]));
475 if (!ptr) return FALSE;
476 acl->dependencies = ptr;
477 acl->allocated_dependencies = new_count;
479 acl->dependencies[acl->num_dependencies++] = *ai;
484 static void free_depend_manifests(struct actctx_loader* acl)
487 for (i = 0; i < acl->num_dependencies; i++)
488 free_assembly_identity(&acl->dependencies[i]);
489 RtlFreeHeap(GetProcessHeap(), 0, acl->dependencies);
492 static WCHAR *build_assembly_dir(struct assembly_identity* ai)
494 static const WCHAR undW[] = {'_',0};
495 static const WCHAR noneW[] = {'n','o','n','e',0};
496 static const WCHAR mskeyW[] = {'d','e','a','d','b','e','e','f',0};
498 const WCHAR *key = ai->public_key ? ai->public_key : noneW;
499 const WCHAR *lang = ai->language ? ai->language : noneW;
500 SIZE_T size = (strlenW(ai->arch) + 1 + strlenW(ai->name) + 1 + strlenW(key) + 24 + 1 +
501 strlenW(lang) + 1) * sizeof(WCHAR) + sizeof(mskeyW);
504 if (!(ret = RtlAllocateHeap( GetProcessHeap(), 0, size ))) return NULL;
506 strcpyW( ret, ai->arch );
507 strcatW( ret, undW );
508 strcatW( ret, ai->name );
509 strcatW( ret, undW );
511 strcatW( ret, undW );
512 sprintfW( ret + strlenW(ret), version_formatW,
513 ai->version.major, ai->version.minor, ai->version.build, ai->version.revision );
514 strcatW( ret, undW );
515 strcatW( ret, lang );
516 strcatW( ret, undW );
517 strcatW( ret, mskeyW );
521 static inline void append_string( WCHAR *buffer, const WCHAR *prefix, const WCHAR *str )
526 strcatW( buffer, prefix );
535 static WCHAR *build_assembly_id( const struct assembly_identity *ai )
537 static const WCHAR archW[] =
538 {',','p','r','o','c','e','s','s','o','r','A','r','c','h','i','t','e','c','t','u','r','e','=',0};
539 static const WCHAR public_keyW[] =
540 {',','p','u','b','l','i','c','K','e','y','T','o','k','e','n','=',0};
541 static const WCHAR typeW[] =
542 {',','t','y','p','e','=',0};
543 static const WCHAR versionW[] =
544 {',','v','e','r','s','i','o','n','=',0};
546 WCHAR version[64], *ret;
549 sprintfW( version, version_formatW,
550 ai->version.major, ai->version.minor, ai->version.build, ai->version.revision );
551 if (ai->name) size += strlenW(ai->name) * sizeof(WCHAR);
552 if (ai->arch) size += strlenW(archW) + strlenW(ai->arch) + 2;
553 if (ai->public_key) size += strlenW(public_keyW) + strlenW(ai->public_key) + 2;
554 if (ai->type) size += strlenW(typeW) + strlenW(ai->type) + 2;
555 size += strlenW(versionW) + strlenW(version) + 2;
557 if (!(ret = RtlAllocateHeap( GetProcessHeap(), 0, (size + 1) * sizeof(WCHAR) )))
560 if (ai->name) strcpyW( ret, ai->name );
562 append_string( ret, archW, ai->arch );
563 append_string( ret, public_keyW, ai->public_key );
564 append_string( ret, typeW, ai->type );
565 append_string( ret, versionW, version );
569 static ACTIVATION_CONTEXT *check_actctx( HANDLE h )
571 ACTIVATION_CONTEXT *actctx = h;
573 if (!h || h == INVALID_HANDLE_VALUE) return NULL;
574 switch (actctx->magic)
583 static inline void actctx_addref( ACTIVATION_CONTEXT *actctx )
585 interlocked_xchg_add( &actctx->ref_count, 1 );
588 static void actctx_release( ACTIVATION_CONTEXT *actctx )
590 if (interlocked_xchg_add( &actctx->ref_count, -1 ) == 1)
594 for (i = 0; i < actctx->num_assemblies; i++)
596 struct assembly *assembly = &actctx->assemblies[i];
597 for (j = 0; j < assembly->num_dlls; j++)
599 struct dll_redirect *dll = &assembly->dlls[j];
600 free_entity_array( &dll->entities );
601 RtlFreeHeap( GetProcessHeap(), 0, dll->name );
602 RtlFreeHeap( GetProcessHeap(), 0, dll->hash );
604 RtlFreeHeap( GetProcessHeap(), 0, assembly->dlls );
605 RtlFreeHeap( GetProcessHeap(), 0, assembly->manifest.info );
606 RtlFreeHeap( GetProcessHeap(), 0, assembly->directory );
607 free_entity_array( &assembly->entities );
608 free_assembly_identity(&assembly->id);
610 RtlFreeHeap( GetProcessHeap(), 0, actctx->config.info );
611 RtlFreeHeap( GetProcessHeap(), 0, actctx->appdir.info );
612 RtlFreeHeap( GetProcessHeap(), 0, actctx->assemblies );
614 RtlFreeHeap( GetProcessHeap(), 0, actctx );
618 static BOOL next_xml_attr(xmlbuf_t* xmlbuf, xmlstr_t* name, xmlstr_t* value,
619 BOOL* error, BOOL* end)
625 while (xmlbuf->ptr < xmlbuf->end && isxmlspace(*xmlbuf->ptr))
628 if (xmlbuf->ptr == xmlbuf->end) return FALSE;
630 if (*xmlbuf->ptr == '/')
633 if (xmlbuf->ptr == xmlbuf->end || *xmlbuf->ptr != '>')
642 if (*xmlbuf->ptr == '>')
650 while (ptr < xmlbuf->end && *ptr != '=' && *ptr != '>' && !isxmlspace(*ptr)) ptr++;
652 if (ptr == xmlbuf->end || *ptr != '=') return FALSE;
654 name->ptr = xmlbuf->ptr;
655 name->len = ptr-xmlbuf->ptr;
659 if (ptr == xmlbuf->end || (*ptr != '"' && *ptr != '\'')) return FALSE;
662 if (ptr == xmlbuf->end) return FALSE;
664 ptr = memchrW(ptr, ptr[-1], xmlbuf->end - ptr);
667 xmlbuf->ptr = xmlbuf->end;
671 value->len = ptr - value->ptr;
672 xmlbuf->ptr = ptr + 1;
674 if (xmlbuf->ptr == xmlbuf->end) return FALSE;
680 static BOOL next_xml_elem(xmlbuf_t* xmlbuf, xmlstr_t* elem)
686 ptr = memchrW(xmlbuf->ptr, '<', xmlbuf->end - xmlbuf->ptr);
689 xmlbuf->ptr = xmlbuf->end;
693 if (ptr + 3 < xmlbuf->end && ptr[0] == '!' && ptr[1] == '-' && ptr[2] == '-') /* skip comment */
695 for (ptr += 3; ptr + 3 <= xmlbuf->end; ptr++)
696 if (ptr[0] == '-' && ptr[1] == '-' && ptr[2] == '>') break;
698 if (ptr + 3 > xmlbuf->end)
700 xmlbuf->ptr = xmlbuf->end;
703 xmlbuf->ptr = ptr + 3;
709 while (ptr < xmlbuf->end && !isxmlspace(*ptr) && *ptr != '>' && (*ptr != '/' || ptr == xmlbuf->ptr))
712 elem->ptr = xmlbuf->ptr;
713 elem->len = ptr - xmlbuf->ptr;
715 return xmlbuf->ptr != xmlbuf->end;
718 static BOOL parse_xml_header(xmlbuf_t* xmlbuf)
720 /* FIXME: parse attributes */
723 for (ptr = xmlbuf->ptr; ptr < xmlbuf->end - 1; ptr++)
725 if (ptr[0] == '?' && ptr[1] == '>')
727 xmlbuf->ptr = ptr + 2;
734 static BOOL parse_text_content(xmlbuf_t* xmlbuf, xmlstr_t* content)
736 const WCHAR *ptr = memchrW(xmlbuf->ptr, '<', xmlbuf->end - xmlbuf->ptr);
738 if (!ptr) return FALSE;
740 content->ptr = xmlbuf->ptr;
741 content->len = ptr - xmlbuf->ptr;
747 static BOOL parse_version(const xmlstr_t *str, struct assembly_version *version)
753 /* major.minor.build.revision */
754 ver[0] = ver[1] = ver[2] = ver[3] = pos = 0;
755 for (curr = str->ptr; curr < str->ptr + str->len; curr++)
757 if (*curr >= '0' && *curr <= '9')
759 ver[pos] = ver[pos] * 10 + *curr - '0';
760 if (ver[pos] >= 0x10000) goto error;
762 else if (*curr == '.')
764 if (++pos >= 4) goto error;
768 version->major = ver[0];
769 version->minor = ver[1];
770 version->build = ver[2];
771 version->revision = ver[3];
775 FIXME( "Wrong version definition in manifest file (%s)\n", debugstr_xmlstr(str) );
779 static BOOL parse_expect_elem(xmlbuf_t* xmlbuf, const WCHAR* name)
782 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
783 if (xmlstr_cmp(&elem, name)) return TRUE;
784 FIXME( "unexpected element %s\n", debugstr_xmlstr(&elem) );
788 static BOOL parse_expect_no_attr(xmlbuf_t* xmlbuf, BOOL* end)
790 xmlstr_t attr_name, attr_value;
793 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, end))
795 WARN("unexpected attr %s=%s\n", debugstr_xmlstr(&attr_name),
796 debugstr_xmlstr(&attr_value));
801 static BOOL parse_end_element(xmlbuf_t *xmlbuf)
804 return parse_expect_no_attr(xmlbuf, &end) && !end;
807 static BOOL parse_expect_end_elem(xmlbuf_t *xmlbuf, const WCHAR *name)
810 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
811 if (!xmlstr_cmp_end(&elem, name))
813 FIXME( "unexpected element %s\n", debugstr_xmlstr(&elem) );
816 return parse_end_element(xmlbuf);
819 static BOOL parse_unknown_elem(xmlbuf_t *xmlbuf, const xmlstr_t *unknown_elem)
821 xmlstr_t attr_name, attr_value, elem;
822 BOOL end = FALSE, error, ret = TRUE;
824 while(next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end));
825 if(error || end) return end;
827 while(ret && (ret = next_xml_elem(xmlbuf, &elem)))
829 if(*elem.ptr == '/' && elem.len - 1 == unknown_elem->len &&
830 !strncmpW(elem.ptr+1, unknown_elem->ptr, unknown_elem->len))
833 ret = parse_unknown_elem(xmlbuf, &elem);
836 return ret && parse_end_element(xmlbuf);
839 static BOOL parse_assembly_identity_elem(xmlbuf_t* xmlbuf, ACTIVATION_CONTEXT* actctx,
840 struct assembly_identity* ai)
842 xmlstr_t attr_name, attr_value;
843 BOOL end = FALSE, error;
845 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
847 if (xmlstr_cmp(&attr_name, nameW))
849 if (!(ai->name = xmlstrdupW(&attr_value))) return FALSE;
851 else if (xmlstr_cmp(&attr_name, typeW))
853 if (!(ai->type = xmlstrdupW(&attr_value))) return FALSE;
855 else if (xmlstr_cmp(&attr_name, versionW))
857 if (!parse_version(&attr_value, &ai->version)) return FALSE;
859 else if (xmlstr_cmp(&attr_name, processorArchitectureW))
861 if (!(ai->arch = xmlstrdupW(&attr_value))) return FALSE;
863 else if (xmlstr_cmp(&attr_name, publicKeyTokenW))
865 if (!(ai->public_key = xmlstrdupW(&attr_value))) return FALSE;
867 else if (xmlstr_cmp(&attr_name, languageW))
869 WARN("Unsupported yet language attribute (%s)\n",
870 debugstr_xmlstr(&attr_value));
871 if (!(ai->language = xmlstrdupW(&attr_value))) return FALSE;
875 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name),
876 debugstr_xmlstr(&attr_value));
880 TRACE( "name=%s version=%s arch=%s\n",
881 debugstr_w(ai->name), debugstr_version(&ai->version), debugstr_w(ai->arch) );
883 if (error || end) return end;
884 return parse_expect_end_elem(xmlbuf, assemblyIdentityW);
887 static BOOL parse_com_class_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
889 xmlstr_t elem, attr_name, attr_value;
890 BOOL ret, end = FALSE, error;
891 struct entity* entity;
893 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION)))
896 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
898 if (xmlstr_cmp(&attr_name, clsidW))
900 if (!(entity->u.comclass.clsid = xmlstrdupW(&attr_value))) return FALSE;
904 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
908 if (error || end) return end;
910 while ((ret = next_xml_elem(xmlbuf, &elem)))
912 if (xmlstr_cmp_end(&elem, comClassW))
914 ret = parse_end_element(xmlbuf);
919 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
920 ret = parse_unknown_elem(xmlbuf, &elem);
926 static BOOL parse_cominterface_proxy_stub_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
928 xmlstr_t attr_name, attr_value;
929 BOOL end = FALSE, error;
930 struct entity* entity;
932 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION)))
935 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
937 if (xmlstr_cmp(&attr_name, iidW))
939 if (!(entity->u.proxy.iid = xmlstrdupW(&attr_value))) return FALSE;
941 if (xmlstr_cmp(&attr_name, nameW))
943 if (!(entity->u.proxy.name = xmlstrdupW(&attr_value))) return FALSE;
947 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
951 if (error || end) return end;
952 return parse_expect_end_elem(xmlbuf, comInterfaceProxyStubW);
955 static BOOL parse_typelib_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
957 xmlstr_t attr_name, attr_value;
958 BOOL end = FALSE, error;
959 struct entity* entity;
961 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION)))
964 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
966 if (xmlstr_cmp(&attr_name, tlbidW))
968 if (!(entity->u.typelib.tlbid = xmlstrdupW(&attr_value))) return FALSE;
970 if (xmlstr_cmp(&attr_name, versionW))
972 if (!(entity->u.typelib.version = xmlstrdupW(&attr_value))) return FALSE;
974 if (xmlstr_cmp(&attr_name, helpdirW))
976 if (!(entity->u.typelib.helpdir = xmlstrdupW(&attr_value))) return FALSE;
980 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
984 if (error || end) return end;
985 return parse_expect_end_elem(xmlbuf, typelibW);
988 static BOOL parse_window_class_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
990 xmlstr_t elem, content;
991 BOOL end = FALSE, ret = TRUE;
992 struct entity* entity;
994 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION)))
997 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
998 if (end) return FALSE;
1000 if (!parse_text_content(xmlbuf, &content)) return FALSE;
1002 if (!(entity->u.class.name = xmlstrdupW(&content))) return FALSE;
1004 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1006 if (xmlstr_cmp_end(&elem, windowClassW))
1008 ret = parse_end_element(xmlbuf);
1013 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1014 ret = parse_unknown_elem(xmlbuf, &elem);
1021 static BOOL parse_binding_redirect_elem(xmlbuf_t* xmlbuf)
1023 xmlstr_t attr_name, attr_value;
1024 BOOL end = FALSE, error;
1026 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1028 if (xmlstr_cmp(&attr_name, oldVersionW))
1030 FIXME("Not stored yet oldVersion=%s\n", debugstr_xmlstr(&attr_value));
1032 else if (xmlstr_cmp(&attr_name, newVersionW))
1034 FIXME("Not stored yet newVersion=%s\n", debugstr_xmlstr(&attr_value));
1038 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1042 if (error || end) return end;
1043 return parse_expect_end_elem(xmlbuf, bindingRedirectW);
1046 static BOOL parse_description_elem(xmlbuf_t* xmlbuf)
1048 xmlstr_t elem, content;
1049 BOOL end = FALSE, ret = TRUE;
1051 if (!parse_expect_no_attr(xmlbuf, &end) || end ||
1052 !parse_text_content(xmlbuf, &content))
1055 TRACE("Got description %s\n", debugstr_xmlstr(&content));
1057 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1059 if (xmlstr_cmp_end(&elem, descriptionW))
1061 ret = parse_end_element(xmlbuf);
1066 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1067 ret = parse_unknown_elem(xmlbuf, &elem);
1074 static BOOL parse_com_interface_external_proxy_stub_elem(xmlbuf_t* xmlbuf,
1075 struct assembly* assembly)
1077 xmlstr_t attr_name, attr_value;
1078 BOOL end = FALSE, error;
1079 struct entity* entity;
1081 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION);
1082 if (!entity) return FALSE;
1084 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1086 if (xmlstr_cmp(&attr_name, iidW))
1088 if (!(entity->u.proxy.iid = xmlstrdupW(&attr_value))) return FALSE;
1090 if (xmlstr_cmp(&attr_name, nameW))
1092 if (!(entity->u.proxy.name = xmlstrdupW(&attr_value))) return FALSE;
1096 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1100 if (error || end) return end;
1101 return parse_expect_end_elem(xmlbuf, comInterfaceExternalProxyStubW);
1104 static BOOL parse_clr_class_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1106 xmlstr_t attr_name, attr_value;
1107 BOOL end = FALSE, error;
1108 struct entity* entity;
1110 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION);
1111 if (!entity) return FALSE;
1113 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1115 if (xmlstr_cmp(&attr_name, nameW))
1117 if (!(entity->u.clrclass.name = xmlstrdupW(&attr_value))) return FALSE;
1119 else if (xmlstr_cmp(&attr_name, clsidW))
1121 if (!(entity->u.clrclass.clsid = xmlstrdupW(&attr_value))) return FALSE;
1125 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1129 if (error || end) return end;
1130 return parse_expect_end_elem(xmlbuf, clrClassW);
1133 static BOOL parse_clr_surrogate_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1135 xmlstr_t attr_name, attr_value;
1136 BOOL end = FALSE, error;
1137 struct entity* entity;
1139 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES);
1140 if (!entity) return FALSE;
1142 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1144 if (xmlstr_cmp(&attr_name, nameW))
1146 if (!(entity->u.clrsurrogate.name = xmlstrdupW(&attr_value))) return FALSE;
1148 else if (xmlstr_cmp(&attr_name, clsidW))
1150 if (!(entity->u.clrsurrogate.clsid = xmlstrdupW(&attr_value))) return FALSE;
1154 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1158 if (error || end) return end;
1159 return parse_expect_end_elem(xmlbuf, clrSurrogateW);
1162 static BOOL parse_dependent_assembly_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl, BOOL optional)
1164 struct assembly_identity ai;
1166 BOOL end = FALSE, ret = TRUE;
1168 if (!parse_expect_no_attr(xmlbuf, &end) || end) return end;
1170 memset(&ai, 0, sizeof(ai));
1171 ai.optional = optional;
1173 if (!parse_expect_elem(xmlbuf, assemblyIdentityW) ||
1174 !parse_assembly_identity_elem(xmlbuf, acl->actctx, &ai))
1177 TRACE( "adding name=%s version=%s arch=%s\n",
1178 debugstr_w(ai.name), debugstr_version(&ai.version), debugstr_w(ai.arch) );
1180 /* store the newly found identity for later loading */
1181 if (!add_dependent_assembly_id(acl, &ai)) return FALSE;
1183 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1185 if (xmlstr_cmp_end(&elem, dependentAssemblyW))
1187 ret = parse_end_element(xmlbuf);
1190 else if (xmlstr_cmp(&elem, bindingRedirectW))
1192 ret = parse_binding_redirect_elem(xmlbuf);
1196 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1197 ret = parse_unknown_elem(xmlbuf, &elem);
1204 static BOOL parse_dependency_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl)
1206 xmlstr_t attr_name, attr_value, elem;
1207 BOOL end = FALSE, ret = TRUE, error, optional = FALSE;
1209 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1211 if (xmlstr_cmp(&attr_name, optionalW))
1213 static const WCHAR yesW[] = {'y','e','s',0};
1214 optional = xmlstr_cmpi( &attr_value, yesW );
1215 TRACE("optional=%s\n", debugstr_xmlstr(&attr_value));
1219 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1223 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1225 if (xmlstr_cmp_end(&elem, dependencyW))
1227 ret = parse_end_element(xmlbuf);
1230 else if (xmlstr_cmp(&elem, dependentAssemblyW))
1232 ret = parse_dependent_assembly_elem(xmlbuf, acl, optional);
1236 WARN("unknown element %s\n", debugstr_xmlstr(&elem));
1237 ret = parse_unknown_elem(xmlbuf, &elem);
1244 static BOOL parse_noinherit_elem(xmlbuf_t* xmlbuf)
1248 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1249 return end || parse_expect_end_elem(xmlbuf, noInheritW);
1252 static BOOL parse_noinheritable_elem(xmlbuf_t* xmlbuf)
1256 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1257 return end || parse_expect_end_elem(xmlbuf, noInheritableW);
1260 static BOOL parse_file_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1262 xmlstr_t attr_name, attr_value, elem;
1263 BOOL end = FALSE, error, ret = TRUE;
1264 struct dll_redirect* dll;
1266 if (!(dll = add_dll_redirect(assembly))) return FALSE;
1268 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1270 if (xmlstr_cmp(&attr_name, nameW))
1272 if (!(dll->name = xmlstrdupW(&attr_value))) return FALSE;
1273 TRACE("name=%s\n", debugstr_xmlstr(&attr_value));
1275 else if (xmlstr_cmp(&attr_name, hashW))
1277 if (!(dll->hash = xmlstrdupW(&attr_value))) return FALSE;
1279 else if (xmlstr_cmp(&attr_name, hashalgW))
1281 static const WCHAR sha1W[] = {'S','H','A','1',0};
1282 if (!xmlstr_cmpi(&attr_value, sha1W))
1283 FIXME("hashalg should be SHA1, got %s\n", debugstr_xmlstr(&attr_value));
1287 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1291 if (error || !dll->name) return FALSE;
1292 if (end) return TRUE;
1294 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1296 if (xmlstr_cmp_end(&elem, fileW))
1298 ret = parse_end_element(xmlbuf);
1301 else if (xmlstr_cmp(&elem, comClassW))
1303 ret = parse_com_class_elem(xmlbuf, dll);
1305 else if (xmlstr_cmp(&elem, comInterfaceProxyStubW))
1307 ret = parse_cominterface_proxy_stub_elem(xmlbuf, dll);
1309 else if (xmlstr_cmp(&elem, asmv2hashW))
1311 WARN("asmv2hash (undocumented) not supported\n");
1312 ret = parse_unknown_elem(xmlbuf, &elem);
1314 else if (xmlstr_cmp(&elem, typelibW))
1316 ret = parse_typelib_elem(xmlbuf, dll);
1318 else if (xmlstr_cmp(&elem, windowClassW))
1320 ret = parse_window_class_elem(xmlbuf, dll);
1324 WARN("unknown elem %s\n", debugstr_xmlstr(&elem));
1325 ret = parse_unknown_elem( xmlbuf, &elem );
1332 static BOOL parse_assembly_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl,
1333 struct assembly* assembly,
1334 struct assembly_identity* expected_ai)
1336 xmlstr_t attr_name, attr_value, elem;
1337 BOOL end = FALSE, error, version = FALSE, xmlns = FALSE, ret = TRUE;
1339 TRACE("(%p)\n", xmlbuf);
1341 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1343 if (xmlstr_cmp(&attr_name, manifestVersionW))
1345 static const WCHAR v10W[] = {'1','.','0',0};
1346 if (!xmlstr_cmp(&attr_value, v10W))
1348 FIXME("wrong version %s\n", debugstr_xmlstr(&attr_value));
1353 else if (xmlstr_cmp(&attr_name, xmlnsW))
1355 if (!xmlstr_cmp(&attr_value, manifestv1W) && !xmlstr_cmp(&attr_value, manifestv3W))
1357 FIXME("wrong namespace %s\n", debugstr_xmlstr(&attr_value));
1364 WARN("unknown attr %s=%s\n", debugstr_xmlstr(&attr_name), debugstr_xmlstr(&attr_value));
1368 if (error || end || !xmlns || !version) return FALSE;
1369 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
1371 if (assembly->type == APPLICATION_MANIFEST && xmlstr_cmp(&elem, noInheritW))
1373 if (!parse_noinherit_elem(xmlbuf) || !next_xml_elem(xmlbuf, &elem))
1375 assembly->no_inherit = TRUE;
1378 if (xmlstr_cmp(&elem, noInheritableW))
1380 if (!parse_noinheritable_elem(xmlbuf) || !next_xml_elem(xmlbuf, &elem))
1383 else if ((assembly->type == ASSEMBLY_MANIFEST || assembly->type == ASSEMBLY_SHARED_MANIFEST) &&
1384 assembly->no_inherit)
1387 if (xmlstr_cmp(&elem, assemblyIdentityW))
1389 if (!parse_assembly_identity_elem(xmlbuf, acl->actctx, &assembly->id)) return FALSE;
1390 ret = next_xml_elem(xmlbuf, &elem);
1394 /* FIXME: more tests */
1395 if (assembly->type == ASSEMBLY_MANIFEST &&
1396 memcmp(&assembly->id.version, &expected_ai->version, sizeof(assembly->id.version)))
1398 FIXME("wrong version for assembly manifest\n");
1401 else if (assembly->type == ASSEMBLY_SHARED_MANIFEST &&
1402 (assembly->id.version.major != expected_ai->version.major ||
1403 assembly->id.version.minor != expected_ai->version.minor ||
1404 assembly->id.version.build < expected_ai->version.build ||
1405 (assembly->id.version.build == expected_ai->version.build &&
1406 assembly->id.version.revision < expected_ai->version.revision)))
1408 FIXME("wrong version for shared assembly manifest\n");
1416 if (xmlstr_cmp_end(&elem, assemblyW))
1418 ret = parse_end_element(xmlbuf);
1421 else if (xmlstr_cmp(&elem, descriptionW))
1423 ret = parse_description_elem(xmlbuf);
1425 else if (xmlstr_cmp(&elem, comInterfaceExternalProxyStubW))
1427 ret = parse_com_interface_external_proxy_stub_elem(xmlbuf, assembly);
1429 else if (xmlstr_cmp(&elem, dependencyW))
1431 ret = parse_dependency_elem(xmlbuf, acl);
1433 else if (xmlstr_cmp(&elem, fileW))
1435 ret = parse_file_elem(xmlbuf, assembly);
1437 else if (xmlstr_cmp(&elem, clrClassW))
1439 ret = parse_clr_class_elem(xmlbuf, assembly);
1441 else if (xmlstr_cmp(&elem, clrSurrogateW))
1443 ret = parse_clr_surrogate_elem(xmlbuf, assembly);
1447 WARN("unknown element %s\n", debugstr_xmlstr(&elem));
1448 ret = parse_unknown_elem(xmlbuf, &elem);
1450 if (ret) ret = next_xml_elem(xmlbuf, &elem);
1456 static NTSTATUS parse_manifest_buffer( struct actctx_loader* acl, struct assembly *assembly,
1457 struct assembly_identity* ai, xmlbuf_t *xmlbuf )
1461 if (!next_xml_elem(xmlbuf, &elem)) return STATUS_SXS_CANT_GEN_ACTCTX;
1463 if (xmlstr_cmp(&elem, xmlW) &&
1464 (!parse_xml_header(xmlbuf) || !next_xml_elem(xmlbuf, &elem)))
1465 return STATUS_SXS_CANT_GEN_ACTCTX;
1467 if (!xmlstr_cmp(&elem, assemblyW))
1469 FIXME("root element is %s, not <assembly>\n", debugstr_xmlstr(&elem));
1470 return STATUS_SXS_CANT_GEN_ACTCTX;
1473 if (!parse_assembly_elem(xmlbuf, acl, assembly, ai))
1475 FIXME("failed to parse manifest %s\n", debugstr_w(assembly->manifest.info) );
1476 return STATUS_SXS_CANT_GEN_ACTCTX;
1479 if (next_xml_elem(xmlbuf, &elem))
1481 FIXME("unexpected element %s\n", debugstr_xmlstr(&elem));
1482 return STATUS_SXS_CANT_GEN_ACTCTX;
1485 if (xmlbuf->ptr != xmlbuf->end)
1487 FIXME("parse error\n");
1488 return STATUS_SXS_CANT_GEN_ACTCTX;
1490 return STATUS_SUCCESS;
1493 static NTSTATUS parse_manifest( struct actctx_loader* acl, struct assembly_identity* ai,
1494 LPCWSTR filename, LPCWSTR directory, BOOL shared,
1495 const void *buffer, SIZE_T size )
1499 struct assembly *assembly;
1502 TRACE( "parsing manifest loaded from %s base dir %s\n", debugstr_w(filename), debugstr_w(directory) );
1504 if (!(assembly = add_assembly(acl->actctx, shared ? ASSEMBLY_SHARED_MANIFEST : ASSEMBLY_MANIFEST)))
1505 return STATUS_SXS_CANT_GEN_ACTCTX;
1507 if (directory && !(assembly->directory = strdupW(directory)))
1508 return STATUS_NO_MEMORY;
1510 if (filename) assembly->manifest.info = strdupW( filename + 4 /* skip \??\ prefix */ );
1511 assembly->manifest.type = assembly->manifest.info ? ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE
1512 : ACTIVATION_CONTEXT_PATH_TYPE_NONE;
1514 unicode_tests = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1515 if (RtlIsTextUnicode( buffer, size, &unicode_tests ))
1517 xmlbuf.ptr = buffer;
1518 xmlbuf.end = xmlbuf.ptr + size / sizeof(WCHAR);
1519 status = parse_manifest_buffer( acl, assembly, ai, &xmlbuf );
1521 else if (unicode_tests & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
1523 const WCHAR *buf = buffer;
1527 if (!(new_buff = RtlAllocateHeap( GetProcessHeap(), 0, size )))
1528 return STATUS_NO_MEMORY;
1529 for (i = 0; i < size / sizeof(WCHAR); i++)
1530 new_buff[i] = RtlUshortByteSwap( buf[i] );
1531 xmlbuf.ptr = new_buff;
1532 xmlbuf.end = xmlbuf.ptr + size / sizeof(WCHAR);
1533 status = parse_manifest_buffer( acl, assembly, ai, &xmlbuf );
1534 RtlFreeHeap( GetProcessHeap(), 0, new_buff );
1538 /* let's assume utf-8 for now */
1539 int len = wine_utf8_mbstowcs( 0, buffer, size, NULL, 0 );
1544 FIXME( "utf-8 conversion failed\n" );
1545 return STATUS_SXS_CANT_GEN_ACTCTX;
1547 if (!(new_buff = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1548 return STATUS_NO_MEMORY;
1549 wine_utf8_mbstowcs( 0, buffer, size, new_buff, len );
1550 xmlbuf.ptr = new_buff;
1551 xmlbuf.end = xmlbuf.ptr + len;
1552 status = parse_manifest_buffer( acl, assembly, ai, &xmlbuf );
1553 RtlFreeHeap( GetProcessHeap(), 0, new_buff );
1558 static NTSTATUS open_nt_file( HANDLE *handle, UNICODE_STRING *name )
1560 OBJECT_ATTRIBUTES attr;
1563 attr.Length = sizeof(attr);
1564 attr.RootDirectory = 0;
1565 attr.Attributes = OBJ_CASE_INSENSITIVE;
1566 attr.ObjectName = name;
1567 attr.SecurityDescriptor = NULL;
1568 attr.SecurityQualityOfService = NULL;
1569 return NtOpenFile( handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_ALERT );
1572 static NTSTATUS get_module_filename( HMODULE module, UNICODE_STRING *str, unsigned int extra_len )
1578 LdrLockLoaderLock(0, NULL, &magic);
1579 status = LdrFindEntryForAddress( module, &pldr );
1580 if (status == STATUS_SUCCESS)
1582 if ((str->Buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1583 pldr->FullDllName.Length + extra_len + sizeof(WCHAR) )))
1585 memcpy( str->Buffer, pldr->FullDllName.Buffer, pldr->FullDllName.Length + sizeof(WCHAR) );
1586 str->Length = pldr->FullDllName.Length;
1587 str->MaximumLength = pldr->FullDllName.Length + extra_len + sizeof(WCHAR);
1589 else status = STATUS_NO_MEMORY;
1591 LdrUnlockLoaderLock(0, magic);
1595 static NTSTATUS get_manifest_in_module( struct actctx_loader* acl, struct assembly_identity* ai,
1596 LPCWSTR filename, LPCWSTR directory, BOOL shared,
1597 HANDLE hModule, LPCWSTR resname, ULONG lang )
1600 UNICODE_STRING nameW;
1601 LDR_RESOURCE_INFO info;
1602 const IMAGE_RESOURCE_DATA_ENTRY* entry = NULL;
1605 if (TRACE_ON(actctx))
1607 if (!filename && !get_module_filename( hModule, &nameW, 0 ))
1609 TRACE( "looking for res %s in module %p %s\n", debugstr_w(resname),
1610 hModule, debugstr_w(nameW.Buffer) );
1611 RtlFreeUnicodeString( &nameW );
1613 else TRACE( "looking for res %s in module %p %s\n", debugstr_w(resname),
1614 hModule, debugstr_w(filename) );
1617 if (!resname) return STATUS_INVALID_PARAMETER;
1619 info.Type = RT_MANIFEST;
1620 info.Language = lang;
1621 if (!((ULONG_PTR)resname >> 16))
1623 info.Name = (ULONG_PTR)resname;
1624 status = LdrFindResource_U(hModule, &info, 3, &entry);
1626 else if (resname[0] == '#')
1629 RtlInitUnicodeString(&nameW, resname + 1);
1630 if (RtlUnicodeStringToInteger(&nameW, 10, &value) != STATUS_SUCCESS || HIWORD(value))
1631 return STATUS_INVALID_PARAMETER;
1633 status = LdrFindResource_U(hModule, &info, 3, &entry);
1637 RtlCreateUnicodeString(&nameW, resname);
1638 RtlUpcaseUnicodeString(&nameW, &nameW, FALSE);
1639 info.Name = (ULONG_PTR)nameW.Buffer;
1640 status = LdrFindResource_U(hModule, &info, 3, &entry);
1641 RtlFreeUnicodeString(&nameW);
1643 if (status == STATUS_SUCCESS) status = LdrAccessResource(hModule, entry, &ptr, NULL);
1645 if (status == STATUS_SUCCESS)
1646 status = parse_manifest(acl, ai, filename, directory, shared, ptr, entry->Size);
1651 static NTSTATUS get_manifest_in_pe_file( struct actctx_loader* acl, struct assembly_identity* ai,
1652 LPCWSTR filename, LPCWSTR directory, BOOL shared,
1653 HANDLE file, LPCWSTR resname, ULONG lang )
1656 OBJECT_ATTRIBUTES attr;
1658 LARGE_INTEGER offset;
1663 TRACE( "looking for res %s in %s\n", debugstr_w(resname), debugstr_w(filename) );
1665 attr.Length = sizeof(attr);
1666 attr.RootDirectory = 0;
1667 attr.ObjectName = NULL;
1668 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1669 attr.SecurityDescriptor = NULL;
1670 attr.SecurityQualityOfService = NULL;
1673 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
1674 &attr, &size, PAGE_READONLY, SEC_COMMIT, file );
1675 if (status != STATUS_SUCCESS) return status;
1677 offset.QuadPart = 0;
1680 status = NtMapViewOfSection( mapping, GetCurrentProcess(), &base, 0, 0, &offset,
1681 &count, ViewShare, 0, PAGE_READONLY );
1683 if (status != STATUS_SUCCESS) return status;
1685 if (RtlImageNtHeader(base)) /* we got a PE file */
1687 HANDLE module = (HMODULE)((ULONG_PTR)base | 1); /* make it a LOAD_LIBRARY_AS_DATAFILE handle */
1688 status = get_manifest_in_module( acl, ai, filename, directory, shared, module, resname, lang );
1690 else status = STATUS_INVALID_IMAGE_FORMAT;
1692 NtUnmapViewOfSection( GetCurrentProcess(), base );
1696 static NTSTATUS get_manifest_in_manifest_file( struct actctx_loader* acl, struct assembly_identity* ai,
1697 LPCWSTR filename, LPCWSTR directory, BOOL shared, HANDLE file )
1699 FILE_END_OF_FILE_INFORMATION info;
1702 OBJECT_ATTRIBUTES attr;
1704 LARGE_INTEGER offset;
1709 TRACE( "loading manifest file %s\n", debugstr_w(filename) );
1711 attr.Length = sizeof(attr);
1712 attr.RootDirectory = 0;
1713 attr.ObjectName = NULL;
1714 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1715 attr.SecurityDescriptor = NULL;
1716 attr.SecurityQualityOfService = NULL;
1719 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
1720 &attr, &size, PAGE_READONLY, SEC_COMMIT, file );
1721 if (status != STATUS_SUCCESS) return status;
1723 offset.QuadPart = 0;
1726 status = NtMapViewOfSection( mapping, GetCurrentProcess(), &base, 0, 0, &offset,
1727 &count, ViewShare, 0, PAGE_READONLY );
1729 if (status != STATUS_SUCCESS) return status;
1731 status = NtQueryInformationFile( file, &io, &info, sizeof(info), FileEndOfFileInformation );
1732 if (status == STATUS_SUCCESS)
1733 status = parse_manifest(acl, ai, filename, directory, shared, base, info.EndOfFile.QuadPart);
1735 NtUnmapViewOfSection( GetCurrentProcess(), base );
1739 /* try to load the .manifest file associated to the file */
1740 static NTSTATUS get_manifest_in_associated_manifest( struct actctx_loader* acl, struct assembly_identity* ai,
1741 LPCWSTR filename, LPCWSTR directory, HMODULE module, LPCWSTR resname )
1743 static const WCHAR fmtW[] = { '.','%','l','u',0 };
1746 UNICODE_STRING nameW;
1748 ULONG_PTR resid = CREATEPROCESS_MANIFEST_RESOURCE_ID;
1750 if (!((ULONG_PTR)resname >> 16)) resid = (ULONG_PTR)resname & 0xffff;
1752 TRACE( "looking for manifest associated with %s id %lu\n", debugstr_w(filename), resid );
1754 if (module) /* use the module filename */
1756 UNICODE_STRING name;
1758 if (!(status = get_module_filename( module, &name, sizeof(dotManifestW) + 10*sizeof(WCHAR) )))
1760 if (resid != 1) sprintfW( name.Buffer + strlenW(name.Buffer), fmtW, resid );
1761 strcatW( name.Buffer, dotManifestW );
1762 if (!RtlDosPathNameToNtPathName_U( name.Buffer, &nameW, NULL, NULL ))
1763 status = STATUS_RESOURCE_DATA_NOT_FOUND;
1764 RtlFreeUnicodeString( &name );
1766 if (status) return status;
1770 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1771 (strlenW(filename) + 10) * sizeof(WCHAR) + sizeof(dotManifestW) )))
1772 return STATUS_NO_MEMORY;
1773 strcpyW( buffer, filename );
1774 if (resid != 1) sprintfW( buffer + strlenW(buffer), fmtW, resid );
1775 strcatW( buffer, dotManifestW );
1776 RtlInitUnicodeString( &nameW, buffer );
1779 if (!open_nt_file( &file, &nameW ))
1781 status = get_manifest_in_manifest_file( acl, ai, nameW.Buffer, directory, FALSE, file );
1784 else status = STATUS_RESOURCE_DATA_NOT_FOUND;
1785 RtlFreeUnicodeString( &nameW );
1789 static WCHAR *lookup_manifest_file( HANDLE dir, struct assembly_identity *ai )
1791 static const WCHAR lookup_fmtW[] =
1792 {'%','s','_','%','s','_','%','s','_','%','u','.','%','u','.','*','.','*','_',
1794 '.','m','a','n','i','f','e','s','t',0};
1796 WCHAR *lookup, *ret = NULL;
1797 UNICODE_STRING lookup_us;
1799 unsigned int data_pos = 0, data_len;
1802 if (!(lookup = RtlAllocateHeap( GetProcessHeap(), 0,
1803 (strlenW(ai->arch) + strlenW(ai->name)
1804 + strlenW(ai->public_key) + 20) * sizeof(WCHAR)
1805 + sizeof(lookup_fmtW) )))
1808 sprintfW( lookup, lookup_fmtW, ai->arch, ai->name, ai->public_key, ai->version.major, ai->version.minor);
1809 RtlInitUnicodeString( &lookup_us, lookup );
1811 NtQueryDirectoryFile( dir, 0, NULL, NULL, &io, buffer, sizeof(buffer),
1812 FileBothDirectoryInformation, FALSE, &lookup_us, TRUE );
1813 if (io.u.Status == STATUS_SUCCESS)
1815 FILE_BOTH_DIR_INFORMATION *dir_info;
1817 ULONG build, revision;
1819 data_len = io.Information;
1823 if (data_pos >= data_len)
1825 NtQueryDirectoryFile( dir, 0, NULL, NULL, &io, buffer, sizeof(buffer),
1826 FileBothDirectoryInformation, FALSE, &lookup_us, FALSE );
1827 if (io.u.Status != STATUS_SUCCESS) break;
1828 data_len = io.Information;
1831 dir_info = (FILE_BOTH_DIR_INFORMATION*)(buffer + data_pos);
1833 if (dir_info->NextEntryOffset) data_pos += dir_info->NextEntryOffset;
1834 else data_pos = data_len;
1836 tmp = (WCHAR *)dir_info->FileName + (strchrW(lookup, '*') - lookup);
1838 if (build < ai->version.build) continue;
1839 tmp = strchrW(tmp, '.') + 1;
1840 revision = atoiW(tmp);
1841 if (build == ai->version.build && revision < ai->version.revision)
1843 ai->version.build = build;
1844 ai->version.revision = revision;
1845 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, dir_info->FileNameLength + sizeof(WCHAR) )))
1847 memcpy( ret, dir_info->FileName, dir_info->FileNameLength );
1848 ret[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
1853 else WARN("no matching file for %s\n", debugstr_w(lookup));
1854 RtlFreeHeap( GetProcessHeap(), 0, lookup );
1858 static NTSTATUS lookup_winsxs(struct actctx_loader* acl, struct assembly_identity* ai)
1860 struct assembly_identity sxs_ai;
1861 UNICODE_STRING path_us;
1862 OBJECT_ATTRIBUTES attr;
1864 WCHAR *path, *file = NULL;
1867 static const WCHAR manifest_dirW[] =
1868 {'\\','w','i','n','s','x','s','\\','m','a','n','i','f','e','s','t','s',0};
1870 if (!ai->arch || !ai->name || !ai->public_key) return STATUS_NO_SUCH_FILE;
1872 if (!(path = RtlAllocateHeap( GetProcessHeap(), 0, windows_dir.Length + sizeof(manifest_dirW) )))
1873 return STATUS_NO_MEMORY;
1875 memcpy( path, windows_dir.Buffer, windows_dir.Length );
1876 memcpy( path + windows_dir.Length/sizeof(WCHAR), manifest_dirW, sizeof(manifest_dirW) );
1878 if (!RtlDosPathNameToNtPathName_U( path, &path_us, NULL, NULL ))
1880 RtlFreeHeap( GetProcessHeap(), 0, path );
1881 return STATUS_NO_SUCH_FILE;
1883 RtlFreeHeap( GetProcessHeap(), 0, path );
1885 attr.Length = sizeof(attr);
1886 attr.RootDirectory = 0;
1887 attr.Attributes = OBJ_CASE_INSENSITIVE;
1888 attr.ObjectName = &path_us;
1889 attr.SecurityDescriptor = NULL;
1890 attr.SecurityQualityOfService = NULL;
1892 if (!NtOpenFile( &handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
1893 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT ))
1896 file = lookup_manifest_file( handle, &sxs_ai );
1901 RtlFreeUnicodeString( &path_us );
1902 return STATUS_NO_SUCH_FILE;
1905 /* append file name to directory path */
1906 if (!(path = RtlReAllocateHeap( GetProcessHeap(), 0, path_us.Buffer,
1907 path_us.Length + (strlenW(file) + 2) * sizeof(WCHAR) )))
1909 RtlFreeHeap( GetProcessHeap(), 0, file );
1910 RtlFreeUnicodeString( &path_us );
1911 return STATUS_NO_MEMORY;
1914 path[path_us.Length/sizeof(WCHAR)] = '\\';
1915 strcpyW( path + path_us.Length/sizeof(WCHAR) + 1, file );
1916 RtlInitUnicodeString( &path_us, path );
1917 *strrchrW(file, '.') = 0; /* remove .manifest extension */
1919 if (!open_nt_file( &handle, &path_us ))
1921 io.u.Status = get_manifest_in_manifest_file(acl, &sxs_ai, path_us.Buffer, file, TRUE, handle);
1924 else io.u.Status = STATUS_NO_SUCH_FILE;
1926 RtlFreeHeap( GetProcessHeap(), 0, file );
1927 RtlFreeUnicodeString( &path_us );
1931 static NTSTATUS lookup_assembly(struct actctx_loader* acl,
1932 struct assembly_identity* ai)
1934 static const WCHAR dotDllW[] = {'.','d','l','l',0};
1936 WCHAR *buffer, *p, *directory;
1938 UNICODE_STRING nameW;
1941 TRACE( "looking for name=%s version=%s arch=%s\n",
1942 debugstr_w(ai->name), debugstr_version(&ai->version), debugstr_w(ai->arch) );
1944 if ((status = lookup_winsxs(acl, ai)) != STATUS_NO_SUCH_FILE) return status;
1946 /* FIXME: add support for language specific lookup */
1948 nameW.Buffer = NULL;
1949 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1950 (strlenW(acl->actctx->appdir.info) + 2 * strlenW(ai->name) + 2) * sizeof(WCHAR) + sizeof(dotManifestW) )))
1951 return STATUS_NO_MEMORY;
1953 if (!(directory = build_assembly_dir( ai )))
1955 RtlFreeHeap( GetProcessHeap(), 0, buffer );
1956 return STATUS_NO_MEMORY;
1959 /* lookup in appdir\name.dll
1960 * appdir\name.manifest
1961 * appdir\name\name.dll
1962 * appdir\name\name.manifest
1964 strcpyW( buffer, acl->actctx->appdir.info );
1965 p = buffer + strlenW(buffer);
1966 for (i = 0; i < 2; i++)
1969 strcpyW( p, ai->name );
1972 strcpyW( p, dotDllW );
1973 if (RtlDosPathNameToNtPathName_U( buffer, &nameW, NULL, NULL ))
1975 status = open_nt_file( &file, &nameW );
1978 status = get_manifest_in_pe_file( acl, ai, nameW.Buffer, directory, FALSE, file,
1979 (LPCWSTR)CREATEPROCESS_MANIFEST_RESOURCE_ID, 0 );
1983 RtlFreeUnicodeString( &nameW );
1986 strcpyW( p, dotManifestW );
1987 if (RtlDosPathNameToNtPathName_U( buffer, &nameW, NULL, NULL ))
1989 status = open_nt_file( &file, &nameW );
1992 status = get_manifest_in_manifest_file( acl, ai, nameW.Buffer, directory, FALSE, file );
1996 RtlFreeUnicodeString( &nameW );
1998 status = STATUS_SXS_ASSEMBLY_NOT_FOUND;
2000 RtlFreeUnicodeString( &nameW );
2001 RtlFreeHeap( GetProcessHeap(), 0, directory );
2002 RtlFreeHeap( GetProcessHeap(), 0, buffer );
2006 static NTSTATUS parse_depend_manifests(struct actctx_loader* acl)
2008 NTSTATUS status = STATUS_SUCCESS;
2011 for (i = 0; i < acl->num_dependencies; i++)
2013 if (lookup_assembly(acl, &acl->dependencies[i]) != STATUS_SUCCESS)
2015 if (!acl->dependencies[i].optional)
2017 FIXME( "Could not find dependent assembly %s\n", debugstr_w(acl->dependencies[i].name) );
2018 status = STATUS_SXS_CANT_GEN_ACTCTX;
2023 /* FIXME should now iterate through all refs */
2027 /* find the appropriate activation context for RtlQueryInformationActivationContext */
2028 static NTSTATUS find_query_actctx( HANDLE *handle, DWORD flags )
2030 NTSTATUS status = STATUS_SUCCESS;
2032 if (flags & QUERY_ACTCTX_FLAG_USE_ACTIVE_ACTCTX)
2034 if (NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2035 *handle = NtCurrentTeb()->ActivationContextStack.ActiveFrame->ActivationContext;
2037 else if (flags & (QUERY_ACTCTX_FLAG_ACTCTX_IS_ADDRESS|QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE))
2042 LdrLockLoaderLock( 0, NULL, &magic );
2043 if (!LdrFindEntryForAddress( *handle, &pldr ))
2045 if ((flags & QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE) && *handle != pldr->BaseAddress)
2046 status = STATUS_DLL_NOT_FOUND;
2048 *handle = pldr->ActivationContext;
2050 else status = STATUS_DLL_NOT_FOUND;
2051 LdrUnlockLoaderLock( 0, magic );
2053 else if (!*handle) *handle = process_actctx;
2058 static NTSTATUS fill_keyed_data(PACTCTX_SECTION_KEYED_DATA data, PVOID v1, PVOID v2, unsigned int i)
2060 data->ulDataFormatVersion = 1;
2062 data->ulLength = 20; /* FIXME */
2063 data->lpSectionGlobalData = NULL; /* FIXME */
2064 data->ulSectionGlobalDataLength = 0; /* FIXME */
2065 data->lpSectionBase = v2;
2066 data->ulSectionTotalLength = 0; /* FIXME */
2067 data->hActCtx = NULL;
2068 if (data->cbSize >= offsetof(ACTCTX_SECTION_KEYED_DATA, ulAssemblyRosterIndex) + sizeof(ULONG))
2069 data->ulAssemblyRosterIndex = i + 1;
2071 return STATUS_SUCCESS;
2074 static NTSTATUS find_dll_redirection(ACTIVATION_CONTEXT* actctx, const UNICODE_STRING *section_name,
2075 PACTCTX_SECTION_KEYED_DATA data)
2077 unsigned int i, j, snlen = section_name->Length / sizeof(WCHAR);
2079 for (i = 0; i < actctx->num_assemblies; i++)
2081 struct assembly *assembly = &actctx->assemblies[i];
2082 for (j = 0; j < assembly->num_dlls; j++)
2084 struct dll_redirect *dll = &assembly->dlls[j];
2085 if (!strncmpiW(section_name->Buffer, dll->name, snlen) && !dll->name[snlen])
2086 return fill_keyed_data(data, dll, assembly, i);
2089 return STATUS_SXS_KEY_NOT_FOUND;
2092 static NTSTATUS find_window_class(ACTIVATION_CONTEXT* actctx, const UNICODE_STRING *section_name,
2093 PACTCTX_SECTION_KEYED_DATA data)
2095 unsigned int i, j, k, snlen = section_name->Length / sizeof(WCHAR);
2097 for (i = 0; i < actctx->num_assemblies; i++)
2099 struct assembly *assembly = &actctx->assemblies[i];
2100 for (j = 0; j < assembly->num_dlls; j++)
2102 struct dll_redirect *dll = &assembly->dlls[j];
2103 for (k = 0; k < dll->entities.num; k++)
2105 struct entity *entity = &dll->entities.base[k];
2106 if (entity->kind == ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION)
2108 if (!strncmpiW(section_name->Buffer, entity->u.class.name, snlen) && !entity->u.class.name[snlen])
2109 return fill_keyed_data(data, entity, dll, i);
2114 return STATUS_SXS_KEY_NOT_FOUND;
2117 static NTSTATUS find_string(ACTIVATION_CONTEXT* actctx, ULONG section_kind,
2118 const UNICODE_STRING *section_name,
2119 DWORD flags, PACTCTX_SECTION_KEYED_DATA data)
2123 switch (section_kind)
2125 case ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION:
2126 status = find_dll_redirection(actctx, section_name, data);
2128 case ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION:
2129 status = find_window_class(actctx, section_name, data);
2131 case ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION:
2132 case ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION:
2133 case ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION:
2134 case ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION:
2135 case ACTIVATION_CONTEXT_SECTION_GLOBAL_OBJECT_RENAME_TABLE:
2136 case ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES:
2137 FIXME("Unsupported yet section_kind %x\n", section_kind);
2138 return STATUS_SXS_SECTION_NOT_FOUND;
2140 WARN("Unknown section_kind %x\n", section_kind);
2141 return STATUS_SXS_SECTION_NOT_FOUND;
2144 if (status != STATUS_SUCCESS) return status;
2146 if (flags & FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX)
2148 actctx_addref(actctx);
2149 data->hActCtx = actctx;
2151 return STATUS_SUCCESS;
2154 /* initialize the activation context for the current process */
2155 void actctx_init(void)
2160 ctx.cbSize = sizeof(ctx);
2161 ctx.lpSource = NULL;
2162 ctx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID | ACTCTX_FLAG_HMODULE_VALID;
2163 ctx.hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
2164 ctx.lpResourceName = (LPCWSTR)CREATEPROCESS_MANIFEST_RESOURCE_ID;
2166 if (!RtlCreateActivationContext( &handle, &ctx )) process_actctx = check_actctx(handle);
2170 /***********************************************************************
2171 * RtlCreateActivationContext (NTDLL.@)
2173 * Create an activation context.
2175 * FIXME: function signature/prototype is wrong
2177 NTSTATUS WINAPI RtlCreateActivationContext( HANDLE *handle, const void *ptr )
2179 const ACTCTXW *pActCtx = ptr; /* FIXME: not the right structure */
2180 const WCHAR *directory = NULL;
2181 ACTIVATION_CONTEXT *actctx;
2182 UNICODE_STRING nameW;
2184 NTSTATUS status = STATUS_NO_MEMORY;
2186 struct actctx_loader acl;
2188 TRACE("%p %08x\n", pActCtx, pActCtx ? pActCtx->dwFlags : 0);
2190 if (!pActCtx || pActCtx->cbSize < sizeof(*pActCtx) ||
2191 (pActCtx->dwFlags & ~ACTCTX_FLAGS_ALL))
2192 return STATUS_INVALID_PARAMETER;
2194 if (!(actctx = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*actctx) )))
2195 return STATUS_NO_MEMORY;
2197 actctx->magic = ACTCTX_MAGIC;
2198 actctx->ref_count = 1;
2199 actctx->config.type = ACTIVATION_CONTEXT_PATH_TYPE_NONE;
2200 actctx->config.info = NULL;
2201 actctx->appdir.type = ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE;
2202 if (pActCtx->dwFlags & ACTCTX_FLAG_APPLICATION_NAME_VALID)
2204 if (!(actctx->appdir.info = strdupW( pActCtx->lpApplicationName ))) goto error;
2211 if ((status = get_module_filename( NtCurrentTeb()->Peb->ImageBaseAddress, &dir, 0 )))
2213 if ((p = strrchrW( dir.Buffer, '\\' ))) p[1] = 0;
2214 actctx->appdir.info = dir.Buffer;
2217 nameW.Buffer = NULL;
2218 if (pActCtx->lpSource)
2220 if (!RtlDosPathNameToNtPathName_U(pActCtx->lpSource, &nameW, NULL, NULL))
2222 status = STATUS_NO_SUCH_FILE;
2225 status = open_nt_file( &file, &nameW );
2228 RtlFreeUnicodeString( &nameW );
2233 acl.actctx = actctx;
2234 acl.dependencies = NULL;
2235 acl.num_dependencies = 0;
2236 acl.allocated_dependencies = 0;
2238 if (pActCtx->dwFlags & ACTCTX_FLAG_LANGID_VALID) lang = pActCtx->wLangId;
2239 if (pActCtx->dwFlags & ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID) directory = pActCtx->lpAssemblyDirectory;
2241 if (pActCtx->dwFlags & ACTCTX_FLAG_RESOURCE_NAME_VALID)
2243 /* if we have a resource it's a PE file */
2244 if (pActCtx->dwFlags & ACTCTX_FLAG_HMODULE_VALID)
2246 status = get_manifest_in_module( &acl, NULL, NULL, directory, FALSE, pActCtx->hModule,
2247 pActCtx->lpResourceName, lang );
2248 if (status && status != STATUS_SXS_CANT_GEN_ACTCTX)
2249 /* FIXME: what to do if pActCtx->lpSource is set */
2250 status = get_manifest_in_associated_manifest( &acl, NULL, NULL, directory,
2251 pActCtx->hModule, pActCtx->lpResourceName );
2253 else if (pActCtx->lpSource)
2255 status = get_manifest_in_pe_file( &acl, NULL, nameW.Buffer, directory, FALSE,
2256 file, pActCtx->lpResourceName, lang );
2257 if (status && status != STATUS_SXS_CANT_GEN_ACTCTX)
2258 status = get_manifest_in_associated_manifest( &acl, NULL, nameW.Buffer, directory,
2259 NULL, pActCtx->lpResourceName );
2261 else status = STATUS_INVALID_PARAMETER;
2265 status = get_manifest_in_manifest_file( &acl, NULL, nameW.Buffer, directory, FALSE, file );
2268 if (file) NtClose( file );
2269 RtlFreeUnicodeString( &nameW );
2271 if (status == STATUS_SUCCESS) status = parse_depend_manifests(&acl);
2272 free_depend_manifests( &acl );
2274 if (status == STATUS_SUCCESS) *handle = actctx;
2275 else actctx_release( actctx );
2279 if (file) NtClose( file );
2280 actctx_release( actctx );
2285 /***********************************************************************
2286 * RtlAddRefActivationContext (NTDLL.@)
2288 void WINAPI RtlAddRefActivationContext( HANDLE handle )
2290 ACTIVATION_CONTEXT *actctx;
2292 if ((actctx = check_actctx( handle ))) actctx_addref( actctx );
2296 /******************************************************************
2297 * RtlReleaseActivationContext (NTDLL.@)
2299 void WINAPI RtlReleaseActivationContext( HANDLE handle )
2301 ACTIVATION_CONTEXT *actctx;
2303 if ((actctx = check_actctx( handle ))) actctx_release( actctx );
2307 /******************************************************************
2308 * RtlActivateActivationContext (NTDLL.@)
2310 NTSTATUS WINAPI RtlActivateActivationContext( ULONG unknown, HANDLE handle, PULONG_PTR cookie )
2312 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2314 if (!(frame = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*frame) )))
2315 return STATUS_NO_MEMORY;
2317 frame->Previous = NtCurrentTeb()->ActivationContextStack.ActiveFrame;
2318 frame->ActivationContext = handle;
2320 NtCurrentTeb()->ActivationContextStack.ActiveFrame = frame;
2321 RtlAddRefActivationContext( handle );
2323 *cookie = (ULONG_PTR)frame;
2324 TRACE( "%p cookie=%lx\n", handle, *cookie );
2325 return STATUS_SUCCESS;
2329 /***********************************************************************
2330 * RtlDeactivateActivationContext (NTDLL.@)
2332 void WINAPI RtlDeactivateActivationContext( ULONG flags, ULONG_PTR cookie )
2334 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame, *top;
2336 TRACE( "%x cookie=%lx\n", flags, cookie );
2338 /* find the right frame */
2339 top = NtCurrentTeb()->ActivationContextStack.ActiveFrame;
2340 for (frame = top; frame; frame = frame->Previous)
2341 if ((ULONG_PTR)frame == cookie) break;
2344 RtlRaiseStatus( STATUS_SXS_INVALID_DEACTIVATION );
2346 if (frame != top && !(flags & DEACTIVATE_ACTCTX_FLAG_FORCE_EARLY_DEACTIVATION))
2347 RtlRaiseStatus( STATUS_SXS_EARLY_DEACTIVATION );
2349 /* pop everything up to and including frame */
2350 NtCurrentTeb()->ActivationContextStack.ActiveFrame = frame->Previous;
2352 while (top != NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2354 frame = top->Previous;
2355 RtlReleaseActivationContext( top->ActivationContext );
2356 RtlFreeHeap( GetProcessHeap(), 0, top );
2362 /******************************************************************
2363 * RtlFreeThreadActivationContextStack (NTDLL.@)
2365 void WINAPI RtlFreeThreadActivationContextStack(void)
2367 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2369 frame = NtCurrentTeb()->ActivationContextStack.ActiveFrame;
2372 RTL_ACTIVATION_CONTEXT_STACK_FRAME *prev = frame->Previous;
2373 RtlReleaseActivationContext( frame->ActivationContext );
2374 RtlFreeHeap( GetProcessHeap(), 0, frame );
2377 NtCurrentTeb()->ActivationContextStack.ActiveFrame = NULL;
2381 /******************************************************************
2382 * RtlGetActiveActivationContext (NTDLL.@)
2384 NTSTATUS WINAPI RtlGetActiveActivationContext( HANDLE *handle )
2386 if (NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2388 *handle = NtCurrentTeb()->ActivationContextStack.ActiveFrame->ActivationContext;
2389 RtlAddRefActivationContext( *handle );
2394 return STATUS_SUCCESS;
2398 /******************************************************************
2399 * RtlIsActivationContextActive (NTDLL.@)
2401 BOOLEAN WINAPI RtlIsActivationContextActive( HANDLE handle )
2403 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2405 for (frame = NtCurrentTeb()->ActivationContextStack.ActiveFrame; frame; frame = frame->Previous)
2406 if (frame->ActivationContext == handle) return TRUE;
2411 /***********************************************************************
2412 * RtlQueryInformationActivationContext (NTDLL.@)
2414 * Get information about an activation context.
2415 * FIXME: function signature/prototype may be wrong
2417 NTSTATUS WINAPI RtlQueryInformationActivationContext( ULONG flags, HANDLE handle, PVOID subinst,
2418 ULONG class, PVOID buffer,
2419 SIZE_T bufsize, SIZE_T *retlen )
2421 ACTIVATION_CONTEXT *actctx;
2424 TRACE("%08x %p %p %u %p %ld %p\n", flags, handle,
2425 subinst, class, buffer, bufsize, retlen);
2427 if ((status = find_query_actctx( &handle, flags ))) return status;
2431 case ActivationContextBasicInformation:
2433 ACTIVATION_CONTEXT_BASIC_INFORMATION *info = buffer;
2435 if (retlen) *retlen = sizeof(*info);
2436 if (!info || bufsize < sizeof(*info)) return STATUS_BUFFER_TOO_SMALL;
2438 info->hActCtx = handle;
2439 info->dwFlags = 0; /* FIXME */
2440 if (!(flags & QUERY_ACTCTX_FLAG_NO_ADDREF)) RtlAddRefActivationContext( handle );
2444 case ActivationContextDetailedInformation:
2446 ACTIVATION_CONTEXT_DETAILED_INFORMATION *acdi = buffer;
2447 struct assembly *assembly = NULL;
2448 SIZE_T len, manifest_len = 0, config_len = 0, appdir_len = 0;
2451 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2453 if (actctx->num_assemblies) assembly = actctx->assemblies;
2455 if (assembly && assembly->manifest.info)
2456 manifest_len = strlenW(assembly->manifest.info) + 1;
2457 if (actctx->config.info) config_len = strlenW(actctx->config.info) + 1;
2458 if (actctx->appdir.info) appdir_len = strlenW(actctx->appdir.info) + 1;
2459 len = sizeof(*acdi) + (manifest_len + config_len + appdir_len) * sizeof(WCHAR);
2461 if (retlen) *retlen = len;
2462 if (!buffer || bufsize < len) return STATUS_BUFFER_TOO_SMALL;
2465 acdi->ulFormatVersion = assembly ? 1 : 0; /* FIXME */
2466 acdi->ulAssemblyCount = actctx->num_assemblies;
2467 acdi->ulRootManifestPathType = assembly ? assembly->manifest.type : 0 /* FIXME */;
2468 acdi->ulRootManifestPathChars = assembly && assembly->manifest.info ? manifest_len - 1 : 0;
2469 acdi->ulRootConfigurationPathType = actctx->config.type;
2470 acdi->ulRootConfigurationPathChars = actctx->config.info ? config_len - 1 : 0;
2471 acdi->ulAppDirPathType = actctx->appdir.type;
2472 acdi->ulAppDirPathChars = actctx->appdir.info ? appdir_len - 1 : 0;
2473 ptr = (LPWSTR)(acdi + 1);
2476 acdi->lpRootManifestPath = ptr;
2477 memcpy(ptr, assembly->manifest.info, manifest_len * sizeof(WCHAR));
2478 ptr += manifest_len;
2480 else acdi->lpRootManifestPath = NULL;
2483 acdi->lpRootConfigurationPath = ptr;
2484 memcpy(ptr, actctx->config.info, config_len * sizeof(WCHAR));
2487 else acdi->lpRootConfigurationPath = NULL;
2490 acdi->lpAppDirPath = ptr;
2491 memcpy(ptr, actctx->appdir.info, appdir_len * sizeof(WCHAR));
2493 else acdi->lpAppDirPath = NULL;
2497 case AssemblyDetailedInformationInActivationContext:
2499 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION *afdi = buffer;
2500 struct assembly *assembly;
2503 SIZE_T len, id_len = 0, ad_len = 0, path_len = 0;
2506 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2507 if (!subinst) return STATUS_INVALID_PARAMETER;
2509 index = *(DWORD*)subinst;
2510 if (!index || index > actctx->num_assemblies) return STATUS_INVALID_PARAMETER;
2512 assembly = &actctx->assemblies[index - 1];
2514 if (!(assembly_id = build_assembly_id( &assembly->id ))) return STATUS_NO_MEMORY;
2515 id_len = strlenW(assembly_id) + 1;
2516 if (assembly->directory) ad_len = strlenW(assembly->directory) + 1;
2518 if (assembly->manifest.info &&
2519 (assembly->type == ASSEMBLY_MANIFEST || assembly->type == ASSEMBLY_SHARED_MANIFEST))
2520 path_len = strlenW(assembly->manifest.info) + 1;
2522 len = sizeof(*afdi) + (id_len + ad_len + path_len) * sizeof(WCHAR);
2524 if (retlen) *retlen = len;
2525 if (!buffer || bufsize < len)
2527 RtlFreeHeap( GetProcessHeap(), 0, assembly_id );
2528 return STATUS_BUFFER_TOO_SMALL;
2531 afdi->ulFlags = 0; /* FIXME */
2532 afdi->ulEncodedAssemblyIdentityLength = (id_len - 1) * sizeof(WCHAR);
2533 afdi->ulManifestPathType = assembly->manifest.type;
2534 afdi->ulManifestPathLength = assembly->manifest.info ? (path_len - 1) * sizeof(WCHAR) : 0;
2535 /* FIXME afdi->liManifestLastWriteTime = 0; */
2536 afdi->ulPolicyPathType = ACTIVATION_CONTEXT_PATH_TYPE_NONE; /* FIXME */
2537 afdi->ulPolicyPathLength = 0;
2538 /* FIXME afdi->liPolicyLastWriteTime = 0; */
2539 afdi->ulMetadataSatelliteRosterIndex = 0; /* FIXME */
2540 afdi->ulManifestVersionMajor = 1;
2541 afdi->ulManifestVersionMinor = 0;
2542 afdi->ulPolicyVersionMajor = 0; /* FIXME */
2543 afdi->ulPolicyVersionMinor = 0; /* FIXME */
2544 afdi->ulAssemblyDirectoryNameLength = ad_len ? (ad_len - 1) * sizeof(WCHAR) : 0;
2545 ptr = (LPWSTR)(afdi + 1);
2546 afdi->lpAssemblyEncodedAssemblyIdentity = ptr;
2547 memcpy( ptr, assembly_id, id_len * sizeof(WCHAR) );
2551 afdi->lpAssemblyManifestPath = ptr;
2552 memcpy(ptr, assembly->manifest.info, path_len * sizeof(WCHAR));
2554 } else afdi->lpAssemblyManifestPath = NULL;
2555 afdi->lpAssemblyPolicyPath = NULL; /* FIXME */
2558 afdi->lpAssemblyDirectoryName = ptr;
2559 memcpy(ptr, assembly->directory, ad_len * sizeof(WCHAR));
2562 else afdi->lpAssemblyDirectoryName = NULL;
2563 RtlFreeHeap( GetProcessHeap(), 0, assembly_id );
2567 case FileInformationInAssemblyOfAssemblyInActivationContext:
2569 const ACTIVATION_CONTEXT_QUERY_INDEX *acqi = subinst;
2570 ASSEMBLY_FILE_DETAILED_INFORMATION *afdi = buffer;
2571 struct assembly *assembly;
2572 struct dll_redirect *dll;
2573 SIZE_T len, dll_len = 0;
2576 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2577 if (!acqi) return STATUS_INVALID_PARAMETER;
2579 if (acqi->ulAssemblyIndex >= actctx->num_assemblies)
2580 return STATUS_INVALID_PARAMETER;
2581 assembly = &actctx->assemblies[acqi->ulAssemblyIndex];
2583 if (acqi->ulFileIndexInAssembly >= assembly->num_dlls)
2584 return STATUS_INVALID_PARAMETER;
2585 dll = &assembly->dlls[acqi->ulFileIndexInAssembly];
2587 if (dll->name) dll_len = strlenW(dll->name) + 1;
2588 len = sizeof(*afdi) + dll_len * sizeof(WCHAR);
2590 if (!buffer || bufsize < len)
2592 if (retlen) *retlen = len;
2593 return STATUS_BUFFER_TOO_SMALL;
2595 if (retlen) *retlen = 0; /* yes that's what native does !! */
2596 afdi->ulFlags = ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION;
2597 afdi->ulFilenameLength = dll_len ? (dll_len - 1) * sizeof(WCHAR) : 0;
2598 afdi->ulPathLength = 0; /* FIXME */
2599 ptr = (LPWSTR)(afdi + 1);
2602 afdi->lpFileName = ptr;
2603 memcpy( ptr, dll->name, dll_len * sizeof(WCHAR) );
2604 } else afdi->lpFileName = NULL;
2605 afdi->lpFilePath = NULL; /* FIXME */
2610 FIXME( "class %u not implemented\n", class );
2611 return STATUS_NOT_IMPLEMENTED;
2613 return STATUS_SUCCESS;
2616 /***********************************************************************
2617 * RtlFindActivationContextSectionString (NTDLL.@)
2619 * Find information about a string in an activation context.
2620 * FIXME: function signature/prototype may be wrong
2622 NTSTATUS WINAPI RtlFindActivationContextSectionString( ULONG flags, const GUID *guid, ULONG section_kind,
2623 const UNICODE_STRING *section_name, PVOID ptr )
2625 PACTCTX_SECTION_KEYED_DATA data = ptr;
2626 NTSTATUS status = STATUS_SXS_KEY_NOT_FOUND;
2628 TRACE("%08x %s %u %s %p\n", flags, debugstr_guid(guid), section_kind,
2629 debugstr_us(section_name), data);
2633 FIXME("expected guid == NULL\n");
2634 return STATUS_INVALID_PARAMETER;
2636 if (flags & ~FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX)
2638 FIXME("unknown flags %08x\n", flags);
2639 return STATUS_INVALID_PARAMETER;
2641 if (!data || data->cbSize < offsetof(ACTCTX_SECTION_KEYED_DATA, ulAssemblyRosterIndex) ||
2642 !section_name || !section_name->Buffer)
2644 WARN("invalid parameter\n");
2645 return STATUS_INVALID_PARAMETER;
2648 if (NtCurrentTeb()->ActivationContextStack.ActiveFrame)
2650 ACTIVATION_CONTEXT *actctx = check_actctx(NtCurrentTeb()->ActivationContextStack.ActiveFrame->ActivationContext);
2651 if (actctx) status = find_string( actctx, section_kind, section_name, flags, data );
2654 if (status != STATUS_SUCCESS)
2655 status = find_string( process_actctx, section_kind, section_name, flags, data );