// File: 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
movq %rsp, %rbp
subq $0x440, %rsp            # imm = 0x440
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x3a8(%rbp)
leaq 0x855b(%rip), %rcx      # 0xbd50
movq %rcx, (%rax)
addq $0x18, %rax
movq %rax, -0x3a0(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x398(%rbp)
leaq -0x11(%rbp), %rdi
movq %rdi, -0x390(%rbp)
callq 0x3420
movq -0x3a0(%rbp), %rdi
movq -0x398(%rbp), %rsi
movq -0x390(%rbp), %rdx
callq 0x4860
jmp 0x383a
leaq -0x11(%rbp), %rdi
callq 0x32b0
movq -0x3a8(%rbp), %rdi
addq $0x38, %rdi
movq %rdi, -0x3c0(%rbp)
callq 0x4910
movq -0x3a8(%rbp), %rdi
addq $0x68, %rdi
movq %rdi, -0x3b8(%rbp)
callq 0x4930
movl $0x400, -0x28(%rbp)     # imm = 0x400
movslq -0x28(%rbp), %rdi
callq 0x3030
movq %rax, -0x3b0(%rbp)
jmp 0x388a
movq -0x3a8(%rbp), %rax
movq -0x3b0(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq -0x238(%rbp), %rdi
callq 0x3300
jmp 0x38aa
movq -0x10(%rbp), %rax
movq %rax, -0x3d0(%rbp)
movl $0x8, %edi
movl $0x10, %esi
callq 0x4950
movl %eax, -0x3c4(%rbp)
jmp 0x38cc
movl -0x3c4(%rbp), %edx
movq -0x3d0(%rbp), %rsi
leaq -0x238(%rbp), %rdi
callq 0x3230
jmp 0x38e7
movq -0x3a8(%rbp), %rax
movq 0x8(%rax), %rsi
movslq -0x28(%rbp), %rdx
leaq -0x238(%rbp), %rdi
callq 0x3330
jmp 0x3904
leaq -0x238(%rbp), %rdi
callq 0x3070
jmp 0x3912
movq -0x3a8(%rbp), %rdi
movq 0x8(%rdi), %rax
movq %rax, 0x10(%rdi)
addq $0x38, %rdi
movl $0x73, -0x23c(%rbp)
leaq -0x23c(%rbp), %rsi
callq 0x4970
movq %rax, -0x3d8(%rbp)
jmp 0x3944
movq -0x3a8(%rbp), %rdi
movq -0x3d8(%rbp), %rax
leaq 0x46d4(%rip), %rcx      # 0x802d
movq %rcx, (%rax)
addq $0x38, %rdi
movl $0x40, -0x240(%rbp)
leaq -0x240(%rbp), %rsi
callq 0x4970
movq %rax, -0x3e0(%rbp)
jmp 0x397f
movq -0x3a8(%rbp), %rdi
movq -0x3e0(%rbp), %rax
leaq 0x46a9(%rip), %rcx      # 0x803d
movq %rcx, (%rax)
addq $0x38, %rdi
movl $0x65, -0x244(%rbp)
leaq -0x244(%rbp), %rsi
callq 0x4970
movq %rax, -0x3e8(%rbp)
jmp 0x39ba
movq -0x3a8(%rbp), %rdi
movq -0x3e8(%rbp), %rax
leaq 0x4676(%rip), %rcx      # 0x8045
movq %rcx, (%rax)
addq $0x38, %rdi
movl $0x72, -0x248(%rbp)
leaq -0x248(%rbp), %rsi
callq 0x4970
movq %rax, -0x3f0(%rbp)
jmp 0x39f5
movq -0x3a8(%rbp), %rdi
movq -0x3f0(%rbp), %rax
leaq 0x4643(%rip), %rcx      # 0x804d
movq %rcx, (%rax)
addq $0x38, %rdi
movl $0x61, -0x24c(%rbp)
leaq -0x24c(%rbp), %rsi
callq 0x4970
movq %rax, -0x3f8(%rbp)
jmp 0x3a30
movq -0x3f8(%rbp), %rax
leaq 0x461a(%rip), %rcx      # 0x8058
movq %rcx, (%rax)
movl $0x0, -0x250(%rbp)
movslq -0x250(%rbp), %rax
cmpq $0x30, %rax
jae 0x3e6e
movq -0x3a8(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x100, %rcx            # imm = 0x100
movslq -0x250(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
xorl %eax, %eax
cmpl 0xc(%rcx), %eax
jne 0x3ac4
jmp 0x3de6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x11(%rbp), %rdi
callq 0x32b0
jmp 0x3eb3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x3e8f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x3e83
leaq -0x270(%rbp), %rdi
callq 0x3390
movq -0x3a8(%rbp), %rax
movq %rax, %rdi
addq $0x38, %rdi
movq 0x10(%rax), %rax
movslq -0x250(%rbp), %rcx
shlq $0x4, %rcx
movl 0x100(%rax,%rcx), %eax
movl %eax, -0x284(%rbp)
leaq -0x284(%rbp), %rsi
callq 0x4a50
movq %rax, -0x400(%rbp)
jmp 0x3b0f
movq -0x400(%rbp), %rax
movq %rax, -0x280(%rbp)
leaq -0x278(%rbp), %rdi
leaq -0x280(%rbp), %rsi
callq 0x4a80
movq -0x3a8(%rbp), %rdi
addq $0x38, %rdi
callq 0x4ad0
movq %rax, -0x298(%rbp)
leaq -0x290(%rbp), %rdi
leaq -0x298(%rbp), %rsi
callq 0x4a80
leaq -0x278(%rbp), %rdi
leaq -0x290(%rbp), %rsi
callq 0x4aa0
testb $0x1, %al
jne 0x3b73
jmp 0x3ba4
leaq -0x278(%rbp), %rdi
callq 0x4b00
movq 0x8(%rax), %rsi
leaq -0x270(%rbp), %rdi
callq 0x3380
jmp 0x3b91
jmp 0x3bbb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x3e60
leaq 0x44b3(%rip), %rsi      # 0x805e
leaq -0x270(%rbp), %rdi
callq 0x3380
jmp 0x3bb9
jmp 0x3bbb
movl $0x0, -0x29c(%rbp)
leaq -0x2c0(%rbp), %rdi
callq 0x3390
movq -0x3a8(%rbp), %rdi
addq $0x68, %rdi
callq 0x4b20
movq %rax, -0x2c8(%rbp)
movl -0x29c(%rbp), %esi
leaq -0x308(%rbp), %rdi
callq 0x36e0
jmp 0x3bfc
leaq -0x2e8(%rbp), %rdi
leaq -0x270(%rbp), %rsi
leaq -0x308(%rbp), %rdx
callq 0x4b50
jmp 0x3c18
leaq -0x2c0(%rbp), %rdi
movq %rdi, -0x410(%rbp)
leaq -0x2e8(%rbp), %rsi
movq %rsi, -0x418(%rbp)
callq 0x3310
movq -0x418(%rbp), %rdi
callq 0x3160
leaq -0x308(%rbp), %rdi
callq 0x3160
movq -0x3a8(%rbp), %rax
movq -0x410(%rbp), %rsi
addq $0x68, %rax
movq %rax, -0x408(%rbp)
leaq -0x330(%rbp), %rdi
callq 0x3120
jmp 0x3c78
movq -0x408(%rbp), %rdi
leaq -0x330(%rbp), %rsi
callq 0x4ba0
movq %rax, -0x420(%rbp)
jmp 0x3c94
movq -0x420(%rbp), %rax
movq %rax, -0x310(%rbp)
movq -0x310(%rbp), %rax
movq %rax, -0x2c8(%rbp)
leaq -0x330(%rbp), %rdi
callq 0x3160
movl -0x29c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x29c(%rbp)
movq -0x3a8(%rbp), %rdi
addq $0x68, %rdi
callq 0x4b20
movq %rax, -0x338(%rbp)
leaq -0x2c8(%rbp), %rdi
leaq -0x338(%rbp), %rsi
callq 0x4bd0
testb $0x1, %al
jne 0x3be8
leaq -0x270(%rbp), %rdi
leaq -0x2c0(%rbp), %rsi
callq 0x3060
jmp 0x3d12
movq -0x3a8(%rbp), %rax
movq %rax, %rcx
addq $0x68, %rcx
movq %rcx, -0x428(%rbp)
movq 0x10(%rax), %rax
movslq -0x250(%rbp), %rcx
shlq $0x4, %rcx
leaq 0x100(%rax,%rcx), %rdx
leaq -0x378(%rbp), %rdi
leaq -0x270(%rbp), %rsi
callq 0x4c40
jmp 0x3d53
movq -0x428(%rbp), %rdi
leaq -0x378(%rbp), %rsi
callq 0x4c00
movb %dl, -0x431(%rbp)
movq %rax, -0x430(%rbp)
jmp 0x3d75
movb -0x431(%rbp), %al
movq -0x430(%rbp), %rcx
movq %rcx, -0x388(%rbp)
movb %al, -0x380(%rbp)
movq -0x388(%rbp), %rax
movq %rax, -0x348(%rbp)
movb -0x380(%rbp), %al
movb %al, -0x340(%rbp)
leaq -0x378(%rbp), %rdi
callq 0x4c90
leaq -0x348(%rbp), %rdi
callq 0x4cb0
movl 0x28(%rax), %ecx
addq $0x400, %rcx            # imm = 0x400
movl %ecx, 0x28(%rax)
leaq -0x2c0(%rbp), %rdi
callq 0x3160
leaq -0x270(%rbp), %rdi
callq 0x3160
movl -0x250(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x250(%rbp)
jmp 0x3a4b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x3e54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x308(%rbp), %rdi
callq 0x3160
jmp 0x3e54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x330(%rbp), %rdi
callq 0x3160
jmp 0x3e54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x378(%rbp), %rdi
callq 0x4c90
leaq -0x2c0(%rbp), %rdi
callq 0x3160
leaq -0x270(%rbp), %rdi
callq 0x3160
jmp 0x3e83
leaq -0x238(%rbp), %rdi
callq 0x30b0
addq $0x440, %rsp            # imm = 0x440
popq %rbp
retq
leaq -0x238(%rbp), %rdi
callq 0x30b0
movq -0x3b8(%rbp), %rdi
callq 0x4cd0
movq -0x3c0(%rbp), %rdi
callq 0x4cf0
movq -0x3a0(%rbp), %rdi
callq 0x3160
movq -0x20(%rbp), %rdi
callq 0x3410
nopl (%rax)