// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rdi, %r15
leaq 0x3792(%rip), %rax      # 0x5d98
movq %rax, (%rdi)
addq $0x18, %rdi
leaq 0xc8(%rsp), %rdx
movq %rdi, 0x38(%rsp)
callq 0x2e62
andl $0x0, 0x40(%r15)
andq $0x0, 0x48(%r15)
leaq 0x38(%r15), %rax
movq %rax, (%rsp)
leaq 0x40(%r15), %rax
movq %rax, 0x50(%r15)
movq %rax, 0x40(%rsp)
movq %rax, 0x58(%r15)
andq $0x0, 0x60(%r15)
leaq 0x68(%r15), %rax
movq %rax, 0x8(%rsp)
leaq 0x70(%r15), %rbx
andl $0x0, 0x70(%r15)
andq $0x0, 0x78(%r15)
movq %rbx, 0x80(%r15)
movq %rbx, 0x88(%r15)
andq $0x0, 0x90(%r15)
movl $0x400, %edi            # imm = 0x400
callq 0x2030
movq %rax, 0x8(%r15)
leaq 0xc8(%rsp), %rdi
callq 0x2210
leaq 0xc8(%rsp), %rdi
pushq $0x18
popq %rdx
movq %r14, %rsi
callq 0x21a0
movq 0x8(%r15), %rsi
leaq 0xc8(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
callq 0x2230
leaq 0xc8(%rsp), %rdi
callq 0x2050
movq 0x8(%r15), %rax
movq %rax, 0x10(%r15)
leaq 0x58(%rsp), %rsi
movl $0x73, (%rsi)
movq (%rsp), %r14
movq %r14, %rdi
callq 0x2e9e
leaq 0x1942(%rip), %rcx      # 0x402d
movq %rcx, (%rax)
leaq 0x58(%rsp), %rsi
movl $0x40, (%rsi)
movq %r14, %rdi
callq 0x2e9e
leaq 0x1935(%rip), %rcx      # 0x403d
movq %rcx, (%rax)
leaq 0x58(%rsp), %rsi
movl $0x65, (%rsi)
movq %r14, %rdi
callq 0x2e9e
leaq 0x1920(%rip), %rcx      # 0x4045
movq %rcx, (%rax)
leaq 0x58(%rsp), %rsi
movl $0x72, (%rsi)
movq %r14, %rdi
callq 0x2e9e
movq %r15, 0x10(%rsp)
leaq 0x1906(%rip), %rcx      # 0x404d
movq %rcx, (%rax)
leaq 0x58(%rsp), %rsi
movl $0x61, (%rsi)
movq %r14, %rdi
callq 0x2e9e
leaq 0x18f4(%rip), %rcx      # 0x4058
movq %rcx, (%rax)
xorl %ecx, %ecx
leaq 0x58(%rsp), %r13
leaq 0xa8(%rsp), %rbp
leaq 0x88(%rsp), %r14
movq (%rsp), %rdi
cmpq $0x30, %rcx
je 0x290a
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq %rcx, %r15
shlq $0x4, %r15
cmpl $0x0, 0x10c(%rax,%r15)
je 0x2902
movq %rcx, 0x50(%rsp)
leaq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rsp)
andq $0x0, 0x20(%rsp)
addq $0x100, %rax            # imm = 0x100
movb $0x0, 0x28(%rsp)
movl (%rax,%r15), %eax
movl %eax, 0x58(%rsp)
movq %r13, %rsi
callq 0x3362
cmpq 0x40(%rsp), %rax
movq %r15, 0x48(%rsp)
je 0x27f7
movq 0x28(%rax), %rsi
leaq 0x18(%rsp), %rdi
callq 0x2350
jmp 0x2808
leaq 0x18(%rsp), %rdi
leaq 0x185b(%rip), %rsi      # 0x405e
callq 0x2350
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
andq $0x0, 0x90(%rsp)
movb $0x0, 0x98(%rsp)
xorl %r12d, %r12d
movq %rbp, %rdi
movl %r12d, %esi
callq 0x2578
movq %r13, %rdi
leaq 0x18(%rsp), %rsi
movq %rbp, %rdx
callq 0x2eec
movq %r14, %rdi
movq %r13, %rsi
callq 0x2220
movq %r13, %rdi
callq 0x20e0
movq %rbp, %rdi
callq 0x20e0
movq %r13, %rdi
movq %r14, %rsi
callq 0x2290
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x3396
movq %rax, %r15
movq %r13, %rdi
callq 0x20e0
incl %r12d
cmpq %rbx, %r15
jne 0x282c
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x2100
movq 0x10(%rsp), %rax
movq 0x10(%rax), %r15
movq %r13, %rdi
movq %r12, %rsi
callq 0x2290
movq 0x48(%rsp), %rax
movups 0x100(%r15,%rax), %xmm0
leaq 0x78(%rsp), %rax
movups %xmm0, (%rax)
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x3420
movq %rax, %r15
movq %r13, %rdi
callq 0x20e0
addl $0x400, 0x48(%r15)      # imm = 0x400
movq %r14, %rdi
callq 0x20e0
leaq 0x18(%rsp), %rdi
callq 0x20e0
movq (%rsp), %rdi
movq 0x50(%rsp), %rcx
incq %rcx
jmp 0x2782
leaq 0xc8(%rsp), %rdi
callq 0x2080
addq $0x2d8, %rsp            # imm = 0x2D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x293a
jmp 0x293a
jmp 0x293a
jmp 0x293a
jmp 0x293a
jmp 0x2935
movq %rax, %rbx
jmp 0x2991
movq %rax, %rbx
jmp 0x2984
jmp 0x2960
jmp 0x295b
jmp 0x295b
jmp 0x2947
movq %rax, %rbx
jmp 0x297a
jmp 0x295b
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0x2968
movq %rax, %rbx
jmp 0x296d
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x20e0
leaq 0x88(%rsp), %rdi
callq 0x20e0
leaq 0x18(%rsp), %rdi
callq 0x20e0
leaq 0xc8(%rsp), %rdi
callq 0x2080
movq 0x8(%rsp), %rdi
callq 0x2fa2
movq (%rsp), %rdi
callq 0x3006
movq 0x38(%rsp), %rdi
callq 0x20e0
movq %rbx, %rdi
callq 0x2320