// File: /workspace/github_cpp_makefile_star2_O1/DimitriFourny[P]uaf_replacer/main.cc
// Function: main
int main(int argc, char* argv[]) {
  if (argc < 2) {
    printf("Usage: %s <json_path> [object_size]\n", argv[0]);
    printf("       %s <json_path> <min_size> <max_size>\n", argv[0]);
    return 1;
  }

  std::string filepath = argv[1];
  bool search_exact_size = false;
  unsigned int target_object_size = 0;
  bool search_range_size = false;
  unsigned int min_size = 0, max_size = 0;
  if (argc == 3) {
    search_exact_size = true;
    target_object_size = std::stoul(argv[2], nullptr, 0);
  } else if (argc > 3) {
    search_range_size = true;
    min_size = std::stoul(argv[2], nullptr, 0);
    max_size = std::stoul(argv[3], nullptr, 0);
  }

  printf("Mapping the file...");
  fflush(stdout);
  size_t filesize = 0;
  char* memfile = map_file(filepath, filesize);
  if (!memfile) {
    return 2;
  }
  memfile[filesize] = 0;
  printf("done\n");

  printf("Mapping the JSON...");
  fflush(stdout);
  rapidjson::Document document;
  document.Parse(memfile);
  if (document.HasParseError()) {
    printf("parsing error\n");
    printf("Error: %s (%zd)\n", GetParseError_En(document.GetParseError()), 
      document.GetErrorOffset());
    free(memfile);
    return 3;
  }
  printf("done\n");
  free(memfile);

  if (search_exact_size) {
    printf("Searching objects of size 0x%x bytes...\n", target_object_size);
  } else if (search_range_size) {
    printf("Searching objects in the range [0x%x, 0x%x] bytes...\n", 
      min_size, max_size);
  }
  printf("\n--------------------\n");

  size_t nb_found = 0;
  for (rapidjson::Value::ConstMemberIterator itr = document.MemberBegin(); 
       itr != document.MemberEnd(); ++itr) {
    if (!itr->value.HasMember("name") || !itr->value.HasMember("type")
        || !itr->value.HasMember("size")) {
      continue; 
    }

    if (search_exact_size) {
      if (itr->value["size"].GetUint() != target_object_size) {
        continue;
      }
    } else if (search_range_size) {
      if (itr->value["size"].GetUint() < min_size ||
          itr->value["size"].GetUint() > max_size) {
        continue;
      }
    }

    printf("%s %s (0x%x bytes)\n", itr->value["type"].GetString(), 
      itr->value["name"].GetString(), itr->value["size"].GetUint());
    nb_found++;
    
    if (itr->value.HasMember("members")) {
      dump_members(document, itr->value, itr->value["members"], 0, 0);
    }
    printf("--------------------\n");
  }

  printf("\n%zu objects found\n", nb_found);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %r15
cmpl $0x1, %edi
jg 0x36d8
movq (%r15), %rsi
leaq 0x3a48(%rip), %rdi      # 0x70fe
xorl %eax, %eax
callq 0x2050
movq (%r15), %rsi
leaq 0x3a5c(%rip), %rdi      # 0x7123
xorl %eax, %eax
callq 0x2050
movl $0x1, %ebx
jmp 0x3caa
movl %edi, %ebx
movq 0x8(%r15), %rsi
leaq 0xa8(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x40f0
cmpl $0x3, %ebx
movl %ebx, 0x4(%rsp)
jne 0x3762
movq 0x10(%r15), %rsi
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x40f0
movq 0x40(%rsp), %r15
callq 0x2040
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
leaq 0x18(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2090
movq %rax, 0x8(%rsp)
cmpq %r15, (%r12)
je 0x3cdd
movl (%r14), %eax
testl %eax, %eax
je 0x37e6
cmpl $0x22, %eax
jne 0x37e9
leaq 0x3ab3(%rip), %rdi      # 0x7210
callq 0x21f0
movq $0x0, 0x8(%rsp)
movl $0x0, %ebp
movl $0x0, %r12d
movl $0x0, %r13d
cmpl $0x4, %ebx
jl 0x38b3
movq 0x10(%r15), %rsi
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x40f0
movq 0x40(%rsp), %r12
callq 0x2040
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
leaq 0x18(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x2090
movq %rax, 0x28(%rsp)
cmpq %r12, (%r13)
je 0x3ce9
movl (%r14), %eax
testl %eax, %eax
je 0x3811
cmpl $0x22, %eax
jne 0x3814
leaq 0x3a2f(%rip), %rdi      # 0x7210
callq 0x21f0
movl %ebp, (%r14)
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3804
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2140
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r13d, %r13d
jmp 0x38b3
movl %ebp, (%r14)
leaq 0x50(%rsp), %rbx
movq -0x10(%rbx), %rdi
cmpq %rbx, %rdi
je 0x382f
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2140
movq 0x18(%r15), %rsi
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x40f0
movq 0x40(%rsp), %r15
movl (%r14), %ebp
movl $0x0, (%r14)
leaq 0x18(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2090
cmpq %r15, (%r12)
je 0x3cf5
movq %rax, %r13
movl (%r14), %eax
testl %eax, %eax
je 0x3888
cmpl $0x22, %eax
jne 0x388b
leaq 0x398d(%rip), %rdi      # 0x7210
callq 0x21f0
movl %ebp, (%r14)
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x38a2
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2140
movb $0x1, %bpl
movq $0x0, 0x8(%rsp)
movq 0x28(%rsp), %r12
leaq 0x3896(%rip), %rdi      # 0x7150
xorl %eax, %eax
callq 0x2050
movq 0x96f8(%rip), %rbx      # 0xcfc0
movq (%rbx), %rdi
callq 0x2190
leaq 0x10(%rsp), %rsi
movq $0x0, (%rsi)
leaq 0xa8(%rsp), %rdi
callq 0x263c
testq %rax, %rax
je 0x397d
movq %rax, %r14
movq 0x10(%rsp), %rax
movb $0x0, (%r14,%rax)
leaq 0x5b48(%rip), %rdi      # 0x944e
callq 0x2220
leaq 0x3852(%rip), %rdi      # 0x7164
xorl %eax, %eax
callq 0x2050
movq (%rbx), %rdi
callq 0x2190
leaq 0x40(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x42fc
leaq 0x18(%rsp), %rsi
movq %r14, (%rsi)
movq %r14, 0x8(%rsi)
leaq 0x40(%rsp), %rdi
callq 0x4936
cmpl $0x0, 0x90(%rsp)
je 0x3987
leaq 0x5b0e(%rip), %rdi      # 0x9469
callq 0x2220
movl 0x90(%rsp), %eax
cmpq $0x11, %rax
ja 0x39b9
leaq 0x6694(%rip), %rcx      # 0xa008
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
jmp 0x39c0
movl $0x2, %ebx
jmp 0x3c89
leaq 0x5ac0(%rip), %rdi      # 0x944e
callq 0x2220
movq %r14, %rdi
callq 0x21b0
movl 0x4(%rsp), %eax
cmpl $0x3, %eax
jne 0x39e8
leaq 0x37de(%rip), %rdi      # 0x7189
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
jmp 0x3a01
leaq 0x3ad0(%rip), %rsi      # 0x7490
movq 0x98(%rsp), %rdx
leaq 0x37a9(%rip), %rdi      # 0x7178
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
callq 0x21b0
movl $0x3, %ebx
jmp 0x3c7f
testb %bpl, %bpl
je 0x3a01
leaq 0x37be(%rip), %rdi      # 0x71b2
movl %r12d, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x2050
movl %ebp, 0x34(%rsp)
movq %r13, 0xa0(%rsp)
movq %r12, 0x28(%rsp)
leaq 0x5a3a(%rip), %rdi      # 0x9453
callq 0x2220
cmpw $0x3, 0x4e(%rsp)
jne 0x3d01
cmpw $0x3, 0x4e(%rsp)
jne 0x3c4b
movabsq $0xffffffffffff, %rax   # imm = 0xFFFFFFFFFFFF
leaq 0x40(%rsp), %rcx
movq 0x8(%rcx), %rbx
andq %rax, %rbx
leaq 0x363e(%rip), %rbp      # 0x7091
leaq 0x18(%rsp), %r12
movq $0x0, 0x38(%rsp)
movq 0x48(%rsp), %rax
movabsq $0xffffffffffff, %rcx   # imm = 0xFFFFFFFFFFFF
andq %rcx, %rax
movl 0x40(%rsp), %ecx
shlq $0x5, %rcx
addq %rax, %rcx
cmpq %rcx, %rbx
je 0x3c6a
leaq 0x10(%rbx), %r13
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x44fa
movq 0x18(%rsp), %r14
movq %r13, %rdi
callq 0x44ba
cmpq %rax, %r14
je 0x3c3b
movq %r12, %rdi
movq %r13, %rsi
leaq 0x35da(%rip), %rdx      # 0x7096
callq 0x44fa
movq 0x18(%rsp), %r14
movq %r13, %rdi
callq 0x44ba
cmpq %rax, %r14
je 0x3c3b
movq %r12, %rdi
movq %r13, %rsi
leaq 0x4174(%rip), %rdx      # 0x7c58
callq 0x44fa
movq 0x18(%rsp), %r14
movq %r13, %rdi
callq 0x44ba
cmpq %rax, %r14
je 0x3c3b
cmpl $0x3, 0x4(%rsp)
jne 0x3b2d
movq %r13, %rdi
leaq 0x4148(%rip), %rsi      # 0x7c58
callq 0x473c
testb $0x40, 0xe(%rax)
je 0x3cbe
movq 0x8(%rsp), %rcx
cmpl %ecx, (%rax)
je 0x3b83
jmp 0x3c3b
cmpb $0x0, 0x34(%rsp)
je 0x3b83
movq %r13, %rdi
leaq 0x411a(%rip), %rsi      # 0x7c58
callq 0x473c
testb $0x40, 0xe(%rax)
je 0x3cbe
movq 0x28(%rsp), %rcx
cmpl %ecx, (%rax)
jb 0x3c3b
movq %r13, %rdi
leaq 0x40f4(%rip), %rsi      # 0x7c58
callq 0x473c
testb $0x40, 0xe(%rax)
je 0x3cbe
movq 0xa0(%rsp), %rcx
cmpl %ecx, (%rax)
ja 0x3c3b
movq %r13, %rdi
leaq 0x3509(%rip), %rsi      # 0x7096
callq 0x473c
movq %rax, %rdi
callq 0x4160
movq %rax, %r15
movq %r13, %rdi
movq %rbp, %rsi
callq 0x473c
movq %rax, %rdi
callq 0x4160
movq %rax, %r14
movq %r13, %rdi
leaq 0x409b(%rip), %rsi      # 0x7c58
callq 0x473c
testb $0x40, 0xe(%rax)
je 0x3cbe
movl (%rax), %ecx
leaq 0x3613(%rip), %rdi      # 0x71e8
movq %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2050
movq %r12, %rdi
movq %r13, %rsi
leaq 0x34f6(%rip), %rdx      # 0x70e5
callq 0x44fa
movq 0x18(%rsp), %r14
movq %r13, %rdi
callq 0x44ba
cmpq %rax, %r14
je 0x3c2a
movq %r13, %rdi
leaq 0x34d5(%rip), %rsi      # 0x70e5
callq 0x473c
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3083
incq 0x38(%rsp)
leaq 0x581e(%rip), %rdi      # 0x9454
callq 0x2220
addq $0x20, %rbx
cmpw $0x3, 0x4e(%rsp)
je 0x3a61
leaq 0x3905(%rip), %rdi      # 0x7557
leaq 0x3879(%rip), %rsi      # 0x74d2
leaq 0x5749(%rip), %rcx      # 0x93a9
movl $0x492, %edx            # imm = 0x492
callq 0x20e0
leaq 0x358b(%rip), %rdi      # 0x71fc
xorl %ebx, %ebx
movq 0x38(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
leaq 0x40(%rsp), %rdi
callq 0x4384
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3caa
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2140
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3f98(%rip), %rdi      # 0x7c5d
leaq 0x3806(%rip), %rsi      # 0x74d2
leaq 0x3fa4(%rip), %rcx      # 0x7c77
movl $0x6ca, %edx            # imm = 0x6CA
callq 0x20e0
leaq 0x352c(%rip), %rdi      # 0x7210
callq 0x20d0
leaq 0x3520(%rip), %rdi      # 0x7210
callq 0x20d0
leaq 0x3514(%rip), %rdi      # 0x7210
callq 0x20d0
leaq 0x384f(%rip), %rdi      # 0x7557
leaq 0x37c3(%rip), %rsi      # 0x74d2
leaq 0x55fd(%rip), %rcx      # 0x9313
movl $0x48f, %edx            # imm = 0x48F
callq 0x20e0
jmp 0x3d42
jmp 0x3d42
movq %rax, %r15
cmpl $0x0, (%r14)
jne 0x3d30
movl %ebp, (%r14)
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
jne 0x3d61
jmp 0x3d7d
jmp 0x3d47
jmp 0x3d42
jmp 0x3d70
movq %rax, %r15
jmp 0x3d7d
movq %rax, %r15
cmpl $0x0, (%r14)
jne 0x3d53
movl %ebp, (%r14)
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3d7d
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2140
jmp 0x3d7d
movq %rax, %r15
leaq 0x40(%rsp), %rdi
callq 0x4384
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3d9e
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2140
movq %r15, %rdi
callq 0x2250