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