// File: /workspace/github_cpp_makefile_star2_O3/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 $0x468, %rsp            # imm = 0x468
movq %rsi, %r13
movl %edi, %ebx
callq 0x16da
movq %rax, 0x30(%rsp)
cmpl $0x3, %ebx
jle 0x161d
leal -0x3(%rbx), %r12d
movl %r12d, 0xc(%rsp)
xorl %r15d, %r15d
leaq 0x1a0(%rsp), %r14
movq 0x10(%r13,%r15,8), %rdi
movq %r14, %rsi
callq 0x1080
testl %eax, %eax
jne 0x1624
cmpl $0x0, 0x1d0(%rsp)
jle 0x1624
incq %r15
cmpq %r15, %r12
jne 0x12f3
movq 0x8(%r13), %rdx
leaq 0x4d29(%rip), %rsi      # 0x6052
leaq 0x3a0(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x1030
leaq 0x4d17(%rip), %rsi      # 0x6059
movq %r14, %rdi
callq 0x10b0
movq %rax, %r14
leaq 0xa0(%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 0x163c
movl %ebx, %eax
movq -0x8(%r13,%rax,8), %rdi
leaq 0x4ce5(%rip), %rsi      # 0x606f
callq 0x10b0
movq %rax, %rbp
leaq 0x4cd9(%rip), %rdi      # 0x6072
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x1150
leaq 0xc(%rsp), %rbx
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %rbp, %rcx
callq 0x1150
movl (%rbx), %eax
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x15d9
xorl %eax, %eax
leaq 0x1a0(%rsp), %rsi
leaq 0x128(%rsp), %r15
movq %r13, 0x18(%rsp)
movq %rax, 0x38(%rsp)
movq 0x10(%r13,%rax,8), %rbx
movq %rbx, %rdi
callq 0x1080
movl 0x1d0(%rsp), %ecx
negl %eax
movl $0x0, %r14d
sbbl %r14d, %r14d
movl %ecx, 0x8(%rsp)
orl %ecx, %r14d
movq %rbx, %rdi
leaq 0x4c38(%rip), %rsi      # 0x6059
callq 0x10b0
movq %rax, 0x10(%rsp)
leaq 0xcc(%rsp), %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x4c(%r13)
movups %xmm0, 0x40(%r13)
movups %xmm0, 0x30(%r13)
movups %xmm0, 0x20(%r13)
movups %xmm0, 0x10(%r13)
movups %xmm0, (%r13)
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 $0x1, 0xc4(%rsp)
movl %r14d, 0xc8(%rsp)
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
callq 0x1140
movq %rax, %r12
movq %rax, %rdi
callq 0x1120
movl $0x5b, %edx
movq %r13, %rdi
movq %rax, %rsi
callq 0x1040
movq %r12, %rdi
callq 0x10c0
movl $0x64, %esi
movl $0x1, %edx
leaq 0xc4(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rcx
callq 0x1150
movq %r15, %rdi
callq 0x5740
movl $0x64, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x57d8
testl %r14d, %r14d
jle 0x1555
movl 0x8(%rsp), %r14d
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
leaq 0x1a0(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rcx
callq 0x10f0
movq %rax, %r13
testl %r13d, %r13d
jle 0x15f1
movl %r13d, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl $0x1, %esi
movq %rbx, %rdi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x1150
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x57d8
subl %r13d, %r14d
jg 0x14fa
testl %r14d, %r14d
jne 0x15f1
movq 0x10(%rsp), %rdi
callq 0x1090
movl $0x20, %edx
movq %r15, %rdi
leaq 0x40(%rsp), %rbx
movq %rbx, %rsi
callq 0x5ab4
leaq 0xa0(%rsp), %rdi
movq %rbx, %rsi
movl $0x20, %edx
leaq 0x60(%rsp), %rcx
movq 0x30(%rsp), %r8
callq 0x2403
movl $0x60, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %rbp, %rcx
callq 0x1150
movq %r15, %rdi
callq 0x5792
movq 0x38(%rsp), %rax
incq %rax
cmpq 0x28(%rsp), %rax
movq 0x18(%rsp), %r13
leaq 0x1a0(%rsp), %rsi
jne 0x13ec
movq %rbp, %rdi
callq 0x1090
leaq 0x4ab5(%rip), %rdi      # 0x609d
callq 0x1100
xorl %eax, %eax
jmp 0x1661
movq 0x79e8(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x4a75(%rip), %rsi      # 0x6077
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x10d0
leaq 0x128(%rsp), %rdi
callq 0x5792
jmp 0x165c
callq 0x1180
jmp 0x165c
movq 0x79b5(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movq 0x10(%r13,%r15,8), %rdx
leaq 0x4a03(%rip), %rsi      # 0x603d
jmp 0x1655
movq 0x799d(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x4a0f(%rip), %rsi      # 0x605c
leaq 0x3a0(%rsp), %rdx
xorl %eax, %eax
callq 0x10d0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1675
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x5792
movq %rbx, %rdi
callq 0x1130
nopl (%rax)