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 */
17 /* INCLUDES *****************************************************************/
23 #include <wine/unicode.h>
25 BOOLEAN RtlpNotAllowingMultipleActivation
;
27 #define ACTCTX_FLAGS_ALL (\
28 ACTCTX_FLAG_PROCESSOR_ARCHITECTURE_VALID |\
29 ACTCTX_FLAG_LANGID_VALID |\
30 ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID |\
31 ACTCTX_FLAG_RESOURCE_NAME_VALID |\
32 ACTCTX_FLAG_SET_PROCESS_DEFAULT |\
33 ACTCTX_FLAG_APPLICATION_NAME_VALID |\
34 ACTCTX_FLAG_SOURCE_IS_ASSEMBLYREF |\
35 ACTCTX_FLAG_HMODULE_VALID )
37 #define ACTCTX_MAGIC_MARKER (PVOID)'gMcA'
39 #define ACTCTX_FAKE_HANDLE ((HANDLE) 0xf00baa)
40 #define ACTCTX_FAKE_COOKIE ((ULONG_PTR) 0xf00bad)
62 struct assembly_version
70 struct assembly_identity
77 struct assembly_version version
;
121 unsigned int allocated
;
128 struct entity_array entities
;
133 APPLICATION_MANIFEST
,
135 ASSEMBLY_SHARED_MANIFEST
,
140 enum assembly_type type
;
141 struct assembly_identity id
;
142 struct file_info manifest
;
145 struct dll_redirect
*dlls
;
146 unsigned int num_dlls
;
147 unsigned int allocated_dlls
;
148 struct entity_array entities
;
151 typedef struct _ASSEMBLY_STORAGE_MAP_ENTRY
154 UNICODE_STRING DosPath
;
156 } ASSEMBLY_STORAGE_MAP_ENTRY
, *PASSEMBLY_STORAGE_MAP_ENTRY
;
158 typedef struct _ASSEMBLY_STORAGE_MAP
162 PASSEMBLY_STORAGE_MAP_ENTRY
*AssemblyArray
;
163 } ASSEMBLY_STORAGE_MAP
, *PASSEMBLY_STORAGE_MAP
;
165 typedef struct _ACTIVATION_CONTEXT
170 PACTIVATION_CONTEXT_DATA ActivationContextData
;
171 PVOID NotificationRoutine
;
172 PVOID NotificationContext
;
173 ULONG SentNotifications
[8];
174 ULONG DisabledNotifications
[8];
175 ASSEMBLY_STORAGE_MAP StorageMap
;
176 PASSEMBLY_STORAGE_MAP_ENTRY InlineStorageMapEntries
;
177 ULONG StackTraceIndex
;
178 PVOID StackTraces
[4][4];
179 struct file_info config
;
180 struct file_info appdir
;
181 struct assembly
*assemblies
;
182 unsigned int num_assemblies
;
183 unsigned int allocated_assemblies
;
184 } ACTIVATION_CONTEXT
, *PIACTIVATION_CONTEXT
;
188 ACTIVATION_CONTEXT
*actctx
;
189 struct assembly_identity
*dependencies
;
190 unsigned int num_dependencies
;
191 unsigned int allocated_dependencies
;
194 typedef struct _ACTIVATION_CONTEXT_WRAPPED
197 ACTIVATION_CONTEXT ActivationContext
;
198 } ACTIVATION_CONTEXT_WRAPPED
, *PACTIVATION_CONTEXT_WRAPPED
;
202 RtlpSxsBreakOnInvalidMarker(IN PACTIVATION_CONTEXT ActCtx
,
203 IN ULONG FailureCode
)
205 EXCEPTION_RECORD ExceptionRecord
;
207 /* Fatal SxS exception header */
208 ExceptionRecord
.ExceptionRecord
= NULL
;
209 ExceptionRecord
.ExceptionCode
= STATUS_SXS_CORRUPTION
;
210 ExceptionRecord
.ExceptionFlags
= EXCEPTION_NONCONTINUABLE
;
212 /* With SxS-specific information plus the context itself */
213 ExceptionRecord
.ExceptionInformation
[0] = 1;
214 ExceptionRecord
.ExceptionInformation
[1] = FailureCode
;
215 ExceptionRecord
.ExceptionInformation
[2] = (ULONG_PTR
)ActCtx
;
216 ExceptionRecord
.NumberParameters
= 3;
219 RtlRaiseException(&ExceptionRecord
);
224 RtlpValidateActCtx(IN PACTIVATION_CONTEXT ActCtx
)
226 PACTIVATION_CONTEXT_WRAPPED pActual
;
228 /* Get the caller-opaque header */
229 pActual
= CONTAINING_RECORD(ActCtx
,
230 ACTIVATION_CONTEXT_WRAPPED
,
233 /* Check if the header matches as expected */
234 if (pActual
->MagicMarker
!= ACTCTX_MAGIC_MARKER
)
236 /* Nope, print out a warning, assert, and then throw an exception */
237 DbgPrint("%s : Invalid activation context marker %p found in activation context %p\n"
238 " This means someone stepped on the allocation, or someone is using a\n"
239 " deallocated activation context\n",
241 pActual
->MagicMarker
,
243 ASSERT(pActual
->MagicMarker
== ACTCTX_MAGIC_MARKER
);
244 RtlpSxsBreakOnInvalidMarker(ActCtx
, 1);
248 static const WCHAR assemblyW
[] = {'a','s','s','e','m','b','l','y',0};
249 static const WCHAR assemblyIdentityW
[] = {'a','s','s','e','m','b','l','y','I','d','e','n','t','i','t','y',0};
250 static const WCHAR bindingRedirectW
[] = {'b','i','n','d','i','n','g','R','e','d','i','r','e','c','t',0};
251 static const WCHAR clrClassW
[] = {'c','l','r','C','l','a','s','s',0};
252 static const WCHAR clrSurrogateW
[] = {'c','l','r','S','u','r','r','o','g','a','t','e',0};
253 static const WCHAR comClassW
[] = {'c','o','m','C','l','a','s','s',0};
254 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};
255 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};
256 static const WCHAR dependencyW
[] = {'d','e','p','e','n','d','e','n','c','y',0};
257 static const WCHAR dependentAssemblyW
[] = {'d','e','p','e','n','d','e','n','t','A','s','s','e','m','b','l','y',0};
258 static const WCHAR descriptionW
[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
259 static const WCHAR fileW
[] = {'f','i','l','e',0};
260 static const WCHAR asmv2hashW
[] = {'a','s','m','v','2',':','h','a','s','h',0};
261 static const WCHAR noInheritW
[] = {'n','o','I','n','h','e','r','i','t',0};
262 static const WCHAR noInheritableW
[] = {'n','o','I','n','h','e','r','i','t','a','b','l','e',0};
263 static const WCHAR typelibW
[] = {'t','y','p','e','l','i','b',0};
264 static const WCHAR windowClassW
[] = {'w','i','n','d','o','w','C','l','a','s','s',0};
266 static const WCHAR clsidW
[] = {'c','l','s','i','d',0};
267 static const WCHAR hashW
[] = {'h','a','s','h',0};
268 static const WCHAR hashalgW
[] = {'h','a','s','h','a','l','g',0};
269 static const WCHAR helpdirW
[] = {'h','e','l','p','d','i','r',0};
270 static const WCHAR iidW
[] = {'i','i','d',0};
271 static const WCHAR languageW
[] = {'l','a','n','g','u','a','g','e',0};
272 static const WCHAR manifestVersionW
[] = {'m','a','n','i','f','e','s','t','V','e','r','s','i','o','n',0};
273 static const WCHAR g_nameW
[] = {'n','a','m','e',0};
274 static const WCHAR newVersionW
[] = {'n','e','w','V','e','r','s','i','o','n',0};
275 static const WCHAR oldVersionW
[] = {'o','l','d','V','e','r','s','i','o','n',0};
276 static const WCHAR optionalW
[] = {'o','p','t','i','o','n','a','l',0};
277 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};
278 static const WCHAR publicKeyTokenW
[] = {'p','u','b','l','i','c','K','e','y','T','o','k','e','n',0};
279 static const WCHAR tlbidW
[] = {'t','l','b','i','d',0};
280 static const WCHAR typeW
[] = {'t','y','p','e',0};
281 static const WCHAR versionW
[] = {'v','e','r','s','i','o','n',0};
282 static const WCHAR xmlnsW
[] = {'x','m','l','n','s',0};
284 static const WCHAR g_xmlW
[] = {'?','x','m','l',0};
285 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};
286 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};
288 static const WCHAR dotManifestW
[] = {'.','m','a','n','i','f','e','s','t',0};
289 static const WCHAR version_formatW
[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
291 static ACTIVATION_CONTEXT_WRAPPED system_actctx
= { ACTCTX_MAGIC_MARKER
, { 1 } };
292 static ACTIVATION_CONTEXT
*process_actctx
= &system_actctx
.ActivationContext
;
294 static WCHAR
*strdupW(const WCHAR
* str
)
298 if (!(ptr
= RtlAllocateHeap(RtlGetProcessHeap(), 0, (strlenW(str
) + 1) * sizeof(WCHAR
))))
300 return strcpyW(ptr
, str
);
303 static WCHAR
*xmlstrdupW(const xmlstr_t
* str
)
307 if ((strW
= RtlAllocateHeap(RtlGetProcessHeap(), 0, (str
->len
+ 1) * sizeof(WCHAR
))))
309 memcpy( strW
, str
->ptr
, str
->len
* sizeof(WCHAR
) );
315 static UNICODE_STRING
xmlstr2unicode(const xmlstr_t
*xmlstr
)
319 res
.Buffer
= (PWSTR
)xmlstr
->ptr
;
320 res
.Length
= res
.MaximumLength
= (USHORT
)xmlstr
->len
* sizeof(WCHAR
);
325 static inline BOOL
xmlstr_cmp(const xmlstr_t
* xmlstr
, const WCHAR
*str
)
327 return !strncmpW(xmlstr
->ptr
, str
, xmlstr
->len
) && !str
[xmlstr
->len
];
330 static inline BOOL
xmlstr_cmpi(const xmlstr_t
* xmlstr
, const WCHAR
*str
)
332 return !strncmpiW(xmlstr
->ptr
, str
, xmlstr
->len
) && !str
[xmlstr
->len
];
335 static inline BOOL
xmlstr_cmp_end(const xmlstr_t
* xmlstr
, const WCHAR
*str
)
337 return (xmlstr
->len
&& xmlstr
->ptr
[0] == '/' &&
338 !strncmpW(xmlstr
->ptr
+ 1, str
, xmlstr
->len
- 1) && !str
[xmlstr
->len
- 1]);
341 static inline BOOL
isxmlspace( WCHAR ch
)
343 return (ch
== ' ' || ch
== '\r' || ch
== '\n' || ch
== '\t');
346 static struct assembly
*add_assembly(ACTIVATION_CONTEXT
*actctx
, enum assembly_type at
)
348 struct assembly
*assembly
;
350 if (actctx
->num_assemblies
== actctx
->allocated_assemblies
)
353 unsigned int new_count
;
354 if (actctx
->assemblies
)
356 new_count
= actctx
->allocated_assemblies
* 2;
357 ptr
= RtlReAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY
,
358 actctx
->assemblies
, new_count
* sizeof(*assembly
) );
363 ptr
= RtlAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, new_count
* sizeof(*assembly
) );
365 if (!ptr
) return NULL
;
366 actctx
->assemblies
= ptr
;
367 actctx
->allocated_assemblies
= new_count
;
370 assembly
= &actctx
->assemblies
[actctx
->num_assemblies
++];
375 static struct dll_redirect
* add_dll_redirect(struct assembly
* assembly
)
377 if (assembly
->num_dlls
== assembly
->allocated_dlls
)
380 unsigned int new_count
;
383 new_count
= assembly
->allocated_dlls
* 2;
384 ptr
= RtlReAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY
,
385 assembly
->dlls
, new_count
* sizeof(*assembly
->dlls
) );
390 ptr
= RtlAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, new_count
* sizeof(*assembly
->dlls
) );
392 if (!ptr
) return NULL
;
393 assembly
->dlls
= ptr
;
394 assembly
->allocated_dlls
= new_count
;
396 return &assembly
->dlls
[assembly
->num_dlls
++];
399 static void free_assembly_identity(struct assembly_identity
*ai
)
401 RtlFreeHeap( RtlGetProcessHeap(), 0, ai
->name
);
402 RtlFreeHeap( RtlGetProcessHeap(), 0, ai
->arch
);
403 RtlFreeHeap( RtlGetProcessHeap(), 0, ai
->public_key
);
404 RtlFreeHeap( RtlGetProcessHeap(), 0, ai
->language
);
405 RtlFreeHeap( RtlGetProcessHeap(), 0, ai
->type
);
408 static struct entity
* add_entity(struct entity_array
*array
, DWORD kind
)
410 struct entity
* entity
;
412 if (array
->num
== array
->allocated
)
415 unsigned int new_count
;
418 new_count
= array
->allocated
* 2;
419 ptr
= RtlReAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY
,
420 array
->base
, new_count
* sizeof(*array
->base
) );
425 ptr
= RtlAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, new_count
* sizeof(*array
->base
) );
427 if (!ptr
) return NULL
;
429 array
->allocated
= new_count
;
431 entity
= &array
->base
[array
->num
++];
436 static void free_entity_array(struct entity_array
*array
)
439 for (i
= 0; i
< array
->num
; i
++)
441 struct entity
*entity
= &array
->base
[i
];
442 switch (entity
->kind
)
444 case ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION
:
445 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.comclass
.clsid
);
447 case ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION
:
448 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.proxy
.iid
);
449 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.proxy
.name
);
451 case ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION
:
452 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.typelib
.tlbid
);
453 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.typelib
.version
);
454 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.typelib
.helpdir
);
456 case ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION
:
457 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.class.name
);
459 case ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION
:
460 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.clrclass
.name
);
461 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.clrclass
.clsid
);
463 case ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES
:
464 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.clrsurrogate
.name
);
465 RtlFreeHeap(RtlGetProcessHeap(), 0, entity
->u
.clrsurrogate
.clsid
);
468 DPRINT1("Unknown entity kind %u\n", entity
->kind
);
471 RtlFreeHeap( RtlGetProcessHeap(), 0, array
->base
);
474 static BOOL
is_matching_string( const WCHAR
*str1
, const WCHAR
*str2
)
476 if (!str1
) return !str2
;
477 return str2
&& !strcmpiW( str1
, str2
);
480 static BOOL
is_matching_identity( const struct assembly_identity
*id1
,
481 const struct assembly_identity
*id2
)
483 if (!is_matching_string( id1
->name
, id2
->name
)) return FALSE
;
484 if (!is_matching_string( id1
->arch
, id2
->arch
)) return FALSE
;
485 if (!is_matching_string( id1
->public_key
, id2
->public_key
)) return FALSE
;
487 if (id1
->language
&& id2
->language
&& strcmpiW( id1
->language
, id2
->language
))
489 static const WCHAR wildcardW
[] = {'*',0};
490 if (strcmpW( wildcardW
, id1
->language
) && strcmpW( wildcardW
, id2
->language
))
493 if (id1
->version
.major
!= id2
->version
.major
) return FALSE
;
494 if (id1
->version
.minor
!= id2
->version
.minor
) return FALSE
;
495 if (id1
->version
.build
> id2
->version
.build
) return FALSE
;
496 if (id1
->version
.build
== id2
->version
.build
&&
497 id1
->version
.revision
> id2
->version
.revision
) return FALSE
;
501 static BOOL
add_dependent_assembly_id(struct actctx_loader
* acl
,
502 struct assembly_identity
* ai
)
506 /* check if we already have that assembly */
508 for (i
= 0; i
< acl
->actctx
->num_assemblies
; i
++)
509 if (is_matching_identity( ai
, &acl
->actctx
->assemblies
[i
].id
))
511 DPRINT( "reusing existing assembly for %S arch %S version %u.%u.%u.%u\n",
512 ai
->name
, ai
->arch
, ai
->version
.major
, ai
->version
.minor
,
513 ai
->version
.build
, ai
->version
.revision
);
517 for (i
= 0; i
< acl
->num_dependencies
; i
++)
518 if (is_matching_identity( ai
, &acl
->dependencies
[i
] ))
520 DPRINT( "reusing existing dependency for %S arch %S version %u.%u.%u.%u\n",
521 ai
->name
, ai
->arch
, ai
->version
.major
, ai
->version
.minor
,
522 ai
->version
.build
, ai
->version
.revision
);
526 if (acl
->num_dependencies
== acl
->allocated_dependencies
)
529 unsigned int new_count
;
530 if (acl
->dependencies
)
532 new_count
= acl
->allocated_dependencies
* 2;
533 ptr
= RtlReAllocateHeap(RtlGetProcessHeap(), 0, acl
->dependencies
,
534 new_count
* sizeof(acl
->dependencies
[0]));
539 ptr
= RtlAllocateHeap(RtlGetProcessHeap(), 0, new_count
* sizeof(acl
->dependencies
[0]));
541 if (!ptr
) return FALSE
;
542 acl
->dependencies
= ptr
;
543 acl
->allocated_dependencies
= new_count
;
545 acl
->dependencies
[acl
->num_dependencies
++] = *ai
;
550 static void free_depend_manifests(struct actctx_loader
* acl
)
553 for (i
= 0; i
< acl
->num_dependencies
; i
++)
554 free_assembly_identity(&acl
->dependencies
[i
]);
555 RtlFreeHeap(RtlGetProcessHeap(), 0, acl
->dependencies
);
558 static WCHAR
*build_assembly_dir(struct assembly_identity
* ai
)
560 static const WCHAR undW
[] = {'_',0};
561 static const WCHAR noneW
[] = {'n','o','n','e',0};
562 static const WCHAR mskeyW
[] = {'d','e','a','d','b','e','e','f',0};
564 const WCHAR
*arch
= ai
->arch
? ai
->arch
: noneW
;
565 const WCHAR
*key
= ai
->public_key
? ai
->public_key
: noneW
;
566 const WCHAR
*lang
= ai
->language
? ai
->language
: noneW
;
567 const WCHAR
*name
= ai
->name
? ai
->name
: noneW
;
568 SIZE_T size
= (strlenW(arch
) + 1 + strlenW(name
) + 1 + strlenW(key
) + 24 + 1 +
569 strlenW(lang
) + 1) * sizeof(WCHAR
) + sizeof(mskeyW
);
572 if (!(ret
= RtlAllocateHeap( RtlGetProcessHeap(), 0, size
))) return NULL
;
574 strcpyW( ret
, arch
);
575 strcatW( ret
, undW
);
576 strcatW( ret
, name
);
577 strcatW( ret
, undW
);
579 strcatW( ret
, undW
);
580 sprintfW( ret
+ strlenW(ret
), version_formatW
,
581 ai
->version
.major
, ai
->version
.minor
, ai
->version
.build
, ai
->version
.revision
);
582 strcatW( ret
, undW
);
583 strcatW( ret
, lang
);
584 strcatW( ret
, undW
);
585 strcatW( ret
, mskeyW
);
589 static inline void append_string( WCHAR
*buffer
, const WCHAR
*prefix
, const WCHAR
*str
)
594 strcatW( buffer
, prefix
);
603 static WCHAR
*build_assembly_id( const struct assembly_identity
*ai
)
605 static const WCHAR archW
[] =
606 {',','p','r','o','c','e','s','s','o','r','A','r','c','h','i','t','e','c','t','u','r','e','=',0};
607 static const WCHAR public_keyW
[] =
608 {',','p','u','b','l','i','c','K','e','y','T','o','k','e','n','=',0};
610 WCHAR version
[64], *ret
;
613 sprintfW( version
, version_formatW
,
614 ai
->version
.major
, ai
->version
.minor
, ai
->version
.build
, ai
->version
.revision
);
615 if (ai
->name
) size
+= strlenW(ai
->name
);
616 if (ai
->arch
) size
+= strlenW(archW
) + strlenW(ai
->arch
) + 2;
617 if (ai
->public_key
) size
+= strlenW(public_keyW
) + strlenW(ai
->public_key
) + 2;
618 if (ai
->type
) size
+= 1 + strlenW(typeW
) + 1 + strlenW(ai
->type
) + 2;
619 size
+= 1+ strlenW(versionW
) + 1 + strlenW(version
) + 2;
621 if (!(ret
= RtlAllocateHeap( RtlGetProcessHeap(), 0, (size
+ 1) * sizeof(WCHAR
) )))
624 if (ai
->name
) strcpyW( ret
, ai
->name
);
626 append_string( ret
, archW
, ai
->arch
);
627 append_string( ret
, public_keyW
, ai
->public_key
);
628 append_string( ret
, typeW
, ai
->type
);
629 append_string( ret
, versionW
, version
);
632 static ACTIVATION_CONTEXT
*check_actctx( HANDLE h
)
634 ACTIVATION_CONTEXT
*ret
= NULL
, *actctx
= h
;
635 PACTIVATION_CONTEXT_WRAPPED pActual
;
637 if (!h
|| h
== INVALID_HANDLE_VALUE
) return NULL
;
642 pActual
= CONTAINING_RECORD(actctx
, ACTIVATION_CONTEXT_WRAPPED
, ActivationContext
);
643 if (pActual
->MagicMarker
== ACTCTX_MAGIC_MARKER
) ret
= &pActual
->ActivationContext
;
646 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
648 DPRINT1("Invalid activation context handle!\n");
654 static inline void actctx_addref( ACTIVATION_CONTEXT
*actctx
)
656 InterlockedExchangeAdd( &actctx
->RefCount
, 1 );
659 static void actctx_release( ACTIVATION_CONTEXT
*actctx
)
661 PACTIVATION_CONTEXT_WRAPPED pActual
;
663 if (InterlockedExchangeAdd(&actctx
->RefCount
, -1) == 1)
667 for (i
= 0; i
< actctx
->num_assemblies
; i
++)
669 struct assembly
*assembly
= &actctx
->assemblies
[i
];
670 for (j
= 0; j
< assembly
->num_dlls
; j
++)
672 struct dll_redirect
*dll
= &assembly
->dlls
[j
];
673 free_entity_array( &dll
->entities
);
674 RtlFreeHeap( RtlGetProcessHeap(), 0, dll
->name
);
675 RtlFreeHeap( RtlGetProcessHeap(), 0, dll
->hash
);
677 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly
->dlls
);
678 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly
->manifest
.info
);
679 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly
->directory
);
680 free_entity_array( &assembly
->entities
);
681 free_assembly_identity(&assembly
->id
);
683 RtlFreeHeap( RtlGetProcessHeap(), 0, actctx
->config
.info
);
684 RtlFreeHeap( RtlGetProcessHeap(), 0, actctx
->appdir
.info
);
685 RtlFreeHeap( RtlGetProcessHeap(), 0, actctx
->assemblies
);
686 pActual
= CONTAINING_RECORD(actctx
, ACTIVATION_CONTEXT_WRAPPED
, ActivationContext
);
687 pActual
->MagicMarker
= 0;
688 RtlFreeHeap(RtlGetProcessHeap(), 0, pActual
);
692 static BOOL
next_xml_attr(xmlbuf_t
* xmlbuf
, xmlstr_t
* name
, xmlstr_t
* value
,
693 BOOL
* error
, BOOL
* end
)
699 while (xmlbuf
->ptr
< xmlbuf
->end
&& isxmlspace(*xmlbuf
->ptr
))
702 if (xmlbuf
->ptr
== xmlbuf
->end
) return FALSE
;
704 if (*xmlbuf
->ptr
== '/')
707 if (xmlbuf
->ptr
== xmlbuf
->end
|| *xmlbuf
->ptr
!= '>')
716 if (*xmlbuf
->ptr
== '>')
724 while (ptr
< xmlbuf
->end
&& *ptr
!= '=' && *ptr
!= '>' && !isxmlspace(*ptr
)) ptr
++;
726 if (ptr
== xmlbuf
->end
|| *ptr
!= '=') return FALSE
;
728 name
->ptr
= xmlbuf
->ptr
;
729 name
->len
= (ULONG
)(ptr
- xmlbuf
->ptr
);
733 if (ptr
== xmlbuf
->end
|| (*ptr
!= '"' && *ptr
!= '\'')) return FALSE
;
736 if (ptr
== xmlbuf
->end
) return FALSE
;
738 ptr
= memchrW(ptr
, ptr
[-1], xmlbuf
->end
- ptr
);
741 xmlbuf
->ptr
= xmlbuf
->end
;
745 value
->len
= (ULONG
)(ptr
- value
->ptr
);
746 xmlbuf
->ptr
= ptr
+ 1;
748 if (xmlbuf
->ptr
== xmlbuf
->end
) return FALSE
;
754 static BOOL
next_xml_elem(xmlbuf_t
* xmlbuf
, xmlstr_t
* elem
)
760 ptr
= memchrW(xmlbuf
->ptr
, '<', xmlbuf
->end
- xmlbuf
->ptr
);
763 xmlbuf
->ptr
= xmlbuf
->end
;
767 if (ptr
+ 3 < xmlbuf
->end
&& ptr
[0] == '!' && ptr
[1] == '-' && ptr
[2] == '-') /* skip comment */
769 for (ptr
+= 3; ptr
+ 3 <= xmlbuf
->end
; ptr
++)
770 if (ptr
[0] == '-' && ptr
[1] == '-' && ptr
[2] == '>') break;
772 if (ptr
+ 3 > xmlbuf
->end
)
774 xmlbuf
->ptr
= xmlbuf
->end
;
777 xmlbuf
->ptr
= ptr
+ 3;
783 while (ptr
< xmlbuf
->end
&& !isxmlspace(*ptr
) && *ptr
!= '>' && (*ptr
!= '/' || ptr
== xmlbuf
->ptr
))
786 elem
->ptr
= xmlbuf
->ptr
;
787 elem
->len
= (ULONG
)(ptr
- xmlbuf
->ptr
);
789 return xmlbuf
->ptr
!= xmlbuf
->end
;
792 static BOOL
parse_xml_header(xmlbuf_t
* xmlbuf
)
794 /* FIXME: parse attributes */
797 for (ptr
= xmlbuf
->ptr
; ptr
< xmlbuf
->end
- 1; ptr
++)
799 if (ptr
[0] == '?' && ptr
[1] == '>')
801 xmlbuf
->ptr
= ptr
+ 2;
808 static BOOL
parse_text_content(xmlbuf_t
* xmlbuf
, xmlstr_t
* content
)
810 const WCHAR
*ptr
= memchrW(xmlbuf
->ptr
, '<', xmlbuf
->end
- xmlbuf
->ptr
);
812 if (!ptr
) return FALSE
;
814 content
->ptr
= xmlbuf
->ptr
;
815 content
->len
= (ULONG
)(ptr
- xmlbuf
->ptr
);
821 static BOOL
parse_version(const xmlstr_t
*str
, struct assembly_version
*version
)
828 /* major.minor.build.revision */
829 ver
[0] = ver
[1] = ver
[2] = ver
[3] = pos
= 0;
830 for (curr
= str
->ptr
; curr
< str
->ptr
+ str
->len
; curr
++)
832 if (*curr
>= '0' && *curr
<= '9')
834 ver
[pos
] = ver
[pos
] * 10 + *curr
- '0';
835 if (ver
[pos
] >= 0x10000) goto error
;
837 else if (*curr
== '.')
839 if (++pos
>= 4) goto error
;
843 version
->major
= ver
[0];
844 version
->minor
= ver
[1];
845 version
->build
= ver
[2];
846 version
->revision
= ver
[3];
850 strU
= xmlstr2unicode(str
);
851 DPRINT1( "Wrong version definition in manifest file (%wZ)\n", &strU
);
855 static BOOL
parse_expect_elem(xmlbuf_t
* xmlbuf
, const WCHAR
* name
)
858 UNICODE_STRING elemU
;
859 if (!next_xml_elem(xmlbuf
, &elem
)) return FALSE
;
860 if (xmlstr_cmp(&elem
, name
)) return TRUE
;
861 elemU
= xmlstr2unicode(&elem
);
862 DPRINT1( "unexpected element %wZ\n", &elemU
);
866 static BOOL
parse_expect_no_attr(xmlbuf_t
* xmlbuf
, BOOL
* end
)
868 xmlstr_t attr_name
, attr_value
;
869 UNICODE_STRING attr_nameU
, attr_valueU
;
872 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, end
))
874 attr_nameU
= xmlstr2unicode(&attr_name
);
875 attr_valueU
= xmlstr2unicode(&attr_value
);
876 DPRINT1( "unexpected attr %wZ=%wZ\n", &attr_nameU
,
882 static BOOL
parse_end_element(xmlbuf_t
*xmlbuf
)
885 return parse_expect_no_attr(xmlbuf
, &end
) && !end
;
888 static BOOL
parse_expect_end_elem(xmlbuf_t
*xmlbuf
, const WCHAR
*name
)
891 UNICODE_STRING elemU
;
892 if (!next_xml_elem(xmlbuf
, &elem
)) return FALSE
;
893 if (!xmlstr_cmp_end(&elem
, name
))
895 elemU
= xmlstr2unicode(&elem
);
896 DPRINT1( "unexpected element %wZ\n", &elemU
);
899 return parse_end_element(xmlbuf
);
902 static BOOL
parse_unknown_elem(xmlbuf_t
*xmlbuf
, const xmlstr_t
*unknown_elem
)
904 xmlstr_t attr_name
, attr_value
, elem
;
905 BOOL end
= FALSE
, error
, ret
= TRUE
;
907 while(next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
));
908 if(error
|| end
) return end
;
910 while(ret
&& (ret
= next_xml_elem(xmlbuf
, &elem
)))
912 if(*elem
.ptr
== '/' && elem
.len
- 1 == unknown_elem
->len
&&
913 !strncmpW(elem
.ptr
+1, unknown_elem
->ptr
, unknown_elem
->len
))
916 ret
= parse_unknown_elem(xmlbuf
, &elem
);
919 return ret
&& parse_end_element(xmlbuf
);
922 static BOOL
parse_assembly_identity_elem(xmlbuf_t
* xmlbuf
, ACTIVATION_CONTEXT
* actctx
,
923 struct assembly_identity
* ai
)
925 xmlstr_t attr_name
, attr_value
;
926 BOOL end
= FALSE
, error
;
927 UNICODE_STRING attr_valueU
, attr_nameU
;
929 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
931 if (xmlstr_cmp(&attr_name
, g_nameW
))
933 if (!(ai
->name
= xmlstrdupW(&attr_value
))) return FALSE
;
935 else if (xmlstr_cmp(&attr_name
, typeW
))
937 if (!(ai
->type
= xmlstrdupW(&attr_value
))) return FALSE
;
939 else if (xmlstr_cmp(&attr_name
, versionW
))
941 if (!parse_version(&attr_value
, &ai
->version
)) return FALSE
;
943 else if (xmlstr_cmp(&attr_name
, processorArchitectureW
))
945 if (!(ai
->arch
= xmlstrdupW(&attr_value
))) return FALSE
;
947 else if (xmlstr_cmp(&attr_name
, publicKeyTokenW
))
949 if (!(ai
->public_key
= xmlstrdupW(&attr_value
))) return FALSE
;
951 else if (xmlstr_cmp(&attr_name
, languageW
))
953 if (!(ai
->language
= xmlstrdupW(&attr_value
))) return FALSE
;
954 DPRINT1("Unsupported yet language attribute (%S)\n",
959 attr_nameU
= xmlstr2unicode(&attr_name
);
960 attr_valueU
= xmlstr2unicode(&attr_value
);
961 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
965 if (error
|| end
) return end
;
966 return parse_expect_end_elem(xmlbuf
, assemblyIdentityW
);
969 static BOOL
parse_com_class_elem(xmlbuf_t
* xmlbuf
, struct dll_redirect
* dll
)
971 xmlstr_t elem
, attr_name
, attr_value
;
972 BOOL ret
, end
= FALSE
, error
;
973 struct entity
* entity
;
974 UNICODE_STRING attr_valueU
, attr_nameU
;
976 if (!(entity
= add_entity(&dll
->entities
, ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION
)))
979 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
981 if (xmlstr_cmp(&attr_name
, clsidW
))
983 if (!(entity
->u
.comclass
.clsid
= xmlstrdupW(&attr_value
))) return FALSE
;
987 attr_nameU
= xmlstr2unicode(&attr_name
);
988 attr_valueU
= xmlstr2unicode(&attr_value
);
989 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
993 if (error
|| end
) return end
;
995 while ((ret
= next_xml_elem(xmlbuf
, &elem
)))
997 if (xmlstr_cmp_end(&elem
, comClassW
))
999 ret
= parse_end_element(xmlbuf
);
1004 attr_nameU
= xmlstr2unicode(&elem
);
1005 DPRINT1("unknown elem %wZ\n", &attr_nameU
);
1006 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1012 static BOOL
parse_cominterface_proxy_stub_elem(xmlbuf_t
* xmlbuf
, struct dll_redirect
* dll
)
1014 xmlstr_t attr_name
, attr_value
;
1015 BOOL end
= FALSE
, error
;
1016 struct entity
* entity
;
1017 UNICODE_STRING attr_valueU
, attr_nameU
;
1019 if (!(entity
= add_entity(&dll
->entities
, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION
)))
1022 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1024 if (xmlstr_cmp(&attr_name
, iidW
))
1026 if (!(entity
->u
.proxy
.iid
= xmlstrdupW(&attr_value
))) return FALSE
;
1028 if (xmlstr_cmp(&attr_name
, g_nameW
))
1030 if (!(entity
->u
.proxy
.name
= xmlstrdupW(&attr_value
))) return FALSE
;
1034 attr_nameU
= xmlstr2unicode(&attr_name
);
1035 attr_valueU
= xmlstr2unicode(&attr_value
);
1036 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1040 if (error
|| end
) return end
;
1041 return parse_expect_end_elem(xmlbuf
, comInterfaceProxyStubW
);
1044 static BOOL
parse_typelib_elem(xmlbuf_t
* xmlbuf
, struct dll_redirect
* dll
)
1046 xmlstr_t attr_name
, attr_value
;
1047 BOOL end
= FALSE
, error
;
1048 struct entity
* entity
;
1049 UNICODE_STRING attr_valueU
, attr_nameU
;
1051 if (!(entity
= add_entity(&dll
->entities
, ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION
)))
1054 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1056 if (xmlstr_cmp(&attr_name
, tlbidW
))
1058 if (!(entity
->u
.typelib
.tlbid
= xmlstrdupW(&attr_value
))) return FALSE
;
1060 if (xmlstr_cmp(&attr_name
, versionW
))
1062 if (!(entity
->u
.typelib
.version
= xmlstrdupW(&attr_value
))) return FALSE
;
1064 if (xmlstr_cmp(&attr_name
, helpdirW
))
1066 if (!(entity
->u
.typelib
.helpdir
= xmlstrdupW(&attr_value
))) return FALSE
;
1070 attr_nameU
= xmlstr2unicode(&attr_name
);
1071 attr_valueU
= xmlstr2unicode(&attr_value
);
1072 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1076 if (error
|| end
) return end
;
1077 return parse_expect_end_elem(xmlbuf
, typelibW
);
1080 static BOOL
parse_window_class_elem(xmlbuf_t
* xmlbuf
, struct dll_redirect
* dll
)
1082 xmlstr_t elem
, content
;
1083 BOOL end
= FALSE
, ret
= TRUE
;
1084 struct entity
* entity
;
1085 UNICODE_STRING elemU
;
1087 if (!(entity
= add_entity(&dll
->entities
, ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION
)))
1090 if (!parse_expect_no_attr(xmlbuf
, &end
)) return FALSE
;
1091 if (end
) return FALSE
;
1093 if (!parse_text_content(xmlbuf
, &content
)) return FALSE
;
1095 if (!(entity
->u
.class.name
= xmlstrdupW(&content
))) return FALSE
;
1097 while (ret
&& (ret
= next_xml_elem(xmlbuf
, &elem
)))
1099 if (xmlstr_cmp_end(&elem
, windowClassW
))
1101 ret
= parse_end_element(xmlbuf
);
1106 elemU
= xmlstr2unicode(&elem
);
1107 DPRINT1("unknown elem %wZ\n", &elemU
);
1108 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1115 static BOOL
parse_binding_redirect_elem(xmlbuf_t
* xmlbuf
)
1117 xmlstr_t attr_name
, attr_value
;
1118 UNICODE_STRING attr_valueU
, attr_nameU
;
1119 BOOL end
= FALSE
, error
;
1121 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1123 attr_nameU
= xmlstr2unicode(&attr_name
);
1124 attr_valueU
= xmlstr2unicode(&attr_value
);
1126 if (xmlstr_cmp(&attr_name
, oldVersionW
))
1128 DPRINT1("Not stored yet oldVersion=%wZ\n", &attr_valueU
);
1130 else if (xmlstr_cmp(&attr_name
, newVersionW
))
1132 DPRINT1("Not stored yet newVersion=%wZ\n", &attr_valueU
);
1136 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1140 if (error
|| end
) return end
;
1141 return parse_expect_end_elem(xmlbuf
, bindingRedirectW
);
1144 static BOOL
parse_description_elem(xmlbuf_t
* xmlbuf
)
1146 xmlstr_t elem
, content
;
1147 UNICODE_STRING elemU
;
1148 BOOL end
= FALSE
, ret
= TRUE
;
1150 if (!parse_expect_no_attr(xmlbuf
, &end
) || end
||
1151 !parse_text_content(xmlbuf
, &content
))
1154 elemU
= xmlstr2unicode(&content
);
1155 DPRINT("Got description %wZ\n", &elemU
);
1157 while (ret
&& (ret
= next_xml_elem(xmlbuf
, &elem
)))
1159 if (xmlstr_cmp_end(&elem
, descriptionW
))
1161 ret
= parse_end_element(xmlbuf
);
1166 elemU
= xmlstr2unicode(&elem
);
1167 DPRINT1("unknown elem %wZ\n", &elemU
);
1168 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1175 static BOOL
parse_com_interface_external_proxy_stub_elem(xmlbuf_t
* xmlbuf
,
1176 struct assembly
* assembly
)
1178 xmlstr_t attr_name
, attr_value
;
1179 UNICODE_STRING attr_nameU
, attr_valueU
;
1180 BOOL end
= FALSE
, error
;
1181 struct entity
* entity
;
1183 entity
= add_entity(&assembly
->entities
, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION
);
1184 if (!entity
) return FALSE
;
1186 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1188 if (xmlstr_cmp(&attr_name
, iidW
))
1190 if (!(entity
->u
.proxy
.iid
= xmlstrdupW(&attr_value
))) return FALSE
;
1192 if (xmlstr_cmp(&attr_name
, g_nameW
))
1194 if (!(entity
->u
.proxy
.name
= xmlstrdupW(&attr_value
))) return FALSE
;
1198 attr_nameU
= xmlstr2unicode(&attr_name
);
1199 attr_valueU
= xmlstr2unicode(&attr_value
);
1200 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1204 if (error
|| end
) return end
;
1205 return parse_expect_end_elem(xmlbuf
, comInterfaceExternalProxyStubW
);
1208 static BOOL
parse_clr_class_elem(xmlbuf_t
* xmlbuf
, struct assembly
* assembly
)
1210 xmlstr_t attr_name
, attr_value
;
1211 UNICODE_STRING attr_nameU
, attr_valueU
;
1212 BOOL end
= FALSE
, error
;
1213 struct entity
* entity
;
1215 entity
= add_entity(&assembly
->entities
, ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION
);
1216 if (!entity
) return FALSE
;
1218 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1220 if (xmlstr_cmp(&attr_name
, g_nameW
))
1222 if (!(entity
->u
.clrclass
.name
= xmlstrdupW(&attr_value
))) return FALSE
;
1224 else if (xmlstr_cmp(&attr_name
, clsidW
))
1226 if (!(entity
->u
.clrclass
.clsid
= xmlstrdupW(&attr_value
))) return FALSE
;
1230 attr_nameU
= xmlstr2unicode(&attr_name
);
1231 attr_valueU
= xmlstr2unicode(&attr_value
);
1232 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1236 if (error
|| end
) return end
;
1237 return parse_expect_end_elem(xmlbuf
, clrClassW
);
1240 static BOOL
parse_clr_surrogate_elem(xmlbuf_t
* xmlbuf
, struct assembly
* assembly
)
1242 xmlstr_t attr_name
, attr_value
;
1243 UNICODE_STRING attr_nameU
, attr_valueU
;
1244 BOOL end
= FALSE
, error
;
1245 struct entity
* entity
;
1247 entity
= add_entity(&assembly
->entities
, ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES
);
1248 if (!entity
) return FALSE
;
1250 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1252 if (xmlstr_cmp(&attr_name
, g_nameW
))
1254 if (!(entity
->u
.clrsurrogate
.name
= xmlstrdupW(&attr_value
))) return FALSE
;
1256 else if (xmlstr_cmp(&attr_name
, clsidW
))
1258 if (!(entity
->u
.clrsurrogate
.clsid
= xmlstrdupW(&attr_value
))) return FALSE
;
1262 attr_nameU
= xmlstr2unicode(&attr_name
);
1263 attr_valueU
= xmlstr2unicode(&attr_value
);
1264 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1268 if (error
|| end
) return end
;
1269 return parse_expect_end_elem(xmlbuf
, clrSurrogateW
);
1272 static BOOL
parse_dependent_assembly_elem(xmlbuf_t
* xmlbuf
, struct actctx_loader
* acl
, BOOL optional
)
1274 struct assembly_identity ai
;
1276 BOOL end
= FALSE
, ret
= TRUE
;
1278 if (!parse_expect_no_attr(xmlbuf
, &end
) || end
) return end
;
1280 memset(&ai
, 0, sizeof(ai
));
1281 ai
.optional
= optional
;
1283 if (!parse_expect_elem(xmlbuf
, assemblyIdentityW
) ||
1284 !parse_assembly_identity_elem(xmlbuf
, acl
->actctx
, &ai
))
1287 /* store the newly found identity for later loading */
1288 if (!add_dependent_assembly_id(acl
, &ai
)) return FALSE
;
1290 while (ret
&& (ret
= next_xml_elem(xmlbuf
, &elem
)))
1292 if (xmlstr_cmp_end(&elem
, dependentAssemblyW
))
1294 ret
= parse_end_element(xmlbuf
);
1297 else if (xmlstr_cmp(&elem
, bindingRedirectW
))
1299 ret
= parse_binding_redirect_elem(xmlbuf
);
1303 DPRINT1("unknown elem %S\n", elem
.ptr
);
1304 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1311 static BOOL
parse_dependency_elem(xmlbuf_t
* xmlbuf
, struct actctx_loader
* acl
)
1313 xmlstr_t attr_name
, attr_value
, elem
;
1314 UNICODE_STRING attr_nameU
, attr_valueU
;
1315 BOOL end
= FALSE
, ret
= TRUE
, error
, optional
= FALSE
;
1317 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1319 attr_nameU
= xmlstr2unicode(&attr_name
);
1320 attr_valueU
= xmlstr2unicode(&attr_value
);
1322 if (xmlstr_cmp(&attr_name
, optionalW
))
1324 static const WCHAR yesW
[] = {'y','e','s',0};
1325 optional
= xmlstr_cmpi( &attr_value
, yesW
);
1326 DPRINT1("optional=%wZ\n", &attr_valueU
);
1330 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1334 while (ret
&& (ret
= next_xml_elem(xmlbuf
, &elem
)))
1336 if (xmlstr_cmp_end(&elem
, dependencyW
))
1338 ret
= parse_end_element(xmlbuf
);
1341 else if (xmlstr_cmp(&elem
, dependentAssemblyW
))
1343 ret
= parse_dependent_assembly_elem(xmlbuf
, acl
, optional
);
1347 attr_nameU
= xmlstr2unicode(&elem
);
1348 DPRINT1("unknown element %wZ\n", &attr_nameU
);
1349 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1356 static BOOL
parse_noinherit_elem(xmlbuf_t
* xmlbuf
)
1360 if (!parse_expect_no_attr(xmlbuf
, &end
)) return FALSE
;
1361 return end
|| parse_expect_end_elem(xmlbuf
, noInheritW
);
1364 static BOOL
parse_noinheritable_elem(xmlbuf_t
* xmlbuf
)
1368 if (!parse_expect_no_attr(xmlbuf
, &end
)) return FALSE
;
1369 return end
|| parse_expect_end_elem(xmlbuf
, noInheritableW
);
1372 static BOOL
parse_file_elem(xmlbuf_t
* xmlbuf
, struct assembly
* assembly
)
1374 xmlstr_t attr_name
, attr_value
, elem
;
1375 UNICODE_STRING attr_nameU
, attr_valueU
;
1376 BOOL end
= FALSE
, error
, ret
= TRUE
;
1377 struct dll_redirect
* dll
;
1379 if (!(dll
= add_dll_redirect(assembly
))) return FALSE
;
1381 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1383 attr_nameU
= xmlstr2unicode(&attr_name
);
1384 attr_valueU
= xmlstr2unicode(&attr_value
);
1386 if (xmlstr_cmp(&attr_name
, g_nameW
))
1388 if (!(dll
->name
= xmlstrdupW(&attr_value
))) return FALSE
;
1389 DPRINT("name=%wZ\n", &attr_valueU
);
1391 else if (xmlstr_cmp(&attr_name
, hashW
))
1393 if (!(dll
->hash
= xmlstrdupW(&attr_value
))) return FALSE
;
1395 else if (xmlstr_cmp(&attr_name
, hashalgW
))
1397 static const WCHAR sha1W
[] = {'S','H','A','1',0};
1398 if (!xmlstr_cmpi(&attr_value
, sha1W
))
1399 DPRINT1("hashalg should be SHA1, got %wZ\n", &attr_valueU
);
1403 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1407 if (error
|| !dll
->name
) return FALSE
;
1408 if (end
) return TRUE
;
1410 while (ret
&& (ret
= next_xml_elem(xmlbuf
, &elem
)))
1412 if (xmlstr_cmp_end(&elem
, fileW
))
1414 ret
= parse_end_element(xmlbuf
);
1417 else if (xmlstr_cmp(&elem
, comClassW
))
1419 ret
= parse_com_class_elem(xmlbuf
, dll
);
1421 else if (xmlstr_cmp(&elem
, comInterfaceProxyStubW
))
1423 ret
= parse_cominterface_proxy_stub_elem(xmlbuf
, dll
);
1425 else if (xmlstr_cmp(&elem
, asmv2hashW
))
1427 DPRINT1("asmv2hash (undocumented) not supported\n");
1428 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1430 else if (xmlstr_cmp(&elem
, typelibW
))
1432 ret
= parse_typelib_elem(xmlbuf
, dll
);
1434 else if (xmlstr_cmp(&elem
, windowClassW
))
1436 ret
= parse_window_class_elem(xmlbuf
, dll
);
1440 attr_nameU
= xmlstr2unicode(&elem
);
1441 DPRINT1("unknown elem %wZ\n", &attr_nameU
);
1442 ret
= parse_unknown_elem( xmlbuf
, &elem
);
1449 static BOOL
parse_assembly_elem(xmlbuf_t
* xmlbuf
, struct actctx_loader
* acl
,
1450 struct assembly
* assembly
,
1451 struct assembly_identity
* expected_ai
)
1453 xmlstr_t attr_name
, attr_value
, elem
;
1454 UNICODE_STRING attr_nameU
, attr_valueU
;
1455 BOOL end
= FALSE
, error
, version
= FALSE
, xmlns
= FALSE
, ret
= TRUE
;
1457 while (next_xml_attr(xmlbuf
, &attr_name
, &attr_value
, &error
, &end
))
1459 attr_nameU
= xmlstr2unicode(&attr_name
);
1460 attr_valueU
= xmlstr2unicode(&attr_value
);
1462 if (xmlstr_cmp(&attr_name
, manifestVersionW
))
1464 static const WCHAR v10W
[] = {'1','.','0',0};
1465 if (!xmlstr_cmp(&attr_value
, v10W
))
1467 DPRINT1("wrong version %wZ\n", &attr_valueU
);
1472 else if (xmlstr_cmp(&attr_name
, xmlnsW
))
1474 if (!xmlstr_cmp(&attr_value
, manifestv1W
) && !xmlstr_cmp(&attr_value
, manifestv3W
))
1476 DPRINT1("wrong namespace %wZ\n", &attr_valueU
);
1483 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU
, &attr_valueU
);
1487 if (error
|| end
|| !xmlns
|| !version
) return FALSE
;
1488 if (!next_xml_elem(xmlbuf
, &elem
)) return FALSE
;
1490 if (assembly
->type
== APPLICATION_MANIFEST
&& xmlstr_cmp(&elem
, noInheritW
))
1492 if (!parse_noinherit_elem(xmlbuf
) || !next_xml_elem(xmlbuf
, &elem
))
1494 assembly
->no_inherit
= TRUE
;
1497 if (xmlstr_cmp(&elem
, noInheritableW
))
1499 if (!parse_noinheritable_elem(xmlbuf
) || !next_xml_elem(xmlbuf
, &elem
))
1502 else if ((assembly
->type
== ASSEMBLY_MANIFEST
|| assembly
->type
== ASSEMBLY_SHARED_MANIFEST
) &&
1503 assembly
->no_inherit
)
1508 if (xmlstr_cmp_end(&elem
, assemblyW
))
1510 ret
= parse_end_element(xmlbuf
);
1513 else if (xmlstr_cmp(&elem
, descriptionW
))
1515 ret
= parse_description_elem(xmlbuf
);
1517 else if (xmlstr_cmp(&elem
, comInterfaceExternalProxyStubW
))
1519 ret
= parse_com_interface_external_proxy_stub_elem(xmlbuf
, assembly
);
1521 else if (xmlstr_cmp(&elem
, dependencyW
))
1523 ret
= parse_dependency_elem(xmlbuf
, acl
);
1525 else if (xmlstr_cmp(&elem
, fileW
))
1527 ret
= parse_file_elem(xmlbuf
, assembly
);
1529 else if (xmlstr_cmp(&elem
, clrClassW
))
1531 ret
= parse_clr_class_elem(xmlbuf
, assembly
);
1533 else if (xmlstr_cmp(&elem
, clrSurrogateW
))
1535 ret
= parse_clr_surrogate_elem(xmlbuf
, assembly
);
1537 else if (xmlstr_cmp(&elem
, assemblyIdentityW
))
1539 if (!parse_assembly_identity_elem(xmlbuf
, acl
->actctx
, &assembly
->id
)) return FALSE
;
1543 /* FIXME: more tests */
1544 if (assembly
->type
== ASSEMBLY_MANIFEST
&&
1545 memcmp(&assembly
->id
.version
, &expected_ai
->version
, sizeof(assembly
->id
.version
)))
1547 DPRINT1("wrong version for assembly manifest: %u.%u.%u.%u / %u.%u.%u.%u\n",
1548 expected_ai
->version
.major
, expected_ai
->version
.minor
,
1549 expected_ai
->version
.build
, expected_ai
->version
.revision
,
1550 assembly
->id
.version
.major
, assembly
->id
.version
.minor
,
1551 assembly
->id
.version
.build
, assembly
->id
.version
.revision
);
1554 else if (assembly
->type
== ASSEMBLY_SHARED_MANIFEST
&&
1555 (assembly
->id
.version
.major
!= expected_ai
->version
.major
||
1556 assembly
->id
.version
.minor
!= expected_ai
->version
.minor
||
1557 assembly
->id
.version
.build
< expected_ai
->version
.build
||
1558 (assembly
->id
.version
.build
== expected_ai
->version
.build
&&
1559 assembly
->id
.version
.revision
< expected_ai
->version
.revision
)))
1561 DPRINT1("wrong version for shared assembly manifest\n");
1568 attr_nameU
= xmlstr2unicode(&elem
);
1569 DPRINT1("unknown element %wZ\n", &attr_nameU
);
1570 ret
= parse_unknown_elem(xmlbuf
, &elem
);
1572 if (ret
) ret
= next_xml_elem(xmlbuf
, &elem
);
1578 static NTSTATUS
parse_manifest_buffer( struct actctx_loader
* acl
, struct assembly
*assembly
,
1579 struct assembly_identity
* ai
, xmlbuf_t
*xmlbuf
)
1582 UNICODE_STRING elemU
;
1584 if (!next_xml_elem(xmlbuf
, &elem
)) return STATUS_SXS_CANT_GEN_ACTCTX
;
1586 if (xmlstr_cmp(&elem
, g_xmlW
) &&
1587 (!parse_xml_header(xmlbuf
) || !next_xml_elem(xmlbuf
, &elem
)))
1588 return STATUS_SXS_CANT_GEN_ACTCTX
;
1590 if (!xmlstr_cmp(&elem
, assemblyW
))
1592 elemU
= xmlstr2unicode(&elem
);
1593 DPRINT1("root element is %wZ, not <assembly>\n", &elemU
);
1594 return STATUS_SXS_CANT_GEN_ACTCTX
;
1597 if (!parse_assembly_elem(xmlbuf
, acl
, assembly
, ai
))
1599 DPRINT1("failed to parse manifest %S\n", assembly
->manifest
.info
);
1600 return STATUS_SXS_CANT_GEN_ACTCTX
;
1603 if (next_xml_elem(xmlbuf
, &elem
))
1605 elemU
= xmlstr2unicode(&elem
);
1606 DPRINT1("unexpected element %wZ\n", &elemU
);
1607 return STATUS_SXS_CANT_GEN_ACTCTX
;
1610 if (xmlbuf
->ptr
!= xmlbuf
->end
)
1612 DPRINT1("parse error\n");
1613 return STATUS_SXS_CANT_GEN_ACTCTX
;
1615 return STATUS_SUCCESS
;
1618 static NTSTATUS
parse_manifest( struct actctx_loader
* acl
, struct assembly_identity
* ai
,
1619 LPCWSTR filename
, LPCWSTR directory
, BOOL shared
,
1620 const void *buffer
, SIZE_T size
)
1624 struct assembly
*assembly
;
1627 DPRINT( "parsing manifest loaded from %S base dir %S\n", filename
, directory
);
1629 if (!(assembly
= add_assembly(acl
->actctx
, shared
? ASSEMBLY_SHARED_MANIFEST
: ASSEMBLY_MANIFEST
)))
1630 return STATUS_SXS_CANT_GEN_ACTCTX
;
1632 if (directory
&& !(assembly
->directory
= strdupW(directory
)))
1633 return STATUS_NO_MEMORY
;
1635 if (filename
) assembly
->manifest
.info
= strdupW( filename
+ 4 /* skip \??\ prefix */ );
1636 assembly
->manifest
.type
= assembly
->manifest
.info
? ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE
1637 : ACTIVATION_CONTEXT_PATH_TYPE_NONE
;
1639 unicode_tests
= IS_TEXT_UNICODE_SIGNATURE
| IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1640 if (RtlIsTextUnicode((PVOID
)buffer
, (ULONG
)size
, &unicode_tests
))
1642 xmlbuf
.ptr
= buffer
;
1643 xmlbuf
.end
= xmlbuf
.ptr
+ size
/ sizeof(WCHAR
);
1644 status
= parse_manifest_buffer( acl
, assembly
, ai
, &xmlbuf
);
1646 else if (unicode_tests
& IS_TEXT_UNICODE_REVERSE_SIGNATURE
)
1648 const WCHAR
*buf
= buffer
;
1652 if (!(new_buff
= RtlAllocateHeap( RtlGetProcessHeap(), 0, size
)))
1653 return STATUS_NO_MEMORY
;
1654 for (i
= 0; i
< size
/ sizeof(WCHAR
); i
++)
1655 new_buff
[i
] = RtlUshortByteSwap( buf
[i
] );
1656 xmlbuf
.ptr
= new_buff
;
1657 xmlbuf
.end
= xmlbuf
.ptr
+ size
/ sizeof(WCHAR
);
1658 status
= parse_manifest_buffer( acl
, assembly
, ai
, &xmlbuf
);
1659 RtlFreeHeap( RtlGetProcessHeap(), 0, new_buff
);
1663 /* TODO: this doesn't handle arbitrary encodings */
1667 status
= RtlMultiByteToUnicodeSize(&sizeU
, buffer
, size
);
1668 if (!NT_SUCCESS(status
))
1670 DPRINT1("RtlMultiByteToUnicodeSize failed with %lx\n", status
);
1671 return STATUS_SXS_CANT_GEN_ACTCTX
;
1674 new_buff
= RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeU
);
1676 return STATUS_NO_MEMORY
;
1678 status
= RtlMultiByteToUnicodeN(new_buff
, sizeU
, &sizeU
, buffer
, size
);
1679 if (!NT_SUCCESS(status
))
1681 DPRINT1("RtlMultiByteToUnicodeN failed with %lx\n", status
);
1682 return STATUS_SXS_CANT_GEN_ACTCTX
;
1685 xmlbuf
.ptr
= new_buff
;
1686 xmlbuf
.end
= xmlbuf
.ptr
+ sizeU
/ sizeof(WCHAR
);
1687 status
= parse_manifest_buffer(acl
, assembly
, ai
, &xmlbuf
);
1688 RtlFreeHeap(RtlGetProcessHeap(), 0, new_buff
);
1693 static NTSTATUS
open_nt_file( HANDLE
*handle
, UNICODE_STRING
*name
)
1695 OBJECT_ATTRIBUTES attr
;
1698 attr
.Length
= sizeof(attr
);
1699 attr
.RootDirectory
= 0;
1700 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1701 attr
.ObjectName
= name
;
1702 attr
.SecurityDescriptor
= NULL
;
1703 attr
.SecurityQualityOfService
= NULL
;
1704 return NtOpenFile(handle
,
1705 GENERIC_READ
| SYNCHRONIZE
,
1708 FILE_SYNCHRONOUS_IO_ALERT
);
1711 static NTSTATUS
get_module_filename( HMODULE module
, UNICODE_STRING
*str
, USHORT extra_len
)
1715 LDR_DATA_TABLE_ENTRY
*pldr
;
1717 LdrLockLoaderLock(0, NULL
, &magic
);
1718 status
= LdrFindEntryForAddress( module
, &pldr
);
1719 if (status
== STATUS_SUCCESS
)
1721 if ((str
->Buffer
= RtlAllocateHeap( RtlGetProcessHeap(), 0,
1722 pldr
->FullDllName
.Length
+ extra_len
+ sizeof(WCHAR
) )))
1724 memcpy( str
->Buffer
, pldr
->FullDllName
.Buffer
, pldr
->FullDllName
.Length
+ sizeof(WCHAR
) );
1725 str
->Length
= pldr
->FullDllName
.Length
;
1726 str
->MaximumLength
= pldr
->FullDllName
.Length
+ extra_len
+ sizeof(WCHAR
);
1728 else status
= STATUS_NO_MEMORY
;
1730 LdrUnlockLoaderLock(0, magic
);
1734 static NTSTATUS
get_manifest_in_module( struct actctx_loader
* acl
, struct assembly_identity
* ai
,
1735 LPCWSTR filename
, LPCWSTR directory
, BOOL shared
,
1736 HANDLE hModule
, LPCWSTR resname
, ULONG lang
)
1739 UNICODE_STRING nameW
;
1740 LDR_RESOURCE_INFO info
;
1741 IMAGE_RESOURCE_DATA_ENTRY
* entry
= NULL
;
1744 //DPRINT( "looking for res %s in module %p %s\n", resname,
1745 // hModule, filename );
1748 if (TRACE_ON(actctx
))
1750 if (!filename
&& !get_module_filename( hModule
, &nameW
, 0 ))
1752 DPRINT( "looking for res %s in module %p %s\n", debugstr_w(resname
),
1753 hModule
, debugstr_w(nameW
.Buffer
) );
1754 RtlFreeUnicodeString( &nameW
);
1756 else DPRINT( "looking for res %s in module %p %s\n", debugstr_w(resname
),
1757 hModule
, debugstr_w(filename
) );
1761 if (!resname
) return STATUS_INVALID_PARAMETER
;
1763 info
.Type
= (ULONG_PTR
)RT_MANIFEST
;
1764 info
.Language
= lang
;
1765 if (!((ULONG_PTR
)resname
>> 16))
1767 info
.Name
= (ULONG_PTR
)resname
;
1768 status
= LdrFindResource_U(hModule
, &info
, 3, &entry
);
1770 else if (resname
[0] == '#')
1773 RtlInitUnicodeString(&nameW
, resname
+ 1);
1774 if (RtlUnicodeStringToInteger(&nameW
, 10, &value
) != STATUS_SUCCESS
|| HIWORD(value
))
1775 return STATUS_INVALID_PARAMETER
;
1777 status
= LdrFindResource_U(hModule
, &info
, 3, &entry
);
1781 RtlCreateUnicodeString(&nameW
, resname
);
1782 RtlUpcaseUnicodeString(&nameW
, &nameW
, FALSE
);
1783 info
.Name
= (ULONG_PTR
)nameW
.Buffer
;
1784 status
= LdrFindResource_U(hModule
, &info
, 3, &entry
);
1785 RtlFreeUnicodeString(&nameW
);
1787 if (status
== STATUS_SUCCESS
) status
= LdrAccessResource(hModule
, entry
, &ptr
, NULL
);
1789 if (status
== STATUS_SUCCESS
)
1790 status
= parse_manifest(acl
, ai
, filename
, directory
, shared
, ptr
, entry
->Size
);
1795 static NTSTATUS
get_manifest_in_pe_file( struct actctx_loader
* acl
, struct assembly_identity
* ai
,
1796 LPCWSTR filename
, LPCWSTR directory
, BOOL shared
,
1797 HANDLE file
, LPCWSTR resname
, ULONG lang
)
1800 OBJECT_ATTRIBUTES attr
;
1802 LARGE_INTEGER offset
;
1807 DPRINT( "looking for res %S in %S\n", resname
, filename
);
1809 attr
.Length
= sizeof(attr
);
1810 attr
.RootDirectory
= 0;
1811 attr
.ObjectName
= NULL
;
1812 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
;
1813 attr
.SecurityDescriptor
= NULL
;
1814 attr
.SecurityQualityOfService
= NULL
;
1817 status
= NtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1818 &attr
, &size
, PAGE_READONLY
, SEC_COMMIT
, file
);
1819 if (status
!= STATUS_SUCCESS
) return status
;
1821 offset
.QuadPart
= 0;
1824 status
= NtMapViewOfSection( mapping
, NtCurrentProcess(), &base
, 0, 0, &offset
,
1825 &count
, ViewShare
, 0, PAGE_READONLY
);
1827 if (status
!= STATUS_SUCCESS
) return status
;
1829 if (RtlImageNtHeader(base
)) /* we got a PE file */
1831 HANDLE module
= (HMODULE
)((ULONG_PTR
)base
| 1); /* make it a LOAD_LIBRARY_AS_DATAFILE handle */
1832 status
= get_manifest_in_module( acl
, ai
, filename
, directory
, shared
, module
, resname
, lang
);
1834 else status
= STATUS_INVALID_IMAGE_FORMAT
;
1836 NtUnmapViewOfSection( NtCurrentProcess(), base
);
1840 static NTSTATUS
get_manifest_in_manifest_file( struct actctx_loader
* acl
, struct assembly_identity
* ai
,
1841 LPCWSTR filename
, LPCWSTR directory
, BOOL shared
, HANDLE file
)
1843 FILE_STANDARD_INFORMATION info
;
1846 OBJECT_ATTRIBUTES attr
;
1848 LARGE_INTEGER offset
;
1853 DPRINT( "loading manifest file %S\n", filename
);
1855 attr
.Length
= sizeof(attr
);
1856 attr
.RootDirectory
= 0;
1857 attr
.ObjectName
= NULL
;
1858 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
;
1859 attr
.SecurityDescriptor
= NULL
;
1860 attr
.SecurityQualityOfService
= NULL
;
1863 status
= NtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1864 &attr
, &size
, PAGE_READONLY
, SEC_COMMIT
, file
);
1866 if (status
!= STATUS_SUCCESS
) return status
;
1868 offset
.QuadPart
= 0;
1871 status
= NtMapViewOfSection( mapping
, NtCurrentProcess(), &base
, 0, 0, &offset
,
1872 &count
, ViewShare
, 0, PAGE_READONLY
);
1874 if (status
!= STATUS_SUCCESS
) return status
;
1876 /* Fixme: WINE uses FileEndOfFileInformation with NtQueryInformationFile. */
1877 status
= NtQueryInformationFile( file
, &io
, &info
, sizeof(info
), FileStandardInformation
);
1879 if (status
== STATUS_SUCCESS
)
1880 status
= parse_manifest(acl
, ai
, filename
, directory
, shared
, base
, (SIZE_T
)info
.EndOfFile
.QuadPart
);
1882 NtUnmapViewOfSection( NtCurrentProcess(), base
);
1887 /* try to load the .manifest file associated to the file */
1888 static NTSTATUS
get_manifest_in_associated_manifest( struct actctx_loader
* acl
, struct assembly_identity
* ai
,
1889 LPCWSTR filename
, LPCWSTR directory
, HMODULE module
, LPCWSTR resname
)
1891 static const WCHAR fmtW
[] = { '.','%','l','u',0 };
1894 UNICODE_STRING nameW
;
1896 ULONG_PTR resid
= (ULONG_PTR
)CREATEPROCESS_MANIFEST_RESOURCE_ID
;
1898 if (!((ULONG_PTR
)resname
>> 16)) resid
= (ULONG_PTR
)resname
& 0xffff;
1900 DPRINT( "looking for manifest associated with %S id %lu\n", filename
, resid
);
1902 if (module
) /* use the module filename */
1904 UNICODE_STRING name
;
1906 if (!(status
= get_module_filename( module
, &name
, sizeof(dotManifestW
) + 10*sizeof(WCHAR
) )))
1908 if (resid
!= 1) sprintfW( name
.Buffer
+ strlenW(name
.Buffer
), fmtW
, resid
);
1909 strcatW( name
.Buffer
, dotManifestW
);
1910 if (!RtlDosPathNameToNtPathName_U( name
.Buffer
, &nameW
, NULL
, NULL
))
1911 status
= STATUS_RESOURCE_DATA_NOT_FOUND
;
1912 RtlFreeUnicodeString( &name
);
1914 if (status
) return status
;
1918 if (!(buffer
= RtlAllocateHeap( RtlGetProcessHeap(), 0,
1919 (strlenW(filename
) + 10) * sizeof(WCHAR
) + sizeof(dotManifestW
) )))
1920 return STATUS_NO_MEMORY
;
1921 strcpyW( buffer
, filename
);
1922 if (resid
!= 1) sprintfW( buffer
+ strlenW(buffer
), fmtW
, resid
);
1923 strcatW( buffer
, dotManifestW
);
1924 RtlInitUnicodeString( &nameW
, buffer
);
1927 if (!open_nt_file( &file
, &nameW
))
1929 status
= get_manifest_in_manifest_file( acl
, ai
, nameW
.Buffer
, directory
, FALSE
, file
);
1932 else status
= STATUS_RESOURCE_DATA_NOT_FOUND
;
1933 RtlFreeUnicodeString( &nameW
);
1937 static WCHAR
*lookup_manifest_file( HANDLE dir
, struct assembly_identity
*ai
)
1939 static const WCHAR lookup_fmtW
[] =
1940 {'%','s','_','%','s','_','%','s','_','%','u','.','%','u','.','*','.','*','_',
1942 '.','m','a','n','i','f','e','s','t',0};
1944 WCHAR
*lookup
, *ret
= NULL
;
1945 UNICODE_STRING lookup_us
;
1947 unsigned int data_pos
= 0, data_len
;
1950 if (!(lookup
= RtlAllocateHeap( RtlGetProcessHeap(), 0,
1951 (strlenW(ai
->arch
) + strlenW(ai
->name
)
1952 + strlenW(ai
->public_key
) + 20) * sizeof(WCHAR
)
1953 + sizeof(lookup_fmtW
) )))
1956 sprintfW( lookup
, lookup_fmtW
, ai
->arch
, ai
->name
, ai
->public_key
, ai
->version
.major
, ai
->version
.minor
);
1957 RtlInitUnicodeString( &lookup_us
, lookup
);
1959 NtQueryDirectoryFile( dir
, 0, NULL
, NULL
, &io
, buffer
, sizeof(buffer
),
1960 FileBothDirectoryInformation
, FALSE
, &lookup_us
, TRUE
);
1961 if (io
.Status
== STATUS_SUCCESS
)
1963 FILE_BOTH_DIR_INFORMATION
*dir_info
;
1965 ULONG build
, revision
;
1967 data_len
= (ULONG
)io
.Information
;
1971 if (data_pos
>= data_len
)
1973 NtQueryDirectoryFile( dir
, 0, NULL
, NULL
, &io
, buffer
, sizeof(buffer
),
1974 FileBothDirectoryInformation
, FALSE
, &lookup_us
, FALSE
);
1975 if (io
.Status
!= STATUS_SUCCESS
) break;
1976 data_len
= (ULONG
)io
.Information
;
1979 dir_info
= (FILE_BOTH_DIR_INFORMATION
*)(buffer
+ data_pos
);
1981 if (dir_info
->NextEntryOffset
) data_pos
+= dir_info
->NextEntryOffset
;
1982 else data_pos
= data_len
;
1984 tmp
= (WCHAR
*)dir_info
->FileName
+ (strchrW(lookup
, '*') - lookup
);
1986 if (build
< ai
->version
.build
) continue;
1987 tmp
= strchrW(tmp
, '.') + 1;
1988 revision
= atoiW(tmp
);
1989 if (build
== ai
->version
.build
&& revision
< ai
->version
.revision
)
1991 ai
->version
.build
= (USHORT
)build
;
1992 ai
->version
.revision
= (USHORT
)revision
;
1994 if ((ret
= RtlAllocateHeap( RtlGetProcessHeap(), 0, dir_info
->FileNameLength
* sizeof(WCHAR
) )))
1996 memcpy( ret
, dir_info
->FileName
, dir_info
->FileNameLength
);
1997 ret
[dir_info
->FileNameLength
/sizeof(WCHAR
)] = 0;
2002 else DPRINT1("no matching file for %S\n", lookup
);
2003 RtlFreeHeap( RtlGetProcessHeap(), 0, lookup
);
2007 static NTSTATUS
lookup_winsxs(struct actctx_loader
* acl
, struct assembly_identity
* ai
)
2009 struct assembly_identity sxs_ai
;
2010 UNICODE_STRING path_us
;
2011 OBJECT_ATTRIBUTES attr
;
2013 WCHAR
*path
, *file
= NULL
;
2016 static const WCHAR manifest_dirW
[] =
2017 {'\\','w','i','n','s','x','s','\\','m','a','n','i','f','e','s','t','s',0};
2019 if (!ai
->arch
|| !ai
->name
|| !ai
->public_key
) return STATUS_NO_SUCH_FILE
;
2021 if (!(path
= RtlAllocateHeap( RtlGetProcessHeap(), 0,
2022 ((strlenW(SharedUserData
->NtSystemRoot
) + 1) *sizeof(WCHAR
)) + sizeof(manifest_dirW
) )))
2023 return STATUS_NO_MEMORY
;
2025 memcpy( path
, SharedUserData
->NtSystemRoot
, strlenW(SharedUserData
->NtSystemRoot
) * sizeof(WCHAR
) );
2026 memcpy( path
+ strlenW(SharedUserData
->NtSystemRoot
), manifest_dirW
, sizeof(manifest_dirW
) );
2028 if (!RtlDosPathNameToNtPathName_U( path
, &path_us
, NULL
, NULL
))
2030 RtlFreeHeap( RtlGetProcessHeap(), 0, path
);
2031 return STATUS_NO_SUCH_FILE
;
2033 RtlFreeHeap( RtlGetProcessHeap(), 0, path
);
2035 attr
.Length
= sizeof(attr
);
2036 attr
.RootDirectory
= 0;
2037 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2038 attr
.ObjectName
= &path_us
;
2039 attr
.SecurityDescriptor
= NULL
;
2040 attr
.SecurityQualityOfService
= NULL
;
2042 if (!NtOpenFile(&handle
,
2043 GENERIC_READ
| SYNCHRONIZE
,
2045 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
2046 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
))
2049 file
= lookup_manifest_file( handle
, &sxs_ai
);
2054 RtlFreeUnicodeString( &path_us
);
2055 return STATUS_NO_SUCH_FILE
;
2058 /* append file name to directory path */
2059 if (!(path
= RtlReAllocateHeap( RtlGetProcessHeap(), 0, path_us
.Buffer
,
2060 path_us
.Length
+ (strlenW(file
) + 2) * sizeof(WCHAR
) )))
2062 RtlFreeHeap( RtlGetProcessHeap(), 0, file
);
2063 RtlFreeUnicodeString( &path_us
);
2064 return STATUS_NO_MEMORY
;
2067 path
[path_us
.Length
/sizeof(WCHAR
)] = '\\';
2068 strcpyW( path
+ path_us
.Length
/sizeof(WCHAR
) + 1, file
);
2069 RtlInitUnicodeString( &path_us
, path
);
2070 *strrchrW(file
, '.') = 0; /* remove .manifest extension */
2072 if (!open_nt_file( &handle
, &path_us
))
2074 io
.Status
= get_manifest_in_manifest_file(acl
, &sxs_ai
, path_us
.Buffer
, file
, TRUE
, handle
);
2077 else io
.Status
= STATUS_NO_SUCH_FILE
;
2079 RtlFreeHeap( RtlGetProcessHeap(), 0, file
);
2080 RtlFreeUnicodeString( &path_us
);
2084 static NTSTATUS
lookup_assembly(struct actctx_loader
* acl
,
2085 struct assembly_identity
* ai
)
2087 static const WCHAR dotDllW
[] = {'.','d','l','l',0};
2089 WCHAR
*buffer
, *p
, *directory
;
2091 UNICODE_STRING nameW
;
2094 DPRINT( "looking for name=%S version=%u.%u.%u.%u arch=%S\n",
2095 ai
->name
, ai
->version
.major
, ai
->version
.minor
, ai
->version
.build
, ai
->version
.revision
, ai
->arch
);
2097 if ((status
= lookup_winsxs(acl
, ai
)) != STATUS_NO_SUCH_FILE
) return status
;
2099 /* FIXME: add support for language specific lookup */
2101 nameW
.Buffer
= NULL
;
2102 if (!(buffer
= RtlAllocateHeap( RtlGetProcessHeap(), 0,
2103 (strlenW(acl
->actctx
->appdir
.info
) + 2 * strlenW(ai
->name
) + 2) * sizeof(WCHAR
) + sizeof(dotManifestW
) )))
2104 return STATUS_NO_MEMORY
;
2106 if (!(directory
= build_assembly_dir( ai
)))
2108 RtlFreeHeap( RtlGetProcessHeap(), 0, buffer
);
2109 return STATUS_NO_MEMORY
;
2112 /* lookup in appdir\name.dll
2113 * appdir\name.manifest
2114 * appdir\name\name.dll
2115 * appdir\name\name.manifest
2117 strcpyW( buffer
, acl
->actctx
->appdir
.info
);
2118 p
= buffer
+ strlenW(buffer
);
2119 for (i
= 0; i
< 2; i
++)
2122 strcpyW( p
, ai
->name
);
2125 strcpyW( p
, dotDllW
);
2126 if (RtlDosPathNameToNtPathName_U( buffer
, &nameW
, NULL
, NULL
))
2128 status
= open_nt_file( &file
, &nameW
);
2131 status
= get_manifest_in_pe_file( acl
, ai
, nameW
.Buffer
, directory
, FALSE
, file
,
2132 (LPCWSTR
)CREATEPROCESS_MANIFEST_RESOURCE_ID
, 0 );
2136 RtlFreeUnicodeString( &nameW
);
2139 strcpyW( p
, dotManifestW
);
2140 if (RtlDosPathNameToNtPathName_U( buffer
, &nameW
, NULL
, NULL
))
2142 status
= open_nt_file( &file
, &nameW
);
2145 status
= get_manifest_in_manifest_file( acl
, ai
, nameW
.Buffer
, directory
, FALSE
, file
);
2149 RtlFreeUnicodeString( &nameW
);
2151 status
= STATUS_SXS_ASSEMBLY_NOT_FOUND
;
2153 RtlFreeUnicodeString( &nameW
);
2154 RtlFreeHeap( RtlGetProcessHeap(), 0, directory
);
2155 RtlFreeHeap( RtlGetProcessHeap(), 0, buffer
);
2159 static NTSTATUS
parse_depend_manifests(struct actctx_loader
* acl
)
2161 NTSTATUS status
= STATUS_SUCCESS
;
2164 for (i
= 0; i
< acl
->num_dependencies
; i
++)
2166 if (lookup_assembly(acl
, &acl
->dependencies
[i
]) != STATUS_SUCCESS
)
2168 if (!acl
->dependencies
[i
].optional
)
2170 DPRINT1( "Could not find dependent assembly %S\n", acl
->dependencies
[i
].name
);
2171 status
= STATUS_SXS_CANT_GEN_ACTCTX
;
2176 /* FIXME should now iterate through all refs */
2180 /* find the appropriate activation context for RtlQueryInformationActivationContext */
2181 static NTSTATUS
find_query_actctx( HANDLE
*handle
, DWORD flags
, ULONG
class )
2183 NTSTATUS status
= STATUS_SUCCESS
;
2185 if (flags
& RTL_QUERY_ACTIVATION_CONTEXT_FLAG_USE_ACTIVE_ACTIVATION_CONTEXT
)
2187 if (*handle
) return STATUS_INVALID_PARAMETER
;
2189 if (NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
)
2190 *handle
= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
->ActivationContext
;
2192 else if (flags
& (RTL_QUERY_ACTIVATION_CONTEXT_FLAG_IS_ADDRESS
| RTL_QUERY_ACTIVATION_CONTEXT_FLAG_IS_HMODULE
))
2195 LDR_DATA_TABLE_ENTRY
*pldr
;
2197 if (!*handle
) return STATUS_INVALID_PARAMETER
;
2199 LdrLockLoaderLock( 0, NULL
, &magic
);
2200 if (!LdrFindEntryForAddress( *handle
, &pldr
))
2202 if ((flags
& RTL_QUERY_ACTIVATION_CONTEXT_FLAG_IS_HMODULE
) && *handle
!= pldr
->DllBase
)
2203 status
= STATUS_DLL_NOT_FOUND
;
2205 *handle
= pldr
->EntryPointActivationContext
;
2207 else status
= STATUS_DLL_NOT_FOUND
;
2208 LdrUnlockLoaderLock( 0, magic
);
2210 else if (!*handle
&& (class != ActivationContextBasicInformation
))
2211 *handle
= process_actctx
;
2216 static NTSTATUS
fill_keyed_data(PACTCTX_SECTION_KEYED_DATA data
, PVOID v1
, PVOID v2
, unsigned int i
)
2218 data
->ulDataFormatVersion
= 1;
2220 data
->ulLength
= 20; /* FIXME */
2221 data
->lpSectionGlobalData
= NULL
; /* FIXME */
2222 data
->ulSectionGlobalDataLength
= 0; /* FIXME */
2223 data
->lpSectionBase
= v2
;
2224 data
->ulSectionTotalLength
= 0; /* FIXME */
2225 data
->hActCtx
= NULL
;
2226 if (data
->cbSize
>= offsetof(ACTCTX_SECTION_KEYED_DATA
, ulAssemblyRosterIndex
) + sizeof(ULONG
))
2227 data
->ulAssemblyRosterIndex
= i
+ 1;
2229 return STATUS_SUCCESS
;
2232 static NTSTATUS
find_dll_redirection(ACTIVATION_CONTEXT
* actctx
, const UNICODE_STRING
*section_name
,
2233 PACTCTX_SECTION_KEYED_DATA data
)
2235 unsigned int i
, j
, snlen
= section_name
->Length
/ sizeof(WCHAR
);
2237 for (i
= 0; i
< actctx
->num_assemblies
; i
++)
2239 struct assembly
*assembly
= &actctx
->assemblies
[i
];
2240 for (j
= 0; j
< assembly
->num_dlls
; j
++)
2242 struct dll_redirect
*dll
= &assembly
->dlls
[j
];
2243 if (!strncmpiW(section_name
->Buffer
, dll
->name
, snlen
) && !dll
->name
[snlen
])
2244 return fill_keyed_data(data
, dll
, assembly
, i
);
2247 return STATUS_SXS_KEY_NOT_FOUND
;
2250 static NTSTATUS
find_window_class(ACTIVATION_CONTEXT
* actctx
, const UNICODE_STRING
*section_name
,
2251 PACTCTX_SECTION_KEYED_DATA data
)
2253 unsigned int i
, j
, k
, snlen
= section_name
->Length
/ sizeof(WCHAR
);
2255 for (i
= 0; i
< actctx
->num_assemblies
; i
++)
2257 struct assembly
*assembly
= &actctx
->assemblies
[i
];
2258 for (j
= 0; j
< assembly
->num_dlls
; j
++)
2260 struct dll_redirect
*dll
= &assembly
->dlls
[j
];
2261 for (k
= 0; k
< dll
->entities
.num
; k
++)
2263 struct entity
*entity
= &dll
->entities
.base
[k
];
2264 if (entity
->kind
== ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION
)
2266 if (!strncmpiW(section_name
->Buffer
, entity
->u
.class.name
, snlen
) && !entity
->u
.class.name
[snlen
])
2267 return fill_keyed_data(data
, entity
, dll
, i
);
2272 return STATUS_SXS_KEY_NOT_FOUND
;
2275 static NTSTATUS
find_string(ACTIVATION_CONTEXT
* actctx
, ULONG section_kind
,
2276 const UNICODE_STRING
*section_name
,
2277 DWORD flags
, PACTCTX_SECTION_KEYED_DATA data
)
2281 switch (section_kind
)
2283 case ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION
:
2284 status
= find_dll_redirection(actctx
, section_name
, data
);
2286 case ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION
:
2287 status
= find_window_class(actctx
, section_name
, data
);
2289 case ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION
:
2290 case ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION
:
2291 case ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION
:
2292 case ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION
:
2293 case ACTIVATION_CONTEXT_SECTION_GLOBAL_OBJECT_RENAME_TABLE
:
2294 case ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES
:
2295 DPRINT1("Unsupported yet section_kind %x\n", section_kind
);
2296 return STATUS_SXS_SECTION_NOT_FOUND
;
2298 DPRINT1("Unknown section_kind %x\n", section_kind
);
2299 return STATUS_SXS_SECTION_NOT_FOUND
;
2302 if (status
!= STATUS_SUCCESS
) return status
;
2304 if (flags
& FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX
)
2306 actctx_addref(actctx
);
2307 data
->hActCtx
= actctx
;
2309 return STATUS_SUCCESS
;
2312 /* initialize the activation context for the current process */
2313 void actctx_init(void)
2318 ctx
.cbSize
= sizeof(ctx
);
2319 ctx
.lpSource
= NULL
;
2320 ctx
.dwFlags
= ACTCTX_FLAG_RESOURCE_NAME_VALID
| ACTCTX_FLAG_HMODULE_VALID
;
2321 ctx
.hModule
= NtCurrentTeb()->ProcessEnvironmentBlock
->ImageBaseAddress
;
2322 ctx
.lpResourceName
= (LPCWSTR
)CREATEPROCESS_MANIFEST_RESOURCE_ID
;
2324 if (NT_SUCCESS(RtlCreateActivationContext(0, (PVOID
)&ctx
, 0, NULL
, NULL
, &handle
)))
2326 process_actctx
= check_actctx(handle
);
2330 /* FUNCTIONS ***************************************************************/
2334 RtlCreateActivationContext(IN ULONG Flags
,
2335 IN PACTIVATION_CONTEXT_DATA ActivationContextData
,
2336 IN ULONG ExtraBytes
,
2337 IN PVOID NotificationRoutine
,
2338 IN PVOID NotificationContext
,
2339 OUT PACTIVATION_CONTEXT
*ActCtx
)
2341 const ACTCTXW
*pActCtx
= (PVOID
)ActivationContextData
;
2342 const WCHAR
*directory
= NULL
;
2343 PACTIVATION_CONTEXT_WRAPPED ActualActCtx
;
2344 ACTIVATION_CONTEXT
*actctx
;
2345 UNICODE_STRING nameW
;
2347 NTSTATUS status
= STATUS_NO_MEMORY
;
2349 struct actctx_loader acl
;
2351 DPRINT("%p %08x\n", pActCtx
, pActCtx
? pActCtx
->dwFlags
: 0);
2353 if (!pActCtx
|| pActCtx
->cbSize
< sizeof(*pActCtx
) ||
2354 (pActCtx
->dwFlags
& ~ACTCTX_FLAGS_ALL
))
2355 return STATUS_INVALID_PARAMETER
;
2358 if (!(ActualActCtx
= RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ActualActCtx
))))
2359 return STATUS_NO_MEMORY
;
2361 ActualActCtx
->MagicMarker
= ACTCTX_MAGIC_MARKER
;
2363 actctx
= &ActualActCtx
->ActivationContext
;
2364 actctx
->RefCount
= 1;
2365 actctx
->config
.type
= ACTIVATION_CONTEXT_PATH_TYPE_NONE
;
2366 actctx
->config
.info
= NULL
;
2367 actctx
->appdir
.type
= ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE
;
2368 if (pActCtx
->dwFlags
& ACTCTX_FLAG_APPLICATION_NAME_VALID
)
2370 if (!(actctx
->appdir
.info
= strdupW( pActCtx
->lpApplicationName
))) goto error
;
2378 if (pActCtx
->dwFlags
& ACTCTX_FLAG_HMODULE_VALID
) module
= pActCtx
->hModule
;
2379 else module
= NtCurrentTeb()->ProcessEnvironmentBlock
->ImageBaseAddress
;
2381 status
= get_module_filename(module
, &dir
, 0);
2382 if (!NT_SUCCESS(status
)) goto error
;
2383 if ((p
= strrchrW( dir
.Buffer
, '\\' ))) p
[1] = 0;
2384 actctx
->appdir
.info
= dir
.Buffer
;
2387 nameW
.Buffer
= NULL
;
2388 if (pActCtx
->lpSource
)
2390 if (!RtlDosPathNameToNtPathName_U(pActCtx
->lpSource
, &nameW
, NULL
, NULL
))
2392 status
= STATUS_NO_SUCH_FILE
;
2395 status
= open_nt_file( &file
, &nameW
);
2396 if (!NT_SUCCESS(status
))
2398 RtlFreeUnicodeString( &nameW
);
2403 acl
.actctx
= actctx
;
2404 acl
.dependencies
= NULL
;
2405 acl
.num_dependencies
= 0;
2406 acl
.allocated_dependencies
= 0;
2408 if (pActCtx
->dwFlags
& ACTCTX_FLAG_LANGID_VALID
) lang
= pActCtx
->wLangId
;
2409 if (pActCtx
->dwFlags
& ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID
) directory
= pActCtx
->lpAssemblyDirectory
;
2411 if (pActCtx
->dwFlags
& ACTCTX_FLAG_RESOURCE_NAME_VALID
)
2413 /* if we have a resource it's a PE file */
2414 if (pActCtx
->dwFlags
& ACTCTX_FLAG_HMODULE_VALID
)
2416 status
= get_manifest_in_module( &acl
, NULL
, NULL
, directory
, FALSE
, pActCtx
->hModule
,
2417 pActCtx
->lpResourceName
, lang
);
2418 if (status
&& status
!= STATUS_SXS_CANT_GEN_ACTCTX
)
2419 /* FIXME: what to do if pActCtx->lpSource is set */
2420 status
= get_manifest_in_associated_manifest( &acl
, NULL
, NULL
, directory
,
2421 pActCtx
->hModule
, pActCtx
->lpResourceName
);
2423 else if (pActCtx
->lpSource
)
2425 status
= get_manifest_in_pe_file( &acl
, NULL
, nameW
.Buffer
, directory
, FALSE
,
2426 file
, pActCtx
->lpResourceName
, lang
);
2427 if (status
&& status
!= STATUS_SXS_CANT_GEN_ACTCTX
)
2428 status
= get_manifest_in_associated_manifest( &acl
, NULL
, nameW
.Buffer
, directory
,
2429 NULL
, pActCtx
->lpResourceName
);
2431 else status
= STATUS_INVALID_PARAMETER
;
2435 status
= get_manifest_in_manifest_file( &acl
, NULL
, nameW
.Buffer
, directory
, FALSE
, file
);
2438 if (file
) NtClose( file
);
2439 RtlFreeUnicodeString( &nameW
);
2441 if (NT_SUCCESS(status
)) status
= parse_depend_manifests(&acl
);
2442 free_depend_manifests( &acl
);
2444 if (NT_SUCCESS(status
))
2446 else actctx_release( actctx
);
2450 if (file
) NtClose( file
);
2451 actctx_release( actctx
);
2456 #define ACT_CTX_VALID(p) ((((ULONG_PTR)p - 1) | 7) != -1)
2460 RtlAddRefActivationContext(IN PACTIVATION_CONTEXT Handle
)
2462 PIACTIVATION_CONTEXT ActCtx
= (PIACTIVATION_CONTEXT
)Handle
;
2463 LONG OldRefCount
, NewRefCount
;
2465 if ((ActCtx
) && (ACT_CTX_VALID(ActCtx
)) && (ActCtx
->RefCount
!= LONG_MAX
))
2467 RtlpValidateActCtx(ActCtx
);
2471 OldRefCount
= ActCtx
->RefCount
;
2472 ASSERT(OldRefCount
> 0);
2474 if (OldRefCount
== LONG_MAX
) break;
2476 NewRefCount
= OldRefCount
+ 1;
2477 if (InterlockedCompareExchange(&ActCtx
->RefCount
,
2479 OldRefCount
) == OldRefCount
)
2485 NewRefCount
= LONG_MAX
;
2486 ASSERT(NewRefCount
> 0);
2492 RtlReleaseActivationContext( HANDLE handle
)
2494 PIACTIVATION_CONTEXT ActCtx
= (PIACTIVATION_CONTEXT
) Handle
;
2496 if ((ActCtx
) && (ACT_CTX_VALID(ActCtx
)) && (ActCtx
->RefCount
!= LONG_MAX
))
2498 RtlpValidateActCtx(ActCtx
);
2500 actctx_release(ActCtx
);
2506 RtlAddRefActivationContext( HANDLE handle
)
2508 ACTIVATION_CONTEXT
*actctx
;
2510 if ((actctx
= check_actctx(handle
))) actctx_addref(actctx
);
2515 RtlReleaseActivationContext( HANDLE handle
)
2517 ACTIVATION_CONTEXT
*actctx
;
2519 if ((actctx
= check_actctx(handle
))) actctx_release(actctx
);
2524 NTAPI
RtlActivateActivationContextEx( ULONG flags
, PTEB tebAddress
, HANDLE handle
, PULONG_PTR cookie
)
2526 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*frame
;
2528 if (!(frame
= RtlAllocateHeap( RtlGetProcessHeap(), 0, sizeof(*frame
) )))
2529 return STATUS_NO_MEMORY
;
2531 frame
->Previous
= tebAddress
->ActivationContextStackPointer
->ActiveFrame
;
2532 frame
->ActivationContext
= handle
;
2535 tebAddress
->ActivationContextStackPointer
->ActiveFrame
= frame
;
2536 RtlAddRefActivationContext( handle
);
2538 *cookie
= (ULONG_PTR
)frame
;
2539 DPRINT( "%p cookie=%lx\n", handle
, *cookie
);
2540 return STATUS_SUCCESS
;
2545 NTAPI
RtlActivateActivationContext( ULONG flags
, HANDLE handle
, PULONG_PTR cookie
)
2547 return RtlActivateActivationContextEx(flags
, NtCurrentTeb(), handle
, cookie
);
2552 RtlDeactivateActivationContext( ULONG flags
, ULONG_PTR cookie
)
2554 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*frame
, *top
;
2556 DPRINT( "%x cookie=%lx\n", flags
, cookie
);
2558 /* find the right frame */
2559 top
= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
;
2560 for (frame
= top
; frame
; frame
= frame
->Previous
)
2561 if ((ULONG_PTR
)frame
== cookie
) break;
2564 RtlRaiseStatus( STATUS_SXS_INVALID_DEACTIVATION
);
2566 if (frame
!= top
&& !(flags
& RTL_DEACTIVATE_ACTIVATION_CONTEXT_FLAG_FORCE_EARLY_DEACTIVATION
))
2567 RtlRaiseStatus( STATUS_SXS_EARLY_DEACTIVATION
);
2569 /* pop everything up to and including frame */
2570 NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
= frame
->Previous
;
2572 while (top
!= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
)
2574 frame
= top
->Previous
;
2575 RtlReleaseActivationContext( top
->ActivationContext
);
2576 RtlFreeHeap( RtlGetProcessHeap(), 0, top
);
2580 return STATUS_SUCCESS
;
2585 RtlFreeActivationContextStack(IN PACTIVATION_CONTEXT_STACK Stack
)
2587 PRTL_ACTIVATION_CONTEXT_STACK_FRAME ActiveFrame
, PrevFrame
;
2589 /* Nothing to do if there is no stack */
2592 /* Get the current active frame */
2593 ActiveFrame
= Stack
->ActiveFrame
;
2595 /* Go through them in backwards order and release */
2598 PrevFrame
= ActiveFrame
->Previous
;
2599 RtlReleaseActivationContext(ActiveFrame
->ActivationContext
);
2600 RtlFreeHeap(RtlGetProcessHeap(), 0, ActiveFrame
);
2601 ActiveFrame
= PrevFrame
;
2604 /* Zero out the active frame */
2605 Stack
->ActiveFrame
= NULL
;
2607 /* TODO: Empty the Frame List Cache */
2608 ASSERT(IsListEmpty(&Stack
->FrameListCache
));
2610 /* Free activation stack memory */
2611 RtlFreeHeap(RtlGetProcessHeap(), 0, Stack
);
2615 NTAPI
RtlFreeThreadActivationContextStack(VOID
)
2617 RtlFreeActivationContextStack(NtCurrentTeb()->ActivationContextStackPointer
);
2618 NtCurrentTeb()->ActivationContextStackPointer
= NULL
;
2623 NTAPI
RtlGetActiveActivationContext( HANDLE
*handle
)
2625 if (NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
)
2627 *handle
= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
->ActivationContext
;
2628 RtlAddRefActivationContext( *handle
);
2633 return STATUS_SUCCESS
;
2638 NTAPI
RtlIsActivationContextActive( HANDLE handle
)
2640 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*frame
;
2642 for (frame
= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
; frame
; frame
= frame
->Previous
)
2643 if (frame
->ActivationContext
== handle
) return TRUE
;
2649 RtlQueryInformationActivationContext( ULONG flags
, HANDLE handle
, PVOID subinst
,
2650 ULONG
class, PVOID buffer
,
2651 SIZE_T bufsize
, SIZE_T
*retlen
)
2653 ACTIVATION_CONTEXT
*actctx
;
2656 DPRINT("%08x %p %p %u %p %Iu %p\n", flags
, handle
,
2657 subinst
, class, buffer
, bufsize
, retlen
);
2659 if (retlen
) *retlen
= 0;
2660 if ((status
= find_query_actctx( &handle
, flags
, class ))) return status
;
2664 case ActivationContextBasicInformation
:
2666 ACTIVATION_CONTEXT_BASIC_INFORMATION
*info
= buffer
;
2668 if (retlen
) *retlen
= sizeof(*info
);
2669 if (!info
|| bufsize
< sizeof(*info
)) return STATUS_BUFFER_TOO_SMALL
;
2671 info
->hActCtx
= handle
;
2672 info
->dwFlags
= 0; /* FIXME */
2673 if (!(flags
& RTL_QUERY_ACTIVATION_CONTEXT_FLAG_NO_ADDREF
)) RtlAddRefActivationContext(handle
);
2677 case ActivationContextDetailedInformation
:
2679 ACTIVATION_CONTEXT_DETAILED_INFORMATION
*acdi
= buffer
;
2680 struct assembly
*assembly
= NULL
;
2681 SIZE_T len
, manifest_len
= 0, config_len
= 0, appdir_len
= 0;
2684 if (!(actctx
= check_actctx(handle
))) return STATUS_INVALID_PARAMETER
;
2686 if (actctx
->num_assemblies
) assembly
= actctx
->assemblies
;
2688 if (assembly
&& assembly
->manifest
.info
)
2689 manifest_len
= strlenW(assembly
->manifest
.info
) + 1;
2690 if (actctx
->config
.info
) config_len
= strlenW(actctx
->config
.info
) + 1;
2691 if (actctx
->appdir
.info
) appdir_len
= strlenW(actctx
->appdir
.info
) + 1;
2692 len
= sizeof(*acdi
) + (manifest_len
+ config_len
+ appdir_len
) * sizeof(WCHAR
);
2694 if (retlen
) *retlen
= len
;
2695 if (!buffer
|| bufsize
< len
) return STATUS_BUFFER_TOO_SMALL
;
2698 acdi
->ulFormatVersion
= assembly
? 1 : 0; /* FIXME */
2699 acdi
->ulAssemblyCount
= actctx
->num_assemblies
;
2700 acdi
->ulRootManifestPathType
= assembly
? assembly
->manifest
.type
: 0 /* FIXME */;
2701 acdi
->ulRootManifestPathChars
= assembly
&& assembly
->manifest
.info
? (DWORD
)manifest_len
- 1 : 0;
2702 acdi
->ulRootConfigurationPathType
= actctx
->config
.type
;
2703 acdi
->ulRootConfigurationPathChars
= actctx
->config
.info
? (DWORD
)config_len
- 1 : 0;
2704 acdi
->ulAppDirPathType
= actctx
->appdir
.type
;
2705 acdi
->ulAppDirPathChars
= actctx
->appdir
.info
? (DWORD
)appdir_len
- 1 : 0;
2706 ptr
= (LPWSTR
)(acdi
+ 1);
2709 acdi
->lpRootManifestPath
= ptr
;
2710 memcpy(ptr
, assembly
->manifest
.info
, manifest_len
* sizeof(WCHAR
));
2711 ptr
+= manifest_len
;
2713 else acdi
->lpRootManifestPath
= NULL
;
2716 acdi
->lpRootConfigurationPath
= ptr
;
2717 memcpy(ptr
, actctx
->config
.info
, config_len
* sizeof(WCHAR
));
2720 else acdi
->lpRootConfigurationPath
= NULL
;
2723 acdi
->lpAppDirPath
= ptr
;
2724 memcpy(ptr
, actctx
->appdir
.info
, appdir_len
* sizeof(WCHAR
));
2726 else acdi
->lpAppDirPath
= NULL
;
2730 case AssemblyDetailedInformationInActivationContext
:
2732 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION
*afdi
= buffer
;
2733 struct assembly
*assembly
;
2736 SIZE_T len
, id_len
= 0, ad_len
= 0, path_len
= 0;
2739 if (!(actctx
= check_actctx(handle
))) return STATUS_INVALID_PARAMETER
;
2740 if (!subinst
) return STATUS_INVALID_PARAMETER
;
2742 index
= *(DWORD
*)subinst
;
2743 if (!index
|| index
> actctx
->num_assemblies
) return STATUS_INVALID_PARAMETER
;
2745 assembly
= &actctx
->assemblies
[index
- 1];
2747 if (!(assembly_id
= build_assembly_id( &assembly
->id
))) return STATUS_NO_MEMORY
;
2748 id_len
= strlenW(assembly_id
) + 1;
2749 if (assembly
->directory
) ad_len
= strlenW(assembly
->directory
) + 1;
2751 if (assembly
->manifest
.info
&&
2752 (assembly
->type
== ASSEMBLY_MANIFEST
|| assembly
->type
== ASSEMBLY_SHARED_MANIFEST
))
2753 path_len
= strlenW(assembly
->manifest
.info
) + 1;
2755 len
= sizeof(*afdi
) + (id_len
+ ad_len
+ path_len
) * sizeof(WCHAR
);
2757 if (retlen
) *retlen
= len
;
2758 if (!buffer
|| bufsize
< len
)
2760 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly_id
);
2761 return STATUS_BUFFER_TOO_SMALL
;
2764 afdi
->ulFlags
= 0; /* FIXME */
2765 afdi
->ulEncodedAssemblyIdentityLength
= (DWORD
)(id_len
- 1) * sizeof(WCHAR
);
2766 afdi
->ulManifestPathType
= assembly
->manifest
.type
;
2767 afdi
->ulManifestPathLength
= assembly
->manifest
.info
? (DWORD
)(path_len
- 1) * sizeof(WCHAR
) : 0;
2768 /* FIXME afdi->liManifestLastWriteTime = 0; */
2769 afdi
->ulPolicyPathType
= ACTIVATION_CONTEXT_PATH_TYPE_NONE
; /* FIXME */
2770 afdi
->ulPolicyPathLength
= 0;
2771 /* FIXME afdi->liPolicyLastWriteTime = 0; */
2772 afdi
->ulMetadataSatelliteRosterIndex
= 0; /* FIXME */
2773 afdi
->ulManifestVersionMajor
= 1;
2774 afdi
->ulManifestVersionMinor
= 0;
2775 afdi
->ulPolicyVersionMajor
= 0; /* FIXME */
2776 afdi
->ulPolicyVersionMinor
= 0; /* FIXME */
2777 afdi
->ulAssemblyDirectoryNameLength
= ad_len
? (DWORD
)(ad_len
- 1) * sizeof(WCHAR
) : 0;
2778 ptr
= (LPWSTR
)(afdi
+ 1);
2779 afdi
->lpAssemblyEncodedAssemblyIdentity
= ptr
;
2780 memcpy( ptr
, assembly_id
, id_len
* sizeof(WCHAR
) );
2784 afdi
->lpAssemblyManifestPath
= ptr
;
2785 memcpy(ptr
, assembly
->manifest
.info
, path_len
* sizeof(WCHAR
));
2787 } else afdi
->lpAssemblyManifestPath
= NULL
;
2788 afdi
->lpAssemblyPolicyPath
= NULL
; /* FIXME */
2791 afdi
->lpAssemblyDirectoryName
= ptr
;
2792 memcpy(ptr
, assembly
->directory
, ad_len
* sizeof(WCHAR
));
2795 else afdi
->lpAssemblyDirectoryName
= NULL
;
2796 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly_id
);
2800 case FileInformationInAssemblyOfAssemblyInActivationContext
:
2802 const ACTIVATION_CONTEXT_QUERY_INDEX
*acqi
= subinst
;
2803 ASSEMBLY_FILE_DETAILED_INFORMATION
*afdi
= buffer
;
2804 struct assembly
*assembly
;
2805 struct dll_redirect
*dll
;
2806 SIZE_T len
, dll_len
= 0;
2809 if (!(actctx
= check_actctx(handle
))) return STATUS_INVALID_PARAMETER
;
2810 if (!acqi
) return STATUS_INVALID_PARAMETER
;
2812 if (acqi
->ulAssemblyIndex
>= actctx
->num_assemblies
)
2813 return STATUS_INVALID_PARAMETER
;
2814 assembly
= &actctx
->assemblies
[acqi
->ulAssemblyIndex
];
2816 if (acqi
->ulFileIndexInAssembly
>= assembly
->num_dlls
)
2817 return STATUS_INVALID_PARAMETER
;
2818 dll
= &assembly
->dlls
[acqi
->ulFileIndexInAssembly
];
2820 if (dll
->name
) dll_len
= strlenW(dll
->name
) + 1;
2821 len
= sizeof(*afdi
) + dll_len
* sizeof(WCHAR
);
2823 if (!buffer
|| bufsize
< len
)
2825 if (retlen
) *retlen
= len
;
2826 return STATUS_BUFFER_TOO_SMALL
;
2828 if (retlen
) *retlen
= 0; /* yes that's what native does !! */
2829 afdi
->ulFlags
= ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION
;
2830 afdi
->ulFilenameLength
= dll_len
? (DWORD
)(dll_len
- 1) * sizeof(WCHAR
) : 0;
2831 afdi
->ulPathLength
= 0; /* FIXME */
2832 ptr
= (LPWSTR
)(afdi
+ 1);
2835 afdi
->lpFileName
= ptr
;
2836 memcpy( ptr
, dll
->name
, dll_len
* sizeof(WCHAR
) );
2837 } else afdi
->lpFileName
= NULL
;
2838 afdi
->lpFilePath
= NULL
; /* FIXME */
2843 DPRINT( "class %u not implemented\n", class );
2844 return STATUS_NOT_IMPLEMENTED
;
2846 return STATUS_SUCCESS
;
2851 RtlQueryInformationActiveActivationContext(ULONG ulInfoClass
,
2853 SIZE_T cbBuffer OPTIONAL
,
2854 SIZE_T
*pcbWrittenOrRequired OPTIONAL
)
2856 return RtlQueryInformationActivationContext(RTL_QUERY_ACTIVATION_CONTEXT_FLAG_USE_ACTIVE_ACTIVATION_CONTEXT
,
2862 pcbWrittenOrRequired
);
2865 #define FIND_ACTCTX_RETURN_FLAGS 0x00000002
2866 #define FIND_ACTCTX_RETURN_ASSEMBLY_METADATA 0x00000004
2867 #define FIND_ACTCTX_VALID_MASK (FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX | FIND_ACTCTX_RETURN_FLAGS | FIND_ACTCTX_RETURN_ASSEMBLY_METADATA)
2871 RtlpFindActivationContextSection_CheckParameters( ULONG flags
, const GUID
*guid
, ULONG section_kind
,
2872 UNICODE_STRING
*section_name
, PACTCTX_SECTION_KEYED_DATA data
)
2874 /* Check general parameter combinations */
2875 if (!section_name
||
2876 (flags
& ~FIND_ACTCTX_VALID_MASK
) ||
2877 ((flags
& FIND_ACTCTX_VALID_MASK
) && !data
) ||
2878 (data
&& data
->cbSize
< offsetof(ACTCTX_SECTION_KEYED_DATA
, ulAssemblyRosterIndex
)))
2880 DPRINT1("invalid parameter\n");
2881 return STATUS_INVALID_PARAMETER
;
2885 if (flags
& FIND_ACTCTX_RETURN_FLAGS
||
2886 flags
& FIND_ACTCTX_RETURN_ASSEMBLY_METADATA
)
2888 DPRINT1("unknown flags %08x\n", flags
);
2889 return STATUS_INVALID_PARAMETER
;
2892 return STATUS_SUCCESS
;
2897 RtlFindActivationContextSectionString( ULONG flags
, const GUID
*guid
, ULONG section_kind
,
2898 UNICODE_STRING
*section_name
, PVOID ptr
)
2900 PACTCTX_SECTION_KEYED_DATA data
= ptr
;
2903 status
= RtlpFindActivationContextSection_CheckParameters(flags
, guid
, section_kind
, section_name
, data
);
2904 if (!NT_SUCCESS(status
)) return status
;
2906 status
= STATUS_SXS_KEY_NOT_FOUND
;
2908 /* if there is no data, but params are valid,
2909 we return that sxs key is not found to be at least somehow compatible */
2910 if (!data
) return status
;
2912 ASSERT(NtCurrentTeb());
2913 ASSERT(NtCurrentTeb()->ActivationContextStackPointer
);
2915 if (NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
)
2917 ACTIVATION_CONTEXT
*actctx
= check_actctx(NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
->ActivationContext
);
2918 if (actctx
) status
= find_string( actctx
, section_kind
, section_name
, flags
, data
);
2921 if (status
!= STATUS_SUCCESS
)
2922 status
= find_string( process_actctx
, section_kind
, section_name
, flags
, data
);
2929 RtlFindActivationContextSectionGuid(ULONG flags
, const GUID
*guid
, ULONG section_kind
, UNICODE_STRING
*section_name
, PVOID ptr
)
2932 return STATUS_NOT_IMPLEMENTED
;
2939 RtlAllocateActivationContextStack(IN PACTIVATION_CONTEXT_STACK
*Stack
)
2941 PACTIVATION_CONTEXT_STACK ContextStack
;
2943 /* Check if it's already allocated */
2944 if (*Stack
) return STATUS_SUCCESS
;
2946 /* Allocate space for the context stack */
2947 ContextStack
= RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ACTIVATION_CONTEXT_STACK
));
2950 return STATUS_NO_MEMORY
;
2953 /* Initialize the context stack */
2954 ContextStack
->Flags
= 0;
2955 ContextStack
->ActiveFrame
= NULL
;
2956 InitializeListHead(&ContextStack
->FrameListCache
);
2957 ContextStack
->NextCookieSequenceNumber
= 1;
2958 ContextStack
->StackId
= 1; //TODO: Timer-based
2960 *Stack
= ContextStack
;
2962 return STATUS_SUCCESS
;
2965 PRTL_ACTIVATION_CONTEXT_STACK_FRAME
2967 RtlActivateActivationContextUnsafeFast(IN PRTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED Frame
,
2970 #if NEW_NTDLL_LOADER
2971 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*ActiveFrame
;
2973 /* Get the curren active frame */
2974 ActiveFrame
= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
;
2976 DPRINT1("ActiveFrame %p, &Frame->Frame %p, Context %p\n", ActiveFrame
, &Frame
->Frame
, Context
);
2978 /* Actually activate it */
2979 Frame
->Frame
.Previous
= ActiveFrame
;
2980 Frame
->Frame
.ActivationContext
= Context
;
2981 Frame
->Frame
.Flags
= 0;
2983 /* Check if we can activate this context */
2984 if ((ActiveFrame
&& (ActiveFrame
->ActivationContext
!= Context
)) ||
2987 /* Set new active frame */
2988 NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
= &Frame
->Frame
;
2989 return &Frame
->Frame
;
2992 /* We can get here only one way: it was already activated */
2993 DPRINT1("Trying to activate improper activation context\n");
2995 /* Activate only if we are allowing multiple activation */
2996 if (!RtlpNotAllowingMultipleActivation
)
2998 NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
= &Frame
->Frame
;
3003 Frame
->Frame
.Flags
= 0x30;
3006 /* Return pointer to the activation frame */
3007 return &Frame
->Frame
;
3010 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*frame
= &Frame
->Frame
;
3012 frame
->Previous
= NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
;
3013 frame
->ActivationContext
= Context
;
3016 NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
= frame
;
3018 return STATUS_SUCCESS
;
3022 PRTL_ACTIVATION_CONTEXT_STACK_FRAME
3024 RtlDeactivateActivationContextUnsafeFast(IN PRTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED Frame
)
3026 RTL_ACTIVATION_CONTEXT_STACK_FRAME
*frame
;
3027 //RTL_ACTIVATION_CONTEXT_STACK_FRAME *top;
3029 /* find the right frame */
3030 //top = NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame;
3031 frame
= &Frame
->Frame
;
3035 DPRINT1("No top frame!\n");
3036 RtlRaiseStatus( STATUS_SXS_INVALID_DEACTIVATION
);
3039 /* pop everything up to and including frame */
3040 NtCurrentTeb()->ActivationContextStackPointer
->ActiveFrame
= frame
->Previous
;
3048 RtlZombifyActivationContext(PVOID Context
)
3052 if (Context
== ACTCTX_FAKE_HANDLE
)
3053 return STATUS_SUCCESS
;
3055 return STATUS_NOT_IMPLEMENTED
;