tlb.c 52 KB

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