4 * Copyright 2004 Ove Kaaven
5 * Copyright 2006 Jacek Caban for CodeWeavers
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.
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.
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
23 #include "wine/port.h"
47 #include "widltypes.h"
48 #include "typelib_struct.h"
52 static typelib_t
*typelib
;
55 /* List of oleauto types that should be recognized by name.
56 * (most of) these seem to be intrinsic types in mktyplib.
57 * This table MUST be alphabetically sorted on the kw field.
59 static const struct oatype
{
66 {"DECIMAL", VT_DECIMAL
},
67 {"HRESULT", VT_HRESULT
},
69 {"LPWSTR", VT_LPWSTR
},
71 {"VARIANT", VT_VARIANT
},
72 {"VARIANT_BOOL", VT_BOOL
}
74 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
75 #define KWP(p) ((const struct oatype *)(p))
77 static int kw_cmp_func(const void *s1
, const void *s2
)
79 return strcmp(KWP(s1
)->kw
, KWP(s2
)->kw
);
82 static unsigned short builtin_vt(const type_t
*t
)
84 const char *kw
= t
->name
;
86 const struct oatype
*kwp
;
89 kwp
= bsearch(&key
, oatypes
, NTYPES
, sizeof(oatypes
[0]), kw_cmp_func
);
93 for (kwp
=NULL
, i
=0; i
< NTYPES
; i
++)
94 if (!kw_cmp_func(&key
, &oatypes
[i
])) {
103 if (is_string_type (t
->attrs
, t
))
105 const type_t
*elem_type
;
107 elem_type
= type_array_get_element(t
);
109 elem_type
= type_pointer_get_ref(t
);
110 if (type_get_type(elem_type
) == TYPE_BASIC
)
112 switch (type_basic_get_type(elem_type
))
114 case TYPE_BASIC_CHAR
: return VT_LPSTR
;
115 case TYPE_BASIC_WCHAR
: return VT_LPWSTR
;
123 static int match(const char*n
, const char*m
)
126 return !strcmp(n
, m
);
129 unsigned short get_type_vt(type_t
*t
)
133 chat("get_type_vt: %p type->name %s\n", t
, t
->name
);
139 if (type_is_alias(t
) && is_attr(t
->attrs
, ATTR_PUBLIC
))
140 return VT_USERDEFINED
;
142 switch (type_get_type(t
)) {
144 switch (type_basic_get_type(t
)) {
145 case TYPE_BASIC_BYTE
:
147 case TYPE_BASIC_CHAR
:
148 case TYPE_BASIC_INT8
:
149 if (type_basic_get_sign(t
) > 0)
153 case TYPE_BASIC_WCHAR
:
154 return VT_I2
; /* mktyplib seems to parse wchar_t as short */
155 case TYPE_BASIC_INT16
:
156 if (type_basic_get_sign(t
) > 0)
161 if (type_basic_get_sign(t
) > 0)
165 case TYPE_BASIC_INT32
:
166 case TYPE_BASIC_LONG
:
167 case TYPE_BASIC_ERROR_STATUS_T
:
168 if (type_basic_get_sign(t
) > 0)
172 case TYPE_BASIC_INT64
:
173 case TYPE_BASIC_HYPER
:
174 if (type_basic_get_sign(t
) > 0)
178 case TYPE_BASIC_INT3264
:
179 if (pointer_size
== 8)
181 if (type_basic_get_sign(t
) > 0)
188 if (type_basic_get_sign(t
) > 0)
193 case TYPE_BASIC_FLOAT
:
195 case TYPE_BASIC_DOUBLE
:
197 case TYPE_BASIC_HANDLE
:
198 error("handles can't be used in typelibs\n");
206 if (type_array_is_decl_as_ptr(t
))
208 if (match(type_array_get_element(t
)->name
, "SAFEARRAY"))
216 if(match(t
->name
, "IUnknown"))
218 if(match(t
->name
, "IDispatch"))
220 return VT_USERDEFINED
;
227 case TYPE_ENCAPSULATED_UNION
:
228 return VT_USERDEFINED
;
234 /* aliases should be filtered out by the type_get_type call above */
239 error("get_type_vt: functions not supported\n");
243 error("get_type_vt: bitfields not supported\n");
250 void start_typelib(typelib_t
*typelib_type
)
252 if (!do_typelib
) return;
253 typelib
= typelib_type
;
256 void end_typelib(void)
258 if (!typelib
) return;
260 create_msft_typelib(typelib
);
264 static void tlb_read(int fd
, void *buf
, int count
)
266 if(read(fd
, buf
, count
) < count
)
267 error("error while reading importlib.\n");
270 static void tlb_lseek(int fd
, off_t offset
)
272 if(lseek(fd
, offset
, SEEK_SET
) == -1)
273 error("lseek failed\n");
276 static void msft_read_guid(int fd
, MSFT_SegDir
*segdir
, int offset
, GUID
*guid
)
278 tlb_lseek(fd
, segdir
->pGuidTab
.offset
+offset
);
279 tlb_read(fd
, guid
, sizeof(GUID
));
282 static void read_msft_importlib(importlib_t
*importlib
, int fd
)
289 importlib
->allocated
= 0;
292 tlb_read(fd
, &header
, sizeof(header
));
294 importlib
->version
= header
.version
;
296 typeinfo_offs
= xmalloc(header
.nrtypeinfos
*sizeof(INT
));
297 tlb_read(fd
, typeinfo_offs
, header
.nrtypeinfos
*sizeof(INT
));
298 tlb_read(fd
, &segdir
, sizeof(segdir
));
300 msft_read_guid(fd
, &segdir
, header
.posguid
, &importlib
->guid
);
302 importlib
->ntypeinfos
= header
.nrtypeinfos
;
303 importlib
->importinfos
= xmalloc(importlib
->ntypeinfos
*sizeof(importinfo_t
));
305 for(i
=0; i
< importlib
->ntypeinfos
; i
++) {
306 MSFT_TypeInfoBase base
;
307 MSFT_NameIntro nameintro
;
310 tlb_lseek(fd
, sizeof(MSFT_Header
) + header
.nrtypeinfos
*sizeof(INT
) + sizeof(MSFT_SegDir
)
312 tlb_read(fd
, &base
, sizeof(base
));
314 importlib
->importinfos
[i
].importlib
= importlib
;
315 importlib
->importinfos
[i
].flags
= (base
.typekind
&0xf)<<24;
316 importlib
->importinfos
[i
].offset
= -1;
317 importlib
->importinfos
[i
].id
= i
;
319 if(base
.posguid
!= -1) {
320 importlib
->importinfos
[i
].flags
|= MSFT_IMPINFO_OFFSET_IS_GUID
;
321 msft_read_guid(fd
, &segdir
, base
.posguid
, &importlib
->importinfos
[i
].guid
);
323 else memset( &importlib
->importinfos
[i
].guid
, 0, sizeof(importlib
->importinfos
[i
].guid
));
325 tlb_lseek(fd
, segdir
.pNametab
.offset
+ base
.NameOffset
);
326 tlb_read(fd
, &nameintro
, sizeof(nameintro
));
328 len
= nameintro
.namelen
& 0xff;
330 importlib
->importinfos
[i
].name
= xmalloc(len
+1);
331 tlb_read(fd
, importlib
->importinfos
[i
].name
, len
);
332 importlib
->importinfos
[i
].name
[len
] = 0;
338 static int open_typelib(const char *name
)
343 file_name
= wpp_find_include(name
, NULL
);
345 return open(name
, O_RDONLY
| O_BINARY
);
347 fd
= open(file_name
, O_RDONLY
| O_BINARY
);
352 static void read_importlib(importlib_t
*importlib
)
357 fd
= open_typelib(importlib
->name
);
359 /* widl extension: if importlib name has no .tlb extension, try using .tlb */
361 const char *p
= strrchr(importlib
->name
, '.');
362 size_t len
= p
? p
- importlib
->name
: strlen(importlib
->name
);
363 if(strcmp(importlib
->name
+ len
, ".tlb")) {
364 char *tlb_name
= xmalloc(len
+ 5);
365 memcpy(tlb_name
, importlib
->name
, len
);
366 strcpy(tlb_name
+ len
, ".tlb");
367 fd
= open_typelib(tlb_name
);
373 error("Could not find importlib %s.\n", importlib
->name
);
375 tlb_read(fd
, &magic
, sizeof(magic
));
379 read_msft_importlib(importlib
, fd
);
382 error("Wrong or unsupported typelib magic %x\n", magic
);
389 void add_importlib(const char *name
)
391 void add_importlib(const char *name
, typelib_t
*typelib
)
394 importlib_t
*importlib
;
398 LIST_FOR_EACH_ENTRY( importlib
, &typelib
->importlibs
, importlib_t
, entry
)
399 if(!strcmp(name
, importlib
->name
))
402 chat("add_importlib: %s\n", name
);
404 importlib
= xmalloc(sizeof(*importlib
));
405 memset( importlib
, 0, sizeof(*importlib
) );
406 importlib
->name
= xstrdup(name
);
408 read_importlib(importlib
);
409 list_add_head( &typelib
->importlibs
, &importlib
->entry
);