2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Runtime Library
4 * PURPOSE: Activation Context Support
5 * FILE: lib/rtl/actctx.c
9 * Jacek Caban for CodeWeavers
11 * Stefan Ginsberg (stefan__100__@hotmail.com)
15 /* Based on Wine 1.1.26 */
22 #include <wine/unicode.h>
24 BOOLEAN RtlpNotAllowingMultipleActivation
;
26 #define ACTCTX_FLAGS_ALL (\
27 ACTCTX_FLAG_PROCESSOR_ARCHITECTURE_VALID |\
28 ACTCTX_FLAG_LANGID_VALID |\
29 ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID |\
30 ACTCTX_FLAG_RESOURCE_NAME_VALID |\
31 ACTCTX_FLAG_SET_PROCESS_DEFAULT |\
32 ACTCTX_FLAG_APPLICATION_NAME_VALID |\
33 ACTCTX_FLAG_SOURCE_IS_ASSEMBLYREF |\
34 ACTCTX_FLAG_HMODULE_VALID )
36 #define ACTCTX_MAGIC_MARKER (PVOID)'gMcA'
38 #define ACTCTX_FAKE_HANDLE ((HANDLE) 0xf00baa)
39 #define ACTCTX_FAKE_COOKIE ((ULONG_PTR) 0xf00bad)
61 struct assembly_version
69 struct assembly_identity
76 struct assembly_version version
;
120 unsigned int allocated
;
127 struct entity_array entities
;
132 APPLICATION_MANIFEST
,
134 ASSEMBLY_SHARED_MANIFEST
,
139 enum assembly_type type
;
140 struct assembly_identity id
;
141 struct file_info manifest
;
144 struct dll_redirect
*dlls
;
145 unsigned int num_dlls
;
146 unsigned int allocated_dlls
;
147 struct entity_array entities
;
150 typedef struct _ASSEMBLY_STORAGE_MAP_ENTRY
153 UNICODE_STRING DosPath
;
155 } ASSEMBLY_STORAGE_MAP_ENTRY
, *PASSEMBLY_STORAGE_MAP_ENTRY
;
157 typedef struct _ASSEMBLY_STORAGE_MAP
161 PASSEMBLY_STORAGE_MAP_ENTRY
*AssemblyArray
;
162 } ASSEMBLY_STORAGE_MAP
, *PASSEMBLY_STORAGE_MAP
;
164 typedef struct _ACTIVATION_CONTEXT
169 PACTIVATION_CONTEXT_DATA ActivationContextData
;
170 PVOID NotificationRoutine
;
171 PVOID NotificationContext
;
172 ULONG SentNotifications
[8];
173 ULONG DisabledNotifications
[8];
174 ASSEMBLY_STORAGE_MAP StorageMap
;
175 PASSEMBLY_STORAGE_MAP_ENTRY InlineStorageMapEntries
;
176 ULONG StackTraceIndex
;
177 PVOID StackTraces
[4][4];
178 struct file_info config
;
179 struct file_info appdir
;
180 struct assembly
*assemblies
;
181 unsigned int num_assemblies
;
182 unsigned int allocated_assemblies
;
183 } ACTIVATION_CONTEXT
, *PIACTIVATION_CONTEXT
;
187 ACTIVATION_CONTEXT
*actctx
;
188 struct assembly_identity
*dependencies
;
189 unsigned int num_dependencies
;
190 unsigned int allocated_dependencies
;
193 typedef struct _ACTIVATION_CONTEXT_WRAPPED
196 ACTIVATION_CONTEXT ActivationContext
;
197 } ACTIVATION_CONTEXT_WRAPPED
, *PACTIVATION_CONTEXT_WRAPPED
;
201 RtlpSxsBreakOnInvalidMarker(IN PACTIVATION_CONTEXT ActCtx
,
202 IN ULONG FailureCode
)
204 EXCEPTION_RECORD ExceptionRecord
;
206 /* Fatal SxS exception header */
207 ExceptionRecord
.ExceptionRecord
= NULL
;
208 ExceptionRecord
.ExceptionCode
= STATUS_SXS_CORRUPTION
;
209 ExceptionRecord
.ExceptionFlags
= EXCEPTION_NONCONTINUABLE
;
211 /* With SxS-specific information plus the context itself */
212 ExceptionRecord
.ExceptionInformation
[0] = 1;
213 ExceptionRecord
.ExceptionInformation
[1] = FailureCode
;
214 ExceptionRecord
.ExceptionInformation
[2] = (ULONG_PTR
)ActCtx
;
215 ExceptionRecord
.NumberParameters
= 3;
218 RtlRaiseException(&ExceptionRecord
);
223 RtlpValidateActCtx(IN PACTIVATION_CONTEXT ActCtx
)
225 PACTIVATION_CONTEXT_WRAPPED pActual
;
227 /* Get the caller-opaque header */
228 pActual
= CONTAINING_RECORD(ActCtx
,
229 ACTIVATION_CONTEXT_WRAPPED
,
232 /* Check if the header matches as expected */
233 if (pActual
->MagicMarker
!= ACTCTX_MAGIC_MARKER
)
235 /* Nope, print out a warning, assert, and then throw an exception */
236 DbgPrint("%s : Invalid activation context marker %p found in activation context %p\n"
237 " This means someone stepped on the allocation, or someone is using a\n"
238 " deallocated activation context\n",
240 pActual
->MagicMarker
,
242 ASSERT(pActual
->MagicMarker
== ACTCTX_MAGIC_MARKER
);
243 RtlpSxsBreakOnInvalidMarker(ActCtx
, 1);
247 static const WCHAR assemblyW
[] = {'a','s','s','e','m','b','l','y',0};
248 static const WCHAR assemblyIdentityW
[] = {'a','s','s','e','m','b','l','y','I','d','e','n','t','i','t','y',0};
249 static const WCHAR bindingRedirectW
[] = {'b','i','n','d','i','n','g','R','e','d','i','r','e','c','t',0};
250 static const WCHAR clrClassW
[] = {'c','l','r','C','l','a','s','s',0};
251 static const WCHAR clrSurrogateW
[] = {'c','l','r','S','u','r','r','o','g','a','t','e',0};
252 static const WCHAR comClassW
[] = {'c','o','m','C','l','a','s','s',0};
253 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};
254 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};
255 static const WCHAR dependencyW
[] = {'d','e','p','e','n','d','e','n','c','y',0};
256 static const WCHAR dependentAssemblyW
[] = {'d','e','p','e','n','d','e','n','t','A','s','s','e','m','b','l','y',0};
257 static const WCHAR descriptionW
[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
258 static const WCHAR fileW
[] = {'f','i','l','e',0};
259 static const WCHAR asmv2hashW
[] = {'a','s','m','v','2',':','h','a','s','h',0};
260 static const WCHAR noInheritW
[] = {'n','o','I','n','h','e','r','i','t',0};
261 static const WCHAR noInheritableW
[] = {'n','o','I','n','h','e','r','i','t','a','b','l','e',0};
262 static const WCHAR typelibW
[] = {'t','y','p','e','l','i','b',0};
263 static const WCHAR windowClassW
[] = {'w','i','n','d','o','w','C','l','a','s','s',0};
265 static const WCHAR clsidW
[] = {'c','l','s','i','d',0};
266 static const WCHAR hashW
[] = {'h','a','s','h',0};
267 static const WCHAR hashalgW
[] = {'h','a','s','h','a','l','g',0};
268 static const WCHAR helpdirW
[] = {'h','e','l','p','d','i','r',0};
269 static const WCHAR iidW
[] = {'i','i','d',0};
270 static const WCHAR languageW
[] = {'l','a','n','g','u','a','g','e',0};
271 static const WCHAR manifestVersionW
[] = {'m','a','n','i','f','e','s','t','V','e','r','s','i','o','n',0};
272 static const WCHAR g_nameW
[] = {'n','a','m','e',0};
273 static const WCHAR newVersionW
[] = {'n','e','w','V','e','r','s','i','o','n',0};
274 static const WCHAR oldVersionW
[] = {'o','l','d','V','e','r','s','i','o','n',0};
275 static const WCHAR optionalW
[] = {'o','p','t','i','o','n','a','l',0};
276 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};
277 static const WCHAR publicKeyTokenW
[] = {'p','u','b','l','i','c','K','e','y','T','o','k','e','n',0};
278 static const WCHAR tlbidW
[] = {'t','l','b','i','d',0};
279 static const WCHAR typeW
[] = {'t','y','p','e',0};
280 static const WCHAR versionW
[] = {'v','e','r','s','i','o','n',0};
281 static const WCHAR xmlnsW
[] = {'x','m','l','n','s',0};
283 static const WCHAR g_xmlW
[] = {'?','x','m','l',0};
284 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};
285 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};
287 static const WCHAR dotManifestW
[] = {'.','m','a','n','i','f','e','s','t',0};
288 static const WCHAR version_formatW
[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
290 static ACTIVATION_CONTEXT_WRAPPED system_actctx
= { ACTCTX_MAGIC_MARKER
, { 1 } };
291 static ACTIVATION_CONTEXT
*process_actctx
= &system_actctx
.ActivationContext
;
293 static WCHAR
*strdupW(const WCHAR
* str
)
297 if (!(ptr
= RtlAllocateHeap(RtlGetProcessHeap(), 0, (strlenW(str
) + 1) * sizeof(WCHAR
))))
299 return strcpyW(ptr
, str
);
302 static WCHAR
*xmlstrdupW(const xmlstr_t
* str
)
306 if ((strW
= RtlAllocateHeap(RtlGetProcessHeap(), 0, (str
->len
+ 1) * sizeof(WCHAR
))))
308 memcpy( strW
, str
->ptr
, str
->len
* sizeof(WCHAR
) );
314 static UNICODE_STRING
xmlstr2unicode(const xmlstr_t
*xmlstr
)
318 res
.Buffer
= (PWSTR
)xmlstr
->ptr
;
319 res
.Length
= res
.MaximumLength
= (USHORT
)xmlstr
->len
* sizeof(WCHAR
);
324 static inline BOOL
xmlstr_cmp(const xmlstr_t
* xmlstr
, const WCHAR
*str
)
326 return !strncmpW(xmlstr
->ptr
, str
, xmlstr
->len
) && !str
[xmlstr
->len
];
329 static inline BOOL
xmlstr_cmpi(const xmlstr_t
* xmlstr
, const WCHAR
*str
)
331 return !strncmpiW(xmlstr
->ptr
, str
, xmlstr
->len
) && !str
[xmlstr
->len
];
334 static inline BOOL
xmlstr_cmp_end(const xmlstr_t
* xmlstr
, const WCHAR
*str
)
336 return (xmlstr
->len
&& xmlstr
->ptr
[0] == '/' &&
337 !strncmpW(xmlstr
->ptr
+ 1, str
, xmlstr
->len
- 1) && !str
[xmlstr
->len
- 1]);
340 static inline BOOL
isxmlspace( WCHAR ch
)
342 return (ch
== ' ' || ch
== '\r' || ch
== '\n' || ch
== '\t');
345 static struct assembly
*add_assembly(ACTIVATION_CONTEXT
*actctx
, enum assembly_type at
)
347 struct assembly
*assembly
;
349 if (actctx
->num_assemblies
== actctx
->allocated_assemblies
)
352 unsigned int new_count
;
353 if (actctx
->assemblies
)
355 new_count
= actctx
->allocated_assemblies
* 2;
356 ptr
= RtlReAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY
,
357 actctx
->assemblies
, new_count
* sizeof(*assembly
) );
362 ptr
= RtlAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, new_count
* sizeof(*assembly
) );
364 if (!ptr
) return NULL
;
365 actctx
->assemblies
= ptr
;
366 actctx
->allocated_assemblies
= new_count
;
369 assembly
= &actctx
->assemblies
[actctx
->num_assemblies
++];
374 static struct dll_redirect
* add_dll_redirect(struct assembly
* assembly
)
376 if (assembly
->num_dlls
== assembly
->allocated_dlls
)
379 unsigned int new_count
;
382 new_count
= assembly
->allocated_dlls
* 2;
383 ptr
= RtlReAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY
,
384 assembly
->dlls
, new_count
* sizeof(*assembly
->dlls
) );
389 ptr
= RtlAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, new_count
* sizeof(*assembly
->dlls
) );
391 if (!ptr
) return NULL
;
392 assembly
->dlls
= ptr
;
393 assembly
->allocated_dlls
= new_count
;
395 return &assembly
->dlls
[assembly
->num_dlls
++];
398 static void free_assembly_identity(struct assembly_identity
*ai
)
400 RtlFreeHeap( RtlGetProcessHeap(), 0, ai
->name
);
401 RtlFreeHeap( RtlGetProcessHeap(), 0, ai
->arch
);
402 RtlFreeHeap( RtlGetProcessHeap(), 0, ai
->public_key
);
403 RtlFreeHeap( RtlGetProcessHeap(), 0, ai
->language
);
404 RtlFreeHeap( RtlGetProcessHeap(), 0, ai
->type
);
407 static struct entity
* add_entity(struct entity_array
*array
, DWORD kind
)
409 struct entity
* entity
;
411 if (array
->num
== array
->allocated
)
414 unsigned int new_count
;
417 new_count
= array
->allocated
* 2;
418 ptr
= RtlReAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY
,
419 array
->base
, new_count
* sizeof(*array
->base
) );
424 ptr
= RtlAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, new_count
* sizeof(*array
->base
) );
426 if (!ptr
) return NULL
;
428 array
->allocated
= new_count
;
430 entity
= &array
->base
[array
->num
++];
435 static void free_entity_array(struct entity_array
*array
)
438 for (i
= 0; i
< array
->num
; i
++)
440 struct entity
*entity
= &array
->base
[i
];
441 switch (entity
->kind
)
443 case ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION
:
444 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.comclass
.clsid
);
446 case ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION
:
447 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.proxy
.iid
);
448 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.proxy
.name
);
450 case ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION
:
451 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.typelib
.tlbid
);
452 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.typelib
.version
);
453 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.typelib
.helpdir
);
455 case ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION
:
456 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.class.name
);
458 case ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION
:
459 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.clrclass
.name
);
460 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.clrclass
.clsid
);
462 case ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES
:
463 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.clrsurrogate
.name
);
464 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.clrsurrogate
.clsid
);
467 DPRINT1("Unknown entity kind %u\n", entity
->kind
);
470 RtlFreeHeap( RtlGetProcessHeap(), 0, array
->base
);
473 static BOOL
is_matching_string( const WCHAR
*str1
, const WCHAR
*str2
)
475 if (!str1
) return !str2
;
476 return str2
&& !strcmpiW( str1
, str2
);
479 static BOOL
is_matching_identity( const struct assembly_identity
*id1
,
480 const struct assembly_identity
*id2
)
482 if (!is_matching_string( id1
->name
, id2
->name
)) return FALSE
;
483 if (!is_matching_string( id1
->arch
, id2
->arch
)) return FALSE
;
484 if (!is_matching_string( id1
->public_key
, id2
->public_key
)) return FALSE
;
486 if (id1
->language
&& id2
->language
&& strcmpiW( id1
->language
, id2
->language
))
488 static const WCHAR wildcardW
[] = {'*',0};
489 if (strcmpW( wildcardW
, id1
->language
) && strcmpW( wildcardW
, id2
->language
))
492 if (id1
->version
.major
!= id2
->version
.major
) return FALSE
;
493 if (id1
->version
.minor
!= id2
->version
.minor
) return FALSE
;
494 if (id1
->version
.build
> id2
->version
.build
) return FALSE
;
495 if (id1
->version
.build
== id2
->version
.build
&&
496 id1
->version
.revision
> id2
->version
.revision
) return FALSE
;
500 static BOOL
add_dependent_assembly_id(struct actctx_loader
* acl
,
501 struct assembly_identity
* ai
)
505 /* check if we already have that assembly */
507 for (i
= 0; i
< acl
->actctx
->num_assemblies
; i
++)
508 if (is_matching_identity( ai
, &acl
->actctx
->assemblies
[i
].id
))
510 DPRINT( "reusing existing assembly for %S arch %S version %u.%u.%u.%u\n",
511 ai
->name
, ai
->arch
, ai
->version
.major
, ai
->version
.minor
,
512 ai
->version
.build
, ai
->version
.revision
);
516 for (i
= 0; i
< acl
->num_dependencies
; i
++)
517 if (is_matching_identity( ai
, &acl
->dependencies
[i
] ))
519 DPRINT( "reusing existing dependency for %S arch %S version %u.%u.%u.%u\n",
520 ai
->name
, ai
->arch
, ai
->version
.major
, ai
->version
.minor
,
521 ai
->version
.build
, ai
->version
.revision
);
525 if (acl
->num_dependencies
== acl
->allocated_dependencies
)
528 unsigned int new_count
;
529 if (acl
->dependencies
)
531 new_count
= acl
->allocated_dependencies
* 2;
532 ptr
= RtlReAllocateHeap(RtlGetProcessHeap(), 0, acl
->dependencies
,
533 new_count
* sizeof(acl
->dependencies
[0]));
538 ptr
= RtlAllocateHeap(RtlGetProcessHeap(), 0, new_count
* sizeof(acl
->dependencies
[0]));
540 if (!ptr
) return FALSE
;
541 acl
->dependencies
= ptr
;
542 acl
->allocated_dependencies
= new_count
;
544 acl
->dependencies
[acl
->num_dependencies
++] = *ai
;
549 static void free_depend_manifests(struct actctx_loader
* acl
)
552 for (i
= 0; i
< acl
->num_dependencies
; i
++)
553 free_assembly_identity(&acl
->dependencies
[i
]);
554 RtlFreeHeap(RtlGetProcessHeap(), 0, acl
->dependencies
);
557 static WCHAR
*build_assembly_dir(struct assembly_identity
* ai
)
559 static const WCHAR undW
[] = {'_',0};
560 static const WCHAR noneW
[] = {'n','o','n','e',0};
561 static const WCHAR mskeyW
[] = {'d','e','a','d','b','e','e','f',0};
563 const WCHAR
*arch
= ai
->arch
? ai
->arch
: noneW
;
564 const WCHAR
*key
= ai
->public_key
? ai
->public_key
: noneW
;
565 const WCHAR
*lang
= ai
->language
? ai
->language
: noneW
;
566 const WCHAR
*name
= ai
->name
? ai
->name
: noneW
;
567 SIZE_T size
= (strlenW(arch
) + 1 + strlenW(name
) + 1 + strlenW(key
) + 24 + 1 +
568 strlenW(lang
) + 1) * sizeof(WCHAR
) + sizeof(mskeyW
);
571 if (!(ret
= RtlAllocateHeap( RtlGetProcessHeap(), 0, size
))) return NULL
;
573 strcpyW( ret
, arch
);
574 strcatW( ret
, undW
);
575 strcatW( ret
, name
);
576 strcatW( ret
, undW
);
578 strcatW( ret
, undW
);
579 sprintfW( ret
+ strlenW(ret
), version_formatW
,
580 ai
->version
.major
, ai
->version
.minor
, ai
->version
.build
, ai
->version
.revision
);
581 strcatW( ret
, undW
);
582 strcatW( ret
, lang
);
583 strcatW( ret
, undW
);
584 strcatW( ret
, mskeyW
);
588 static inline void append_string( WCHAR
*buffer
, const WCHAR
*prefix
, const WCHAR
*str
)
593 strcatW( buffer
, prefix
);
602 static WCHAR
*build_assembly_id( const struct assembly_identity
*ai
)
604 static const WCHAR archW
[] =
605 {',','p','r','o','c','e','s','s','o','r','A','r','c','h','i','t','e','c','t','u','r','e','=',0};
606 static const WCHAR public_keyW
[] =
607 {',','p','u','b','l','i','c','K','e','y','T','o','k','e','n','=',0};
609 WCHAR version
[64], *ret
;
612 sprintfW( version
, version_formatW
,
613 ai
->version
.major
, ai
->version
.minor
, ai
->version
.build
, ai
->version
.revision
);
614 if (ai
->name
) size
+= strlenW(ai
->name
);
615 if (ai
->arch
) size
+= strlenW(archW
) + strlenW(ai
->arch
) + 2;
616 if (ai
->public_key
) size
+= strlenW(public_keyW
) + strlenW(ai
->public_key
) + 2;
617 if (ai
->type
) size
+= 1 + strlenW(typeW
) + 1 + strlenW(ai
->type
) + 2;
618 size
+= 1+ strlenW(versionW
) + 1 + strlenW(version
) + 2;
620 if (!(ret
= RtlAllocateHeap( RtlGetProcessHeap(), 0, (size
+ 1) * sizeof(WCHAR
) )))
623 if (ai
->name
) strcpyW( ret
, ai
->name
);
625 append_string( ret
, archW
, ai
->arch
);
626 append_string( ret
, public_keyW
, ai
->public_key
);
627 append_string( ret
, typeW
, ai
->type
);
628 append_string( ret
, versionW
, version
);
631 static ACTIVATION_CONTEXT
*check_actctx( HANDLE h
)
633 ACTIVATION_CONTEXT
*ret
= NULL
, *actctx
= h
;
634 PACTIVATION_CONTEXT_WRAPPED pActual
;
636 if (!h
|| h
== INVALID_HANDLE_VALUE
) return NULL
;
641 pActual
= CONTAINING_RECORD(actctx
, ACTIVATION_CONTEXT_WRAPPED
, ActivationContext
);
642 if (pActual
->MagicMarker
== ACTCTX_MAGIC_MARKER
) ret
= &pActual
->ActivationContext
;
645 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
647 DPRINT1("Invalid activation context handle!\n");
653 static inline void actctx_addref( ACTIVATION_CONTEXT
*actctx
)
655 InterlockedExchangeAdd( &actctx
->RefCount
, 1 );
658 static void actctx_release( ACTIVATION_CONTEXT
*actctx
)
660 PACTIVATION_CONTEXT_WRAPPED pActual
;
662 if (InterlockedExchangeAdd(&actctx
->RefCount
, -1) == 1)
666 for (i
= 0; i
< actctx
->num_assemblies
; i
++)
668 struct assembly
*assembly
= &actctx
->assemblies
[i
];
669 for (j
= 0; j
< assembly
->num_dlls
; j
++)
671 struct dll_redirect
*dll
= &assembly
->dlls
[j
];
672 free_entity_array( &dll
->entities
);
673 RtlFreeHeap( RtlGetProcessHeap(), 0, dll
->name
);
674 RtlFreeHeap( RtlGetProcessHeap(), 0, dll
->hash
);
676 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly
->dlls
);
677 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly
->manifest
.info
);
678 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly
->directory
);
679 free_entity_array( &assembly
->entities
);
680 free_assembly_identity(&assembly
->id
);
682 RtlFreeHeap( RtlGetProcessHeap(), 0, actctx
->config
.info
);
683 RtlFreeHeap( RtlGetProcessHeap(), 0, actctx
->appdir
.info
);
684 RtlFreeHeap( RtlGetProcessHeap(), 0, actctx
->assemblies
);
685 pActual
= CONTAINING_RECORD(actctx
, ACTIVATION_CONTEXT_WRAPPED
, ActivationContext
);
686 pActual
->MagicMarker
= 0;
687 RtlFreeHeap(RtlGetProcessHeap(), 0, pActual
);
691 static BOOL
next_xml_attr(xmlbuf_t
* xmlbuf
, xmlstr_t
* name
, xmlstr_t
* value
,
692 BOOL
* error
, BOOL
* end
)
698 while (xmlbuf
->ptr
< xmlbuf
->end
&& isxmlspace(*xmlbuf
->ptr
))
701 if (xmlbuf
->ptr
== xmlbuf
->end
) return FALSE
;
703 if (*xmlbuf
->ptr
== '/')
706 if (xmlbuf
->ptr
== xmlbuf
->end
|| *xmlbuf
->ptr
!= '>')
715 if (*xmlbuf
->ptr
== '>')
723 while (ptr
< xmlbuf
->end
&& *ptr
!= '=' && *ptr
!= '>' && !isxmlspace(*ptr
)) ptr
++;
725 if (ptr
== xmlbuf
->end
|| *ptr
!= '=') return FALSE
;
727 name
->ptr
= xmlbuf
->ptr
;
728 name
->len
= (ULONG
)(ptr
- xmlbuf
->ptr
);
732 if (ptr
== xmlbuf
->end
|| (*ptr
!= '"' && *ptr
!= '\'')) return FALSE
;
735 if (ptr
== xmlbuf
->end
) return FALSE
;
737 ptr
= memchrW(ptr
, ptr
[-1], xmlbuf
->end
- ptr
);
740 xmlbuf
->ptr
= xmlbuf
->end
;
744 value
->len
= (ULONG
)(ptr
- value
->ptr
);
745 xmlbuf
->ptr
= ptr
+ 1;
747 if (xmlbuf
->ptr
== xmlbuf
->end
) return FALSE
;
753 static BOOL
next_xml_elem(xmlbuf_t
* xmlbuf
, xmlstr_t
* elem
)
759 ptr
= memchrW(xmlbuf
->ptr
, '<', xmlbuf
->end
- xmlbuf
->ptr
);
762 xmlbuf
->ptr
= xmlbuf
->end
;
766 if (ptr
+ 3 < xmlbuf
->end
&& ptr
[0] == '!' && ptr
[1] == '-' && ptr
[2] == '-') /* skip comment */
768 for (ptr
+= 3; ptr
+ 3 <= xmlbuf
->end
; ptr
++)
769 if (ptr
[0] == '-' && ptr
[1] == '-' && ptr
[2] == '>') break;
771 if (ptr
+ 3 > xmlbuf
->end
)
773 xmlbuf
->ptr
= xmlbuf
->end
;
776 xmlbuf
->ptr
= ptr
+ 3;
782 while (ptr
< xmlbuf
->end
&& !isxmlspace(*ptr
) && *ptr
!= '>' && (*ptr
!= '/' || ptr
== xmlbuf
->ptr
))
785 elem
->ptr
= xmlbuf
->ptr
;
786 elem
->len
= (ULONG
)(ptr
- xmlbuf
->ptr
);
788 return xmlbuf
->ptr
!= xmlbuf
->end
;
791 static BOOL
parse_xml_header(xmlbuf_t
* xmlbuf
)
793 /* FIXME: parse attributes */
796 for (ptr
= xmlbuf
->ptr
; ptr
< xmlbuf
->end
- 1; ptr
++)
798 if (ptr
[0] == '?' && ptr
[1] == '>')
800 xmlbuf
->ptr
= ptr
+ 2;
807 static BOOL
parse_text_content(xmlbuf_t
* xmlbuf
, xmlstr_t
* content
)
809 const WCHAR
*ptr
= memchrW(xmlbuf
->ptr
, '<', xmlbuf
->end
- xmlbuf
->ptr
);
811 if (!ptr
) return FALSE
;
813 content
->ptr
= xmlbuf
->ptr
;
814 content
->len
= (ULONG
)(ptr
- xmlbuf
->ptr
);
820 static BOOL
parse_version(const xmlstr_t
*str
, struct assembly_version
*version
)
827 /* major.minor.build.revision */
828 ver
[0] = ver
[1] = ver
[2] = ver
[3] = pos
= 0;
829 for (curr
= str
->ptr
; curr
< str
->ptr
+ str
->len
; curr
++)
831 if (*curr
>= '0' && *curr
<= '9')
833 ver
[pos
] = ver
[pos
] * 10 + *curr
- '0';
834 if (ver
[pos
] >= 0x10000) goto error
;
836 else if (*curr
== '.')
838 if (++pos
>= 4) goto error
;
842 version
->major
= ver
[0];
843 version
->minor
= ver
[1];
844 version
->build
= ver
[2];
845 version
->revision
= ver
[3];
849 strU
= xmlstr2unicode(str
);
850 DPRINT1( "Wrong version definition in manifest file (%wZ)\n", &strU
);
854 static BOOL
parse_expect_elem(xmlbuf_t
* xmlbuf
, const WCHAR
* name
)
857 UNICODE_STRING elemU
;
858 if (!next_xml_elem(xmlbuf
, &elem
)) return FALSE
;
859 if (xmlstr_cmp(&elem
, name
)) return TRUE
;
860 elemU
= xmlstr2unicode(&elem
);
861 DPRINT1( "unexpected element %wZ\n", &elemU
);
865 static BOOL
parse_expect_no_attr(xmlbuf_t
* xmlbuf
, BOOL
* end
)
867 xmlstr_t attr_name
, attr_value
;
868 UNICODE_STRING attr_nameU
, attr_valueU
;
871 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, end
))
873 attr_nameU
= xmlstr2unicode(&attr_name
);
874 attr_valueU
= xmlstr2unicode(&attr_value
);
875 DPRINT1( "unexpected attr %wZ=%wZ\n", &attr_nameU
,
881 static BOOL
parse_end_element(xmlbuf_t
*xmlbuf
)
884 return parse_expect_no_attr(xmlbuf
, &end
) && !end
;
887 static BOOL
parse_expect_end_elem(xmlbuf_t
*xmlbuf
, const WCHAR
*name
)
890 UNICODE_STRING elemU
;
891 if (!next_xml_elem(xmlbuf
, &elem
)) return FALSE
;
892 if (!xmlstr_cmp_end(&elem
, name
))
894 elemU
= xmlstr2unicode(&elem
);
895 DPRINT1( "unexpected element %wZ\n", &elemU
);
898 return parse_end_element(xmlbuf
);
901 static BOOL
parse_unknown_elem(xmlbuf_t
*xmlbuf
, const xmlstr_t
*unknown_elem
)
903 xmlstr_t attr_name
, attr_value
, elem
;
904 BOOL end
= FALSE
, error
, ret
= TRUE
;
906 while(next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
));
907 if(error
|| end
) return end
;
909 while(ret
&& (ret
= next_xml_elem(xmlbuf
, &elem
)))
911 if(*elem
.ptr
== '/' && elem
.len
- 1 == unknown_elem
->len
&&
912 !strncmpW(elem
.ptr
+1, unknown_elem
->ptr
, unknown_elem
->len
))
915 ret
= parse_unknown_elem(xmlbuf
, &elem
);
918 return ret
&& parse_end_element(xmlbuf
);
921 static BOOL
parse_assembly_identity_elem(xmlbuf_t
* xmlbuf
, ACTIVATION_CONTEXT
* actctx
,
922 struct assembly_identity
* ai
)
924 xmlstr_t attr_name
, attr_value
;
925 BOOL end
= FALSE
, error
;
926 UNICODE_STRING attr_valueU
, attr_nameU
;
928 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
930 if (xmlstr_cmp(&attr_name
, g_nameW
))
932 if (!(ai
->name
= xmlstrdupW(&attr_value
))) return FALSE
;
934 else if (xmlstr_cmp(&attr_name
, typeW
))
936 if (!(ai
->type
= xmlstrdupW(&attr_value
))) return FALSE
;
938 else if (xmlstr_cmp(&attr_name
, versionW
))
940 if (!parse_version(&attr_value
, &ai
->version
)) return FALSE
;
942 else if (xmlstr_cmp(&attr_name
, processorArchitectureW
))
944 if (!(ai
->arch
= xmlstrdupW(&attr_value
))) return FALSE
;
946 else if (xmlstr_cmp(&attr_name
, publicKeyTokenW
))
948 if (!(ai
->public_key
= xmlstrdupW(&attr_value
))) return FALSE
;
950 else if (xmlstr_cmp(&attr_name
, languageW
))
952 if (!(ai
->language
= xmlstrdupW(&attr_value
))) return FALSE
;
953 DPRINT1("Unsupported yet language attribute (%S)\n",
958 attr_nameU
= xmlstr2unicode(&attr_name
);
959 attr_valueU
= xmlstr2unicode(&attr_value
);
960 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
964 if (error
|| end
) return end
;
965 return parse_expect_end_elem(xmlbuf
, assemblyIdentityW
);
968 static BOOL
parse_com_class_elem(xmlbuf_t
* xmlbuf
, struct dll_redirect
* dll
)
970 xmlstr_t elem
, attr_name
, attr_value
;
971 BOOL ret
, end
= FALSE
, error
;
972 struct entity
* entity
;
973 UNICODE_STRING attr_valueU
, attr_nameU
;
975 if (!(entity
= add_entity(&dll
->entities
, ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION
)))
978 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
980 if (xmlstr_cmp(&attr_name
, clsidW
))
982 if (!(entity
->u
.comclass
.clsid
= xmlstrdupW(&attr_value
))) return FALSE
;
986 attr_nameU
= xmlstr2unicode(&attr_name
);
987 attr_valueU
= xmlstr2unicode(&attr_value
);
988 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
992 if (error
|| end
) return end
;
994 while ((ret
= next_xml_elem(xmlbuf
, &elem
)))
996 if (xmlstr_cmp_end(&elem
, comClassW
))
998 ret
= parse_end_element(xmlbuf
);
1003 attr_nameU
= xmlstr2unicode(&elem
);
1004 DPRINT1("unknown elem %wZ\n", &attr_nameU
);
1005 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1011 static BOOL
parse_cominterface_proxy_stub_elem(xmlbuf_t
* xmlbuf
, struct dll_redirect
* dll
)
1013 xmlstr_t attr_name
, attr_value
;
1014 BOOL end
= FALSE
, error
;
1015 struct entity
* entity
;
1016 UNICODE_STRING attr_valueU
, attr_nameU
;
1018 if (!(entity
= add_entity(&dll
->entities
, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION
)))
1021 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1023 if (xmlstr_cmp(&attr_name
, iidW
))
1025 if (!(entity
->u
.proxy
.iid
= xmlstrdupW(&attr_value
))) return FALSE
;
1027 if (xmlstr_cmp(&attr_name
, g_nameW
))
1029 if (!(entity
->u
.proxy
.name
= xmlstrdupW(&attr_value
))) return FALSE
;
1033 attr_nameU
= xmlstr2unicode(&attr_name
);
1034 attr_valueU
= xmlstr2unicode(&attr_value
);
1035 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1039 if (error
|| end
) return end
;
1040 return parse_expect_end_elem(xmlbuf
, comInterfaceProxyStubW
);
1043 static BOOL
parse_typelib_elem(xmlbuf_t
* xmlbuf
, struct dll_redirect
* dll
)
1045 xmlstr_t attr_name
, attr_value
;
1046 BOOL end
= FALSE
, error
;
1047 struct entity
* entity
;
1048 UNICODE_STRING attr_valueU
, attr_nameU
;
1050 if (!(entity
= add_entity(&dll
->entities
, ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION
)))
1053 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1055 if (xmlstr_cmp(&attr_name
, tlbidW
))
1057 if (!(entity
->u
.typelib
.tlbid
= xmlstrdupW(&attr_value
))) return FALSE
;
1059 if (xmlstr_cmp(&attr_name
, versionW
))
1061 if (!(entity
->u
.typelib
.version
= xmlstrdupW(&attr_value
))) return FALSE
;
1063 if (xmlstr_cmp(&attr_name
, helpdirW
))
1065 if (!(entity
->u
.typelib
.helpdir
= xmlstrdupW(&attr_value
))) return FALSE
;
1069 attr_nameU
= xmlstr2unicode(&attr_name
);
1070 attr_valueU
= xmlstr2unicode(&attr_value
);
1071 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1075 if (error
|| end
) return end
;
1076 return parse_expect_end_elem(xmlbuf
, typelibW
);
1079 static BOOL
parse_window_class_elem(xmlbuf_t
* xmlbuf
, struct dll_redirect
* dll
)
1081 xmlstr_t elem
, content
;
1082 BOOL end
= FALSE
, ret
= TRUE
;
1083 struct entity
* entity
;
1084 UNICODE_STRING elemU
;
1086 if (!(entity
= add_entity(&dll
->entities
, ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION
)))
1089 if (!parse_expect_no_attr(xmlbuf
, &end
)) return FALSE
;
1090 if (end
) return FALSE
;
1092 if (!parse_text_content(xmlbuf
, &content
)) return FALSE
;
1094 if (!(entity
->u
.class.name
= xmlstrdupW(&content
))) return FALSE
;
1096 while (ret
&& (ret
= next_xml_elem(xmlbuf
, &elem
)))
1098 if (xmlstr_cmp_end(&elem
, windowClassW
))
1100 ret
= parse_end_element(xmlbuf
);
1105 elemU
= xmlstr2unicode(&elem
);
1106 DPRINT1("unknown elem %wZ\n", &elemU
);
1107 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1114 static BOOL
parse_binding_redirect_elem(xmlbuf_t
* xmlbuf
)
1116 xmlstr_t attr_name
, attr_value
;
1117 UNICODE_STRING attr_valueU
, attr_nameU
;
1118 BOOL end
= FALSE
, error
;
1120 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1122 attr_nameU
= xmlstr2unicode(&attr_name
);
1123 attr_valueU
= xmlstr2unicode(&attr_value
);
1125 if (xmlstr_cmp(&attr_name
, oldVersionW
))
1127 DPRINT1("Not stored yet oldVersion=%wZ\n", &attr_valueU
);
1129 else if (xmlstr_cmp(&attr_name
, newVersionW
))
1131 DPRINT1("Not stored yet newVersion=%wZ\n", &attr_valueU
);
1135 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1139 if (error
|| end
) return end
;
1140 return parse_expect_end_elem(xmlbuf
, bindingRedirectW
);
1143 static BOOL
parse_description_elem(xmlbuf_t
* xmlbuf
)
1145 xmlstr_t elem
, content
;
1146 UNICODE_STRING elemU
;
1147 BOOL end
= FALSE
, ret
= TRUE
;
1149 if (!parse_expect_no_attr(xmlbuf
, &end
) || end
||
1150 !parse_text_content(xmlbuf
, &content
))
1153 elemU
= xmlstr2unicode(&content
);
1154 DPRINT("Got description %wZ\n", &elemU
);
1156 while (ret
&& (ret
= next_xml_elem(xmlbuf
, &elem
)))
1158 if (xmlstr_cmp_end(&elem
, descriptionW
))
1160 ret
= parse_end_element(xmlbuf
);
1165 elemU
= xmlstr2unicode(&elem
);
1166 DPRINT1("unknown elem %wZ\n", &elemU
);
1167 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1174 static BOOL
parse_com_interface_external_proxy_stub_elem(xmlbuf_t
* xmlbuf
,
1175 struct assembly
* assembly
)
1177 xmlstr_t attr_name
, attr_value
;
1178 UNICODE_STRING attr_nameU
, attr_valueU
;
1179 BOOL end
= FALSE
, error
;
1180 struct entity
* entity
;
1182 entity
= add_entity(&assembly
->entities
, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION
);
1183 if (!entity
) return FALSE
;
1185 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1187 if (xmlstr_cmp(&attr_name
, iidW
))
1189 if (!(entity
->u
.proxy
.iid
= xmlstrdupW(&attr_value
))) return FALSE
;
1191 if (xmlstr_cmp(&attr_name
, g_nameW
))
1193 if (!(entity
->u
.proxy
.name
= xmlstrdupW(&attr_value
))) return FALSE
;
1197 attr_nameU
= xmlstr2unicode(&attr_name
);
1198 attr_valueU
= xmlstr2unicode(&attr_value
);
1199 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1203 if (error
|| end
) return end
;
1204 return parse_expect_end_elem(xmlbuf
, comInterfaceExternalProxyStubW
);
1207 static BOOL
parse_clr_class_elem(xmlbuf_t
* xmlbuf
, struct assembly
* assembly
)
1209 xmlstr_t attr_name
, attr_value
;
1210 UNICODE_STRING attr_nameU
, attr_valueU
;
1211 BOOL end
= FALSE
, error
;
1212 struct entity
* entity
;
1214 entity
= add_entity(&assembly
->entities
, ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION
);
1215 if (!entity
) return FALSE
;
1217 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1219 if (xmlstr_cmp(&attr_name
, g_nameW
))
1221 if (!(entity
->u
.clrclass
.name
= xmlstrdupW(&attr_value
))) return FALSE
;
1223 else if (xmlstr_cmp(&attr_name
, clsidW
))
1225 if (!(entity
->u
.clrclass
.clsid
= xmlstrdupW(&attr_value
))) return FALSE
;
1229 attr_nameU
= xmlstr2unicode(&attr_name
);
1230 attr_valueU
= xmlstr2unicode(&attr_value
);
1231 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1235 if (error
|| end
) return end
;
1236 return parse_expect_end_elem(xmlbuf
, clrClassW
);
1239 static BOOL
parse_clr_surrogate_elem(xmlbuf_t
* xmlbuf
, struct assembly
* assembly
)
1241 xmlstr_t attr_name
, attr_value
;
1242 UNICODE_STRING attr_nameU
, attr_valueU
;
1243 BOOL end
= FALSE
, error
;
1244 struct entity
* entity
;
1246 entity
= add_entity(&assembly
->entities
, ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES
);
1247 if (!entity
) return FALSE
;
1249 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1251 if (xmlstr_cmp(&attr_name
, g_nameW
))
1253 if (!(entity
->u
.clrsurrogate
.name
= xmlstrdupW(&attr_value
))) return FALSE
;
1255 else if (xmlstr_cmp(&attr_name
, clsidW
))
1257 if (!(entity
->u
.clrsurrogate
.clsid
= xmlstrdupW(&attr_value
))) return FALSE
;
1261 attr_nameU
= xmlstr2unicode(&attr_name
);
1262 attr_valueU
= xmlstr2unicode(&attr_value
);
1263 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1267 if (error
|| end
) return end
;
1268 return parse_expect_end_elem(xmlbuf
, clrSurrogateW
);
1271 static BOOL
parse_dependent_assembly_elem(xmlbuf_t
* xmlbuf
, struct actctx_loader
* acl
, BOOL optional
)
1273 struct assembly_identity ai
;
1275 BOOL end
= FALSE
, ret
= TRUE
;
1277 if (!parse_expect_no_attr(xmlbuf
, &end
) || end
) return end
;
1279 memset(&ai
, 0, sizeof(ai
));
1280 ai
.optional
= optional
;
1282 if (!parse_expect_elem(xmlbuf
, assemblyIdentityW
) ||
1283 !parse_assembly_identity_elem(xmlbuf
, acl
->actctx
, &ai
))
1286 /* store the newly found identity for later loading */
1287 if (!add_dependent_assembly_id(acl
, &ai
)) return FALSE
;
1289 while (ret
&& (ret
= next_xml_elem(xmlbuf
, &elem
)))
1291 if (xmlstr_cmp_end(&elem
, dependentAssemblyW
))
1293 ret
= parse_end_element(xmlbuf
);
1296 else if (xmlstr_cmp(&elem
, bindingRedirectW
))
1298 ret
= parse_binding_redirect_elem(xmlbuf
);
1302 DPRINT1("unknown elem %S\n", elem
.ptr
);
1303 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1310 static BOOL
parse_dependency_elem(xmlbuf_t
* xmlbuf
, struct actctx_loader
* acl
)
1312 xmlstr_t attr_name
, attr_value
, elem
;
1313 UNICODE_STRING attr_nameU
, attr_valueU
;
1314 BOOL end
= FALSE
, ret
= TRUE
, error
, optional
= FALSE
;
1316 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1318 attr_nameU
= xmlstr2unicode(&attr_name
);
1319 attr_valueU
= xmlstr2unicode(&attr_value
);
1321 if (xmlstr_cmp(&attr_name
, optionalW
))
1323 static const WCHAR yesW
[] = {'y','e','s',0};
1324 optional
= xmlstr_cmpi( &attr_value
, yesW
);
1325 DPRINT1("optional=%wZ\n", &attr_valueU
);
1329 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1333 while (ret
&& (ret
= next_xml_elem(xmlbuf
, &elem
)))
1335 if (xmlstr_cmp_end(&elem
, dependencyW
))
1337 ret
= parse_end_element(xmlbuf
);
1340 else if (xmlstr_cmp(&elem
, dependentAssemblyW
))
1342 ret
= parse_dependent_assembly_elem(xmlbuf
, acl
, optional
);
1346 attr_nameU
= xmlstr2unicode(&elem
);
1347 DPRINT1("unknown element %wZ\n", &attr_nameU
);
1348 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1355 static BOOL
parse_noinherit_elem(xmlbuf_t
* xmlbuf
)
1359 if (!parse_expect_no_attr(xmlbuf
, &end
)) return FALSE
;
1360 return end
|| parse_expect_end_elem(xmlbuf
, noInheritW
);
1363 static BOOL
parse_noinheritable_elem(xmlbuf_t
* xmlbuf
)
1367 if (!parse_expect_no_attr(xmlbuf
, &end
)) return FALSE
;
1368 return end
|| parse_expect_end_elem(xmlbuf
, noInheritableW
);
1371 static BOOL
parse_file_elem(xmlbuf_t
* xmlbuf
, struct assembly
* assembly
)
1373 xmlstr_t attr_name
, attr_value
, elem
;
1374 UNICODE_STRING attr_nameU
, attr_valueU
;
1375 BOOL end
= FALSE
, error
, ret
= TRUE
;
1376 struct dll_redirect
* dll
;
1378 if (!(dll
= add_dll_redirect(assembly
))) return FALSE
;
1380 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1382 attr_nameU
= xmlstr2unicode(&attr_name
);
1383 attr_valueU
= xmlstr2unicode(&attr_value
);
1385 if (xmlstr_cmp(&attr_name
, g_nameW
))
1387 if (!(dll
->name
= xmlstrdupW(&attr_value
))) return FALSE
;
1388 DPRINT("name=%wZ\n", &attr_valueU
);
1390 else if (xmlstr_cmp(&attr_name
, hashW
))
1392 if (!(dll
->hash
= xmlstrdupW(&attr_value
))) return FALSE
;
1394 else if (xmlstr_cmp(&attr_name
, hashalgW
))
1396 static const WCHAR sha1W
[] = {'S','H','A','1',0};
1397 if (!xmlstr_cmpi(&attr_value
, sha1W
))
1398 DPRINT1("hashalg should be SHA1, got %wZ\n", &attr_valueU
);
1402 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1406 if (error
|| !dll
->name
) return FALSE
;
1407 if (end
) return TRUE
;
1409 while (ret
&& (ret
= next_xml_elem(xmlbuf
, &elem
)))
1411 if (xmlstr_cmp_end(&elem
, fileW
))
1413 ret
= parse_end_element(xmlbuf
);
1416 else if (xmlstr_cmp(&elem
, comClassW
))
1418 ret
= parse_com_class_elem(xmlbuf
, dll
);
1420 else if (xmlstr_cmp(&elem
, comInterfaceProxyStubW
))
1422 ret
= parse_cominterface_proxy_stub_elem(xmlbuf
, dll
);
1424 else if (xmlstr_cmp(&elem
, asmv2hashW
))
1426 DPRINT1("asmv2hash (undocumented) not supported\n");
1427 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1429 else if (xmlstr_cmp(&elem
, typelibW
))
1431 ret
= parse_typelib_elem(xmlbuf
, dll
);
1433 else if (xmlstr_cmp(&elem
, windowClassW
))
1435 ret
= parse_window_class_elem(xmlbuf
, dll
);
1439 attr_nameU
= xmlstr2unicode(&elem
);
1440 DPRINT1("unknown elem %wZ\n", &attr_nameU
);
1441 ret
= parse_unknown_elem( xmlbuf
, &elem
);
1448 static BOOL
parse_assembly_elem(xmlbuf_t
* xmlbuf
, struct actctx_loader
* acl
,
1449 struct assembly
* assembly
,
1450 struct assembly_identity
* expected_ai
)
1452 xmlstr_t attr_name
, attr_value
, elem
;
1453 UNICODE_STRING attr_nameU
, attr_valueU
;
1454 BOOL end
= FALSE
, error
, version
= FALSE
, xmlns
= FALSE
, ret
= TRUE
;
1456 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1458 attr_nameU
= xmlstr2unicode(&attr_name
);
1459 attr_valueU
= xmlstr2unicode(&attr_value
);
1461 if (xmlstr_cmp(&attr_name
, manifestVersionW
))
1463 static const WCHAR v10W
[] = {'1','.','0',0};
1464 if (!xmlstr_cmp(&attr_value
, v10W
))
1466 DPRINT1("wrong version %wZ\n", &attr_valueU
);
1471 else if (xmlstr_cmp(&attr_name
, xmlnsW
))
1473 if (!xmlstr_cmp(&attr_value
, manifestv1W
) && !xmlstr_cmp(&attr_value
, manifestv3W
))
1475 DPRINT1("wrong namespace %wZ\n", &attr_valueU
);
1482 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1486 if (error
|| end
|| !xmlns
|| !version
) return FALSE
;
1487 if (!next_xml_elem(xmlbuf
, &elem
)) return FALSE
;
1489 if (assembly
->type
== APPLICATION_MANIFEST
&& xmlstr_cmp(&elem
, noInheritW
))
1491 if (!parse_noinherit_elem(xmlbuf
) || !next_xml_elem(xmlbuf
, &elem
))
1493 assembly
->no_inherit
= TRUE
;
1496 if (xmlstr_cmp(&elem
, noInheritableW
))
1498 if (!parse_noinheritable_elem(xmlbuf
) || !next_xml_elem(xmlbuf
, &elem
))
1501 else if ((assembly
->type
== ASSEMBLY_MANIFEST
|| assembly
->type
== ASSEMBLY_SHARED_MANIFEST
) &&
1502 assembly
->no_inherit
)
1507 if (xmlstr_cmp_end(&elem
, assemblyW
))
1509 ret
= parse_end_element(xmlbuf
);
1512 else if (xmlstr_cmp(&elem
, descriptionW
))
1514 ret
= parse_description_elem(xmlbuf
);
1516 else if (xmlstr_cmp(&elem
, comInterfaceExternalProxyStubW
))
1518 ret
= parse_com_interface_external_proxy_stub_elem(xmlbuf
, assembly
);
1520 else if (xmlstr_cmp(&elem
, dependencyW
))
1522 ret
= parse_dependency_elem(xmlbuf
, acl
);
1524 else if (xmlstr_cmp(&elem
, fileW
))
1526 ret
= parse_file_elem(xmlbuf
, assembly
);
1528 else if (xmlstr_cmp(&elem
, clrClassW
))
1530 ret
= parse_clr_class_elem(xmlbuf
, assembly
);
1532 else if (xmlstr_cmp(&elem
, clrSurrogateW
))
1534 ret
= parse_clr_surrogate_elem(xmlbuf
, assembly
);
1536 else if (xmlstr_cmp(&elem
, assemblyIdentityW
))
1538 if (!parse_assembly_identity_elem(xmlbuf
, acl
->actctx
, &assembly
->id
)) return FALSE
;
1542 /* FIXME: more tests */
1543 if (assembly
->type
== ASSEMBLY_MANIFEST
&&
1544 memcmp(&assembly
->id
.version
, &expected_ai
->version
, sizeof(assembly
->id
.version
)))
1546 DPRINT1("wrong version for assembly manifest: %u.%u.%u.%u / %u.%u.%u.%u\n",
1547 expected_ai
->version
.major
, expected_ai
->version
.minor
,
1548 expected_ai
->version
.build
, expected_ai
->version
.revision
,
1549 assembly
->id
.version
.major
, assembly
->id
.version
.minor
,
1550 assembly
->id
.version
.build
, assembly
->id
.version
.revision
);
1553 else if (assembly
->type
== ASSEMBLY_SHARED_MANIFEST
&&
1554 (assembly
->id
.version
.major
!= expected_ai
->version
.major
||
1555 assembly
->id
.version
.minor
!= expected_ai
->version
.minor
||
1556 assembly
->id
.version
.build
< expected_ai
->version
.build
||
1557 (assembly
->id
.version
.build
== expected_ai
->version
.build
&&
1558 assembly
->id
.version
.revision
< expected_ai
->version
.revision
)))
1560 DPRINT1("wrong version for shared assembly manifest\n");
1567 attr_nameU
= xmlstr2unicode(&elem
);
1568 DPRINT1("unknown element %wZ\n", &attr_nameU
);
1569 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1571 if (ret
) ret
= next_xml_elem(xmlbuf
, &elem
);
1577 static NTSTATUS
parse_manifest_buffer( struct actctx_loader
* acl
, struct assembly
*assembly
,
1578 struct assembly_identity
* ai
, xmlbuf_t
*xmlbuf
)
1581 UNICODE_STRING elemU
;
1583 if (!next_xml_elem(xmlbuf
, &elem
)) return STATUS_SXS_CANT_GEN_ACTCTX
;
1585 if (xmlstr_cmp(&elem
, g_xmlW
) &&
1586 (!parse_xml_header(xmlbuf
) || !next_xml_elem(xmlbuf
, &elem
)))
1587 return STATUS_SXS_CANT_GEN_ACTCTX
;
1589 if (!xmlstr_cmp(&elem
, assemblyW
))
1591 elemU
= xmlstr2unicode(&elem
);
1592 DPRINT1("root element is %wZ, not <assembly>\n", &elemU
);
1593 return STATUS_SXS_CANT_GEN_ACTCTX
;
1596 if (!parse_assembly_elem(xmlbuf
, acl
, assembly
, ai
))
1598 DPRINT1("failed to parse manifest %S\n", assembly
->manifest
.info
);
1599 return STATUS_SXS_CANT_GEN_ACTCTX
;
1602 if (next_xml_elem(xmlbuf
, &elem
))
1604 elemU
= xmlstr2unicode(&elem
);
1605 DPRINT1("unexpected element %wZ\n", &elemU
);
1606 return STATUS_SXS_CANT_GEN_ACTCTX
;
1609 if (xmlbuf
->ptr
!= xmlbuf
->end
)
1611 DPRINT1("parse error\n");
1612 return STATUS_SXS_CANT_GEN_ACTCTX
;
1614 return STATUS_SUCCESS
;
1617 static NTSTATUS
parse_manifest( struct actctx_loader
* acl
, struct assembly_identity
* ai
,
1618 LPCWSTR filename
, LPCWSTR directory
, BOOL shared
,
1619 const void *buffer
, SIZE_T size
)
1623 struct assembly
*assembly
;
1626 DPRINT( "parsing manifest loaded from %S base dir %S\n", filename
, directory
);
1628 if (!(assembly
= add_assembly(acl
->actctx
, shared
? ASSEMBLY_SHARED_MANIFEST
: ASSEMBLY_MANIFEST
)))
1629 return STATUS_SXS_CANT_GEN_ACTCTX
;
1631 if (directory
&& !(assembly
->directory
= strdupW(directory
)))
1632 return STATUS_NO_MEMORY
;
1634 if (filename
) assembly
->manifest
.info
= strdupW( filename
+ 4 /* skip \??\ prefix */ );
1635 assembly
->manifest
.type
= assembly
->manifest
.info
? ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE
1636 : ACTIVATION_CONTEXT_PATH_TYPE_NONE
;
1638 unicode_tests
= IS_TEXT_UNICODE_SIGNATURE
| IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1639 if (RtlIsTextUnicode((PVOID
)buffer
, (ULONG
)size
, &unicode_tests
))
1641 xmlbuf
.ptr
= buffer
;
1642 xmlbuf
.end
= xmlbuf
.ptr
+ size
/ sizeof(WCHAR
);
1643 status
= parse_manifest_buffer( acl
, assembly
, ai
, &xmlbuf
);
1645 else if (unicode_tests
& IS_TEXT_UNICODE_REVERSE_SIGNATURE
)
1647 const WCHAR
*buf
= buffer
;
1651 if (!(new_buff
= RtlAllocateHeap( RtlGetProcessHeap(), 0, size
)))
1652 return STATUS_NO_MEMORY
;
1653 for (i
= 0; i
< size
/ sizeof(WCHAR
); i
++)
1654 new_buff
[i
] = RtlUshortByteSwap( buf
[i
] );
1655 xmlbuf
.ptr
= new_buff
;
1656 xmlbuf
.end
= xmlbuf
.ptr
+ size
/ sizeof(WCHAR
);
1657 status
= parse_manifest_buffer( acl
, assembly
, ai
, &xmlbuf
);
1658 RtlFreeHeap( RtlGetProcessHeap(), 0, new_buff
);
1662 /* TODO: this doesn't handle arbitrary encodings */
1666 status
= RtlMultiByteToUnicodeSize(&sizeU
, buffer
, size
);
1667 if (!NT_SUCCESS(status
))
1669 DPRINT1("RtlMultiByteToUnicodeSize failed with %lx\n", status
);
1670 return STATUS_SXS_CANT_GEN_ACTCTX
;
1673 new_buff
= RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeU
);
1675 return STATUS_NO_MEMORY
;
1677 status
= RtlMultiByteToUnicodeN(new_buff
, sizeU
, &sizeU
, buffer
, size
);
1678 if (!NT_SUCCESS(status
))
1680 DPRINT1("RtlMultiByteToUnicodeN failed with %lx\n", status
);
1681 return STATUS_SXS_CANT_GEN_ACTCTX
;
1684 xmlbuf
.ptr
= new_buff
;
1685 xmlbuf
.end
= xmlbuf
.ptr
+ sizeU
/ sizeof(WCHAR
);
1686 status
= parse_manifest_buffer(acl
, assembly
, ai
, &xmlbuf
);
1687 RtlFreeHeap(RtlGetProcessHeap(), 0, new_buff
);
1692 static NTSTATUS
open_nt_file( HANDLE
*handle
, UNICODE_STRING
*name
)
1694 OBJECT_ATTRIBUTES attr
;
1697 attr
.Length
= sizeof(attr
);
1698 attr
.RootDirectory
= 0;
1699 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1700 attr
.ObjectName
= name
;
1701 attr
.SecurityDescriptor
= NULL
;
1702 attr
.SecurityQualityOfService
= NULL
;
1703 return NtOpenFile(handle
,
1704 GENERIC_READ
| SYNCHRONIZE
,
1707 FILE_SYNCHRONOUS_IO_ALERT
);
1710 static NTSTATUS
get_module_filename( HMODULE module
, UNICODE_STRING
*str
, USHORT extra_len
)
1714 LDR_DATA_TABLE_ENTRY
*pldr
;
1716 LdrLockLoaderLock(0, NULL
, &magic
);
1717 status
= LdrFindEntryForAddress( module
, &pldr
);
1718 if (status
== STATUS_SUCCESS
)
1720 if ((str
->Buffer
= RtlAllocateHeap( RtlGetProcessHeap(), 0,
1721 pldr
->FullDllName
.Length
+ extra_len
+ sizeof(WCHAR
) )))
1723 memcpy( str
->Buffer
, pldr
->FullDllName
.Buffer
, pldr
->FullDllName
.Length
+ sizeof(WCHAR
) );
1724 str
->Length
= pldr
->FullDllName
.Length
;
1725 str
->MaximumLength
= pldr
->FullDllName
.Length
+ extra_len
+ sizeof(WCHAR
);
1727 else status
= STATUS_NO_MEMORY
;
1729 LdrUnlockLoaderLock(0, magic
);
1733 static NTSTATUS
get_manifest_in_module( struct actctx_loader
* acl
, struct assembly_identity
* ai
,
1734 LPCWSTR filename
, LPCWSTR directory
, BOOL shared
,
1735 HANDLE hModule
, LPCWSTR resname
, ULONG lang
)
1738 UNICODE_STRING nameW
;
1739 LDR_RESOURCE_INFO info
;
1740 IMAGE_RESOURCE_DATA_ENTRY
* entry
= NULL
;
1743 //DPRINT( "looking for res %s in module %p %s\n", resname,
1744 // hModule, filename );
1747 if (TRACE_ON(actctx
))
1749 if (!filename
&& !get_module_filename( hModule
, &nameW
, 0 ))
1751 DPRINT( "looking for res %s in module %p %s\n", debugstr_w(resname
),
1752 hModule
, debugstr_w(nameW
.Buffer
) );
1753 RtlFreeUnicodeString( &nameW
);
1755 else DPRINT( "looking for res %s in module %p %s\n", debugstr_w(resname
),
1756 hModule
, debugstr_w(filename
) );
1760 if (!resname
) return STATUS_INVALID_PARAMETER
;
1762 info
.Type
= (ULONG_PTR
)RT_MANIFEST
;
1763 info
.Language
= lang
;
1764 if (!((ULONG_PTR
)resname
>> 16))
1766 info
.Name
= (ULONG_PTR
)resname
;
1767 status
= LdrFindResource_U(hModule
, &info
, 3, &entry
);
1769 else if (resname
[0] == '#')
1772 RtlInitUnicodeString(&nameW
, resname
+ 1);
1773 if (RtlUnicodeStringToInteger(&nameW
, 10, &value
) != STATUS_SUCCESS
|| HIWORD(value
))
1774 return STATUS_INVALID_PARAMETER
;
1776 status
= LdrFindResource_U(hModule
, &info
, 3, &entry
);
1780 RtlCreateUnicodeString(&nameW
, resname
);
1781 RtlUpcaseUnicodeString(&nameW
, &nameW
, FALSE
);
1782 info
.Name
= (ULONG_PTR
)nameW
.Buffer
;
1783 status
= LdrFindResource_U(hModule
, &info
, 3, &entry
);
1784 RtlFreeUnicodeString(&nameW
);
1786 if (status
== STATUS_SUCCESS
) status
= LdrAccessResource(hModule
, entry
, &ptr
, NULL
);
1788 if (status
== STATUS_SUCCESS
)
1789 status
= parse_manifest(acl
, ai
, filename
, directory
, shared
, ptr
, entry
->Size
);
1794 static NTSTATUS
get_manifest_in_pe_file( struct actctx_loader
* acl
, struct assembly_identity
* ai
,
1795 LPCWSTR filename
, LPCWSTR directory
, BOOL shared
,
1796 HANDLE file
, LPCWSTR resname
, ULONG lang
)
1799 OBJECT_ATTRIBUTES attr
;
1801 LARGE_INTEGER offset
;
1806 DPRINT( "looking for res %S in %S\n", resname
, filename
);
1808 attr
.Length
= sizeof(attr
);
1809 attr
.RootDirectory
= 0;
1810 attr
.ObjectName
= NULL
;
1811 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
;
1812 attr
.SecurityDescriptor
= NULL
;
1813 attr
.SecurityQualityOfService
= NULL
;
1816 status
= NtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1817 &attr
, &size
, PAGE_READONLY
, SEC_COMMIT
, file
);
1818 if (status
!= STATUS_SUCCESS
) return status
;
1820 offset
.QuadPart
= 0;
1823 status
= NtMapViewOfSection( mapping
, NtCurrentProcess(), &base
, 0, 0, &offset
,
1824 &count
, ViewShare
, 0, PAGE_READONLY
);
1826 if (status
!= STATUS_SUCCESS
) return status
;
1828 if (RtlImageNtHeader(base
)) /* we got a PE file */
1830 HANDLE module
= (HMODULE
)((ULONG_PTR
)base
| 1); /* make it a LOAD_LIBRARY_AS_DATAFILE handle */
1831 status
= get_manifest_in_module( acl
, ai
, filename
, directory
, shared
, module
, resname
, lang
);
1833 else status
= STATUS_INVALID_IMAGE_FORMAT
;
1835 NtUnmapViewOfSection( NtCurrentProcess(), base
);
1839 static NTSTATUS
get_manifest_in_manifest_file( struct actctx_loader
* acl
, struct assembly_identity
* ai
,
1840 LPCWSTR filename
, LPCWSTR directory
, BOOL shared
, HANDLE file
)
1842 FILE_STANDARD_INFORMATION info
;
1845 OBJECT_ATTRIBUTES attr
;
1847 LARGE_INTEGER offset
;
1852 DPRINT( "loading manifest file %S\n", filename
);
1854 attr
.Length
= sizeof(attr
);
1855 attr
.RootDirectory
= 0;
1856 attr
.ObjectName
= NULL
;
1857 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
;
1858 attr
.SecurityDescriptor
= NULL
;
1859 attr
.SecurityQualityOfService
= NULL
;
1862 status
= NtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1863 &attr
, &size
, PAGE_READONLY
, SEC_COMMIT
, file
);
1865 if (status
!= STATUS_SUCCESS
) return status
;
1867 offset
.QuadPart
= 0;
1870 status
= NtMapViewOfSection( mapping
, NtCurrentProcess(), &base
, 0, 0, &offset
,
1871 &count
, ViewShare
, 0, PAGE_READONLY
);
1873 if (status
!= STATUS_SUCCESS
) return status
;
1875 /* Fixme: WINE uses FileEndOfFileInformation with NtQueryInformationFile. */
1876 status
= NtQueryInformationFile( file
, &io
, &info
, sizeof(info
), FileStandardInformation
);
1878 if (status
== STATUS_SUCCESS
)
1879 status
= parse_manifest(acl
, ai
, filename
, directory
, shared
, base
, (SIZE_T
)info
.EndOfFile
.QuadPart
);
1881 NtUnmapViewOfSection( NtCurrentProcess(), base
);
1886 /* try to load the .manifest file associated to the file */
1887 static NTSTATUS
get_manifest_in_associated_manifest( struct actctx_loader
* acl
, struct assembly_identity
* ai
,
1888 LPCWSTR filename
, LPCWSTR directory
, HMODULE module
, LPCWSTR resname
)
1890 static const WCHAR fmtW
[] = { '.','%','l','u',0 };
1893 UNICODE_STRING nameW
;
1895 ULONG_PTR resid
= (ULONG_PTR
)CREATEPROCESS_MANIFEST_RESOURCE_ID
;
1897 if (!((ULONG_PTR
)resname
>> 16)) resid
= (ULONG_PTR
)resname
& 0xffff;
1899 DPRINT( "looking for manifest associated with %S id %lu\n", filename
, resid
);
1901 if (module
) /* use the module filename */
1903 UNICODE_STRING name
;
1905 if (!(status
= get_module_filename( module
, &name
, sizeof(dotManifestW
) + 10*sizeof(WCHAR
) )))
1907 if (resid
!= 1) sprintfW( name
.Buffer
+ strlenW(name
.Buffer
), fmtW
, resid
);
1908 strcatW( name
.Buffer
, dotManifestW
);
1909 if (!RtlDosPathNameToNtPathName_U( name
.Buffer
, &nameW
, NULL
, NULL
))
1910 status
= STATUS_RESOURCE_DATA_NOT_FOUND
;
1911 RtlFreeUnicodeString( &name
);
1913 if (status
) return status
;
1917 if (!(buffer
= RtlAllocateHeap( RtlGetProcessHeap(), 0,
1918 (strlenW(filename
) + 10) * sizeof(WCHAR
) + sizeof(dotManifestW
) )))
1919 return STATUS_NO_MEMORY
;
1920 strcpyW( buffer
, filename
);
1921 if (resid
!= 1) sprintfW( buffer
+ strlenW(buffer
), fmtW
, resid
);
1922 strcatW( buffer
, dotManifestW
);
1923 RtlInitUnicodeString( &nameW
, buffer
);
1926 if (!open_nt_file( &file
, &nameW
))
1928 status
= get_manifest_in_manifest_file( acl
, ai
, nameW
.Buffer
, directory
, FALSE
, file
);
1931 else status
= STATUS_RESOURCE_DATA_NOT_FOUND
;
1932 RtlFreeUnicodeString( &nameW
);
1936 static WCHAR
*lookup_manifest_file( HANDLE dir
, struct assembly_identity
*ai
)
1938 static const WCHAR lookup_fmtW
[] =
1939 {'%','s','_','%','s','_','%','s','_','%','u','.','%','u','.','*','.','*','_',
1941 '.','m','a','n','i','f','e','s','t',0};
1943 WCHAR
*lookup
, *ret
= NULL
;
1944 UNICODE_STRING lookup_us
;
1946 unsigned int data_pos
= 0, data_len
;
1949 if (!(lookup
= RtlAllocateHeap( RtlGetProcessHeap(), 0,
1950 (strlenW(ai
->arch
) + strlenW(ai
->name
)
1951 + strlenW(ai
->public_key
) + 20) * sizeof(WCHAR
)
1952 + sizeof(lookup_fmtW
) )))
1955 sprintfW( lookup
, lookup_fmtW
, ai
->arch
, ai
->name
, ai
->public_key
, ai
->version
.major
, ai
->version
.minor
);
1956 RtlInitUnicodeString( &lookup_us
, lookup
);
1958 NtQueryDirectoryFile( dir
, 0, NULL
, NULL
, &io
, buffer
, sizeof(buffer
),
1959 FileBothDirectoryInformation
, FALSE
, &lookup_us
, TRUE
);
1960 if (io
.Status
== STATUS_SUCCESS
)
1962 FILE_BOTH_DIR_INFORMATION
*dir_info
;
1964 ULONG build
, revision
;
1966 data_len
= (ULONG
)io
.Information
;
1970 if (data_pos
>= data_len
)
1972 NtQueryDirectoryFile( dir
, 0, NULL
, NULL
, &io
, buffer
, sizeof(buffer
),
1973 FileBothDirectoryInformation
, FALSE
, &lookup_us
, FALSE
);
1974 if (io
.Status
!= STATUS_SUCCESS
) break;
1975 data_len
= (ULONG
)io
.Information
;
1978 dir_info
= (FILE_BOTH_DIR_INFORMATION
*)(buffer
+ data_pos
);
1980 if (dir_info
->NextEntryOffset
) data_pos
+= dir_info
->NextEntryOffset
;
1981 else data_pos
= data_len
;
1983 tmp
= (WCHAR
*)dir_info
->FileName
+ (strchrW(lookup
, '*') - lookup
);
1985 if (build
< ai
->version
.build
) continue;
1986 tmp
= strchrW(tmp
, '.') + 1;
1987 revision
= atoiW(tmp
);
1988 if (build
== ai
->version
.build
&& revision
< ai
->version
.revision
)
1990 ai
->version
.build
= (USHORT
)build
;
1991 ai
->version
.revision
= (USHORT
)revision
;
1993 if ((ret
= RtlAllocateHeap( RtlGetProcessHeap(), 0, dir_info
->FileNameLength
* sizeof(WCHAR
) )))
1995 memcpy( ret
, dir_info
->FileName
, dir_info
->FileNameLength
);
1996 ret
[dir_info
->FileNameLength
/sizeof(WCHAR
)] = 0;
2001 else DPRINT1("no matching file for %S\n", lookup
);
2002 RtlFreeHeap( RtlGetProcessHeap(), 0, lookup
);
2006 static NTSTATUS
lookup_winsxs(struct actctx_loader
* acl
, struct assembly_identity
* ai
)
2008 struct assembly_identity sxs_ai
;
2009 UNICODE_STRING path_us
;
2010 OBJECT_ATTRIBUTES attr
;
2012 WCHAR
*path
, *file
= NULL
;
2015 static const WCHAR manifest_dirW
[] =
2016 {'\\','w','i','n','s','x','s','\\','m','a','n','i','f','e','s','t','s',0};
2018 if (!ai
->arch
|| !ai
->name
|| !ai
->public_key
) return STATUS_NO_SUCH_FILE
;
2020 if (!(path
= RtlAllocateHeap( RtlGetProcessHeap(), 0,
2021 ((strlenW(SharedUserData
->NtSystemRoot
) + 1) *sizeof(WCHAR
)) + sizeof(manifest_dirW
) )))
2022 return STATUS_NO_MEMORY
;
2024 memcpy( path
, SharedUserData
->NtSystemRoot
, strlenW(SharedUserData
->NtSystemRoot
) * sizeof(WCHAR
) );
2025 memcpy( path
+ strlenW(SharedUserData
->NtSystemRoot
), manifest_dirW
, sizeof(manifest_dirW
) );
2027 if (!RtlDosPathNameToNtPathName_U( path
, &path_us
, NULL
, NULL
))
2029 RtlFreeHeap( RtlGetProcessHeap(), 0, path
);
2030 return STATUS_NO_SUCH_FILE
;
2032 RtlFreeHeap( RtlGetProcessHeap(), 0, path
);
2034 attr
.Length
= sizeof(attr
);
2035 attr
.RootDirectory
= 0;
2036 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2037 attr
.ObjectName
= &path_us
;
2038 attr
.SecurityDescriptor
= NULL
;
2039 attr
.SecurityQualityOfService
= NULL
;
2041 if (!NtOpenFile(&handle
,
2042 GENERIC_READ
| SYNCHRONIZE
,
2044 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2045 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
))
2048 file
= lookup_manifest_file( handle
, &sxs_ai
);
2053 RtlFreeUnicodeString( &path_us
);
2054 return STATUS_NO_SUCH_FILE
;
2057 /* append file name to directory path */
2058 if (!(path
= RtlReAllocateHeap( RtlGetProcessHeap(), 0, path_us
.Buffer
,
2059 path_us
.Length
+ (strlenW(file
) + 2) * sizeof(WCHAR
) )))
2061 RtlFreeHeap( RtlGetProcessHeap(), 0, file
);
2062 RtlFreeUnicodeString( &path_us
);
2063 return STATUS_NO_MEMORY
;
2066 path
[path_us
.Length
/sizeof(WCHAR
)] = '\\';
2067 strcpyW( path
+ path_us
.Length
/sizeof(WCHAR
) + 1, file
);
2068 RtlInitUnicodeString( &path_us
, path
);
2069 *strrchrW(file
, '.') = 0; /* remove .manifest extension */
2071 if (!open_nt_file( &handle
, &path_us
))
2073 io
.Status
= get_manifest_in_manifest_file(acl
, &sxs_ai
, path_us
.Buffer
, file
, TRUE
, handle
);
2076 else io
.Status
= STATUS_NO_SUCH_FILE
;
2078 RtlFreeHeap( RtlGetProcessHeap(), 0, file
);
2079 RtlFreeUnicodeString( &path_us
);
2083 static NTSTATUS
lookup_assembly(struct actctx_loader
* acl
,
2084 struct assembly_identity
* ai
)
2086 static const WCHAR dotDllW
[] = {'.','d','l','l',0};
2088 WCHAR
*buffer
, *p
, *directory
;
2090 UNICODE_STRING nameW
;
2093 DPRINT( "looking for name=%S version=%u.%u.%u.%u arch=%S\n",
2094 ai
->name
, ai
->version
.major
, ai
->version
.minor
, ai
->version
.build
, ai
->version
.revision
, ai
->arch
);
2096 if ((status
= lookup_winsxs(acl
, ai
)) != STATUS_NO_SUCH_FILE
) return status
;
2098 /* FIXME: add support for language specific lookup */
2100 nameW
.Buffer
= NULL
;
2101 if (!(buffer
= RtlAllocateHeap( RtlGetProcessHeap(), 0,
2102 (strlenW(acl
->actctx
->appdir
.info
) + 2 * strlenW(ai
->name
) + 2) * sizeof(WCHAR
) + sizeof(dotManifestW
) )))
2103 return STATUS_NO_MEMORY
;
2105 if (!(directory
= build_assembly_dir( ai
)))
2107 RtlFreeHeap( RtlGetProcessHeap(), 0, buffer
);
2108 return STATUS_NO_MEMORY
;
2111 /* lookup in appdir\name.dll
2112 * appdir\name.manifest
2113 * appdir\name\name.dll
2114 * appdir\name\name.manifest
2116 strcpyW( buffer
, acl
->actctx
->appdir
.info
);
2117 p
= buffer
+ strlenW(buffer
);
2118 for (i
= 0; i
< 2; i
++)
2121 strcpyW( p
, ai
->name
);
2124 strcpyW( p
, dotDllW
);
2125 if (RtlDosPathNameToNtPathName_U( buffer
, &nameW
, NULL
, NULL
))
2127 status
= open_nt_file( &file
, &nameW
);
2130 status
= get_manifest_in_pe_file( acl
, ai
, nameW
.Buffer
, directory
, FALSE
, file
,
2131 (LPCWSTR
)CREATEPROCESS_MANIFEST_RESOURCE_ID
, 0 );
2135 RtlFreeUnicodeString( &nameW
);
2138 strcpyW( p
, dotManifestW
);
2139 if (RtlDosPathNameToNtPathName_U( buffer
, &nameW
, NULL
, NULL
))
2141 status
= open_nt_file( &file
, &nameW
);
2144 status
= get_manifest_in_manifest_file( acl
, ai
, nameW
.Buffer
, directory
, FALSE
, file
);
2148 RtlFreeUnicodeString( &nameW
);
2150 status
= STATUS_SXS_ASSEMBLY_NOT_FOUND
;
2152 RtlFreeUnicodeString( &nameW
);
2153 RtlFreeHeap( RtlGetProcessHeap(), 0, directory
);
2154 RtlFreeHeap( RtlGetProcessHeap(), 0, buffer
);
2158 static NTSTATUS
parse_depend_manifests(struct actctx_loader
* acl
)
2160 NTSTATUS status
= STATUS_SUCCESS
;
2163 for (i
= 0; i
< acl
->num_dependencies
; i
++)
2165 if (lookup_assembly(acl
, &acl
->dependencies
[i
]) != STATUS_SUCCESS
)
2167 if (!acl
->dependencies
[i
].optional
)
2169 DPRINT1( "Could not find dependent assembly %S\n", acl
->dependencies
[i
].name
);
2170 status
= STATUS_SXS_CANT_GEN_ACTCTX
;
2175 /* FIXME should now iterate through all refs */
2179 /* find the appropriate activation context for RtlQueryInformationActivationContext */
2180 static NTSTATUS
find_query_actctx( HANDLE
*handle
, DWORD flags
, ULONG
class )
2182 NTSTATUS status
= STATUS_SUCCESS
;
2184 if (flags
& RTL_QUERY_ACTIVATION_CONTEXT_FLAG_USE_ACTIVE_ACTIVATION_CONTEXT
)
2186 if (*handle
) return STATUS_INVALID_PARAMETER
;
2188 if (NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
)
2189 *handle
= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
->ActivationContext
;
2191 else if (flags
& (RTL_QUERY_ACTIVATION_CONTEXT_FLAG_IS_ADDRESS
| RTL_QUERY_ACTIVATION_CONTEXT_FLAG_IS_HMODULE
))
2194 LDR_DATA_TABLE_ENTRY
*pldr
;
2196 if (!*handle
) return STATUS_INVALID_PARAMETER
;
2198 LdrLockLoaderLock( 0, NULL
, &magic
);
2199 if (!LdrFindEntryForAddress( *handle
, &pldr
))
2201 if ((flags
& RTL_QUERY_ACTIVATION_CONTEXT_FLAG_IS_HMODULE
) && *handle
!= pldr
->DllBase
)
2202 status
= STATUS_DLL_NOT_FOUND
;
2204 *handle
= pldr
->EntryPointActivationContext
;
2206 else status
= STATUS_DLL_NOT_FOUND
;
2207 LdrUnlockLoaderLock( 0, magic
);
2209 else if (!*handle
&& (class != ActivationContextBasicInformation
))
2210 *handle
= process_actctx
;
2215 static NTSTATUS
fill_keyed_data(PACTCTX_SECTION_KEYED_DATA data
, PVOID v1
, PVOID v2
, unsigned int i
)
2217 data
->ulDataFormatVersion
= 1;
2219 data
->ulLength
= 20; /* FIXME */
2220 data
->lpSectionGlobalData
= NULL
; /* FIXME */
2221 data
->ulSectionGlobalDataLength
= 0; /* FIXME */
2222 data
->lpSectionBase
= v2
;
2223 data
->ulSectionTotalLength
= 0; /* FIXME */
2224 data
->hActCtx
= NULL
;
2225 if (data
->cbSize
>= offsetof(ACTCTX_SECTION_KEYED_DATA
, ulAssemblyRosterIndex
) + sizeof(ULONG
))
2226 data
->ulAssemblyRosterIndex
= i
+ 1;
2228 return STATUS_SUCCESS
;
2231 static NTSTATUS
find_dll_redirection(ACTIVATION_CONTEXT
* actctx
, const UNICODE_STRING
*section_name
,
2232 PACTCTX_SECTION_KEYED_DATA data
)
2234 unsigned int i
, j
, snlen
= section_name
->Length
/ sizeof(WCHAR
);
2236 for (i
= 0; i
< actctx
->num_assemblies
; i
++)
2238 struct assembly
*assembly
= &actctx
->assemblies
[i
];
2239 for (j
= 0; j
< assembly
->num_dlls
; j
++)
2241 struct dll_redirect
*dll
= &assembly
->dlls
[j
];
2242 if (!strncmpiW(section_name
->Buffer
, dll
->name
, snlen
) && !dll
->name
[snlen
])
2243 return fill_keyed_data(data
, dll
, assembly
, i
);
2246 return STATUS_SXS_KEY_NOT_FOUND
;
2249 static NTSTATUS
find_window_class(ACTIVATION_CONTEXT
* actctx
, const UNICODE_STRING
*section_name
,
2250 PACTCTX_SECTION_KEYED_DATA data
)
2252 unsigned int i
, j
, k
, snlen
= section_name
->Length
/ sizeof(WCHAR
);
2254 for (i
= 0; i
< actctx
->num_assemblies
; i
++)
2256 struct assembly
*assembly
= &actctx
->assemblies
[i
];
2257 for (j
= 0; j
< assembly
->num_dlls
; j
++)
2259 struct dll_redirect
*dll
= &assembly
->dlls
[j
];
2260 for (k
= 0; k
< dll
->entities
.num
; k
++)
2262 struct entity
*entity
= &dll
->entities
.base
[k
];
2263 if (entity
->kind
== ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION
)
2265 if (!strncmpiW(section_name
->Buffer
, entity
->u
.class.name
, snlen
) && !entity
->u
.class.name
[snlen
])
2266 return fill_keyed_data(data
, entity
, dll
, i
);
2271 return STATUS_SXS_KEY_NOT_FOUND
;
2274 static NTSTATUS
find_string(ACTIVATION_CONTEXT
* actctx
, ULONG section_kind
,
2275 const UNICODE_STRING
*section_name
,
2276 DWORD flags
, PACTCTX_SECTION_KEYED_DATA data
)
2280 switch (section_kind
)
2282 case ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION
:
2283 status
= find_dll_redirection(actctx
, section_name
, data
);
2285 case ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION
:
2286 status
= find_window_class(actctx
, section_name
, data
);
2288 case ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION
:
2289 case ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION
:
2290 case ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION
:
2291 case ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION
:
2292 case ACTIVATION_CONTEXT_SECTION_GLOBAL_OBJECT_RENAME_TABLE
:
2293 case ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES
:
2294 DPRINT1("Unsupported yet section_kind %x\n", section_kind
);
2295 return STATUS_SXS_SECTION_NOT_FOUND
;
2297 DPRINT1("Unknown section_kind %x\n", section_kind
);
2298 return STATUS_SXS_SECTION_NOT_FOUND
;
2301 if (status
!= STATUS_SUCCESS
) return status
;
2303 if (flags
& FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX
)
2305 actctx_addref(actctx
);
2306 data
->hActCtx
= actctx
;
2308 return STATUS_SUCCESS
;
2311 /* initialize the activation context for the current process */
2312 void actctx_init(void)
2317 ctx
.cbSize
= sizeof(ctx
);
2318 ctx
.lpSource
= NULL
;
2319 ctx
.dwFlags
= ACTCTX_FLAG_RESOURCE_NAME_VALID
| ACTCTX_FLAG_HMODULE_VALID
;
2320 ctx
.hModule
= NtCurrentTeb()->ProcessEnvironmentBlock
->ImageBaseAddress
;
2321 ctx
.lpResourceName
= (LPCWSTR
)CREATEPROCESS_MANIFEST_RESOURCE_ID
;
2323 if (NT_SUCCESS(RtlCreateActivationContext(0, (PVOID
)&ctx
, 0, NULL
, NULL
, &handle
)))
2325 process_actctx
= check_actctx(handle
);
2329 /* FUNCTIONS ***************************************************************/
2333 RtlCreateActivationContext(IN ULONG Flags
,
2334 IN PACTIVATION_CONTEXT_DATA ActivationContextData
,
2335 IN ULONG ExtraBytes
,
2336 IN PVOID NotificationRoutine
,
2337 IN PVOID NotificationContext
,
2338 OUT PACTIVATION_CONTEXT
*ActCtx
)
2340 const ACTCTXW
*pActCtx
= (PVOID
)ActivationContextData
;
2341 const WCHAR
*directory
= NULL
;
2342 PACTIVATION_CONTEXT_WRAPPED ActualActCtx
;
2343 ACTIVATION_CONTEXT
*actctx
;
2344 UNICODE_STRING nameW
;
2346 NTSTATUS status
= STATUS_NO_MEMORY
;
2348 struct actctx_loader acl
;
2350 DPRINT("%p %08x\n", pActCtx
, pActCtx
? pActCtx
->dwFlags
: 0);
2352 if (!pActCtx
|| pActCtx
->cbSize
< sizeof(*pActCtx
) ||
2353 (pActCtx
->dwFlags
& ~ACTCTX_FLAGS_ALL
))
2354 return STATUS_INVALID_PARAMETER
;
2357 if (!(ActualActCtx
= RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ActualActCtx
))))
2358 return STATUS_NO_MEMORY
;
2360 ActualActCtx
->MagicMarker
= ACTCTX_MAGIC_MARKER
;
2362 actctx
= &ActualActCtx
->ActivationContext
;
2363 actctx
->RefCount
= 1;
2364 actctx
->config
.type
= ACTIVATION_CONTEXT_PATH_TYPE_NONE
;
2365 actctx
->config
.info
= NULL
;
2366 actctx
->appdir
.type
= ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE
;
2367 if (pActCtx
->dwFlags
& ACTCTX_FLAG_APPLICATION_NAME_VALID
)
2369 if (!(actctx
->appdir
.info
= strdupW( pActCtx
->lpApplicationName
))) goto error
;
2377 if (pActCtx
->dwFlags
& ACTCTX_FLAG_HMODULE_VALID
) module
= pActCtx
->hModule
;
2378 else module
= NtCurrentTeb()->ProcessEnvironmentBlock
->ImageBaseAddress
;
2380 status
= get_module_filename(module
, &dir
, 0);
2381 if (!NT_SUCCESS(status
)) goto error
;
2382 if ((p
= strrchrW( dir
.Buffer
, '\\' ))) p
[1] = 0;
2383 actctx
->appdir
.info
= dir
.Buffer
;
2386 nameW
.Buffer
= NULL
;
2387 if (pActCtx
->lpSource
)
2389 if (!RtlDosPathNameToNtPathName_U(pActCtx
->lpSource
, &nameW
, NULL
, NULL
))
2391 status
= STATUS_NO_SUCH_FILE
;
2394 status
= open_nt_file( &file
, &nameW
);
2395 if (!NT_SUCCESS(status
))
2397 RtlFreeUnicodeString( &nameW
);
2402 acl
.actctx
= actctx
;
2403 acl
.dependencies
= NULL
;
2404 acl
.num_dependencies
= 0;
2405 acl
.allocated_dependencies
= 0;
2407 if (pActCtx
->dwFlags
& ACTCTX_FLAG_LANGID_VALID
) lang
= pActCtx
->wLangId
;
2408 if (pActCtx
->dwFlags
& ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID
) directory
= pActCtx
->lpAssemblyDirectory
;
2410 if (pActCtx
->dwFlags
& ACTCTX_FLAG_RESOURCE_NAME_VALID
)
2412 /* if we have a resource it's a PE file */
2413 if (pActCtx
->dwFlags
& ACTCTX_FLAG_HMODULE_VALID
)
2415 status
= get_manifest_in_module( &acl
, NULL
, NULL
, directory
, FALSE
, pActCtx
->hModule
,
2416 pActCtx
->lpResourceName
, lang
);
2417 if (status
&& status
!= STATUS_SXS_CANT_GEN_ACTCTX
)
2418 /* FIXME: what to do if pActCtx->lpSource is set */
2419 status
= get_manifest_in_associated_manifest( &acl
, NULL
, NULL
, directory
,
2420 pActCtx
->hModule
, pActCtx
->lpResourceName
);
2422 else if (pActCtx
->lpSource
)
2424 status
= get_manifest_in_pe_file( &acl
, NULL
, nameW
.Buffer
, directory
, FALSE
,
2425 file
, pActCtx
->lpResourceName
, lang
);
2426 if (status
&& status
!= STATUS_SXS_CANT_GEN_ACTCTX
)
2427 status
= get_manifest_in_associated_manifest( &acl
, NULL
, nameW
.Buffer
, directory
,
2428 NULL
, pActCtx
->lpResourceName
);
2430 else status
= STATUS_INVALID_PARAMETER
;
2434 status
= get_manifest_in_manifest_file( &acl
, NULL
, nameW
.Buffer
, directory
, FALSE
, file
);
2437 if (file
) NtClose( file
);
2438 RtlFreeUnicodeString( &nameW
);
2440 if (NT_SUCCESS(status
)) status
= parse_depend_manifests(&acl
);
2441 free_depend_manifests( &acl
);
2443 if (NT_SUCCESS(status
))
2445 else actctx_release( actctx
);
2449 if (file
) NtClose( file
);
2450 actctx_release( actctx
);
2455 #define ACT_CTX_VALID(p) ((((ULONG_PTR)p - 1) | 7) != -1)
2459 RtlAddRefActivationContext(IN PACTIVATION_CONTEXT Handle
)
2461 PIACTIVATION_CONTEXT ActCtx
= (PIACTIVATION_CONTEXT
)Handle
;
2462 LONG OldRefCount
, NewRefCount
;
2464 if ((ActCtx
) && (ACT_CTX_VALID(ActCtx
)) && (ActCtx
->RefCount
!= LONG_MAX
))
2466 RtlpValidateActCtx(ActCtx
);
2470 OldRefCount
= ActCtx
->RefCount
;
2471 ASSERT(OldRefCount
> 0);
2473 if (OldRefCount
== LONG_MAX
) break;
2475 NewRefCount
= OldRefCount
+ 1;
2476 if (InterlockedCompareExchange(&ActCtx
->RefCount
,
2478 OldRefCount
) == OldRefCount
)
2484 NewRefCount
= LONG_MAX
;
2485 ASSERT(NewRefCount
> 0);
2491 RtlReleaseActivationContext( HANDLE handle
)
2493 PIACTIVATION_CONTEXT ActCtx
= (PIACTIVATION_CONTEXT
) Handle
;
2495 if ((ActCtx
) && (ACT_CTX_VALID(ActCtx
)) && (ActCtx
->RefCount
!= LONG_MAX
))
2497 RtlpValidateActCtx(ActCtx
);
2499 actctx_release(ActCtx
);
2505 RtlAddRefActivationContext( HANDLE handle
)
2507 ACTIVATION_CONTEXT
*actctx
;
2509 if ((actctx
= check_actctx(handle
))) actctx_addref(actctx
);
2514 RtlReleaseActivationContext( HANDLE handle
)
2516 ACTIVATION_CONTEXT
*actctx
;
2518 if ((actctx
= check_actctx(handle
))) actctx_release(actctx
);
2523 NTAPI
RtlActivateActivationContextEx( ULONG flags
, PTEB tebAddress
, HANDLE handle
, PULONG_PTR cookie
)
2525 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*frame
;
2527 if (!(frame
= RtlAllocateHeap( RtlGetProcessHeap(), 0, sizeof(*frame
) )))
2528 return STATUS_NO_MEMORY
;
2530 frame
->Previous
= tebAddress
->ActivationContextStackPointer
->ActiveFrame
;
2531 frame
->ActivationContext
= handle
;
2534 tebAddress
->ActivationContextStackPointer
->ActiveFrame
= frame
;
2535 RtlAddRefActivationContext( handle
);
2537 *cookie
= (ULONG_PTR
)frame
;
2538 DPRINT( "%p cookie=%lx\n", handle
, *cookie
);
2539 return STATUS_SUCCESS
;
2544 NTAPI
RtlActivateActivationContext( ULONG flags
, HANDLE handle
, PULONG_PTR cookie
)
2546 return RtlActivateActivationContextEx(flags
, NtCurrentTeb(), handle
, cookie
);
2551 RtlDeactivateActivationContext( ULONG flags
, ULONG_PTR cookie
)
2553 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*frame
, *top
;
2555 DPRINT( "%x cookie=%lx\n", flags
, cookie
);
2557 /* find the right frame */
2558 top
= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
;
2559 for (frame
= top
; frame
; frame
= frame
->Previous
)
2560 if ((ULONG_PTR
)frame
== cookie
) break;
2563 RtlRaiseStatus( STATUS_SXS_INVALID_DEACTIVATION
);
2565 if (frame
!= top
&& !(flags
& RTL_DEACTIVATE_ACTIVATION_CONTEXT_FLAG_FORCE_EARLY_DEACTIVATION
))
2566 RtlRaiseStatus( STATUS_SXS_EARLY_DEACTIVATION
);
2568 /* pop everything up to and including frame */
2569 NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
= frame
->Previous
;
2571 while (top
!= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
)
2573 frame
= top
->Previous
;
2574 RtlReleaseActivationContext( top
->ActivationContext
);
2575 RtlFreeHeap( RtlGetProcessHeap(), 0, top
);
2579 return STATUS_SUCCESS
;
2584 RtlFreeActivationContextStack(IN PACTIVATION_CONTEXT_STACK Stack
)
2586 PRTL_ACTIVATION_CONTEXT_STACK_FRAME ActiveFrame
, PrevFrame
;
2588 /* Nothing to do if there is no stack */
2591 /* Get the current active frame */
2592 ActiveFrame
= Stack
->ActiveFrame
;
2594 /* Go through them in backwards order and release */
2597 PrevFrame
= ActiveFrame
->Previous
;
2598 RtlReleaseActivationContext(ActiveFrame
->ActivationContext
);
2599 RtlFreeHeap(RtlGetProcessHeap(), 0, ActiveFrame
);
2600 ActiveFrame
= PrevFrame
;
2603 /* Zero out the active frame */
2604 Stack
->ActiveFrame
= NULL
;
2606 /* TODO: Empty the Frame List Cache */
2607 ASSERT(IsListEmpty(&Stack
->FrameListCache
));
2609 /* Free activation stack memory */
2610 RtlFreeHeap(RtlGetProcessHeap(), 0, Stack
);
2614 NTAPI
RtlFreeThreadActivationContextStack(VOID
)
2616 RtlFreeActivationContextStack(NtCurrentTeb()->ActivationContextStackPointer
);
2617 NtCurrentTeb()->ActivationContextStackPointer
= NULL
;
2622 NTAPI
RtlGetActiveActivationContext( HANDLE
*handle
)
2624 if (NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
)
2626 *handle
= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
->ActivationContext
;
2627 RtlAddRefActivationContext( *handle
);
2632 return STATUS_SUCCESS
;
2637 NTAPI
RtlIsActivationContextActive( HANDLE handle
)
2639 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*frame
;
2641 for (frame
= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
; frame
; frame
= frame
->Previous
)
2642 if (frame
->ActivationContext
== handle
) return TRUE
;
2648 RtlQueryInformationActivationContext( ULONG flags
, HANDLE handle
, PVOID subinst
,
2649 ULONG
class, PVOID buffer
,
2650 SIZE_T bufsize
, SIZE_T
*retlen
)
2652 ACTIVATION_CONTEXT
*actctx
;
2655 DPRINT("%08x %p %p %u %p %Iu %p\n", flags
, handle
,
2656 subinst
, class, buffer
, bufsize
, retlen
);
2658 if (retlen
) *retlen
= 0;
2659 if ((status
= find_query_actctx( &handle
, flags
, class ))) return status
;
2663 case ActivationContextBasicInformation
:
2665 ACTIVATION_CONTEXT_BASIC_INFORMATION
*info
= buffer
;
2667 if (retlen
) *retlen
= sizeof(*info
);
2668 if (!info
|| bufsize
< sizeof(*info
)) return STATUS_BUFFER_TOO_SMALL
;
2670 info
->hActCtx
= handle
;
2671 info
->dwFlags
= 0; /* FIXME */
2672 if (!(flags
& RTL_QUERY_ACTIVATION_CONTEXT_FLAG_NO_ADDREF
)) RtlAddRefActivationContext(handle
);
2676 case ActivationContextDetailedInformation
:
2678 ACTIVATION_CONTEXT_DETAILED_INFORMATION
*acdi
= buffer
;
2679 struct assembly
*assembly
= NULL
;
2680 SIZE_T len
, manifest_len
= 0, config_len
= 0, appdir_len
= 0;
2683 if (!(actctx
= check_actctx(handle
))) return STATUS_INVALID_PARAMETER
;
2685 if (actctx
->num_assemblies
) assembly
= actctx
->assemblies
;
2687 if (assembly
&& assembly
->manifest
.info
)
2688 manifest_len
= strlenW(assembly
->manifest
.info
) + 1;
2689 if (actctx
->config
.info
) config_len
= strlenW(actctx
->config
.info
) + 1;
2690 if (actctx
->appdir
.info
) appdir_len
= strlenW(actctx
->appdir
.info
) + 1;
2691 len
= sizeof(*acdi
) + (manifest_len
+ config_len
+ appdir_len
) * sizeof(WCHAR
);
2693 if (retlen
) *retlen
= len
;
2694 if (!buffer
|| bufsize
< len
) return STATUS_BUFFER_TOO_SMALL
;
2697 acdi
->ulFormatVersion
= assembly
? 1 : 0; /* FIXME */
2698 acdi
->ulAssemblyCount
= actctx
->num_assemblies
;
2699 acdi
->ulRootManifestPathType
= assembly
? assembly
->manifest
.type
: 0 /* FIXME */;
2700 acdi
->ulRootManifestPathChars
= assembly
&& assembly
->manifest
.info
? (DWORD
)manifest_len
- 1 : 0;
2701 acdi
->ulRootConfigurationPathType
= actctx
->config
.type
;
2702 acdi
->ulRootConfigurationPathChars
= actctx
->config
.info
? (DWORD
)config_len
- 1 : 0;
2703 acdi
->ulAppDirPathType
= actctx
->appdir
.type
;
2704 acdi
->ulAppDirPathChars
= actctx
->appdir
.info
? (DWORD
)appdir_len
- 1 : 0;
2705 ptr
= (LPWSTR
)(acdi
+ 1);
2708 acdi
->lpRootManifestPath
= ptr
;
2709 memcpy(ptr
, assembly
->manifest
.info
, manifest_len
* sizeof(WCHAR
));
2710 ptr
+= manifest_len
;
2712 else acdi
->lpRootManifestPath
= NULL
;
2715 acdi
->lpRootConfigurationPath
= ptr
;
2716 memcpy(ptr
, actctx
->config
.info
, config_len
* sizeof(WCHAR
));
2719 else acdi
->lpRootConfigurationPath
= NULL
;
2722 acdi
->lpAppDirPath
= ptr
;
2723 memcpy(ptr
, actctx
->appdir
.info
, appdir_len
* sizeof(WCHAR
));
2725 else acdi
->lpAppDirPath
= NULL
;
2729 case AssemblyDetailedInformationInActivationContext
:
2731 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION
*afdi
= buffer
;
2732 struct assembly
*assembly
;
2735 SIZE_T len
, id_len
= 0, ad_len
= 0, path_len
= 0;
2738 if (!(actctx
= check_actctx(handle
))) return STATUS_INVALID_PARAMETER
;
2739 if (!subinst
) return STATUS_INVALID_PARAMETER
;
2741 index
= *(DWORD
*)subinst
;
2742 if (!index
|| index
> actctx
->num_assemblies
) return STATUS_INVALID_PARAMETER
;
2744 assembly
= &actctx
->assemblies
[index
- 1];
2746 if (!(assembly_id
= build_assembly_id( &assembly
->id
))) return STATUS_NO_MEMORY
;
2747 id_len
= strlenW(assembly_id
) + 1;
2748 if (assembly
->directory
) ad_len
= strlenW(assembly
->directory
) + 1;
2750 if (assembly
->manifest
.info
&&
2751 (assembly
->type
== ASSEMBLY_MANIFEST
|| assembly
->type
== ASSEMBLY_SHARED_MANIFEST
))
2752 path_len
= strlenW(assembly
->manifest
.info
) + 1;
2754 len
= sizeof(*afdi
) + (id_len
+ ad_len
+ path_len
) * sizeof(WCHAR
);
2756 if (retlen
) *retlen
= len
;
2757 if (!buffer
|| bufsize
< len
)
2759 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly_id
);
2760 return STATUS_BUFFER_TOO_SMALL
;
2763 afdi
->ulFlags
= 0; /* FIXME */
2764 afdi
->ulEncodedAssemblyIdentityLength
= (DWORD
)(id_len
- 1) * sizeof(WCHAR
);
2765 afdi
->ulManifestPathType
= assembly
->manifest
.type
;
2766 afdi
->ulManifestPathLength
= assembly
->manifest
.info
? (DWORD
)(path_len
- 1) * sizeof(WCHAR
) : 0;
2767 /* FIXME afdi->liManifestLastWriteTime = 0; */
2768 afdi
->ulPolicyPathType
= ACTIVATION_CONTEXT_PATH_TYPE_NONE
; /* FIXME */
2769 afdi
->ulPolicyPathLength
= 0;
2770 /* FIXME afdi->liPolicyLastWriteTime = 0; */
2771 afdi
->ulMetadataSatelliteRosterIndex
= 0; /* FIXME */
2772 afdi
->ulManifestVersionMajor
= 1;
2773 afdi
->ulManifestVersionMinor
= 0;
2774 afdi
->ulPolicyVersionMajor
= 0; /* FIXME */
2775 afdi
->ulPolicyVersionMinor
= 0; /* FIXME */
2776 afdi
->ulAssemblyDirectoryNameLength
= ad_len
? (DWORD
)(ad_len
- 1) * sizeof(WCHAR
) : 0;
2777 ptr
= (LPWSTR
)(afdi
+ 1);
2778 afdi
->lpAssemblyEncodedAssemblyIdentity
= ptr
;
2779 memcpy( ptr
, assembly_id
, id_len
* sizeof(WCHAR
) );
2783 afdi
->lpAssemblyManifestPath
= ptr
;
2784 memcpy(ptr
, assembly
->manifest
.info
, path_len
* sizeof(WCHAR
));
2786 } else afdi
->lpAssemblyManifestPath
= NULL
;
2787 afdi
->lpAssemblyPolicyPath
= NULL
; /* FIXME */
2790 afdi
->lpAssemblyDirectoryName
= ptr
;
2791 memcpy(ptr
, assembly
->directory
, ad_len
* sizeof(WCHAR
));
2794 else afdi
->lpAssemblyDirectoryName
= NULL
;
2795 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly_id
);
2799 case FileInformationInAssemblyOfAssemblyInActivationContext
:
2801 const ACTIVATION_CONTEXT_QUERY_INDEX
*acqi
= subinst
;
2802 ASSEMBLY_FILE_DETAILED_INFORMATION
*afdi
= buffer
;
2803 struct assembly
*assembly
;
2804 struct dll_redirect
*dll
;
2805 SIZE_T len
, dll_len
= 0;
2808 if (!(actctx
= check_actctx(handle
))) return STATUS_INVALID_PARAMETER
;
2809 if (!acqi
) return STATUS_INVALID_PARAMETER
;
2811 if (acqi
->ulAssemblyIndex
>= actctx
->num_assemblies
)
2812 return STATUS_INVALID_PARAMETER
;
2813 assembly
= &actctx
->assemblies
[acqi
->ulAssemblyIndex
];
2815 if (acqi
->ulFileIndexInAssembly
>= assembly
->num_dlls
)
2816 return STATUS_INVALID_PARAMETER
;
2817 dll
= &assembly
->dlls
[acqi
->ulFileIndexInAssembly
];
2819 if (dll
->name
) dll_len
= strlenW(dll
->name
) + 1;
2820 len
= sizeof(*afdi
) + dll_len
* sizeof(WCHAR
);
2822 if (!buffer
|| bufsize
< len
)
2824 if (retlen
) *retlen
= len
;
2825 return STATUS_BUFFER_TOO_SMALL
;
2827 if (retlen
) *retlen
= 0; /* yes that's what native does !! */
2828 afdi
->ulFlags
= ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION
;
2829 afdi
->ulFilenameLength
= dll_len
? (DWORD
)(dll_len
- 1) * sizeof(WCHAR
) : 0;
2830 afdi
->ulPathLength
= 0; /* FIXME */
2831 ptr
= (LPWSTR
)(afdi
+ 1);
2834 afdi
->lpFileName
= ptr
;
2835 memcpy( ptr
, dll
->name
, dll_len
* sizeof(WCHAR
) );
2836 } else afdi
->lpFileName
= NULL
;
2837 afdi
->lpFilePath
= NULL
; /* FIXME */
2842 DPRINT( "class %u not implemented\n", class );
2843 return STATUS_NOT_IMPLEMENTED
;
2845 return STATUS_SUCCESS
;
2850 RtlQueryInformationActiveActivationContext(ULONG ulInfoClass
,
2852 SIZE_T cbBuffer OPTIONAL
,
2853 SIZE_T
*pcbWrittenOrRequired OPTIONAL
)
2855 return RtlQueryInformationActivationContext(RTL_QUERY_ACTIVATION_CONTEXT_FLAG_USE_ACTIVE_ACTIVATION_CONTEXT
,
2861 pcbWrittenOrRequired
);
2864 #define FIND_ACTCTX_RETURN_FLAGS 0x00000002
2865 #define FIND_ACTCTX_RETURN_ASSEMBLY_METADATA 0x00000004
2866 #define FIND_ACTCTX_VALID_MASK (FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX | FIND_ACTCTX_RETURN_FLAGS | FIND_ACTCTX_RETURN_ASSEMBLY_METADATA)
2870 RtlpFindActivationContextSection_CheckParameters( ULONG flags
, const GUID
*guid
, ULONG section_kind
,
2871 UNICODE_STRING
*section_name
, PACTCTX_SECTION_KEYED_DATA data
)
2873 /* Check general parameter combinations */
2874 if (!section_name
||
2875 (flags
& ~FIND_ACTCTX_VALID_MASK
) ||
2876 ((flags
& FIND_ACTCTX_VALID_MASK
) && !data
) ||
2877 (data
&& data
->cbSize
< offsetof(ACTCTX_SECTION_KEYED_DATA
, ulAssemblyRosterIndex
)))
2879 DPRINT1("invalid parameter\n");
2880 return STATUS_INVALID_PARAMETER
;
2884 if (flags
& FIND_ACTCTX_RETURN_FLAGS
||
2885 flags
& FIND_ACTCTX_RETURN_ASSEMBLY_METADATA
)
2887 DPRINT1("unknown flags %08x\n", flags
);
2888 return STATUS_INVALID_PARAMETER
;
2891 return STATUS_SUCCESS
;
2896 RtlFindActivationContextSectionString( ULONG flags
, const GUID
*guid
, ULONG section_kind
,
2897 UNICODE_STRING
*section_name
, PVOID ptr
)
2899 PACTCTX_SECTION_KEYED_DATA data
= ptr
;
2902 status
= RtlpFindActivationContextSection_CheckParameters(flags
, guid
, section_kind
, section_name
, data
);
2903 if (!NT_SUCCESS(status
)) return status
;
2905 status
= STATUS_SXS_KEY_NOT_FOUND
;
2907 /* if there is no data, but params are valid,
2908 we return that sxs key is not found to be at least somehow compatible */
2909 if (!data
) return status
;
2911 ASSERT(NtCurrentTeb());
2912 ASSERT(NtCurrentTeb()->ActivationContextStackPointer
);
2914 if (NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
)
2916 ACTIVATION_CONTEXT
*actctx
= check_actctx(NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
->ActivationContext
);
2917 if (actctx
) status
= find_string( actctx
, section_kind
, section_name
, flags
, data
);
2920 if (status
!= STATUS_SUCCESS
)
2921 status
= find_string( process_actctx
, section_kind
, section_name
, flags
, data
);
2928 RtlFindActivationContextSectionGuid(ULONG flags
, const GUID
*guid
, ULONG section_kind
, UNICODE_STRING
*section_name
, PVOID ptr
)
2931 return STATUS_NOT_IMPLEMENTED
;
2938 RtlAllocateActivationContextStack(IN PACTIVATION_CONTEXT_STACK
*Stack
)
2940 PACTIVATION_CONTEXT_STACK ContextStack
;
2942 /* Check if it's already allocated */
2943 if (*Stack
) return STATUS_SUCCESS
;
2945 /* Allocate space for the context stack */
2946 ContextStack
= RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ACTIVATION_CONTEXT_STACK
));
2949 return STATUS_NO_MEMORY
;
2952 /* Initialize the context stack */
2953 ContextStack
->Flags
= 0;
2954 ContextStack
->ActiveFrame
= NULL
;
2955 InitializeListHead(&ContextStack
->FrameListCache
);
2956 ContextStack
->NextCookieSequenceNumber
= 1;
2957 ContextStack
->StackId
= 1; //TODO: Timer-based
2959 *Stack
= ContextStack
;
2961 return STATUS_SUCCESS
;
2964 PRTL_ACTIVATION_CONTEXT_STACK_FRAME
2966 RtlActivateActivationContextUnsafeFast(IN PRTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED Frame
,
2969 #if NEW_NTDLL_LOADER
2970 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*ActiveFrame
;
2972 /* Get the curren active frame */
2973 ActiveFrame
= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
;
2975 DPRINT1("ActiveFrame %p, &Frame->Frame %p, Context %p\n", ActiveFrame
, &Frame
->Frame
, Context
);
2977 /* Actually activate it */
2978 Frame
->Frame
.Previous
= ActiveFrame
;
2979 Frame
->Frame
.ActivationContext
= Context
;
2980 Frame
->Frame
.Flags
= 0;
2982 /* Check if we can activate this context */
2983 if ((ActiveFrame
&& (ActiveFrame
->ActivationContext
!= Context
)) ||
2986 /* Set new active frame */
2987 NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
= &Frame
->Frame
;
2988 return &Frame
->Frame
;
2991 /* We can get here only one way: it was already activated */
2992 DPRINT1("Trying to activate improper activation context\n");
2994 /* Activate only if we are allowing multiple activation */
2995 if (!RtlpNotAllowingMultipleActivation
)
2997 NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
= &Frame
->Frame
;
3002 Frame
->Frame
.Flags
= 0x30;
3005 /* Return pointer to the activation frame */
3006 return &Frame
->Frame
;
3009 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*frame
= &Frame
->Frame
;
3011 frame
->Previous
= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
;
3012 frame
->ActivationContext
= Context
;
3015 NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
= frame
;
3017 return STATUS_SUCCESS
;
3021 PRTL_ACTIVATION_CONTEXT_STACK_FRAME
3023 RtlDeactivateActivationContextUnsafeFast(IN PRTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED Frame
)
3025 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*frame
;
3026 //RTL_ACTIVATION_CONTEXT_STACK_FRAME *top;
3028 /* find the right frame */
3029 //top = NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame;
3030 frame
= &Frame
->Frame
;
3034 DPRINT1("No top frame!\n");
3035 RtlRaiseStatus( STATUS_SXS_INVALID_DEACTIVATION
);
3038 /* pop everything up to and including frame */
3039 NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
= frame
->Previous
;
3047 RtlZombifyActivationContext(PVOID Context
)
3051 if (Context
== ACTCTX_FAKE_HANDLE
)
3052 return STATUS_SUCCESS
;
3054 return STATUS_NOT_IMPLEMENTED
;