// File: /workspace/github_cpp_makefile_star2_O3/simap[P]ecctools/verifier.cpp
// Function: main
int main(int argc, char ** argv) {
  const struct uECC_Curve_t * curve = uECC_secp256k1();

  if (argc != 3) {
    fprintf(stderr, "Need a public key and archive\n");
    return -1;
  }

  // uint8_t private1[32];
  uint8_t public1[64];
  char tmp[100];
  FILE *fp, *fpin;


  sprintf(tmp, "%s.pub", argv[1]);
  fp = fopen(tmp, "rb");
  int nread = fread(public1, 1, sizeof(public1), fp);
  fclose(fp);
  if (nread != sizeof(public1)) {
    fprintf(stderr, "can't read key %s\n", tmp);
    return -1;
  }

  fpin = fopen(argv[2], "rb");
  FUHeader header = {0};
  nread = fread(&header, 1, sizeof(FUHeader), fpin);
  if (nread != sizeof(FUHeader) || strncmp("STFU", header.magic, 4) != 0) {
    fprintf(stderr, "can't read magic and header\n");
    return -1;
  }

  for (int fileNum = 0; fileNum < header.numFiles; fileNum++) {
    FUFileHeader header = {0};
    nread = fread(&header, 1, sizeof(header), fpin);
    if (nread != sizeof(header)) {
      fprintf(stderr, "unable to read header\n");
      return -1;
    }

    printf("Scanning type:%d size:%d name:%s\n", header.type, header.size, header.name);

    int bytesRemaining = header.size;

    //hash it
    uint8_t buf[512];
    SHA256 sha;
    sha.update(&header, sizeof(header));
    while (bytesRemaining > 0) {
      int nread = fread(buf, 1, bytesRemaining < 512 ? bytesRemaining : 512, fpin);
      if (nread > 0) {
        bytesRemaining -= nread;
        sha.update(buf, nread);
      } else {
        break;
      }
    }
    if (bytesRemaining) {
      fprintf(stderr, "had trouble reading file for hash, %d bytes more wanted\n", bytesRemaining);
      return -1;
    }

    FUFileFooter footer = {0};
    nread = fread(&footer, 1, sizeof(footer), fpin);
    if (nread != sizeof(footer)) {
      fprintf(stderr, "unable to read footer\n");
      return -1;
    }

    uint8_t hash[32];
    sha.finalize(hash, 32);
    if (memcmp(hash, footer.hash, 32) != 0) {
      fprintf(stderr, "hash mismatch!\n");
      return -1; 
    }

    int signatureOK = uECC_verify(public1, hash, 32, footer.signature, curve);
    if (!signatureOK) {
      fprintf(stderr, "signature invalid!\n");
      return -1;
    }
  }
  fclose(fpin);
  printf("signed transfer file update verification complete!\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movq %rsi, %rbx
movl %edi, %ebp
callq 0x16c6
movq %rax, 0x18(%rsp)
cmpl $0x3, %ebp
jne 0x15e6
movq 0x8(%rbx), %rdx
leaq 0x4d23(%rip), %rsi      # 0x6023
leaq 0x1d0(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x1040
leaq 0x4d11(%rip), %rsi      # 0x602a
movq %r14, %rdi
callq 0x10a0
movq %rax, %r14
leaq 0x190(%rsp), %rdi
movl $0x1, %esi
movl $0x40, %edx
movq %rax, %rcx
callq 0x10d0
movq %rax, %r15
movq %r14, %rdi
callq 0x1080
cmpl $0x40, %r15d
jne 0x15ed
movq 0x10(%rbx), %rdi
leaq 0x4ccc(%rip), %rsi      # 0x602a
callq 0x10a0
movq %rax, %r14
leaq 0x10(%rsp), %rdi
movq $0x0, (%rdi)
movl $0x1, %esi
movl $0x8, %edx
movq %rax, %rcx
callq 0x10d0
cmpl $0x8, %eax
jne 0x160f
cmpl $0x55465453, 0x10(%rsp) # imm = 0x55465453
jne 0x160f
movl 0x14(%rsp), %eax
movl %eax, 0x8(%rsp)
testl %eax, %eax
je 0x1590
leaq 0x20(%rsp), %rbp
leaq 0x118(%rsp), %r12
movl $0x200, %r13d           # imm = 0x200
leaq 0x240(%rsp), %r15
xorl %ebx, %ebx
movl %ebx, 0xc(%rsp)
pxor %xmm0, %xmm0
movdqa %xmm0, 0x70(%rsp)
movdqa %xmm0, 0x60(%rsp)
movdqa %xmm0, 0x50(%rsp)
movdqa %xmm0, 0x40(%rsp)
movdqa %xmm0, 0x30(%rsp)
movdqa %xmm0, 0x20(%rsp)
movl $0x0, 0x80(%rsp)
movl $0x1, %esi
movl $0x64, %edx
movq %rbp, %rdi
movq %r14, %rcx
callq 0x10d0
cmpl $0x64, %eax
jne 0x1616
movl 0x20(%rsp), %esi
movl 0x24(%rsp), %edx
leaq 0x4c48(%rip), %rdi      # 0x6074
leaq 0x28(%rsp), %rcx
xorl %eax, %eax
callq 0x1030
movl 0x24(%rsp), %ebx
movq %r12, %rdi
callq 0x572c
movl $0x64, %edx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x57c4
testl %ebx, %ebx
jle 0x1495
cmpl %r13d, %ebx
movl $0x200, %edx            # imm = 0x200
cmovbl %ebx, %edx
movl $0x1, %esi
movq %r15, %rdi
movq %r14, %rcx
callq 0x10d0
movq %rax, %rbp
testl %ebp, %ebp
jle 0x15a8
movl %ebp, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %r12, %rdi
movq %r15, %rsi
callq 0x57c4
subl %ebp, %ebx
jg 0x1458
testl %ebx, %ebx
jne 0x15a8
pxor %xmm0, %xmm0
movdqa %xmm0, 0xe0(%rsp)
movdqa %xmm0, 0xd0(%rsp)
movdqa %xmm0, 0xc0(%rsp)
movdqa %xmm0, 0xb0(%rsp)
movdqa %xmm0, 0xa0(%rsp)
movdqa %xmm0, 0x90(%rsp)
movl $0x1, %esi
movl $0x60, %edx
leaq 0x90(%rsp), %rdi
movq %r14, %rcx
callq 0x10d0
cmpl $0x60, %eax
jne 0x1622
movl $0x20, %edx
movq %r12, %rdi
leaq 0xf0(%rsp), %rbx
movq %rbx, %rsi
callq 0x5aa0
movdqa 0xf0(%rsp), %xmm0
movdqa 0x100(%rsp), %xmm1
pcmpeqb 0xa0(%rsp), %xmm1
pcmpeqb 0x90(%rsp), %xmm0
pand %xmm1, %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
leaq 0x20(%rsp), %rbp
jne 0x1647
leaq 0x190(%rsp), %rdi
movq %rbx, %rsi
movl $0x20, %edx
leaq 0xb0(%rsp), %rcx
movq 0x18(%rsp), %r8
callq 0x2b13
testl %eax, %eax
movl 0xc(%rsp), %ebx
je 0x1651
movq %r12, %rdi
callq 0x577e
incl %ebx
cmpl 0x8(%rsp), %ebx
jne 0x13c8
movq %r14, %rdi
callq 0x1080
leaq 0x4b6b(%rip), %rdi      # 0x610a
callq 0x10e0
xorl %eax, %eax
jmp 0x15d4
movq 0x7a31(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x4add(%rip), %rsi      # 0x6096
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
leaq 0x118(%rsp), %rdi
callq 0x577e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1140
jmp 0x15cf
movq 0x79ec(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x4a2f(%rip), %rsi      # 0x602d
leaq 0x1d0(%rsp), %rdx
xorl %eax, %eax
callq 0x10b0
jmp 0x15cf
callq 0x115c
jmp 0x15cf
leaq 0x20(%rsp), %rdi
callq 0x1178
jmp 0x15cf
movq 0x79b7(%rip), %rax      # 0x8fe0
movq (%rax), %rcx
leaq 0x4a9c(%rip), %rdi      # 0x60cf
movl $0x16, %esi
movl $0x1, %edx
callq 0x1110
jmp 0x15c2
callq 0x119a
jmp 0x15c2
callq 0x11bc
jmp 0x15c2
jmp 0x1661
jmp 0x1661
jmp 0x1661
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x577e
movq %rbx, %rdi
callq 0x1100
nopl (%rax)