// File: /workspace/github_cpp_makefile_star2_O2/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 $0x478, %rsp            # imm = 0x478
movq %rsi, %r13
movl %edi, %ebx
callq 0x16a9
movq %rax, 0x20(%rsp)
cmpl $0x3, %ebx
jle 0x15ff
leal -0x3(%rbx), %r15d
movl %r15d, 0x14(%rsp)
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x1313
movq 0x10(%r13,%r14,8), %rdi
callq 0x127c
incq %r14
testl %eax, %eax
jg 0x12db
movq 0x6ce8(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
movq 0x8(%r13,%r14,8), %rdx
leaq 0x3d36(%rip), %rsi      # 0x503d
xorl %eax, %eax
callq 0x10f0
jmp 0x161b
movq 0x8(%r13), %rdx
leaq 0x3d34(%rip), %rsi      # 0x5052
leaq 0x1a0(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x1030
leaq 0x3d22(%rip), %rsi      # 0x5059
movq %r14, %rdi
callq 0x10d0
movq %rax, %r14
leaq 0xa0(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x20
popq %rdx
movq %rax, %rcx
callq 0x1110
movq %rax, %r15
movq %r14, %rdi
callq 0x10b0
cmpl $0x20, %r15d
jne 0x1620
movq $0x0, 0x8(%rsp)
movl %ebx, %eax
movq -0x8(%r13,%rax,8), %rdi
leaq 0x3ceb(%rip), %rsi      # 0x506f
callq 0x10d0
movq %rax, %r12
leaq 0x3cdf(%rip), %rdi      # 0x5072
pushq $0x4
popq %rbx
pushq $0x1
popq %r15
movq %rbx, %rsi
movq %r15, %rdx
movq %rax, %rcx
callq 0x1160
leaq 0x14(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, (%rsp)
movq %r12, %rcx
callq 0x1160
movl (%r14), %eax
movq %rax, 0x28(%rsp)
leaq 0x270(%rsp), %r14
xorl %eax, %eax
movq %r13, 0x18(%rsp)
cmpq 0x28(%rsp), %rax
je 0x15d3
movq %rax, 0x38(%rsp)
movq 0x10(%r13,%rax,8), %r12
movq %r12, %rdi
callq 0x127c
movq %rax, %rbp
movq %r12, %rdi
leaq 0x3c56(%rip), %rsi      # 0x5059
callq 0x10d0
movq %rax, %rbx
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 %ebp, 0xc8(%rsp)
movq %r12, 0x30(%rsp)
movq %r12, %rdi
callq 0x1150
movq %r15, %r12
movq %rax, %r15
movq %rax, %rdi
callq 0x1130
movq %r13, %rdi
movq %rax, %rsi
pushq $0x5b
popq %rdx
callq 0x1060
movq %r15, %rdi
movq %r12, %r15
callq 0x10e0
leaq 0xc4(%rsp), %rdi
pushq $0x64
popq %rsi
movq %r12, %rdx
movq (%rsp), %rcx
callq 0x1160
leaq 0x128(%rsp), %r13
movq %r13, %rdi
callq 0x47f0
movq %r13, %rdi
leaq 0xc4(%rsp), %rsi
pushq $0x64
popq %rdx
callq 0x485c
testl %ebp, %ebp
jle 0x1532
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rcx
callq 0x1110
movq %rax, %r13
testl %r13d, %r13d
jle 0x15aa
movq %r15, %rsi
movl %r13d, %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq %r14, %rdi
movq %r15, %rdx
movq (%rsp), %rcx
callq 0x1160
leaq 0x128(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x485c
subl %r13d, %ebp
movq %r12, %r15
jmp 0x14d8
jne 0x15aa
movq %rbx, %rdi
callq 0x10b0
leaq 0x128(%rsp), %rdi
leaq 0x40(%rsp), %rsi
pushq $0x20
popq %rdx
callq 0x4b28
leaq 0xa0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
pushq $0x20
popq %rdx
leaq 0x60(%rsp), %rcx
movq 0x20(%rsp), %r8
callq 0x20e0
leaq 0x40(%rsp), %rdi
pushq $0x60
popq %rsi
movq %r15, %rdx
movq (%rsp), %rcx
callq 0x1160
movb $0x1, %bl
movq 0x18(%rsp), %r13
leaq 0x128(%rsp), %rdi
callq 0x4820
movq 0x38(%rsp), %rax
incq %rax
testb %bl, %bl
jne 0x13d9
jmp 0x15e8
movq 0x6a2f(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
xorl %ebx, %ebx
leaq 0x3aba(%rip), %rsi      # 0x5077
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x10f0
pushq $-0x1
popq %rax
movq %rax, 0x8(%rsp)
jmp 0x1586
movq (%rsp), %rdi
callq 0x10b0
leaq 0x3aba(%rip), %rdi      # 0x509d
callq 0x1120
movq 0x8(%rsp), %rax
addq $0x478, %rsp            # imm = 0x478
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x69da(%rip), %rax      # 0x7fe0
movq (%rax), %rcx
leaq 0x39f4(%rip), %rdi      # 0x5004
pushq $0x38
popq %rsi
pushq $0x1
popq %rdx
callq 0x1160
pushq $-0x1
popq %rax
jmp 0x15ed
movq 0x69b9(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x3a2b(%rip), %rsi      # 0x505c
leaq 0x1a0(%rsp), %rdx
xorl %eax, %eax
callq 0x10f0
pushq $-0x1
popq %rax
movq %rax, 0x8(%rsp)
jmp 0x15e8
jmp 0x164c
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x4820
movq %rbx, %rdi
callq 0x1140