// File: /workspace/github_cpp_makefile_star2_O1/devinsmith[P]qvdreader/QvdFile.cpp
// Function: QvdFile::parseSymbolAndData()
bool QvdFile::parseSymbolAndData()
{
  for (std::vector<QvdField>::iterator it = _hdr.Fields.begin();
      it != _hdr.Fields.end(); ++it) {
    DEBUG(printf("Parsing symbols for %s, need to read %d symbols\n",
            it->FieldName.c_str(), it->NoOfSymbols););

    for (unsigned int i = 0; i < it->NoOfSymbols; i++) {
      unsigned char typeByte = static_cast<unsigned char>(readByte());
      QvdSymbol symbol;

      char stringByte;
      double d;

      symbol.Type = typeByte;
      switch (typeByte) {
      case 0x01: // INT (4 bytes)
        symbol.IntValue = readInt32();
        break;
      case 0x02: // NUMERIC (double 8 bytes)
        // XXX: Not endian safe.
        d = readDouble();
        symbol.DoubleValue = d;
        break;
      case 0x04: // String value (0 terminated)
        while ((stringByte = readByte()) != 0) {
          symbol.StringValue += stringByte;
        }
        break;
      case 0x05: // Dual (INT format) 4 bytes, followed by string format.
        symbol.IntValue = readInt32();
        while ((stringByte = readByte()) != 0) {
          symbol.StringValue += stringByte;
        }
        break;
      case 0x06: // DUAL (Double format) 8 bytes followed by string format.
        // XXX: Not endian safe.
        d = readDouble();
        symbol.DoubleValue = d;

        // Read string.
        while ((stringByte = readByte()) != 0) {
          symbol.StringValue += stringByte;
        }
        break;
      default:
        printf("Unknown byte: 0x%02x\n", typeByte);
      }

      it->Symbols.push_back(symbol);
    }
  }

  // Sort fields by BitOffset
  std::sort(_hdr.Fields.begin(), _hdr.Fields.end(), less_than_bitOffset());

  DEBUG(printf("Total number of rows: %d\n", _hdr.NoOfRecords););
  size_t rowNumber = 0;

  DEBUG(
   printf("[\n");
   printf("  {\n"););

  if (_bufLen == 0) {
    if (!_eof) {
      readBytes();
    } else {
      return 0; // throw an error.
    }
  }
  DEBUG(dump_hex(0, _dataPtrStart, _bufLen););

  for (rowNumber = 0; rowNumber < _hdr.NoOfRecords; rowNumber++) {
    DEBUG(printf("==== ROW: %d ====\n", (int)rowNumber););
    // Read first row now;
    for (std::vector<QvdField>::iterator it = _hdr.Fields.begin();
        it != _hdr.Fields.end(); ++it) {
      if (it->BitWidth > 0) {
        DEBUG(printf("Parsing data for %s (%d), need to read %d bits for this field\n",
                     it->FieldName.c_str(), it->BitOffset, it->BitWidth););

        int idx = get_bits_index(it->BitWidth);
        if (it->Bias != 0)
          idx += it->Bias;
        DEBUG(printf("> Index = %d\n", idx););
        _hdr.Indices.push_back(idx);
        DEBUG(
        if (idx == -2) {
          printf("NULL\n");
          continue;
        });

        DEBUG(
        if (it->Symbols.size() == 0) {
          printf("NULL\n");
          continue;
        });

        DEBUG(
        QvdSymbol sym = it->Symbols[idx];
        switch (sym.Type) {
        case 0x01:
          printf("%d\n", sym.IntValue);
          break;
        case 0x02:
          printf("%f\n", sym.DoubleValue);
          break;
        case 0x04:
          printf("%s\n", sym.StringValue.c_str());
          break;
        case 0x05:
          printf("%u (%s)\n", (unsigned int)sym.IntValue, sym.StringValue.c_str());
          break;
        case 0x06:
          printf("%f (%s)\n", sym.DoubleValue, sym.StringValue.c_str());
          break;
        default:
          printf("Unknown value\n");
          break;
        });
      }

    }
  }
  DEBUG(
  printf("  }\n");
  printf("]\n"););
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq 0x98(%rdi), %r15
movq 0xa0(%rdi), %r13
cmpq %r13, %r15
je 0x5657
leaq 0x18(%rsp), %r14
leaq 0x28(%rsp), %r12
cmpl $0x0, 0x98(%r15)
je 0x5640
leaq 0xa8(%r15), %r13
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x42b0
movq %r12, 0x18(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x20(%rsp)
movb $0x0, 0x28(%rsp)
movl $0x0, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movzbl %al, %esi
movl %esi, 0x10(%rsp)
leal -0x1(%rsi), %eax
cmpl $0x5, %eax
ja 0x5565
leaq 0x4beb(%rip), %rcx      # 0xa120
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
callq 0x42a0
movl %eax, 0x38(%rsp)
jmp 0x559f
movq %rbx, %rdi
callq 0x42b0
testb %al, %al
je 0x559f
movsbl %al, %esi
movq %r14, %rdi
callq 0x42c0
jmp 0x554c
leaq 0x4be7(%rip), %rdi      # 0xa153
xorl %eax, %eax
callq 0x4030
jmp 0x559f
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x42b0
movb %al, 0x8(%rsp,%r12)
incq %r12
cmpq $0x8, %r12
jne 0x5578
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
leaq 0x28(%rsp), %r12
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
callq 0x42d0
movq 0x18(%rsp), %rdi
cmpq %r12, %rdi
je 0x55c3
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4370
incl %ebp
cmpl 0x98(%r15), %ebp
jb 0x54f9
jmp 0x5640
movq %rbx, %rdi
callq 0x42a0
movl %eax, 0x38(%rsp)
movq %rbx, %rdi
callq 0x42b0
testb %al, %al
je 0x559f
movsbl %al, %esi
movq %r14, %rdi
callq 0x42c0
jmp 0x55e0
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x42b0
movb %al, 0x8(%rsp,%r12)
incq %r12
cmpq $0x8, %r12
jne 0x55fc
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
leaq 0x28(%rsp), %r12
movq %rbx, %rdi
callq 0x42b0
testb %al, %al
je 0x559f
movsbl %al, %esi
movq %r14, %rdi
callq 0x42c0
jmp 0x5623
addq $0xc0, %r15
movq 0xa0(%rbx), %r13
cmpq %r13, %r15
jne 0x54e2
movq 0x98(%rbx), %r14
cmpq %r13, %r14
je 0x569e
movq %r13, %rax
subq %r14, %rax
sarq $0x6, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rcx
bsrq %rcx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0x41e0
movq %r14, %rdi
movq %r13, %rsi
callq 0x4350
cmpq $0x0, 0x2138(%rbx)
jne 0x56f5
cmpb $0x0, 0x2140(%rbx)
je 0x56b8
xorl %ebp, %ebp
jmp 0x578c
leaq 0x130(%rbx), %r14
movq 0x128(%rbx), %rcx
movl $0x1, %esi
movl $0x2000, %edx           # imm = 0x2000
movq %r14, %rdi
callq 0x44c0
cmpq $0x1fff, %rax           # imm = 0x1FFF
ja 0x56e7
movb $0x1, 0x2140(%rbx)
movq %r14, 0x2130(%rbx)
movq %rax, 0x2138(%rbx)
movb $0x1, %bpl
cmpl $0x0, 0x8c(%rbx)
je 0x578c
leaq 0xc8(%rbx), %r14
xorl %r12d, %r12d
leaq 0x10(%rsp), %r15
movq 0x98(%rbx), %r13
cmpq 0xa0(%rbx), %r13
je 0x577e
movl 0x24(%r13), %esi
testq %rsi, %rsi
je 0x5775
movq %rbx, %rdi
callq 0x4520
movl %eax, 0x10(%rsp)
movl 0x28(%r13), %ecx
testl %ecx, %ecx
je 0x5747
addl %eax, %ecx
movl %ecx, 0x10(%rsp)
movq 0xd0(%rbx), %rsi
cmpq 0xd8(%rbx), %rsi
je 0x576a
movl 0x10(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xd0(%rbx)
jmp 0x5775
movq %r14, %rdi
movq %r15, %rdx
callq 0x40f0
addq $0xc0, %r13
jmp 0x571b
incq %r12
movl 0x8c(%rbx), %eax
cmpq %rax, %r12
jb 0x5714
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x57a3
jmp 0x57a3
jmp 0x57a3
movq %rax, %rbx
movq 0x18(%rsp), %rdi
cmpq %r12, %rdi
je 0x57bd
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4370
movq %rbx, %rdi
callq 0x45c0
nop