// File: /workspace/github_cpp_makefile_star2_O1/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 0x4150
movq %rax, %r14
leaq 0x130(%rbx), %r15
movq 0x128(%rbx), %rcx
movl $0x1, %esi
movl $0x2000, %edx           # imm = 0x2000
movq %r15, %rdi
callq 0x44c0
movq %rax, %r13
cmpq $0x1fff, %rax           # imm = 0x1FFF
ja 0x52d9
movb $0x1, 0x2140(%rbx)
xorl %ebp, %ebp
cmpq $0x2000, %r13           # imm = 0x2000
setb %r12b
movq %r15, 0x2130(%rbx)
movq %r13, 0x2138(%rbx)
movq %r15, %rdi
leaq 0x4e6b(%rip), %rsi      # 0xa169
callq 0x4070
testq %rax, %rax
je 0x532a
addq $0x11, %rax
movq %rax, 0x2130(%rbx)
subq %r15, %rax
subq %rax, %r13
movq %r13, 0x2138(%rbx)
movl $0x1, %ebp
movq %rax, %r13
jmp 0x532d
movb %r12b, %bpl
movq %r14, %rdi
movq %r15, %rsi
movl %r13d, %edx
movl %ebp, %ecx
callq 0x41c0
testl %eax, %eax
jne 0x53b2
testl %ebp, %ebp
je 0x52ae
movq 0x10(%r14), %r15
testq %r15, %r15
je 0x53b7
cmpl $0x0, 0x18(%r14)
je 0x53b7
movq %r15, %rdi
callq 0x44e0
testq %rax, %rax
je 0x539e
movq %rax, %r12
leaq 0x4e2e(%rip), %r13      # 0xa19e
cmpl $0x1, 0x8(%r12)
jne 0x5394
movq 0x10(%r12), %rdi
movq %r13, %rsi
callq 0x4470
testl %eax, %eax
jne 0x5394
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4170
movq 0x30(%r12), %r12
testq %r12, %r12
jne 0x5370
movq %r15, %rdi
callq 0x45f0
movq %r14, %rdi
callq 0x4180
movb $0x1, %al
jmp 0x53b9
callq 0x46e8
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq