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