typelib.c 9.6 KB

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