// File: /workspace/github_c_makefile_star2_O1/mikeakohn[P]print_pe/build/../src/vb.c
// Function: vb_info_print
int vb_info_print(
  FILE *in,
  struct optional_header_t *optional_header,
  struct section_header_t *section_header)
{
  struct vb_header_t vb_header;
  struct com_reg_data_t com_reg_data;
  struct reg_info_t reg_info;
  int marker,ptr;
  int t;
  int addr;

  marker = ftell(in);
  fseek(in, (optional_header->AddressOfEntryPoint - section_header->VirtualAddress) + section_header->PointerToRawData, SEEK_SET);

  ptr = getc(in);
  /* Compiled VB executables / DLLs have a couple of entry point signitures. */
  /* 0x68, 0x90, 0x5A */
  switch (ptr)
  {
    case 0x5a: /* 0x5A = POP EDX, this means the file might be a VB DLL or OCX. */
	    /* The pointer to the VB5! data stuff is in the first exported function. Usually DllCanUnloadNow()
	     * Once we get to that address it turns into case 0x68 again. */
	    /* Get the address to the export directory */
	    addr = (optional_header->directory_entry[0].virtual_address - section_header->VirtualAddress) + section_header->PointerToRawData;
	    /* ExportDirectory->AddressOfFunctions = addr+28; */
	    addr = get_ptr(in, get_ptr(in,(addr + 28)));
	    fseek(in, addr + 2, SEEK_SET); /* read past 0x58, 0x68 */
	    ptr = read_uint16(in);
	    break;
    case 0x90: /* 0x90 = NOP, then it's case 0x68 */
	    getc(in); /* read the NOP then fall through to case 0x68 */
    case 0x68: /* 0x68 = PUSH (word) */
	    ptr = read_uint16(in);
	    break;
    default:
	    fseek(in, marker, SEEK_SET);
	    return -2;
	    break;
  }

  if (section_header->VirtualAddress <= ptr &&
      ptr < section_header->VirtualAddress + section_header->SizeOfRawData)
  {
    fseek(in, (ptr - section_header->VirtualAddress) + section_header->PointerToRawData, SEEK_SET);
  }
    else
  {
    fseek(in, marker, SEEK_SET);
    return -3;
  }

  read_chars(in, vb_header.szVbMagic, 4);

  if (strcmp(vb_header.szVbMagic, "VB5!") != 0)
  {
    fseek(in, marker, SEEK_SET);
    return -1;
  }

  vb_header.wRuntimeBuild = read_uint16(in);
  read_chars(in, vb_header.szLangDll, 14);
  read_chars(in, vb_header.szSecLangDll, 14);
  vb_header.wRuntimeRevision = read_uint16(in);
  vb_header.dwLCID = read_uint32(in);
  vb_header.dwSecLCID = read_uint32(in);
  vb_header.lpSubMain = read_uint32(in);
  vb_header.lpProjectData = read_uint32(in);
  vb_header.fMdlIntCtls = read_uint32(in);
  vb_header.fMdlIntCtls2 = read_uint32(in);
  vb_header.dwThreadFlags = read_uint32(in);
  vb_header.dwThreadCount = read_uint32(in);
  vb_header.wFormCount = read_uint16(in);
  vb_header.wExternalCount = read_uint16(in);
  vb_header.dwThunkCount = read_uint32(in);
  vb_header.lpGuiTable = read_uint32(in);
  vb_header.lpExternalTable = read_uint32(in);
  vb_header.lpComRegisterData = read_uint32(in);
  vb_header.bSzProjectDescription = read_uint32(in);
  vb_header.bSzProjectExeName = read_uint32(in);
  vb_header.bSzProjectHelpFile = read_uint32(in);
  vb_header.bSzProjectName = read_uint32(in);

  printf("  -- VB Header --\n");
  printf("         szVbMagic: %s\n", vb_header.szVbMagic);
  printf("     wRuntimeBuild: %d\n", vb_header.wRuntimeBuild);
  printf("         szLangDll: %s\n", vb_header.szLangDll);
  printf("      szSecLangDll: %s\n", vb_header.szSecLangDll);
  printf("  wRuntimeRevision: %d\n", vb_header.wRuntimeRevision);
  printf("            dwLCID: %d\n", vb_header.dwLCID);
  printf("         dwSecLCID: %d\n", vb_header.dwSecLCID);
  printf("         lpSubMain: %d\n", vb_header.lpSubMain);
  printf("     lpProjectData: %d\n", vb_header.lpProjectData);
  printf("       fMdlIntCtls: 0x%08x\n", vb_header.fMdlIntCtls);
  printf("      fMdlIntCtls2: 0x%08x\n", vb_header.fMdlIntCtls2);
  printf("     dwThreadFlags: %d", vb_header.dwThreadFlags);
  printf(" (");

  if ((vb_header.dwThreadFlags & 1) == 1) printf(" ApartmentModel");
  if ((vb_header.dwThreadFlags & 2) == 2) printf(" RequireLicense");
  if ((vb_header.dwThreadFlags & 4) == 4) printf(" Unattended");
  if ((vb_header.dwThreadFlags & 8) == 8) printf(" SingleThreaded");
  if ((vb_header.dwThreadFlags & 16) == 16) printf(" Retained");

  printf(" )\n");
  printf("     dwThreadCount: %d\n", vb_header.dwThreadCount);
  printf("        wFormCount: %d\n", vb_header.wFormCount);
  printf("    wExternalCount: %d\n", vb_header.wExternalCount);
  printf("      dwThunkCount: %d\n", vb_header.dwThunkCount);
  printf("        lpGuiTable: %d\n", vb_header.lpGuiTable);
  printf("   lpExternalTable: %d\n", vb_header.lpExternalTable);
  printf(" lpComRegisterData: %d\n", vb_header.lpComRegisterData);
  printf("    bSzProjectDesc: %d (", vb_header.bSzProjectDescription);
  print_string(in, ((vb_header.bSzProjectDescription+ptr)-section_header->VirtualAddress)+section_header->PointerToRawData);
  printf(")\n");
  printf(" bSzProjectExeName: %d (",vb_header.bSzProjectExeName);
  print_string(in, ((vb_header.bSzProjectExeName + ptr) - section_header->VirtualAddress) + section_header->PointerToRawData);
  printf(")\n");
  printf("bSzProjectHelpFile: %d (",vb_header.bSzProjectHelpFile);
  print_string(in, ((vb_header.bSzProjectHelpFile + ptr) - section_header->VirtualAddress) + section_header->PointerToRawData);
  printf(")\n");
  printf("    bSzProjectName: %d (",vb_header.bSzProjectName);
  print_string(in, ((vb_header.bSzProjectName + ptr) - section_header->VirtualAddress) + section_header->PointerToRawData);
  printf(")\n");
  printf("\n");

  if (vb_header.lpComRegisterData != 0)
  {
    com_reg_data_read(&com_reg_data, in, vb_header.lpComRegisterData - optional_header->ImageBase);

    com_reg_data_print(&com_reg_data);

    if (com_reg_data.bRegInfo != 0)
    {
      t = com_reg_data.bRegInfo;

      while (1)
      {
        reg_info_read(&reg_info, in, vb_header.lpComRegisterData - optional_header->ImageBase + t);

        get_string(in, (char *)reg_info.szObjectName, vb_header.lpComRegisterData - optional_header->ImageBase + reg_info.bObjectName);

        reg_info_print(&reg_info);

        if (reg_info.bNextObject == 0) { break; }

        t = reg_info.bNextObject;
      }
    }
  }

  fseek(in, marker, SEEK_SET);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4e8, %rsp            # imm = 0x4E8
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbx
callq 0x30f0
movq %rax, %r14
movl 0x10(%r15), %esi
subl 0x10(%r12), %esi
addl 0x18(%r12), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3100
movq %rbx, %rdi
callq 0x3150
cmpl $0x90, %eax
je 0x6fed
cmpl $0x68, %eax
je 0x6ff5
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
cmpl $0x5a, %eax
jne 0x74ca
movl 0x7c(%r15), %eax
movl 0x18(%r12), %ecx
subl 0x10(%r12), %eax
leal (%rax,%rcx), %esi
addl $0x1c, %esi
movq %rbx, %rdi
callq 0x53e0
movq %rbx, %rdi
movl %eax, %esi
callq 0x53e0
addl $0x2, %eax
movslq %eax, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3100
jmp 0x6ff5
movq %rbx, %rdi
callq 0x3150
movq %rbx, %rdi
callq 0x520c
movl %eax, %ebp
movl 0x10(%r12), %eax
movl $0xfffffffd, %r13d      # imm = 0xFFFFFFFD
movl %ebp, %esi
subl %eax, %esi
jb 0x74ca
addl 0x14(%r12), %eax
cmpl %eax, %ebp
jae 0x74ca
addl 0x18(%r12), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3100
movq %r14, (%rsp)
leaq 0x8(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
movl $0x4, %edx
callq 0x5232
movl $0x21354256, %eax       # imm = 0x21354256
xorl (%r14), %eax
movzbl 0x4(%r14), %ecx
movq (%rsp), %r14
orl %eax, %ecx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jne 0x74ca
movq %rbx, %rdi
callq 0x520c
leaq 0x10(%rsp), %rsi
movw %ax, -0x2(%rsi)
movq %rbx, %rdi
movl $0xe, %edx
callq 0x5232
leaq 0x1f(%rsp), %r13
movq %rbx, %rdi
movq %r13, %rsi
movl $0xe, %edx
callq 0x5232
movq %rbx, %rdi
callq 0x520c
movw %ax, 0xf(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x11(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x15(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x19(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x1d(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x21(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x25(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x29(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x2d(%r13)
movq %rbx, %rdi
callq 0x520c
movw %ax, 0x31(%r13)
movq %rbx, %rdi
callq 0x520c
movw %ax, 0x33(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x35(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x39(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x3d(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x41(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x45(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x49(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x4d(%r13)
movq %rbx, %rdi
callq 0x51ca
movl %eax, 0x51(%r13)
leaq 0x3c5a(%rip), %rdi      # 0xade3
callq 0x3060
leaq 0x39d5(%rip), %rdi      # 0xab6a
leaq 0x8(%rsp), %r14
movq %r14, %rsi
xorl %eax, %eax
callq 0x3090
movzwl 0x6(%r14), %esi
leaq 0x39d2(%rip), %rdi      # 0xab82
xorl %eax, %eax
callq 0x3090
leaq 0x39dc(%rip), %rdi      # 0xab9a
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x3090
leaq 0x39e1(%rip), %rdi      # 0xabb2
movq %r13, %rsi
xorl %eax, %eax
callq 0x3090
movzwl 0x26(%r14), %esi
leaq 0x39e3(%rip), %rdi      # 0xabca
xorl %eax, %eax
callq 0x3090
movl 0x28(%r14), %esi
leaq 0x39e9(%rip), %rdi      # 0xabe2
xorl %eax, %eax
callq 0x3090
movl 0x2c(%r14), %esi
leaq 0x39ef(%rip), %rdi      # 0xabfa
xorl %eax, %eax
callq 0x3090
movl 0x30(%r14), %esi
leaq 0x39f5(%rip), %rdi      # 0xac12
xorl %eax, %eax
callq 0x3090
movl 0x34(%r14), %esi
leaq 0x39fb(%rip), %rdi      # 0xac2a
xorl %eax, %eax
callq 0x3090
movl 0x38(%r14), %esi
leaq 0x3a01(%rip), %rdi      # 0xac42
xorl %eax, %eax
callq 0x3090
movl 0x3c(%r14), %esi
leaq 0x3a0b(%rip), %rdi      # 0xac5e
xorl %eax, %eax
callq 0x3090
movl 0x40(%r14), %esi
leaq 0x3a15(%rip), %rdi      # 0xac7a
xorl %eax, %eax
callq 0x3090
leaq 0x3b22(%rip), %rdi      # 0xad95
xorl %eax, %eax
callq 0x3090
testb $0x1, 0x40(%r14)
je 0x728f
leaq 0x3a09(%rip), %rdi      # 0xac91
xorl %eax, %eax
callq 0x3090
testb $0x2, 0x48(%rsp)
je 0x72a4
leaq 0x3a04(%rip), %rdi      # 0xaca1
xorl %eax, %eax
callq 0x3090
testb $0x4, 0x48(%rsp)
je 0x72b9
leaq 0x39ff(%rip), %rdi      # 0xacb1
xorl %eax, %eax
callq 0x3090
testb $0x8, 0x48(%rsp)
je 0x72ce
leaq 0x39f6(%rip), %rdi      # 0xacbd
xorl %eax, %eax
callq 0x3090
testb $0x10, 0x48(%rsp)
je 0x72e3
leaq 0x39f1(%rip), %rdi      # 0xaccd
xorl %eax, %eax
callq 0x3090
leaq 0x3b0b(%rip), %rdi      # 0xadf5
callq 0x3060
movl 0x4c(%rsp), %esi
leaq 0x39dd(%rip), %rdi      # 0xacd7
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x3090
movzwl 0x50(%rsp), %esi
leaq 0x39df(%rip), %rdi      # 0xacef
xorl %eax, %eax
callq 0x3090
movzwl 0x52(%rsp), %esi
leaq 0x39e4(%rip), %rdi      # 0xad07
xorl %eax, %eax
callq 0x3090
movl 0x54(%rsp), %esi
leaq 0x39ea(%rip), %rdi      # 0xad1f
xorl %eax, %eax
callq 0x3090
movl 0x58(%rsp), %esi
leaq 0x39f0(%rip), %rdi      # 0xad37
xorl %eax, %eax
callq 0x3090
movl 0x5c(%rsp), %esi
leaq 0x39f6(%rip), %rdi      # 0xad4f
xorl %eax, %eax
callq 0x3090
movl 0x60(%rsp), %esi
leaq 0x39fc(%rip), %rdi      # 0xad67
xorl %eax, %eax
callq 0x3090
movl 0x64(%rsp), %esi
leaq 0x3a02(%rip), %rdi      # 0xad7f
xorl %eax, %eax
callq 0x3090
movl 0x64(%rsp), %esi
addl %ebp, %esi
subl 0x10(%r12), %esi
addl 0x18(%r12), %esi
movq %rbx, %rdi
callq 0x5313
leaq 0x3a53(%rip), %r14      # 0xadf6
movq %r14, %rdi
callq 0x3060
movl 0x68(%rsp), %esi
leaq 0x39e2(%rip), %rdi      # 0xad98
xorl %eax, %eax
callq 0x3090
movl 0x68(%rsp), %esi
addl %ebp, %esi
subl 0x10(%r12), %esi
addl 0x18(%r12), %esi
movq %rbx, %rdi
callq 0x5313
movq %r14, %rdi
callq 0x3060
movl 0x6c(%rsp), %esi
leaq 0x39c9(%rip), %rdi      # 0xadb1
xorl %eax, %eax
callq 0x3090
movl 0x6c(%rsp), %esi
addl %ebp, %esi
subl 0x10(%r12), %esi
addl 0x18(%r12), %esi
movq %rbx, %rdi
callq 0x5313
movq %r14, %rdi
callq 0x3060
movl 0x70(%rsp), %esi
leaq 0x39b0(%rip), %rdi      # 0xadca
xorl %eax, %eax
callq 0x3090
addl 0x70(%rsp), %ebp
subl 0x10(%r12), %ebp
addl 0x18(%r12), %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x5313
movq %r14, %rdi
callq 0x3060
movl $0xa, %edi
callq 0x3030
movl 0x60(%rsp), %edx
testl %edx, %edx
je 0x74c6
subl 0x20(%r15), %edx
leaq 0x74(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3e3e
movq %r14, %rdi
callq 0x3eed
movl (%r14), %edx
testl %edx, %edx
je 0x74c6
leaq 0xe8(%rsp), %r14
leaq 0xa0(%rsp), %r12
addl 0x60(%rsp), %edx
subl 0x20(%r15), %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3bd8
movl 0x60(%rsp), %edx
subl 0x20(%r15), %edx
addl 0xa4(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x536d
movq %r12, %rdi
callq 0x3ce8
movl 0xa0(%rsp), %edx
testl %edx, %edx
jne 0x7486
movq (%rsp), %r14
movslq %r14d, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3100
movl %r13d, %eax
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq