tlb.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  1. /*
  2. * Dump a typelib (tlb) file
  3. *
  4. * Copyright 2006 Jacek Caban
  5. * Copyright 2015 Dmitry Timoshkov
  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 <stdlib.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include "windef.h"
  27. #include "winedump.h"
  28. #define MSFT_MAGIC 0x5446534d
  29. #define SLTG_MAGIC 0x47544c53
  30. #define HELPDLLFLAG 0x0100
  31. enum TYPEKIND {
  32. TKIND_ENUM = 0,
  33. TKIND_RECORD,
  34. TKIND_MODULE,
  35. TKIND_INTERFACE,
  36. TKIND_DISPATCH,
  37. TKIND_COCLASS,
  38. TKIND_ALIAS,
  39. TKIND_UNION,
  40. TKIND_MAX
  41. };
  42. enum VARENUM {
  43. VT_EMPTY = 0,
  44. VT_NULL = 1,
  45. VT_I2 = 2,
  46. VT_I4 = 3,
  47. VT_R4 = 4,
  48. VT_R8 = 5,
  49. VT_CY = 6,
  50. VT_DATE = 7,
  51. VT_BSTR = 8,
  52. VT_DISPATCH = 9,
  53. VT_ERROR = 10,
  54. VT_BOOL = 11,
  55. VT_VARIANT = 12,
  56. VT_UNKNOWN = 13,
  57. VT_DECIMAL = 14,
  58. VT_I1 = 16,
  59. VT_UI1 = 17,
  60. VT_UI2 = 18,
  61. VT_UI4 = 19,
  62. VT_I8 = 20,
  63. VT_UI8 = 21,
  64. VT_INT = 22,
  65. VT_UINT = 23,
  66. VT_VOID = 24,
  67. VT_HRESULT = 25,
  68. VT_PTR = 26,
  69. VT_SAFEARRAY = 27,
  70. VT_CARRAY = 28,
  71. VT_USERDEFINED = 29,
  72. VT_LPSTR = 30,
  73. VT_LPWSTR = 31,
  74. VT_RECORD = 36,
  75. VT_INT_PTR = 37,
  76. VT_UINT_PTR = 38,
  77. VT_FILETIME = 64,
  78. VT_BLOB = 65,
  79. VT_STREAM = 66,
  80. VT_STORAGE = 67,
  81. VT_STREAMED_OBJECT = 68,
  82. VT_STORED_OBJECT = 69,
  83. VT_BLOB_OBJECT = 70,
  84. VT_CF = 71,
  85. VT_CLSID = 72,
  86. VT_VERSIONED_STREAM = 73,
  87. VT_BSTR_BLOB = 0xfff,
  88. VT_VECTOR = 0x1000,
  89. VT_ARRAY = 0x2000,
  90. VT_BYREF = 0x4000,
  91. VT_RESERVED = 0x8000,
  92. VT_ILLEGAL = 0xffff,
  93. VT_ILLEGALMASKED = 0xfff,
  94. VT_TYPEMASK = 0xfff
  95. };
  96. struct seg_t;
  97. typedef BOOL (*dump_seg_t)(struct seg_t*);
  98. typedef struct seg_t {
  99. const char *name;
  100. dump_seg_t func;
  101. int offset;
  102. int length;
  103. } seg_t;
  104. static seg_t segdir[15];
  105. enum SEGDIRTYPE {
  106. SEGDIR_TYPEINFO,
  107. SEGDIR_IMPINFO,
  108. SEGDIR_IMPFILES,
  109. SEGDIR_REF,
  110. SEGDIR_GUIDHASH,
  111. SEGDIR_GUID,
  112. SEGDIR_NAMEHASH,
  113. SEGDIR_NAME,
  114. SEGDIR_STRING,
  115. SEGDIR_TYPEDESC,
  116. SEGDIR_ARRAYDESC,
  117. SEGDIR_CUSTDATA,
  118. SEGDIR_CDGUID,
  119. SEGDIR_res0e,
  120. SEGDIR_res0f
  121. };
  122. static int offset=0;
  123. static int indent;
  124. static int typeinfo_cnt;
  125. static int header_flags = 0;
  126. static BOOL msft_eof = FALSE;
  127. static int msft_typeinfo_offs[1000];
  128. static int msft_typeinfo_kind[1000];
  129. static int msft_typeinfo_impltypes[1000];
  130. static int msft_typeinfo_elemcnt[1000];
  131. static int msft_typeinfo_cnt = 0;
  132. static const char * const tkind[TKIND_MAX] = {
  133. "TKIND_ENUM", "TKIND_RECORD", "TKIND_MODULE",
  134. "TKIND_INTERFACE", "TKIND_DISPATCH", "TKIND_COCLASS",
  135. "TKIND_ALIAS", "TKIND_UNION"
  136. };
  137. static const void *tlb_read(int size) {
  138. const void *ret = PRD(offset, size);
  139. if(ret)
  140. offset += size;
  141. else
  142. msft_eof = TRUE;
  143. return ret;
  144. }
  145. static int tlb_read_int(void)
  146. {
  147. const int *ret = tlb_read(sizeof(int));
  148. return ret ? *ret : -1;
  149. }
  150. static int tlb_read_short(void)
  151. {
  152. const unsigned short *ret = tlb_read(sizeof(short));
  153. return ret ? *ret : -1;
  154. }
  155. static int tlb_read_byte(void)
  156. {
  157. const unsigned char *ret = tlb_read(sizeof(char));
  158. return ret ? *ret : -1;
  159. }
  160. static void print_offset(void)
  161. {
  162. int i;
  163. for(i=0; i<indent; i++)
  164. printf(" ");
  165. }
  166. static void print_begin_block(const char *name)
  167. {
  168. print_offset();
  169. printf("%s {\n", name);
  170. indent++;
  171. }
  172. static void print_begin_block_id(const char *name, int id)
  173. {
  174. char buf[64];
  175. sprintf(buf, "%s %d", name, id);
  176. print_begin_block(buf);
  177. }
  178. static void print_end_block(void)
  179. {
  180. indent--;
  181. print_offset();
  182. printf("}\n");
  183. }
  184. static int print_byte(const char *name)
  185. {
  186. unsigned char ret;
  187. print_offset();
  188. printf("%s = %02xh\n", name, ret=tlb_read_byte());
  189. return ret;
  190. }
  191. static int print_hex(const char *name)
  192. {
  193. int ret;
  194. print_offset();
  195. printf("%s = %08xh\n", name, ret=tlb_read_int());
  196. return ret;
  197. }
  198. static int print_hex_id(const char *name, int id)
  199. {
  200. char buf[64];
  201. sprintf(buf, name, id);
  202. return print_hex(buf);
  203. }
  204. static int print_short_hex(const char *name)
  205. {
  206. int ret;
  207. print_offset();
  208. printf("%s = %04xh\n", name, ret=tlb_read_short());
  209. return ret;
  210. }
  211. static int print_short_dec(const char *name)
  212. {
  213. int ret;
  214. print_offset();
  215. printf("%s = %d\n", name, ret=tlb_read_short());
  216. return ret;
  217. }
  218. static int print_dec(const char *name)
  219. {
  220. int ret;
  221. print_offset();
  222. printf("%s = %d\n", name, ret=tlb_read_int());
  223. return ret;
  224. }
  225. static void print_guid(const char *name)
  226. {
  227. GUID guid = *(const GUID*)tlb_read(sizeof(guid));
  228. print_offset();
  229. printf("%s = {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n", name,
  230. guid.Data1, guid.Data2, guid.Data3, guid.Data4[0],
  231. guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4],
  232. guid.Data4[5], guid.Data4[6], guid.Data4[7]);
  233. }
  234. static void print_vartype(int vartype)
  235. {
  236. static const char *vartypes[VT_LPWSTR+1] = {
  237. "VT_EMPTY", "VT_NULL", "VT_I2", "VT_I4", "VT_R4",
  238. "VT_R8", "VT_CY", "VT_DATE", "VT_BSTR", "VT_DISPATCH",
  239. "VT_ERROR", "VT_BOOL", "VT_VARIANT", "VT_UNKNOWN","VT_DECIMAL",
  240. "unk 15", "VT_I1", "VT_UI1", "VT_UI2", "VT_UI4",
  241. "VT_I8", "VT_UI8", "VT_INT", "VT_UINT", "VT_VOID",
  242. "VT_HRESULT", "VT_PTR", "VT_SAFEARRAY","VT_CARRAY", "VT_USERDEFINED",
  243. "VT_LPSTR", "VT_LPWSTR"
  244. };
  245. vartype &= VT_TYPEMASK;
  246. if (vartype >= VT_EMPTY && vartype <= VT_LPWSTR)
  247. printf("%s\n", vartypes[vartype]);
  248. else
  249. printf("unk %d\n", vartype);
  250. }
  251. static void print_ctl2(const char *name)
  252. {
  253. int len;
  254. const char *buf;
  255. print_offset();
  256. len = tlb_read_short();
  257. printf("%s = %d \"", name, len);
  258. len >>= 2;
  259. buf = tlb_read(len);
  260. fwrite(buf, len, 1, stdout);
  261. printf("\"");
  262. len += 2;
  263. while(len++ & 3)
  264. printf("\\%02x", tlb_read_byte());
  265. printf("\n");
  266. }
  267. static int tlb_isprint(unsigned char c)
  268. {
  269. return c >= 32;
  270. }
  271. static void dump_binary(int size)
  272. {
  273. const unsigned char *ptr;
  274. int i, j;
  275. if (!size) return;
  276. ptr = tlb_read(size);
  277. if (!ptr) return;
  278. print_offset();
  279. printf("%08x: ", offset - size);
  280. for (i = 0; i < size; i++)
  281. {
  282. printf("%02x%c", ptr[i], (i % 16 == 7) ? '-' : ' ');
  283. if ((i % 16) == 15)
  284. {
  285. printf( " " );
  286. for (j = 0; j < 16; j++)
  287. printf("%c", tlb_isprint(ptr[i-15+j]) ? ptr[i-15+j] : '.');
  288. if (i < size-1)
  289. {
  290. printf("\n");
  291. print_offset();
  292. printf("%08x: ", offset - size + i + 1);
  293. }
  294. }
  295. }
  296. if (i % 16)
  297. {
  298. printf("%*s ", 3 * (16-(i%16)), "");
  299. for (j = 0; j < i % 16; j++)
  300. printf("%c", tlb_isprint(ptr[i-(i%16)+j]) ? ptr[i-(i%16)+j] : '.');
  301. }
  302. printf("\n");
  303. }
  304. static int dump_msft_varflags(void)
  305. {
  306. static const char *syskind[] = {
  307. "SYS_WIN16", "SYS_WIN32", "SYS_MAC", "SYS_WIN64", "unknown"
  308. };
  309. int kind, flags;
  310. print_offset();
  311. flags = tlb_read_int();
  312. kind = flags & 0xf;
  313. if (kind > 3) kind = 4;
  314. printf("varflags = %08x, syskind = %s\n", flags, syskind[kind]);
  315. return flags;
  316. }
  317. static void dump_msft_version(void)
  318. {
  319. unsigned version;
  320. print_offset();
  321. version = tlb_read_int();
  322. printf("version = %u.%u\n", version & 0xffff, version >> 16);
  323. }
  324. static void dump_msft_header(void)
  325. {
  326. print_begin_block("Header");
  327. print_hex("magic1");
  328. print_hex("magic2");
  329. print_hex("posguid");
  330. print_hex("lcid");
  331. print_hex("lcid2");
  332. header_flags = dump_msft_varflags();
  333. dump_msft_version();
  334. print_hex("flags");
  335. typeinfo_cnt = print_dec("ntypeinfos");
  336. print_dec("helpstring");
  337. print_dec("helpstringcontext");
  338. print_dec("helpcontext");
  339. print_dec("nametablecount");
  340. print_dec("nametablechars");
  341. print_hex("NameOffset");
  342. print_hex("helpfile");
  343. print_hex("CustomDataOffset");
  344. print_hex("res44");
  345. print_hex("res48");
  346. print_hex("dispatchpos");
  347. print_hex("res50");
  348. print_end_block();
  349. }
  350. static int dump_msft_typekind(void)
  351. {
  352. int ret, typekind;
  353. print_offset();
  354. ret = tlb_read_int();
  355. typekind = ret & 0xf;
  356. printf("typekind = %s, align = %d\n", typekind < TKIND_MAX ? tkind[typekind] : "unknown", (ret >> 11) & 0x1f);
  357. return ret;
  358. }
  359. static void dump_msft_typeinfobase(void)
  360. {
  361. print_begin_block_id("TypeInfoBase", msft_typeinfo_cnt);
  362. msft_typeinfo_kind[msft_typeinfo_cnt] = dump_msft_typekind();
  363. msft_typeinfo_offs[msft_typeinfo_cnt] = print_hex("memoffset");
  364. print_hex("res2");
  365. print_hex("res3");
  366. print_hex("res4");
  367. print_hex("res5");
  368. msft_typeinfo_elemcnt[msft_typeinfo_cnt] = print_hex("cElement");
  369. print_hex("res7");
  370. print_hex("res8");
  371. print_hex("res9");
  372. print_hex("resA");
  373. print_hex("posguid");
  374. print_hex("flags");
  375. print_hex("NameOffset");
  376. print_hex("version");
  377. print_hex("docstringoffs");
  378. print_hex("docstringcontext");
  379. print_hex("helpcontext");
  380. print_hex("oCustData");
  381. msft_typeinfo_impltypes[msft_typeinfo_cnt++] = print_short_hex("cImplTypes");
  382. print_short_hex("bSizeVftt");
  383. print_dec("size");
  384. print_hex("datatype1");
  385. print_hex("datatype2");
  386. print_hex("res18");
  387. print_hex("res19");
  388. print_end_block();
  389. }
  390. static BOOL dump_msft_typeinfobases(seg_t *seg)
  391. {
  392. int i;
  393. for(i = 0; offset < seg->offset+seg->length; i++)
  394. dump_msft_typeinfobase();
  395. assert(offset == seg->offset+seg->length);
  396. return TRUE;
  397. }
  398. static void dump_msft_impinfo(int n)
  399. {
  400. print_begin_block_id("ImpInfo", n);
  401. print_hex("flags");
  402. print_hex("oImpInfo");
  403. print_hex("oGuid");
  404. print_end_block();
  405. }
  406. static BOOL dump_msft_impinfos(seg_t *seg)
  407. {
  408. int i;
  409. for(i = 0; offset < seg->offset+seg->length; i++)
  410. dump_msft_impinfo(i);
  411. assert(offset == seg->offset+seg->length);
  412. return TRUE;
  413. }
  414. static void dump_msft_impfile(int n)
  415. {
  416. print_begin_block_id("ImpFile", n);
  417. print_hex("guid");
  418. print_hex("lcid");
  419. print_hex("version");
  420. print_ctl2("impfile");
  421. print_end_block();
  422. }
  423. static BOOL dump_msft_impfiles(seg_t *seg)
  424. {
  425. int i;
  426. for(i = 0; offset < seg->offset+seg->length; i++)
  427. dump_msft_impfile(i);
  428. assert(offset == seg->offset+seg->length);
  429. return TRUE;
  430. }
  431. static BOOL dump_msft_reftabs(seg_t *seg)
  432. {
  433. print_begin_block("RefTab");
  434. dump_binary(seg->length); /* FIXME */
  435. print_end_block();
  436. return TRUE;
  437. }
  438. static BOOL dump_msft_guidhashtab(seg_t *seg)
  439. {
  440. print_begin_block("GuidHashTab");
  441. dump_binary(seg->length); /* FIXME */
  442. print_end_block();
  443. assert(offset == seg->offset+seg->length);
  444. return TRUE;
  445. }
  446. static void dump_msft_guidentry(int n)
  447. {
  448. print_begin_block_id("GuidEntry", n);
  449. print_guid("guid");
  450. print_hex("hreftype");
  451. print_hex("next_hash");
  452. print_end_block();
  453. }
  454. static BOOL dump_msft_guidtab(seg_t *seg)
  455. {
  456. int i;
  457. for(i = 0; offset < seg->offset+seg->length; i++)
  458. dump_msft_guidentry(i);
  459. assert(offset == seg->offset+seg->length);
  460. return TRUE;
  461. }
  462. static BOOL dump_msft_namehashtab(seg_t *seg)
  463. {
  464. print_begin_block("NameHashTab");
  465. dump_binary(seg->length); /* FIXME */
  466. print_end_block();
  467. return TRUE;
  468. }
  469. static void print_string0(void)
  470. {
  471. unsigned char c;
  472. printf("\"");
  473. while ((c = tlb_read_byte()) != 0)
  474. {
  475. if (isprint(c))
  476. fwrite(&c, 1, 1, stdout);
  477. else
  478. {
  479. char buf[16];
  480. sprintf(buf, "\\%u", c);
  481. fwrite(buf, 1, strlen(buf), stdout);
  482. }
  483. }
  484. printf("\"");
  485. }
  486. static void print_string(int len)
  487. {
  488. printf("\"");
  489. fwrite(tlb_read(len), len, 1, stdout);
  490. printf("\"");
  491. }
  492. static void dump_string(int len, int align_off)
  493. {
  494. print_string(len);
  495. printf(" ");
  496. while((len++ + align_off) & 3)
  497. printf("\\%2.2x", tlb_read_byte());
  498. }
  499. static void dump_msft_name(int base, int n)
  500. {
  501. int len;
  502. print_begin_block_id("Name", n);
  503. print_hex("hreftype");
  504. print_hex("next_hash");
  505. len = print_hex("namelen")&0xff;
  506. print_offset();
  507. printf("name = ");
  508. dump_string(len, 0);
  509. printf("\n");
  510. print_end_block();
  511. }
  512. static BOOL dump_msft_nametab(seg_t *seg)
  513. {
  514. int i, base = offset;
  515. for(i = 0; offset < seg->offset+seg->length; i++)
  516. dump_msft_name(base, i);
  517. assert(offset == seg->offset+seg->length);
  518. return TRUE;
  519. }
  520. static void dump_msft_string(int n)
  521. {
  522. int len;
  523. print_begin_block_id("String", n);
  524. len = print_short_hex("stringlen");
  525. print_offset();
  526. printf("string = ");
  527. dump_string(len, 2);
  528. if(len < 3) {
  529. for(len = 0; len < 4; len++)
  530. printf("\\%2.2x", tlb_read_byte());
  531. }
  532. printf("\n");
  533. print_end_block();
  534. }
  535. static BOOL dump_msft_stringtab(seg_t *seg)
  536. {
  537. int i;
  538. for(i = 0; offset < seg->offset+seg->length; i++)
  539. dump_msft_string(i);
  540. assert(offset == seg->offset+seg->length);
  541. return TRUE;
  542. }
  543. static void dump_msft_typedesc(int n)
  544. {
  545. print_begin_block_id("TYPEDESC", n);
  546. print_hex("hreftype");
  547. print_hex("vt");
  548. print_end_block();
  549. }
  550. static BOOL dump_msft_typedesctab(seg_t *seg)
  551. {
  552. int i;
  553. print_begin_block("TypedescTab");
  554. for(i = 0; offset < seg->offset+seg->length; i++)
  555. dump_msft_typedesc(i);
  556. print_end_block();
  557. assert(offset == seg->offset+seg->length);
  558. return TRUE;
  559. }
  560. static BOOL dump_msft_arraydescs(seg_t *seg)
  561. {
  562. print_begin_block("ArrayDescriptions");
  563. dump_binary(seg->length); /* FIXME */
  564. print_end_block();
  565. return TRUE;
  566. }
  567. static BOOL dump_msft_custdata(seg_t *seg)
  568. {
  569. unsigned short vt;
  570. unsigned i, n;
  571. print_begin_block("CustData");
  572. for(i=0; offset < seg->offset+seg->length; i++) {
  573. print_offset();
  574. vt = tlb_read_short();
  575. printf("vt %d", vt);
  576. n = tlb_read_int();
  577. switch(vt) {
  578. case VT_BSTR:
  579. printf(" len %d: ", n);
  580. dump_string(n, 2);
  581. printf("\n");
  582. break;
  583. default:
  584. printf(": %x ", n);
  585. printf("\\%2.2x ", tlb_read_byte());
  586. printf("\\%2.2x\n", tlb_read_byte());
  587. }
  588. }
  589. print_end_block();
  590. return TRUE;
  591. }
  592. static void dump_msft_cdguid(int n)
  593. {
  594. print_begin_block_id("CGUid", n);
  595. print_hex("GuidOffset");
  596. print_hex("DataOffset");
  597. print_hex("next");
  598. print_end_block();
  599. }
  600. static BOOL dump_msft_cdguids(seg_t *seg)
  601. {
  602. int i;
  603. for(i = 0; offset < seg->offset+seg->length; i++)
  604. dump_msft_cdguid(i);
  605. assert(offset == seg->offset+seg->length);
  606. return TRUE;
  607. }
  608. static BOOL dump_msft_res0e(seg_t *seg)
  609. {
  610. print_begin_block("res0e");
  611. dump_binary(seg->length);
  612. print_end_block();
  613. return TRUE;
  614. }
  615. static BOOL dump_msft_res0f(seg_t *seg)
  616. {
  617. print_begin_block("res0f");
  618. dump_binary(seg->length);
  619. print_end_block();
  620. return TRUE;
  621. }
  622. /* Used for function return value and arguments type */
  623. static void dump_msft_datatype(const char *name)
  624. {
  625. int datatype;
  626. print_offset();
  627. datatype = tlb_read_int();
  628. printf("%s = %08x", name, datatype);
  629. if (datatype < 0) {
  630. printf(", ");
  631. print_vartype(datatype);
  632. }
  633. else {
  634. const short *vt;
  635. if (datatype > segdir[SEGDIR_TYPEDESC].length) {
  636. printf(", invalid offset\n");
  637. return;
  638. }
  639. /* FIXME: in case of VT_USERDEFINED use hreftype */
  640. vt = PRD(segdir[SEGDIR_TYPEDESC].offset + datatype, 4*sizeof(short));
  641. datatype = vt[0] & VT_TYPEMASK;
  642. if (datatype == VT_PTR) {
  643. printf(", VT_PTR -> ");
  644. if (vt[3] < 0)
  645. datatype = vt[2];
  646. else {
  647. vt = PRD(segdir[SEGDIR_TYPEDESC].offset + vt[2], 4*sizeof(short));
  648. datatype = *vt;
  649. }
  650. }
  651. else {
  652. printf(", ");
  653. datatype = *vt;
  654. }
  655. print_vartype(datatype);
  656. }
  657. }
  658. static void dump_defaultvalue(int id)
  659. {
  660. int offset;
  661. print_offset();
  662. offset = tlb_read_int();
  663. printf("default value[%d] = %08x", id, offset);
  664. if (offset == -1)
  665. printf("\n");
  666. else if (offset < 0) {
  667. printf(", ");
  668. print_vartype((offset & 0x7c000000) >> 26);
  669. }
  670. else {
  671. const unsigned short *vt;
  672. if (offset > segdir[SEGDIR_CUSTDATA].length) {
  673. printf(", invalid offset\n");
  674. return;
  675. }
  676. vt = PRD(segdir[SEGDIR_CUSTDATA].offset + offset, sizeof(*vt));
  677. printf(", ");
  678. print_vartype(*vt);
  679. }
  680. }
  681. static void dump_msft_func(int n)
  682. {
  683. int size, args_cnt, i, extra_attr, fkccic;
  684. print_begin_block_id("FuncRecord", n);
  685. size = print_short_hex("size");
  686. print_short_hex("index");
  687. dump_msft_datatype("retval type");
  688. print_hex("flags");
  689. print_short_hex("VtableOffset");
  690. print_short_hex("funcdescsize");
  691. fkccic = print_hex("FKCCIC");
  692. args_cnt = print_short_hex("nrargs");
  693. print_short_hex("noptargs");
  694. extra_attr = size/sizeof(INT) - 6 - args_cnt*(fkccic&0x1000 ? 4 : 3);
  695. if(extra_attr)
  696. print_hex("helpcontext");
  697. if(extra_attr >= 2)
  698. print_hex("oHelpString");
  699. if(extra_attr >= 3)
  700. print_hex("toEntry");
  701. if(extra_attr >= 4)
  702. print_hex("res9");
  703. if(extra_attr >= 5)
  704. print_hex("resA");
  705. if(extra_attr >= 6)
  706. print_hex("HelpStringContext");
  707. if(extra_attr >= 7)
  708. print_hex("oCustData");
  709. for(i = 0; i < extra_attr-7; i++)
  710. print_hex_id("oArgCustData", i);
  711. if(fkccic & 0x1000) {
  712. for(i=0; i < args_cnt; i++)
  713. dump_defaultvalue(i);
  714. }
  715. for(i=0; i < args_cnt; i++) {
  716. print_begin_block_id("param", i);
  717. /* FIXME: Handle default values */
  718. dump_msft_datatype("datatype");
  719. print_hex("name");
  720. print_hex("paramflags");
  721. print_end_block();
  722. }
  723. print_end_block();
  724. }
  725. static void dump_msft_var(int n)
  726. {
  727. INT size;
  728. print_begin_block_id("VarRecord", n);
  729. size = print_hex("recsize")&0x1ff;
  730. print_hex("DataType");
  731. print_hex("flags");
  732. print_short_hex("VarKind");
  733. print_short_hex("vardescsize");
  734. print_hex("OffsValue");
  735. if(size > 5*sizeof(INT))
  736. dump_binary(size - 5*sizeof(INT));
  737. print_end_block();
  738. }
  739. static void dump_msft_ref(int n)
  740. {
  741. print_begin_block_id("RefRecord", n);
  742. print_hex("reftype");
  743. print_hex("flags");
  744. print_hex("oCustData");
  745. print_hex("onext");
  746. print_end_block();
  747. }
  748. static void dump_msft_coclass(int n)
  749. {
  750. int i;
  751. print_dec("size");
  752. for(i=0; i < msft_typeinfo_impltypes[n]; i++)
  753. dump_msft_ref(i);
  754. }
  755. static BOOL dump_msft_typeinfo(int n)
  756. {
  757. int i;
  758. print_begin_block_id("TypeInfo", n);
  759. if((msft_typeinfo_kind[n] & 0xf) == TKIND_COCLASS) {
  760. dump_msft_coclass(n);
  761. print_end_block();
  762. return TRUE;
  763. }
  764. print_dec("size");
  765. for(i = 0; i < LOWORD(msft_typeinfo_elemcnt[n]); i++)
  766. dump_msft_func(i);
  767. for(i = 0; i < HIWORD(msft_typeinfo_elemcnt[n]); i++)
  768. dump_msft_var(i);
  769. for(i = 0; i < LOWORD(msft_typeinfo_elemcnt[n]); i++)
  770. print_hex_id("func %d id", i);
  771. for(i = 0; i < HIWORD(msft_typeinfo_elemcnt[n]); i++)
  772. print_hex_id("var %d id", i);
  773. for(i = 0; i < LOWORD(msft_typeinfo_elemcnt[n]); i++)
  774. print_hex_id("func %d name", i);
  775. for(i = 0; i < HIWORD(msft_typeinfo_elemcnt[n]); i++)
  776. print_hex_id("var %d name", i);
  777. for(i = 0; i < LOWORD(msft_typeinfo_elemcnt[n]); i++)
  778. print_hex_id("func %d offset", i);
  779. for(i = 0; i < HIWORD(msft_typeinfo_elemcnt[n]); i++)
  780. print_hex_id("var %d offset", i);
  781. print_end_block();
  782. return TRUE;
  783. }
  784. static seg_t segdir[] = {
  785. {"TypeInfoTab", dump_msft_typeinfobases, -1, -1},
  786. {"ImpInfo", dump_msft_impinfos, -1, -1},
  787. {"ImpFiles", dump_msft_impfiles, -1, -1},
  788. {"RefTab", dump_msft_reftabs, -1, -1},
  789. {"GuidHashTab", dump_msft_guidhashtab, -1, -1},
  790. {"GuidTab", dump_msft_guidtab, -1, -1},
  791. {"NameHashTab", dump_msft_namehashtab, -1, -1},
  792. {"pNameTab", dump_msft_nametab, -1, -1},
  793. {"pStringTab", dump_msft_stringtab, -1, -1},
  794. {"TypedescTab", dump_msft_typedesctab, -1, -1},
  795. {"ArrayDescriptions", dump_msft_arraydescs, -1, -1},
  796. {"CustData", dump_msft_custdata, -1, -1},
  797. {"CDGuid", dump_msft_cdguids, -1, -1},
  798. {"res0e", dump_msft_res0e, -1, -1},
  799. {"res0f", dump_msft_res0f, -1, -1}
  800. };
  801. static void dump_msft_seg(seg_t *seg)
  802. {
  803. print_begin_block(seg->name);
  804. seg->offset = print_hex("offset");
  805. seg->length = print_dec("length");
  806. print_hex("res08");
  807. print_hex("res0c");
  808. print_end_block();
  809. }
  810. static void dump_msft_segdir(void)
  811. {
  812. int i;
  813. print_begin_block("SegDir");
  814. for(i=0; i < ARRAY_SIZE(segdir); i++)
  815. dump_msft_seg(segdir+i);
  816. print_end_block();
  817. }
  818. static BOOL dump_offset(void)
  819. {
  820. int i;
  821. for(i=0; i < ARRAY_SIZE(segdir); i++)
  822. if(segdir[i].offset == offset)
  823. return segdir[i].func(segdir+i);
  824. for(i=0; i < msft_typeinfo_cnt; i++)
  825. if(msft_typeinfo_offs[i] == offset)
  826. return dump_msft_typeinfo(i);
  827. return FALSE;
  828. }
  829. static void msft_dump(void)
  830. {
  831. int i;
  832. dump_msft_header();
  833. for(i=0; i < typeinfo_cnt; i++)
  834. print_hex_id("typeinfo %d offset", i);
  835. if(header_flags & HELPDLLFLAG)
  836. print_hex("help dll offset");
  837. print_offset();
  838. printf("\n");
  839. dump_msft_segdir();
  840. while(!msft_eof) {
  841. if(!dump_offset())
  842. print_hex("unknown");
  843. }
  844. }
  845. /****************************** SLTG Typelibs ******************************/
  846. struct block_entry
  847. {
  848. DWORD len;
  849. WORD index_string;
  850. WORD next;
  851. };
  852. struct bitstream
  853. {
  854. const BYTE *buffer;
  855. DWORD length;
  856. WORD current;
  857. };
  858. #include "pshpack1.h"
  859. struct sltg_typeinfo_header
  860. {
  861. short magic;
  862. int href_table;
  863. int res06;
  864. int elem_table;
  865. int res0e;
  866. int version;
  867. int res16;
  868. struct
  869. {
  870. unsigned unknown1 : 3;
  871. unsigned flags : 16;
  872. unsigned unknown2 : 5;
  873. unsigned typekind : 8;
  874. } misc;
  875. int res1e;
  876. };
  877. struct sltg_member_header
  878. {
  879. short res00;
  880. short res02;
  881. char res04;
  882. int extra;
  883. };
  884. struct sltg_tail
  885. {
  886. unsigned short cFuncs;
  887. unsigned short cVars;
  888. unsigned short cImplTypes;
  889. unsigned short res06; /* always 0000 */
  890. unsigned short funcs_off; /* offset to functions (starting from the member header) */
  891. unsigned short vars_off; /* offset to vars (starting from the member header) */
  892. unsigned short impls_off; /* offset to implemented types (starting from the member header) */
  893. unsigned short funcs_bytes; /* bytes used by function data */
  894. unsigned short vars_bytes; /* bytes used by var data */
  895. unsigned short impls_bytes; /* bytes used by implemented type data */
  896. unsigned short tdescalias_vt; /* for TKIND_ALIAS */
  897. unsigned short res16; /* always ffff */
  898. unsigned short res18; /* always 0000 */
  899. unsigned short res1a; /* always 0000 */
  900. unsigned short simple_alias; /* tdescalias_vt is a vt rather than an offset? */
  901. unsigned short res1e; /* always 0000 */
  902. unsigned short cbSizeInstance;
  903. unsigned short cbAlignment;
  904. unsigned short res24;
  905. unsigned short res26;
  906. unsigned short cbSizeVft;
  907. unsigned short res2a; /* always ffff */
  908. unsigned short res2c; /* always ffff */
  909. unsigned short res2e; /* always ffff */
  910. unsigned short res30; /* always ffff */
  911. unsigned short res32;
  912. unsigned short res34;
  913. };
  914. struct sltg_variable
  915. {
  916. char magic; /* 0x0a */
  917. char flags;
  918. short next;
  919. short name;
  920. short byte_offs; /* pos in struct, or offset to const type or const data (if flags & 0x08) */
  921. short type; /* if flags & 0x02 this is the type, else offset to type */
  922. int memid;
  923. short helpcontext; /* ?? */
  924. short helpstring; /* ?? */
  925. #if 0
  926. short varflags; /* only present if magic & 0x20 */
  927. #endif
  928. };
  929. #include "poppack.h"
  930. static const char *lookup_code(const BYTE *table, DWORD table_size, struct bitstream *bits)
  931. {
  932. const BYTE *p = table;
  933. while (p < table + table_size && *p == 0x80)
  934. {
  935. if (p + 2 >= table + table_size) return NULL;
  936. if (!(bits->current & 0xff))
  937. {
  938. if (!bits->length) return NULL;
  939. bits->current = (*bits->buffer << 8) | 1;
  940. bits->buffer++;
  941. bits->length--;
  942. }
  943. if (bits->current & 0x8000)
  944. {
  945. p += 3;
  946. }
  947. else
  948. {
  949. p = table + (*(p + 2) | (*(p + 1) << 8));
  950. }
  951. bits->current <<= 1;
  952. }
  953. if (p + 1 < table + table_size && *(p + 1))
  954. {
  955. /* FIXME: What is the meaning of *p? */
  956. const BYTE *q = p + 1;
  957. while (q < table + table_size && *q) q++;
  958. return (q < table + table_size) ? (const char *)(p + 1) : NULL;
  959. }
  960. return NULL;
  961. }
  962. static const char *decode_string(const BYTE *table, const char *stream, DWORD stream_length, DWORD *read_bytes)
  963. {
  964. char *buf;
  965. DWORD buf_size, table_size;
  966. const char *p;
  967. struct bitstream bits;
  968. bits.buffer = (const BYTE *)stream;
  969. bits.length = stream_length;
  970. bits.current = 0;
  971. buf_size = *(const WORD *)table;
  972. table += sizeof(WORD);
  973. table_size = *(const DWORD *)table;
  974. table += sizeof(DWORD);
  975. buf = malloc(buf_size);
  976. buf[0] = 0;
  977. while ((p = lookup_code(table, table_size, &bits)))
  978. {
  979. if (buf[0]) strcat(buf, " ");
  980. assert(strlen(buf) + strlen(p) + 1 <= buf_size);
  981. strcat(buf, p);
  982. }
  983. if (read_bytes) *read_bytes = stream_length - bits.length;
  984. return buf;
  985. }
  986. static void print_sltg_name(const char *name)
  987. {
  988. unsigned short len = tlb_read_short();
  989. print_offset();
  990. printf("%s = %#x (", name, len);
  991. if (len != 0xffff) print_string(len);
  992. printf(")\n");
  993. }
  994. static int dump_sltg_header(int *sltg_first_blk, int *size_of_index, int *size_of_pad)
  995. {
  996. int n_file_blocks;
  997. print_begin_block("Header");
  998. print_hex("magic");
  999. n_file_blocks = print_short_dec("# file blocks");
  1000. *size_of_pad = print_short_hex("pad");
  1001. *size_of_index = print_short_hex("size of index");
  1002. *sltg_first_blk = print_short_dec("first block");
  1003. print_guid("guid");
  1004. print_hex("res1c");
  1005. print_hex("res20");
  1006. print_end_block();
  1007. return n_file_blocks;
  1008. }
  1009. static void dump_sltg_index(int count)
  1010. {
  1011. int i;
  1012. printf("index:\n");
  1013. print_string0();
  1014. printf("\n");
  1015. print_string0();
  1016. printf("\n");
  1017. for (i = 0; i < count - 2; i++)
  1018. {
  1019. print_string0();
  1020. printf("\n");
  1021. }
  1022. printf("\n");
  1023. }
  1024. static void dump_sltg_pad(int size_of_pad)
  1025. {
  1026. printf("pad:\n");
  1027. dump_binary(size_of_pad);
  1028. printf("\n");
  1029. }
  1030. static void dump_sltg_block_entry(int idx, const char *index)
  1031. {
  1032. char name[32];
  1033. short index_offset;
  1034. sprintf(name, "Block entry %d", idx);
  1035. print_begin_block(name);
  1036. print_hex("len");
  1037. index_offset = tlb_read_short();
  1038. print_offset();
  1039. printf("index string = %xh \"%s\"\n", index_offset, index + index_offset);
  1040. print_short_hex("next");
  1041. print_end_block();
  1042. }
  1043. static void dump_sltg_library_block(void)
  1044. {
  1045. print_begin_block("Library block entry");
  1046. print_short_hex("magic");
  1047. print_short_hex("res02");
  1048. print_sltg_name("name");
  1049. print_short_hex("res06");
  1050. print_sltg_name("helpstring");
  1051. print_sltg_name("helpfile");
  1052. print_hex("helpcontext");
  1053. print_short_hex("syskind");
  1054. print_short_hex("lcid");
  1055. print_hex("res12");
  1056. print_short_hex("libflags");
  1057. dump_msft_version();
  1058. print_guid("uuid");
  1059. print_end_block();
  1060. }
  1061. static void skip_sltg_library_block(void)
  1062. {
  1063. unsigned short skip;
  1064. tlb_read_short();
  1065. tlb_read_short();
  1066. skip = tlb_read_short();
  1067. if (skip != 0xffff) tlb_read(skip);
  1068. tlb_read_short();
  1069. skip = tlb_read_short();
  1070. if (skip != 0xffff) tlb_read(skip);
  1071. skip = tlb_read_short();
  1072. if (skip != 0xffff) tlb_read(skip);
  1073. tlb_read_int();
  1074. tlb_read_short();
  1075. tlb_read_short();
  1076. tlb_read_int();
  1077. tlb_read_short();
  1078. tlb_read_int();
  1079. tlb_read(sizeof(GUID));
  1080. }
  1081. static void dump_sltg_other_typeinfo(int idx, const char *hlp_strings)
  1082. {
  1083. int hlpstr_len, saved_offset;
  1084. char name[32];
  1085. sprintf(name, "Other typeinfo %d", idx);
  1086. print_begin_block(name);
  1087. print_sltg_name("index name");
  1088. print_sltg_name("other name");
  1089. print_short_hex("res1a");
  1090. print_short_hex("name offset");
  1091. print_offset();
  1092. hlpstr_len = tlb_read_short();
  1093. if (hlpstr_len)
  1094. {
  1095. const char *str;
  1096. saved_offset = offset;
  1097. str = tlb_read(hlpstr_len);
  1098. str = decode_string((const BYTE *)hlp_strings, str, hlpstr_len, NULL);
  1099. printf("helpstring: \"%s\"\n", str);
  1100. offset = saved_offset;
  1101. print_offset();
  1102. printf("helpstring encoded bits: %d bytes\n", hlpstr_len);
  1103. dump_binary(hlpstr_len);
  1104. }
  1105. else
  1106. printf("helpstring: \"\"\n");
  1107. print_short_hex("res20");
  1108. print_hex("helpcontext");
  1109. print_short_hex("res26");
  1110. print_guid("uuid");
  1111. print_short_dec("typekind");
  1112. print_end_block();
  1113. }
  1114. static void skip_sltg_other_typeinfo(void)
  1115. {
  1116. unsigned short skip;
  1117. skip = tlb_read_short();
  1118. if (skip != 0xffff) tlb_read(skip);
  1119. skip = tlb_read_short();
  1120. if (skip != 0xffff) tlb_read(skip);
  1121. tlb_read_short();
  1122. tlb_read_short();
  1123. skip = tlb_read_short();
  1124. if (skip) tlb_read(skip);
  1125. tlb_read_short();
  1126. tlb_read_int();
  1127. tlb_read_short();
  1128. tlb_read(sizeof(GUID));
  1129. tlb_read_short();
  1130. }
  1131. static void sltg_print_simple_type(short type)
  1132. {
  1133. print_offset();
  1134. if ((type & 0x0f00) == 0x0e00)
  1135. printf("*");
  1136. printf("%04x | (%d)\n", type & 0xff80, type & 0x7f);
  1137. }
  1138. static void dump_safe_array(int array_offset)
  1139. {
  1140. int i, cDims, saved_offset = offset;
  1141. offset = array_offset;
  1142. print_offset();
  1143. printf("safe array starts at %#x\n", offset);
  1144. cDims = print_short_dec("cDims");
  1145. print_short_hex("fFeatures");
  1146. print_dec("cbElements");
  1147. print_dec("cLocks");
  1148. print_hex("pvData");
  1149. for (i = 0; i < cDims; i++)
  1150. dump_binary(8); /* sizeof(SAFEARRAYBOUND) */
  1151. print_offset();
  1152. printf("safe array ends at %#x\n", offset);
  1153. offset = saved_offset;
  1154. }
  1155. static int sltg_print_compound_type(int vars_start_offset, int type_offset)
  1156. {
  1157. short type, vt;
  1158. int type_bytes, saved_offset = offset;
  1159. offset = vars_start_offset + type_offset;
  1160. print_offset();
  1161. printf("type description starts at %#x\n", offset);
  1162. for (;;)
  1163. {
  1164. do
  1165. {
  1166. type = tlb_read_short();
  1167. vt = type & 0x7f;
  1168. if (vt == VT_PTR)
  1169. {
  1170. print_offset();
  1171. printf("%04x | VT_PTR\n", type & 0xff80);
  1172. }
  1173. } while (vt == VT_PTR);
  1174. if (vt == VT_USERDEFINED)
  1175. {
  1176. short href = tlb_read_short();
  1177. print_offset();
  1178. if ((type & 0x0f00) == 0x0e00)
  1179. printf("*");
  1180. printf("%04x | VT_USERDEFINED (href %d)\n", type & 0xff80, href);
  1181. break;
  1182. }
  1183. else if (vt == VT_CARRAY)
  1184. {
  1185. short off;
  1186. off = tlb_read_short();
  1187. print_offset();
  1188. printf("VT_CARRAY: offset %#x (+%#x=%#x)\n",
  1189. off, vars_start_offset, off + vars_start_offset);
  1190. dump_safe_array(vars_start_offset + off);
  1191. /* type description follows */
  1192. print_offset();
  1193. printf("array element type:\n");
  1194. continue;
  1195. }
  1196. else if (vt == VT_SAFEARRAY)
  1197. {
  1198. short off;
  1199. off = tlb_read_short();
  1200. print_offset();
  1201. printf("VT_SAFEARRAY: offset %#x (+%#x=%#x)\n",
  1202. off, vars_start_offset, off + vars_start_offset);
  1203. dump_safe_array(vars_start_offset + off);
  1204. break;
  1205. }
  1206. else
  1207. {
  1208. sltg_print_simple_type(type);
  1209. break;
  1210. }
  1211. }
  1212. print_offset();
  1213. printf("type description ends at %#x\n", offset);
  1214. type_bytes = offset - saved_offset;
  1215. offset = saved_offset;
  1216. return type_bytes;
  1217. }
  1218. static void dump_type(int len, const char *hlp_strings)
  1219. {
  1220. union
  1221. {
  1222. struct
  1223. {
  1224. unsigned unknown1 : 3;
  1225. unsigned flags : 13;
  1226. unsigned unknown2 : 8;
  1227. unsigned typekind : 8;
  1228. } s;
  1229. unsigned flags;
  1230. } misc;
  1231. int typeinfo_start_offset, extra, member_offset, href_offset, i;
  1232. int vars_header_bytes = 0, vars_bytes = 0, saved_offset;
  1233. const void *block;
  1234. const struct sltg_typeinfo_header *ti;
  1235. const struct sltg_member_header *mem;
  1236. const struct sltg_tail *tail;
  1237. typeinfo_start_offset = offset;
  1238. block = tlb_read(len);
  1239. offset = typeinfo_start_offset;
  1240. ti = block;
  1241. mem = (const struct sltg_member_header *)((char *)block + ti->elem_table);
  1242. tail = (const struct sltg_tail *)((char *)(mem + 1) + mem->extra);
  1243. typeinfo_start_offset = offset;
  1244. print_short_hex("magic");
  1245. href_offset = tlb_read_int();
  1246. print_offset();
  1247. if (href_offset != -1)
  1248. printf("href offset = %#x (+%#x=%#x)\n",
  1249. href_offset, typeinfo_start_offset, href_offset + typeinfo_start_offset);
  1250. else
  1251. printf("href offset = ffffffffh\n");
  1252. print_hex("res06");
  1253. member_offset = tlb_read_int();
  1254. print_offset();
  1255. printf("member offset = %#x (+%#x=%#x)\n",
  1256. member_offset, typeinfo_start_offset, member_offset + typeinfo_start_offset);
  1257. print_hex("res0e");
  1258. print_hex("version");
  1259. print_hex("res16");
  1260. misc.flags = print_hex("misc");
  1261. print_offset();
  1262. printf("misc: unknown1 %02x, flags %04x, unknown2 %02x, typekind %u (%s)\n",
  1263. misc.s.unknown1, misc.s.flags, misc.s.unknown2, misc.s.typekind,
  1264. misc.s.typekind < TKIND_MAX ? tkind[misc.s.typekind] : "unknown");
  1265. print_hex("res1e");
  1266. if (href_offset != -1)
  1267. {
  1268. int i, number;
  1269. print_begin_block("href_table");
  1270. print_short_hex("magic");
  1271. print_hex("res02");
  1272. print_hex("res06");
  1273. print_hex("res0a");
  1274. print_hex("res0e");
  1275. print_hex("res12");
  1276. print_hex("res16");
  1277. print_hex("res1a");
  1278. print_hex("res1e");
  1279. print_hex("res22");
  1280. print_hex("res26");
  1281. print_hex("res2a");
  1282. print_hex("res2e");
  1283. print_hex("res32");
  1284. print_hex("res36");
  1285. print_hex("res3a");
  1286. print_hex("res3e");
  1287. print_short_hex("res42");
  1288. number = print_hex("number");
  1289. for (i = 0; i < number; i += 8)
  1290. dump_binary(8);
  1291. print_short_hex("res50");
  1292. print_byte("res52");
  1293. print_hex("res53");
  1294. for (i = 0; i < number/8; i++)
  1295. print_sltg_name("name");
  1296. print_byte("resxx");
  1297. print_end_block();
  1298. }
  1299. print_offset();
  1300. printf("member_header starts at %#x, current offset = %#x\n", typeinfo_start_offset + member_offset, offset);
  1301. member_offset = offset;
  1302. print_short_hex("res00");
  1303. print_short_hex("res02");
  1304. print_byte("res04");
  1305. extra = print_hex("extra");
  1306. if (misc.s.typekind == TKIND_RECORD || misc.s.typekind == TKIND_ENUM)
  1307. {
  1308. int vars_start_offset = offset;
  1309. for (i = 0; i < tail->cVars; i++)
  1310. {
  1311. char name[32];
  1312. int saved_off;
  1313. char magic, flags;
  1314. short next, value;
  1315. sprintf(name, "variable %d", i);
  1316. print_begin_block(name);
  1317. saved_off = offset;
  1318. dump_binary(sizeof(struct sltg_variable));
  1319. offset = saved_off;
  1320. magic = print_byte("magic");
  1321. flags = print_byte("flags");
  1322. next = tlb_read_short();
  1323. print_offset();
  1324. if (next != -1)
  1325. printf("next offset = %#x (+%#x=%#x)\n",
  1326. next, vars_start_offset, next + vars_start_offset);
  1327. else
  1328. printf("next offset = ffffh\n");
  1329. print_short_hex("name");
  1330. if (flags & 0x40)
  1331. print_short_hex("dispatch");
  1332. else if (flags & 0x10)
  1333. {
  1334. if (flags & 0x08)
  1335. print_short_hex("const value");
  1336. else
  1337. {
  1338. value = tlb_read_short();
  1339. print_offset();
  1340. printf("byte offset = %#x (+%#x=%#x)\n",
  1341. value, vars_start_offset, value + vars_start_offset);
  1342. }
  1343. }
  1344. else
  1345. print_short_hex("oInst");
  1346. value = tlb_read_short();
  1347. if (!(flags & 0x02))
  1348. {
  1349. print_offset();
  1350. printf("type offset = %#x (+%#x=%#x)\n",
  1351. value, vars_start_offset, value + vars_start_offset);
  1352. print_offset();
  1353. printf("type:\n");
  1354. vars_bytes += sltg_print_compound_type(vars_start_offset, value);
  1355. }
  1356. else
  1357. {
  1358. print_offset();
  1359. printf("type:\n");
  1360. sltg_print_simple_type(value);
  1361. }
  1362. print_hex("memid");
  1363. print_short_hex("helpcontext");
  1364. value = tlb_read_short();
  1365. print_offset();
  1366. if (value != -1)
  1367. {
  1368. const char *str;
  1369. DWORD hlpstr_maxlen;
  1370. printf("helpstring offset = %#x (+%#x=%#x)\n",
  1371. value, vars_start_offset, value + vars_start_offset);
  1372. saved_offset = offset;
  1373. offset = value + vars_start_offset;
  1374. hlpstr_maxlen = member_offset + sizeof(struct sltg_member_header) + mem->extra - offset;
  1375. str = tlb_read(hlpstr_maxlen);
  1376. str = decode_string((const BYTE *)hlp_strings, str, hlpstr_maxlen, &hlpstr_maxlen);
  1377. print_offset();
  1378. printf("helpstring: \"%s\"\n", str);
  1379. offset = value + vars_start_offset;
  1380. print_offset();
  1381. printf("helpstring encoded bits: %d bytes\n", hlpstr_maxlen);
  1382. dump_binary(hlpstr_maxlen);
  1383. offset = saved_offset;
  1384. }
  1385. else
  1386. printf("helpstring offset = ffffh\n");
  1387. if (magic & 0x20)
  1388. {
  1389. print_short_hex("varflags");
  1390. vars_header_bytes += 2;
  1391. }
  1392. vars_header_bytes += sizeof(struct sltg_variable);
  1393. if (next != -1)
  1394. {
  1395. if (offset != vars_start_offset + next)
  1396. dump_binary(vars_start_offset + next - offset);
  1397. }
  1398. print_end_block();
  1399. }
  1400. }
  1401. else if (misc.s.typekind == TKIND_INTERFACE || misc.s.typekind == TKIND_COCLASS)
  1402. {
  1403. short next, i;
  1404. int funcs_start_offset = offset;
  1405. for (i = 0; i < tail->cImplTypes; i++)
  1406. {
  1407. char name[64];
  1408. sprintf(name, "impl.type %d (current offset %#x)", i, offset);
  1409. print_begin_block(name);
  1410. print_short_hex("res00");
  1411. next = tlb_read_short();
  1412. print_offset();
  1413. if (next != -1)
  1414. printf("next offset = %#x (+%#x=%#x)\n",
  1415. next, funcs_start_offset, next + funcs_start_offset);
  1416. else
  1417. printf("next offset = ffffh\n");
  1418. print_short_hex("res04");
  1419. print_byte("impltypeflags");
  1420. print_byte("res07");
  1421. print_short_hex("res08");
  1422. print_short_hex("ref");
  1423. print_short_hex("res0c");
  1424. print_short_hex("res0e");
  1425. print_short_hex("res10");
  1426. print_short_hex("res12");
  1427. print_short_hex("pos in table");
  1428. print_end_block();
  1429. }
  1430. for (i = 0; i < tail->cFuncs; i++)
  1431. {
  1432. char name[64];
  1433. BYTE magic, flags;
  1434. short args_off, value, n_params, j;
  1435. sprintf(name, "function %d (current offset %#x)", i, offset);
  1436. print_begin_block(name);
  1437. magic = print_byte("magic");
  1438. flags = tlb_read_byte();
  1439. print_offset();
  1440. printf("invoke_kind = %u\n", flags >> 4);
  1441. next = tlb_read_short();
  1442. print_offset();
  1443. if (next != -1)
  1444. printf("next offset = %#x (+%#x=%#x)\n",
  1445. next, funcs_start_offset, next + funcs_start_offset);
  1446. else
  1447. printf("next offset = ffffh\n");
  1448. print_short_hex("name");
  1449. print_hex("dispid");
  1450. print_short_hex("helpcontext");
  1451. value = tlb_read_short();
  1452. print_offset();
  1453. if (value != -1)
  1454. {
  1455. const char *str;
  1456. DWORD hlpstr_maxlen;
  1457. printf("helpstring offset = %#x (+%#x=%#x)\n",
  1458. value, funcs_start_offset, value + funcs_start_offset);
  1459. saved_offset = offset;
  1460. offset = value + funcs_start_offset;
  1461. hlpstr_maxlen = member_offset + sizeof(struct sltg_member_header) + mem->extra - offset;
  1462. str = tlb_read(hlpstr_maxlen);
  1463. str = decode_string((const BYTE *)hlp_strings, str, hlpstr_maxlen, &hlpstr_maxlen);
  1464. print_offset();
  1465. printf("helpstring: \"%s\"\n", str);
  1466. offset = value + funcs_start_offset;
  1467. print_offset();
  1468. printf("helpstring encoded bits: %d bytes\n", hlpstr_maxlen);
  1469. dump_binary(hlpstr_maxlen);
  1470. offset = saved_offset;
  1471. }
  1472. else
  1473. printf("helpstring offset = ffffh\n");
  1474. args_off = tlb_read_short();
  1475. print_offset();
  1476. if (args_off != -1)
  1477. printf("args off = %#x (+%#x=%#x)\n",
  1478. args_off, funcs_start_offset, args_off + funcs_start_offset);
  1479. else
  1480. printf("args off = ffffh\n");
  1481. flags = tlb_read_byte();
  1482. n_params = flags >> 3;
  1483. print_offset();
  1484. printf("callconv %u, cParams %u\n", flags & 0x7, n_params);
  1485. flags = tlb_read_byte();
  1486. print_offset();
  1487. printf("retnextop %02x, cParamsOpt %u\n", flags, (flags & 0x7e) >> 1);
  1488. value = print_short_hex("rettype");
  1489. if (!(flags & 0x80))
  1490. {
  1491. print_offset();
  1492. printf("rettype offset = %#x (+%#x=%#x)\n",
  1493. value, funcs_start_offset, value + funcs_start_offset);
  1494. print_offset();
  1495. printf("rettype:\n");
  1496. sltg_print_compound_type(funcs_start_offset, value);
  1497. }
  1498. else
  1499. {
  1500. print_offset();
  1501. printf("rettype:\n");
  1502. sltg_print_simple_type(value);
  1503. }
  1504. print_short_hex("vtblpos");
  1505. if (magic & 0x20)
  1506. print_short_hex("funcflags");
  1507. if (n_params)
  1508. {
  1509. offset = args_off + funcs_start_offset;
  1510. print_offset();
  1511. printf("arguments start at %#x\n", offset);
  1512. }
  1513. for (j = 0; j < n_params; j++)
  1514. {
  1515. char name[32];
  1516. unsigned short name_offset;
  1517. sprintf(name, "arg %d", j);
  1518. print_begin_block(name);
  1519. name_offset = tlb_read_short();
  1520. print_offset();
  1521. printf("name: %04xh\n", name_offset);
  1522. value = tlb_read_short();
  1523. print_offset();
  1524. printf("type/offset %04xh\n", value);
  1525. if (name_offset & 1) /* type follows */
  1526. {
  1527. print_offset();
  1528. printf("type follows, using current offset for type\n");
  1529. offset -= 2;
  1530. value = offset - funcs_start_offset;
  1531. }
  1532. print_offset();
  1533. printf("arg[%d] off = %#x (+%#x=%#x)\n",
  1534. j, value, funcs_start_offset, value + funcs_start_offset);
  1535. print_offset();
  1536. printf("type:\n");
  1537. value = sltg_print_compound_type(funcs_start_offset, value);
  1538. if (name_offset & 1)
  1539. offset += value;
  1540. print_end_block();
  1541. }
  1542. if (n_params)
  1543. {
  1544. print_offset();
  1545. printf("arguments end at %#x\n", offset);
  1546. }
  1547. if (next != -1)
  1548. {
  1549. if (offset != funcs_start_offset + next)
  1550. dump_binary(funcs_start_offset + next - offset);
  1551. }
  1552. print_end_block();
  1553. }
  1554. }
  1555. else
  1556. {
  1557. printf("skipping %#x bytes\n", extra);
  1558. dump_binary(extra);
  1559. }
  1560. if (offset < member_offset + sizeof(struct sltg_member_header) + mem->extra)
  1561. {
  1562. print_offset();
  1563. printf("skipping %d bytes\n", member_offset + (int)sizeof(struct sltg_member_header) + mem->extra - offset);
  1564. dump_binary(member_offset + sizeof(struct sltg_member_header) + mem->extra - offset);
  1565. }
  1566. print_offset();
  1567. printf("dumped %d (%#x) bytes\n", offset - typeinfo_start_offset, offset - typeinfo_start_offset);
  1568. len -= offset - typeinfo_start_offset;
  1569. print_offset();
  1570. printf("sltg_tail %d (%#x) bytes:\n", len, len);
  1571. saved_offset = offset;
  1572. dump_binary(len);
  1573. offset = saved_offset;
  1574. print_short_hex("cFuncs");
  1575. print_short_hex("cVars");
  1576. print_short_hex("cImplTypes");
  1577. print_short_hex("res06");
  1578. print_short_hex("funcs_off");
  1579. print_short_hex("vars_off");
  1580. print_short_hex("impls_off");
  1581. print_short_hex("funcs_bytes");
  1582. print_short_hex("vars_bytes");
  1583. print_short_hex("impls_bytes");
  1584. print_short_hex("tdescalias_vt");
  1585. print_short_hex("res16");
  1586. print_short_hex("res18");
  1587. print_short_hex("res1a");
  1588. print_short_hex("simple_alias");
  1589. print_short_hex("res1e");
  1590. print_short_hex("cbSizeInstance");
  1591. print_short_hex("cbAlignment");
  1592. print_short_hex("res24");
  1593. print_short_hex("res26");
  1594. print_short_hex("cbSizeVft");
  1595. print_short_hex("res2a");
  1596. print_short_hex("res2c");
  1597. print_short_hex("res2e");
  1598. print_short_hex("res30");
  1599. print_short_hex("res32");
  1600. print_short_hex("res34");
  1601. offset = saved_offset + len;
  1602. }
  1603. static void sltg_dump(void)
  1604. {
  1605. int i, n_file_blocks, n_first_blk, size_of_index, size_of_pad;
  1606. int name_table_start, name_table_size, saved_offset;
  1607. int libblk_start, libblk_len, hlpstr_len, len;
  1608. const char *index, *hlp_strings;
  1609. const struct block_entry *entry;
  1610. n_file_blocks = dump_sltg_header(&n_first_blk, &size_of_index, &size_of_pad);
  1611. saved_offset = offset;
  1612. entry = tlb_read((n_file_blocks - 1) * sizeof(*entry));
  1613. if (!entry) return;
  1614. index = tlb_read(size_of_index);
  1615. if (!index) return;
  1616. offset = saved_offset;
  1617. for (i = 0; i < n_file_blocks - 1; i++)
  1618. dump_sltg_block_entry(i, index);
  1619. saved_offset = offset;
  1620. dump_sltg_index(n_file_blocks);
  1621. assert(offset - saved_offset == size_of_index);
  1622. dump_sltg_pad(size_of_pad);
  1623. /* read the helpstrings for later decoding */
  1624. saved_offset = offset;
  1625. for (i = n_first_blk - 1; entry[i].next != 0; i = entry[i].next - 1)
  1626. tlb_read(entry[i].len);
  1627. libblk_start = offset;
  1628. skip_sltg_library_block();
  1629. tlb_read(0x40);
  1630. typeinfo_cnt = tlb_read_short();
  1631. for (i = 0; i < typeinfo_cnt; i++)
  1632. skip_sltg_other_typeinfo();
  1633. len = tlb_read_int();
  1634. hlpstr_len = (libblk_start + len) - offset;
  1635. hlp_strings = tlb_read(hlpstr_len);
  1636. assert(hlp_strings != NULL);
  1637. /* check the helpstrings header values */
  1638. len = *(int *)(hlp_strings + 2);
  1639. assert(hlpstr_len == len + 6);
  1640. offset = saved_offset;
  1641. for (i = n_first_blk - 1; entry[i].next != 0; i = entry[i].next - 1)
  1642. {
  1643. short magic;
  1644. char name[32];
  1645. saved_offset = offset;
  1646. sprintf(name, "Block %d", i);
  1647. print_begin_block(name);
  1648. magic = tlb_read_short();
  1649. assert(magic == 0x0501);
  1650. offset -= 2;
  1651. dump_binary(entry[i].len);
  1652. print_end_block();
  1653. offset = saved_offset;
  1654. print_begin_block(name);
  1655. dump_type(entry[i].len, hlp_strings);
  1656. print_end_block();
  1657. offset = saved_offset + entry[i].len;
  1658. }
  1659. libblk_len = entry[i].len;
  1660. libblk_start = offset;
  1661. dump_sltg_library_block();
  1662. printf("skipping 0x40 bytes\n");
  1663. dump_binary(0x40);
  1664. printf("\n");
  1665. typeinfo_cnt = print_short_dec("typeinfo count");
  1666. printf("\n");
  1667. for (i = 0; i < typeinfo_cnt; i++)
  1668. dump_sltg_other_typeinfo(i, hlp_strings);
  1669. len = print_hex("offset from start of library block to name table");
  1670. printf("%#x + %#x = %#x\n", libblk_start, len, libblk_start + len);
  1671. len = (libblk_start + len) - offset;
  1672. printf("skipping %#x bytes (encoded/compressed helpstrings)\n", len);
  1673. printf("max string length: %#x, strings length %#x\n", *(short *)hlp_strings, *(int *)(hlp_strings + 2));
  1674. dump_binary(len);
  1675. printf("\n");
  1676. len = print_short_hex("name table jump");
  1677. if (len == 0xffff)
  1678. {
  1679. printf("skipping 0x000a bytes\n");
  1680. dump_binary(0x000a);
  1681. printf("\n");
  1682. }
  1683. else if (len == 0x0200)
  1684. {
  1685. printf("skipping 0x002a bytes\n");
  1686. dump_binary(0x002a);
  1687. printf("\n");
  1688. }
  1689. else
  1690. {
  1691. printf("FIXME: please report! (%#x)\n", len);
  1692. assert(0);
  1693. }
  1694. printf("skipping 0x200 bytes\n");
  1695. dump_binary(0x200);
  1696. printf("\n");
  1697. name_table_size = print_hex("name table size");
  1698. name_table_start = offset;
  1699. printf("name table offset = %#x\n\n", offset);
  1700. while (offset < name_table_start + name_table_size)
  1701. {
  1702. int aligned_len;
  1703. dump_binary(8);
  1704. print_string0();
  1705. printf("\n");
  1706. len = offset - name_table_start;
  1707. aligned_len = (len + 0x1f) & ~0x1f;
  1708. if (aligned_len - len < 4)
  1709. dump_binary(aligned_len - len);
  1710. else
  1711. dump_binary(len & 1);
  1712. printf("\n");
  1713. }
  1714. print_hex("01ffff01");
  1715. len = print_hex("length");
  1716. printf("skipping %#x bytes\n", len);
  1717. dump_binary(len);
  1718. printf("\n");
  1719. len = (libblk_start + libblk_len) - offset;
  1720. printf("skipping libblk remainder %#x bytes\n", len);
  1721. dump_binary(len);
  1722. printf("\n");
  1723. /* FIXME: msodumper/olestream.py parses this block differently
  1724. print_short_hex("unknown");
  1725. print_short_hex("byte order mark");
  1726. i = tlb_read_short();
  1727. printf("version = %u.%u\n", i & 0xff, i >> 8);
  1728. print_short_hex("system identifier");
  1729. print_hex("unknown");
  1730. printf("\n");
  1731. */
  1732. printf("skipping 12 bytes\n");
  1733. dump_binary(12);
  1734. printf("\n");
  1735. print_guid("uuid");
  1736. printf("\n");
  1737. /* 0x0008,"TYPELIB",0 */
  1738. dump_binary(12);
  1739. printf("\n");
  1740. printf("skipping 12 bytes\n");
  1741. dump_binary(12);
  1742. printf("\n");
  1743. printf("skipping remainder 0x10 bytes\n");
  1744. dump_binary(0x10);
  1745. printf("\n");
  1746. }
  1747. void tlb_dump(void)
  1748. {
  1749. const DWORD *sig = PRD(0, sizeof(DWORD));
  1750. if (*sig == MSFT_MAGIC)
  1751. msft_dump();
  1752. else
  1753. sltg_dump();
  1754. }
  1755. enum FileSig get_kind_tlb(void)
  1756. {
  1757. const DWORD *sig = PRD(0, sizeof(DWORD));
  1758. if (sig && (*sig == MSFT_MAGIC || *sig == SLTG_MAGIC)) return SIG_TLB;
  1759. return SIG_UNKNOWN;
  1760. }