Sync to wine-0.9.60:
[reactos.git] / reactos / tools / widl / write_msft.c
1 /*
2 * Typelib v2 (MSFT) generation
3 *
4 * Copyright 2004 Alastair Bridgewater
5 * 2004, 2005 Huw Davies
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 *
21 * --------------------------------------------------------------------------------------
22 * Known problems:
23 *
24 * Badly incomplete.
25 *
26 * Only works on little-endian systems.
27 *
28 */
29
30 #include "config.h"
31 #include "wine/port.h"
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <time.h>
39
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
42
43 #include <host/typedefs.h>
44 #include <host/nls.h>
45
46 #include "widltypes.h"
47 #include "typelib.h"
48 #include "typelib_struct.h"
49 #include "utils.h"
50 #include "header.h"
51 #include "hash.h"
52
53 enum MSFT_segment_index {
54 MSFT_SEG_TYPEINFO = 0, /* type information */
55 MSFT_SEG_IMPORTINFO, /* import information */
56 MSFT_SEG_IMPORTFILES, /* import filenames */
57 MSFT_SEG_REFERENCES, /* references (?) */
58 MSFT_SEG_GUIDHASH, /* hash table for guids? */
59 MSFT_SEG_GUID, /* guid storage */
60 MSFT_SEG_NAMEHASH, /* hash table for names */
61 MSFT_SEG_NAME, /* name storage */
62 MSFT_SEG_STRING, /* string storage */
63 MSFT_SEG_TYPEDESC, /* type descriptions */
64 MSFT_SEG_ARRAYDESC, /* array descriptions */
65 MSFT_SEG_CUSTDATA, /* custom data */
66 MSFT_SEG_CUSTDATAGUID, /* custom data guids */
67 MSFT_SEG_UNKNOWN, /* ??? */
68 MSFT_SEG_UNKNOWN2, /* ??? */
69 MSFT_SEG_MAX /* total number of segments */
70 };
71
72 typedef struct tagMSFT_ImpFile {
73 int guid;
74 LCID lcid;
75 int version;
76 char filename[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
77 } MSFT_ImpFile;
78
79 typedef struct _msft_typelib_t
80 {
81 typelib_t *typelib;
82 MSFT_Header typelib_header;
83 MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
84 char *typelib_segment_data[MSFT_SEG_MAX];
85 int typelib_segment_block_length[MSFT_SEG_MAX];
86
87 INT typelib_typeinfo_offsets[0x200]; /* Hope that's enough. */
88
89 INT *typelib_namehash_segment;
90 INT *typelib_guidhash_segment;
91
92 INT help_string_dll_offset;
93
94 struct _msft_typeinfo_t *typeinfos;
95 struct _msft_typeinfo_t *last_typeinfo;
96 } msft_typelib_t;
97
98 typedef struct _msft_typeinfo_t
99 {
100 msft_typelib_t *typelib;
101 MSFT_TypeInfoBase *typeinfo;
102
103 int typekind;
104
105 unsigned int var_data_allocated;
106 int *var_data;
107
108 unsigned int func_data_allocated;
109 int *func_data;
110
111 int vars_allocated;
112 int *var_indices;
113 int *var_names;
114 int *var_offsets;
115
116 int funcs_allocated;
117 int *func_indices;
118 int *func_names;
119 int *func_offsets;
120
121 int datawidth;
122
123 struct _msft_typeinfo_t *next_typeinfo;
124 } msft_typeinfo_t;
125
126
127
128 /*================== Internal functions ===================================*/
129
130 /****************************************************************************
131 * ctl2_init_header
132 *
133 * Initializes the type library header of a new typelib.
134 */
135 static void ctl2_init_header(
136 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
137 {
138 typelib->typelib_header.magic1 = 0x5446534d;
139 typelib->typelib_header.magic2 = 0x00010002;
140 typelib->typelib_header.posguid = -1;
141 typelib->typelib_header.lcid = 0x0409; /* or do we use the current one? */
142 typelib->typelib_header.lcid2 = 0x0;
143 typelib->typelib_header.varflags = 0x40;
144 typelib->typelib_header.version = 0;
145 typelib->typelib_header.flags = 0;
146 typelib->typelib_header.nrtypeinfos = 0;
147 typelib->typelib_header.helpstring = -1;
148 typelib->typelib_header.helpstringcontext = 0;
149 typelib->typelib_header.helpcontext = 0;
150 typelib->typelib_header.nametablecount = 0;
151 typelib->typelib_header.nametablechars = 0;
152 typelib->typelib_header.NameOffset = -1;
153 typelib->typelib_header.helpfile = -1;
154 typelib->typelib_header.CustomDataOffset = -1;
155 typelib->typelib_header.res44 = 0x20;
156 typelib->typelib_header.res48 = 0x80;
157 typelib->typelib_header.dispatchpos = -1;
158 typelib->typelib_header.nimpinfos = 0;
159 }
160
161 /****************************************************************************
162 * ctl2_init_segdir
163 *
164 * Initializes the segment directory of a new typelib.
165 */
166 static void ctl2_init_segdir(
167 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
168 {
169 int i;
170 MSFT_pSeg *segdir;
171
172 segdir = &typelib->typelib_segdir[MSFT_SEG_TYPEINFO];
173
174 for (i = 0; i < 15; i++) {
175 segdir[i].offset = -1;
176 segdir[i].length = 0;
177 segdir[i].res08 = -1;
178 segdir[i].res0c = 0x0f;
179 }
180 }
181
182 /****************************************************************************
183 * ctl2_hash_guid
184 *
185 * Generates a hash key from a GUID.
186 *
187 * RETURNS
188 *
189 * The hash key for the GUID.
190 */
191 static int ctl2_hash_guid(
192 REFGUID guid) /* [I] The guid to hash. */
193 {
194 int hash;
195 int i;
196
197 hash = 0;
198 for (i = 0; i < 8; i ++) {
199 hash ^= ((const short *)guid)[i];
200 }
201
202 return hash & 0x1f;
203 }
204
205 /****************************************************************************
206 * ctl2_find_guid
207 *
208 * Locates a guid in a type library.
209 *
210 * RETURNS
211 *
212 * The offset into the GUID segment of the guid, or -1 if not found.
213 */
214 static int ctl2_find_guid(
215 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
216 int hash_key, /* [I] The hash key for the guid. */
217 REFGUID guid) /* [I] The guid to find. */
218 {
219 int offset;
220 MSFT_GuidEntry *guidentry;
221
222 offset = typelib->typelib_guidhash_segment[hash_key];
223 while (offset != -1) {
224 guidentry = (MSFT_GuidEntry *)&typelib->typelib_segment_data[MSFT_SEG_GUID][offset];
225
226 if (!memcmp(guidentry, guid, sizeof(GUID))) return offset;
227
228 offset = guidentry->next_hash;
229 }
230
231 return offset;
232 }
233
234 /****************************************************************************
235 * ctl2_find_name
236 *
237 * Locates a name in a type library.
238 *
239 * RETURNS
240 *
241 * The offset into the NAME segment of the name, or -1 if not found.
242 *
243 * NOTES
244 *
245 * The name must be encoded as with ctl2_encode_name().
246 */
247 static int ctl2_find_name(
248 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
249 char *name) /* [I] The encoded name to find. */
250 {
251 int offset;
252 int *namestruct;
253
254 offset = typelib->typelib_namehash_segment[name[2] & 0x7f];
255 while (offset != -1) {
256 namestruct = (int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][offset];
257
258 if (!((namestruct[2] ^ *((int *)name)) & 0xffff00ff)) {
259 /* hash codes and lengths match, final test */
260 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
261 }
262
263 /* move to next item in hash bucket */
264 offset = namestruct[1];
265 }
266
267 return offset;
268 }
269
270 /****************************************************************************
271 * ctl2_encode_name
272 *
273 * Encodes a name string to a form suitable for storing into a type library
274 * or comparing to a name stored in a type library.
275 *
276 * RETURNS
277 *
278 * The length of the encoded name, including padding and length+hash fields.
279 *
280 * NOTES
281 *
282 * Will throw an exception if name or result are NULL. Is not multithread
283 * safe in the slightest.
284 */
285 static int ctl2_encode_name(
286 msft_typelib_t *typelib, /* [I] The typelib to operate against (used for LCID only). */
287 const char *name, /* [I] The name string to encode. */
288 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
289 {
290 int length;
291 static char converted_name[0x104];
292 int offset;
293 int value;
294
295 length = strlen(name);
296 memcpy(converted_name + 4, name, length);
297 converted_name[0] = length & 0xff;
298
299 converted_name[length + 4] = 0;
300
301 converted_name[1] = 0x00;
302
303 value = lhash_val_of_name_sys(typelib->typelib_header.varflags & 0x0f, typelib->typelib_header.lcid, converted_name + 4);
304
305 converted_name[2] = value;
306 converted_name[3] = value >> 8;
307
308 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
309
310 *result = converted_name;
311
312 return (length + 7) & ~3;
313 }
314
315 /****************************************************************************
316 * ctl2_encode_string
317 *
318 * Encodes a string to a form suitable for storing into a type library or
319 * comparing to a string stored in a type library.
320 *
321 * RETURNS
322 *
323 * The length of the encoded string, including padding and length fields.
324 *
325 * NOTES
326 *
327 * Will throw an exception if string or result are NULL. Is not multithread
328 * safe in the slightest.
329 */
330 static int ctl2_encode_string(
331 const char *string, /* [I] The string to encode. */
332 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
333 {
334 int length;
335 static char converted_string[0x104];
336 int offset;
337
338 length = strlen(string);
339 memcpy(converted_string + 2, string, length);
340 converted_string[0] = length & 0xff;
341 converted_string[1] = (length >> 8) & 0xff;
342
343 if(length < 3) { /* strings of this length are padded with up to 8 bytes incl the 2 byte length */
344 for(offset = 0; offset < 4; offset++)
345 converted_string[length + offset + 2] = 0x57;
346 length += 4;
347 }
348 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
349
350 *result = converted_string;
351
352 return (length + 5) & ~3;
353 }
354
355 /****************************************************************************
356 * ctl2_alloc_segment
357 *
358 * Allocates memory from a segment in a type library.
359 *
360 * RETURNS
361 *
362 * Success: The offset within the segment of the new data area.
363 *
364 * BUGS
365 *
366 * Does not (yet) handle the case where the allocated segment memory needs to grow.
367 */
368 static int ctl2_alloc_segment(
369 msft_typelib_t *typelib, /* [I] The type library in which to allocate. */
370 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
371 int size, /* [I] The amount to allocate. */
372 int block_size) /* [I] Initial allocation block size, or 0 for default. */
373 {
374 int offset;
375
376 if(!typelib->typelib_segment_data[segment]) {
377 if (!block_size) block_size = 0x2000;
378
379 typelib->typelib_segment_block_length[segment] = block_size;
380 typelib->typelib_segment_data[segment] = xmalloc(block_size);
381 if (!typelib->typelib_segment_data[segment]) return -1;
382 memset(typelib->typelib_segment_data[segment], 0x57, block_size);
383 }
384
385 while ((typelib->typelib_segdir[segment].length + size) > typelib->typelib_segment_block_length[segment]) {
386 char *block;
387
388 block_size = typelib->typelib_segment_block_length[segment];
389 block = xrealloc(typelib->typelib_segment_data[segment], block_size << 1);
390
391 if (segment == MSFT_SEG_TYPEINFO) {
392 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
393 msft_typeinfo_t *typeinfo;
394
395 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
396 typeinfo->typeinfo = (void *)&block[((char *)typeinfo->typeinfo) - typelib->typelib_segment_data[segment]];
397 }
398 }
399
400 memset(block + block_size, 0x57, block_size);
401 typelib->typelib_segment_block_length[segment] = block_size << 1;
402 typelib->typelib_segment_data[segment] = block;
403 }
404
405 offset = typelib->typelib_segdir[segment].length;
406 typelib->typelib_segdir[segment].length += size;
407
408 return offset;
409 }
410
411 /****************************************************************************
412 * ctl2_alloc_typeinfo
413 *
414 * Allocates and initializes a typeinfo structure in a type library.
415 *
416 * RETURNS
417 *
418 * Success: The offset of the new typeinfo.
419 * Failure: -1 (this is invariably an out of memory condition).
420 */
421 static int ctl2_alloc_typeinfo(
422 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
423 int nameoffset) /* [I] The offset of the name for this typeinfo. */
424 {
425 int offset;
426 MSFT_TypeInfoBase *typeinfo;
427
428 offset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
429
430 typelib->typelib_typeinfo_offsets[typelib->typelib_header.nrtypeinfos++] = offset;
431
432 typeinfo = (void *)(typelib->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
433
434 typeinfo->typekind = (typelib->typelib_header.nrtypeinfos - 1) << 16;
435 typeinfo->memoffset = -1; /* should be EOF if no elements */
436 typeinfo->res2 = 0;
437 typeinfo->res3 = -1;
438 typeinfo->res4 = 3;
439 typeinfo->res5 = 0;
440 typeinfo->cElement = 0;
441 typeinfo->res7 = 0;
442 typeinfo->res8 = 0;
443 typeinfo->res9 = 0;
444 typeinfo->resA = 0;
445 typeinfo->posguid = -1;
446 typeinfo->flags = 0;
447 typeinfo->NameOffset = nameoffset;
448 typeinfo->version = 0;
449 typeinfo->docstringoffs = -1;
450 typeinfo->helpstringcontext = 0;
451 typeinfo->helpcontext = 0;
452 typeinfo->oCustData = -1;
453 typeinfo->cbSizeVft = 0;
454 typeinfo->cImplTypes = 0;
455 typeinfo->size = 0;
456 typeinfo->datatype1 = -1;
457 typeinfo->datatype2 = 0;
458 typeinfo->res18 = 0;
459 typeinfo->res19 = -1;
460
461 return offset;
462 }
463
464 /****************************************************************************
465 * ctl2_alloc_guid
466 *
467 * Allocates and initializes a GUID structure in a type library. Also updates
468 * the GUID hash table as needed.
469 *
470 * RETURNS
471 *
472 * Success: The offset of the new GUID.
473 */
474 static int ctl2_alloc_guid(
475 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
476 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
477 {
478 int offset;
479 MSFT_GuidEntry *guid_space;
480 int hash_key;
481
482 hash_key = ctl2_hash_guid(&guid->guid);
483
484 offset = ctl2_find_guid(typelib, hash_key, &guid->guid);
485 if (offset != -1) return offset;
486
487 offset = ctl2_alloc_segment(typelib, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
488
489 guid_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_GUID] + offset);
490 *guid_space = *guid;
491
492 guid_space->next_hash = typelib->typelib_guidhash_segment[hash_key];
493 typelib->typelib_guidhash_segment[hash_key] = offset;
494
495 return offset;
496 }
497
498 /****************************************************************************
499 * ctl2_alloc_name
500 *
501 * Allocates and initializes a name within a type library. Also updates the
502 * name hash table as needed.
503 *
504 * RETURNS
505 *
506 * Success: The offset within the segment of the new name.
507 * Failure: -1 (this is invariably an out of memory condition).
508 */
509 static int ctl2_alloc_name(
510 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
511 const char *name) /* [I] The name to store. */
512 {
513 int length;
514 int offset;
515 MSFT_NameIntro *name_space;
516 char *encoded_name;
517
518 length = ctl2_encode_name(typelib, name, &encoded_name);
519
520 offset = ctl2_find_name(typelib, encoded_name);
521 if (offset != -1) return offset;
522
523 offset = ctl2_alloc_segment(typelib, MSFT_SEG_NAME, length + 8, 0);
524
525 name_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_NAME] + offset);
526 name_space->hreftype = -1;
527 name_space->next_hash = -1;
528 memcpy(&name_space->namelen, encoded_name, length);
529
530 if (typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
531 name_space->next_hash = typelib->typelib_namehash_segment[encoded_name[2] & 0x7f];
532
533 typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
534
535 typelib->typelib_header.nametablecount += 1;
536 typelib->typelib_header.nametablechars += *encoded_name;
537
538 return offset;
539 }
540
541 /****************************************************************************
542 * ctl2_alloc_string
543 *
544 * Allocates and initializes a string in a type library.
545 *
546 * RETURNS
547 *
548 * Success: The offset within the segment of the new string.
549 * Failure: -1 (this is invariably an out of memory condition).
550 */
551 static int ctl2_alloc_string(
552 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
553 const char *string) /* [I] The string to store. */
554 {
555 int length;
556 int offset;
557 char *string_space;
558 char *encoded_string;
559
560 length = ctl2_encode_string(string, &encoded_string);
561
562 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_STRING].length;
563 offset += ((((typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) & 0xff)
564 | (typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 0] & 0xff)) + 5) & ~3) {
565 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
566 }
567
568 offset = ctl2_alloc_segment(typelib, MSFT_SEG_STRING, length, 0);
569
570 string_space = typelib->typelib_segment_data[MSFT_SEG_STRING] + offset;
571 memcpy(string_space, encoded_string, length);
572
573 return offset;
574 }
575
576 /****************************************************************************
577 * alloc_msft_importinfo
578 *
579 * Allocates and initializes an import information structure in a type library.
580 *
581 * RETURNS
582 *
583 * Success: The offset of the new importinfo.
584 * Failure: -1 (this is invariably an out of memory condition).
585 */
586 static int alloc_msft_importinfo(
587 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
588 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
589 {
590 int offset;
591 MSFT_ImpInfo *impinfo_space;
592
593 for (offset = 0;
594 offset < typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
595 offset += sizeof(MSFT_ImpInfo)) {
596 if (!memcmp(&(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][offset]),
597 impinfo, sizeof(MSFT_ImpInfo))) {
598 return offset;
599 }
600 }
601
602 impinfo->flags |= typelib->typelib_header.nimpinfos++;
603
604 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
605
606 impinfo_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
607 *impinfo_space = *impinfo;
608
609 return offset;
610 }
611
612 /****************************************************************************
613 * alloc_importfile
614 *
615 * Allocates and initializes an import file definition in a type library.
616 *
617 * RETURNS
618 *
619 * Success: The offset of the new importinfo.
620 * Failure: -1 (this is invariably an out of memory condition).
621 */
622 static int alloc_importfile(
623 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
624 int guidoffset, /* [I] The offset to the GUID for the imported library. */
625 int major_version, /* [I] The major version number of the imported library. */
626 int minor_version, /* [I] The minor version number of the imported library. */
627 const char *filename) /* [I] The filename of the imported library. */
628 {
629 int length;
630 int offset;
631 MSFT_ImpFile *importfile;
632 char *encoded_string;
633
634 length = ctl2_encode_string(filename, &encoded_string);
635
636 encoded_string[0] <<= 2;
637 encoded_string[0] |= 1;
638
639 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
640 offset += ((((typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) & 0xff)
641 | (typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc] & 0xff)) >> 2) + 0xc) {
642 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
643 }
644
645 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
646
647 importfile = (MSFT_ImpFile *)&typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
648 importfile->guid = guidoffset;
649 importfile->lcid = typelib->typelib_header.lcid2;
650 importfile->version = major_version | (minor_version << 16);
651 memcpy(&importfile->filename, encoded_string, length);
652
653 return offset;
654 }
655
656 static void alloc_importinfo(msft_typelib_t *typelib, importinfo_t *importinfo)
657 {
658 importlib_t *importlib = importinfo->importlib;
659
660 chat("alloc_importinfo: %s\n", importinfo->name);
661
662 if(!importlib->allocated) {
663 MSFT_GuidEntry guid;
664 int guid_idx;
665
666 chat("allocating importlib %s\n", importlib->name);
667
668 importlib->allocated = -1;
669
670 memcpy(&guid.guid, &importlib->guid, sizeof(GUID));
671 guid.hreftype = 2;
672
673 guid_idx = ctl2_alloc_guid(typelib, &guid);
674
675 alloc_importfile(typelib, guid_idx, importlib->version&0xffff,
676 importlib->version>>16, importlib->name);
677 }
678
679 if(importinfo->offset == -1 || !(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID)) {
680 MSFT_ImpInfo impinfo;
681
682 impinfo.flags = importinfo->flags;
683 impinfo.oImpFile = 0;
684
685 if(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID) {
686 MSFT_GuidEntry guid;
687
688 guid.hreftype = 0;
689 memcpy(&guid.guid, &importinfo->guid, sizeof(GUID));
690
691 impinfo.oGuid = ctl2_alloc_guid(typelib, &guid);
692
693 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
694
695 typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo.oGuid+sizeof(GUID)]
696 = importinfo->offset+1;
697
698 if(!strcmp(importinfo->name, "IDispatch"))
699 typelib->typelib_header.dispatchpos = importinfo->offset+1;
700 }else {
701 impinfo.oGuid = importinfo->id;
702 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
703 }
704 }
705 }
706
707 static importinfo_t *find_importinfo(msft_typelib_t *typelib, const char *name)
708 {
709 importlib_t *importlib;
710 int i;
711
712 chat("search importlib %s\n", name);
713
714 if(!name)
715 return NULL;
716
717 LIST_FOR_EACH_ENTRY( importlib, &typelib->typelib->importlibs, importlib_t, entry )
718 {
719 for(i=0; i < importlib->ntypeinfos; i++) {
720 if(!strcmp(name, importlib->importinfos[i].name)) {
721 chat("Found %s in importlib.\n", name);
722 return importlib->importinfos+i;
723 }
724 }
725 }
726
727 return NULL;
728 }
729
730 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure);
731 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface);
732 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration);
733 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls);
734 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
735
736
737 /****************************************************************************
738 * encode_type
739 *
740 * Encodes a type, storing information in the TYPEDESC and ARRAYDESC
741 * segments as needed.
742 *
743 * RETURNS
744 *
745 * Success: 0.
746 * Failure: -1.
747 */
748 static int encode_type(
749 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
750 int vt, /* [I] vt to encode */
751 type_t *type, /* [I] type */
752 int *encoded_type, /* [O] The encoded type description. */
753 int *width, /* [O] The width of the type, or NULL. */
754 int *alignment, /* [O] The alignment of the type, or NULL. */
755 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
756 {
757 int default_type;
758 int scratch;
759 int typeoffset;
760 int *typedata;
761 int target_type;
762 int child_size = 0;
763
764 chat("encode_type vt %d type %p\n", vt, type);
765
766 default_type = 0x80000000 | (vt << 16) | vt;
767 if (!width) width = &scratch;
768 if (!alignment) alignment = &scratch;
769 if (!decoded_size) decoded_size = &scratch;
770
771
772 switch (vt) {
773 case VT_I1:
774 case VT_UI1:
775 *encoded_type = default_type;
776 *width = 1;
777 *alignment = 1;
778 break;
779
780 case VT_INT:
781 *encoded_type = 0x80000000 | (VT_I4 << 16) | VT_INT;
782 if ((typelib->typelib_header.varflags & 0x0f) == SYS_WIN16) {
783 *width = 2;
784 *alignment = 2;
785 } else {
786 *width = 4;
787 *alignment = 4;
788 }
789 break;
790
791 case VT_UINT:
792 *encoded_type = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
793 if ((typelib->typelib_header.varflags & 0x0f) == SYS_WIN16) {
794 *width = 2;
795 *alignment = 2;
796 } else {
797 *width = 4;
798 *alignment = 4;
799 }
800 break;
801
802 case VT_UI2:
803 case VT_I2:
804 case VT_BOOL:
805 *encoded_type = default_type;
806 *width = 2;
807 *alignment = 2;
808 break;
809
810 case VT_I4:
811 case VT_UI4:
812 case VT_R4:
813 case VT_ERROR:
814 case VT_BSTR:
815 case VT_HRESULT:
816 *encoded_type = default_type;
817 *width = 4;
818 *alignment = 4;
819 break;
820
821 case VT_R8:
822 case VT_I8:
823 case VT_UI8:
824 *encoded_type = default_type;
825 *width = 8;
826 *alignment = 8;
827 break;
828
829 case VT_CY:
830 case VT_DATE:
831 *encoded_type = default_type;
832 *width = 8;
833 *alignment = 8;
834 break;
835
836 case VT_VOID:
837 *encoded_type = 0x80000000 | (VT_EMPTY << 16) | vt;
838 *width = 0;
839 *alignment = 1;
840 break;
841
842 case VT_UNKNOWN:
843 case VT_DISPATCH:
844 *encoded_type = default_type;
845 *width = 4;
846 *alignment = 4;
847 break;
848
849 case VT_VARIANT:
850 *encoded_type = default_type;
851 break;
852
853 case VT_LPSTR:
854 case VT_LPWSTR:
855 *encoded_type = 0xfffe0000 | vt;
856 *width = 4;
857 *alignment = 4;
858 break;
859
860 case VT_PTR:
861 {
862 int next_vt;
863 for(next_vt = 0; type->ref; type = type->ref) {
864 next_vt = get_type_vt(type->ref);
865 if (next_vt != 0)
866 break;
867 }
868 /* if no type found then it must be void */
869 if (next_vt == 0)
870 next_vt = VT_VOID;
871
872 encode_type(typelib, next_vt, type->ref, &target_type, NULL, NULL, &child_size);
873 if(type->ref && (type->ref->type == RPC_FC_IP)) {
874 chat("encode_type: skipping ptr\n");
875 *encoded_type = target_type;
876 *width = 4;
877 *alignment = 4;
878 *decoded_size = child_size;
879 break;
880 }
881
882 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
883 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
884 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
885 }
886
887 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
888 int mix_field;
889
890 if (target_type & 0x80000000) {
891 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
892 } else {
893 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
894 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
895 }
896
897 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
898 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
899
900 typedata[0] = (mix_field << 16) | VT_PTR;
901 typedata[1] = target_type;
902 }
903
904 *encoded_type = typeoffset;
905
906 *width = 4;
907 *alignment = 4;
908 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
909 break;
910 }
911
912
913 case VT_SAFEARRAY:
914 {
915 int next_vt;
916
917 /* skip over SAFEARRAY type straight to element type */
918 type = type->ref;
919
920 for(next_vt = 0; type->ref; type = type->ref) {
921 next_vt = get_type_vt(type->ref);
922 if (next_vt != 0)
923 break;
924 }
925
926 encode_type(typelib, next_vt, type->ref, &target_type, NULL, NULL, &child_size);
927
928 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
929 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
930 if (((typedata[0] & 0xffff) == VT_SAFEARRAY) && (typedata[1] == target_type)) break;
931 }
932
933 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
934 int mix_field;
935
936 if (target_type & 0x80000000) {
937 mix_field = ((target_type >> 16) & VT_TYPEMASK) | VT_ARRAY;
938 } else {
939 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
940 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
941 }
942
943 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
944 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
945
946 typedata[0] = (mix_field << 16) | VT_SAFEARRAY;
947 typedata[1] = target_type;
948 }
949
950 *encoded_type = typeoffset;
951
952 *width = 4;
953 *alignment = 4;
954 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
955 break;
956 }
957
958
959 case VT_USERDEFINED:
960 {
961 int typeinfo_offset;
962
963 /* typedef'd types without attributes aren't included in the typelib */
964 while (type->typelib_idx < 0 && type->kind == TKIND_ALIAS && ! type->attrs)
965 type = type->orig;
966
967 chat("encode_type: VT_USERDEFINED - type %p name = %s type->type %d idx %d\n", type,
968 type->name, type->type, type->typelib_idx);
969
970 if(type->typelib_idx == -1) {
971 chat("encode_type: trying to ref not added type\n");
972 switch(type->type) {
973 case RPC_FC_STRUCT:
974 case RPC_FC_PSTRUCT:
975 case RPC_FC_CSTRUCT:
976 case RPC_FC_CPSTRUCT:
977 case RPC_FC_CVSTRUCT:
978 case RPC_FC_BOGUS_STRUCT:
979 add_structure_typeinfo(typelib, type);
980 break;
981 case RPC_FC_IP:
982 add_interface_typeinfo(typelib, type);
983 break;
984 case RPC_FC_ENUM16:
985 add_enum_typeinfo(typelib, type);
986 break;
987 case 0:
988 if (type->kind == TKIND_COCLASS)
989 add_coclass_typeinfo(typelib, type);
990 else if (type->kind == TKIND_DISPATCH)
991 add_dispinterface_typeinfo(typelib, type);
992 else
993 error("encode_type: VT_USERDEFINED - can't yet add typedef's on the fly\n");
994 break;
995 default:
996 error("encode_type: VT_USERDEFINED - unhandled type %d\n", type->type);
997 }
998 }
999
1000 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
1001 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1002 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1003 if ((typedata[0] == ((0x7fff << 16) | VT_USERDEFINED)) && (typedata[1] == typeinfo_offset)) break;
1004 }
1005
1006 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1007 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1008 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1009
1010 typedata[0] = (0x7fff << 16) | VT_USERDEFINED;
1011 typedata[1] = typeinfo_offset;
1012 }
1013
1014 *encoded_type = typeoffset;
1015 *width = 0;
1016 *alignment = 1;
1017
1018 if(type->type == RPC_FC_IP) {
1019 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1020 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1021 if ((typedata[0] == ((0x7fff << 16) | VT_PTR)) && (typedata[1] == *encoded_type)) break;
1022 }
1023 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1024 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1025 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1026
1027 typedata[0] = (0x7fff << 16) | VT_PTR;
1028 typedata[1] = *encoded_type;
1029 }
1030 *encoded_type = typeoffset;
1031 *width = 4;
1032 *alignment = 4;
1033 *decoded_size += 8;
1034 }
1035 break;
1036 }
1037
1038 default:
1039 error("encode_type: unrecognized type %d.\n", vt);
1040 *encoded_type = default_type;
1041 *width = 0;
1042 *alignment = 1;
1043 break;
1044 }
1045
1046 return 0;
1047 }
1048
1049 static void dump_type(type_t *t)
1050 {
1051 chat("dump_type: %p name %s type %d ref %p attrs %p\n", t, t->name, t->type, t->ref, t->attrs);
1052 if(t->ref) dump_type(t->ref);
1053 }
1054
1055 static int encode_var(
1056 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
1057 type_t *type, /* [I] The type description to encode. */
1058 var_t *var, /* [I] The var to encode. */
1059 int *encoded_type, /* [O] The encoded type description. */
1060 int *width, /* [O] The width of the type, or NULL. */
1061 int *alignment, /* [O] The alignment of the type, or NULL. */
1062 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1063 {
1064 int typeoffset;
1065 int *typedata;
1066 int target_type;
1067 int child_size;
1068 int vt;
1069 int scratch;
1070
1071 if (!width) width = &scratch;
1072 if (!alignment) alignment = &scratch;
1073 if (!decoded_size) decoded_size = &scratch;
1074 *decoded_size = 0;
1075
1076 chat("encode_var: var %p type %p type->name %s type->ref %p\n",
1077 var, type, type->name ? type->name : "NULL", type->ref);
1078
1079 if (type->declarray) {
1080 int num_dims, elements = 1, arrayoffset;
1081 type_t *atype;
1082 int *arraydata;
1083
1084 num_dims = 0;
1085 for (atype = type; atype->declarray; atype = atype->ref)
1086 ++num_dims;
1087
1088 chat("array with %d dimensions\n", num_dims);
1089 encode_var(typelib, atype, var, &target_type, width, alignment, NULL);
1090 arrayoffset = ctl2_alloc_segment(typelib, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(long), 0);
1091 arraydata = (void *)&typelib->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1092
1093 arraydata[0] = target_type;
1094 arraydata[1] = num_dims;
1095 arraydata[1] |= ((num_dims * 2 * sizeof(long)) << 16);
1096
1097 arraydata += 2;
1098 for (atype = type; atype->declarray; atype = atype->ref)
1099 {
1100 arraydata[0] = atype->dim;
1101 arraydata[1] = 0;
1102 arraydata += 2;
1103 elements *= atype->dim;
1104 }
1105
1106 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1107 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1108
1109 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1110 typedata[1] = arrayoffset;
1111
1112 *encoded_type = typeoffset;
1113 *width = *width * elements;
1114 *decoded_size = 20 /*sizeof(ARRAYDESC)*/ + (num_dims - 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1115 return 0;
1116 }
1117
1118 vt = get_type_vt(type);
1119 if (vt == VT_PTR) {
1120 int skip_ptr = encode_var(typelib, type->ref, var, &target_type, NULL, NULL, &child_size);
1121
1122 if(skip_ptr == 2) {
1123 chat("encode_var: skipping ptr\n");
1124 *encoded_type = target_type;
1125 *decoded_size = child_size;
1126 *width = 4;
1127 *alignment = 4;
1128 return 0;
1129 }
1130
1131 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1132 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1133 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
1134 }
1135
1136 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1137 int mix_field;
1138
1139 if (target_type & 0x80000000) {
1140 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
1141 } else {
1142 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1143 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1144 }
1145
1146 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1147 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1148
1149 typedata[0] = (mix_field << 16) | VT_PTR;
1150 typedata[1] = target_type;
1151 }
1152
1153 *encoded_type = typeoffset;
1154
1155 *width = 4;
1156 *alignment = 4;
1157 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
1158 return 0;
1159 }
1160
1161 dump_type(type);
1162
1163 encode_type(typelib, vt, type, encoded_type, width, alignment, decoded_size);
1164 if(type->type == RPC_FC_IP) return 2;
1165 return 0;
1166 }
1167
1168 static unsigned long get_ulong_val(unsigned long val, int vt)
1169 {
1170 switch(vt) {
1171 case VT_I2:
1172 case VT_BOOL:
1173 case VT_UI2:
1174 return val & 0xffff;
1175 case VT_I1:
1176 case VT_UI1:
1177 return val & 0xff;
1178 }
1179
1180 return val;
1181 }
1182
1183 static void write_value(msft_typelib_t* typelib, int *out, int vt, void *value)
1184 {
1185 switch(vt) {
1186 case VT_I2:
1187 case VT_I4:
1188 case VT_R4:
1189 case VT_BOOL:
1190 case VT_I1:
1191 case VT_UI1:
1192 case VT_UI2:
1193 case VT_UI4:
1194 case VT_INT:
1195 case VT_UINT:
1196 case VT_HRESULT:
1197 case VT_PTR:
1198 {
1199 const unsigned long lv = get_ulong_val(*(unsigned long*)value, vt);
1200 if((lv & 0x3ffffff) == lv) {
1201 *out = 0x80000000;
1202 *out |= vt << 26;
1203 *out |= lv;
1204 } else {
1205 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, 8, 0);
1206 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1207 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], value, 4);
1208 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6]) = 0x5757;
1209 *out = offset;
1210 }
1211 return;
1212 }
1213 case VT_BSTR:
1214 {
1215 char *s = (char *) value;
1216 int len = strlen(s), seg_len = (len + 6 + 3) & ~0x3;
1217 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, seg_len, 0);
1218 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1219 *((unsigned int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2]) = len;
1220 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], value, len);
1221 len += 6;
1222 while(len < seg_len) {
1223 *((char *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+len]) = 0x57;
1224 len++;
1225 }
1226 *out = offset;
1227 return;
1228 }
1229
1230 default:
1231 warning("can't write value of type %d yet\n", vt);
1232 }
1233 return;
1234 }
1235
1236 static HRESULT set_custdata(msft_typelib_t *typelib, REFGUID guid,
1237 int vt, void *value, int *offset)
1238 {
1239 MSFT_GuidEntry guidentry;
1240 int guidoffset;
1241 int custoffset;
1242 int *custdata;
1243 int data_out;
1244
1245 guidentry.guid = *guid;
1246
1247 guidentry.hreftype = -1;
1248 guidentry.next_hash = -1;
1249
1250 guidoffset = ctl2_alloc_guid(typelib, &guidentry);
1251 write_value(typelib, &data_out, vt, value);
1252
1253 custoffset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATAGUID, 12, 0);
1254
1255 custdata = (int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1256 custdata[0] = guidoffset;
1257 custdata[1] = data_out;
1258 custdata[2] = *offset;
1259 *offset = custoffset;
1260
1261 return S_OK;
1262 }
1263
1264 static HRESULT add_func_desc(msft_typeinfo_t* typeinfo, const func_t *func, int index)
1265 {
1266 int offset, name_offset;
1267 int *typedata, typedata_size;
1268 int i, id, next_idx;
1269 int decoded_size, extra_attr = 0;
1270 int num_params = 0, num_optional = 0, num_defaults = 0;
1271 var_t *arg;
1272 char *namedata;
1273 const attr_t *attr;
1274 unsigned int funcflags = 0, callconv = 4 /* CC_STDCALL */;
1275 unsigned int funckind, invokekind = 1 /* INVOKE_FUNC */;
1276 int help_context = 0, help_string_context = 0, help_string_offset = -1;
1277 int entry = -1, entry_is_ord = 0;
1278
1279 chat("add_func_desc(%p,%d)\n", typeinfo, index);
1280
1281 id = ((0x6000 | (typeinfo->typeinfo->datatype2 & 0xffff)) << 16) | index;
1282
1283 switch(typeinfo->typekind) {
1284 case TKIND_DISPATCH:
1285 funckind = 0x4; /* FUNC_DISPATCH */
1286 break;
1287 case TKIND_MODULE:
1288 funckind = 0x3; /* FUNC_STATIC */
1289 break;
1290 default:
1291 funckind = 0x1; /* FUNC_PUREVIRTUAL */
1292 break;
1293 }
1294
1295 if (is_local( func->def->attrs )) {
1296 chat("add_func_desc: skipping local function\n");
1297 return S_FALSE;
1298 }
1299
1300 if (func->args)
1301 LIST_FOR_EACH_ENTRY( arg, func->args, var_t, entry )
1302 {
1303 num_params++;
1304 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1305 if(attr->type == ATTR_DEFAULTVALUE_EXPR || attr->type == ATTR_DEFAULTVALUE_STRING)
1306 num_defaults++;
1307 else if(attr->type == ATTR_OPTIONAL)
1308 num_optional++;
1309 }
1310 }
1311
1312 chat("add_func_desc: num of params %d\n", num_params);
1313
1314 name_offset = ctl2_alloc_name(typeinfo->typelib, func->def->name);
1315
1316 if (func->def->attrs) LIST_FOR_EACH_ENTRY( attr, func->def->attrs, const attr_t, entry ) {
1317 expr_t *expr = attr->u.pval;
1318 switch(attr->type) {
1319 case ATTR_BINDABLE:
1320 funcflags |= 0x4; /* FUNCFLAG_BINDABLE */
1321 break;
1322 case ATTR_DISPLAYBIND:
1323 funcflags |= 0x10; /* FUNCFLAG_DISPLAYBIND */
1324 break;
1325 case ATTR_ENTRY_ORDINAL:
1326 extra_attr = max(extra_attr, 3);
1327 entry = expr->cval;
1328 entry_is_ord = 1;
1329 break;
1330 case ATTR_ENTRY_STRING:
1331 extra_attr = max(extra_attr, 3);
1332 entry = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1333 break;
1334 case ATTR_HELPCONTEXT:
1335 extra_attr = max(extra_attr, 1);
1336 help_context = expr->u.lval;
1337 break;
1338 case ATTR_HELPSTRING:
1339 extra_attr = max(extra_attr, 2);
1340 help_string_offset = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1341 break;
1342 case ATTR_HELPSTRINGCONTEXT:
1343 extra_attr = max(extra_attr, 6);
1344 help_string_context = expr->u.lval;
1345 break;
1346 case ATTR_HIDDEN:
1347 funcflags |= 0x40; /* FUNCFLAG_FHIDDEN */
1348 break;
1349 case ATTR_ID:
1350 id = expr->cval;
1351 break;
1352 case ATTR_NONBROWSABLE:
1353 funcflags |= 0x400; /* FUNCFLAG_NONBROWSABLE */
1354 break;
1355 case ATTR_OUT:
1356 break;
1357 case ATTR_PROPGET:
1358 invokekind = 0x2; /* INVOKE_PROPERTYGET */
1359 break;
1360 case ATTR_PROPPUT:
1361 invokekind = 0x4; /* INVOKE_PROPERTYPUT */
1362 break;
1363 case ATTR_PROPPUTREF:
1364 invokekind = 0x8; /* INVOKE_PROPERTYPUTREF */
1365 break;
1366 case ATTR_RESTRICTED:
1367 funcflags |= 0x1; /* FUNCFLAG_FRESTRICTED */
1368 break;
1369 case ATTR_VARARG:
1370 if (num_optional || num_defaults)
1371 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1372 else
1373 num_optional = -1;
1374 break;
1375 default:
1376 warning("add_func_desc: ignoring attr %d\n", attr->type);
1377 break;
1378 }
1379 }
1380
1381 /* allocate type data space for us */
1382 typedata_size = 0x18 + extra_attr * sizeof(int) + (num_params * (num_defaults ? 16 : 12));
1383
1384 if (!typeinfo->func_data) {
1385 typeinfo->func_data = xmalloc(0x100);
1386 typeinfo->func_data_allocated = 0x100;
1387 typeinfo->func_data[0] = 0;
1388 }
1389
1390 if(typeinfo->func_data[0] + typedata_size + sizeof(int) > typeinfo->func_data_allocated) {
1391 typeinfo->func_data_allocated = max(typeinfo->func_data_allocated * 2,
1392 typeinfo->func_data[0] + typedata_size + sizeof(int));
1393 typeinfo->func_data = xrealloc(typeinfo->func_data, typeinfo->func_data_allocated);
1394 }
1395
1396 offset = typeinfo->func_data[0];
1397 typeinfo->func_data[0] += typedata_size;
1398 typedata = typeinfo->func_data + (offset >> 2) + 1;
1399
1400
1401 /* find func with the same name - if it exists use its id */
1402 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1403 if(name_offset == typeinfo->func_names[i]) {
1404 id = typeinfo->func_indices[i];
1405 break;
1406 }
1407 }
1408
1409 /* find the first func with the same id and link via the hiword of typedata[4] */
1410 next_idx = index;
1411 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1412 if(id == typeinfo->func_indices[i]) {
1413 next_idx = typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] >> 16;
1414 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] &= 0xffff;
1415 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] |= (index << 16);
1416 break;
1417 }
1418 }
1419
1420 /* fill out the basic type information */
1421 typedata[0] = typedata_size | (index << 16);
1422 encode_var(typeinfo->typelib, get_func_return_type(func), func->def, &typedata[1], NULL, NULL, &decoded_size);
1423 typedata[2] = funcflags;
1424 typedata[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size) << 16) | typeinfo->typeinfo->cbSizeVft;
1425 typedata[4] = (next_idx << 16) | (callconv << 8) | (invokekind << 3) | funckind;
1426 if(num_defaults) typedata[4] |= 0x1000;
1427 if(entry_is_ord) typedata[4] |= 0x2000;
1428 typedata[5] = (num_optional << 16) | num_params;
1429
1430 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1431 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1432 typedata[3] += (16 /*sizeof(ELEMDESC)*/ * num_params) << 16;
1433 typedata[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults) << 16;
1434
1435 switch(extra_attr) {
1436 case 6: typedata[11] = help_string_context;
1437 case 5: typedata[10] = -1;
1438 case 4: typedata[9] = -1;
1439 case 3: typedata[8] = entry;
1440 case 2: typedata[7] = help_string_offset;
1441 case 1: typedata[6] = help_context;
1442 case 0:
1443 break;
1444 default:
1445 warning("unknown number of optional attrs\n");
1446 }
1447
1448 if (func->args)
1449 {
1450 i = 0;
1451 LIST_FOR_EACH_ENTRY( arg, func->args, var_t, entry )
1452 {
1453 int paramflags = 0;
1454 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1455 int *defaultdata = num_defaults ? typedata + 6 + extra_attr + i : NULL;
1456
1457 if(defaultdata) *defaultdata = -1;
1458
1459 encode_var(typeinfo->typelib, arg->type, arg, paramdata, NULL, NULL, &decoded_size);
1460 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1461 switch(attr->type) {
1462 case ATTR_DEFAULTVALUE_EXPR:
1463 {
1464 int vt;
1465 expr_t *expr = (expr_t *)attr->u.pval;
1466 if (arg->type->type == RPC_FC_ENUM16)
1467 vt = VT_INT;
1468 else
1469 vt = get_type_vt(arg->type);
1470 paramflags |= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1471 chat("default value %ld\n", expr->cval);
1472 write_value(typeinfo->typelib, defaultdata, vt, &expr->cval);
1473 break;
1474 }
1475 case ATTR_DEFAULTVALUE_STRING:
1476 {
1477 char *s = (char *)attr->u.pval;
1478 int vt;
1479 if (arg->type->type == RPC_FC_ENUM16)
1480 vt = VT_INT;
1481 else
1482 vt = get_type_vt(arg->type);
1483 paramflags |= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1484 chat("default value '%s'\n", s);
1485 write_value(typeinfo->typelib, defaultdata, vt, s);
1486 break;
1487 }
1488 case ATTR_IN:
1489 paramflags |= 0x01; /* PARAMFLAG_FIN */
1490 break;
1491 case ATTR_OPTIONAL:
1492 paramflags |= 0x10; /* PARAMFLAG_FOPT */
1493 break;
1494 case ATTR_OUT:
1495 paramflags |= 0x02; /* PARAMFLAG_FOUT */
1496 break;
1497 case ATTR_RETVAL:
1498 paramflags |= 0x08; /* PARAMFLAG_FRETVAL */
1499 typedata[4] |= 0x4000;
1500 break;
1501 default:
1502 chat("unhandled param attr %d\n", attr->type);
1503 break;
1504 }
1505 }
1506 paramdata[1] = -1;
1507 paramdata[2] = paramflags;
1508 typedata[3] += decoded_size << 16;
1509 i++;
1510 }
1511 }
1512
1513 if(typeinfo->funcs_allocated == 0) {
1514 typeinfo->funcs_allocated = 10;
1515 typeinfo->func_indices = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1516 typeinfo->func_names = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1517 typeinfo->func_offsets = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1518 }
1519 if(typeinfo->funcs_allocated == (typeinfo->typeinfo->cElement & 0xffff)) {
1520 typeinfo->funcs_allocated *= 2;
1521 typeinfo->func_indices = xrealloc(typeinfo->func_indices, typeinfo->funcs_allocated * sizeof(int));
1522 typeinfo->func_names = xrealloc(typeinfo->func_names, typeinfo->funcs_allocated * sizeof(int));
1523 typeinfo->func_offsets = xrealloc(typeinfo->func_offsets, typeinfo->funcs_allocated * sizeof(int));
1524 }
1525
1526 /* update the index data */
1527 typeinfo->func_indices[typeinfo->typeinfo->cElement & 0xffff] = id;
1528 typeinfo->func_offsets[typeinfo->typeinfo->cElement & 0xffff] = offset;
1529 typeinfo->func_names[typeinfo->typeinfo->cElement & 0xffff] = name_offset;
1530
1531 /* ??? */
1532 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x20;
1533 typeinfo->typeinfo->res2 <<= 1;
1534 /* ??? */
1535 if (index < 2) typeinfo->typeinfo->res2 += num_params << 4;
1536
1537 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1538 typeinfo->typeinfo->res3 += 0x38 + num_params * 0x10;
1539 if(num_defaults) typeinfo->typeinfo->res3 += num_params * 0x4;
1540
1541 /* adjust size of VTBL */
1542 if(funckind != 0x3 /* FUNC_STATIC */)
1543 typeinfo->typeinfo->cbSizeVft += 4;
1544
1545 /* Increment the number of function elements */
1546 typeinfo->typeinfo->cElement += 1;
1547
1548 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + name_offset;
1549 if (*((INT *)namedata) == -1) {
1550 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1551 if(typeinfo->typekind == TKIND_MODULE)
1552 namedata[9] |= 0x10;
1553 } else
1554 namedata[9] &= ~0x10;
1555
1556 if(typeinfo->typekind == TKIND_MODULE)
1557 namedata[9] |= 0x20;
1558
1559 if (func->args)
1560 {
1561 i = 0;
1562 LIST_FOR_EACH_ENTRY( arg, func->args, var_t, entry )
1563 {
1564 /* don't give the last arg of a [propput*] func a name */
1565 if(i != num_params - 1 || (invokekind != 0x4 /* INVOKE_PROPERTYPUT */ && invokekind != 0x8 /* INVOKE_PROPERTYPUTREF */))
1566 {
1567 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1568 offset = ctl2_alloc_name(typeinfo->typelib, arg->name);
1569 paramdata[1] = offset;
1570 }
1571 i++;
1572 }
1573 }
1574 return S_OK;
1575 }
1576
1577 static HRESULT add_var_desc(msft_typeinfo_t *typeinfo, UINT index, var_t* var)
1578 {
1579 int offset, id;
1580 unsigned int typedata_size;
1581 INT *typedata;
1582 int var_datawidth;
1583 int var_alignment;
1584 int var_type_size, var_kind = 0 /* VAR_PERINSTANCE */;
1585 int alignment;
1586 int varflags = 0;
1587 const attr_t *attr;
1588 char *namedata;
1589 int var_num = (typeinfo->typeinfo->cElement >> 16) & 0xffff;
1590
1591 chat("add_var_desc(%d, %s)\n", index, var->name);
1592
1593 id = 0x40000000 + index;
1594
1595 if (var->attrs) LIST_FOR_EACH_ENTRY( attr, var->attrs, const attr_t, entry ) {
1596 expr_t *expr = attr->u.pval;
1597 switch(attr->type) {
1598 case ATTR_HIDDEN:
1599 varflags |= 0x40; /* VARFLAG_FHIDDEN */
1600 break;
1601 case ATTR_ID:
1602 id = expr->cval;
1603 break;
1604 case ATTR_READONLY:
1605 varflags |= 0x01; /* VARFLAG_FREADONLY */
1606 break;
1607 case ATTR_RESTRICTED:
1608 varflags |= 0x80; /* VARFLAG_FRESTRICTED */
1609 break;
1610 case ATTR_SOURCE:
1611 varflags |= 0x02; /* VARFLAG_FSOURCE */
1612 break;
1613 default:
1614 warning("AddVarDesc: unhandled attr type %d\n", attr->type);
1615 break;
1616 }
1617 }
1618
1619 /* allocate type data space for us */
1620 typedata_size = 0x14;
1621
1622 if (!typeinfo->var_data) {
1623 typeinfo->var_data = xmalloc(0x100);
1624 typeinfo->var_data_allocated = 0x100;
1625 typeinfo->var_data[0] = 0;
1626 }
1627
1628 if(typeinfo->var_data[0] + typedata_size + sizeof(int) > typeinfo->var_data_allocated) {
1629 typeinfo->var_data_allocated = max(typeinfo->var_data_allocated * 2,
1630 typeinfo->var_data[0] + typedata_size + sizeof(int));
1631 typeinfo->var_data = xrealloc(typeinfo->var_data, typeinfo->var_data_allocated);
1632 }
1633
1634 offset = typeinfo->var_data[0];
1635 typeinfo->var_data[0] += typedata_size;
1636 typedata = typeinfo->var_data + (offset >> 2) + 1;
1637
1638 /* fill out the basic type information */
1639 typedata[0] = typedata_size | (index << 16);
1640 typedata[2] = varflags;
1641 typedata[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1642
1643 if(typeinfo->vars_allocated == 0) {
1644 typeinfo->vars_allocated = 10;
1645 typeinfo->var_indices = xmalloc(typeinfo->vars_allocated * sizeof(int));
1646 typeinfo->var_names = xmalloc(typeinfo->vars_allocated * sizeof(int));
1647 typeinfo->var_offsets = xmalloc(typeinfo->vars_allocated * sizeof(int));
1648 }
1649 if(typeinfo->vars_allocated == var_num) {
1650 typeinfo->vars_allocated *= 2;
1651 typeinfo->var_indices = xrealloc(typeinfo->var_indices, typeinfo->vars_allocated * sizeof(int));
1652 typeinfo->var_names = xrealloc(typeinfo->var_names, typeinfo->vars_allocated * sizeof(int));
1653 typeinfo->var_offsets = xrealloc(typeinfo->var_offsets, typeinfo->vars_allocated * sizeof(int));
1654 }
1655 /* update the index data */
1656 typeinfo->var_indices[var_num] = id;
1657 typeinfo->var_names[var_num] = -1;
1658 typeinfo->var_offsets[var_num] = offset;
1659
1660 /* figure out type widths and whatnot */
1661 encode_var(typeinfo->typelib, var->type, var, &typedata[1], &var_datawidth,
1662 &var_alignment, &var_type_size);
1663
1664 /* pad out starting position to data width */
1665 typeinfo->datawidth += var_alignment - 1;
1666 typeinfo->datawidth &= ~(var_alignment - 1);
1667
1668 switch(typeinfo->typekind) {
1669 case TKIND_ENUM:
1670 write_value(typeinfo->typelib, &typedata[4], VT_I4, &var->eval->cval);
1671 var_kind = 2; /* VAR_CONST */
1672 var_type_size += 16; /* sizeof(VARIANT) */
1673 typeinfo->datawidth = var_datawidth;
1674 break;
1675 case TKIND_RECORD:
1676 typedata[4] = typeinfo->datawidth;
1677 typeinfo->datawidth += var_datawidth;
1678 break;
1679 case TKIND_DISPATCH:
1680 var_kind = 3; /* VAR_DISPATCH */
1681 typeinfo->datawidth = 4;
1682 var_alignment = 4;
1683 break;
1684 default:
1685 error("add_var_desc: unhandled type kind %d\n", typeinfo->typekind);
1686 break;
1687 }
1688
1689 /* add type description size to total required allocation */
1690 typedata[3] += var_type_size << 16 | var_kind;
1691
1692 /* fix type alignment */
1693 alignment = (typeinfo->typeinfo->typekind >> 11) & 0x1f;
1694 if (alignment < var_alignment) {
1695 alignment = var_alignment;
1696 typeinfo->typeinfo->typekind &= ~0xffc0;
1697 typeinfo->typeinfo->typekind |= alignment << 11 | alignment << 6;
1698 }
1699
1700 /* ??? */
1701 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x1a;
1702 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
1703 typeinfo->typeinfo->res2 <<= 1;
1704 }
1705
1706 /* ??? */
1707 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1708 typeinfo->typeinfo->res3 += 0x2c;
1709
1710 /* increment the number of variable elements */
1711 typeinfo->typeinfo->cElement += 0x10000;
1712
1713 /* pad data width to alignment */
1714 typeinfo->typeinfo->size = (typeinfo->datawidth + (alignment - 1)) & ~(alignment - 1);
1715
1716 offset = ctl2_alloc_name(typeinfo->typelib, var->name);
1717 if (offset == -1) return E_OUTOFMEMORY;
1718
1719 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
1720 if (*((INT *)namedata) == -1) {
1721 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1722 if(typeinfo->typekind != TKIND_DISPATCH)
1723 namedata[9] |= 0x10;
1724 } else
1725 namedata[9] &= ~0x10;
1726
1727 if (typeinfo->typekind == TKIND_ENUM) {
1728 namedata[9] |= 0x20;
1729 }
1730 typeinfo->var_names[var_num] = offset;
1731
1732 return S_OK;
1733 }
1734
1735 static HRESULT add_impl_type(msft_typeinfo_t *typeinfo, type_t *ref, importinfo_t *importinfo)
1736 {
1737 if(importinfo) {
1738 alloc_importinfo(typeinfo->typelib, importinfo);
1739 typeinfo->typeinfo->datatype1 = importinfo->offset+1;
1740 }else {
1741 if(ref->typelib_idx == -1)
1742 add_interface_typeinfo(typeinfo->typelib, ref);
1743 if(ref->typelib_idx == -1)
1744 error("add_impl_type: unable to add inherited interface\n");
1745
1746 typeinfo->typeinfo->datatype1 = typeinfo->typelib->typelib_typeinfo_offsets[ref->typelib_idx];
1747 }
1748
1749 typeinfo->typeinfo->cImplTypes++;
1750 return S_OK;
1751 }
1752
1753 static msft_typeinfo_t *create_msft_typeinfo(msft_typelib_t *typelib, enum type_kind kind,
1754 const char *name, const attr_list_t *attrs)
1755 {
1756 const attr_t *attr;
1757 msft_typeinfo_t *msft_typeinfo;
1758 int nameoffset;
1759 int typeinfo_offset;
1760 MSFT_TypeInfoBase *typeinfo;
1761 MSFT_GuidEntry guidentry;
1762
1763 chat("create_msft_typeinfo: name %s kind %d\n", name, kind);
1764
1765 msft_typeinfo = xmalloc(sizeof(*msft_typeinfo));
1766 memset( msft_typeinfo, 0, sizeof(*msft_typeinfo) );
1767
1768 msft_typeinfo->typelib = typelib;
1769
1770 nameoffset = ctl2_alloc_name(typelib, name);
1771 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
1772 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
1773
1774 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
1775 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
1776
1777 msft_typeinfo->typekind = kind;
1778 msft_typeinfo->typeinfo = typeinfo;
1779
1780 typeinfo->typekind |= kind | 0x20;
1781
1782 if(kind == TKIND_COCLASS)
1783 typeinfo->flags |= 0x2; /* TYPEFLAG_FCANCREATE */
1784
1785 if (attrs) LIST_FOR_EACH_ENTRY( attr, attrs, const attr_t, entry ) {
1786 switch(attr->type) {
1787 case ATTR_AGGREGATABLE:
1788 if (kind == TKIND_COCLASS)
1789 typeinfo->flags |= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1790 break;
1791
1792 case ATTR_APPOBJECT:
1793 if (kind == TKIND_COCLASS)
1794 typeinfo->flags |= 0x1; /* TYPEFLAG_FAPPOBJECT */
1795 break;
1796
1797 case ATTR_CONTROL:
1798 if (kind == TKIND_COCLASS)
1799 typeinfo->flags |= 0x20; /* TYPEFLAG_FCONTROL */
1800 break;
1801
1802 case ATTR_DISPINTERFACE:
1803 break;
1804
1805 case ATTR_DLLNAME:
1806 {
1807 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1808 typeinfo->datatype1 = offset;
1809 break;
1810 }
1811
1812 case ATTR_DUAL:
1813 /* FIXME: check interface is compatible */
1814 typeinfo->typekind = (typeinfo->typekind & ~0xff) | 0x34;
1815 typeinfo->flags |= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1816 break;
1817
1818 case ATTR_HELPCONTEXT:
1819 {
1820 expr_t *expr = (expr_t*)attr->u.pval;
1821 typeinfo->helpcontext = expr->cval;
1822 break;
1823 }
1824 case ATTR_HELPSTRING:
1825 {
1826 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1827 if (offset == -1) break;
1828 typeinfo->docstringoffs = offset;
1829 break;
1830 }
1831 case ATTR_HELPSTRINGCONTEXT:
1832 {
1833 expr_t *expr = (expr_t*)attr->u.pval;
1834 typeinfo->helpstringcontext = expr->cval;
1835 break;
1836 }
1837 case ATTR_HIDDEN:
1838 typeinfo->flags |= 0x10; /* TYPEFLAG_FHIDDEN */
1839 break;
1840
1841 case ATTR_LOCAL:
1842 break;
1843
1844 case ATTR_NONCREATABLE:
1845 typeinfo->flags &= ~0x2; /* TYPEFLAG_FCANCREATE */
1846 break;
1847
1848 case ATTR_NONEXTENSIBLE:
1849 typeinfo->flags |= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1850 break;
1851
1852 case ATTR_OBJECT:
1853 break;
1854
1855 case ATTR_ODL:
1856 break;
1857
1858 case ATTR_OLEAUTOMATION:
1859 typeinfo->flags |= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1860 break;
1861
1862 case ATTR_PUBLIC:
1863 break;
1864
1865 case ATTR_RESTRICTED:
1866 typeinfo->flags |= 0x200; /* TYPEFLAG_FRESTRICTED */
1867 break;
1868
1869 case ATTR_UUID:
1870 guidentry.guid = *(GUID*)attr->u.pval;
1871 guidentry.hreftype = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1872 guidentry.next_hash = -1;
1873 typeinfo->posguid = ctl2_alloc_guid(typelib, &guidentry);
1874 #if 0
1875 if (IsEqualIID(guid, &IID_IDispatch)) {
1876 typelib->typelib_header.dispatchpos = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1877 }
1878 #endif
1879 break;
1880
1881 case ATTR_VERSION:
1882 typeinfo->version = attr->u.ival;
1883 break;
1884
1885 default:
1886 warning("create_msft_typeinfo: ignoring attr %d\n", attr->type);
1887 break;
1888 }
1889 }
1890
1891 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = msft_typeinfo;
1892 typelib->last_typeinfo = msft_typeinfo;
1893 if (!typelib->typeinfos) typelib->typeinfos = msft_typeinfo;
1894
1895
1896 return msft_typeinfo;
1897 }
1898
1899 static void add_dispatch(msft_typelib_t *typelib)
1900 {
1901 int guid_offset, impfile_offset;
1902 MSFT_GuidEntry guidentry;
1903 MSFT_ImpInfo impinfo;
1904 GUID stdole = {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1905 GUID iid_idispatch = {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1906
1907 if(typelib->typelib_header.dispatchpos != -1) return;
1908
1909 guidentry.guid = stdole;
1910 guidentry.hreftype = 2;
1911 guidentry.next_hash = -1;
1912 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
1913 impfile_offset = alloc_importfile(typelib, guid_offset, 2, 0, "stdole2.tlb");
1914
1915 guidentry.guid = iid_idispatch;
1916 guidentry.hreftype = 1;
1917 guidentry.next_hash = -1;
1918 impinfo.flags = TKIND_INTERFACE << 24 | MSFT_IMPINFO_OFFSET_IS_GUID;
1919 impinfo.oImpFile = impfile_offset;
1920 impinfo.oGuid = ctl2_alloc_guid(typelib, &guidentry);
1921 typelib->typelib_header.dispatchpos = alloc_msft_importinfo(typelib, &impinfo) | 0x01;
1922 }
1923
1924 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface)
1925 {
1926 int idx = 0;
1927 const func_t *func;
1928 var_t *var;
1929 msft_typeinfo_t *msft_typeinfo;
1930
1931 if (-1 < dispinterface->typelib_idx)
1932 return;
1933
1934 dispinterface->typelib_idx = typelib->typelib_header.nrtypeinfos;
1935 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_DISPATCH, dispinterface->name,
1936 dispinterface->attrs);
1937
1938 msft_typeinfo->typeinfo->size = 4;
1939 msft_typeinfo->typeinfo->typekind |= 0x2100;
1940
1941 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
1942 add_dispatch(typelib);
1943 msft_typeinfo->typeinfo->cImplTypes = 1;
1944
1945 /* count the no of funcs, as the variable indices come after the funcs */
1946 if (dispinterface->funcs)
1947 LIST_FOR_EACH_ENTRY( func, dispinterface->funcs, const func_t, entry ) idx++;
1948
1949 if (dispinterface->fields_or_args)
1950 LIST_FOR_EACH_ENTRY( var, dispinterface->fields_or_args, var_t, entry )
1951 add_var_desc(msft_typeinfo, idx++, var);
1952
1953 if (dispinterface->funcs)
1954 {
1955 idx = 0;
1956 LIST_FOR_EACH_ENTRY( func, dispinterface->funcs, const func_t, entry )
1957 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
1958 idx++;
1959 }
1960 }
1961
1962 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface)
1963 {
1964 int idx = 0;
1965 const func_t *func;
1966 type_t *ref;
1967 msft_typeinfo_t *msft_typeinfo;
1968 importinfo_t *ref_importinfo = NULL;
1969 int num_parents = 0, num_funcs = 0;
1970 const type_t *derived;
1971
1972 if (-1 < interface->typelib_idx)
1973 return;
1974
1975 if (is_attr(interface->attrs, ATTR_DISPINTERFACE))
1976 return add_dispinterface_typeinfo(typelib, interface);
1977
1978 /* midl adds the parent interface first, unless the parent itself
1979 has no parent (i.e. it stops before IUnknown). */
1980
1981 if(interface->ref) {
1982 ref_importinfo = find_importinfo(typelib, interface->ref->name);
1983
1984 if(!ref_importinfo && interface->ref->ref && interface->ref->typelib_idx == -1)
1985 add_interface_typeinfo(typelib, interface->ref);
1986 }
1987
1988 interface->typelib_idx = typelib->typelib_header.nrtypeinfos;
1989 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_INTERFACE, interface->name, interface->attrs);
1990 msft_typeinfo->typeinfo->size = 4;
1991 msft_typeinfo->typeinfo->typekind |= 0x2200;
1992
1993 for (derived = interface->ref; derived; derived = derived->ref)
1994 if (derived->name && !strcmp(derived->name, "IDispatch"))
1995 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
1996
1997 /* can't be dual if it doesn't derive from IDispatch */
1998 if (!(msft_typeinfo->typeinfo->flags & 0x1000)) /* TYPEFLAG_FDISPATCHABLE */
1999 msft_typeinfo->typeinfo->flags &= ~0x40; /* TYPEFLAG_FDUAL */
2000
2001 if(interface->ref)
2002 add_impl_type(msft_typeinfo, interface->ref, ref_importinfo);
2003
2004 /* count the number of inherited interfaces and non-local functions */
2005 for(ref = interface->ref; ref; ref = ref->ref) {
2006 num_parents++;
2007 if (ref->funcs)
2008 LIST_FOR_EACH_ENTRY( func, ref->funcs, const func_t, entry )
2009 if (!is_local(func->def->attrs)) num_funcs++;
2010 }
2011 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2012 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * 4;
2013
2014 if (interface->funcs)
2015 LIST_FOR_EACH_ENTRY( func, interface->funcs, const func_t, entry )
2016 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2017 idx++;
2018 }
2019
2020 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure)
2021 {
2022 int idx = 0;
2023 var_t *cur;
2024 msft_typeinfo_t *msft_typeinfo;
2025
2026 if (-1 < structure->typelib_idx)
2027 return;
2028
2029 structure->typelib_idx = typelib->typelib_header.nrtypeinfos;
2030 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_RECORD, structure->name, structure->attrs);
2031 msft_typeinfo->typeinfo->size = 0;
2032
2033 if (structure->fields_or_args)
2034 LIST_FOR_EACH_ENTRY( cur, structure->fields_or_args, var_t, entry )
2035 add_var_desc(msft_typeinfo, idx++, cur);
2036 }
2037
2038 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration)
2039 {
2040 int idx = 0;
2041 var_t *cur;
2042 msft_typeinfo_t *msft_typeinfo;
2043
2044 enumeration->typelib_idx = typelib->typelib_header.nrtypeinfos;
2045 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ENUM, enumeration->name, enumeration->attrs);
2046 msft_typeinfo->typeinfo->size = 0;
2047
2048 if (enumeration->fields_or_args)
2049 LIST_FOR_EACH_ENTRY( cur, enumeration->fields_or_args, var_t, entry )
2050 add_var_desc(msft_typeinfo, idx++, cur);
2051 }
2052
2053 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *tdef)
2054 {
2055 msft_typeinfo_t *msft_typeinfo;
2056 int alignment;
2057
2058 if (-1 < tdef->typelib_idx)
2059 return;
2060
2061 tdef->typelib_idx = typelib->typelib_header.nrtypeinfos;
2062 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ALIAS, tdef->name, tdef->attrs);
2063 encode_type(typelib, get_type_vt(tdef->orig), tdef->orig, &msft_typeinfo->typeinfo->datatype1, &msft_typeinfo->typeinfo->size,
2064 &alignment, &msft_typeinfo->typeinfo->datatype2);
2065 msft_typeinfo->typeinfo->typekind |= (alignment << 11 | alignment << 6);
2066 }
2067
2068 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls)
2069 {
2070 msft_typeinfo_t *msft_typeinfo;
2071 ifref_t *iref;
2072 int num_ifaces = 0, offset, i;
2073 MSFT_RefRecord *ref, *first = NULL, *first_source = NULL;
2074 int have_default = 0, have_default_source = 0;
2075 const attr_t *attr;
2076
2077 if (-1 < cls->typelib_idx)
2078 return;
2079
2080 cls->typelib_idx = typelib->typelib_header.nrtypeinfos;
2081 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_COCLASS, cls->name, cls->attrs);
2082
2083 if (cls->ifaces) LIST_FOR_EACH_ENTRY( iref, cls->ifaces, ifref_t, entry ) num_ifaces++;
2084
2085 offset = msft_typeinfo->typeinfo->datatype1 = ctl2_alloc_segment(typelib, MSFT_SEG_REFERENCES,
2086 num_ifaces * sizeof(*ref), 0);
2087
2088 i = 0;
2089 if (cls->ifaces) LIST_FOR_EACH_ENTRY( iref, cls->ifaces, ifref_t, entry ) {
2090 if(iref->iface->typelib_idx == -1)
2091 add_interface_typeinfo(typelib, iref->iface);
2092 ref = (MSFT_RefRecord*) (typelib->typelib_segment_data[MSFT_SEG_REFERENCES] + offset + i * sizeof(*ref));
2093 ref->reftype = typelib->typelib_typeinfo_offsets[iref->iface->typelib_idx];
2094 ref->flags = 0;
2095 ref->oCustData = -1;
2096 ref->onext = -1;
2097 if(i < num_ifaces - 1)
2098 ref->onext = offset + (i + 1) * sizeof(*ref);
2099
2100 if (iref->attrs) LIST_FOR_EACH_ENTRY( attr, iref->attrs, const attr_t, entry ) {
2101 switch(attr->type) {
2102 case ATTR_DEFAULT:
2103 ref->flags |= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2104 break;
2105 case ATTR_RESTRICTED:
2106 ref->flags |= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2107 break;
2108 case ATTR_SOURCE:
2109 ref->flags |= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2110 break;
2111 default:
2112 warning("add_coclass_typeinfo: unhandled attr %d\n", attr->type);
2113 }
2114 }
2115 if(ref->flags & 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2116 if(ref->flags & 0x2) /* IMPLTYPEFLAG_SOURCE */
2117 have_default_source = 1;
2118 else
2119 have_default = 1;
2120 }
2121
2122 /* If the interface is non-restricted and we haven't already had one then
2123 remember it so that we can use it as a default later */
2124 if((ref->flags & 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2125 if(ref->flags & 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2126 if(!first_source)
2127 first_source = ref;
2128 }
2129 else if(!first)
2130 first = ref;
2131 }
2132 i++;
2133 }
2134
2135 /* If we haven't had a default interface, then set the default flags on the
2136 first ones */
2137 if(!have_default && first)
2138 first->flags |= 0x1;
2139 if(!have_default_source && first_source)
2140 first_source->flags |= 0x1;
2141
2142 msft_typeinfo->typeinfo->cImplTypes = num_ifaces;
2143 msft_typeinfo->typeinfo->size = 4;
2144 msft_typeinfo->typeinfo->typekind |= 0x2200;
2145 }
2146
2147 static void add_module_typeinfo(msft_typelib_t *typelib, type_t *module)
2148 {
2149 int idx = 0;
2150 const func_t *func;
2151 msft_typeinfo_t *msft_typeinfo;
2152
2153 if (-1 < module->typelib_idx)
2154 return;
2155
2156 module->typelib_idx = typelib->typelib_header.nrtypeinfos;
2157 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_MODULE, module->name, module->attrs);
2158 msft_typeinfo->typeinfo->typekind |= 0x0a00;
2159
2160 if (module->funcs)
2161 LIST_FOR_EACH_ENTRY( func, module->funcs, const func_t, entry )
2162 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2163 idx++;
2164
2165 msft_typeinfo->typeinfo->size = idx;
2166 }
2167
2168 static void add_entry(msft_typelib_t *typelib, typelib_entry_t *entry)
2169 {
2170 switch(entry->type->kind) {
2171 case TKIND_INTERFACE:
2172 case TKIND_DISPATCH:
2173 add_interface_typeinfo(typelib, entry->type);
2174 break;
2175
2176 case TKIND_RECORD:
2177 add_structure_typeinfo(typelib, entry->type);
2178 break;
2179
2180 case TKIND_ENUM:
2181 add_enum_typeinfo(typelib, entry->type);
2182 break;
2183
2184 case TKIND_ALIAS:
2185 add_typedef_typeinfo(typelib, entry->type);
2186 break;
2187
2188 case TKIND_COCLASS:
2189 add_coclass_typeinfo(typelib, entry->type);
2190 break;
2191
2192 case TKIND_MODULE:
2193 add_module_typeinfo(typelib, entry->type);
2194 break;
2195
2196 default:
2197 error("add_entry: unhandled type %d\n", entry->type->kind);
2198 break;
2199 }
2200 }
2201
2202 static void set_name(msft_typelib_t *typelib)
2203 {
2204 int offset;
2205
2206 offset = ctl2_alloc_name(typelib, typelib->typelib->name);
2207 if (offset == -1) return;
2208 typelib->typelib_header.NameOffset = offset;
2209 return;
2210 }
2211
2212 static void set_version(msft_typelib_t *typelib)
2213 {
2214 typelib->typelib_header.version = get_attrv( typelib->typelib->attrs, ATTR_VERSION );
2215 }
2216
2217 static void set_guid(msft_typelib_t *typelib)
2218 {
2219 MSFT_GuidEntry guidentry;
2220 int offset;
2221 void *ptr;
2222 GUID guid = {0,0,0,{0,0,0,0,0,0}};
2223
2224 guidentry.guid = guid;
2225 guidentry.hreftype = -2;
2226 guidentry.next_hash = -1;
2227
2228 ptr = get_attrp( typelib->typelib->attrs, ATTR_UUID );
2229 if (ptr) guidentry.guid = *(GUID *)ptr;
2230
2231 offset = ctl2_alloc_guid(typelib, &guidentry);
2232 typelib->typelib_header.posguid = offset;
2233
2234 return;
2235 }
2236
2237 static void set_doc_string(msft_typelib_t *typelib)
2238 {
2239 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRING );
2240
2241 if (str)
2242 {
2243 int offset = ctl2_alloc_string(typelib, str);
2244 if (offset != -1) typelib->typelib_header.helpstring = offset;
2245 }
2246 }
2247
2248 static void set_help_file_name(msft_typelib_t *typelib)
2249 {
2250 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPFILE );
2251
2252 if (str)
2253 {
2254 int offset = ctl2_alloc_string(typelib, str);
2255 if (offset != -1)
2256 {
2257 typelib->typelib_header.helpfile = offset;
2258 typelib->typelib_header.varflags |= 0x10;
2259 }
2260 }
2261 }
2262
2263 static void set_help_context(msft_typelib_t *typelib)
2264 {
2265 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPCONTEXT );
2266 if (expr) typelib->typelib_header.helpcontext = expr->cval;
2267 }
2268
2269 static void set_help_string_dll(msft_typelib_t *typelib)
2270 {
2271 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGDLL );
2272
2273 if (str)
2274 {
2275 int offset = ctl2_alloc_string(typelib, str);
2276 if (offset != -1)
2277 {
2278 typelib->help_string_dll_offset = offset;
2279 typelib->typelib_header.varflags |= 0x100;
2280 }
2281 }
2282 }
2283
2284 static void set_help_string_context(msft_typelib_t *typelib)
2285 {
2286 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGCONTEXT );
2287 if (expr) typelib->typelib_header.helpstringcontext = expr->cval;
2288 }
2289
2290 static void set_lcid(msft_typelib_t *typelib)
2291 {
2292 typelib->typelib_header.lcid2 = 0x0;
2293 return;
2294 }
2295
2296 static void set_lib_flags(msft_typelib_t *typelib)
2297 {
2298 const attr_t *attr;
2299
2300 typelib->typelib_header.flags = 0;
2301 if (!typelib->typelib->attrs) return;
2302 LIST_FOR_EACH_ENTRY( attr, typelib->typelib->attrs, const attr_t, entry )
2303 {
2304 switch(attr->type) {
2305 case ATTR_CONTROL:
2306 typelib->typelib_header.flags |= 0x02; /* LIBFLAG_FCONTROL */
2307 break;
2308 case ATTR_HIDDEN:
2309 typelib->typelib_header.flags |= 0x04; /* LIBFLAG_FHIDDEN */
2310 break;
2311 case ATTR_RESTRICTED:
2312 typelib->typelib_header.flags |= 0x01; /* LIBFLAG_FRESTRICTED */
2313 break;
2314 default:
2315 break;
2316 }
2317 }
2318 return;
2319 }
2320
2321 static int ctl2_write_chunk(int fd, void *segment, int length)
2322 {
2323 if (write(fd, segment, length) != length) {
2324 close(fd);
2325 return 0;
2326 }
2327 return -1;
2328 }
2329
2330 static int ctl2_write_segment(msft_typelib_t *typelib, int fd, int segment)
2331 {
2332 if (write(fd, typelib->typelib_segment_data[segment], typelib->typelib_segdir[segment].length)
2333 != typelib->typelib_segdir[segment].length) {
2334 close(fd);
2335 return 0;
2336 }
2337
2338 return -1;
2339 }
2340
2341 static void ctl2_finalize_typeinfos(msft_typelib_t *typelib, int filesize)
2342 {
2343 msft_typeinfo_t *typeinfo;
2344
2345 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2346 typeinfo->typeinfo->memoffset = filesize;
2347 if (typeinfo->func_data)
2348 filesize += typeinfo->func_data[0] + ((typeinfo->typeinfo->cElement & 0xffff) * 12);
2349 if (typeinfo->var_data)
2350 filesize += typeinfo->var_data[0] + (((typeinfo->typeinfo->cElement >> 16) & 0xffff) * 12);
2351 if (typeinfo->func_data || typeinfo->var_data)
2352 filesize += 4;
2353 }
2354 }
2355
2356 static int ctl2_finalize_segment(msft_typelib_t *typelib, int filepos, int segment)
2357 {
2358 if (typelib->typelib_segdir[segment].length) {
2359 typelib->typelib_segdir[segment].offset = filepos;
2360 } else {
2361 typelib->typelib_segdir[segment].offset = -1;
2362 }
2363
2364 return typelib->typelib_segdir[segment].length;
2365 }
2366
2367
2368 static void ctl2_write_typeinfos(msft_typelib_t *typelib, int fd)
2369 {
2370 msft_typeinfo_t *typeinfo;
2371 int typedata_size;
2372
2373 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2374 if (!typeinfo->func_data && !typeinfo->var_data) continue;
2375 typedata_size = 0;
2376 if (typeinfo->func_data)
2377 typedata_size = typeinfo->func_data[0];
2378 if (typeinfo->var_data)
2379 typedata_size += typeinfo->var_data[0];
2380 ctl2_write_chunk(fd, &typedata_size, sizeof(int));
2381 if (typeinfo->func_data)
2382 ctl2_write_chunk(fd, typeinfo->func_data + 1, typeinfo->func_data[0]);
2383 if (typeinfo->var_data)
2384 ctl2_write_chunk(fd, typeinfo->var_data + 1, typeinfo->var_data[0]);
2385 if (typeinfo->func_indices)
2386 ctl2_write_chunk(fd, typeinfo->func_indices, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2387 if (typeinfo->var_indices)
2388 ctl2_write_chunk(fd, typeinfo->var_indices, (typeinfo->typeinfo->cElement >> 16) * 4);
2389 if (typeinfo->func_names)
2390 ctl2_write_chunk(fd, typeinfo->func_names, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2391 if (typeinfo->var_names)
2392 ctl2_write_chunk(fd, typeinfo->var_names, (typeinfo->typeinfo->cElement >> 16) * 4);
2393 if (typeinfo->func_offsets)
2394 ctl2_write_chunk(fd, typeinfo->func_offsets, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2395 if (typeinfo->var_offsets) {
2396 int add = 0, i, offset;
2397 if(typeinfo->func_data)
2398 add = typeinfo->func_data[0];
2399 for(i = 0; i < (typeinfo->typeinfo->cElement >> 16); i++) {
2400 offset = typeinfo->var_offsets[i];
2401 offset += add;
2402 ctl2_write_chunk(fd, &offset, 4);
2403 }
2404 }
2405 }
2406 }
2407
2408 static int save_all_changes(msft_typelib_t *typelib)
2409 {
2410 int retval;
2411 int filepos;
2412 int fd;
2413
2414 chat("save_all_changes(%p)\n", typelib);
2415
2416 retval = TYPE_E_IOERROR;
2417
2418 fd = open(typelib->typelib->filename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0666);
2419 if (fd == -1) return retval;
2420
2421 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
2422 if(typelib->typelib_header.varflags & 0x100) filepos += 4; /* helpstringdll */
2423 filepos += typelib->typelib_header.nrtypeinfos * 4;
2424
2425 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEINFO);
2426 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUIDHASH);
2427 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUID);
2428 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_REFERENCES);
2429 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTINFO);
2430 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTFILES);
2431 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAMEHASH);
2432 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAME);
2433 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_STRING);
2434 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEDESC);
2435 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_ARRAYDESC);
2436 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATA);
2437 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATAGUID);
2438
2439 ctl2_finalize_typeinfos(typelib, filepos);
2440
2441 if (!ctl2_write_chunk(fd, &typelib->typelib_header, sizeof(typelib->typelib_header))) return retval;
2442 if(typelib->typelib_header.varflags & 0x100)
2443 if (!ctl2_write_chunk(fd, &typelib->help_string_dll_offset, sizeof(typelib->help_string_dll_offset)))
2444 return retval;
2445
2446 if (!ctl2_write_chunk(fd, typelib->typelib_typeinfo_offsets, typelib->typelib_header.nrtypeinfos * 4)) return retval;
2447 if (!ctl2_write_chunk(fd, &typelib->typelib_segdir, sizeof(typelib->typelib_segdir))) return retval;
2448 if (!ctl2_write_segment(typelib, fd, MSFT_SEG_TYPEINFO )) return retval;
2449 if (!ctl2_write_segment(typelib, fd, MSFT_SEG_GUIDHASH )) return retval;
2450 if (!ctl2_write_segment(typelib, fd, MSFT_SEG_GUID )) return retval;
2451 if (!ctl2_write_segment(typelib, fd, MSFT_SEG_REFERENCES )) return retval;
2452 if (!ctl2_write_segment(typelib, fd, MSFT_SEG_IMPORTINFO )) return retval;
2453 if (!ctl2_write_segment(typelib, fd, MSFT_SEG_IMPORTFILES )) return retval;
2454 if (!ctl2_write_segment(typelib, fd, MSFT_SEG_NAMEHASH )) return retval;
2455 if (!ctl2_write_segment(typelib, fd, MSFT_SEG_NAME )) return retval;
2456 if (!ctl2_write_segment(typelib, fd, MSFT_SEG_STRING )) return retval;
2457 if (!ctl2_write_segment(typelib, fd, MSFT_SEG_TYPEDESC )) return retval;
2458 if (!ctl2_write_segment(typelib, fd, MSFT_SEG_ARRAYDESC )) return retval;
2459 if (!ctl2_write_segment(typelib, fd, MSFT_SEG_CUSTDATA )) return retval;
2460 if (!ctl2_write_segment(typelib, fd, MSFT_SEG_CUSTDATAGUID)) return retval;
2461
2462 ctl2_write_typeinfos(typelib, fd);
2463
2464 if (close(fd) == -1) return retval;
2465
2466 retval = S_OK;
2467 return retval;
2468 }
2469
2470 int create_msft_typelib(typelib_t *typelib)
2471 {
2472 msft_typelib_t *msft;
2473 int failed = 0;
2474 typelib_entry_t *entry;
2475 time_t cur_time;
2476 char *time_override;
2477 unsigned int version = 5 << 24 | 1 << 16 | 164; /* 5.01.0164 */
2478 GUID midl_time_guid = {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2479 GUID midl_version_guid = {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2480
2481 msft = xmalloc(sizeof(*msft));
2482 memset(msft, 0, sizeof(*msft));
2483 msft->typelib = typelib;
2484
2485 ctl2_init_header(msft);
2486 ctl2_init_segdir(msft);
2487
2488 msft->typelib_header.varflags |= SYS_WIN32;
2489
2490 /*
2491 * The following two calls return an offset or -1 if out of memory. We
2492 * specifically need an offset of 0, however, so...
2493 */
2494 if (ctl2_alloc_segment(msft, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
2495 if (ctl2_alloc_segment(msft, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
2496
2497 if(failed)
2498 {
2499 free(msft);
2500 return 0;
2501 }
2502
2503 msft->typelib_guidhash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_GUIDHASH];
2504 msft->typelib_namehash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_NAMEHASH];
2505
2506 memset(msft->typelib_guidhash_segment, 0xff, 0x80);
2507 memset(msft->typelib_namehash_segment, 0xff, 0x200);
2508
2509 set_lib_flags(msft);
2510 set_lcid(msft);
2511 set_help_file_name(msft);
2512 set_doc_string(msft);
2513 set_guid(msft);
2514 set_version(msft);
2515 set_name(msft);
2516 set_help_context(msft);
2517 set_help_string_dll(msft);
2518 set_help_string_context(msft);
2519
2520 /* midl adds two sets of custom data to the library: the current unix time
2521 and midl's version number */
2522 time_override = getenv( "WIDL_TIME_OVERRIDE");
2523 cur_time = time_override ? atol( time_override) : time(NULL);
2524 set_custdata(msft, &midl_time_guid, VT_UI4, &cur_time, &msft->typelib_header.CustomDataOffset);
2525 set_custdata(msft, &midl_version_guid, VT_UI4, &version, &msft->typelib_header.CustomDataOffset);
2526
2527 LIST_FOR_EACH_ENTRY( entry, &typelib->entries, typelib_entry_t, entry )
2528 add_entry(msft, entry);
2529
2530 save_all_changes(msft);
2531 free(msft);
2532 return 1;
2533 }