typelib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * IDL Compiler
  3. *
  4. * Copyright 2004 Ove Kaaven
  5. * Copyright 2006 Jacek Caban for CodeWeavers
  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. #include "config.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include "widl.h"
  28. #include "windef.h"
  29. #include "winbase.h"
  30. #include "utils.h"
  31. #include "wpp_private.h"
  32. #include "parser.h"
  33. #include "header.h"
  34. #include "typelib.h"
  35. #include "widltypes.h"
  36. #include "typelib_struct.h"
  37. #include "typetree.h"
  38. /* List of oleauto types that should be recognized by name.
  39. * (most of) these seem to be intrinsic types in mktyplib.
  40. * This table MUST be alphabetically sorted on the kw field.
  41. */
  42. static const struct oatype {
  43. const char *kw;
  44. unsigned short vt;
  45. } oatypes[] = {
  46. {"BSTR", VT_BSTR},
  47. {"CURRENCY", VT_CY},
  48. {"DATE", VT_DATE},
  49. {"DECIMAL", VT_DECIMAL},
  50. {"HRESULT", VT_HRESULT},
  51. {"LPSTR", VT_LPSTR},
  52. {"LPWSTR", VT_LPWSTR},
  53. {"SCODE", VT_ERROR},
  54. {"VARIANT", VT_VARIANT},
  55. {"VARIANT_BOOL", VT_BOOL}
  56. };
  57. #define KWP(p) ((const struct oatype *)(p))
  58. static int kw_cmp_func(const void *s1, const void *s2)
  59. {
  60. return strcmp(KWP(s1)->kw, KWP(s2)->kw);
  61. }
  62. static unsigned short builtin_vt(const type_t *t)
  63. {
  64. const char *kw = t->name;
  65. struct oatype key;
  66. const struct oatype *kwp;
  67. key.kw = kw;
  68. #ifdef KW_BSEARCH
  69. kwp = bsearch(&key, oatypes, ARRAY_SIZE(oatypes), sizeof(oatypes[0]), kw_cmp_func);
  70. #else
  71. {
  72. unsigned int i;
  73. for (kwp = NULL, i = 0; i < ARRAY_SIZE(oatypes); i++)
  74. if (!kw_cmp_func(&key, &oatypes[i])) {
  75. kwp = &oatypes[i];
  76. break;
  77. }
  78. }
  79. #endif
  80. if (kwp) {
  81. return kwp->vt;
  82. }
  83. if (is_string_type (t->attrs, t))
  84. {
  85. const type_t *elem_type;
  86. if (is_array(t))
  87. elem_type = type_array_get_element_type(t);
  88. else
  89. elem_type = type_pointer_get_ref_type(t);
  90. if (type_get_type(elem_type) == TYPE_BASIC)
  91. {
  92. switch (type_basic_get_type(elem_type))
  93. {
  94. case TYPE_BASIC_CHAR: return VT_LPSTR;
  95. case TYPE_BASIC_WCHAR: return VT_LPWSTR;
  96. default: break;
  97. }
  98. }
  99. }
  100. return 0;
  101. }
  102. static int match(const char*n, const char*m)
  103. {
  104. if (!n) return 0;
  105. return !strcmp(n, m);
  106. }
  107. unsigned short get_type_vt(type_t *t)
  108. {
  109. unsigned short vt;
  110. chat("get_type_vt: %p type->name %s\n", t, t->name);
  111. if (t->name) {
  112. vt = builtin_vt(t);
  113. if (vt) return vt;
  114. }
  115. if (type_is_alias(t) &&
  116. (is_attr(t->attrs, ATTR_PUBLIC) || is_attr(t->attrs, ATTR_WIREMARSHAL)))
  117. return VT_USERDEFINED;
  118. switch (type_get_type(t)) {
  119. case TYPE_BASIC:
  120. switch (type_basic_get_type(t)) {
  121. case TYPE_BASIC_BYTE:
  122. return VT_UI1;
  123. case TYPE_BASIC_CHAR:
  124. case TYPE_BASIC_INT8:
  125. if (type_basic_get_sign(t) > 0)
  126. return VT_UI1;
  127. else
  128. return VT_I1;
  129. case TYPE_BASIC_WCHAR:
  130. return VT_I2; /* mktyplib seems to parse wchar_t as short */
  131. case TYPE_BASIC_INT16:
  132. if (type_basic_get_sign(t) > 0)
  133. return VT_UI2;
  134. else
  135. return VT_I2;
  136. case TYPE_BASIC_INT:
  137. if (type_basic_get_sign(t) > 0)
  138. return VT_UINT;
  139. else
  140. return VT_INT;
  141. case TYPE_BASIC_INT32:
  142. case TYPE_BASIC_LONG:
  143. case TYPE_BASIC_ERROR_STATUS_T:
  144. if (type_basic_get_sign(t) > 0)
  145. return VT_UI4;
  146. else
  147. return VT_I4;
  148. case TYPE_BASIC_INT64:
  149. case TYPE_BASIC_HYPER:
  150. if (type_basic_get_sign(t) > 0)
  151. return VT_UI8;
  152. else
  153. return VT_I8;
  154. case TYPE_BASIC_INT3264:
  155. if (pointer_size == 8)
  156. {
  157. if (type_basic_get_sign(t) > 0)
  158. return VT_UI8;
  159. else
  160. return VT_I8;
  161. }
  162. else
  163. {
  164. if (type_basic_get_sign(t) > 0)
  165. return VT_UI4;
  166. else
  167. return VT_I4;
  168. }
  169. case TYPE_BASIC_FLOAT:
  170. return VT_R4;
  171. case TYPE_BASIC_DOUBLE:
  172. return VT_R8;
  173. case TYPE_BASIC_HANDLE:
  174. error("handles can't be used in typelibs\n");
  175. }
  176. break;
  177. case TYPE_POINTER:
  178. return VT_PTR;
  179. case TYPE_ARRAY:
  180. if (type_array_is_decl_as_ptr(t))
  181. {
  182. if (match(type_array_get_element_type(t)->name, "SAFEARRAY"))
  183. return VT_SAFEARRAY;
  184. return VT_PTR;
  185. }
  186. else
  187. return VT_CARRAY;
  188. case TYPE_INTERFACE:
  189. if(match(t->name, "IUnknown"))
  190. return VT_UNKNOWN;
  191. if(match(t->name, "IDispatch"))
  192. return VT_DISPATCH;
  193. return VT_USERDEFINED;
  194. case TYPE_ENUM:
  195. case TYPE_STRUCT:
  196. case TYPE_COCLASS:
  197. case TYPE_MODULE:
  198. case TYPE_UNION:
  199. case TYPE_ENCAPSULATED_UNION:
  200. case TYPE_RUNTIMECLASS:
  201. case TYPE_DELEGATE:
  202. return VT_USERDEFINED;
  203. case TYPE_VOID:
  204. return VT_VOID;
  205. case TYPE_ALIAS:
  206. case TYPE_APICONTRACT:
  207. case TYPE_PARAMETERIZED_TYPE:
  208. case TYPE_PARAMETER:
  209. /* not supposed to be here */
  210. assert(0);
  211. break;
  212. case TYPE_FUNCTION:
  213. error("get_type_vt: functions not supported\n");
  214. break;
  215. case TYPE_BITFIELD:
  216. error("get_type_vt: bitfields not supported\n");
  217. break;
  218. }
  219. return 0;
  220. }
  221. static void msft_read_guid(void *data, MSFT_SegDir *segdir, int offset, GUID *guid)
  222. {
  223. memcpy( guid, (char *)data + segdir->pGuidTab.offset + offset, sizeof(*guid) );
  224. }
  225. static void read_msft_importlib(importlib_t *importlib, void *data, unsigned int size)
  226. {
  227. MSFT_Header *header = data;
  228. MSFT_SegDir *segdir;
  229. int *typeinfo_offs;
  230. int i;
  231. importlib->allocated = 0;
  232. importlib->version = header->version;
  233. typeinfo_offs = (int *)(header + 1);
  234. segdir = (MSFT_SegDir *)(typeinfo_offs + header->nrtypeinfos);
  235. msft_read_guid(data, segdir, header->posguid, &importlib->guid);
  236. importlib->ntypeinfos = header->nrtypeinfos;
  237. importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
  238. for(i=0; i < importlib->ntypeinfos; i++) {
  239. MSFT_TypeInfoBase *base = (MSFT_TypeInfoBase *)((char *)(segdir + 1) + typeinfo_offs[i]);
  240. MSFT_NameIntro *nameintro;
  241. int len;
  242. importlib->importinfos[i].importlib = importlib;
  243. importlib->importinfos[i].flags = (base->typekind & 0xf)<<24;
  244. importlib->importinfos[i].offset = -1;
  245. importlib->importinfos[i].id = i;
  246. if(base->posguid != -1) {
  247. importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
  248. msft_read_guid(data, segdir, base->posguid, &importlib->importinfos[i].guid);
  249. }
  250. else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
  251. nameintro = (MSFT_NameIntro *)((char *)data + segdir->pNametab.offset + base->NameOffset);
  252. len = nameintro->namelen & 0xff;
  253. importlib->importinfos[i].name = xmalloc(len+1);
  254. memcpy( importlib->importinfos[i].name, nameintro + 1, len );
  255. importlib->importinfos[i].name[len] = 0;
  256. }
  257. }
  258. static unsigned int rva_to_va( const IMAGE_NT_HEADERS32 *nt, unsigned int rva )
  259. {
  260. IMAGE_SECTION_HEADER *sec = (IMAGE_SECTION_HEADER *)((char *)&nt->OptionalHeader + nt->FileHeader.SizeOfOptionalHeader);
  261. unsigned int i;
  262. for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
  263. if (sec->VirtualAddress <= rva && sec->VirtualAddress + sec->Misc.VirtualSize > rva)
  264. return sec->PointerToRawData + (rva - sec->VirtualAddress);
  265. error( "no PE section found for addr %x\n", rva );
  266. }
  267. static void read_pe_importlib(importlib_t *importlib, void *data, unsigned int size)
  268. {
  269. IMAGE_DOS_HEADER *dos = data;
  270. IMAGE_NT_HEADERS32 *nt;
  271. IMAGE_DATA_DIRECTORY *dir;
  272. IMAGE_RESOURCE_DIRECTORY *root, *resdir;
  273. IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
  274. IMAGE_RESOURCE_DATA_ENTRY *resdata;
  275. void *ptr;
  276. unsigned int i, va;
  277. if (dos->e_lfanew < sizeof(*dos) || dos->e_lfanew >= size) error( "not a PE file\n" );
  278. nt = (IMAGE_NT_HEADERS32 *)((char *)data + dos->e_lfanew);
  279. if (nt->Signature != IMAGE_NT_SIGNATURE) error( "not a PE file\n" );
  280. if ((char *)&nt->OptionalHeader + nt->FileHeader.SizeOfOptionalHeader > (char *)data + size)
  281. error( "invalid PE file\n" );
  282. switch (nt->OptionalHeader.Magic)
  283. {
  284. case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
  285. dir = &nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE];
  286. break;
  287. case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
  288. dir = &((IMAGE_NT_HEADERS64 *)nt)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE];
  289. break;
  290. default:
  291. error( "invalid PE file\n" );
  292. }
  293. if (!dir->VirtualAddress || !dir->Size) error( "resource not found in PE file\n" );
  294. va = rva_to_va( nt, dir->VirtualAddress );
  295. if (va + dir->Size > size) error( "invalid resource data in PE file\n" );
  296. root = resdir = (IMAGE_RESOURCE_DIRECTORY *)((char *)data + va );
  297. entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
  298. for (i = 0; i < resdir->NumberOfNamedEntries; i++, entry++)
  299. {
  300. static const WCHAR typelibW[] = {'T','Y','P','E','L','I','B'};
  301. WCHAR *name = (WCHAR *)((char *)root + entry->NameOffset);
  302. if (name[0] != ARRAY_SIZE(typelibW)) continue;
  303. if (!memcmp( name + 1, typelibW, sizeof(typelibW) )) break;
  304. }
  305. if (i == resdir->NumberOfNamedEntries) error( "typelib resource not found in PE file\n" );
  306. while (entry->DataIsDirectory)
  307. {
  308. resdir = (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->OffsetToDirectory);
  309. entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
  310. }
  311. resdata = (IMAGE_RESOURCE_DATA_ENTRY *)((char *)root + entry->OffsetToData);
  312. ptr = (char *)data + rva_to_va( nt, resdata->OffsetToData );
  313. if (memcmp( ptr, "MSFT", 4 )) error( "invalid typelib found in PE file\n" );
  314. read_msft_importlib( importlib, ptr, resdata->Size );
  315. }
  316. static void read_importlib(importlib_t *importlib)
  317. {
  318. int fd, size;
  319. void *data;
  320. fd = open_typelib(importlib->name);
  321. /* widl extension: if importlib name has no .tlb extension, try using .tlb */
  322. if (fd < 0 && !strendswith( importlib->name, ".tlb" ))
  323. fd = open_typelib( strmake( "%s.tlb", importlib->name ));
  324. if(fd < 0)
  325. error("Could not find importlib %s.\n", importlib->name);
  326. size = lseek( fd, 0, SEEK_END );
  327. data = xmalloc( size );
  328. lseek( fd, 0, SEEK_SET );
  329. if (read( fd, data, size) < size) error("error while reading importlib.\n");
  330. close( fd );
  331. if (!memcmp( data, "MSFT", 4 ))
  332. read_msft_importlib(importlib, data, size);
  333. else if (!memcmp( data, "MZ", 2 ))
  334. read_pe_importlib(importlib, data, size);
  335. else
  336. error("Wrong or unsupported typelib\n");
  337. free( data );
  338. }
  339. void add_importlib(const char *name, typelib_t *typelib)
  340. {
  341. importlib_t *importlib;
  342. if(!typelib) return;
  343. LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
  344. if(!strcmp(name, importlib->name))
  345. return;
  346. chat("add_importlib: %s\n", name);
  347. importlib = xmalloc(sizeof(*importlib));
  348. memset( importlib, 0, sizeof(*importlib) );
  349. importlib->name = xstrdup(name);
  350. read_importlib(importlib);
  351. list_add_head( &typelib->importlibs, &importlib->entry );
  352. }