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"
43 #include "widltypes.h"
44 #include "typelib_struct.h"
48 /* List of oleauto types that should be recognized by name.
49 * (most of) these seem to be intrinsic types in mktyplib.
50 * This table MUST be alphabetically sorted on the kw field.
52 static const struct oatype
{
59 {"DECIMAL", VT_DECIMAL
},
60 {"HRESULT", VT_HRESULT
},
62 {"LPWSTR", VT_LPWSTR
},
64 {"VARIANT", VT_VARIANT
},
65 {"VARIANT_BOOL", VT_BOOL
}
67 #define KWP(p) ((const struct oatype *)(p))
69 static int kw_cmp_func(const void *s1
, const void *s2
)
71 return strcmp(KWP(s1
)->kw
, KWP(s2
)->kw
);
74 static unsigned short builtin_vt(const type_t
*t
)
76 const char *kw
= t
->name
;
78 const struct oatype
*kwp
;
81 kwp
= bsearch(&key
, oatypes
, ARRAY_SIZE(oatypes
), sizeof(oatypes
[0]), kw_cmp_func
);
85 for (kwp
= NULL
, i
= 0; i
< ARRAY_SIZE(oatypes
); i
++)
86 if (!kw_cmp_func(&key
, &oatypes
[i
])) {
95 if (is_string_type (t
->attrs
, t
))
97 const type_t
*elem_type
;
99 elem_type
= type_array_get_element_type(t
);
101 elem_type
= type_pointer_get_ref_type(t
);
102 if (type_get_type(elem_type
) == TYPE_BASIC
)
104 switch (type_basic_get_type(elem_type
))
106 case TYPE_BASIC_CHAR
: return VT_LPSTR
;
107 case TYPE_BASIC_WCHAR
: return VT_LPWSTR
;
115 static int match(const char*n
, const char*m
)
118 return !strcmp(n
, m
);
121 unsigned short get_type_vt(type_t
*t
)
125 chat("get_type_vt: %p type->name %s\n", t
, t
->name
);
131 if (type_is_alias(t
) &&
132 (is_attr(t
->attrs
, ATTR_PUBLIC
) || is_attr(t
->attrs
, ATTR_WIREMARSHAL
)))
133 return VT_USERDEFINED
;
135 switch (type_get_type(t
)) {
137 switch (type_basic_get_type(t
)) {
138 case TYPE_BASIC_BYTE
:
140 case TYPE_BASIC_CHAR
:
141 case TYPE_BASIC_INT8
:
142 if (type_basic_get_sign(t
) > 0)
146 case TYPE_BASIC_WCHAR
:
147 return VT_I2
; /* mktyplib seems to parse wchar_t as short */
148 case TYPE_BASIC_INT16
:
149 if (type_basic_get_sign(t
) > 0)
154 if (type_basic_get_sign(t
) > 0)
158 case TYPE_BASIC_INT32
:
159 case TYPE_BASIC_LONG
:
160 case TYPE_BASIC_ERROR_STATUS_T
:
161 if (type_basic_get_sign(t
) > 0)
165 case TYPE_BASIC_INT64
:
166 case TYPE_BASIC_HYPER
:
167 if (type_basic_get_sign(t
) > 0)
171 case TYPE_BASIC_INT3264
:
172 if (pointer_size
== 8)
174 if (type_basic_get_sign(t
) > 0)
181 if (type_basic_get_sign(t
) > 0)
186 case TYPE_BASIC_FLOAT
:
188 case TYPE_BASIC_DOUBLE
:
190 case TYPE_BASIC_HANDLE
:
191 error("handles can't be used in typelibs\n");
199 if (type_array_is_decl_as_ptr(t
))
201 if (match(type_array_get_element_type(t
)->name
, "SAFEARRAY"))
209 if(match(t
->name
, "IUnknown"))
211 if(match(t
->name
, "IDispatch"))
213 return VT_USERDEFINED
;
220 case TYPE_ENCAPSULATED_UNION
:
221 return VT_USERDEFINED
;
227 case TYPE_APICONTRACT
:
228 /* not supposed to be here */
233 error("get_type_vt: functions not supported\n");
237 error("get_type_vt: bitfields not supported\n");
243 static void tlb_read(int fd
, void *buf
, int count
)
245 if(read(fd
, buf
, count
) < count
)
246 error("error while reading importlib.\n");
249 static void tlb_lseek(int fd
, off_t offset
)
251 if(lseek(fd
, offset
, SEEK_SET
) == -1)
252 error("lseek failed\n");
255 static void msft_read_guid(int fd
, MSFT_SegDir
*segdir
, int offset
, GUID
*guid
)
257 tlb_lseek(fd
, segdir
->pGuidTab
.offset
+offset
);
258 tlb_read(fd
, guid
, sizeof(GUID
));
261 static void read_msft_importlib(importlib_t
*importlib
, int fd
)
268 importlib
->allocated
= 0;
271 tlb_read(fd
, &header
, sizeof(header
));
273 importlib
->version
= header
.version
;
275 typeinfo_offs
= xmalloc(header
.nrtypeinfos
*sizeof(INT
));
276 tlb_read(fd
, typeinfo_offs
, header
.nrtypeinfos
*sizeof(INT
));
277 tlb_read(fd
, &segdir
, sizeof(segdir
));
279 msft_read_guid(fd
, &segdir
, header
.posguid
, &importlib
->guid
);
281 importlib
->ntypeinfos
= header
.nrtypeinfos
;
282 importlib
->importinfos
= xmalloc(importlib
->ntypeinfos
*sizeof(importinfo_t
));
284 for(i
=0; i
< importlib
->ntypeinfos
; i
++) {
285 MSFT_TypeInfoBase base
;
286 MSFT_NameIntro nameintro
;
289 tlb_lseek(fd
, sizeof(MSFT_Header
) + header
.nrtypeinfos
*sizeof(INT
) + sizeof(MSFT_SegDir
)
291 tlb_read(fd
, &base
, sizeof(base
));
293 importlib
->importinfos
[i
].importlib
= importlib
;
294 importlib
->importinfos
[i
].flags
= (base
.typekind
&0xf)<<24;
295 importlib
->importinfos
[i
].offset
= -1;
296 importlib
->importinfos
[i
].id
= i
;
298 if(base
.posguid
!= -1) {
299 importlib
->importinfos
[i
].flags
|= MSFT_IMPINFO_OFFSET_IS_GUID
;
300 msft_read_guid(fd
, &segdir
, base
.posguid
, &importlib
->importinfos
[i
].guid
);
302 else memset( &importlib
->importinfos
[i
].guid
, 0, sizeof(importlib
->importinfos
[i
].guid
));
304 tlb_lseek(fd
, segdir
.pNametab
.offset
+ base
.NameOffset
);
305 tlb_read(fd
, &nameintro
, sizeof(nameintro
));
307 len
= nameintro
.namelen
& 0xff;
309 importlib
->importinfos
[i
].name
= xmalloc(len
+1);
310 tlb_read(fd
, importlib
->importinfos
[i
].name
, len
);
311 importlib
->importinfos
[i
].name
[len
] = 0;
317 static int open_typelib(const char *name
)
322 file_name
= wpp_find_include(name
, NULL
);
324 return open(name
, O_RDONLY
| O_BINARY
);
326 fd
= open(file_name
, O_RDONLY
| O_BINARY
);
331 static void read_importlib(importlib_t
*importlib
)
336 fd
= open_typelib(importlib
->name
);
338 /* widl extension: if importlib name has no .tlb extension, try using .tlb */
340 const char *p
= strrchr(importlib
->name
, '.');
341 size_t len
= p
? p
- importlib
->name
: strlen(importlib
->name
);
342 if(strcmp(importlib
->name
+ len
, ".tlb")) {
343 char *tlb_name
= xmalloc(len
+ 5);
344 memcpy(tlb_name
, importlib
->name
, len
);
345 strcpy(tlb_name
+ len
, ".tlb");
346 fd
= open_typelib(tlb_name
);
352 error("Could not find importlib %s.\n", importlib
->name
);
354 tlb_read(fd
, &magic
, sizeof(magic
));
358 read_msft_importlib(importlib
, fd
);
361 error("Wrong or unsupported typelib magic %x\n", magic
);
367 void add_importlib(const char *name
, typelib_t
*typelib
)
369 importlib_t
*importlib
;
373 LIST_FOR_EACH_ENTRY( importlib
, &typelib
->importlibs
, importlib_t
, entry
)
374 if(!strcmp(name
, importlib
->name
))
377 chat("add_importlib: %s\n", name
);
379 importlib
= xmalloc(sizeof(*importlib
));
380 memset( importlib
, 0, sizeof(*importlib
) );
381 importlib
->name
= xstrdup(name
);
383 read_importlib(importlib
);
384 list_add_head( &typelib
->importlibs
, &importlib
->entry
);