Fix other _SEH2_YIELD() issues, spotted for most of them by Pigglesworth, the other...
[reactos.git] / reactos / lib / rtl / actctx.c
1 /*
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
6 * PROGRAMERS:
7 * Jon Griffiths
8 * Eric Pouech
9 * Jacek Caban for CodeWeavers
10 * Alexandre Julliard
11 * Stefan Ginsberg (stefan__100__@hotmail.com)
12 * Samuel SerapiĆ³n
13 */
14
15 /* Based on Wine 1.1.26 */
16
17 /* INCLUDES *****************************************************************/
18 #include <rtl.h>
19
20 #define NDEBUG
21 #include <debug.h>
22
23 #include <wine/unicode.h>
24
25 BOOLEAN RtlpNotAllowingMultipleActivation;
26
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 )
36
37 #define ACTCTX_MAGIC 0xC07E3E11
38
39 #define ACTCTX_FAKE_HANDLE ((HANDLE) 0xf00baa)
40 #define ACTCTX_FAKE_COOKIE ((ULONG_PTR) 0xf00bad)
41
42
43
44 typedef struct
45 {
46 const WCHAR *ptr;
47 unsigned int len;
48 } xmlstr_t;
49
50 typedef struct
51 {
52 const WCHAR *ptr;
53 const WCHAR *end;
54 } xmlbuf_t;
55
56 struct file_info
57 {
58 ULONG type;
59 WCHAR *info;
60 };
61
62 struct assembly_version
63 {
64 USHORT major;
65 USHORT minor;
66 USHORT build;
67 USHORT revision;
68 };
69
70 struct assembly_identity
71 {
72 WCHAR *name;
73 WCHAR *arch;
74 WCHAR *public_key;
75 WCHAR *language;
76 WCHAR *type;
77 struct assembly_version version;
78 BOOL optional;
79 };
80
81 struct entity
82 {
83 DWORD kind;
84 union
85 {
86 struct
87 {
88 WCHAR *tlbid;
89 WCHAR *version;
90 WCHAR *helpdir;
91 } typelib;
92 struct
93 {
94 WCHAR *clsid;
95 } comclass;
96 struct {
97 WCHAR *iid;
98 WCHAR *name;
99 } proxy;
100 struct
101 {
102 WCHAR *name;
103 } class;
104 struct
105 {
106 WCHAR *name;
107 WCHAR *clsid;
108 } clrclass;
109 struct
110 {
111 WCHAR *name;
112 WCHAR *clsid;
113 } clrsurrogate;
114 } u;
115 };
116
117 struct entity_array
118 {
119 struct entity *base;
120 unsigned int num;
121 unsigned int allocated;
122 };
123
124 struct dll_redirect
125 {
126 WCHAR *name;
127 WCHAR *hash;
128 struct entity_array entities;
129 };
130
131 enum assembly_type
132 {
133 APPLICATION_MANIFEST,
134 ASSEMBLY_MANIFEST,
135 ASSEMBLY_SHARED_MANIFEST,
136 };
137
138 struct assembly
139 {
140 enum assembly_type type;
141 struct assembly_identity id;
142 struct file_info manifest;
143 WCHAR *directory;
144 BOOL no_inherit;
145 struct dll_redirect *dlls;
146 unsigned int num_dlls;
147 unsigned int allocated_dlls;
148 struct entity_array entities;
149 };
150
151 typedef struct _ACTIVATION_CONTEXT
152 {
153 ULONG magic;
154 long ref_count;
155 struct file_info config;
156 struct file_info appdir;
157 struct assembly *assemblies;
158 unsigned int num_assemblies;
159 unsigned int allocated_assemblies;
160 } ACTIVATION_CONTEXT;
161
162 struct actctx_loader
163 {
164 ACTIVATION_CONTEXT *actctx;
165 struct assembly_identity *dependencies;
166 unsigned int num_dependencies;
167 unsigned int allocated_dependencies;
168 };
169
170 static const WCHAR assemblyW[] = {'a','s','s','e','m','b','l','y',0};
171 static const WCHAR assemblyIdentityW[] = {'a','s','s','e','m','b','l','y','I','d','e','n','t','i','t','y',0};
172 static const WCHAR bindingRedirectW[] = {'b','i','n','d','i','n','g','R','e','d','i','r','e','c','t',0};
173 static const WCHAR clrClassW[] = {'c','l','r','C','l','a','s','s',0};
174 static const WCHAR clrSurrogateW[] = {'c','l','r','S','u','r','r','o','g','a','t','e',0};
175 static const WCHAR comClassW[] = {'c','o','m','C','l','a','s','s',0};
176 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};
177 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};
178 static const WCHAR dependencyW[] = {'d','e','p','e','n','d','e','n','c','y',0};
179 static const WCHAR dependentAssemblyW[] = {'d','e','p','e','n','d','e','n','t','A','s','s','e','m','b','l','y',0};
180 static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
181 static const WCHAR fileW[] = {'f','i','l','e',0};
182 static const WCHAR asmv2hashW[] = {'a','s','m','v','2',':','h','a','s','h',0};
183 static const WCHAR noInheritW[] = {'n','o','I','n','h','e','r','i','t',0};
184 static const WCHAR noInheritableW[] = {'n','o','I','n','h','e','r','i','t','a','b','l','e',0};
185 static const WCHAR typelibW[] = {'t','y','p','e','l','i','b',0};
186 static const WCHAR windowClassW[] = {'w','i','n','d','o','w','C','l','a','s','s',0};
187
188 static const WCHAR clsidW[] = {'c','l','s','i','d',0};
189 static const WCHAR hashW[] = {'h','a','s','h',0};
190 static const WCHAR hashalgW[] = {'h','a','s','h','a','l','g',0};
191 static const WCHAR helpdirW[] = {'h','e','l','p','d','i','r',0};
192 static const WCHAR iidW[] = {'i','i','d',0};
193 static const WCHAR languageW[] = {'l','a','n','g','u','a','g','e',0};
194 static const WCHAR manifestVersionW[] = {'m','a','n','i','f','e','s','t','V','e','r','s','i','o','n',0};
195 static const WCHAR g_nameW[] = {'n','a','m','e',0};
196 static const WCHAR newVersionW[] = {'n','e','w','V','e','r','s','i','o','n',0};
197 static const WCHAR oldVersionW[] = {'o','l','d','V','e','r','s','i','o','n',0};
198 static const WCHAR optionalW[] = {'o','p','t','i','o','n','a','l',0};
199 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};
200 static const WCHAR publicKeyTokenW[] = {'p','u','b','l','i','c','K','e','y','T','o','k','e','n',0};
201 static const WCHAR tlbidW[] = {'t','l','b','i','d',0};
202 static const WCHAR typeW[] = {'t','y','p','e',0};
203 static const WCHAR versionW[] = {'v','e','r','s','i','o','n',0};
204 static const WCHAR xmlnsW[] = {'x','m','l','n','s',0};
205
206 static const WCHAR g_xmlW[] = {'?','x','m','l',0};
207 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};
208 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};
209
210 static const WCHAR dotManifestW[] = {'.','m','a','n','i','f','e','s','t',0};
211 static const WCHAR version_formatW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
212
213 static ACTIVATION_CONTEXT system_actctx = { ACTCTX_MAGIC, 1 };
214 static ACTIVATION_CONTEXT *process_actctx = &system_actctx;
215
216 static WCHAR *strdupW(const WCHAR* str)
217 {
218 WCHAR* ptr;
219
220 if (!(ptr = RtlAllocateHeap(RtlGetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(WCHAR))))
221 return NULL;
222 return strcpyW(ptr, str);
223 }
224
225 static WCHAR *xmlstrdupW(const xmlstr_t* str)
226 {
227 WCHAR *strW;
228
229 if ((strW = RtlAllocateHeap(RtlGetProcessHeap(), 0, (str->len + 1) * sizeof(WCHAR))))
230 {
231 memcpy( strW, str->ptr, str->len * sizeof(WCHAR) );
232 strW[str->len] = 0;
233 }
234 return strW;
235 }
236
237 static UNICODE_STRING xmlstr2unicode(const xmlstr_t *xmlstr)
238 {
239 UNICODE_STRING res;
240
241 res.Buffer = (PWSTR)xmlstr->ptr;
242 res.Length = res.MaximumLength = (USHORT)xmlstr->len * sizeof(WCHAR);
243
244 return res;
245 }
246
247 static inline BOOL xmlstr_cmp(const xmlstr_t* xmlstr, const WCHAR *str)
248 {
249 return !strncmpW(xmlstr->ptr, str, xmlstr->len) && !str[xmlstr->len];
250 }
251
252 static inline BOOL xmlstr_cmpi(const xmlstr_t* xmlstr, const WCHAR *str)
253 {
254 return !strncmpiW(xmlstr->ptr, str, xmlstr->len) && !str[xmlstr->len];
255 }
256
257 static inline BOOL xmlstr_cmp_end(const xmlstr_t* xmlstr, const WCHAR *str)
258 {
259 return (xmlstr->len && xmlstr->ptr[0] == '/' &&
260 !strncmpW(xmlstr->ptr + 1, str, xmlstr->len - 1) && !str[xmlstr->len - 1]);
261 }
262
263 static inline BOOL isxmlspace( WCHAR ch )
264 {
265 return (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t');
266 }
267
268 static struct assembly *add_assembly(ACTIVATION_CONTEXT *actctx, enum assembly_type at)
269 {
270 struct assembly *assembly;
271
272 if (actctx->num_assemblies == actctx->allocated_assemblies)
273 {
274 void *ptr;
275 unsigned int new_count;
276 if (actctx->assemblies)
277 {
278 new_count = actctx->allocated_assemblies * 2;
279 ptr = RtlReAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY,
280 actctx->assemblies, new_count * sizeof(*assembly) );
281 }
282 else
283 {
284 new_count = 4;
285 ptr = RtlAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*assembly) );
286 }
287 if (!ptr) return NULL;
288 actctx->assemblies = ptr;
289 actctx->allocated_assemblies = new_count;
290 }
291
292 assembly = &actctx->assemblies[actctx->num_assemblies++];
293 assembly->type = at;
294 return assembly;
295 }
296
297 static struct dll_redirect* add_dll_redirect(struct assembly* assembly)
298 {
299 if (assembly->num_dlls == assembly->allocated_dlls)
300 {
301 void *ptr;
302 unsigned int new_count;
303 if (assembly->dlls)
304 {
305 new_count = assembly->allocated_dlls * 2;
306 ptr = RtlReAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY,
307 assembly->dlls, new_count * sizeof(*assembly->dlls) );
308 }
309 else
310 {
311 new_count = 4;
312 ptr = RtlAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*assembly->dlls) );
313 }
314 if (!ptr) return NULL;
315 assembly->dlls = ptr;
316 assembly->allocated_dlls = new_count;
317 }
318 return &assembly->dlls[assembly->num_dlls++];
319 }
320
321 static void free_assembly_identity(struct assembly_identity *ai)
322 {
323 RtlFreeHeap( RtlGetProcessHeap(), 0, ai->name );
324 RtlFreeHeap( RtlGetProcessHeap(), 0, ai->arch );
325 RtlFreeHeap( RtlGetProcessHeap(), 0, ai->public_key );
326 RtlFreeHeap( RtlGetProcessHeap(), 0, ai->language );
327 RtlFreeHeap( RtlGetProcessHeap(), 0, ai->type );
328 }
329
330 static struct entity* add_entity(struct entity_array *array, DWORD kind)
331 {
332 struct entity* entity;
333
334 if (array->num == array->allocated)
335 {
336 void *ptr;
337 unsigned int new_count;
338 if (array->base)
339 {
340 new_count = array->allocated * 2;
341 ptr = RtlReAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY,
342 array->base, new_count * sizeof(*array->base) );
343 }
344 else
345 {
346 new_count = 4;
347 ptr = RtlAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY, new_count * sizeof(*array->base) );
348 }
349 if (!ptr) return NULL;
350 array->base = ptr;
351 array->allocated = new_count;
352 }
353 entity = &array->base[array->num++];
354 entity->kind = kind;
355 return entity;
356 }
357
358 static void free_entity_array(struct entity_array *array)
359 {
360 unsigned int i;
361 for (i = 0; i < array->num; i++)
362 {
363 struct entity *entity = &array->base[i];
364 switch (entity->kind)
365 {
366 case ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION:
367 RtlFreeHeap(RtlGetProcessHeap(), 0, entity->u.comclass.clsid);
368 break;
369 case ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION:
370 RtlFreeHeap(RtlGetProcessHeap(), 0, entity->u.proxy.iid);
371 RtlFreeHeap(RtlGetProcessHeap(), 0, entity->u.proxy.name);
372 break;
373 case ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION:
374 RtlFreeHeap(RtlGetProcessHeap(), 0, entity->u.typelib.tlbid);
375 RtlFreeHeap(RtlGetProcessHeap(), 0, entity->u.typelib.version);
376 RtlFreeHeap(RtlGetProcessHeap(), 0, entity->u.typelib.helpdir);
377 break;
378 case ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION:
379 RtlFreeHeap(RtlGetProcessHeap(), 0, entity->u.class.name);
380 break;
381 case ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION:
382 RtlFreeHeap(RtlGetProcessHeap(), 0, entity->u.clrclass.name);
383 RtlFreeHeap(RtlGetProcessHeap(), 0, entity->u.clrclass.clsid);
384 break;
385 case ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES:
386 RtlFreeHeap(RtlGetProcessHeap(), 0, entity->u.clrsurrogate.name);
387 RtlFreeHeap(RtlGetProcessHeap(), 0, entity->u.clrsurrogate.clsid);
388 break;
389 default:
390 DPRINT1("Unknown entity kind %u\n", entity->kind);
391 }
392 }
393 RtlFreeHeap( RtlGetProcessHeap(), 0, array->base );
394 }
395
396 static BOOL is_matching_string( const WCHAR *str1, const WCHAR *str2 )
397 {
398 if (!str1) return !str2;
399 return str2 && !strcmpiW( str1, str2 );
400 }
401
402 static BOOL is_matching_identity( const struct assembly_identity *id1,
403 const struct assembly_identity *id2 )
404 {
405 if (!is_matching_string( id1->name, id2->name )) return FALSE;
406 if (!is_matching_string( id1->arch, id2->arch )) return FALSE;
407 if (!is_matching_string( id1->public_key, id2->public_key )) return FALSE;
408
409 if (id1->language && id2->language && strcmpiW( id1->language, id2->language ))
410 {
411 static const WCHAR wildcardW[] = {'*',0};
412 if (strcmpW( wildcardW, id1->language ) && strcmpW( wildcardW, id2->language ))
413 return FALSE;
414 }
415 if (id1->version.major != id2->version.major) return FALSE;
416 if (id1->version.minor != id2->version.minor) return FALSE;
417 if (id1->version.build > id2->version.build) return FALSE;
418 if (id1->version.build == id2->version.build &&
419 id1->version.revision > id2->version.revision) return FALSE;
420 return TRUE;
421 }
422
423 static BOOL add_dependent_assembly_id(struct actctx_loader* acl,
424 struct assembly_identity* ai)
425 {
426 unsigned int i;
427
428 /* check if we already have that assembly */
429
430 for (i = 0; i < acl->actctx->num_assemblies; i++)
431 if (is_matching_identity( ai, &acl->actctx->assemblies[i].id ))
432 {
433 DPRINT( "reusing existing assembly for %S arch %S version %u.%u.%u.%u\n",
434 ai->name, ai->arch, ai->version.major, ai->version.minor,
435 ai->version.build, ai->version.revision );
436 return TRUE;
437 }
438
439 for (i = 0; i < acl->num_dependencies; i++)
440 if (is_matching_identity( ai, &acl->dependencies[i] ))
441 {
442 DPRINT( "reusing existing dependency for %S arch %S version %u.%u.%u.%u\n",
443 ai->name, ai->arch, ai->version.major, ai->version.minor,
444 ai->version.build, ai->version.revision );
445 return TRUE;
446 }
447
448 if (acl->num_dependencies == acl->allocated_dependencies)
449 {
450 void *ptr;
451 unsigned int new_count;
452 if (acl->dependencies)
453 {
454 new_count = acl->allocated_dependencies * 2;
455 ptr = RtlReAllocateHeap(RtlGetProcessHeap(), 0, acl->dependencies,
456 new_count * sizeof(acl->dependencies[0]));
457 }
458 else
459 {
460 new_count = 4;
461 ptr = RtlAllocateHeap(RtlGetProcessHeap(), 0, new_count * sizeof(acl->dependencies[0]));
462 }
463 if (!ptr) return FALSE;
464 acl->dependencies = ptr;
465 acl->allocated_dependencies = new_count;
466 }
467 acl->dependencies[acl->num_dependencies++] = *ai;
468
469 return TRUE;
470 }
471
472 static void free_depend_manifests(struct actctx_loader* acl)
473 {
474 unsigned int i;
475 for (i = 0; i < acl->num_dependencies; i++)
476 free_assembly_identity(&acl->dependencies[i]);
477 RtlFreeHeap(RtlGetProcessHeap(), 0, acl->dependencies);
478 }
479
480 static WCHAR *build_assembly_dir(struct assembly_identity* ai)
481 {
482 static const WCHAR undW[] = {'_',0};
483 static const WCHAR noneW[] = {'n','o','n','e',0};
484 static const WCHAR mskeyW[] = {'d','e','a','d','b','e','e','f',0};
485
486 const WCHAR *arch = ai->arch ? ai->arch : noneW;
487 const WCHAR *key = ai->public_key ? ai->public_key : noneW;
488 const WCHAR *lang = ai->language ? ai->language : noneW;
489 const WCHAR *name = ai->name ? ai->name : noneW;
490 SIZE_T size = (strlenW(arch) + 1 + strlenW(name) + 1 + strlenW(key) + 24 + 1 +
491 strlenW(lang) + 1) * sizeof(WCHAR) + sizeof(mskeyW);
492 WCHAR *ret;
493
494 if (!(ret = RtlAllocateHeap( RtlGetProcessHeap(), 0, size ))) return NULL;
495
496 strcpyW( ret, arch );
497 strcatW( ret, undW );
498 strcatW( ret, name );
499 strcatW( ret, undW );
500 strcatW( ret, key );
501 strcatW( ret, undW );
502 sprintfW( ret + strlenW(ret), version_formatW,
503 ai->version.major, ai->version.minor, ai->version.build, ai->version.revision );
504 strcatW( ret, undW );
505 strcatW( ret, lang );
506 strcatW( ret, undW );
507 strcatW( ret, mskeyW );
508 return ret;
509 }
510
511 static inline void append_string( WCHAR *buffer, const WCHAR *prefix, const WCHAR *str )
512 {
513 WCHAR *p = buffer;
514
515 if (!str) return;
516 strcatW( buffer, prefix );
517 p += strlenW(p);
518 *p++ = '"';
519 strcpyW( p, str );
520 p += strlenW(p);
521 *p++ = '"';
522 *p = 0;
523 }
524
525 static WCHAR *build_assembly_id( const struct assembly_identity *ai )
526 {
527 static const WCHAR archW[] =
528 {',','p','r','o','c','e','s','s','o','r','A','r','c','h','i','t','e','c','t','u','r','e','=',0};
529 static const WCHAR public_keyW[] =
530 {',','p','u','b','l','i','c','K','e','y','T','o','k','e','n','=',0};
531
532 WCHAR version[64], *ret;
533 SIZE_T size = 0;
534
535 sprintfW( version, version_formatW,
536 ai->version.major, ai->version.minor, ai->version.build, ai->version.revision );
537 if (ai->name) size += strlenW(ai->name);
538 if (ai->arch) size += strlenW(archW) + strlenW(ai->arch) + 2;
539 if (ai->public_key) size += strlenW(public_keyW) + strlenW(ai->public_key) + 2;
540 if (ai->type) size += 1 + strlenW(typeW) + 1 + strlenW(ai->type) + 2;
541 size += 1+ strlenW(versionW) + 1 + strlenW(version) + 2;
542
543 if (!(ret = RtlAllocateHeap( RtlGetProcessHeap(), 0, (size + 1) * sizeof(WCHAR) )))
544 return NULL;
545
546 if (ai->name) strcpyW( ret, ai->name );
547 else *ret = 0;
548 append_string( ret, archW, ai->arch );
549 append_string( ret, public_keyW, ai->public_key );
550 append_string( ret, typeW, ai->type );
551 append_string( ret, versionW, version );
552 return ret;
553 }
554
555 static ACTIVATION_CONTEXT *check_actctx( HANDLE h )
556 {
557 ACTIVATION_CONTEXT *ret = NULL, *actctx = h;
558
559 if (!h || h == INVALID_HANDLE_VALUE) return NULL;
560 _SEH2_TRY
561 {
562 if (actctx && actctx->magic == ACTCTX_MAGIC) ret = actctx;
563 }
564 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
565 {
566 DPRINT1("Invalid activation context handle!\n");
567 }
568 _SEH2_END;
569 return ret;
570 }
571
572 static inline void actctx_addref( ACTIVATION_CONTEXT *actctx )
573 {
574 InterlockedExchangeAdd( &actctx->ref_count, 1 );
575 }
576
577 static void actctx_release( ACTIVATION_CONTEXT *actctx )
578 {
579 if (InterlockedExchangeAdd( &actctx->ref_count, -1 ) == 1)
580 {
581 unsigned int i, j;
582
583 for (i = 0; i < actctx->num_assemblies; i++)
584 {
585 struct assembly *assembly = &actctx->assemblies[i];
586 for (j = 0; j < assembly->num_dlls; j++)
587 {
588 struct dll_redirect *dll = &assembly->dlls[j];
589 free_entity_array( &dll->entities );
590 RtlFreeHeap( RtlGetProcessHeap(), 0, dll->name );
591 RtlFreeHeap( RtlGetProcessHeap(), 0, dll->hash );
592 }
593 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly->dlls );
594 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly->manifest.info );
595 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly->directory );
596 free_entity_array( &assembly->entities );
597 free_assembly_identity(&assembly->id);
598 }
599 RtlFreeHeap( RtlGetProcessHeap(), 0, actctx->config.info );
600 RtlFreeHeap( RtlGetProcessHeap(), 0, actctx->appdir.info );
601 RtlFreeHeap( RtlGetProcessHeap(), 0, actctx->assemblies );
602 actctx->magic = 0;
603 RtlFreeHeap( RtlGetProcessHeap(), 0, actctx );
604 }
605 }
606
607 static BOOL next_xml_attr(xmlbuf_t* xmlbuf, xmlstr_t* name, xmlstr_t* value,
608 BOOL* error, BOOL* end)
609 {
610 const WCHAR* ptr;
611
612 *error = TRUE;
613
614 while (xmlbuf->ptr < xmlbuf->end && isxmlspace(*xmlbuf->ptr))
615 xmlbuf->ptr++;
616
617 if (xmlbuf->ptr == xmlbuf->end) return FALSE;
618
619 if (*xmlbuf->ptr == '/')
620 {
621 xmlbuf->ptr++;
622 if (xmlbuf->ptr == xmlbuf->end || *xmlbuf->ptr != '>')
623 return FALSE;
624
625 xmlbuf->ptr++;
626 *end = TRUE;
627 *error = FALSE;
628 return FALSE;
629 }
630
631 if (*xmlbuf->ptr == '>')
632 {
633 xmlbuf->ptr++;
634 *error = FALSE;
635 return FALSE;
636 }
637
638 ptr = xmlbuf->ptr;
639 while (ptr < xmlbuf->end && *ptr != '=' && *ptr != '>' && !isxmlspace(*ptr)) ptr++;
640
641 if (ptr == xmlbuf->end || *ptr != '=') return FALSE;
642
643 name->ptr = xmlbuf->ptr;
644 name->len = (ULONG)(ptr - xmlbuf->ptr);
645 xmlbuf->ptr = ptr;
646
647 ptr++;
648 if (ptr == xmlbuf->end || (*ptr != '"' && *ptr != '\'')) return FALSE;
649
650 value->ptr = ++ptr;
651 if (ptr == xmlbuf->end) return FALSE;
652
653 ptr = memchrW(ptr, ptr[-1], xmlbuf->end - ptr);
654 if (!ptr)
655 {
656 xmlbuf->ptr = xmlbuf->end;
657 return FALSE;
658 }
659
660 value->len = (ULONG)(ptr - value->ptr);
661 xmlbuf->ptr = ptr + 1;
662
663 if (xmlbuf->ptr == xmlbuf->end) return FALSE;
664
665 *error = FALSE;
666 return TRUE;
667 }
668
669 static BOOL next_xml_elem(xmlbuf_t* xmlbuf, xmlstr_t* elem)
670 {
671 const WCHAR* ptr;
672
673 for (;;)
674 {
675 ptr = memchrW(xmlbuf->ptr, '<', xmlbuf->end - xmlbuf->ptr);
676 if (!ptr)
677 {
678 xmlbuf->ptr = xmlbuf->end;
679 return FALSE;
680 }
681 ptr++;
682 if (ptr + 3 < xmlbuf->end && ptr[0] == '!' && ptr[1] == '-' && ptr[2] == '-') /* skip comment */
683 {
684 for (ptr += 3; ptr + 3 <= xmlbuf->end; ptr++)
685 if (ptr[0] == '-' && ptr[1] == '-' && ptr[2] == '>') break;
686
687 if (ptr + 3 > xmlbuf->end)
688 {
689 xmlbuf->ptr = xmlbuf->end;
690 return FALSE;
691 }
692 xmlbuf->ptr = ptr + 3;
693 }
694 else break;
695 }
696
697 xmlbuf->ptr = ptr;
698 while (ptr < xmlbuf->end && !isxmlspace(*ptr) && *ptr != '>' && (*ptr != '/' || ptr == xmlbuf->ptr))
699 ptr++;
700
701 elem->ptr = xmlbuf->ptr;
702 elem->len = (ULONG)(ptr - xmlbuf->ptr);
703 xmlbuf->ptr = ptr;
704 return xmlbuf->ptr != xmlbuf->end;
705 }
706
707 static BOOL parse_xml_header(xmlbuf_t* xmlbuf)
708 {
709 /* FIXME: parse attributes */
710 const WCHAR *ptr;
711
712 for (ptr = xmlbuf->ptr; ptr < xmlbuf->end - 1; ptr++)
713 {
714 if (ptr[0] == '?' && ptr[1] == '>')
715 {
716 xmlbuf->ptr = ptr + 2;
717 return TRUE;
718 }
719 }
720 return FALSE;
721 }
722
723 static BOOL parse_text_content(xmlbuf_t* xmlbuf, xmlstr_t* content)
724 {
725 const WCHAR *ptr = memchrW(xmlbuf->ptr, '<', xmlbuf->end - xmlbuf->ptr);
726
727 if (!ptr) return FALSE;
728
729 content->ptr = xmlbuf->ptr;
730 content->len = (ULONG)(ptr - xmlbuf->ptr);
731 xmlbuf->ptr = ptr;
732
733 return TRUE;
734 }
735
736 static BOOL parse_version(const xmlstr_t *str, struct assembly_version *version)
737 {
738 unsigned int ver[4];
739 unsigned int pos;
740 const WCHAR *curr;
741 UNICODE_STRING strU;
742
743 /* major.minor.build.revision */
744 ver[0] = ver[1] = ver[2] = ver[3] = pos = 0;
745 for (curr = str->ptr; curr < str->ptr + str->len; curr++)
746 {
747 if (*curr >= '0' && *curr <= '9')
748 {
749 ver[pos] = ver[pos] * 10 + *curr - '0';
750 if (ver[pos] >= 0x10000) goto error;
751 }
752 else if (*curr == '.')
753 {
754 if (++pos >= 4) goto error;
755 }
756 else goto error;
757 }
758 version->major = ver[0];
759 version->minor = ver[1];
760 version->build = ver[2];
761 version->revision = ver[3];
762 return TRUE;
763
764 error:
765 strU = xmlstr2unicode(str);
766 DPRINT1( "Wrong version definition in manifest file (%wZ)\n", &strU );
767 return FALSE;
768 }
769
770 static BOOL parse_expect_elem(xmlbuf_t* xmlbuf, const WCHAR* name)
771 {
772 xmlstr_t elem;
773 UNICODE_STRING elemU;
774 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
775 if (xmlstr_cmp(&elem, name)) return TRUE;
776 elemU = xmlstr2unicode(&elem);
777 DPRINT1( "unexpected element %wZ\n", &elemU );
778 return FALSE;
779 }
780
781 static BOOL parse_expect_no_attr(xmlbuf_t* xmlbuf, BOOL* end)
782 {
783 xmlstr_t attr_name, attr_value;
784 UNICODE_STRING attr_nameU, attr_valueU;
785 BOOL error;
786
787 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, end))
788 {
789 attr_nameU = xmlstr2unicode(&attr_name);
790 attr_valueU = xmlstr2unicode(&attr_value);
791 DPRINT1( "unexpected attr %wZ=%wZ\n", &attr_nameU,
792 &attr_valueU);
793 }
794 return !error;
795 }
796
797 static BOOL parse_end_element(xmlbuf_t *xmlbuf)
798 {
799 BOOL end = FALSE;
800 return parse_expect_no_attr(xmlbuf, &end) && !end;
801 }
802
803 static BOOL parse_expect_end_elem(xmlbuf_t *xmlbuf, const WCHAR *name)
804 {
805 xmlstr_t elem;
806 UNICODE_STRING elemU;
807 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
808 if (!xmlstr_cmp_end(&elem, name))
809 {
810 elemU = xmlstr2unicode(&elem);
811 DPRINT1( "unexpected element %wZ\n", &elemU );
812 return FALSE;
813 }
814 return parse_end_element(xmlbuf);
815 }
816
817 static BOOL parse_unknown_elem(xmlbuf_t *xmlbuf, const xmlstr_t *unknown_elem)
818 {
819 xmlstr_t attr_name, attr_value, elem;
820 BOOL end = FALSE, error, ret = TRUE;
821
822 while(next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end));
823 if(error || end) return end;
824
825 while(ret && (ret = next_xml_elem(xmlbuf, &elem)))
826 {
827 if(*elem.ptr == '/' && elem.len - 1 == unknown_elem->len &&
828 !strncmpW(elem.ptr+1, unknown_elem->ptr, unknown_elem->len))
829 break;
830 else
831 ret = parse_unknown_elem(xmlbuf, &elem);
832 }
833
834 return ret && parse_end_element(xmlbuf);
835 }
836
837 static BOOL parse_assembly_identity_elem(xmlbuf_t* xmlbuf, ACTIVATION_CONTEXT* actctx,
838 struct assembly_identity* ai)
839 {
840 xmlstr_t attr_name, attr_value;
841 BOOL end = FALSE, error;
842 UNICODE_STRING attr_valueU, attr_nameU;
843
844 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
845 {
846 if (xmlstr_cmp(&attr_name, g_nameW))
847 {
848 if (!(ai->name = xmlstrdupW(&attr_value))) return FALSE;
849 }
850 else if (xmlstr_cmp(&attr_name, typeW))
851 {
852 if (!(ai->type = xmlstrdupW(&attr_value))) return FALSE;
853 }
854 else if (xmlstr_cmp(&attr_name, versionW))
855 {
856 if (!parse_version(&attr_value, &ai->version)) return FALSE;
857 }
858 else if (xmlstr_cmp(&attr_name, processorArchitectureW))
859 {
860 if (!(ai->arch = xmlstrdupW(&attr_value))) return FALSE;
861 }
862 else if (xmlstr_cmp(&attr_name, publicKeyTokenW))
863 {
864 if (!(ai->public_key = xmlstrdupW(&attr_value))) return FALSE;
865 }
866 else if (xmlstr_cmp(&attr_name, languageW))
867 {
868 if (!(ai->language = xmlstrdupW(&attr_value))) return FALSE;
869 DPRINT1("Unsupported yet language attribute (%S)\n",
870 ai->language);
871 }
872 else
873 {
874 attr_nameU = xmlstr2unicode(&attr_name);
875 attr_valueU = xmlstr2unicode(&attr_value);
876 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU, &attr_valueU);
877 }
878 }
879
880 if (error || end) return end;
881 return parse_expect_end_elem(xmlbuf, assemblyIdentityW);
882 }
883
884 static BOOL parse_com_class_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
885 {
886 xmlstr_t elem, attr_name, attr_value;
887 BOOL ret, end = FALSE, error;
888 struct entity* entity;
889 UNICODE_STRING attr_valueU, attr_nameU;
890
891 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION)))
892 return FALSE;
893
894 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
895 {
896 if (xmlstr_cmp(&attr_name, clsidW))
897 {
898 if (!(entity->u.comclass.clsid = xmlstrdupW(&attr_value))) return FALSE;
899 }
900 else
901 {
902 attr_nameU = xmlstr2unicode(&attr_name);
903 attr_valueU = xmlstr2unicode(&attr_value);
904 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU, &attr_valueU);
905 }
906 }
907
908 if (error || end) return end;
909
910 while ((ret = next_xml_elem(xmlbuf, &elem)))
911 {
912 if (xmlstr_cmp_end(&elem, comClassW))
913 {
914 ret = parse_end_element(xmlbuf);
915 break;
916 }
917 else
918 {
919 attr_nameU = xmlstr2unicode(&elem);
920 DPRINT1("unknown elem %wZ\n", &attr_nameU);
921 ret = parse_unknown_elem(xmlbuf, &elem);
922 }
923 }
924 return ret;
925 }
926
927 static BOOL parse_cominterface_proxy_stub_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
928 {
929 xmlstr_t attr_name, attr_value;
930 BOOL end = FALSE, error;
931 struct entity* entity;
932 UNICODE_STRING attr_valueU, attr_nameU;
933
934 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION)))
935 return FALSE;
936
937 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
938 {
939 if (xmlstr_cmp(&attr_name, iidW))
940 {
941 if (!(entity->u.proxy.iid = xmlstrdupW(&attr_value))) return FALSE;
942 }
943 if (xmlstr_cmp(&attr_name, g_nameW))
944 {
945 if (!(entity->u.proxy.name = xmlstrdupW(&attr_value))) return FALSE;
946 }
947 else
948 {
949 attr_nameU = xmlstr2unicode(&attr_name);
950 attr_valueU = xmlstr2unicode(&attr_value);
951 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU, &attr_valueU);
952 }
953 }
954
955 if (error || end) return end;
956 return parse_expect_end_elem(xmlbuf, comInterfaceProxyStubW);
957 }
958
959 static BOOL parse_typelib_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
960 {
961 xmlstr_t attr_name, attr_value;
962 BOOL end = FALSE, error;
963 struct entity* entity;
964 UNICODE_STRING attr_valueU, attr_nameU;
965
966 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION)))
967 return FALSE;
968
969 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
970 {
971 if (xmlstr_cmp(&attr_name, tlbidW))
972 {
973 if (!(entity->u.typelib.tlbid = xmlstrdupW(&attr_value))) return FALSE;
974 }
975 if (xmlstr_cmp(&attr_name, versionW))
976 {
977 if (!(entity->u.typelib.version = xmlstrdupW(&attr_value))) return FALSE;
978 }
979 if (xmlstr_cmp(&attr_name, helpdirW))
980 {
981 if (!(entity->u.typelib.helpdir = xmlstrdupW(&attr_value))) return FALSE;
982 }
983 else
984 {
985 attr_nameU = xmlstr2unicode(&attr_name);
986 attr_valueU = xmlstr2unicode(&attr_value);
987 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU, &attr_valueU);
988 }
989 }
990
991 if (error || end) return end;
992 return parse_expect_end_elem(xmlbuf, typelibW);
993 }
994
995 static BOOL parse_window_class_elem(xmlbuf_t* xmlbuf, struct dll_redirect* dll)
996 {
997 xmlstr_t elem, content;
998 BOOL end = FALSE, ret = TRUE;
999 struct entity* entity;
1000 UNICODE_STRING elemU;
1001
1002 if (!(entity = add_entity(&dll->entities, ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION)))
1003 return FALSE;
1004
1005 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1006 if (end) return FALSE;
1007
1008 if (!parse_text_content(xmlbuf, &content)) return FALSE;
1009
1010 if (!(entity->u.class.name = xmlstrdupW(&content))) return FALSE;
1011
1012 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1013 {
1014 if (xmlstr_cmp_end(&elem, windowClassW))
1015 {
1016 ret = parse_end_element(xmlbuf);
1017 break;
1018 }
1019 else
1020 {
1021 elemU = xmlstr2unicode(&elem);
1022 DPRINT1("unknown elem %wZ\n", &elemU);
1023 ret = parse_unknown_elem(xmlbuf, &elem);
1024 }
1025 }
1026
1027 return ret;
1028 }
1029
1030 static BOOL parse_binding_redirect_elem(xmlbuf_t* xmlbuf)
1031 {
1032 xmlstr_t attr_name, attr_value;
1033 UNICODE_STRING attr_valueU, attr_nameU;
1034 BOOL end = FALSE, error;
1035
1036 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1037 {
1038 attr_nameU = xmlstr2unicode(&attr_name);
1039 attr_valueU = xmlstr2unicode(&attr_value);
1040
1041 if (xmlstr_cmp(&attr_name, oldVersionW))
1042 {
1043 DPRINT1("Not stored yet oldVersion=%wZ\n", &attr_valueU);
1044 }
1045 else if (xmlstr_cmp(&attr_name, newVersionW))
1046 {
1047 DPRINT1("Not stored yet newVersion=%wZ\n", &attr_valueU);
1048 }
1049 else
1050 {
1051 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU, &attr_valueU);
1052 }
1053 }
1054
1055 if (error || end) return end;
1056 return parse_expect_end_elem(xmlbuf, bindingRedirectW);
1057 }
1058
1059 static BOOL parse_description_elem(xmlbuf_t* xmlbuf)
1060 {
1061 xmlstr_t elem, content;
1062 UNICODE_STRING elemU;
1063 BOOL end = FALSE, ret = TRUE;
1064
1065 if (!parse_expect_no_attr(xmlbuf, &end) || end ||
1066 !parse_text_content(xmlbuf, &content))
1067 return FALSE;
1068
1069 elemU = xmlstr2unicode(&content);
1070 DPRINT("Got description %wZ\n", &elemU);
1071
1072 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1073 {
1074 if (xmlstr_cmp_end(&elem, descriptionW))
1075 {
1076 ret = parse_end_element(xmlbuf);
1077 break;
1078 }
1079 else
1080 {
1081 elemU = xmlstr2unicode(&elem);
1082 DPRINT1("unknown elem %wZ\n", &elemU);
1083 ret = parse_unknown_elem(xmlbuf, &elem);
1084 }
1085 }
1086
1087 return ret;
1088 }
1089
1090 static BOOL parse_com_interface_external_proxy_stub_elem(xmlbuf_t* xmlbuf,
1091 struct assembly* assembly)
1092 {
1093 xmlstr_t attr_name, attr_value;
1094 BOOL end = FALSE, error;
1095 struct entity* entity;
1096
1097 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION);
1098 if (!entity) return FALSE;
1099
1100 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1101 {
1102 if (xmlstr_cmp(&attr_name, iidW))
1103 {
1104 if (!(entity->u.proxy.iid = xmlstrdupW(&attr_value))) return FALSE;
1105 }
1106 if (xmlstr_cmp(&attr_name, g_nameW))
1107 {
1108 if (!(entity->u.proxy.name = xmlstrdupW(&attr_value))) return FALSE;
1109 }
1110 else
1111 {
1112 DPRINT1("unknown attr %S=%S\n", attr_name.ptr, attr_value.ptr);
1113 }
1114 }
1115
1116 if (error || end) return end;
1117 return parse_expect_end_elem(xmlbuf, comInterfaceExternalProxyStubW);
1118 }
1119
1120 static BOOL parse_clr_class_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1121 {
1122 xmlstr_t attr_name, attr_value;
1123 UNICODE_STRING attr_nameU, attr_valueU;
1124 BOOL end = FALSE, error;
1125 struct entity* entity;
1126
1127 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION);
1128 if (!entity) return FALSE;
1129
1130 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1131 {
1132 if (xmlstr_cmp(&attr_name, g_nameW))
1133 {
1134 if (!(entity->u.clrclass.name = xmlstrdupW(&attr_value))) return FALSE;
1135 }
1136 else if (xmlstr_cmp(&attr_name, clsidW))
1137 {
1138 if (!(entity->u.clrclass.clsid = xmlstrdupW(&attr_value))) return FALSE;
1139 }
1140 else
1141 {
1142 attr_nameU = xmlstr2unicode(&attr_name);
1143 attr_valueU = xmlstr2unicode(&attr_value);
1144 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU, &attr_valueU);
1145 }
1146 }
1147
1148 if (error || end) return end;
1149 return parse_expect_end_elem(xmlbuf, clrClassW);
1150 }
1151
1152 static BOOL parse_clr_surrogate_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1153 {
1154 xmlstr_t attr_name, attr_value;
1155 UNICODE_STRING attr_nameU, attr_valueU;
1156 BOOL end = FALSE, error;
1157 struct entity* entity;
1158
1159 entity = add_entity(&assembly->entities, ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES);
1160 if (!entity) return FALSE;
1161
1162 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1163 {
1164 if (xmlstr_cmp(&attr_name, g_nameW))
1165 {
1166 if (!(entity->u.clrsurrogate.name = xmlstrdupW(&attr_value))) return FALSE;
1167 }
1168 else if (xmlstr_cmp(&attr_name, clsidW))
1169 {
1170 if (!(entity->u.clrsurrogate.clsid = xmlstrdupW(&attr_value))) return FALSE;
1171 }
1172 else
1173 {
1174 attr_nameU = xmlstr2unicode(&attr_name);
1175 attr_valueU = xmlstr2unicode(&attr_value);
1176 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU, &attr_valueU);
1177 }
1178 }
1179
1180 if (error || end) return end;
1181 return parse_expect_end_elem(xmlbuf, clrSurrogateW);
1182 }
1183
1184 static BOOL parse_dependent_assembly_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl, BOOL optional)
1185 {
1186 struct assembly_identity ai;
1187 xmlstr_t elem;
1188 BOOL end = FALSE, ret = TRUE;
1189
1190 if (!parse_expect_no_attr(xmlbuf, &end) || end) return end;
1191
1192 memset(&ai, 0, sizeof(ai));
1193 ai.optional = optional;
1194
1195 if (!parse_expect_elem(xmlbuf, assemblyIdentityW) ||
1196 !parse_assembly_identity_elem(xmlbuf, acl->actctx, &ai))
1197 return FALSE;
1198
1199 /* store the newly found identity for later loading */
1200 if (!add_dependent_assembly_id(acl, &ai)) return FALSE;
1201
1202 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1203 {
1204 if (xmlstr_cmp_end(&elem, dependentAssemblyW))
1205 {
1206 ret = parse_end_element(xmlbuf);
1207 break;
1208 }
1209 else if (xmlstr_cmp(&elem, bindingRedirectW))
1210 {
1211 ret = parse_binding_redirect_elem(xmlbuf);
1212 }
1213 else
1214 {
1215 DPRINT1("unknown elem %S\n", elem.ptr);
1216 ret = parse_unknown_elem(xmlbuf, &elem);
1217 }
1218 }
1219
1220 return ret;
1221 }
1222
1223 static BOOL parse_dependency_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl)
1224 {
1225 xmlstr_t attr_name, attr_value, elem;
1226 UNICODE_STRING attr_nameU, attr_valueU;
1227 BOOL end = FALSE, ret = TRUE, error, optional = FALSE;
1228
1229 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1230 {
1231 attr_nameU = xmlstr2unicode(&attr_name);
1232 attr_valueU = xmlstr2unicode(&attr_value);
1233
1234 if (xmlstr_cmp(&attr_name, optionalW))
1235 {
1236 static const WCHAR yesW[] = {'y','e','s',0};
1237 optional = xmlstr_cmpi( &attr_value, yesW );
1238 DPRINT1("optional=%wZ\n", &attr_valueU);
1239 }
1240 else
1241 {
1242 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU, &attr_valueU);
1243 }
1244 }
1245
1246 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1247 {
1248 if (xmlstr_cmp_end(&elem, dependencyW))
1249 {
1250 ret = parse_end_element(xmlbuf);
1251 break;
1252 }
1253 else if (xmlstr_cmp(&elem, dependentAssemblyW))
1254 {
1255 ret = parse_dependent_assembly_elem(xmlbuf, acl, optional);
1256 }
1257 else
1258 {
1259 attr_nameU = xmlstr2unicode(&elem);
1260 DPRINT1("unknown element %wZ\n", &attr_nameU);
1261 ret = parse_unknown_elem(xmlbuf, &elem);
1262 }
1263 }
1264
1265 return ret;
1266 }
1267
1268 static BOOL parse_noinherit_elem(xmlbuf_t* xmlbuf)
1269 {
1270 BOOL end = FALSE;
1271
1272 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1273 return end || parse_expect_end_elem(xmlbuf, noInheritW);
1274 }
1275
1276 static BOOL parse_noinheritable_elem(xmlbuf_t* xmlbuf)
1277 {
1278 BOOL end = FALSE;
1279
1280 if (!parse_expect_no_attr(xmlbuf, &end)) return FALSE;
1281 return end || parse_expect_end_elem(xmlbuf, noInheritableW);
1282 }
1283
1284 static BOOL parse_file_elem(xmlbuf_t* xmlbuf, struct assembly* assembly)
1285 {
1286 xmlstr_t attr_name, attr_value, elem;
1287 UNICODE_STRING attr_nameU, attr_valueU;
1288 BOOL end = FALSE, error, ret = TRUE;
1289 struct dll_redirect* dll;
1290
1291 if (!(dll = add_dll_redirect(assembly))) return FALSE;
1292
1293 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1294 {
1295 attr_nameU = xmlstr2unicode(&attr_name);
1296 attr_valueU = xmlstr2unicode(&attr_value);
1297
1298 if (xmlstr_cmp(&attr_name, g_nameW))
1299 {
1300 if (!(dll->name = xmlstrdupW(&attr_value))) return FALSE;
1301 DPRINT("name=%wZ\n", &attr_valueU);
1302 }
1303 else if (xmlstr_cmp(&attr_name, hashW))
1304 {
1305 if (!(dll->hash = xmlstrdupW(&attr_value))) return FALSE;
1306 }
1307 else if (xmlstr_cmp(&attr_name, hashalgW))
1308 {
1309 static const WCHAR sha1W[] = {'S','H','A','1',0};
1310 if (!xmlstr_cmpi(&attr_value, sha1W))
1311 DPRINT1("hashalg should be SHA1, got %wZ\n", &attr_valueU);
1312 }
1313 else
1314 {
1315 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU, &attr_valueU);
1316 }
1317 }
1318
1319 if (error || !dll->name) return FALSE;
1320 if (end) return TRUE;
1321
1322 while (ret && (ret = next_xml_elem(xmlbuf, &elem)))
1323 {
1324 if (xmlstr_cmp_end(&elem, fileW))
1325 {
1326 ret = parse_end_element(xmlbuf);
1327 break;
1328 }
1329 else if (xmlstr_cmp(&elem, comClassW))
1330 {
1331 ret = parse_com_class_elem(xmlbuf, dll);
1332 }
1333 else if (xmlstr_cmp(&elem, comInterfaceProxyStubW))
1334 {
1335 ret = parse_cominterface_proxy_stub_elem(xmlbuf, dll);
1336 }
1337 else if (xmlstr_cmp(&elem, asmv2hashW))
1338 {
1339 DPRINT1("asmv2hash (undocumented) not supported\n");
1340 ret = parse_unknown_elem(xmlbuf, &elem);
1341 }
1342 else if (xmlstr_cmp(&elem, typelibW))
1343 {
1344 ret = parse_typelib_elem(xmlbuf, dll);
1345 }
1346 else if (xmlstr_cmp(&elem, windowClassW))
1347 {
1348 ret = parse_window_class_elem(xmlbuf, dll);
1349 }
1350 else
1351 {
1352 attr_nameU = xmlstr2unicode(&elem);
1353 DPRINT1("unknown elem %wZ\n", &attr_nameU);
1354 ret = parse_unknown_elem( xmlbuf, &elem );
1355 }
1356 }
1357
1358 return ret;
1359 }
1360
1361 static BOOL parse_assembly_elem(xmlbuf_t* xmlbuf, struct actctx_loader* acl,
1362 struct assembly* assembly,
1363 struct assembly_identity* expected_ai)
1364 {
1365 xmlstr_t attr_name, attr_value, elem;
1366 UNICODE_STRING attr_nameU, attr_valueU;
1367 BOOL end = FALSE, error, version = FALSE, xmlns = FALSE, ret = TRUE;
1368
1369 while (next_xml_attr(xmlbuf, &attr_name, &attr_value, &error, &end))
1370 {
1371 attr_nameU = xmlstr2unicode(&attr_name);
1372 attr_valueU = xmlstr2unicode(&attr_value);
1373
1374 if (xmlstr_cmp(&attr_name, manifestVersionW))
1375 {
1376 static const WCHAR v10W[] = {'1','.','0',0};
1377 if (!xmlstr_cmp(&attr_value, v10W))
1378 {
1379 DPRINT1("wrong version %wZ\n", &attr_valueU);
1380 return FALSE;
1381 }
1382 version = TRUE;
1383 }
1384 else if (xmlstr_cmp(&attr_name, xmlnsW))
1385 {
1386 if (!xmlstr_cmp(&attr_value, manifestv1W) && !xmlstr_cmp(&attr_value, manifestv3W))
1387 {
1388 DPRINT1("wrong namespace %wZ\n", &attr_valueU);
1389 return FALSE;
1390 }
1391 xmlns = TRUE;
1392 }
1393 else
1394 {
1395 DPRINT1("unknown attr %wZ=%wZ\n", &attr_nameU, &attr_valueU);
1396 }
1397 }
1398
1399 if (error || end || !xmlns || !version) return FALSE;
1400 if (!next_xml_elem(xmlbuf, &elem)) return FALSE;
1401
1402 if (assembly->type == APPLICATION_MANIFEST && xmlstr_cmp(&elem, noInheritW))
1403 {
1404 if (!parse_noinherit_elem(xmlbuf) || !next_xml_elem(xmlbuf, &elem))
1405 return FALSE;
1406 assembly->no_inherit = TRUE;
1407 }
1408
1409 if (xmlstr_cmp(&elem, noInheritableW))
1410 {
1411 if (!parse_noinheritable_elem(xmlbuf) || !next_xml_elem(xmlbuf, &elem))
1412 return FALSE;
1413 }
1414 else if ((assembly->type == ASSEMBLY_MANIFEST || assembly->type == ASSEMBLY_SHARED_MANIFEST) &&
1415 assembly->no_inherit)
1416 return FALSE;
1417
1418 while (ret)
1419 {
1420 if (xmlstr_cmp_end(&elem, assemblyW))
1421 {
1422 ret = parse_end_element(xmlbuf);
1423 break;
1424 }
1425 else if (xmlstr_cmp(&elem, descriptionW))
1426 {
1427 ret = parse_description_elem(xmlbuf);
1428 }
1429 else if (xmlstr_cmp(&elem, comInterfaceExternalProxyStubW))
1430 {
1431 ret = parse_com_interface_external_proxy_stub_elem(xmlbuf, assembly);
1432 }
1433 else if (xmlstr_cmp(&elem, dependencyW))
1434 {
1435 ret = parse_dependency_elem(xmlbuf, acl);
1436 }
1437 else if (xmlstr_cmp(&elem, fileW))
1438 {
1439 ret = parse_file_elem(xmlbuf, assembly);
1440 }
1441 else if (xmlstr_cmp(&elem, clrClassW))
1442 {
1443 ret = parse_clr_class_elem(xmlbuf, assembly);
1444 }
1445 else if (xmlstr_cmp(&elem, clrSurrogateW))
1446 {
1447 ret = parse_clr_surrogate_elem(xmlbuf, assembly);
1448 }
1449 else if (xmlstr_cmp(&elem, assemblyIdentityW))
1450 {
1451 if (!parse_assembly_identity_elem(xmlbuf, acl->actctx, &assembly->id)) return FALSE;
1452
1453 if (expected_ai)
1454 {
1455 /* FIXME: more tests */
1456 if (assembly->type == ASSEMBLY_MANIFEST &&
1457 memcmp(&assembly->id.version, &expected_ai->version, sizeof(assembly->id.version)))
1458 {
1459 DPRINT1("wrong version for assembly manifest: %u.%u.%u.%u / %u.%u.%u.%u\n",
1460 expected_ai->version.major, expected_ai->version.minor,
1461 expected_ai->version.build, expected_ai->version.revision,
1462 assembly->id.version.major, assembly->id.version.minor,
1463 assembly->id.version.build, assembly->id.version.revision);
1464 ret = FALSE;
1465 }
1466 else if (assembly->type == ASSEMBLY_SHARED_MANIFEST &&
1467 (assembly->id.version.major != expected_ai->version.major ||
1468 assembly->id.version.minor != expected_ai->version.minor ||
1469 assembly->id.version.build < expected_ai->version.build ||
1470 (assembly->id.version.build == expected_ai->version.build &&
1471 assembly->id.version.revision < expected_ai->version.revision)))
1472 {
1473 DPRINT1("wrong version for shared assembly manifest\n");
1474 ret = FALSE;
1475 }
1476 }
1477 }
1478 else
1479 {
1480 attr_nameU = xmlstr2unicode(&elem);
1481 DPRINT1("unknown element %wZ\n", &attr_nameU);
1482 ret = parse_unknown_elem(xmlbuf, &elem);
1483 }
1484 if (ret) ret = next_xml_elem(xmlbuf, &elem);
1485 }
1486
1487 return ret;
1488 }
1489
1490 static NTSTATUS parse_manifest_buffer( struct actctx_loader* acl, struct assembly *assembly,
1491 struct assembly_identity* ai, xmlbuf_t *xmlbuf )
1492 {
1493 xmlstr_t elem;
1494 UNICODE_STRING elemU;
1495
1496 if (!next_xml_elem(xmlbuf, &elem)) return STATUS_SXS_CANT_GEN_ACTCTX;
1497
1498 if (xmlstr_cmp(&elem, g_xmlW) &&
1499 (!parse_xml_header(xmlbuf) || !next_xml_elem(xmlbuf, &elem)))
1500 return STATUS_SXS_CANT_GEN_ACTCTX;
1501
1502 if (!xmlstr_cmp(&elem, assemblyW))
1503 {
1504 elemU = xmlstr2unicode(&elem);
1505 DPRINT1("root element is %wZ, not <assembly>\n", &elemU);
1506 return STATUS_SXS_CANT_GEN_ACTCTX;
1507 }
1508
1509 if (!parse_assembly_elem(xmlbuf, acl, assembly, ai))
1510 {
1511 DPRINT1("failed to parse manifest %S\n", assembly->manifest.info );
1512 return STATUS_SXS_CANT_GEN_ACTCTX;
1513 }
1514
1515 if (next_xml_elem(xmlbuf, &elem))
1516 {
1517 elemU = xmlstr2unicode(&elem);
1518 DPRINT1("unexpected element %wZ\n", &elemU);
1519 return STATUS_SXS_CANT_GEN_ACTCTX;
1520 }
1521
1522 if (xmlbuf->ptr != xmlbuf->end)
1523 {
1524 DPRINT1("parse error\n");
1525 return STATUS_SXS_CANT_GEN_ACTCTX;
1526 }
1527 return STATUS_SUCCESS;
1528 }
1529
1530 static NTSTATUS parse_manifest( struct actctx_loader* acl, struct assembly_identity* ai,
1531 LPCWSTR filename, LPCWSTR directory, BOOL shared,
1532 const void *buffer, SIZE_T size )
1533 {
1534 xmlbuf_t xmlbuf;
1535 NTSTATUS status;
1536 struct assembly *assembly;
1537 int unicode_tests;
1538
1539 DPRINT( "parsing manifest loaded from %S base dir %S\n", filename, directory );
1540
1541 if (!(assembly = add_assembly(acl->actctx, shared ? ASSEMBLY_SHARED_MANIFEST : ASSEMBLY_MANIFEST)))
1542 return STATUS_SXS_CANT_GEN_ACTCTX;
1543
1544 if (directory && !(assembly->directory = strdupW(directory)))
1545 return STATUS_NO_MEMORY;
1546
1547 if (filename) assembly->manifest.info = strdupW( filename + 4 /* skip \??\ prefix */ );
1548 assembly->manifest.type = assembly->manifest.info ? ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE
1549 : ACTIVATION_CONTEXT_PATH_TYPE_NONE;
1550
1551 unicode_tests = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1552 if (RtlIsTextUnicode((PVOID)buffer, (ULONG)size, &unicode_tests ))
1553 {
1554 xmlbuf.ptr = buffer;
1555 xmlbuf.end = xmlbuf.ptr + size / sizeof(WCHAR);
1556 status = parse_manifest_buffer( acl, assembly, ai, &xmlbuf );
1557 }
1558 else if (unicode_tests & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
1559 {
1560 const WCHAR *buf = buffer;
1561 WCHAR *new_buff;
1562 unsigned int i;
1563
1564 if (!(new_buff = RtlAllocateHeap( RtlGetProcessHeap(), 0, size )))
1565 return STATUS_NO_MEMORY;
1566 for (i = 0; i < size / sizeof(WCHAR); i++)
1567 new_buff[i] = RtlUshortByteSwap( buf[i] );
1568 xmlbuf.ptr = new_buff;
1569 xmlbuf.end = xmlbuf.ptr + size / sizeof(WCHAR);
1570 status = parse_manifest_buffer( acl, assembly, ai, &xmlbuf );
1571 RtlFreeHeap( RtlGetProcessHeap(), 0, new_buff );
1572 }
1573 else
1574 {
1575 /* TODO: this doesn't handle arbitrary encodings */
1576 ANSI_STRING xmlA;
1577 UNICODE_STRING xmlW;
1578
1579 ASSERT(size < MAXUSHORT);
1580 xmlA.Buffer = (PCHAR)buffer;
1581 xmlA.Length = xmlA.MaximumLength = (USHORT)size;
1582
1583 _SEH2_TRY
1584 {
1585 status = RtlAnsiStringToUnicodeString(&xmlW, &xmlA, TRUE);
1586 }
1587 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1588 {
1589 DPRINT1("Exception accessing buffer\n");
1590 _SEH2_YIELD(return STATUS_SXS_CANT_GEN_ACTCTX);
1591 }
1592 _SEH2_END;
1593
1594 if (!NT_SUCCESS(status))
1595 {
1596 DPRINT1("RtlAnsiStringToUnicodeString failed with %lx\n", status);
1597 return STATUS_SXS_CANT_GEN_ACTCTX;
1598 }
1599 ASSERT(xmlW.Buffer != NULL);
1600
1601 xmlbuf.ptr = xmlW.Buffer;
1602 xmlbuf.end = xmlbuf.ptr + xmlW.Length / sizeof(WCHAR);
1603 status = parse_manifest_buffer( acl, assembly, ai, &xmlbuf );
1604
1605 RtlFreeUnicodeString(&xmlW);
1606 }
1607 return status;
1608 }
1609
1610 static NTSTATUS open_nt_file( HANDLE *handle, UNICODE_STRING *name )
1611 {
1612 OBJECT_ATTRIBUTES attr;
1613 IO_STATUS_BLOCK io;
1614
1615 attr.Length = sizeof(attr);
1616 attr.RootDirectory = 0;
1617 attr.Attributes = OBJ_CASE_INSENSITIVE;
1618 attr.ObjectName = name;
1619 attr.SecurityDescriptor = NULL;
1620 attr.SecurityQualityOfService = NULL;
1621 return NtOpenFile(handle,
1622 GENERIC_READ | SYNCHRONIZE,
1623 &attr, &io,
1624 FILE_SHARE_READ,
1625 FILE_SYNCHRONOUS_IO_ALERT);
1626 }
1627
1628 static NTSTATUS get_module_filename( HMODULE module, UNICODE_STRING *str, USHORT extra_len )
1629 {
1630 NTSTATUS status;
1631 ULONG magic;
1632 LDR_DATA_TABLE_ENTRY *pldr;
1633
1634 LdrLockLoaderLock(0, NULL, &magic);
1635 status = LdrFindEntryForAddress( module, &pldr );
1636 if (status == STATUS_SUCCESS)
1637 {
1638 if ((str->Buffer = RtlAllocateHeap( RtlGetProcessHeap(), 0,
1639 pldr->FullDllName.Length + extra_len + sizeof(WCHAR) )))
1640 {
1641 memcpy( str->Buffer, pldr->FullDllName.Buffer, pldr->FullDllName.Length + sizeof(WCHAR) );
1642 str->Length = pldr->FullDllName.Length;
1643 str->MaximumLength = pldr->FullDllName.Length + extra_len + sizeof(WCHAR);
1644 }
1645 else status = STATUS_NO_MEMORY;
1646 }
1647 LdrUnlockLoaderLock(0, magic);
1648 return status;
1649 }
1650
1651 static NTSTATUS get_manifest_in_module( struct actctx_loader* acl, struct assembly_identity* ai,
1652 LPCWSTR filename, LPCWSTR directory, BOOL shared,
1653 HANDLE hModule, LPCWSTR resname, ULONG lang )
1654 {
1655 NTSTATUS status;
1656 UNICODE_STRING nameW;
1657 LDR_RESOURCE_INFO info;
1658 IMAGE_RESOURCE_DATA_ENTRY* entry = NULL;
1659 void *ptr;
1660
1661 //DPRINT( "looking for res %s in module %p %s\n", resname,
1662 // hModule, filename );
1663
1664 #if 0
1665 if (TRACE_ON(actctx))
1666 {
1667 if (!filename && !get_module_filename( hModule, &nameW, 0 ))
1668 {
1669 DPRINT( "looking for res %s in module %p %s\n", debugstr_w(resname),
1670 hModule, debugstr_w(nameW.Buffer) );
1671 RtlFreeUnicodeString( &nameW );
1672 }
1673 else DPRINT( "looking for res %s in module %p %s\n", debugstr_w(resname),
1674 hModule, debugstr_w(filename) );
1675 }
1676 #endif
1677
1678 if (!resname) return STATUS_INVALID_PARAMETER;
1679
1680 info.Type = (ULONG_PTR)RT_MANIFEST;
1681 info.Language = lang;
1682 if (!((ULONG_PTR)resname >> 16))
1683 {
1684 info.Name = (ULONG_PTR)resname;
1685 status = LdrFindResource_U(hModule, &info, 3, &entry);
1686 }
1687 else if (resname[0] == '#')
1688 {
1689 ULONG value;
1690 RtlInitUnicodeString(&nameW, resname + 1);
1691 if (RtlUnicodeStringToInteger(&nameW, 10, &value) != STATUS_SUCCESS || HIWORD(value))
1692 return STATUS_INVALID_PARAMETER;
1693 info.Name = value;
1694 status = LdrFindResource_U(hModule, &info, 3, &entry);
1695 }
1696 else
1697 {
1698 RtlCreateUnicodeString(&nameW, resname);
1699 RtlUpcaseUnicodeString(&nameW, &nameW, FALSE);
1700 info.Name = (ULONG_PTR)nameW.Buffer;
1701 status = LdrFindResource_U(hModule, &info, 3, &entry);
1702 RtlFreeUnicodeString(&nameW);
1703 }
1704 if (status == STATUS_SUCCESS) status = LdrAccessResource(hModule, entry, &ptr, NULL);
1705
1706 if (status == STATUS_SUCCESS)
1707 status = parse_manifest(acl, ai, filename, directory, shared, ptr, entry->Size);
1708
1709 return status;
1710 }
1711
1712 static NTSTATUS get_manifest_in_pe_file( struct actctx_loader* acl, struct assembly_identity* ai,
1713 LPCWSTR filename, LPCWSTR directory, BOOL shared,
1714 HANDLE file, LPCWSTR resname, ULONG lang )
1715 {
1716 HANDLE mapping;
1717 OBJECT_ATTRIBUTES attr;
1718 LARGE_INTEGER size;
1719 LARGE_INTEGER offset;
1720 NTSTATUS status;
1721 SIZE_T count;
1722 void *base;
1723
1724 DPRINT( "looking for res %S in %S\n", resname, filename );
1725
1726 attr.Length = sizeof(attr);
1727 attr.RootDirectory = 0;
1728 attr.ObjectName = NULL;
1729 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1730 attr.SecurityDescriptor = NULL;
1731 attr.SecurityQualityOfService = NULL;
1732
1733 size.QuadPart = 0;
1734 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
1735 &attr, &size, PAGE_READONLY, SEC_COMMIT, file );
1736 if (status != STATUS_SUCCESS) return status;
1737
1738 offset.QuadPart = 0;
1739 count = 0;
1740 base = NULL;
1741 status = NtMapViewOfSection( mapping, NtCurrentProcess(), &base, 0, 0, &offset,
1742 &count, ViewShare, 0, PAGE_READONLY );
1743 NtClose( mapping );
1744 if (status != STATUS_SUCCESS) return status;
1745
1746 if (RtlImageNtHeader(base)) /* we got a PE file */
1747 {
1748 HANDLE module = (HMODULE)((ULONG_PTR)base | 1); /* make it a LOAD_LIBRARY_AS_DATAFILE handle */
1749 status = get_manifest_in_module( acl, ai, filename, directory, shared, module, resname, lang );
1750 }
1751 else status = STATUS_INVALID_IMAGE_FORMAT;
1752
1753 NtUnmapViewOfSection( NtCurrentProcess(), base );
1754 return status;
1755 }
1756
1757 static NTSTATUS get_manifest_in_manifest_file( struct actctx_loader* acl, struct assembly_identity* ai,
1758 LPCWSTR filename, LPCWSTR directory, BOOL shared, HANDLE file )
1759 {
1760 FILE_STANDARD_INFORMATION info;
1761 IO_STATUS_BLOCK io;
1762 HANDLE mapping;
1763 OBJECT_ATTRIBUTES attr;
1764 LARGE_INTEGER size;
1765 LARGE_INTEGER offset;
1766 NTSTATUS status;
1767 SIZE_T count;
1768 void *base;
1769
1770 DPRINT( "loading manifest file %S\n", filename );
1771
1772 attr.Length = sizeof(attr);
1773 attr.RootDirectory = 0;
1774 attr.ObjectName = NULL;
1775 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1776 attr.SecurityDescriptor = NULL;
1777 attr.SecurityQualityOfService = NULL;
1778
1779 size.QuadPart = 0;
1780 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
1781 &attr, &size, PAGE_READONLY, SEC_COMMIT, file );
1782
1783 if (status != STATUS_SUCCESS) return status;
1784
1785 offset.QuadPart = 0;
1786 count = 0;
1787 base = NULL;
1788 status = NtMapViewOfSection( mapping, NtCurrentProcess(), &base, 0, 0, &offset,
1789 &count, ViewShare, 0, PAGE_READONLY );
1790
1791 if (status != STATUS_SUCCESS) return status;
1792
1793 /* Fixme: WINE uses FileEndOfFileInformation with NtQueryInformationFile. */
1794 status = NtQueryInformationFile( file, &io, &info, sizeof(info), FileStandardInformation);
1795
1796 if (status == STATUS_SUCCESS)
1797 status = parse_manifest(acl, ai, filename, directory, shared, base, (SIZE_T)info.EndOfFile.QuadPart);
1798
1799 NtUnmapViewOfSection( NtCurrentProcess(), base );
1800 NtClose( mapping );
1801 return status;
1802 }
1803
1804 /* try to load the .manifest file associated to the file */
1805 static NTSTATUS get_manifest_in_associated_manifest( struct actctx_loader* acl, struct assembly_identity* ai,
1806 LPCWSTR filename, LPCWSTR directory, HMODULE module, LPCWSTR resname )
1807 {
1808 static const WCHAR fmtW[] = { '.','%','l','u',0 };
1809 WCHAR *buffer;
1810 NTSTATUS status;
1811 UNICODE_STRING nameW;
1812 HANDLE file;
1813 ULONG_PTR resid = (ULONG_PTR)CREATEPROCESS_MANIFEST_RESOURCE_ID;
1814
1815 if (!((ULONG_PTR)resname >> 16)) resid = (ULONG_PTR)resname & 0xffff;
1816
1817 DPRINT( "looking for manifest associated with %S id %lu\n", filename, resid );
1818
1819 if (module) /* use the module filename */
1820 {
1821 UNICODE_STRING name;
1822
1823 if (!(status = get_module_filename( module, &name, sizeof(dotManifestW) + 10*sizeof(WCHAR) )))
1824 {
1825 if (resid != 1) sprintfW( name.Buffer + strlenW(name.Buffer), fmtW, resid );
1826 strcatW( name.Buffer, dotManifestW );
1827 if (!RtlDosPathNameToNtPathName_U( name.Buffer, &nameW, NULL, NULL ))
1828 status = STATUS_RESOURCE_DATA_NOT_FOUND;
1829 RtlFreeUnicodeString( &name );
1830 }
1831 if (status) return status;
1832 }
1833 else
1834 {
1835 if (!(buffer = RtlAllocateHeap( RtlGetProcessHeap(), 0,
1836 (strlenW(filename) + 10) * sizeof(WCHAR) + sizeof(dotManifestW) )))
1837 return STATUS_NO_MEMORY;
1838 strcpyW( buffer, filename );
1839 if (resid != 1) sprintfW( buffer + strlenW(buffer), fmtW, resid );
1840 strcatW( buffer, dotManifestW );
1841 RtlInitUnicodeString( &nameW, buffer );
1842 }
1843
1844 if (!open_nt_file( &file, &nameW ))
1845 {
1846 status = get_manifest_in_manifest_file( acl, ai, nameW.Buffer, directory, FALSE, file );
1847 NtClose( file );
1848 }
1849 else status = STATUS_RESOURCE_DATA_NOT_FOUND;
1850 RtlFreeUnicodeString( &nameW );
1851 return status;
1852 }
1853
1854 static WCHAR *lookup_manifest_file( HANDLE dir, struct assembly_identity *ai )
1855 {
1856 static const WCHAR lookup_fmtW[] =
1857 {'%','s','_','%','s','_','%','s','_','%','u','.','%','u','.','*','.','*','_',
1858 '*', /* FIXME */
1859 '.','m','a','n','i','f','e','s','t',0};
1860
1861 WCHAR *lookup, *ret = NULL;
1862 UNICODE_STRING lookup_us;
1863 IO_STATUS_BLOCK io;
1864 unsigned int data_pos = 0, data_len;
1865 char buffer[8192];
1866
1867 if (!(lookup = RtlAllocateHeap( RtlGetProcessHeap(), 0,
1868 (strlenW(ai->arch) + strlenW(ai->name)
1869 + strlenW(ai->public_key) + 20) * sizeof(WCHAR)
1870 + sizeof(lookup_fmtW) )))
1871 return NULL;
1872
1873 sprintfW( lookup, lookup_fmtW, ai->arch, ai->name, ai->public_key, ai->version.major, ai->version.minor);
1874 RtlInitUnicodeString( &lookup_us, lookup );
1875
1876 NtQueryDirectoryFile( dir, 0, NULL, NULL, &io, buffer, sizeof(buffer),
1877 FileBothDirectoryInformation, FALSE, &lookup_us, TRUE );
1878 if (io.Status == STATUS_SUCCESS)
1879 {
1880 FILE_BOTH_DIR_INFORMATION *dir_info;
1881 WCHAR *tmp;
1882 ULONG build, revision;
1883
1884 data_len = (ULONG)io.Information;
1885
1886 for (;;)
1887 {
1888 if (data_pos >= data_len)
1889 {
1890 NtQueryDirectoryFile( dir, 0, NULL, NULL, &io, buffer, sizeof(buffer),
1891 FileBothDirectoryInformation, FALSE, &lookup_us, FALSE );
1892 if (io.Status != STATUS_SUCCESS) break;
1893 data_len = (ULONG)io.Information;
1894 data_pos = 0;
1895 }
1896 dir_info = (FILE_BOTH_DIR_INFORMATION*)(buffer + data_pos);
1897
1898 if (dir_info->NextEntryOffset) data_pos += dir_info->NextEntryOffset;
1899 else data_pos = data_len;
1900
1901 tmp = (WCHAR *)dir_info->FileName + (strchrW(lookup, '*') - lookup);
1902 build = atoiW(tmp);
1903 if (build < ai->version.build) continue;
1904 tmp = strchrW(tmp, '.') + 1;
1905 revision = atoiW(tmp);
1906 if (build == ai->version.build && revision < ai->version.revision)
1907 continue;
1908 ai->version.build = (USHORT)build;
1909 ai->version.revision = (USHORT)revision;
1910
1911 if ((ret = RtlAllocateHeap( RtlGetProcessHeap(), 0, dir_info->FileNameLength * sizeof(WCHAR) )))
1912 {
1913 memcpy( ret, dir_info->FileName, dir_info->FileNameLength );
1914 ret[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
1915 }
1916 break;
1917 }
1918 }
1919 else DPRINT1("no matching file for %S\n", lookup);
1920 RtlFreeHeap( RtlGetProcessHeap(), 0, lookup );
1921 return ret;
1922 }
1923
1924 static NTSTATUS lookup_winsxs(struct actctx_loader* acl, struct assembly_identity* ai)
1925 {
1926 struct assembly_identity sxs_ai;
1927 UNICODE_STRING path_us;
1928 OBJECT_ATTRIBUTES attr;
1929 IO_STATUS_BLOCK io;
1930 WCHAR *path, *file = NULL;
1931 HANDLE handle;
1932
1933 static const WCHAR manifest_dirW[] =
1934 {'\\','w','i','n','s','x','s','\\','m','a','n','i','f','e','s','t','s',0};
1935
1936 if (!ai->arch || !ai->name || !ai->public_key) return STATUS_NO_SUCH_FILE;
1937
1938 if (!(path = RtlAllocateHeap( RtlGetProcessHeap(), 0,
1939 ((strlenW(SharedUserData->NtSystemRoot) + 1) *sizeof(WCHAR)) + sizeof(manifest_dirW) )))
1940 return STATUS_NO_MEMORY;
1941
1942 memcpy( path, SharedUserData->NtSystemRoot, strlenW(SharedUserData->NtSystemRoot) * sizeof(WCHAR) );
1943 memcpy( path + strlenW(SharedUserData->NtSystemRoot), manifest_dirW, sizeof(manifest_dirW) );
1944
1945 if (!RtlDosPathNameToNtPathName_U( path, &path_us, NULL, NULL ))
1946 {
1947 RtlFreeHeap( RtlGetProcessHeap(), 0, path );
1948 return STATUS_NO_SUCH_FILE;
1949 }
1950 RtlFreeHeap( RtlGetProcessHeap(), 0, path );
1951
1952 attr.Length = sizeof(attr);
1953 attr.RootDirectory = 0;
1954 attr.Attributes = OBJ_CASE_INSENSITIVE;
1955 attr.ObjectName = &path_us;
1956 attr.SecurityDescriptor = NULL;
1957 attr.SecurityQualityOfService = NULL;
1958
1959 if (!NtOpenFile(&handle,
1960 GENERIC_READ | SYNCHRONIZE,
1961 &attr, &io,
1962 FILE_SHARE_READ | FILE_SHARE_WRITE,
1963 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT))
1964 {
1965 sxs_ai = *ai;
1966 file = lookup_manifest_file( handle, &sxs_ai );
1967 NtClose( handle );
1968 }
1969 if (!file)
1970 {
1971 RtlFreeUnicodeString( &path_us );
1972 return STATUS_NO_SUCH_FILE;
1973 }
1974
1975 /* append file name to directory path */
1976 if (!(path = RtlReAllocateHeap( RtlGetProcessHeap(), 0, path_us.Buffer,
1977 path_us.Length + (strlenW(file) + 2) * sizeof(WCHAR) )))
1978 {
1979 RtlFreeHeap( RtlGetProcessHeap(), 0, file );
1980 RtlFreeUnicodeString( &path_us );
1981 return STATUS_NO_MEMORY;
1982 }
1983
1984 path[path_us.Length/sizeof(WCHAR)] = '\\';
1985 strcpyW( path + path_us.Length/sizeof(WCHAR) + 1, file );
1986 RtlInitUnicodeString( &path_us, path );
1987 *strrchrW(file, '.') = 0; /* remove .manifest extension */
1988
1989 if (!open_nt_file( &handle, &path_us ))
1990 {
1991 io.Status = get_manifest_in_manifest_file(acl, &sxs_ai, path_us.Buffer, file, TRUE, handle);
1992 NtClose( handle );
1993 }
1994 else io.Status = STATUS_NO_SUCH_FILE;
1995
1996 RtlFreeHeap( RtlGetProcessHeap(), 0, file );
1997 RtlFreeUnicodeString( &path_us );
1998 return io.Status;
1999 }
2000
2001 static NTSTATUS lookup_assembly(struct actctx_loader* acl,
2002 struct assembly_identity* ai)
2003 {
2004 static const WCHAR dotDllW[] = {'.','d','l','l',0};
2005 unsigned int i;
2006 WCHAR *buffer, *p, *directory;
2007 NTSTATUS status;
2008 UNICODE_STRING nameW;
2009 HANDLE file;
2010
2011 DPRINT( "looking for name=%S version=%u.%u.%u.%u arch=%S\n",
2012 ai->name, ai->version.major, ai->version.minor, ai->version.build, ai->version.revision, ai->arch );
2013
2014 if ((status = lookup_winsxs(acl, ai)) != STATUS_NO_SUCH_FILE) return status;
2015
2016 /* FIXME: add support for language specific lookup */
2017
2018 nameW.Buffer = NULL;
2019 if (!(buffer = RtlAllocateHeap( RtlGetProcessHeap(), 0,
2020 (strlenW(acl->actctx->appdir.info) + 2 * strlenW(ai->name) + 2) * sizeof(WCHAR) + sizeof(dotManifestW) )))
2021 return STATUS_NO_MEMORY;
2022
2023 if (!(directory = build_assembly_dir( ai )))
2024 {
2025 RtlFreeHeap( RtlGetProcessHeap(), 0, buffer );
2026 return STATUS_NO_MEMORY;
2027 }
2028
2029 /* lookup in appdir\name.dll
2030 * appdir\name.manifest
2031 * appdir\name\name.dll
2032 * appdir\name\name.manifest
2033 */
2034 strcpyW( buffer, acl->actctx->appdir.info );
2035 p = buffer + strlenW(buffer);
2036 for (i = 0; i < 2; i++)
2037 {
2038 *p++ = '\\';
2039 strcpyW( p, ai->name );
2040 p += strlenW(p);
2041
2042 strcpyW( p, dotDllW );
2043 if (RtlDosPathNameToNtPathName_U( buffer, &nameW, NULL, NULL ))
2044 {
2045 status = open_nt_file( &file, &nameW );
2046 if (!status)
2047 {
2048 status = get_manifest_in_pe_file( acl, ai, nameW.Buffer, directory, FALSE, file,
2049 (LPCWSTR)CREATEPROCESS_MANIFEST_RESOURCE_ID, 0 );
2050 NtClose( file );
2051 break;
2052 }
2053 RtlFreeUnicodeString( &nameW );
2054 }
2055
2056 strcpyW( p, dotManifestW );
2057 if (RtlDosPathNameToNtPathName_U( buffer, &nameW, NULL, NULL ))
2058 {
2059 status = open_nt_file( &file, &nameW );
2060 if (!status)
2061 {
2062 status = get_manifest_in_manifest_file( acl, ai, nameW.Buffer, directory, FALSE, file );
2063 NtClose( file );
2064 break;
2065 }
2066 RtlFreeUnicodeString( &nameW );
2067 }
2068 status = STATUS_SXS_ASSEMBLY_NOT_FOUND;
2069 }
2070 RtlFreeUnicodeString( &nameW );
2071 RtlFreeHeap( RtlGetProcessHeap(), 0, directory );
2072 RtlFreeHeap( RtlGetProcessHeap(), 0, buffer );
2073 return status;
2074 }
2075
2076 static NTSTATUS parse_depend_manifests(struct actctx_loader* acl)
2077 {
2078 NTSTATUS status = STATUS_SUCCESS;
2079 unsigned int i;
2080
2081 for (i = 0; i < acl->num_dependencies; i++)
2082 {
2083 if (lookup_assembly(acl, &acl->dependencies[i]) != STATUS_SUCCESS)
2084 {
2085 if (!acl->dependencies[i].optional)
2086 {
2087 DPRINT1( "Could not find dependent assembly %S\n", acl->dependencies[i].name );
2088 status = STATUS_SXS_CANT_GEN_ACTCTX;
2089 break;
2090 }
2091 }
2092 }
2093 /* FIXME should now iterate through all refs */
2094 return status;
2095 }
2096
2097 /* find the appropriate activation context for RtlQueryInformationActivationContext */
2098 static NTSTATUS find_query_actctx( HANDLE *handle, DWORD flags, ULONG class )
2099 {
2100 NTSTATUS status = STATUS_SUCCESS;
2101
2102 if (flags & RTL_QUERY_ACTIVATION_CONTEXT_FLAG_USE_ACTIVE_ACTIVATION_CONTEXT)
2103 {
2104 if (*handle) return STATUS_INVALID_PARAMETER;
2105
2106 if (NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame)
2107 *handle = NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame->ActivationContext;
2108 }
2109 else if (flags & (RTL_QUERY_ACTIVATION_CONTEXT_FLAG_IS_ADDRESS | RTL_QUERY_ACTIVATION_CONTEXT_FLAG_IS_HMODULE))
2110 {
2111 ULONG magic;
2112 LDR_DATA_TABLE_ENTRY *pldr;
2113
2114 if (!*handle) return STATUS_INVALID_PARAMETER;
2115
2116 LdrLockLoaderLock( 0, NULL, &magic );
2117 if (!LdrFindEntryForAddress( *handle, &pldr ))
2118 {
2119 if ((flags & RTL_QUERY_ACTIVATION_CONTEXT_FLAG_IS_HMODULE) && *handle != pldr->DllBase)
2120 status = STATUS_DLL_NOT_FOUND;
2121 else
2122 *handle = pldr->EntryPointActivationContext;
2123 }
2124 else status = STATUS_DLL_NOT_FOUND;
2125 LdrUnlockLoaderLock( 0, magic );
2126 }
2127 else if (!*handle && (class != ActivationContextBasicInformation))
2128 *handle = process_actctx;
2129
2130 return status;
2131 }
2132
2133 static NTSTATUS fill_keyed_data(PACTCTX_SECTION_KEYED_DATA data, PVOID v1, PVOID v2, unsigned int i)
2134 {
2135 data->ulDataFormatVersion = 1;
2136 data->lpData = v1;
2137 data->ulLength = 20; /* FIXME */
2138 data->lpSectionGlobalData = NULL; /* FIXME */
2139 data->ulSectionGlobalDataLength = 0; /* FIXME */
2140 data->lpSectionBase = v2;
2141 data->ulSectionTotalLength = 0; /* FIXME */
2142 data->hActCtx = NULL;
2143 if (data->cbSize >= offsetof(ACTCTX_SECTION_KEYED_DATA, ulAssemblyRosterIndex) + sizeof(ULONG))
2144 data->ulAssemblyRosterIndex = i + 1;
2145
2146 return STATUS_SUCCESS;
2147 }
2148
2149 static NTSTATUS find_dll_redirection(ACTIVATION_CONTEXT* actctx, const UNICODE_STRING *section_name,
2150 PACTCTX_SECTION_KEYED_DATA data)
2151 {
2152 unsigned int i, j, snlen = section_name->Length / sizeof(WCHAR);
2153
2154 for (i = 0; i < actctx->num_assemblies; i++)
2155 {
2156 struct assembly *assembly = &actctx->assemblies[i];
2157 for (j = 0; j < assembly->num_dlls; j++)
2158 {
2159 struct dll_redirect *dll = &assembly->dlls[j];
2160 if (!strncmpiW(section_name->Buffer, dll->name, snlen) && !dll->name[snlen])
2161 return fill_keyed_data(data, dll, assembly, i);
2162 }
2163 }
2164 return STATUS_SXS_KEY_NOT_FOUND;
2165 }
2166
2167 static NTSTATUS find_window_class(ACTIVATION_CONTEXT* actctx, const UNICODE_STRING *section_name,
2168 PACTCTX_SECTION_KEYED_DATA data)
2169 {
2170 unsigned int i, j, k, snlen = section_name->Length / sizeof(WCHAR);
2171
2172 for (i = 0; i < actctx->num_assemblies; i++)
2173 {
2174 struct assembly *assembly = &actctx->assemblies[i];
2175 for (j = 0; j < assembly->num_dlls; j++)
2176 {
2177 struct dll_redirect *dll = &assembly->dlls[j];
2178 for (k = 0; k < dll->entities.num; k++)
2179 {
2180 struct entity *entity = &dll->entities.base[k];
2181 if (entity->kind == ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION)
2182 {
2183 if (!strncmpiW(section_name->Buffer, entity->u.class.name, snlen) && !entity->u.class.name[snlen])
2184 return fill_keyed_data(data, entity, dll, i);
2185 }
2186 }
2187 }
2188 }
2189 return STATUS_SXS_KEY_NOT_FOUND;
2190 }
2191
2192 static NTSTATUS find_string(ACTIVATION_CONTEXT* actctx, ULONG section_kind,
2193 const UNICODE_STRING *section_name,
2194 DWORD flags, PACTCTX_SECTION_KEYED_DATA data)
2195 {
2196 NTSTATUS status;
2197
2198 switch (section_kind)
2199 {
2200 case ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION:
2201 status = find_dll_redirection(actctx, section_name, data);
2202 break;
2203 case ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION:
2204 status = find_window_class(actctx, section_name, data);
2205 break;
2206 case ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION:
2207 case ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION:
2208 case ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION:
2209 case ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION:
2210 case ACTIVATION_CONTEXT_SECTION_GLOBAL_OBJECT_RENAME_TABLE:
2211 case ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES:
2212 DPRINT1("Unsupported yet section_kind %x\n", section_kind);
2213 return STATUS_SXS_SECTION_NOT_FOUND;
2214 default:
2215 DPRINT1("Unknown section_kind %x\n", section_kind);
2216 return STATUS_SXS_SECTION_NOT_FOUND;
2217 }
2218
2219 if (status != STATUS_SUCCESS) return status;
2220
2221 if (flags & FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX)
2222 {
2223 actctx_addref(actctx);
2224 data->hActCtx = actctx;
2225 }
2226 return STATUS_SUCCESS;
2227 }
2228
2229 /* initialize the activation context for the current process */
2230 void actctx_init(void)
2231 {
2232 ACTCTXW ctx;
2233 HANDLE handle;
2234
2235 ctx.cbSize = sizeof(ctx);
2236 ctx.lpSource = NULL;
2237 ctx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID | ACTCTX_FLAG_HMODULE_VALID;
2238 ctx.hModule = NtCurrentTeb()->ProcessEnvironmentBlock->ImageBaseAddress;
2239 ctx.lpResourceName = (LPCWSTR)CREATEPROCESS_MANIFEST_RESOURCE_ID;
2240
2241 if (!RtlCreateActivationContext( &handle, &ctx )) process_actctx = check_actctx(handle);
2242 }
2243
2244 /* FUNCTIONS ***************************************************************/
2245
2246 NTSTATUS WINAPI RtlCreateActivationContext( HANDLE *handle, void *ptr )
2247 {
2248 const ACTCTXW *pActCtx = ptr;
2249 const WCHAR *directory = NULL;
2250 ACTIVATION_CONTEXT *actctx;
2251 UNICODE_STRING nameW;
2252 ULONG lang = 0;
2253 NTSTATUS status = STATUS_NO_MEMORY;
2254 HANDLE file = 0;
2255 struct actctx_loader acl;
2256
2257 DPRINT("%p %08x\n", pActCtx, pActCtx ? pActCtx->dwFlags : 0);
2258
2259 if (!pActCtx || pActCtx->cbSize < sizeof(*pActCtx) ||
2260 (pActCtx->dwFlags & ~ACTCTX_FLAGS_ALL))
2261 return STATUS_INVALID_PARAMETER;
2262
2263
2264 if (!(actctx = RtlAllocateHeap( RtlGetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*actctx) )))
2265 return STATUS_NO_MEMORY;
2266
2267 actctx->magic = ACTCTX_MAGIC;
2268 actctx->ref_count = 1;
2269 actctx->config.type = ACTIVATION_CONTEXT_PATH_TYPE_NONE;
2270 actctx->config.info = NULL;
2271 actctx->appdir.type = ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE;
2272 if (pActCtx->dwFlags & ACTCTX_FLAG_APPLICATION_NAME_VALID)
2273 {
2274 if (!(actctx->appdir.info = strdupW( pActCtx->lpApplicationName ))) goto error;
2275 }
2276 else
2277 {
2278 UNICODE_STRING dir;
2279 WCHAR *p;
2280 HMODULE module;
2281
2282 if (pActCtx->dwFlags & ACTCTX_FLAG_HMODULE_VALID) module = pActCtx->hModule;
2283 else module = NtCurrentTeb()->ProcessEnvironmentBlock->ImageBaseAddress;
2284
2285 status = get_module_filename(module, &dir, 0);
2286 if (!NT_SUCCESS(status)) goto error;
2287 if ((p = strrchrW( dir.Buffer, '\\' ))) p[1] = 0;
2288 actctx->appdir.info = dir.Buffer;
2289 }
2290
2291 nameW.Buffer = NULL;
2292 if (pActCtx->lpSource)
2293 {
2294 if (!RtlDosPathNameToNtPathName_U(pActCtx->lpSource, &nameW, NULL, NULL))
2295 {
2296 status = STATUS_NO_SUCH_FILE;
2297 goto error;
2298 }
2299 status = open_nt_file( &file, &nameW );
2300 if (!NT_SUCCESS(status))
2301 {
2302 RtlFreeUnicodeString( &nameW );
2303 goto error;
2304 }
2305 }
2306
2307 acl.actctx = actctx;
2308 acl.dependencies = NULL;
2309 acl.num_dependencies = 0;
2310 acl.allocated_dependencies = 0;
2311
2312 if (pActCtx->dwFlags & ACTCTX_FLAG_LANGID_VALID) lang = pActCtx->wLangId;
2313 if (pActCtx->dwFlags & ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID) directory = pActCtx->lpAssemblyDirectory;
2314
2315 if (pActCtx->dwFlags & ACTCTX_FLAG_RESOURCE_NAME_VALID)
2316 {
2317 /* if we have a resource it's a PE file */
2318 if (pActCtx->dwFlags & ACTCTX_FLAG_HMODULE_VALID)
2319 {
2320 status = get_manifest_in_module( &acl, NULL, NULL, directory, FALSE, pActCtx->hModule,
2321 pActCtx->lpResourceName, lang );
2322 if (status && status != STATUS_SXS_CANT_GEN_ACTCTX)
2323 /* FIXME: what to do if pActCtx->lpSource is set */
2324 status = get_manifest_in_associated_manifest( &acl, NULL, NULL, directory,
2325 pActCtx->hModule, pActCtx->lpResourceName );
2326 }
2327 else if (pActCtx->lpSource)
2328 {
2329 status = get_manifest_in_pe_file( &acl, NULL, nameW.Buffer, directory, FALSE,
2330 file, pActCtx->lpResourceName, lang );
2331 if (status && status != STATUS_SXS_CANT_GEN_ACTCTX)
2332 status = get_manifest_in_associated_manifest( &acl, NULL, nameW.Buffer, directory,
2333 NULL, pActCtx->lpResourceName );
2334 }
2335 else status = STATUS_INVALID_PARAMETER;
2336 }
2337 else
2338 {
2339 status = get_manifest_in_manifest_file( &acl, NULL, nameW.Buffer, directory, FALSE, file );
2340 }
2341
2342 if (file) NtClose( file );
2343 RtlFreeUnicodeString( &nameW );
2344
2345 if (NT_SUCCESS(status)) status = parse_depend_manifests(&acl);
2346 free_depend_manifests( &acl );
2347
2348 if (NT_SUCCESS(status))
2349 *handle = actctx;
2350 else actctx_release( actctx );
2351 return status;
2352
2353 error:
2354 if (file) NtClose( file );
2355 actctx_release( actctx );
2356 return status;
2357 }
2358
2359 VOID
2360 NTAPI
2361 RtlAddRefActivationContext(HANDLE handle)
2362 {
2363 ACTIVATION_CONTEXT *actctx;
2364
2365 if ((actctx = check_actctx( handle ))) actctx_addref( actctx );
2366 }
2367
2368 VOID
2369 NTAPI
2370 RtlReleaseActivationContext( HANDLE handle )
2371 {
2372 ACTIVATION_CONTEXT *actctx;
2373
2374 if ((actctx = check_actctx( handle ))) actctx_release( actctx );
2375 }
2376
2377 NTSTATUS
2378 NTAPI RtlActivateActivationContextEx( ULONG flags, PTEB tebAddress, HANDLE handle, PULONG_PTR cookie )
2379 {
2380 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2381
2382 if (!(frame = RtlAllocateHeap( RtlGetProcessHeap(), 0, sizeof(*frame) )))
2383 return STATUS_NO_MEMORY;
2384
2385 frame->Previous = tebAddress->ActivationContextStackPointer->ActiveFrame;
2386 frame->ActivationContext = handle;
2387 frame->Flags = 0;
2388
2389 tebAddress->ActivationContextStackPointer->ActiveFrame = frame;
2390 RtlAddRefActivationContext( handle );
2391
2392 *cookie = (ULONG_PTR)frame;
2393 DPRINT( "%p cookie=%lx\n", handle, *cookie );
2394 return STATUS_SUCCESS;
2395 }
2396
2397
2398 NTSTATUS
2399 NTAPI RtlActivateActivationContext( ULONG flags, HANDLE handle, PULONG_PTR cookie )
2400 {
2401 return RtlActivateActivationContextEx(flags, NtCurrentTeb(), handle, cookie);
2402 }
2403
2404 NTSTATUS
2405 NTAPI
2406 RtlDeactivateActivationContext( ULONG flags, ULONG_PTR cookie )
2407 {
2408 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame, *top;
2409
2410 DPRINT( "%x cookie=%lx\n", flags, cookie );
2411
2412 /* find the right frame */
2413 top = NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame;
2414 for (frame = top; frame; frame = frame->Previous)
2415 if ((ULONG_PTR)frame == cookie) break;
2416
2417 if (!frame)
2418 RtlRaiseStatus( STATUS_SXS_INVALID_DEACTIVATION );
2419
2420 if (frame != top && !(flags & RTL_DEACTIVATE_ACTIVATION_CONTEXT_FLAG_FORCE_EARLY_DEACTIVATION))
2421 RtlRaiseStatus( STATUS_SXS_EARLY_DEACTIVATION );
2422
2423 /* pop everything up to and including frame */
2424 NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame = frame->Previous;
2425
2426 while (top != NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame)
2427 {
2428 frame = top->Previous;
2429 RtlReleaseActivationContext( top->ActivationContext );
2430 RtlFreeHeap( RtlGetProcessHeap(), 0, top );
2431 top = frame;
2432 }
2433
2434 return STATUS_SUCCESS;
2435 }
2436
2437 VOID
2438 NTAPI
2439 RtlFreeActivationContextStack(IN PACTIVATION_CONTEXT_STACK Stack)
2440 {
2441 PRTL_ACTIVATION_CONTEXT_STACK_FRAME ActiveFrame, PrevFrame;
2442
2443 /* Nothing to do if there is no stack */
2444 if (!Stack) return;
2445
2446 /* Get the current active frame */
2447 ActiveFrame = Stack->ActiveFrame;
2448
2449 /* Go through them in backwards order and release */
2450 while (ActiveFrame)
2451 {
2452 PrevFrame = ActiveFrame->Previous;
2453 RtlReleaseActivationContext(ActiveFrame->ActivationContext);
2454 RtlFreeHeap(RtlGetProcessHeap(), 0, ActiveFrame);
2455 ActiveFrame = PrevFrame;
2456 }
2457
2458 /* Zero out the active frame */
2459 Stack->ActiveFrame = NULL;
2460
2461 /* TODO: Empty the Frame List Cache */
2462 ASSERT(IsListEmpty(&Stack->FrameListCache));
2463
2464 /* Free activation stack memory */
2465 RtlFreeHeap(RtlGetProcessHeap(), 0, Stack);
2466 }
2467
2468 VOID
2469 NTAPI RtlFreeThreadActivationContextStack(VOID)
2470 {
2471 RtlFreeActivationContextStack(NtCurrentTeb()->ActivationContextStackPointer);
2472 NtCurrentTeb()->ActivationContextStackPointer = NULL;
2473 }
2474
2475
2476 NTSTATUS
2477 NTAPI RtlGetActiveActivationContext( HANDLE *handle )
2478 {
2479 if (NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame)
2480 {
2481 *handle = NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame->ActivationContext;
2482 RtlAddRefActivationContext( *handle );
2483 }
2484 else
2485 *handle = 0;
2486
2487 return STATUS_SUCCESS;
2488 }
2489
2490
2491 BOOLEAN
2492 NTAPI RtlIsActivationContextActive( HANDLE handle )
2493 {
2494 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2495
2496 for (frame = NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame; frame; frame = frame->Previous)
2497 if (frame->ActivationContext == handle) return TRUE;
2498 return FALSE;
2499 }
2500
2501 NTSTATUS
2502 NTAPI
2503 RtlQueryInformationActivationContext( ULONG flags, HANDLE handle, PVOID subinst,
2504 ULONG class, PVOID buffer,
2505 SIZE_T bufsize, SIZE_T *retlen )
2506 {
2507 ACTIVATION_CONTEXT *actctx;
2508 NTSTATUS status;
2509
2510 DPRINT("%08x %p %p %u %p %Iu %p\n", flags, handle,
2511 subinst, class, buffer, bufsize, retlen);
2512
2513 if (retlen) *retlen = 0;
2514 if ((status = find_query_actctx( &handle, flags, class ))) return status;
2515
2516 switch (class)
2517 {
2518 case ActivationContextBasicInformation:
2519 {
2520 ACTIVATION_CONTEXT_BASIC_INFORMATION *info = buffer;
2521
2522 if (retlen) *retlen = sizeof(*info);
2523 if (!info || bufsize < sizeof(*info)) return STATUS_BUFFER_TOO_SMALL;
2524
2525 info->hActCtx = handle;
2526 info->dwFlags = 0; /* FIXME */
2527 if (!(flags & RTL_QUERY_ACTIVATION_CONTEXT_FLAG_NO_ADDREF)) RtlAddRefActivationContext(handle);
2528 }
2529 break;
2530
2531 case ActivationContextDetailedInformation:
2532 {
2533 ACTIVATION_CONTEXT_DETAILED_INFORMATION *acdi = buffer;
2534 struct assembly *assembly = NULL;
2535 SIZE_T len, manifest_len = 0, config_len = 0, appdir_len = 0;
2536 LPWSTR ptr;
2537
2538 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2539
2540 if (actctx->num_assemblies) assembly = actctx->assemblies;
2541
2542 if (assembly && assembly->manifest.info)
2543 manifest_len = strlenW(assembly->manifest.info) + 1;
2544 if (actctx->config.info) config_len = strlenW(actctx->config.info) + 1;
2545 if (actctx->appdir.info) appdir_len = strlenW(actctx->appdir.info) + 1;
2546 len = sizeof(*acdi) + (manifest_len + config_len + appdir_len) * sizeof(WCHAR);
2547
2548 if (retlen) *retlen = len;
2549 if (!buffer || bufsize < len) return STATUS_BUFFER_TOO_SMALL;
2550
2551 acdi->dwFlags = 0;
2552 acdi->ulFormatVersion = assembly ? 1 : 0; /* FIXME */
2553 acdi->ulAssemblyCount = actctx->num_assemblies;
2554 acdi->ulRootManifestPathType = assembly ? assembly->manifest.type : 0 /* FIXME */;
2555 acdi->ulRootManifestPathChars = assembly && assembly->manifest.info ? (DWORD)manifest_len - 1 : 0;
2556 acdi->ulRootConfigurationPathType = actctx->config.type;
2557 acdi->ulRootConfigurationPathChars = actctx->config.info ? (DWORD)config_len - 1 : 0;
2558 acdi->ulAppDirPathType = actctx->appdir.type;
2559 acdi->ulAppDirPathChars = actctx->appdir.info ? (DWORD)appdir_len - 1 : 0;
2560 ptr = (LPWSTR)(acdi + 1);
2561 if (manifest_len)
2562 {
2563 acdi->lpRootManifestPath = ptr;
2564 memcpy(ptr, assembly->manifest.info, manifest_len * sizeof(WCHAR));
2565 ptr += manifest_len;
2566 }
2567 else acdi->lpRootManifestPath = NULL;
2568 if (config_len)
2569 {
2570 acdi->lpRootConfigurationPath = ptr;
2571 memcpy(ptr, actctx->config.info, config_len * sizeof(WCHAR));
2572 ptr += config_len;
2573 }
2574 else acdi->lpRootConfigurationPath = NULL;
2575 if (appdir_len)
2576 {
2577 acdi->lpAppDirPath = ptr;
2578 memcpy(ptr, actctx->appdir.info, appdir_len * sizeof(WCHAR));
2579 }
2580 else acdi->lpAppDirPath = NULL;
2581 }
2582 break;
2583
2584 case AssemblyDetailedInformationInActivationContext:
2585 {
2586 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION *afdi = buffer;
2587 struct assembly *assembly;
2588 WCHAR *assembly_id;
2589 DWORD index;
2590 SIZE_T len, id_len = 0, ad_len = 0, path_len = 0;
2591 LPWSTR ptr;
2592
2593 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2594 if (!subinst) return STATUS_INVALID_PARAMETER;
2595
2596 index = *(DWORD*)subinst;
2597 if (!index || index > actctx->num_assemblies) return STATUS_INVALID_PARAMETER;
2598
2599 assembly = &actctx->assemblies[index - 1];
2600
2601 if (!(assembly_id = build_assembly_id( &assembly->id ))) return STATUS_NO_MEMORY;
2602 id_len = strlenW(assembly_id) + 1;
2603 if (assembly->directory) ad_len = strlenW(assembly->directory) + 1;
2604
2605 if (assembly->manifest.info &&
2606 (assembly->type == ASSEMBLY_MANIFEST || assembly->type == ASSEMBLY_SHARED_MANIFEST))
2607 path_len = strlenW(assembly->manifest.info) + 1;
2608
2609 len = sizeof(*afdi) + (id_len + ad_len + path_len) * sizeof(WCHAR);
2610
2611 if (retlen) *retlen = len;
2612 if (!buffer || bufsize < len)
2613 {
2614 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly_id );
2615 return STATUS_BUFFER_TOO_SMALL;
2616 }
2617
2618 afdi->ulFlags = 0; /* FIXME */
2619 afdi->ulEncodedAssemblyIdentityLength = (DWORD)(id_len - 1) * sizeof(WCHAR);
2620 afdi->ulManifestPathType = assembly->manifest.type;
2621 afdi->ulManifestPathLength = assembly->manifest.info ? (DWORD)(path_len - 1) * sizeof(WCHAR) : 0;
2622 /* FIXME afdi->liManifestLastWriteTime = 0; */
2623 afdi->ulPolicyPathType = ACTIVATION_CONTEXT_PATH_TYPE_NONE; /* FIXME */
2624 afdi->ulPolicyPathLength = 0;
2625 /* FIXME afdi->liPolicyLastWriteTime = 0; */
2626 afdi->ulMetadataSatelliteRosterIndex = 0; /* FIXME */
2627 afdi->ulManifestVersionMajor = 1;
2628 afdi->ulManifestVersionMinor = 0;
2629 afdi->ulPolicyVersionMajor = 0; /* FIXME */
2630 afdi->ulPolicyVersionMinor = 0; /* FIXME */
2631 afdi->ulAssemblyDirectoryNameLength = ad_len ? (DWORD)(ad_len - 1) * sizeof(WCHAR) : 0;
2632 ptr = (LPWSTR)(afdi + 1);
2633 afdi->lpAssemblyEncodedAssemblyIdentity = ptr;
2634 memcpy( ptr, assembly_id, id_len * sizeof(WCHAR) );
2635 ptr += id_len;
2636 if (path_len)
2637 {
2638 afdi->lpAssemblyManifestPath = ptr;
2639 memcpy(ptr, assembly->manifest.info, path_len * sizeof(WCHAR));
2640 ptr += path_len;
2641 } else afdi->lpAssemblyManifestPath = NULL;
2642 afdi->lpAssemblyPolicyPath = NULL; /* FIXME */
2643 if (ad_len)
2644 {
2645 afdi->lpAssemblyDirectoryName = ptr;
2646 memcpy(ptr, assembly->directory, ad_len * sizeof(WCHAR));
2647 ptr += ad_len;
2648 }
2649 else afdi->lpAssemblyDirectoryName = NULL;
2650 RtlFreeHeap( RtlGetProcessHeap(), 0, assembly_id );
2651 }
2652 break;
2653
2654 case FileInformationInAssemblyOfAssemblyInActivationContext:
2655 {
2656 const ACTIVATION_CONTEXT_QUERY_INDEX *acqi = subinst;
2657 ASSEMBLY_FILE_DETAILED_INFORMATION *afdi = buffer;
2658 struct assembly *assembly;
2659 struct dll_redirect *dll;
2660 SIZE_T len, dll_len = 0;
2661 LPWSTR ptr;
2662
2663 if (!(actctx = check_actctx(handle))) return STATUS_INVALID_PARAMETER;
2664 if (!acqi) return STATUS_INVALID_PARAMETER;
2665
2666 if (acqi->ulAssemblyIndex >= actctx->num_assemblies)
2667 return STATUS_INVALID_PARAMETER;
2668 assembly = &actctx->assemblies[acqi->ulAssemblyIndex];
2669
2670 if (acqi->ulFileIndexInAssembly >= assembly->num_dlls)
2671 return STATUS_INVALID_PARAMETER;
2672 dll = &assembly->dlls[acqi->ulFileIndexInAssembly];
2673
2674 if (dll->name) dll_len = strlenW(dll->name) + 1;
2675 len = sizeof(*afdi) + dll_len * sizeof(WCHAR);
2676
2677 if (!buffer || bufsize < len)
2678 {
2679 if (retlen) *retlen = len;
2680 return STATUS_BUFFER_TOO_SMALL;
2681 }
2682 if (retlen) *retlen = 0; /* yes that's what native does !! */
2683 afdi->ulFlags = ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION;
2684 afdi->ulFilenameLength = dll_len ? (DWORD)(dll_len - 1) * sizeof(WCHAR) : 0;
2685 afdi->ulPathLength = 0; /* FIXME */
2686 ptr = (LPWSTR)(afdi + 1);
2687 if (dll_len)
2688 {
2689 afdi->lpFileName = ptr;
2690 memcpy( ptr, dll->name, dll_len * sizeof(WCHAR) );
2691 } else afdi->lpFileName = NULL;
2692 afdi->lpFilePath = NULL; /* FIXME */
2693 }
2694 break;
2695
2696 default:
2697 DPRINT( "class %u not implemented\n", class );
2698 return STATUS_NOT_IMPLEMENTED;
2699 }
2700 return STATUS_SUCCESS;
2701 }
2702
2703 NTSTATUS
2704 NTAPI
2705 RtlQueryInformationActiveActivationContext(ULONG ulInfoClass,
2706 PVOID pvBuffer,
2707 SIZE_T cbBuffer OPTIONAL,
2708 SIZE_T *pcbWrittenOrRequired OPTIONAL)
2709 {
2710 return RtlQueryInformationActivationContext(RTL_QUERY_ACTIVATION_CONTEXT_FLAG_USE_ACTIVE_ACTIVATION_CONTEXT,
2711 NULL,
2712 NULL,
2713 ulInfoClass,
2714 pvBuffer,
2715 cbBuffer,
2716 pcbWrittenOrRequired);
2717 }
2718
2719 #define FIND_ACTCTX_RETURN_FLAGS 0x00000002
2720 #define FIND_ACTCTX_RETURN_ASSEMBLY_METADATA 0x00000004
2721 #define FIND_ACTCTX_VALID_MASK (FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX | FIND_ACTCTX_RETURN_FLAGS | FIND_ACTCTX_RETURN_ASSEMBLY_METADATA)
2722
2723 NTSTATUS
2724 NTAPI
2725 RtlpFindActivationContextSection_CheckParameters( ULONG flags, const GUID *guid, ULONG section_kind,
2726 UNICODE_STRING *section_name, PACTCTX_SECTION_KEYED_DATA data )
2727 {
2728 /* Check general parameter combinations */
2729 if (!section_name ||
2730 (flags & ~FIND_ACTCTX_VALID_MASK) ||
2731 ((flags & FIND_ACTCTX_VALID_MASK) && !data) ||
2732 (data && data->cbSize < offsetof(ACTCTX_SECTION_KEYED_DATA, ulAssemblyRosterIndex)))
2733 {
2734 DPRINT1("invalid parameter\n");
2735 return STATUS_INVALID_PARAMETER;
2736 }
2737
2738 /* TODO */
2739 if (flags & FIND_ACTCTX_RETURN_FLAGS ||
2740 flags & FIND_ACTCTX_RETURN_ASSEMBLY_METADATA)
2741 {
2742 DPRINT1("unknown flags %08x\n", flags);
2743 return STATUS_INVALID_PARAMETER;
2744 }
2745
2746 return STATUS_SUCCESS;
2747 }
2748
2749 NTSTATUS
2750 NTAPI
2751 RtlFindActivationContextSectionString( ULONG flags, const GUID *guid, ULONG section_kind,
2752 UNICODE_STRING *section_name, PVOID ptr )
2753 {
2754 PACTCTX_SECTION_KEYED_DATA data = ptr;
2755 NTSTATUS status;
2756
2757 status = RtlpFindActivationContextSection_CheckParameters(flags, guid, section_kind, section_name, data);
2758 if (!NT_SUCCESS(status)) return status;
2759
2760 status = STATUS_SXS_KEY_NOT_FOUND;
2761
2762 /* if there is no data, but params are valid,
2763 we return that sxs key is not found to be at least somehow compatible */
2764 if (!data) return status;
2765
2766 ASSERT(NtCurrentTeb());
2767 ASSERT(NtCurrentTeb()->ActivationContextStackPointer);
2768
2769 if (NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame)
2770 {
2771 ACTIVATION_CONTEXT *actctx = check_actctx(NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame->ActivationContext);
2772 if (actctx) status = find_string( actctx, section_kind, section_name, flags, data );
2773 }
2774
2775 if (status != STATUS_SUCCESS)
2776 status = find_string( process_actctx, section_kind, section_name, flags, data );
2777
2778 return status;
2779 }
2780
2781 NTSTATUS
2782 NTAPI
2783 RtlFindActivationContextSectionGuid(ULONG flags, const GUID *guid, ULONG section_kind, UNICODE_STRING *section_name, PVOID ptr)
2784 {
2785 UNIMPLEMENTED;
2786 return STATUS_NOT_IMPLEMENTED;
2787 }
2788
2789 /* Stubs */
2790
2791 NTSTATUS
2792 NTAPI
2793 RtlAllocateActivationContextStack(IN PACTIVATION_CONTEXT_STACK *Stack)
2794 {
2795 PACTIVATION_CONTEXT_STACK ContextStack;
2796
2797 /* Check if it's already allocated */
2798 if (*Stack) return STATUS_SUCCESS;
2799
2800 /* Allocate space for the context stack */
2801 ContextStack = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACTIVATION_CONTEXT_STACK));
2802 if (!ContextStack)
2803 {
2804 return STATUS_NO_MEMORY;
2805 }
2806
2807 /* Initialize the context stack */
2808 ContextStack->Flags = 0;
2809 ContextStack->ActiveFrame = NULL;
2810 InitializeListHead(&ContextStack->FrameListCache);
2811 ContextStack->NextCookieSequenceNumber = 1;
2812 ContextStack->StackId = 1; //TODO: Timer-based
2813
2814 *Stack = ContextStack;
2815
2816 return STATUS_SUCCESS;
2817 }
2818
2819 PRTL_ACTIVATION_CONTEXT_STACK_FRAME
2820 FASTCALL
2821 RtlActivateActivationContextUnsafeFast(IN PRTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED Frame,
2822 IN PVOID Context)
2823 {
2824 #if NEW_NTDLL_LOADER
2825 RTL_ACTIVATION_CONTEXT_STACK_FRAME *ActiveFrame;
2826
2827 /* Get the curren active frame */
2828 ActiveFrame = NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame;
2829
2830 DPRINT1("ActiveFrame %p, &Frame->Frame %p, Context %p\n", ActiveFrame, &Frame->Frame, Context);
2831
2832 /* Actually activate it */
2833 Frame->Frame.Previous = ActiveFrame;
2834 Frame->Frame.ActivationContext = Context;
2835 Frame->Frame.Flags = 0;
2836
2837 /* Check if we can activate this context */
2838 if ((ActiveFrame && (ActiveFrame->ActivationContext != Context)) ||
2839 Context)
2840 {
2841 /* Set new active frame */
2842 NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame = &Frame->Frame;
2843 return &Frame->Frame;
2844 }
2845
2846 /* We can get here only one way: it was already activated */
2847 DPRINT1("Trying to activate improper activation context\n");
2848
2849 /* Activate only if we are allowing multiple activation */
2850 if (!RtlpNotAllowingMultipleActivation)
2851 {
2852 NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame = &Frame->Frame;
2853 }
2854 else
2855 {
2856 /* Set flag */
2857 Frame->Frame.Flags = 0x30;
2858 }
2859
2860 /* Return pointer to the activation frame */
2861 return &Frame->Frame;
2862 #else
2863
2864 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame = &Frame->Frame;
2865
2866 frame->Previous = NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame;
2867 frame->ActivationContext = Context;
2868 frame->Flags = 0;
2869
2870 NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame = frame;
2871
2872 return STATUS_SUCCESS;
2873 #endif
2874 }
2875
2876 PRTL_ACTIVATION_CONTEXT_STACK_FRAME
2877 FASTCALL
2878 RtlDeactivateActivationContextUnsafeFast(IN PRTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED Frame)
2879 {
2880 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
2881 //RTL_ACTIVATION_CONTEXT_STACK_FRAME *top;
2882
2883 /* find the right frame */
2884 //top = NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame;
2885 frame = &Frame->Frame;
2886
2887 if (!frame)
2888 {
2889 DPRINT1("No top frame!\n");
2890 RtlRaiseStatus( STATUS_SXS_INVALID_DEACTIVATION );
2891 }
2892
2893 /* pop everything up to and including frame */
2894 NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame = frame->Previous;
2895
2896 return frame;
2897 }
2898
2899
2900 NTSTATUS
2901 NTAPI
2902 RtlZombifyActivationContext(PVOID Context)
2903 {
2904 UNIMPLEMENTED;
2905
2906 if (Context == ACTCTX_FAKE_HANDLE)
2907 return STATUS_SUCCESS;
2908
2909 return STATUS_NOT_IMPLEMENTED;
2910 }
2911