// File: /workspace/github_cpp_makefile_star2_O2/DimitriFourny[P]unxip/unxip.cpp
// Function: main
int main(int argc, char* argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <file.xip>\n", argv[0]);
    return 1;
  }
  char* filepath = argv[1];

  FILE* file = fopen(filepath, "rb");
  if (!file) {
    fprintf(stderr, "Error: File \"%s\" can't be opened\n", filepath);
    return 1;
  }

  std::string outdir = CreateXipDirectory(filepath);

  // Check the XIP headers
  struct XipHeader header;
  memset(&header, 0, sizeof(header));
  size_t nb_read = 0;
  nb_read = fread(&header, 1, sizeof(header), file);

  if (nb_read < 4 || memcmp(&header.signature, "xar!", 4)) {
    fprintf(stderr, "Invalid XAR signature\n");
    return InvalidXipFile(file);
  }

  // Big endian to little endian
  header.header_size = __bswap_16(header.header_size);
  header.xar_version = __bswap_16(header.xar_version);
  header.toc_size_compressed = __bswap_64(header.toc_size_compressed);
  header.toc_size_uncompressed = __bswap_64(header.toc_size_uncompressed);
  header.checksum_algo = __bswap_32(header.checksum_algo);

  if (fseek(file, header.header_size, SEEK_SET)) {
    fprintf(stderr, "Header size is invalid\n");
    return InvalidXipFile(file);
  }

  if (!header.toc_size_compressed ||
      header.toc_size_compressed > MAX_TOC_SIZE ||
      header.toc_size_uncompressed > MAX_TOC_SIZE) {
    fprintf(stderr, "Table of Contents invalid size\n");
    return InvalidXipFile(file);
  }

  // Read the Table of Contents (TOC)
  uint8_t* toc_compressed = new uint8_t[header.toc_size_compressed];
  nb_read = fread(toc_compressed, 1, header.toc_size_compressed, file);
  if (nb_read < header.toc_size_compressed) {
    fprintf(stderr, "Can't read the Table of Contents\n");
    return InvalidXipFile(file);
  }

  const size_t toc_size = header.toc_size_uncompressed;
  uint8_t* toc = new uint8_t[toc_size];
  bool success =
      ZlibUncompress(toc_compressed, header.toc_size_compressed, toc, toc_size);
  if (!success) {
    fprintf(stderr, "Can't uncompress the Table of Contents\n");
    return InvalidXipFile(file);
  }
  delete[] toc_compressed;

  SaveToFile(outdir + "xip_toc.xml", toc, toc_size);

  // Parse the TOC to get the files
  auto files = ParseTableOfContents(toc, toc_size);
  std::string savepath;
  for (const auto& xar_file : files) {
    savepath = outdir + xar_file.name;
    printf("%s\t", savepath.c_str());
    fflush(stdout);

    const size_t offset =
        header.header_size + header.toc_size_compressed + xar_file.offset;
    success = SaveFileStreamToFile(savepath, file, offset, xar_file.size);

    if (success) {
      printf("done\n");
    } else {
      printf("error\n");
    }
  }

  fclose(file);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
cmpl $0x1, %edi
jle 0x2b0a
movq 0x8(%rsi), %rbx
leaq 0x17fb(%rip), %rsi      # 0x40d9
movq %rbx, %rdi
callq 0x21a0
testq %rax, %rax
je 0x2b20
movq %rax, %r14
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
callq 0x242d
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movups %xmm0, 0xc(%rdi)
movaps %xmm0, (%rdi)
pushq $0x1
popq %rsi
pushq $0x1c
popq %rdx
movq %r14, (%rsp)
movq %r14, %rcx
callq 0x2240
cmpq $0x4, %rax
jb 0x2af2
cmpl $0x21726178, 0x40(%rsp) # imm = 0x21726178
jne 0x2af2
movzwl 0x44(%rsp), %eax
rolw $0x8, %ax
movq 0x48(%rsp), %r14
movq 0x50(%rsp), %r12
movzwl %ax, %r15d
movq (%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2180
testl %eax, %eax
jne 0x2b40
bswapq %r14
leaq -0x10000(%r14), %rax
cmpq $-0xffff, %rax          # imm = 0xFFFF0001
jb 0x2b55
bswapq %r12
cmpq $0x10000, %r12          # imm = 0x10000
jae 0x2b55
movq %r14, %rdi
callq 0x2030
movq %rax, %r13
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r14, %rdx
movq (%rsp), %rcx
callq 0x2240
cmpq %r14, %rax
jb 0x2b6a
movq %r12, %rdi
callq 0x2030
movq %rax, %rbx
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0x23cc
testb %al, %al
je 0x2b7f
movq %r13, %rdi
callq 0x21d0
leaq 0x17b8(%rip), %rdx      # 0x419c
leaq 0x8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2c85
leaq 0x8(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x24de
movq %r13, %rdi
callq 0x20a0
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2793
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x28(%rsp), %r12
movq 0x30(%rsp), %r13
addq %r15, %r14
leaq 0x88(%rsp), %r15
leaq 0x8(%rsp), %rbp
leaq 0x17a0(%rip), %rbx      # 0x41f0
cmpq %r13, %r12
je 0x2ace
movq %r15, %rdi
leaq 0x68(%rsp), %rsi
movq %r12, %rdx
callq 0x2cbd
movq %rbp, %rdi
movq %r15, %rsi
callq 0x21c0
movq %r15, %rdi
callq 0x20a0
movq 0x8(%rsp), %rsi
leaq 0x1724(%rip), %rdi      # 0x41a8
xorl %eax, %eax
callq 0x2050
movq 0x3536(%rip), %rax      # 0x5fc8
movq (%rax), %rdi
callq 0x2190
movq 0x20(%r12), %rdx
addq %r14, %rdx
movq 0x28(%r12), %rcx
movq %rbp, %rdi
movq (%rsp), %rsi
callq 0x254f
testb %al, %al
movq %rbx, %rdi
leaq 0x1737(%rip), %rax      # 0x41f6
cmovneq %rax, %rdi
callq 0x2250
addq $0x30, %r12
jmp 0x2a50
movq (%rsp), %rdi
callq 0x2130
leaq 0x8(%rsp), %rdi
callq 0x20a0
leaq 0x28(%rsp), %rdi
callq 0x2c64
xorl %ebx, %ebx
jmp 0x2ba7
movq 0x34e7(%rip), %rax      # 0x5fe0
movq (%rax), %rcx
leaq 0x1600(%rip), %rdi      # 0x4103
pushq $0x16
jmp 0x2b92
movq 0x34cf(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
movq (%rsi), %rdx
leaq 0x15a5(%rip), %rsi      # 0x40c3
jmp 0x2b34
movq 0x34b9(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
leaq 0x15ab(%rip), %rsi      # 0x40dc
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2210
pushq $0x1
popq %rbx
jmp 0x2bb1
movq 0x3499(%rip), %rax      # 0x5fe0
movq (%rax), %rcx
leaq 0x15c9(%rip), %rdi      # 0x411a
pushq $0x17
jmp 0x2b92
movq 0x3484(%rip), %rax      # 0x5fe0
movq (%rax), %rcx
leaq 0x15cc(%rip), %rdi      # 0x4132
pushq $0x1f
jmp 0x2b92
movq 0x346f(%rip), %rax      # 0x5fe0
movq (%rax), %rcx
leaq 0x15d7(%rip), %rdi      # 0x4152
pushq $0x21
jmp 0x2b92
movq 0x345a(%rip), %rax      # 0x5fe0
movq (%rax), %rcx
leaq 0x15e4(%rip), %rdi      # 0x4174
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
movq (%rsp), %rdi
callq 0x288c
pushq $0x1
popq %rbx
leaq 0x68(%rsp), %rdi
callq 0x20a0
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2bcd
jmp 0x2bcd
jmp 0x2bcd
jmp 0x2bcd
movq %rax, %rbx
jmp 0x2be9
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x20a0
leaq 0x28(%rsp), %rdi
callq 0x2c64
leaq 0x68(%rsp), %rdi
callq 0x20a0
movq %rbx, %rdi
callq 0x2270
nop