// File: tsfn[P]doc2txt/src/Storage.cpp
// Function: Storage::init(_IO_FILE*)
bool Storage::init(FILE *doc_file) {
  if (doc_file == NULL) {
    return false;
  }

  uchar *buf, *mbuf, *msat, *sat, *ssat, *dir_stream;
  uint buf_size, mbuf_size, msat_size, sat_size, ssat_size, dir_stream_size;

  if (!read_buf(doc_file, &buf, &buf_size)) return false;
  if (!read_msat(buf, buf_size, &msat, &msat_size)) return false;
  if (!read_sat(buf, buf_size, msat, msat_size, &sat, &sat_size)) return false;
  free(msat);

  /* Directory Stream */
  if (!read_stream(*(uint *)(buf + 0x30), 512, sat, sat_size,
        buf + 512, buf_size - 512, &dir_stream, &dir_stream_size)) {
    return false;
  }
  if (dir_stream_size < 128) return false;

  /* short-sector Stream's mbuf and ssat */
  if (!read_stream(*(uint *)(dir_stream + 0x74), 512, sat, sat_size,
        buf + 512, buf_size - 512, &mbuf, &mbuf_size)) {
    return false;
  }
  if (!read_stream(*(uint *)(buf + 0x3C), 512, sat, sat_size,
        buf + 512, buf_size - 512, &ssat, &ssat_size)) {
    return false;
  }

  /* all streams */
  uint _ulMiniSectorCutoff = *(uint *)(buf + 0x38);

  stream_num = dir_stream_size / 128 - 1;
  stream_name = (uchar **)malloc(stream_num * sizeof(uchar *));
  stream_table = (uchar **)malloc(stream_num * sizeof(uchar *));
  stream_sizes = (uint *)malloc(stream_num * sizeof(uint));

  memset(stream_name, 0, stream_num * sizeof(uchar *));
  memset(stream_table, 0, stream_num * sizeof(uchar *));
  memset(stream_sizes, 0, stream_num * sizeof(uint));

  for (uint i = 0; i < stream_num; ++i) {
    uchar *dir_entry = dir_stream + i * 128;
    stream_name[i] = (uchar *)malloc(64);
    memcpy(stream_name[i], dir_entry, 64);
    if (i == 0) continue;
    uint id = *(uint *)(dir_entry + 0x74);
    stream_sizes[i] = *(uint *)(dir_entry + 0x78);
    /*
    for (uint j = 0; j < 32; ++j) {
      printf("%c", stream_name[i][j * 2]);
    }
    printf("-> sz=%u, id=%u\n", stream_sizes[i], id);
    */
    if (stream_sizes[i] >= _ulMiniSectorCutoff) {
      if (!read_stream(id, 512, sat, sat_size,
            buf + 512, buf_size - 512, &stream_table[i], &stream_sizes[i])) {
        return false;
      }
    } else {
      if (!read_stream(id, 64, ssat, ssat_size,
            mbuf, mbuf_size, &stream_table[i], &stream_sizes[i])) {
        return false;
      }
    }
  }

  free(buf);
  free(mbuf);
  free(sat);
  free(ssat);
  free(dir_stream);
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x14fb
movb $0x0, -0x1(%rbp)
jmp 0x188e
movq -0x18(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x4c(%rbp), %rdx
callq 0x18a0
testb $0x1, %al
jne 0x1519
movb $0x0, -0x1(%rbp)
jmp 0x188e
movq -0x20(%rbp), %rdi
movl -0x4c(%rbp), %esi
leaq -0x30(%rbp), %rdx
leaq -0x54(%rbp), %rcx
callq 0x1990
testb $0x1, %al
jne 0x153a
movb $0x0, -0x1(%rbp)
jmp 0x188e
movq -0x20(%rbp), %rdi
movl -0x4c(%rbp), %esi
movq -0x30(%rbp), %rdx
movl -0x54(%rbp), %ecx
leaq -0x38(%rbp), %r8
leaq -0x58(%rbp), %r9
callq 0x1b80
testb $0x1, %al
jne 0x1562
movb $0x0, -0x1(%rbp)
jmp 0x188e
movq -0x30(%rbp), %rdi
callq 0x1160
movq -0x20(%rbp), %rax
movl 0x30(%rax), %edi
movq -0x38(%rbp), %rdx
movl -0x58(%rbp), %ecx
movq -0x20(%rbp), %r8
addq $0x200, %r8             # imm = 0x200
movl -0x4c(%rbp), %r9d
subl $0x200, %r9d            # imm = 0x200
movl $0x200, %esi            # imm = 0x200
leaq -0x48(%rbp), %r10
leaq -0x60(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1cb0
testb $0x1, %al
jne 0x15b7
movb $0x0, -0x1(%rbp)
jmp 0x188e
cmpl $0x80, -0x60(%rbp)
jae 0x15c9
movb $0x0, -0x1(%rbp)
jmp 0x188e
movq -0x48(%rbp), %rax
movl 0x74(%rax), %edi
movq -0x38(%rbp), %rdx
movl -0x58(%rbp), %ecx
movq -0x20(%rbp), %r8
addq $0x200, %r8             # imm = 0x200
movl -0x4c(%rbp), %r9d
subl $0x200, %r9d            # imm = 0x200
movl $0x200, %esi            # imm = 0x200
leaq -0x28(%rbp), %r10
leaq -0x50(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1cb0
testb $0x1, %al
jne 0x1615
movb $0x0, -0x1(%rbp)
jmp 0x188e
movq -0x20(%rbp), %rax
movl 0x3c(%rax), %edi
movq -0x38(%rbp), %rdx
movl -0x58(%rbp), %ecx
movq -0x20(%rbp), %r8
addq $0x200, %r8             # imm = 0x200
movl -0x4c(%rbp), %r9d
subl $0x200, %r9d            # imm = 0x200
movl $0x200, %esi            # imm = 0x200
leaq -0x40(%rbp), %r10
leaq -0x5c(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1cb0
testb $0x1, %al
jne 0x1661
movb $0x0, -0x1(%rbp)
jmp 0x188e
movq -0x80(%rbp), %rax
movq -0x20(%rbp), %rcx
movl 0x38(%rcx), %ecx
movl %ecx, -0x64(%rbp)
movl -0x60(%rbp), %ecx
shrl $0x7, %ecx
subl $0x1, %ecx
movl %ecx, (%rax)
movl (%rax), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x1180
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rcx, 0x8(%rax)
movl (%rax), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x1180
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rcx, 0x10(%rax)
movl (%rax), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x1180
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rcx, 0x18(%rax)
movq 0x8(%rax), %rdi
movl (%rax), %eax
movl %eax, %edx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x10b0
movq -0x80(%rbp), %rax
movq 0x10(%rax), %rdi
movl (%rax), %eax
movl %eax, %edx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x10b0
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rdi
movl (%rax), %eax
movl %eax, %edx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x10b0
movl $0x0, -0x68(%rbp)
movq -0x80(%rbp), %rcx
movl -0x68(%rbp), %eax
cmpl (%rcx), %eax
jae 0x185d
movq -0x48(%rbp), %rax
movl -0x68(%rbp), %ecx
shll $0x7, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movl $0x40, %edi
callq 0x1180
movq %rax, %rsi
movq -0x80(%rbp), %rax
movq 0x8(%rax), %rcx
movl -0x68(%rbp), %edx
movq %rsi, (%rcx,%rdx,8)
movq 0x8(%rax), %rax
movl -0x68(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
movq -0x70(%rbp), %rsi
movl $0x40, %edx
callq 0x10e0
cmpl $0x0, -0x68(%rbp)
jne 0x176c
jmp 0x184f
movq -0x80(%rbp), %rax
movq -0x70(%rbp), %rcx
movl 0x74(%rcx), %ecx
movl %ecx, -0x74(%rbp)
movq -0x70(%rbp), %rcx
movl 0x78(%rcx), %esi
movq 0x18(%rax), %rcx
movl -0x68(%rbp), %edx
movl %esi, (%rcx,%rdx,4)
movq 0x18(%rax), %rax
movl -0x68(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x64(%rbp), %eax
jb 0x17fc
movq -0x80(%rbp), %rax
movl -0x74(%rbp), %edi
movq -0x38(%rbp), %rdx
movl -0x58(%rbp), %ecx
movq -0x20(%rbp), %r8
addq $0x200, %r8             # imm = 0x200
movl -0x4c(%rbp), %r9d
subl $0x200, %r9d            # imm = 0x200
movq 0x10(%rax), %r10
movl -0x68(%rbp), %esi
shlq $0x3, %rsi
addq %rsi, %r10
movq 0x18(%rax), %rax
movl -0x68(%rbp), %esi
shlq $0x2, %rsi
addq %rsi, %rax
movl $0x200, %esi            # imm = 0x200
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1cb0
testb $0x1, %al
jne 0x17fa
movb $0x0, -0x1(%rbp)
jmp 0x188e
jmp 0x184d
movq -0x80(%rbp), %rax
movl -0x74(%rbp), %edi
movq -0x40(%rbp), %rdx
movl -0x5c(%rbp), %ecx
movq -0x28(%rbp), %r8
movl -0x50(%rbp), %r9d
movq 0x10(%rax), %r10
movl -0x68(%rbp), %esi
shlq $0x3, %rsi
addq %rsi, %r10
movq 0x18(%rax), %rax
movl -0x68(%rbp), %esi
shlq $0x2, %rsi
addq %rsi, %rax
movl $0x40, %esi
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1cb0
testb $0x1, %al
jne 0x184b
movb $0x0, -0x1(%rbp)
jmp 0x188e
jmp 0x184d
jmp 0x184f
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x170a
movq -0x20(%rbp), %rdi
callq 0x1160
movq -0x28(%rbp), %rdi
callq 0x1160
movq -0x38(%rbp), %rdi
callq 0x1160
movq -0x40(%rbp), %rdi
callq 0x1160
movq -0x48(%rbp), %rdi
callq 0x1160
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)