// File: /workspace/github_cpp_makefile_star2_O1/Vanuan[P]pocketbook_free_swupdate/PocketBookUpdate.cpp
// Function: PocketBookUpdate::PocketBookUpdate(char const*)
PocketBookUpdate::PocketBookUpdate(const char*filename) : mFilename(filename)
{
  // allocate buffer
  int size_of_header = sizeof(PocketBookUpdateHeader);
  buffer = new char[size_of_header];

  // read buffer
  fstream file;
  file.open(filename);
  file.read(buffer, size_of_header);
  file.close();

  // interpret buffer as header
  header = reinterpret_cast<PocketBookUpdateHeader*>(buffer);

  partsNames[SWUPDATE_TAR_GZ] = "swupdate.tar.gz";
  partsNames[ELF_IMG] = "elf.img";
  partsNames[APP_IMG] = "app.img";
  partsNames[ROOTFS_IMG] = "rootfs.img";
  partsNames[A_IMG] = "a.img";

  for(int i = 0; i < sizeof(header->fwParts)/sizeof(header->fwParts[0]); ++i)
  {
    if(0 == header->fwParts[i].size ) continue;
    string name;
    // printName
    map<PartitionType, const char*>::const_iterator partNameIter =
       partsNames.find(static_cast<PartitionType>(header->fwParts[i].type));
    if(partNameIter != partsNames.end())
    {
      name = partNameIter->second;
    }
    else
    {
      name = "unknownImg";
    }
    int nameCounter = 0;
    string tempName;
    map<string, FWPart>::iterator it = parts.end();
    do {
      tempName = name + convertInt(nameCounter);
      it = parts.find(string(tempName));
      nameCounter++;
    }
    while(it != parts.end());
    name = tempName;
    pair<map<string, FWPart>::iterator,bool> index = 
        parts.insert(pair<string, FWPart>(name, header->fwParts[i]));
    index.first->second.offset += sizeof(*header);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2d8, %rsp            # imm = 0x2D8
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x36e0(%rip), %rax      # 0x5d88
movq %rax, (%rdi)
addq $0x18, %rdi
leaq 0xc8(%rsp), %rdx
callq 0x31d2
leaq 0x38(%r15), %rax
movq %rax, 0x88(%rsp)
leaq 0x40(%r15), %r13
xorl %eax, %eax
movl %eax, 0x40(%r15)
movq %rax, 0x48(%r15)
movq %r13, 0x50(%r15)
movq %r13, 0x58(%r15)
movq %rax, 0x60(%r15)
leaq 0x68(%r15), %rcx
movq %rcx, 0x30(%rsp)
leaq 0x70(%r15), %r14
movl %eax, 0x70(%r15)
movq %rax, 0x78(%r15)
movq %r14, 0x80(%r15)
movq %r14, 0x88(%r15)
movq %rax, 0x90(%r15)
movl $0x400, %edi            # imm = 0x400
movq %r15, 0x8(%rsp)
callq 0x2030
movq %rax, 0x8(%r15)
leaq 0xc8(%rsp), %rdi
callq 0x2250
leaq 0xc8(%rsp), %rdi
movq %rbx, %rsi
movl $0x18, %edx
callq 0x21b0
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0xc8(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
callq 0x2270
leaq 0xc8(%rsp), %rdi
callq 0x2060
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
movq %rax, 0x10(%rcx)
leaq 0x38(%rsp), %rsi
movl $0x73, (%rsi)
movq 0x88(%rsp), %rbx
movq %rbx, %rdi
callq 0x3210
leaq 0x1895(%rip), %rcx      # 0x402d
movq %rcx, (%rax)
leaq 0x38(%rsp), %rsi
movl $0x40, (%rsi)
movq %rbx, %rdi
callq 0x3210
leaq 0x1888(%rip), %rcx      # 0x403d
movq %rcx, (%rax)
leaq 0x38(%rsp), %rsi
movl $0x65, (%rsi)
movq %rbx, %rdi
callq 0x3210
leaq 0x1873(%rip), %rcx      # 0x4045
movq %rcx, (%rax)
leaq 0x38(%rsp), %rsi
movl $0x72, (%rsi)
movq %rbx, %rdi
callq 0x3210
leaq 0x185e(%rip), %rcx      # 0x404d
movq %rcx, (%rax)
leaq 0x38(%rsp), %rsi
movl $0x61, (%rsi)
movq %rbx, %rdi
callq 0x3210
leaq 0x184c(%rip), %rcx      # 0x4058
movq %rcx, (%rax)
leaq 0x48(%rsp), %r15
xorl %ecx, %ecx
leaq 0xa8(%rsp), %rbp
leaq 0x38(%rsp), %r12
movq %r13, 0x90(%rsp)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq %rcx, %rdx
shlq $0x4, %rdx
cmpl $0x0, 0x10c(%rax,%rdx)
je 0x2acb
movq %rcx, 0x98(%rsp)
addq $0x100, %rax            # imm = 0x100
leaq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
movq %rdx, 0xa0(%rsp)
movl (%rax,%rdx), %eax
movq 0x8(%rsp), %rcx
movq 0x48(%rcx), %rdx
movq %r13, %rcx
testq %rdx, %rdx
je 0x28a5
movq %r13, %rcx
xorl %esi, %esi
cmpl %eax, 0x20(%rdx)
setl %sil
cmovgeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x288e
movq %r13, %rdx
cmpq %r13, %rcx
je 0x28b7
cmpl 0x20(%rcx), %eax
cmovlq %r13, %rcx
movq %rcx, %rdx
cmpq %r13, %rdx
je 0x28de
movq 0x28(%rdx), %rbx
movq %rbx, %rdi
callq 0x20f0
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %rcx
movq %rax, %r8
callq 0x2300
jmp 0x28f9
movl $0xa, %r8d
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x176a(%rip), %rcx      # 0x405e
callq 0x2300
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
movq $0x0, 0x70(%rsp)
movb $0x0, 0x78(%rsp)
xorl %r13d, %r13d
movq %rbp, %rdi
movl %r13d, %esi
callq 0x25ec
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %r8
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2220
movq %r15, 0x38(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x2958
movq %rdx, 0x38(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x48(%rsp)
jmp 0x295f
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rdx, 0x40(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x68(%rsp), %rdi
movq %r12, %rsi
callq 0x2260
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x299b
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x29c0
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %r15, 0x38(%rsp)
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x3348
movq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x366e
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x2a01
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
incl %r13d
cmpq %r14, %rbx
jne 0x2914
leaq 0x10(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2110
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rbx
movq %r15, 0x38(%rsp)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x3348
movq 0xa0(%rsp), %rax
movups 0x100(%rbx,%rax), %xmm0
movups %xmm0, 0x10(%r15)
movq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x36f8
movq %rax, %r13
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x2a7b
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
addl $0x400, 0x48(%r13)      # imm = 0x400
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x2a9f
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
movq 0x90(%rsp), %r13
je 0x2ac3
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x98(%rsp), %rcx
incq %rcx
cmpq $0x30, %rcx
jne 0x282b
leaq 0xc8(%rsp), %rdi
callq 0x2090
addq $0x2d8, %rsp            # imm = 0x2D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2b0b
jmp 0x2b0b
jmp 0x2b0b
jmp 0x2b0b
jmp 0x2b0b
jmp 0x2b03
movq %rax, %r14
jmp 0x2ba6
movq %rax, %r14
jmp 0x2b99
jmp 0x2b42
jmp 0x2b5e
jmp 0x2b5e
movq %rax, %r14
jmp 0x2b7d
jmp 0x2b5e
movq %rax, %r14
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x2b61
movq 0xb8(%rsp), %rsi
jmp 0x2b54
movq %rax, %r14
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x2b61
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x2b61
movq %rax, %r14
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x2b7d
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21f0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x2b99
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0xc8(%rsp), %rdi
callq 0x2090
movq 0x30(%rsp), %rdi
callq 0x3280
movq 0x88(%rsp), %rdi
callq 0x32f8
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
addq $0x28, %rax
cmpq %rax, %rdi
je 0x2bda
movq (%rax), %rsi
incq %rsi
callq 0x21f0
movq %r14, %rdi
callq 0x2350