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

  if (argc < 4) {
    fprintf(stderr, "Need a key, at least one input file, and an output file\n");
    return -1;
  }

  uint32_t numFiles = argc - 3;
  char ** fileNames = &argv[2];

  //check all files first
  for (int fileNum = 0; fileNum < numFiles; fileNum++) {
    int filesize = fsize(fileNames[fileNum]);
    if (filesize < 1) {
      fprintf(stderr, "Can't stat file: %s\n", fileNames[fileNum]);
      return -1;      
    }
  }

  uint8_t privateKey[32];
  char tmp[200];
  FILE * fp, *fpout;
  int nread;

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

  fpout = fopen(argv[argc-1], "wb");
  fwrite("STFU", 4, 1, fpout);
  fwrite(&numFiles, sizeof(numFiles), 1, fpout);

  for (int fileNum = 0; fileNum < numFiles; fileNum++) {
    char * fileName = fileNames[fileNum];
    int bytesRemaining = fsize(fileName);
    fp = fopen(fileName, "rb");

    //do header
    FUFileHeader header = {0};
    FUFileFooter footer = {0};
    header.type = 1;
    header.size = bytesRemaining;
    {
      char * fileBaseName = strdup(fileName);
      strncpy(header.name, basename(fileBaseName), sizeof(header.name)-1);
      free(fileBaseName);
    }
    fwrite(&header, sizeof(header), 1, fpout);

    //hash header + file, and copy to archive
    uint8_t buf[512];
    SHA256 sha;
    sha.update(&header, sizeof(header));
    while (bytesRemaining > 0) {
      int nread = fread(buf, 1, 512, fp);
      if (nread > 0) {
        bytesRemaining -= nread;
        fwrite(buf, 1, nread, fpout);
        sha.update(buf, nread);
      } else {
        break;
      }
    }
    if (bytesRemaining) {
      fprintf(stderr, "had trouble reading file for hash %s\n", fileName);
      return -1;
    }
    fclose(fp);

    //hash, sign, and write out footer
    sha.finalize(footer.hash, 32);
    uECC_sign(privateKey, footer.hash, 32, footer.signature, curve);
    fwrite(&footer, sizeof(footer), 1, fpout);

  }
  fclose(fpout);
  printf("signed transfer file update archive complete!\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
movq %rsi, %rbx
movl %edi, %ebp
callq 0x176a
movq %rax, 0x30(%rsp)
cmpl $0x3, %ebp
jle 0x16d5
movq %rbx, 0x18(%rsp)
leaq 0x10(%rbx), %r15
movl %ebp, 0x10(%rsp)
addl $-0x3, %ebp
movl %ebp, 0x24(%rsp)
setne %r12b
movq %r15, 0x38(%rsp)
je 0x1393
movl %ebp, %ebx
movq %rbx, %r13
negq %r13
movl $0x0, 0xc(%rsp)
movl $0x1, %ebp
movq (%r15), %rdi
leaq 0x280(%rsp), %rsi
callq 0x1080
testl %eax, %eax
sete %al
movq 0x2b0(%rsp), %rcx
testl %ecx, %ecx
setg %r14b
andb %al, %r14b
je 0x1381
testb %r14b, %r14b
je 0x139b
cmpq %rbx, %rbp
setb %r12b
addq $0x8, %r15
leaq 0x1(,%rbp), %rax
addq %r13, %rax
incq %rbp
cmpq $0x1, %rax
jne 0x1333
jmp 0x139b
movq %r15, %rdi
callq 0x1180
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
jmp 0x135b
movl $0x0, 0xc(%rsp)
testb $0x1, %r12b
jne 0x16bf
movq 0x18(%rsp), %rbx
movq 0x8(%rbx), %rdx
leaq 0x4c9d(%rip), %rsi      # 0x6052
leaq 0x1b0(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x1030
leaq 0x4c8b(%rip), %rsi      # 0x6059
movq %r14, %rdi
callq 0x10b0
movq %rax, %r14
leaq 0x110(%rsp), %rdi
movl $0x1, %esi
movl $0x20, %edx
movq %rax, %rcx
callq 0x10f0
movq %rax, %r15
movq %r14, %rdi
callq 0x1090
cmpl $0x20, %r15d
jne 0x16dc
movl 0x10(%rsp), %eax
movq -0x8(%rbx,%rax,8), %rdi
leaq 0x4c57(%rip), %rsi      # 0x606f
callq 0x10b0
movq %rax, %r15
leaq 0x4c4b(%rip), %rdi      # 0x6072
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x1150
leaq 0x24(%rsp), %rbx
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x1150
movl (%rbx), %eax
movq %rax, 0x10(%rsp)
testq %rax, %rax
setne %al
je 0x16a7
leaq 0x280(%rsp), %rbp
xorl %ecx, %ecx
movb %al, 0xb(%rsp)
movq 0x38(%rsp), %rax
movq %rcx, 0x18(%rsp)
movq (%rax,%rcx,8), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1080
negl %eax
movl $0x0, %r14d
sbbl %r14d, %r14d
orl 0x2b0(%rsp), %r14d
movq %rbx, %rdi
leaq 0x4baf(%rip), %rsi      # 0x6059
callq 0x10b0
movq %rax, %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movl $0x0, 0xa0(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movl $0x1, 0x40(%rsp)
movl %r14d, 0x44(%rsp)
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
callq 0x1140
movq %rax, %rbx
movq %rax, %rdi
callq 0x1120
movl $0x5b, %edx
leaq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0x1040
movq %rbx, %rdi
callq 0x10c0
movl $0x64, %esi
movl $0x1, %edx
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x1150
leaq 0x138(%rsp), %r12
movq %r12, %rdi
callq 0x5974
movl $0x64, %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x5a16
testl %r14d, %r14d
jle 0x15e6
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
movq %rbp, %rdi
movq %r13, %rcx
callq 0x10f0
movq %rax, %r12
testl %r12d, %r12d
jle 0x15e1
movl %r12d, %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movl $0x1, %esi
movq %rbp, %rdi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x1150
leaq 0x138(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x5a16
subl %r12d, %r14d
testl %r12d, %r12d
jg 0x158d
testl %r14d, %r14d
jne 0x167c
movq %r13, %rdi
callq 0x1090
movl $0x20, %edx
leaq 0x138(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x5cc4
leaq 0x110(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movl $0x20, %edx
leaq 0xd0(%rsp), %rcx
movq 0x30(%rsp), %r8
callq 0x2455
movl $0x60, %esi
movl $0x1, %edx
leaq 0xb0(%rsp), %rdi
movq %r15, %rcx
callq 0x1150
leaq 0x138(%rsp), %rdi
callq 0x59c6
testl %r14d, %r14d
jne 0x16a3
movq 0x18(%rsp), %rcx
incq %rcx
cmpq 0x10(%rsp), %rcx
setb %al
jne 0x1470
jmp 0x16a7
movq 0x795d(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x49ea(%rip), %rsi      # 0x6077
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x10d0
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
jmp 0x1652
movb 0xb(%rsp), %al
testb $0x1, %al
jne 0x16bf
movq %r15, %rdi
callq 0x1090
leaq 0x49e3(%rip), %rdi      # 0x609d
callq 0x1100
movl 0xc(%rsp), %eax
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x119e
jmp 0x16fc
movq 0x78fd(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x496f(%rip), %rsi      # 0x605c
leaq 0x1b0(%rsp), %rdx
xorl %eax, %eax
callq 0x10d0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x16c3
jmp 0x1705
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
callq 0x59c6
movq %rbx, %rdi
callq 0x1130
nopl (%rax)