typelib.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. return VT_USERDEFINED;
  205. case TYPE_VOID:
  206. return VT_VOID;
  207. case TYPE_ALIAS:
  208. /* aliases should be filtered out by the type_get_type call above */
  209. assert(0);
  210. break;
  211. case TYPE_FUNCTION:
  212. error("get_type_vt: functions not supported\n");
  213. break;
  214. case TYPE_BITFIELD:
  215. error("get_type_vt: bitfields not supported\n");
  216. break;
  217. }
  218. return 0;
  219. }
  220. static void tlb_read(int fd, void *buf, int count)
  221. {
  222. if(read(fd, buf, count) < count)
  223. error("error while reading importlib.\n");
  224. }
  225. static void tlb_lseek(int fd, off_t offset)
  226. {
  227. if(lseek(fd, offset, SEEK_SET) == -1)
  228. error("lseek failed\n");
  229. }
  230. static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
  231. {
  232. tlb_lseek(fd, segdir->pGuidTab.offset+offset);
  233. tlb_read(fd, guid, sizeof(GUID));
  234. }
  235. static void read_msft_importlib(importlib_t *importlib, int fd)
  236. {
  237. MSFT_Header header;
  238. MSFT_SegDir segdir;
  239. int *typeinfo_offs;
  240. int i;
  241. importlib->allocated = 0;
  242. tlb_lseek(fd, 0);
  243. tlb_read(fd, &header, sizeof(header));
  244. importlib->version = header.version;
  245. typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
  246. tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
  247. tlb_read(fd, &segdir, sizeof(segdir));
  248. msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
  249. importlib->ntypeinfos = header.nrtypeinfos;
  250. importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
  251. for(i=0; i < importlib->ntypeinfos; i++) {
  252. MSFT_TypeInfoBase base;
  253. MSFT_NameIntro nameintro;
  254. int len;
  255. tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
  256. + typeinfo_offs[i]);
  257. tlb_read(fd, &base, sizeof(base));
  258. importlib->importinfos[i].importlib = importlib;
  259. importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
  260. importlib->importinfos[i].offset = -1;
  261. importlib->importinfos[i].id = i;
  262. if(base.posguid != -1) {
  263. importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
  264. msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
  265. }
  266. else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
  267. tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
  268. tlb_read(fd, &nameintro, sizeof(nameintro));
  269. len = nameintro.namelen & 0xff;
  270. importlib->importinfos[i].name = xmalloc(len+1);
  271. tlb_read(fd, importlib->importinfos[i].name, len);
  272. importlib->importinfos[i].name[len] = 0;
  273. }
  274. free(typeinfo_offs);
  275. }
  276. static int open_typelib(const char *name)
  277. {
  278. char *file_name;
  279. int fd;
  280. file_name = wpp_find_include(name, NULL);
  281. if(!file_name)
  282. return open(name, O_RDONLY | O_BINARY );
  283. fd = open(file_name, O_RDONLY | O_BINARY );
  284. free(file_name);
  285. return fd;
  286. }
  287. static void read_importlib(importlib_t *importlib)
  288. {
  289. int fd;
  290. INT magic;
  291. fd = open_typelib(importlib->name);
  292. /* widl extension: if importlib name has no .tlb extension, try using .tlb */
  293. if(fd < 0) {
  294. const char *p = strrchr(importlib->name, '.');
  295. size_t len = p ? p - importlib->name : strlen(importlib->name);
  296. if(strcmp(importlib->name + len, ".tlb")) {
  297. char *tlb_name = xmalloc(len + 5);
  298. memcpy(tlb_name, importlib->name, len);
  299. strcpy(tlb_name + len, ".tlb");
  300. fd = open_typelib(tlb_name);
  301. free(tlb_name);
  302. }
  303. }
  304. if(fd < 0)
  305. error("Could not find importlib %s.\n", importlib->name);
  306. tlb_read(fd, &magic, sizeof(magic));
  307. switch(magic) {
  308. case MSFT_MAGIC:
  309. read_msft_importlib(importlib, fd);
  310. break;
  311. default:
  312. error("Wrong or unsupported typelib magic %x\n", magic);
  313. };
  314. close(fd);
  315. }
  316. void add_importlib(const char *name, typelib_t *typelib)
  317. {
  318. importlib_t *importlib;
  319. if(!typelib) return;
  320. LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
  321. if(!strcmp(name, importlib->name))
  322. return;
  323. chat("add_importlib: %s\n", name);
  324. importlib = xmalloc(sizeof(*importlib));
  325. memset( importlib, 0, sizeof(*importlib) );
  326. importlib->name = xstrdup(name);
  327. read_importlib(importlib);
  328. list_add_head( &typelib->importlibs, &importlib->entry );
  329. }