// File: /workspace/github_cpp_makefile_star2_O2/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 0x169d
movq %rax, 0x28(%rsp)
cmpl $0x3, %ebp
jne 0x15b6
movq 0x8(%rbx), %rdx
leaq 0x3da3(%rip), %rsi      # 0x5023
leaq 0x160(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x1040
leaq 0x3d91(%rip), %rsi      # 0x502a
movq %r14, %rdi
callq 0x10d0
movq %rax, %r14
leaq 0x120(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x40
popq %rdx
movq %rax, %rcx
callq 0x1100
movq %rax, %r15
movq %r14, %rdi
callq 0x10a0
cmpl $0x40, %r15d
jne 0x15d7
movq 0x10(%rbx), %rdi
leaq 0x3d50(%rip), %rsi      # 0x502a
callq 0x10d0
leaq 0x20(%rsp), %rdi
andq $0x0, (%rdi)
pushq $0x1
popq %rsi
pushq $0x8
popq %rdx
movq %rax, 0x10(%rsp)
movq %rax, %rcx
callq 0x1100
cmpl $0x8, %eax
jne 0x15fc
cmpl $0x55465453, 0x20(%rsp) # imm = 0x55465453
jne 0x15fc
movl 0x24(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x30(%rsp), %r14
pushq $0x1
popq %rbp
pushq $0x64
popq %r12
leaq 0x1c8(%rsp), %r15
leaq 0x240(%rsp), %rbx
xorl %eax, %eax
movl $0x0, 0xc(%rsp)
cmpl 0x18(%rsp), %eax
je 0x1588
movl %eax, 0x1c(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
andl $0x0, 0x90(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
callq 0x1100
cmpl $0x64, %eax
jne 0x1611
movl 0x30(%rsp), %esi
movl 0x34(%rsp), %edx
leaq 0x3ccf(%rip), %rdi      # 0x5074
leaq 0x38(%rsp), %rcx
xorl %eax, %eax
callq 0x1030
movq %r12, %rbp
movl 0x34(%rsp), %r12d
movq %r15, %rdi
callq 0x47e4
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x4850
pushq $0x1
popq %rbp
testl %r12d, %r12d
jle 0x141d
movl $0x200, %eax            # imm = 0x200
cmpl %eax, %r12d
movl $0x200, %edx            # imm = 0x200
cmovbl %r12d, %edx
movq %rbx, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rcx
callq 0x1100
movq %rax, %r13
testl %r13d, %r13d
jle 0x14fc
movl %r13d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4850
subl %r13d, %r12d
jmp 0x13d2
jne 0x14fc
xorps %xmm0, %xmm0
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
leaq 0xa0(%rsp), %rdi
movq %rbp, %rsi
pushq $0x60
popq %rdx
movq 0x10(%rsp), %rcx
callq 0x1100
cmpl $0x60, %eax
jne 0x151c
movq %r15, %rdi
leaq 0x100(%rsp), %r13
movq %r13, %rsi
pushq $0x20
popq %r14
movq %r14, %rdx
callq 0x4b1c
pushq $0x64
popq %r12
movq %r13, %rdi
leaq 0xa0(%rsp), %rsi
movq %r14, %rdx
callq 0x10c0
testl %eax, %eax
jne 0x1548
leaq 0x120(%rsp), %rdi
movq %r13, %rsi
pushq $0x20
popq %rdx
leaq 0xc0(%rsp), %rcx
movq 0x28(%rsp), %r8
callq 0x2704
movb $0x1, %r13b
testl %eax, %eax
je 0x155d
movq %r15, %rdi
callq 0x4814
movl 0x1c(%rsp), %eax
incl %eax
testb %r13b, %r13b
leaq 0x30(%rsp), %r14
jne 0x1340
jmp 0x159e
movq 0x6add(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
xorl %r13d, %r13d
leaq 0x3b86(%rip), %rsi      # 0x5096
movl %r12d, %edx
xorl %eax, %eax
callq 0x10e0
jmp 0x153b
movq 0x6abd(%rip), %rax      # 0x7fe0
movq (%rax), %rcx
leaq 0x3ba2(%rip), %rdi      # 0x50cf
pushq $0x16
popq %rsi
movq %rbp, %rdx
callq 0x1130
xorl %r13d, %r13d
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
pushq $0x64
popq %r12
jmp 0x14db
movq 0x6a91(%rip), %rax      # 0x7fe0
movq (%rax), %rcx
leaq 0x3b8d(%rip), %rdi      # 0x50e6
pushq $0xf
jmp 0x1570
movq 0x6a7c(%rip), %rax      # 0x7fe0
movq (%rax), %rcx
leaq 0x3b88(%rip), %rdi      # 0x50f6
pushq $0x13
popq %rsi
movq %rbp, %rdx
callq 0x1130
xorl %r13d, %r13d
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
jmp 0x14db
movq 0x10(%rsp), %rdi
callq 0x10a0
leaq 0x3b71(%rip), %rdi      # 0x510a
callq 0x1110
movl 0xc(%rsp), %ebx
movl %ebx, %eax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rbx
movq 0x6a20(%rip), %rax      # 0x7fe0
movq (%rax), %rcx
leaq 0x3a3a(%rip), %rdi      # 0x5004
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x1130
jmp 0x15a2
movq 0x6a02(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x3a45(%rip), %rsi      # 0x502d
leaq 0x160(%rsp), %rdx
xorl %eax, %eax
callq 0x10e0
pushq $-0x1
popq %rbx
jmp 0x15a2
movq 0x69dd(%rip), %rax      # 0x7fe0
movq (%rax), %rcx
leaq 0x3a33(%rip), %rdi      # 0x5040
pushq $0x1c
jmp 0x1624
movq 0x69c8(%rip), %rax      # 0x7fe0
movq (%rax), %rcx
leaq 0x3a3b(%rip), %rdi      # 0x505d
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x1130
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
jmp 0x159e
jmp 0x163f
jmp 0x163f
jmp 0x163f
movq %rax, %rbx
leaq 0x1c8(%rsp), %rdi
callq 0x4814
movq %rbx, %rdi
callq 0x1120
nop