// File: /workspace/github_cpp_makefile_star2_O1/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 0x170a
movq %rax, 0x18(%rsp)
cmpl $0x3, %ebp
jne 0x166c
movq 0x8(%rbx), %rdx
leaq 0x4d03(%rip), %rsi      # 0x6023
leaq 0x1d0(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x1040
leaq 0x4cf1(%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 0x1673
movq 0x10(%rbx), %rdi
leaq 0x4cac(%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 0x1695
cmpl $0x55465453, 0x10(%rsp) # imm = 0x55465453
jne 0x1695
movl 0x14(%rsp), %eax
movl %eax, 0x8(%rsp)
testl %eax, %eax
setne 0x3(%rsp)
je 0x1633
leaq 0x40(%rsp), %r15
movl $0x200, %r13d           # imm = 0x200
leaq 0x240(%rsp), %rbp
movl $0x0, 0x4(%rsp)
xorl %eax, %eax
movl %eax, 0xc(%rsp)
pxor %xmm0, %xmm0
movdqa %xmm0, 0x90(%rsp)
movdqa %xmm0, 0x80(%rsp)
movdqa %xmm0, 0x70(%rsp)
movdqa %xmm0, 0x60(%rsp)
movdqa %xmm0, 0x50(%rsp)
movdqa %xmm0, 0x40(%rsp)
movl $0x0, 0xa0(%rsp)
movl $0x1, %esi
movl $0x64, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x10d0
cmpl $0x64, %eax
jne 0x15d7
movl 0x40(%rsp), %esi
movl 0x44(%rsp), %edx
leaq 0x4c1d(%rip), %rdi      # 0x6074
leaq 0x48(%rsp), %rcx
xorl %eax, %eax
callq 0x1030
movq %r15, %r12
movl 0x44(%rsp), %ebx
leaq 0x118(%rsp), %r15
movq %r15, %rdi
callq 0x5914
movl $0x64, %edx
movq %r15, %rdi
movq %r12, %r15
movq %r12, %rsi
callq 0x59b6
testl %ebx, %ebx
jle 0x14d5
cmpl %r13d, %ebx
movl $0x200, %edx            # imm = 0x200
cmovll %ebx, %edx
movl $0x1, %esi
movq %rbp, %rdi
movq %r14, %rcx
callq 0x10d0
movq %rax, %r12
testl %r12d, %r12d
jle 0x14d0
movl %r12d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leaq 0x118(%rsp), %rdi
movq %rbp, %rsi
callq 0x59b6
subl %r12d, %ebx
testl %r12d, %r12d
jg 0x148d
testl %ebx, %ebx
jne 0x15e9
pxor %xmm0, %xmm0
movdqa %xmm0, 0x100(%rsp)
movdqa %xmm0, 0xf0(%rsp)
movdqa %xmm0, 0xe0(%rsp)
movdqa %xmm0, 0xd0(%rsp)
movdqa %xmm0, 0xc0(%rsp)
movdqa %xmm0, 0xb0(%rsp)
movl $0x1, %esi
movl $0x60, %edx
leaq 0xb0(%rsp), %rdi
movq %r14, %rcx
callq 0x10d0
cmpl $0x60, %eax
jne 0x1610
movl $0x20, %edx
leaq 0x118(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x5c64
movdqa 0x20(%rsp), %xmm0
movdqa 0x30(%rsp), %xmm1
pcmpeqb 0xc0(%rsp), %xmm1
pcmpeqb 0xb0(%rsp), %xmm0
pand %xmm1, %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x1617
leaq 0x190(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movl $0x20, %edx
leaq 0xd0(%rsp), %rcx
movq 0x18(%rsp), %r8
callq 0x2b2e
testl %eax, %eax
setne %r12b
je 0x161e
leaq 0x118(%rsp), %rdi
callq 0x5966
testb %r12b, %r12b
movl 0xc(%rsp), %eax
je 0x163b
incl %eax
cmpl 0x8(%rsp), %eax
setb 0x3(%rsp)
jne 0x13ed
jmp 0x163b
callq 0x1178
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
xorl %r12d, %r12d
jmp 0x15bb
movq 0x79f0(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
xorl %r12d, %r12d
leaq 0x4a99(%rip), %rsi      # 0x6096
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
jmp 0x15ae
callq 0x119a
jmp 0x1623
callq 0x11bc
jmp 0x1623
callq 0x11de
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
xorl %r12d, %r12d
jmp 0x15ae
movl $0x0, 0x4(%rsp)
testb $0x1, 0x3(%rsp)
jne 0x1656
movq %r14, %rdi
callq 0x1080
leaq 0x4ab9(%rip), %rdi      # 0x610a
callq 0x10e0
movl 0x4(%rsp), %eax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1140
jmp 0x169a
movq 0x7966(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x49a9(%rip), %rsi      # 0x602d
leaq 0x1d0(%rsp), %rdx
xorl %eax, %eax
callq 0x10b0
jmp 0x169a
callq 0x115c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x165a
jmp 0x16a7
jmp 0x16a7
jmp 0x16a7
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x5966
movq %rbx, %rdi
callq 0x1100
nop