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"
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
46 #include "widltypes.h"
47 #include "typelib_struct.h"
50 static typelib_t
*typelib
;
52 int is_ptr(const type_t
*t
)
54 return type_get_type(t
) == TYPE_POINTER
;
57 int is_array(const type_t
*t
)
59 return type_get_type(t
) == TYPE_ARRAY
;
62 /* List of oleauto types that should be recognized by name.
63 * (most of) these seem to be intrinsic types in mktyplib.
64 * This table MUST be alphabetically sorted on the kw field.
66 static const struct oatype
{
73 {"DECIMAL", VT_DECIMAL
},
74 {"HRESULT", VT_HRESULT
},
76 {"LPWSTR", VT_LPWSTR
},
78 {"VARIANT", VT_VARIANT
},
79 {"VARIANT_BOOL", VT_BOOL
}
81 #define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
82 #define KWP(p) ((const struct oatype *)(p))
84 static int kw_cmp_func(const void *s1
, const void *s2
)
86 return strcmp(KWP(s1
)->kw
, KWP(s2
)->kw
);
89 static unsigned short builtin_vt(const type_t
*t
)
91 const char *kw
= t
->name
;
93 const struct oatype
*kwp
;
96 kwp
= bsearch(&key
, oatypes
, NTYPES
, sizeof(oatypes
[0]), kw_cmp_func
);
100 for (kwp
=NULL
, i
=0; i
< NTYPES
; i
++)
101 if (!kw_cmp_func(&key
, &oatypes
[i
])) {
110 if (is_string_type (t
->attrs
, t
))
112 const type_t
*elem_type
;
114 elem_type
= type_array_get_element(t
);
116 elem_type
= type_pointer_get_ref(t
);
117 if (type_get_type(elem_type
) == TYPE_BASIC
)
119 switch (type_basic_get_type(elem_type
))
121 case TYPE_BASIC_CHAR
: return VT_LPSTR
;
122 case TYPE_BASIC_WCHAR
: return VT_LPWSTR
;
130 static int match(const char*n
, const char*m
)
133 return !strcmp(n
, m
);
136 unsigned short get_type_vt(type_t
*t
)
140 chat("get_type_vt: %p type->name %s\n", t
, t
->name
);
146 if (type_is_alias(t
) && is_attr(t
->attrs
, ATTR_PUBLIC
))
147 return VT_USERDEFINED
;
149 switch (type_get_type(t
)) {
151 switch (type_basic_get_type(t
)) {
152 case TYPE_BASIC_BYTE
:
154 case TYPE_BASIC_CHAR
:
155 case TYPE_BASIC_INT8
:
156 if (type_basic_get_sign(t
) > 0)
160 case TYPE_BASIC_WCHAR
:
161 return VT_I2
; /* mktyplib seems to parse wchar_t as short */
162 case TYPE_BASIC_INT16
:
163 if (type_basic_get_sign(t
) > 0)
168 if (type_basic_get_sign(t
) > 0)
172 case TYPE_BASIC_INT32
:
173 case TYPE_BASIC_ERROR_STATUS_T
:
174 if (type_basic_get_sign(t
) > 0)
178 case TYPE_BASIC_INT64
:
179 case TYPE_BASIC_HYPER
:
180 if (type_basic_get_sign(t
) > 0)
184 case TYPE_BASIC_FLOAT
:
186 case TYPE_BASIC_DOUBLE
:
188 case TYPE_BASIC_HANDLE
:
189 error("handles can't be used in typelibs\n");
197 if (type_array_is_decl_as_ptr(t
))
199 if (match(type_array_get_element(t
)->name
, "SAFEARRAY"))
203 error("get_type_vt: array types not supported\n");
207 if(match(t
->name
, "IUnknown"))
209 if(match(t
->name
, "IDispatch"))
211 return VT_USERDEFINED
;
218 case TYPE_ENCAPSULATED_UNION
:
219 return VT_USERDEFINED
;
225 /* aliases should be filtered out by the type_get_type call above */
230 error("get_type_vt: functions not supported\n");
236 void start_typelib(typelib_t
*typelib_type
)
238 if (!do_typelib
) return;
240 typelib
= typelib_type
;
241 typelib
->filename
= xstrdup(typelib_name
);
244 void end_typelib(void)
246 if (!typelib
) return;
248 create_msft_typelib(typelib
);
251 static void tlb_read(int fd
, void *buf
, int count
)
253 if(read(fd
, buf
, count
) < count
)
254 error("error while reading importlib.\n");
257 static void tlb_lseek(int fd
, off_t offset
)
259 if(lseek(fd
, offset
, SEEK_SET
) == -1)
260 error("lseek failed\n");
263 static void msft_read_guid(int fd
, MSFT_SegDir
*segdir
, int offset
, GUID
*guid
)
265 tlb_lseek(fd
, segdir
->pGuidTab
.offset
+offset
);
266 tlb_read(fd
, guid
, sizeof(GUID
));
269 static void read_msft_importlib(importlib_t
*importlib
, int fd
)
276 importlib
->allocated
= 0;
279 tlb_read(fd
, &header
, sizeof(header
));
281 importlib
->version
= header
.version
;
283 typeinfo_offs
= xmalloc(header
.nrtypeinfos
*sizeof(INT
));
284 tlb_read(fd
, typeinfo_offs
, header
.nrtypeinfos
*sizeof(INT
));
285 tlb_read(fd
, &segdir
, sizeof(segdir
));
287 msft_read_guid(fd
, &segdir
, header
.posguid
, &importlib
->guid
);
289 importlib
->ntypeinfos
= header
.nrtypeinfos
;
290 importlib
->importinfos
= xmalloc(importlib
->ntypeinfos
*sizeof(importinfo_t
));
292 for(i
=0; i
< importlib
->ntypeinfos
; i
++) {
293 MSFT_TypeInfoBase base
;
294 MSFT_NameIntro nameintro
;
297 tlb_lseek(fd
, sizeof(MSFT_Header
) + header
.nrtypeinfos
*sizeof(INT
) + sizeof(MSFT_SegDir
)
299 tlb_read(fd
, &base
, sizeof(base
));
301 importlib
->importinfos
[i
].importlib
= importlib
;
302 importlib
->importinfos
[i
].flags
= (base
.typekind
&0xf)<<24;
303 importlib
->importinfos
[i
].offset
= -1;
304 importlib
->importinfos
[i
].id
= i
;
306 if(base
.posguid
!= -1) {
307 importlib
->importinfos
[i
].flags
|= MSFT_IMPINFO_OFFSET_IS_GUID
;
308 msft_read_guid(fd
, &segdir
, base
.posguid
, &importlib
->importinfos
[i
].guid
);
310 else memset( &importlib
->importinfos
[i
].guid
, 0, sizeof(importlib
->importinfos
[i
].guid
));
312 tlb_lseek(fd
, segdir
.pNametab
.offset
+ base
.NameOffset
);
313 tlb_read(fd
, &nameintro
, sizeof(nameintro
));
315 len
= nameintro
.namelen
& 0xff;
317 importlib
->importinfos
[i
].name
= xmalloc(len
+1);
318 tlb_read(fd
, importlib
->importinfos
[i
].name
, len
);
319 importlib
->importinfos
[i
].name
[len
] = 0;
325 static void read_importlib(importlib_t
*importlib
)
331 file_name
= wpp_find_include(importlib
->name
, NULL
);
333 fd
= open(file_name
, O_RDONLY
| O_BINARY
);
336 fd
= open(importlib
->name
, O_RDONLY
| O_BINARY
);
340 error("Could not open importlib %s.\n", importlib
->name
);
342 tlb_read(fd
, &magic
, sizeof(magic
));
346 read_msft_importlib(importlib
, fd
);
349 error("Wrong or unsupported typelib magic %x\n", magic
);
355 void add_importlib(const char *name
)
357 importlib_t
*importlib
;
361 LIST_FOR_EACH_ENTRY( importlib
, &typelib
->importlibs
, importlib_t
, entry
)
362 if(!strcmp(name
, importlib
->name
))
365 chat("add_importlib: %s\n", name
);
367 importlib
= xmalloc(sizeof(*importlib
));
368 memset( importlib
, 0, sizeof(*importlib
) );
369 importlib
->name
= xstrdup(name
);
371 read_importlib(importlib
);
372 list_add_head( &typelib
->importlibs
, &importlib
->entry
);