// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
cmpl $0x1, %edi
jg 0x300b
movq (%r14), %rsi
leaq 0x3113(%rip), %rdi      # 0x60fe
xorl %eax, %eax
callq 0x2050
movq (%r14), %rsi
leaq 0x3127(%rip), %rdi      # 0x6123
xorl %eax, %eax
callq 0x2050
pushq $0x1
popq %rbx
jmp 0x3405
movl %edi, %ebp
movq 0x8(%r14), %rsi
leaq 0xa8(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x378a
cmpl $0x3, %ebp
jne 0x306a
movq 0x10(%r14), %rsi
leaq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x378a
xorl %r15d, %r15d
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x393d
movq %rax, %r12
leaq 0x48(%rsp), %rdi
callq 0x20b0
movq $0x0, 0x8(%rsp)
xorl %r13d, %r13d
jmp 0x30ee
xorl %r12d, %r12d
movl $0x0, %eax
movq %rax, 0x8(%rsp)
movl $0x0, %r15d
movl $0x0, %r13d
cmpl $0x4, %ebp
jb 0x30ee
movq 0x10(%r14), %rsi
leaq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x378a
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x393d
movq %rax, %r15
leaq 0x48(%rsp), %rdi
callq 0x20b0
movq 0x18(%r14), %rsi
leaq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x378a
xorl %r12d, %r12d
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x393d
movq %rax, %r13
leaq 0x48(%rsp), %rdi
callq 0x20b0
movb $0x1, %al
movq %rax, 0x8(%rsp)
leaq 0x305b(%rip), %rdi      # 0x6150
xorl %eax, %eax
callq 0x2050
movq 0x8eb5(%rip), %rbx      # 0xbfb8
movq (%rbx), %rdi
callq 0x2180
leaq 0x10(%rsp), %rsi
andq $0x0, (%rsi)
leaq 0xa8(%rsp), %rdi
callq 0x23bc
testq %rax, %rax
je 0x31c4
movq %rax, %r14
movq 0x10(%rsp), %rax
movb $0x0, (%r14,%rax)
leaq 0x5385(%rip), %rdi      # 0x84c3
callq 0x2220
leaq 0x301a(%rip), %rdi      # 0x6164
xorl %eax, %eax
callq 0x2050
movq (%rbx), %rdi
callq 0x2180
leaq 0x48(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x395a
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0x40ac
cmpl $0x0, 0x98(%rsp)
je 0x31cc
leaq 0x5354(%rip), %rdi      # 0x84de
callq 0x2220
movl 0x98(%rsp), %edi
callq 0x39d5
movq 0xa0(%rsp), %rdx
leaq 0x2fce(%rip), %rdi      # 0x6178
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
callq 0x21a0
pushq $0x3
popq %rbx
jmp 0x33ee
pushq $0x2
popq %rbx
jmp 0x33f8
leaq 0x52f0(%rip), %rdi      # 0x84c3
callq 0x2220
movq %r14, %rdi
callq 0x21a0
cmpl $0x3, %ebp
jne 0x31f8
leaq 0x2f9d(%rip), %rdi      # 0x6189
movl %r12d, %esi
xorl %eax, %eax
callq 0x2050
jmp 0x3213
cmpb $0x0, 0x8(%rsp)
je 0x3213
leaq 0x2fac(%rip), %rdi      # 0x61b2
movl %r15d, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x2050
movq %r13, 0x30(%rsp)
movq %r12, 0x40(%rsp)
movq %r15, 0x38(%rsp)
movl %ebp, 0x24(%rsp)
leaq 0x529b(%rip), %rdi      # 0x84c8
callq 0x2220
leaq 0x18(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x39f4
leaq 0x18(%rsp), %rax
movq (%rax), %r13
leaq 0x48(%rsp), %rbp
leaq 0x2e3c(%rip), %r14      # 0x6091
movq $0x0, 0x28(%rsp)
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x3a30
cmpq 0x18(%rsp), %r13
je 0x33d9
leaq 0x10(%r13), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x37c6
testb %al, %al
je 0x33d0
movq %r12, %rdi
leaq 0x2dff(%rip), %rsi      # 0x6096
callq 0x37c6
testb %al, %al
je 0x33d0
movq %r12, %rdi
leaq 0x39aa(%rip), %rsi      # 0x6c58
callq 0x37c6
testb %al, %al
je 0x33d0
cmpl $0x3, 0x24(%rsp)
jne 0x32e4
movq %r12, %rdi
leaq 0x398c(%rip), %rsi      # 0x6c58
callq 0x3ef6
movq %rax, %rdi
callq 0x3914
cmpl 0x40(%rsp), %eax
je 0x332d
jmp 0x33d0
cmpb $0x0, 0x8(%rsp)
je 0x332d
movq %r12, %rdi
leaq 0x3963(%rip), %rsi      # 0x6c58
callq 0x3ef6
movq %rax, %rdi
callq 0x3914
cmpl 0x38(%rsp), %eax
jb 0x33d0
movq %r12, %rdi
leaq 0x3942(%rip), %rsi      # 0x6c58
callq 0x3ef6
movq %rax, %rdi
callq 0x3914
cmpl 0x30(%rsp), %eax
ja 0x33d0
movq %r12, %rdi
leaq 0x2d5f(%rip), %rsi      # 0x6096
callq 0x3ef6
movq %rax, %rdi
callq 0x37ec
movq %rax, %r15
movq %r12, %rdi
movq %r14, %rsi
callq 0x3ef6
movq %rax, %rdi
callq 0x37ec
movq %rax, %rbx
movq %r12, %rdi
leaq 0x38f1(%rip), %rsi      # 0x6c58
callq 0x3ef6
movq %rax, %rdi
callq 0x3914
leaq 0x2e6d(%rip), %rdi      # 0x61e8
movq %r15, %rsi
movq %rbx, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x2050
movq %r12, %rdi
leaq 0x2d51(%rip), %rsi      # 0x60e5
callq 0x37c6
testb %al, %al
je 0x33bf
movq %r12, %rdi
leaq 0x2d3e(%rip), %rsi      # 0x60e5
callq 0x3ef6
movq %rbp, %rdi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2a86
incq 0x28(%rsp)
leaq 0x50fe(%rip), %rdi      # 0x84c9
callq 0x2220
addq $0x20, %r13
jmp 0x325e
leaq 0x2e1c(%rip), %rdi      # 0x61fc
xorl %ebx, %ebx
movq 0x28(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
leaq 0x48(%rsp), %rdi
callq 0x3a76
leaq 0xa8(%rsp), %rdi
callq 0x20b0
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x343d
jmp 0x3423
jmp 0x3436
jmp 0x3423
jmp 0x3436
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x20b0
jmp 0x344a
jmp 0x3436
jmp 0x343d
movq %rax, %rbx
jmp 0x344a
jmp 0x343d
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x3a76
leaq 0xa8(%rsp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x2250
nop