// File: /workspace/github_cpp_makefile_star2_O2/devinsmith[P]qvdreader/QvdFile.cpp
// Function: QvdFile::parseXmlHeader(char const*)
bool QvdFile::parseXmlHeader(const char *filename)
{
  const char *endQvdTableHeader = "</QvdTableHeader>";
  xmlParserCtxtPtr ctxt;

  ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, filename);

  // Read XML content of the file.
  int done;
  do {
    size_t len = readBytes();
    done = len < sizeof(_buf); // Did we reach end of file?

    // Unfortunately the Qvd file format includes an XML based
    // header followed by raw binary data.
    // We know that it occurs after </QvdTableHeader> so we need
    // to scan our read buffer each time to see if we have that tag.
    //
    // If we do, we alter the length read, set a pointer to the data just
    // after the tag and proceed to finish reading the XML portion of the
    // file.

    char *end;
    if ((end = strstr(_buf, endQvdTableHeader)) != NULL) {
      end += strlen(endQvdTableHeader);
      // For the XML portion, the dataPtrStart marks the end of the XML
      // and the start of raw data to be parsed later on.
      //
      // We treat this as an "End of File" situation for expat.
      _dataPtrStart = end;
      _bufLen = len;
      len = end - _buf;
      _bufLen -= len;
      done = 1;
    }

    if (xmlParseChunk(ctxt, _buf, len, done) != 0) {
      fprintf(stderr, "An error occurred parsing the XML\n");
      return false;
#if 0
      fprintf(stderr, "%s at line %lu\n",
        XML_ErrorString(XML_GetErrorCode(parser)),
        XML_GetCurrentLineNumber(parser));
#endif
    }
  } while (!done);

  if (ctxt->myDoc == NULL || !ctxt->wellFormed) {
    // XML header is not well formed.
    return false;
  }

  xmlDoc *doc = ctxt->myDoc; /* the resulting document tree */
  xmlNode *root_element = xmlDocGetRootElement(doc);

  for (xmlNode *node = root_element; node; node = node->next) {
    if (node->type == XML_ELEMENT_NODE &&
        strcmp((char *)node->name, "QvdTableHeader") == 0) {
      _hdr.ParseXml(node);
    }
  }

  xmlFreeDoc(doc);
  xmlFreeParserCtxt(ctxt);
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r8
movq %rdi, %rbx
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x5220
movq %rax, %r14
leaq 0x130(%rbx), %r15
movq %rbx, %rdi
callq 0x5830
movq %rax, %r13
xorl %ebp, %ebp
cmpq $0x2000, %rax           # imm = 0x2000
setb %r12b
movq %r15, %rdi
leaq 0x2dc6(%rip), %rsi      # 0x9151
callq 0x50f0
testq %rax, %rax
je 0x63b5
addq $0x11, %rax
movq %rax, 0x2130(%rbx)
subq %r15, %rax
subq %rax, %r13
movq %r13, 0x2138(%rbx)
movq %rax, %r13
pushq $0x1
popq %rbp
jmp 0x63b8
movb %r12b, %bpl
movq %r14, %rdi
movq %r15, %rsi
movl %r13d, %edx
movl %ebp, %ecx
callq 0x52a0
testl %eax, %eax
jne 0x6436
testl %ebp, %ebp
je 0x636a
movq 0x10(%r14), %r15
testq %r15, %r15
je 0x6452
cmpl $0x0, 0x18(%r14)
je 0x6452
movq %r15, %rdi
callq 0x5690
movq %rax, %r12
leaq 0x2d94(%rip), %r13      # 0x9186
testq %r12, %r12
je 0x6422
cmpl $0x1, 0x8(%r12)
jne 0x641b
movq 0x10(%r12), %rdi
movq %r13, %rsi
callq 0x5620
testl %eax, %eax
jne 0x641b
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5240
movq 0x30(%r12), %r12
jmp 0x63f2
movq %r15, %rdi
callq 0x5800
movq %r14, %rdi
callq 0x5260
movb $0x1, %al
jmp 0x6454
movq 0x5b9b(%rip), %rax      # 0xbfd8
movq (%rax), %rcx
leaq 0x2d1c(%rip), %rdi      # 0x9163
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x5850
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop