tlb.c 51.4 KB
Newer Older
1 2 3 4
/*
 *  Dump a typelib (tlb) file
 *
 *  Copyright 2006 Jacek Caban
5
 *  Copyright 2015 Dmitry Timoshkov
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "winedump.h"

#define MSFT_MAGIC 0x5446534d
31
#define SLTG_MAGIC 0x47544c53
32 33 34 35 36 37 38 39 40 41 42 43 44 45
#define HELPDLLFLAG 0x0100

enum TYPEKIND {
  TKIND_ENUM = 0,
  TKIND_RECORD,
  TKIND_MODULE,
  TKIND_INTERFACE,
  TKIND_DISPATCH,
  TKIND_COCLASS,
  TKIND_ALIAS,
  TKIND_UNION,
  TKIND_MAX
};

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
enum VARENUM {
    VT_EMPTY = 0,
    VT_NULL = 1,
    VT_I2 = 2,
    VT_I4 = 3,
    VT_R4 = 4,
    VT_R8 = 5,
    VT_CY = 6,
    VT_DATE = 7,
    VT_BSTR = 8,
    VT_DISPATCH = 9,
    VT_ERROR = 10,
    VT_BOOL = 11,
    VT_VARIANT = 12,
    VT_UNKNOWN = 13,
    VT_DECIMAL = 14,
    VT_I1 = 16,
    VT_UI1 = 17,
    VT_UI2 = 18,
    VT_UI4 = 19,
    VT_I8 = 20,
    VT_UI8 = 21,
    VT_INT = 22,
    VT_UINT = 23,
    VT_VOID = 24,
    VT_HRESULT = 25,
    VT_PTR = 26,
    VT_SAFEARRAY = 27,
    VT_CARRAY = 28,
    VT_USERDEFINED = 29,
    VT_LPSTR = 30,
    VT_LPWSTR = 31,
    VT_RECORD = 36,
    VT_INT_PTR = 37,
    VT_UINT_PTR = 38,
    VT_FILETIME = 64,
    VT_BLOB = 65,
    VT_STREAM = 66,
    VT_STORAGE = 67,
    VT_STREAMED_OBJECT = 68,
    VT_STORED_OBJECT = 69,
    VT_BLOB_OBJECT = 70,
    VT_CF = 71,
    VT_CLSID = 72,
    VT_VERSIONED_STREAM = 73,
    VT_BSTR_BLOB = 0xfff,
    VT_VECTOR = 0x1000,
    VT_ARRAY = 0x2000,
    VT_BYREF = 0x4000,
    VT_RESERVED = 0x8000,
    VT_ILLEGAL = 0xffff,
    VT_ILLEGALMASKED = 0xfff,
    VT_TYPEMASK = 0xfff
};

101 102
struct seg_t;

103
typedef BOOL (*dump_seg_t)(struct seg_t*);
104 105 106 107 108 109 110

typedef struct seg_t {
    const char *name;
    dump_seg_t func;
    int offset;
    int length;
} seg_t;
111
static seg_t segdir[15];
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

enum SEGDIRTYPE {
    SEGDIR_TYPEINFO,
    SEGDIR_IMPINFO,
    SEGDIR_IMPFILES,
    SEGDIR_REF,
    SEGDIR_GUIDHASH,
    SEGDIR_GUID,
    SEGDIR_NAMEHASH,
    SEGDIR_NAME,
    SEGDIR_STRING,
    SEGDIR_TYPEDESC,
    SEGDIR_ARRAYDESC,
    SEGDIR_CUSTDATA,
    SEGDIR_CDGUID,
    SEGDIR_res0e,
    SEGDIR_res0f
};
130 131 132 133 134

static int offset=0;
static int indent;
static int typeinfo_cnt;
static int header_flags = 0;
135
static BOOL msft_eof = FALSE;
136 137 138 139 140 141 142

static int msft_typeinfo_offs[1000];
static int msft_typeinfo_kind[1000];
static int msft_typeinfo_impltypes[1000];
static int msft_typeinfo_elemcnt[1000];
static int msft_typeinfo_cnt = 0;

143 144 145 146 147 148
static const char * const tkind[TKIND_MAX] = {
    "TKIND_ENUM", "TKIND_RECORD", "TKIND_MODULE",
    "TKIND_INTERFACE", "TKIND_DISPATCH", "TKIND_COCLASS",
    "TKIND_ALIAS", "TKIND_UNION"
};

149 150 151 152 153 154
static const void *tlb_read(int size) {
    const void *ret = PRD(offset, size);

    if(ret)
        offset += size;
    else
155
        msft_eof = TRUE;
156 157 158 159 160 161

    return ret;
}

static int tlb_read_int(void)
{
162
    const int *ret = tlb_read(sizeof(int));
163 164 165 166 167
    return ret ? *ret : -1;
}

static int tlb_read_short(void)
{
168
    const unsigned short *ret = tlb_read(sizeof(short));
169 170 171 172 173
    return ret ? *ret : -1;
}

static int tlb_read_byte(void)
{
174
    const unsigned char *ret = tlb_read(sizeof(char));
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    return ret ? *ret : -1;
}

static void print_offset(void)
{
    int i;
    for(i=0; i<indent; i++)
        printf("    ");
}

static void print_begin_block(const char *name)
{
    print_offset();
    printf("%s {\n", name);
    indent++;
}

static void print_begin_block_id(const char *name, int id)
{
    char buf[64];
    sprintf(buf, "%s %d", name, id);
    print_begin_block(buf);
}

static void print_end_block(void)
{
    indent--;
    print_offset();
    printf("}\n");
204 205 206 207 208
}

static int print_byte(const char *name)
{
    unsigned char ret;
209
    print_offset();
210 211
    printf("%s = %02xh\n", name, ret=tlb_read_byte());
    return ret;
212 213 214 215 216 217
}

static int print_hex(const char *name)
{
    int ret;
    print_offset();
218
    printf("%s = %08xh\n", name, ret=tlb_read_int());
219 220 221 222 223 224 225 226 227 228 229 230 231 232
    return ret;
}

static int print_hex_id(const char *name, int id)
{
    char buf[64];
    sprintf(buf, name, id);
    return print_hex(buf);
}

static int print_short_hex(const char *name)
{
    int ret;
    print_offset();
233
    printf("%s = %04xh\n", name, ret=tlb_read_short());
234 235 236
    return ret;
}

237 238 239 240 241 242 243 244
static int print_short_dec(const char *name)
{
    int ret;
    print_offset();
    printf("%s = %d\n", name, ret=tlb_read_short());
    return ret;
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
static int print_dec(const char *name)
{
    int ret;
    print_offset();
    printf("%s = %d\n", name, ret=tlb_read_int());
    return ret;
}

static void print_guid(const char *name)
{
    GUID guid = *(const GUID*)tlb_read(sizeof(guid));

    print_offset();

    printf("%s = {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n", name,
260
           (unsigned int)guid.Data1, guid.Data2, guid.Data3, guid.Data4[0],
261 262 263 264
           guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4],
           guid.Data4[5], guid.Data4[6], guid.Data4[7]);
}

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
static void print_vartype(int vartype)
{
    static const char *vartypes[VT_LPWSTR+1] = {
      "VT_EMPTY",   "VT_NULL", "VT_I2",       "VT_I4",     "VT_R4",
      "VT_R8",      "VT_CY",   "VT_DATE",     "VT_BSTR",   "VT_DISPATCH",
      "VT_ERROR",   "VT_BOOL", "VT_VARIANT",  "VT_UNKNOWN","VT_DECIMAL",
      "unk 15",     "VT_I1",   "VT_UI1",      "VT_UI2",    "VT_UI4",
      "VT_I8",      "VT_UI8",  "VT_INT",      "VT_UINT",   "VT_VOID",
      "VT_HRESULT", "VT_PTR",  "VT_SAFEARRAY","VT_CARRAY", "VT_USERDEFINED",
      "VT_LPSTR",   "VT_LPWSTR"
    };

    vartype &= VT_TYPEMASK;
    if (vartype >= VT_EMPTY && vartype <= VT_LPWSTR)
        printf("%s\n", vartypes[vartype]);
    else
        printf("unk %d\n", vartype);
}

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
static void print_ctl2(const char *name)
{
    int len;
    const char *buf;

    print_offset();

    len = tlb_read_short();

    printf("%s = %d \"", name, len);
    len >>= 2;
    buf = tlb_read(len);
    fwrite(buf, len, 1, stdout);
    printf("\"");
    len += 2;

    while(len++ & 3)
        printf("\\%02x", tlb_read_byte());
    printf("\n");
}

305
static int tlb_isprint(unsigned char c)
306
{
307 308 309 310 311 312 313 314 315
    return c >= 32;
}

static void dump_binary(int size)
{
    const unsigned char *ptr;
    int i, j;

    if (!size) return;
316

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    ptr = tlb_read(size);
    if (!ptr) return;

    print_offset();
    printf("%08x: ", offset - size);

    for (i = 0; i < size; i++)
    {
        printf("%02x%c", ptr[i], (i % 16 == 7) ? '-' : ' ');
        if ((i % 16) == 15)
        {
            printf( " " );
            for (j = 0; j < 16; j++)
                printf("%c", tlb_isprint(ptr[i-15+j]) ? ptr[i-15+j] : '.');
            if (i < size-1)
            {
                printf("\n");
                print_offset();
                printf("%08x: ", offset - size + i + 1);
            }
337 338
        }
    }
339 340 341 342 343 344 345
    if (i % 16)
    {
        printf("%*s ", 3 * (16-(i%16)), "");
        for (j = 0; j < i % 16; j++)
            printf("%c", tlb_isprint(ptr[i-(i%16)+j]) ? ptr[i-(i%16)+j] : '.');
    }
    printf("\n");
346 347
}

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
static int dump_msft_varflags(void)
{
    static const char *syskind[] = {
        "SYS_WIN16", "SYS_WIN32", "SYS_MAC", "SYS_WIN64", "unknown"
    };
    int kind, flags;

    print_offset();
    flags = tlb_read_int();
    kind = flags & 0xf;
    if (kind > 3) kind = 4;
    printf("varflags = %08x, syskind = %s\n", flags, syskind[kind]);
    return flags;
}

static void dump_msft_version(void)
{
365
    unsigned version;
366 367
    print_offset();
    version = tlb_read_int();
368
    printf("version = %u.%u\n", version & 0xffff, version >> 16);
369 370
}

371 372 373 374 375 376 377 378
static void dump_msft_header(void)
{
    print_begin_block("Header");

    print_hex("magic1");
    print_hex("magic2");
    print_hex("posguid");
    print_hex("lcid");
379 380 381
    print_hex("lcid2");
    header_flags = dump_msft_varflags();
    dump_msft_version();
382 383
    print_hex("flags");
    typeinfo_cnt = print_dec("ntypeinfos");
384 385 386 387
    print_dec("helpstring");
    print_dec("helpstringcontext");
    print_dec("helpcontext");
    print_dec("nametablecount");
388 389 390 391 392 393 394 395 396 397 398 399
    print_dec("nametablechars");
    print_hex("NameOffset");
    print_hex("helpfile");
    print_hex("CustomDataOffset");
    print_hex("res44");
    print_hex("res48");
    print_hex("dispatchpos");
    print_hex("res50");

    print_end_block();
}

400 401 402 403 404 405 406 407 408 409 410
static int dump_msft_typekind(void)
{
    int ret, typekind;

    print_offset();
    ret = tlb_read_int();
    typekind = ret & 0xf;
    printf("typekind = %s, align = %d\n", typekind < TKIND_MAX ? tkind[typekind] : "unknown", (ret >> 11) & 0x1f);
    return ret;
}

411 412 413 414
static void dump_msft_typeinfobase(void)
{
    print_begin_block_id("TypeInfoBase", msft_typeinfo_cnt);

415
    msft_typeinfo_kind[msft_typeinfo_cnt] = dump_msft_typekind();
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    msft_typeinfo_offs[msft_typeinfo_cnt] = print_hex("memoffset");
    print_hex("res2");
    print_hex("res3");
    print_hex("res4");
    print_hex("res5");
    msft_typeinfo_elemcnt[msft_typeinfo_cnt] = print_hex("cElement");
    print_hex("res7");
    print_hex("res8");
    print_hex("res9");
    print_hex("resA");
    print_hex("posguid");
    print_hex("flags");
    print_hex("NameOffset");
    print_hex("version");
    print_hex("docstringoffs");
    print_hex("docstringcontext");
    print_hex("helpcontext");
    print_hex("oCustData");
    msft_typeinfo_impltypes[msft_typeinfo_cnt++] = print_short_hex("cImplTypes");
    print_short_hex("bSizeVftt");
    print_dec("size");
    print_hex("datatype1");
    print_hex("datatype2");
    print_hex("res18");
    print_hex("res19");

    print_end_block();
}

445
static BOOL dump_msft_typeinfobases(seg_t *seg)
446 447 448 449 450 451 452
{
    int i;

    for(i = 0; offset < seg->offset+seg->length; i++)
        dump_msft_typeinfobase();

    assert(offset == seg->offset+seg->length);
453
    return TRUE;
454 455 456 457 458 459 460 461 462 463 464 465 466
}

static void dump_msft_impinfo(int n)
{
    print_begin_block_id("ImpInfo", n);

    print_hex("flags");
    print_hex("oImpInfo");
    print_hex("oGuid");

    print_end_block();
}

467
static BOOL dump_msft_impinfos(seg_t *seg)
468 469 470 471 472 473 474
{
    int i;

    for(i = 0; offset < seg->offset+seg->length; i++)
        dump_msft_impinfo(i);

    assert(offset == seg->offset+seg->length);
475
    return TRUE;
476 477 478 479 480 481 482 483 484 485 486 487 488 489
}

static void dump_msft_impfile(int n)
{
    print_begin_block_id("ImpFile", n);

    print_hex("guid");
    print_hex("lcid");
    print_hex("version");
    print_ctl2("impfile");

    print_end_block();
}

490
static BOOL dump_msft_impfiles(seg_t *seg)
491 492 493 494 495 496 497
{
    int i;

    for(i = 0; offset < seg->offset+seg->length; i++)
        dump_msft_impfile(i);

    assert(offset == seg->offset+seg->length);
498
    return TRUE;
499 500
}

501
static BOOL dump_msft_reftabs(seg_t *seg)
502 503 504 505 506 507 508
{
    print_begin_block("RefTab");

    dump_binary(seg->length); /* FIXME */

    print_end_block();

509
    return TRUE;
510 511
}

512
static BOOL dump_msft_guidhashtab(seg_t *seg)
513
{
514
    print_begin_block("GuidHashTab");
515 516 517 518 519 520

    dump_binary(seg->length); /* FIXME */

    print_end_block();

    assert(offset == seg->offset+seg->length);
521
    return TRUE;
522 523 524 525 526 527 528 529 530 531 532 533 534
}

static void dump_msft_guidentry(int n)
{
    print_begin_block_id("GuidEntry", n);

    print_guid("guid");
    print_hex("hreftype");
    print_hex("next_hash");

    print_end_block();
}

535
static BOOL dump_msft_guidtab(seg_t *seg)
536 537 538 539 540 541 542
{
    int i;

    for(i = 0; offset < seg->offset+seg->length; i++)
        dump_msft_guidentry(i);

    assert(offset == seg->offset+seg->length);
543
    return TRUE;
544 545
}

546
static BOOL dump_msft_namehashtab(seg_t *seg)
547
{
548
    print_begin_block("NameHashTab");
549 550 551 552

    dump_binary(seg->length); /* FIXME */

    print_end_block();
553
    return TRUE;
554 555
}

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
static void print_string0(void)
{
    unsigned char c;

    printf("\"");
    while ((c = tlb_read_byte()) != 0)
    {
        if (isprint(c))
            fwrite(&c, 1, 1, stdout);
        else
        {
            char buf[16];
            sprintf(buf, "\\%u", c);
            fwrite(buf, 1, strlen(buf), stdout);
        }
    }
    printf("\"");
}

575
static void print_string(int len)
576 577 578
{
    printf("\"");
    fwrite(tlb_read(len), len, 1, stdout);
579 580 581 582 583 584 585
    printf("\"");
}

static void dump_string(int len, int align_off)
{
    print_string(len);
    printf(" ");
586 587 588 589
    while((len++ + align_off) & 3)
        printf("\\%2.2x", tlb_read_byte());
}

590 591 592 593 594 595 596 597 598 599 600
static void dump_msft_name(int base, int n)
{
    int len;

    print_begin_block_id("Name", n);

    print_hex("hreftype");
    print_hex("next_hash");
    len = print_hex("namelen")&0xff;

    print_offset();
601 602
    printf("name = ");
    dump_string(len, 0);
603 604 605 606 607
    printf("\n");

    print_end_block();
}

608
static BOOL dump_msft_nametab(seg_t *seg)
609 610 611 612 613 614 615
{
    int i, base = offset;

    for(i = 0; offset < seg->offset+seg->length; i++)
        dump_msft_name(base, i);

    assert(offset == seg->offset+seg->length);
616
    return TRUE;
617 618
}

619
static void dump_msft_string(int n)
620
{
621
    int len;
622

623 624 625 626 627 628 629 630 631 632 633 634 635
    print_begin_block_id("String", n);

    len = print_short_hex("stringlen");

    print_offset();
    printf("string = ");
    dump_string(len, 2);

    if(len < 3) {
        for(len = 0; len < 4; len++)
            printf("\\%2.2x", tlb_read_byte());
    }
    printf("\n");
636 637

    print_end_block();
638 639
}

640
static BOOL dump_msft_stringtab(seg_t *seg)
641 642 643 644 645
{
    int i;

    for(i = 0; offset < seg->offset+seg->length; i++)
        dump_msft_string(i);
646

647
    assert(offset == seg->offset+seg->length);
648
    return TRUE;
649 650 651 652 653 654 655 656 657 658 659 660
}

static void dump_msft_typedesc(int n)
{
    print_begin_block_id("TYPEDESC", n);

    print_hex("hreftype");
    print_hex("vt");

    print_end_block();
}

661
static BOOL dump_msft_typedesctab(seg_t *seg)
662 663 664 665 666 667 668 669 670 671 672
{
    int i;

    print_begin_block("TypedescTab");

    for(i = 0; offset < seg->offset+seg->length; i++)
        dump_msft_typedesc(i);

    print_end_block();

    assert(offset == seg->offset+seg->length);
673
    return TRUE;
674 675
}

676
static BOOL dump_msft_arraydescs(seg_t *seg)
677 678 679 680 681 682
{
    print_begin_block("ArrayDescriptions");

    dump_binary(seg->length); /* FIXME */

    print_end_block();
683
    return TRUE;
684 685
}

686
static BOOL dump_msft_custdata(seg_t *seg)
687
{
688 689 690
    unsigned short vt;
    unsigned i, n;

691 692
    print_begin_block("CustData");

693 694 695 696 697 698 699 700
    for(i=0; offset < seg->offset+seg->length; i++) {
        print_offset();

        vt = tlb_read_short();
        printf("vt %d", vt);
        n = tlb_read_int();

        switch(vt) {
701
        case VT_BSTR:
702 703 704 705 706 707
            printf(" len %d: ", n);
            dump_string(n, 2);
            printf("\n");
            break;
        default:
            printf(": %x ", n);
708 709
            printf("\\%2.2x ", tlb_read_byte());
            printf("\\%2.2x\n", tlb_read_byte());
710 711
        }
    }
712 713

    print_end_block();
714
    return TRUE;
715 716 717 718 719 720 721 722 723 724 725 726 727
}

static void dump_msft_cdguid(int n)
{
    print_begin_block_id("CGUid", n);

    print_hex("GuidOffset");
    print_hex("DataOffset");
    print_hex("next");

    print_end_block();
}

728
static BOOL dump_msft_cdguids(seg_t *seg)
729 730 731 732 733 734 735
{
    int i;

    for(i = 0; offset < seg->offset+seg->length; i++)
        dump_msft_cdguid(i);

    assert(offset == seg->offset+seg->length);
736
    return TRUE;
737 738
}

739
static BOOL dump_msft_res0e(seg_t *seg)
740 741 742 743 744
{
    print_begin_block("res0e");
    dump_binary(seg->length);
    print_end_block();

745
    return TRUE;
746 747
}

748
static BOOL dump_msft_res0f(seg_t *seg)
749 750 751 752 753
{
    print_begin_block("res0f");
    dump_binary(seg->length);
    print_end_block();

754
    return TRUE;
755 756
}

757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
/* Used for function return value and arguments type */
static void dump_msft_datatype(const char *name)
{
    int datatype;

    print_offset();
    datatype = tlb_read_int();
    printf("%s = %08x", name, datatype);
    if (datatype < 0) {
       printf(", ");
       print_vartype(datatype);
    }
    else {
       const short *vt;

       if (datatype > segdir[SEGDIR_TYPEDESC].length) {
           printf(", invalid offset\n");
           return;
       }

       /* FIXME: in case of VT_USERDEFINED use hreftype */
       vt = PRD(segdir[SEGDIR_TYPEDESC].offset + datatype, 4*sizeof(short));
       datatype = vt[0] & VT_TYPEMASK;
       if (datatype == VT_PTR) {
           printf(", VT_PTR -> ");
           if (vt[3] < 0)
               datatype = vt[2];
           else {
               vt = PRD(segdir[SEGDIR_TYPEDESC].offset + vt[2], 4*sizeof(short));
               datatype = *vt;
           }
       }
       else {
           printf(", ");
           datatype = *vt;
       }

       print_vartype(datatype);
    }
}

static void dump_defaultvalue(int id)
{
    int offset;

    print_offset();
    offset = tlb_read_int();

    printf("default value[%d] = %08x", id, offset);
    if (offset == -1)
        printf("\n");
    else if (offset < 0) {
        printf(", ");
        print_vartype((offset & 0x7c000000) >> 26);
    }
    else {
        const unsigned short *vt;

        if (offset > segdir[SEGDIR_CUSTDATA].length) {
            printf(", invalid offset\n");
            return;
        }

        vt = PRD(segdir[SEGDIR_CUSTDATA].offset + offset, sizeof(*vt));
        printf(", ");
        print_vartype(*vt);
    }
}

826 827
static void dump_msft_func(int n)
{
828
    int size, args_cnt, i, extra_attr, fkccic;
829 830 831 832 833

    print_begin_block_id("FuncRecord", n);

    size = print_short_hex("size");
    print_short_hex("index");
834
    dump_msft_datatype("retval type");
835 836 837
    print_hex("flags");
    print_short_hex("VtableOffset");
    print_short_hex("funcdescsize");
838
    fkccic = print_hex("FKCCIC");
839 840 841
    args_cnt = print_short_hex("nrargs");
    print_short_hex("noptargs");

842
    extra_attr = size/sizeof(INT) - 6 - args_cnt*(fkccic&0x1000 ? 4 : 3);
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857

    if(extra_attr)
        print_hex("helpcontext");
    if(extra_attr >= 2)
        print_hex("oHelpString");
    if(extra_attr >= 3)
        print_hex("toEntry");
    if(extra_attr >= 4)
        print_hex("res9");
    if(extra_attr >= 5)
        print_hex("resA");
    if(extra_attr >= 6)
        print_hex("HelpStringContext");
    if(extra_attr >= 7)
        print_hex("oCustData");
858 859
    for(i = 0; i < extra_attr-7; i++)
        print_hex_id("oArgCustData", i);
860

861 862
    if(fkccic & 0x1000) {
        for(i=0; i < args_cnt; i++)
863
            dump_defaultvalue(i);
864 865
    }

866 867 868 869
    for(i=0; i < args_cnt; i++) {
        print_begin_block_id("param", i);

        /* FIXME: Handle default values */
870 871
        dump_msft_datatype("datatype");
        print_hex("name");
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
        print_hex("paramflags");

        print_end_block();
    }

    print_end_block();
}

static void dump_msft_var(int n)
{
    INT size;

    print_begin_block_id("VarRecord", n);

    size = print_hex("recsize")&0x1ff;
    print_hex("DataType");
    print_hex("flags");
    print_short_hex("VarKind");
    print_short_hex("vardescsize");
    print_hex("OffsValue");

    if(size > 5*sizeof(INT))
        dump_binary(size - 5*sizeof(INT));

    print_end_block();
}

static void dump_msft_ref(int n)
{
    print_begin_block_id("RefRecord", n);

    print_hex("reftype");
    print_hex("flags");
    print_hex("oCustData");
    print_hex("onext");

    print_end_block();
}

static void dump_msft_coclass(int n)
{
    int i;

    print_dec("size");

    for(i=0; i < msft_typeinfo_impltypes[n]; i++)
        dump_msft_ref(i);
}

921
static BOOL dump_msft_typeinfo(int n)
922 923 924 925 926 927 928 929
{
    int i;

    print_begin_block_id("TypeInfo", n);

    if((msft_typeinfo_kind[n] & 0xf) == TKIND_COCLASS) {
        dump_msft_coclass(n);
        print_end_block();
930
        return TRUE;
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
    }

    print_dec("size");

    for(i = 0; i < LOWORD(msft_typeinfo_elemcnt[n]); i++)
        dump_msft_func(i);

    for(i = 0; i < HIWORD(msft_typeinfo_elemcnt[n]); i++)
        dump_msft_var(i);

    for(i = 0; i < LOWORD(msft_typeinfo_elemcnt[n]); i++)
        print_hex_id("func %d id", i);

    for(i = 0; i < HIWORD(msft_typeinfo_elemcnt[n]); i++)
        print_hex_id("var %d id", i);

    for(i = 0; i < LOWORD(msft_typeinfo_elemcnt[n]); i++)
        print_hex_id("func %d name", i);

    for(i = 0; i < HIWORD(msft_typeinfo_elemcnt[n]); i++)
        print_hex_id("var %d name", i);

    for(i = 0; i < LOWORD(msft_typeinfo_elemcnt[n]); i++)
        print_hex_id("func %d offset", i);

    for(i = 0; i < HIWORD(msft_typeinfo_elemcnt[n]); i++)
        print_hex_id("var %d offset", i);

    print_end_block();

961
    return TRUE;
962 963
}

964
static seg_t segdir[] = {
965 966 967 968
    {"TypeInfoTab",       dump_msft_typeinfobases, -1, -1},
    {"ImpInfo",           dump_msft_impinfos, -1, -1},
    {"ImpFiles",          dump_msft_impfiles, -1, -1},
    {"RefTab",            dump_msft_reftabs, -1, -1},
969
    {"GuidHashTab",       dump_msft_guidhashtab, -1, -1},
970
    {"GuidTab",           dump_msft_guidtab, -1, -1},
971
    {"NameHashTab",       dump_msft_namehashtab, -1, -1},
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
    {"pNameTab",          dump_msft_nametab, -1, -1},
    {"pStringTab",        dump_msft_stringtab, -1, -1},
    {"TypedescTab",       dump_msft_typedesctab, -1, -1},
    {"ArrayDescriptions", dump_msft_arraydescs, -1, -1},
    {"CustData",          dump_msft_custdata, -1, -1},
    {"CDGuid",            dump_msft_cdguids, -1, -1},
    {"res0e",             dump_msft_res0e, -1, -1},
    {"res0f",             dump_msft_res0f, -1, -1}
};

static void dump_msft_seg(seg_t *seg)
{
    print_begin_block(seg->name);

    seg->offset = print_hex("offset");
    seg->length = print_dec("length");
    print_hex("res08");
    print_hex("res0c");

    print_end_block();
}

static void dump_msft_segdir(void)
{
    int i;

    print_begin_block("SegDir");

1000
    for(i=0; i < ARRAY_SIZE(segdir); i++)
1001 1002 1003 1004 1005
        dump_msft_seg(segdir+i);

    print_end_block();
}

1006
static BOOL dump_offset(void)
1007 1008 1009
{
    int i;

1010
    for(i=0; i < ARRAY_SIZE(segdir); i++)
1011 1012 1013 1014 1015 1016 1017
        if(segdir[i].offset == offset)
            return segdir[i].func(segdir+i);

    for(i=0; i < msft_typeinfo_cnt; i++)
        if(msft_typeinfo_offs[i] == offset)
            return dump_msft_typeinfo(i);

1018
    return FALSE;
1019 1020
}

1021
static void msft_dump(void)
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
{
    int i;

    dump_msft_header();

    for(i=0; i < typeinfo_cnt; i++)
        print_hex_id("typeinfo %d offset", i);

    if(header_flags & HELPDLLFLAG)
        print_hex("help dll offset");
    print_offset();
    printf("\n");

    dump_msft_segdir();

    while(!msft_eof) {
        if(!dump_offset())
            print_hex("unknown");
    }
}
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164

/****************************** SLTG Typelibs ******************************/

struct block_entry
{
    DWORD len;
    WORD index_string;
    WORD next;
};

struct bitstream
{
    const BYTE *buffer;
    DWORD       length;
    WORD        current;
};

#include "pshpack1.h"
struct sltg_typeinfo_header
{
    short magic;
    int href_table;
    int res06;
    int elem_table;
    int res0e;
    int version;
    int res16;
    struct
    {
        unsigned unknown1  : 3;
        unsigned flags     : 16;
        unsigned unknown2  : 5;
        unsigned typekind  : 8;
    } misc;
    int res1e;
};

struct sltg_member_header
{
    short res00;
    short res02;
    char res04;
    int extra;
};

struct sltg_tail
{
    unsigned short cFuncs;
    unsigned short cVars;
    unsigned short cImplTypes;
    unsigned short res06; /* always 0000 */
    unsigned short funcs_off; /* offset to functions (starting from the member header) */
    unsigned short vars_off; /* offset to vars (starting from the member header) */
    unsigned short impls_off; /* offset to implemented types (starting from the member header) */
    unsigned short funcs_bytes; /* bytes used by function data */
    unsigned short vars_bytes; /* bytes used by var data */
    unsigned short impls_bytes; /* bytes used by implemented type data */
    unsigned short tdescalias_vt; /* for TKIND_ALIAS */
    unsigned short res16; /* always ffff */
    unsigned short res18; /* always 0000 */
    unsigned short res1a; /* always 0000 */
    unsigned short simple_alias; /* tdescalias_vt is a vt rather than an offset? */
    unsigned short res1e; /* always 0000 */
    unsigned short cbSizeInstance;
    unsigned short cbAlignment;
    unsigned short res24;
    unsigned short res26;
    unsigned short cbSizeVft;
    unsigned short res2a; /* always ffff */
    unsigned short res2c; /* always ffff */
    unsigned short res2e; /* always ffff */
    unsigned short res30; /* always ffff */
    unsigned short res32;
    unsigned short res34;
};

struct sltg_variable
{
  char magic; /* 0x0a */
  char flags;
  short next;
  short name;
  short byte_offs; /* pos in struct, or offset to const type or const data (if flags & 0x08) */
  short type; /* if flags & 0x02 this is the type, else offset to type */
  int memid;
  short helpcontext; /* ?? */
  short helpstring; /* ?? */
#if 0
  short varflags; /* only present if magic & 0x20 */
#endif
};
#include "poppack.h"

static const char *lookup_code(const BYTE *table, DWORD table_size, struct bitstream *bits)
{
    const BYTE *p = table;

    while (p < table + table_size && *p == 0x80)
    {
        if (p + 2 >= table + table_size) return NULL;

        if (!(bits->current & 0xff))
        {
            if (!bits->length) return NULL;
            bits->current = (*bits->buffer << 8) | 1;
            bits->buffer++;
            bits->length--;
        }

        if (bits->current & 0x8000)
        {
            p += 3;
        }
        else
        {
            p = table + (*(p + 2) | (*(p + 1) << 8));
        }

        bits->current <<= 1;
    }

    if (p + 1 < table + table_size && *(p + 1))
    {
1165
        /* FIXME: What is the meaning of *p? */
1166 1167 1168 1169 1170 1171 1172 1173
        const BYTE *q = p + 1;
        while (q < table + table_size && *q) q++;
        return (q < table + table_size) ? (const char *)(p + 1) : NULL;
    }

    return NULL;
}

1174
static const char *decode_string(const BYTE *table, const char *stream, UINT stream_length, UINT *read_bytes)
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
{
    char *buf;
    DWORD buf_size, table_size;
    const char *p;
    struct bitstream bits;

    bits.buffer = (const BYTE *)stream;
    bits.length = stream_length;
    bits.current = 0;

    buf_size = *(const WORD *)table;
    table += sizeof(WORD);
    table_size = *(const DWORD *)table;
    table += sizeof(DWORD);

1190
    buf = xmalloc(buf_size);
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
    buf[0] = 0;

    while ((p = lookup_code(table, table_size, &bits)))
    {
        if (buf[0]) strcat(buf, " ");
        assert(strlen(buf) + strlen(p) + 1 <= buf_size);
        strcat(buf, p);
    }

    if (read_bytes) *read_bytes = stream_length - bits.length;

    return buf;
}

static void print_sltg_name(const char *name)
{
    unsigned short len = tlb_read_short();
    print_offset();
    printf("%s = %#x (", name, len);
    if (len != 0xffff) print_string(len);
    printf(")\n");
}

1214
static int dump_sltg_header(int *sltg_first_blk, int *size_of_index, int *size_of_pad)
1215 1216 1217 1218 1219 1220 1221
{
    int n_file_blocks;

    print_begin_block("Header");

    print_hex("magic");
    n_file_blocks = print_short_dec("# file blocks");
1222
    *size_of_pad = print_short_hex("pad");
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
    *size_of_index = print_short_hex("size of index");
    *sltg_first_blk = print_short_dec("first block");
    print_guid("guid");
    print_hex("res1c");
    print_hex("res20");

    print_end_block();

    return n_file_blocks;
}

static void dump_sltg_index(int count)
{
    int i;

    printf("index:\n");

    print_string0();
    printf("\n");
    print_string0();
    printf("\n");

    for (i = 0; i < count - 2; i++)
    {
        print_string0();
        printf("\n");
    }

    printf("\n");
}

1254
static void dump_sltg_pad(int size_of_pad)
1255
{
1256 1257
    printf("pad:\n");
    dump_binary(size_of_pad);
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
    printf("\n");
}

static void dump_sltg_block_entry(int idx, const char *index)
{
    char name[32];
    short index_offset;

    sprintf(name, "Block entry %d", idx);
    print_begin_block(name);

    print_hex("len");
    index_offset = tlb_read_short();
    print_offset();
    printf("index string = %xh \"%s\"\n", index_offset, index + index_offset);
    print_short_hex("next");

    print_end_block();
}

static void dump_sltg_library_block(void)
{
    print_begin_block("Library block entry");

    print_short_hex("magic");
    print_short_hex("res02");
    print_sltg_name("name");
    print_short_hex("res06");
    print_sltg_name("helpstring");
    print_sltg_name("helpfile");
    print_hex("helpcontext");
    print_short_hex("syskind");
    print_short_hex("lcid");
    print_hex("res12");
    print_short_hex("libflags");
    dump_msft_version();
    print_guid("uuid");

    print_end_block();
}

static void skip_sltg_library_block(void)
{
    unsigned short skip;

    tlb_read_short();
    tlb_read_short();
    skip = tlb_read_short();
    if (skip != 0xffff) tlb_read(skip);
    tlb_read_short();
    skip = tlb_read_short();
    if (skip != 0xffff) tlb_read(skip);
    skip = tlb_read_short();
    if (skip != 0xffff) tlb_read(skip);
    tlb_read_int();
    tlb_read_short();
    tlb_read_short();
    tlb_read_int();
    tlb_read_short();
    tlb_read_int();
    tlb_read(sizeof(GUID));
}

static void dump_sltg_other_typeinfo(int idx, const char *hlp_strings)
{
    int hlpstr_len, saved_offset;
    char name[32];

    sprintf(name, "Other typeinfo %d", idx);
    print_begin_block(name);

    print_sltg_name("index name");
    print_sltg_name("other name");
    print_short_hex("res1a");
    print_short_hex("name offset");

    print_offset();
    hlpstr_len = tlb_read_short();
    if (hlpstr_len)
    {
        const char *str;

        saved_offset = offset;
        str = tlb_read(hlpstr_len);
        str = decode_string((const BYTE *)hlp_strings, str, hlpstr_len, NULL);
        printf("helpstring: \"%s\"\n", str);

        offset = saved_offset;
        print_offset();
        printf("helpstring encoded bits: %d bytes\n", hlpstr_len);
        dump_binary(hlpstr_len);
    }
    else
        printf("helpstring: \"\"\n");

    print_short_hex("res20");
    print_hex("helpcontext");
    print_short_hex("res26");
    print_guid("uuid");
    print_short_dec("typekind");

    print_end_block();
}

static void skip_sltg_other_typeinfo(void)
{
    unsigned short skip;

    skip = tlb_read_short();
    if (skip != 0xffff) tlb_read(skip);
    skip = tlb_read_short();
    if (skip != 0xffff) tlb_read(skip);
    tlb_read_short();
    tlb_read_short();
    skip = tlb_read_short();
    if (skip) tlb_read(skip);
    tlb_read_short();
    tlb_read_int();
    tlb_read_short();
    tlb_read(sizeof(GUID));
    tlb_read_short();
}

static void sltg_print_simple_type(short type)
{
    print_offset();
    if ((type & 0x0f00) == 0x0e00)
        printf("*");
    printf("%04x | (%d)\n", type & 0xff80, type & 0x7f);
}

static void dump_safe_array(int array_offset)
{
    int i, cDims, saved_offset = offset;

    offset = array_offset;

    print_offset();
    printf("safe array starts at %#x\n", offset);

    cDims = print_short_dec("cDims");
    print_short_hex("fFeatures");
    print_dec("cbElements");
    print_dec("cLocks");
    print_hex("pvData");

    for (i = 0; i < cDims; i++)
        dump_binary(8); /* sizeof(SAFEARRAYBOUND) */

    print_offset();
    printf("safe array ends at %#x\n", offset);
    offset = saved_offset;
}

static int sltg_print_compound_type(int vars_start_offset, int type_offset)
{
    short type, vt;
    int type_bytes, saved_offset = offset;

    offset = vars_start_offset + type_offset;
    print_offset();
    printf("type description starts at %#x\n", offset);

    for (;;)
    {
        do
        {
            type = tlb_read_short();
            vt = type & 0x7f;

            if (vt == VT_PTR)
            {
                print_offset();
                printf("%04x | VT_PTR\n", type & 0xff80);
            }
        } while (vt == VT_PTR);

        if (vt == VT_USERDEFINED)
        {
            short href = tlb_read_short();
            print_offset();
            if ((type & 0x0f00) == 0x0e00)
                printf("*");
            printf("%04x | VT_USERDEFINED (href %d)\n", type & 0xff80, href);
            break;
        }
        else if (vt == VT_CARRAY)
        {
            short off;

            off = tlb_read_short();
            print_offset();
            printf("VT_CARRAY: offset %#x (+%#x=%#x)\n",
                   off, vars_start_offset, off + vars_start_offset);
            dump_safe_array(vars_start_offset + off);

            /* type description follows */
            print_offset();
            printf("array element type:\n");
            continue;
        }
        else if (vt == VT_SAFEARRAY)
        {
            short off;

            off = tlb_read_short();
            print_offset();
            printf("VT_SAFEARRAY: offset %#x (+%#x=%#x)\n",
                   off, vars_start_offset, off + vars_start_offset);
            dump_safe_array(vars_start_offset + off);
            break;
        }
        else
        {
            sltg_print_simple_type(type);
            break;
        }
    }

    print_offset();
    printf("type description ends at %#x\n", offset);
    type_bytes =  offset - saved_offset;
    offset = saved_offset;

    return type_bytes;
}

static void dump_type(int len, const char *hlp_strings)
{
    union
    {
        struct
        {
            unsigned unknown1  : 3;
            unsigned flags     : 13;
            unsigned unknown2  : 8;
            unsigned typekind  : 8;
        } s;
        unsigned flags;
    } misc;
    int typeinfo_start_offset, extra, member_offset, href_offset, i;
1499
    int saved_offset;
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
    const void *block;
    const struct sltg_typeinfo_header *ti;
    const struct sltg_member_header *mem;
    const struct sltg_tail *tail;

    typeinfo_start_offset = offset;
    block = tlb_read(len);
    offset = typeinfo_start_offset;

    ti = block;
    mem = (const struct sltg_member_header *)((char *)block + ti->elem_table);
    tail = (const struct sltg_tail *)((char *)(mem + 1) + mem->extra);

    typeinfo_start_offset = offset;

    print_short_hex("magic");
    href_offset = tlb_read_int();
    print_offset();
    if (href_offset != -1)
        printf("href offset = %#x (+%#x=%#x)\n",
               href_offset, typeinfo_start_offset, href_offset + typeinfo_start_offset);
    else
        printf("href offset = ffffffffh\n");
    print_hex("res06");
    member_offset = tlb_read_int();
    print_offset();
    printf("member offset = %#x (+%#x=%#x)\n",
           member_offset, typeinfo_start_offset, member_offset + typeinfo_start_offset);
    print_hex("res0e");
    print_hex("version");
    print_hex("res16");
    misc.flags = print_hex("misc");
    print_offset();
    printf("misc: unknown1 %02x, flags %04x, unknown2 %02x, typekind %u (%s)\n",
           misc.s.unknown1, misc.s.flags, misc.s.unknown2, misc.s.typekind,
           misc.s.typekind < TKIND_MAX ? tkind[misc.s.typekind] : "unknown");
    print_hex("res1e");

    if (href_offset != -1)
    {
        int i, number;

        print_begin_block("href_table");

        print_short_hex("magic");
        print_hex("res02");
        print_hex("res06");
        print_hex("res0a");
        print_hex("res0e");
        print_hex("res12");
        print_hex("res16");
        print_hex("res1a");
        print_hex("res1e");
        print_hex("res22");
        print_hex("res26");
        print_hex("res2a");
        print_hex("res2e");
        print_hex("res32");
        print_hex("res36");
        print_hex("res3a");
        print_hex("res3e");
        print_short_hex("res42");
        number = print_hex("number");

        for (i = 0; i < number; i += 8)
            dump_binary(8);

        print_short_hex("res50");
        print_byte("res52");
        print_hex("res53");

        for (i = 0; i < number/8; i++)
            print_sltg_name("name");

        print_byte("resxx");

        print_end_block();
    }

    print_offset();
    printf("member_header starts at %#x, current offset = %#x\n", typeinfo_start_offset + member_offset, offset);
    member_offset = offset;
    print_short_hex("res00");
    print_short_hex("res02");
    print_byte("res04");
    extra = print_hex("extra");

    if (misc.s.typekind == TKIND_RECORD || misc.s.typekind == TKIND_ENUM)
    {
        int vars_start_offset = offset;

        for (i = 0; i < tail->cVars; i++)
        {
            char name[32];
            int saved_off;
            char magic, flags;
            short next, value;

            sprintf(name, "variable %d", i);
            print_begin_block(name);

            saved_off = offset;
            dump_binary(sizeof(struct sltg_variable));
            offset = saved_off;

            magic = print_byte("magic");
            flags = print_byte("flags");
            next = tlb_read_short();
            print_offset();
            if (next != -1)
                printf("next offset = %#x (+%#x=%#x)\n",
                       next, vars_start_offset, next + vars_start_offset);
            else
                printf("next offset = ffffh\n");
            print_short_hex("name");

            if (flags & 0x40)
                print_short_hex("dispatch");
            else if (flags & 0x10)
            {
                if (flags & 0x08)
                    print_short_hex("const value");
                else
                {
                    value = tlb_read_short();
                    print_offset();
                    printf("byte offset = %#x (+%#x=%#x)\n",
                           value, vars_start_offset, value + vars_start_offset);
                }
            }
            else
                print_short_hex("oInst");

            value = tlb_read_short();
            if (!(flags & 0x02))
            {
                print_offset();
                printf("type offset = %#x (+%#x=%#x)\n",
                       value, vars_start_offset, value + vars_start_offset);
                print_offset();
                printf("type:\n");
1641
                sltg_print_compound_type(vars_start_offset, value);
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
            }
            else
            {
                print_offset();
                printf("type:\n");
                sltg_print_simple_type(value);
            }

            print_hex("memid");
            print_short_hex("helpcontext");

            value = tlb_read_short();
            print_offset();
            if (value != -1)
            {
                const char *str;
1658
                UINT hlpstr_maxlen;
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683

                printf("helpstring offset = %#x (+%#x=%#x)\n",
                       value, vars_start_offset, value + vars_start_offset);

                saved_offset = offset;

                offset = value + vars_start_offset;

                hlpstr_maxlen = member_offset + sizeof(struct sltg_member_header) + mem->extra - offset;

                str = tlb_read(hlpstr_maxlen);
                str = decode_string((const BYTE *)hlp_strings, str, hlpstr_maxlen, &hlpstr_maxlen);
                print_offset();
                printf("helpstring: \"%s\"\n", str);

                offset = value + vars_start_offset;
                print_offset();
                printf("helpstring encoded bits: %d bytes\n", hlpstr_maxlen);
                dump_binary(hlpstr_maxlen);

                offset = saved_offset;
            }
            else
                printf("helpstring offset = ffffh\n");

1684
            if (magic & 0x20) print_short_hex("varflags");
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757

            if (next != -1)
            {
                if (offset != vars_start_offset + next)
                    dump_binary(vars_start_offset + next - offset);
            }

            print_end_block();
        }
    }
    else if (misc.s.typekind == TKIND_INTERFACE || misc.s.typekind == TKIND_COCLASS)
    {
        short next, i;
        int funcs_start_offset = offset;

        for (i = 0; i < tail->cImplTypes; i++)
        {
            char name[64];

            sprintf(name, "impl.type %d (current offset %#x)", i, offset);
            print_begin_block(name);

            print_short_hex("res00");
            next = tlb_read_short();
            print_offset();
            if (next != -1)
                printf("next offset = %#x (+%#x=%#x)\n",
                       next, funcs_start_offset, next + funcs_start_offset);
            else
                printf("next offset = ffffh\n");
            print_short_hex("res04");
            print_byte("impltypeflags");
            print_byte("res07");
            print_short_hex("res08");
            print_short_hex("ref");
            print_short_hex("res0c");
            print_short_hex("res0e");
            print_short_hex("res10");
            print_short_hex("res12");
            print_short_hex("pos in table");

            print_end_block();
        }

        for (i = 0; i < tail->cFuncs; i++)
        {
            char name[64];
            BYTE magic, flags;
            short args_off, value, n_params, j;

            sprintf(name, "function %d (current offset %#x)", i, offset);
            print_begin_block(name);

            magic = print_byte("magic");
            flags = tlb_read_byte();
            print_offset();
            printf("invoke_kind = %u\n", flags >> 4);
            next = tlb_read_short();
            print_offset();
            if (next != -1)
                printf("next offset = %#x (+%#x=%#x)\n",
                       next, funcs_start_offset, next + funcs_start_offset);
            else
                printf("next offset = ffffh\n");
            print_short_hex("name");
            print_hex("dispid");
            print_short_hex("helpcontext");

            value = tlb_read_short();
            print_offset();
            if (value != -1)
            {
                const char *str;
1758
                UINT hlpstr_maxlen;
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930

                printf("helpstring offset = %#x (+%#x=%#x)\n",
                       value, funcs_start_offset, value + funcs_start_offset);

                saved_offset = offset;

                offset = value + funcs_start_offset;

                hlpstr_maxlen = member_offset + sizeof(struct sltg_member_header) + mem->extra - offset;

                str = tlb_read(hlpstr_maxlen);
                str = decode_string((const BYTE *)hlp_strings, str, hlpstr_maxlen, &hlpstr_maxlen);
                print_offset();
                printf("helpstring: \"%s\"\n", str);

                offset = value + funcs_start_offset;
                print_offset();
                printf("helpstring encoded bits: %d bytes\n", hlpstr_maxlen);
                dump_binary(hlpstr_maxlen);

                offset = saved_offset;
            }
            else
                printf("helpstring offset = ffffh\n");

            args_off = tlb_read_short();
            print_offset();
            if (args_off != -1)
                printf("args off = %#x (+%#x=%#x)\n",
                       args_off, funcs_start_offset, args_off + funcs_start_offset);
            else
                printf("args off = ffffh\n");
            flags = tlb_read_byte();
            n_params = flags >> 3;
            print_offset();
            printf("callconv %u, cParams %u\n", flags & 0x7, n_params);

            flags = tlb_read_byte();
            print_offset();
            printf("retnextop %02x, cParamsOpt %u\n", flags, (flags & 0x7e) >> 1);

            value = print_short_hex("rettype");
            if (!(flags & 0x80))
            {
                print_offset();
                printf("rettype offset = %#x (+%#x=%#x)\n",
                       value, funcs_start_offset, value + funcs_start_offset);
                print_offset();
                printf("rettype:\n");
                sltg_print_compound_type(funcs_start_offset, value);
            }
            else
            {
                print_offset();
                printf("rettype:\n");
                sltg_print_simple_type(value);
            }

            print_short_hex("vtblpos");
            if (magic & 0x20)
                print_short_hex("funcflags");

            if (n_params)
            {
                offset = args_off + funcs_start_offset;
                print_offset();
                printf("arguments start at %#x\n", offset);
            }

            for (j = 0; j < n_params; j++)
            {
                char name[32];
                unsigned short name_offset;

                sprintf(name, "arg %d", j);
                print_begin_block(name);

                name_offset = tlb_read_short();
                print_offset();
                printf("name: %04xh\n", name_offset);

                value = tlb_read_short();
                print_offset();
                printf("type/offset %04xh\n", value);
                if (name_offset & 1) /* type follows */
                {
                    print_offset();
                    printf("type follows, using current offset for type\n");
                    offset -= 2;
                    value = offset - funcs_start_offset;
                }

                print_offset();
                printf("arg[%d] off = %#x (+%#x=%#x)\n",
                       j, value, funcs_start_offset, value + funcs_start_offset);
                print_offset();
                printf("type:\n");
                value = sltg_print_compound_type(funcs_start_offset, value);
                if (name_offset & 1)
                    offset += value;

                print_end_block();
            }

            if (n_params)
            {
                print_offset();
                printf("arguments end at %#x\n", offset);
            }

            if (next != -1)
            {
                if (offset != funcs_start_offset + next)
                    dump_binary(funcs_start_offset + next - offset);
            }

            print_end_block();
        }
    }
    else
    {
        printf("skipping %#x bytes\n", extra);
        dump_binary(extra);
    }

    if (offset < member_offset + sizeof(struct sltg_member_header) + mem->extra)
    {
        print_offset();
        printf("skipping %d bytes\n", member_offset + (int)sizeof(struct sltg_member_header) + mem->extra - offset);
        dump_binary(member_offset + sizeof(struct sltg_member_header) + mem->extra - offset);
    }

    print_offset();
    printf("dumped %d (%#x) bytes\n", offset - typeinfo_start_offset, offset - typeinfo_start_offset);
    len -= offset - typeinfo_start_offset;
    print_offset();
    printf("sltg_tail %d (%#x) bytes:\n", len, len);
    saved_offset = offset;
    dump_binary(len);
    offset = saved_offset;
    print_short_hex("cFuncs");
    print_short_hex("cVars");
    print_short_hex("cImplTypes");
    print_short_hex("res06");
    print_short_hex("funcs_off");
    print_short_hex("vars_off");
    print_short_hex("impls_off");
    print_short_hex("funcs_bytes");
    print_short_hex("vars_bytes");
    print_short_hex("impls_bytes");
    print_short_hex("tdescalias_vt");
    print_short_hex("res16");
    print_short_hex("res18");
    print_short_hex("res1a");
    print_short_hex("simple_alias");
    print_short_hex("res1e");
    print_short_hex("cbSizeInstance");
    print_short_hex("cbAlignment");
    print_short_hex("res24");
    print_short_hex("res26");
    print_short_hex("cbSizeVft");
    print_short_hex("res2a");
    print_short_hex("res2c");
    print_short_hex("res2e");
    print_short_hex("res30");
    print_short_hex("res32");
    print_short_hex("res34");
    offset = saved_offset + len;
}

static void sltg_dump(void)
{
1931
    int i, n_file_blocks, n_first_blk, size_of_index, size_of_pad;
1932 1933 1934 1935 1936
    int name_table_start, name_table_size, saved_offset;
    int libblk_start, libblk_len, hlpstr_len, len;
    const char *index, *hlp_strings;
    const struct block_entry *entry;

1937
    n_file_blocks = dump_sltg_header(&n_first_blk, &size_of_index, &size_of_pad);
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952

    saved_offset = offset;
    entry = tlb_read((n_file_blocks - 1) * sizeof(*entry));
    if (!entry) return;
    index = tlb_read(size_of_index);
    if (!index) return;
    offset = saved_offset;

    for (i = 0; i < n_file_blocks - 1; i++)
        dump_sltg_block_entry(i, index);

    saved_offset = offset;
    dump_sltg_index(n_file_blocks);
    assert(offset - saved_offset == size_of_index);

1953
    dump_sltg_pad(size_of_pad);
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124

    /* read the helpstrings for later decoding */
    saved_offset = offset;

    for (i = n_first_blk - 1; entry[i].next != 0; i = entry[i].next - 1)
        tlb_read(entry[i].len);

    libblk_start = offset;
    skip_sltg_library_block();
    tlb_read(0x40);
    typeinfo_cnt = tlb_read_short();

    for (i = 0; i < typeinfo_cnt; i++)
        skip_sltg_other_typeinfo();

    len = tlb_read_int();
    hlpstr_len = (libblk_start + len) - offset;
    hlp_strings = tlb_read(hlpstr_len);
    assert(hlp_strings != NULL);
    /* check the helpstrings header values */
    len = *(int *)(hlp_strings + 2);
    assert(hlpstr_len == len + 6);

    offset = saved_offset;

    for (i = n_first_blk - 1; entry[i].next != 0; i = entry[i].next - 1)
    {
        short magic;
        char name[32];

        saved_offset = offset;

        sprintf(name, "Block %d", i);
        print_begin_block(name);
        magic = tlb_read_short();
        assert(magic == 0x0501);
        offset -= 2;
        dump_binary(entry[i].len);
        print_end_block();

        offset = saved_offset;

        print_begin_block(name);
        dump_type(entry[i].len, hlp_strings);
        print_end_block();

        offset = saved_offset + entry[i].len;
    }

    libblk_len = entry[i].len;

    libblk_start = offset;
    dump_sltg_library_block();

    printf("skipping 0x40 bytes\n");
    dump_binary(0x40);
    printf("\n");
    typeinfo_cnt = print_short_dec("typeinfo count");
    printf("\n");

    for (i = 0; i < typeinfo_cnt; i++)
        dump_sltg_other_typeinfo(i, hlp_strings);

    len = print_hex("offset from start of library block to name table");
    printf("%#x + %#x = %#x\n", libblk_start, len, libblk_start + len);
    len = (libblk_start + len) - offset;
    printf("skipping %#x bytes (encoded/compressed helpstrings)\n", len);
    printf("max string length: %#x, strings length %#x\n", *(short *)hlp_strings, *(int *)(hlp_strings + 2));
    dump_binary(len);
    printf("\n");

    len = print_short_hex("name table jump");
    if (len == 0xffff)
    {
        printf("skipping 0x000a bytes\n");
        dump_binary(0x000a);
        printf("\n");
    }
    else if (len == 0x0200)
    {
        printf("skipping 0x002a bytes\n");
        dump_binary(0x002a);
        printf("\n");
    }
    else
    {
        printf("FIXME: please report! (%#x)\n", len);
        assert(0);
    }

    printf("skipping 0x200 bytes\n");
    dump_binary(0x200);
    printf("\n");

    name_table_size = print_hex("name table size");

    name_table_start = offset;
    printf("name table offset = %#x\n\n", offset);

    while (offset < name_table_start + name_table_size)
    {
        int aligned_len;

        dump_binary(8);
        print_string0();
        printf("\n");

        len = offset - name_table_start;
        aligned_len = (len + 0x1f) & ~0x1f;
        if (aligned_len - len < 4)
            dump_binary(aligned_len - len);
        else
            dump_binary(len & 1);
        printf("\n");
    }

    print_hex("01ffff01");
    len = print_hex("length");
    printf("skipping %#x bytes\n", len);
    dump_binary(len);
    printf("\n");

    len = (libblk_start + libblk_len) - offset;
    printf("skipping libblk remainder %#x bytes\n", len);
    dump_binary(len);
    printf("\n");

    /* FIXME: msodumper/olestream.py parses this block differently
    print_short_hex("unknown");
    print_short_hex("byte order mark");
    i = tlb_read_short();
    printf("version = %u.%u\n", i & 0xff, i >> 8);
    print_short_hex("system identifier");
    print_hex("unknown");
    printf("\n");
    */
    printf("skipping 12 bytes\n");
    dump_binary(12);
    printf("\n");

    print_guid("uuid");
    printf("\n");

    /* 0x0008,"TYPELIB",0 */
    dump_binary(12);
    printf("\n");

    printf("skipping 12 bytes\n");
    dump_binary(12);
    printf("\n");

    printf("skipping remainder 0x10 bytes\n");
    dump_binary(0x10);
    printf("\n");
}

void tlb_dump(void)
{
    const DWORD *sig = PRD(0, sizeof(DWORD));
    if (*sig == MSFT_MAGIC)
        msft_dump();
    else
        sltg_dump();
}

enum FileSig get_kind_tlb(void)
{
    const DWORD *sig = PRD(0, sizeof(DWORD));
    if (sig && (*sig == MSFT_MAGIC || *sig == SLTG_MAGIC)) return SIG_TLB;
    return SIG_UNKNOWN;
}