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
))
114 fc
= type_array_get_element(t
)->type
;
116 fc
= type_pointer_get_ref(t
)->type
;
119 case RPC_FC_CHAR
: return VT_LPSTR
;
120 case RPC_FC_WCHAR
: return VT_LPWSTR
;
127 static int match(const char*n
, const char*m
)
130 return !strcmp(n
, m
);
133 unsigned short get_type_vt(type_t
*t
)
137 chat("get_type_vt: %p type->name %s\n", t
, t
->name
);
143 if (type_is_alias(t
) && is_attr(t
->attrs
, ATTR_PUBLIC
))
144 return VT_USERDEFINED
;
154 return VT_I2
; /* mktyplib seems to parse wchar_t as short */
160 if (match(t
->name
, "int")) return VT_INT
;
163 if (match(t
->name
, "int")) return VT_UINT
;
166 if (t
->sign
< 0) return VT_UI8
;
167 if (match(t
->name
, "MIDL_uhyper")) return VT_UI8
;
177 case RPC_FC_SMFARRAY
:
178 case RPC_FC_LGFARRAY
:
179 case RPC_FC_SMVARRAY
:
180 case RPC_FC_LGVARRAY
:
183 case RPC_FC_BOGUS_ARRAY
:
186 if (match(t
->ref
->name
, "SAFEARRAY"))
191 error("get_type_vt: unknown-deref-type: %d\n", t
->ref
->type
);
194 if(match(t
->name
, "IUnknown"))
196 if(match(t
->name
, "IDispatch"))
198 return VT_USERDEFINED
;
204 case RPC_FC_CPSTRUCT
:
205 case RPC_FC_CVSTRUCT
:
206 case RPC_FC_BOGUS_STRUCT
:
209 return VT_USERDEFINED
;
213 error("get_type_vt: unknown type: 0x%02x\n", t
->type
);
218 void start_typelib(typelib_t
*typelib_type
)
220 if (!do_typelib
) return;
222 typelib
= typelib_type
;
223 typelib
->filename
= xstrdup(typelib_name
);
226 void end_typelib(void)
228 if (!typelib
) return;
230 create_msft_typelib(typelib
);
233 static void tlb_read(int fd
, void *buf
, int count
)
235 if(read(fd
, buf
, count
) < count
)
236 error("error while reading importlib.\n");
239 static void tlb_lseek(int fd
, off_t offset
)
241 if(lseek(fd
, offset
, SEEK_SET
) == -1)
242 error("lseek failed\n");
245 static void msft_read_guid(int fd
, MSFT_SegDir
*segdir
, int offset
, GUID
*guid
)
247 tlb_lseek(fd
, segdir
->pGuidTab
.offset
+offset
);
248 tlb_read(fd
, guid
, sizeof(GUID
));
251 static void read_msft_importlib(importlib_t
*importlib
, int fd
)
258 importlib
->allocated
= 0;
261 tlb_read(fd
, &header
, sizeof(header
));
263 importlib
->version
= header
.version
;
265 typeinfo_offs
= xmalloc(header
.nrtypeinfos
*sizeof(INT
));
266 tlb_read(fd
, typeinfo_offs
, header
.nrtypeinfos
*sizeof(INT
));
267 tlb_read(fd
, &segdir
, sizeof(segdir
));
269 msft_read_guid(fd
, &segdir
, header
.posguid
, &importlib
->guid
);
271 importlib
->ntypeinfos
= header
.nrtypeinfos
;
272 importlib
->importinfos
= xmalloc(importlib
->ntypeinfos
*sizeof(importinfo_t
));
274 for(i
=0; i
< importlib
->ntypeinfos
; i
++) {
275 MSFT_TypeInfoBase base
;
276 MSFT_NameIntro nameintro
;
279 tlb_lseek(fd
, sizeof(MSFT_Header
) + header
.nrtypeinfos
*sizeof(INT
) + sizeof(MSFT_SegDir
)
281 tlb_read(fd
, &base
, sizeof(base
));
283 importlib
->importinfos
[i
].importlib
= importlib
;
284 importlib
->importinfos
[i
].flags
= (base
.typekind
&0xf)<<24;
285 importlib
->importinfos
[i
].offset
= -1;
286 importlib
->importinfos
[i
].id
= i
;
288 if(base
.posguid
!= -1) {
289 importlib
->importinfos
[i
].flags
|= MSFT_IMPINFO_OFFSET_IS_GUID
;
290 msft_read_guid(fd
, &segdir
, base
.posguid
, &importlib
->importinfos
[i
].guid
);
292 else memset( &importlib
->importinfos
[i
].guid
, 0, sizeof(importlib
->importinfos
[i
].guid
));
294 tlb_lseek(fd
, segdir
.pNametab
.offset
+ base
.NameOffset
);
295 tlb_read(fd
, &nameintro
, sizeof(nameintro
));
297 len
= nameintro
.namelen
& 0xff;
299 importlib
->importinfos
[i
].name
= xmalloc(len
+1);
300 tlb_read(fd
, importlib
->importinfos
[i
].name
, len
);
301 importlib
->importinfos
[i
].name
[len
] = 0;
307 static void read_importlib(importlib_t
*importlib
)
313 file_name
= wpp_find_include(importlib
->name
, NULL
);
315 fd
= open(file_name
, O_RDONLY
| O_BINARY
);
318 fd
= open(importlib
->name
, O_RDONLY
| O_BINARY
);
322 error("Could not open importlib %s.\n", importlib
->name
);
324 tlb_read(fd
, &magic
, sizeof(magic
));
328 read_msft_importlib(importlib
, fd
);
331 error("Wrong or unsupported typelib magic %x\n", magic
);
337 void add_importlib(const char *name
)
339 importlib_t
*importlib
;
343 LIST_FOR_EACH_ENTRY( importlib
, &typelib
->importlibs
, importlib_t
, entry
)
344 if(!strcmp(name
, importlib
->name
))
347 chat("add_importlib: %s\n", name
);
349 importlib
= xmalloc(sizeof(*importlib
));
350 memset( importlib
, 0, sizeof(*importlib
) );
351 importlib
->name
= xstrdup(name
);
353 read_importlib(importlib
);
354 list_add_head( &typelib
->importlibs
, &importlib
->entry
);