/**
 * htmlDocDumpMemoryFormat:
 * @cur:  the document
 * @mem:  OUT: the memory pointer
 * @size:  OUT: the memory length
 * @format:  should formatting spaces been added
 *
 * Dump an HTML document in memory and return the xmlChar * and it's size.
 * It's up to the caller to free the memory.
 */
void
htmlDocDumpMemoryFormat(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
    xmlOutputBufferPtr buf;
    xmlCharEncodingHandlerPtr handler = KD_NULL;
    const char *encoding;

    xmlInitParser();

    if ((mem == KD_NULL) || (size == KD_NULL))
        return;
    if (cur == KD_NULL) {
	*mem = KD_NULL;
	*size = 0;
	return;
    }

    encoding = (const char *) htmlGetMetaEncoding(cur);

    if (encoding != KD_NULL) {
	xmlCharEncoding enc;

	enc = xmlParseCharEncoding(encoding);
	if (enc != cur->charset) {
	    if (cur->charset != XML_CHAR_ENCODING_UTF8) {
		/*
		 * Not supported yet
		 */
		*mem = KD_NULL;
		*size = 0;
		return;
	    }

	    handler = xmlFindCharEncodingHandler(encoding);
	    if (handler == KD_NULL) {
		*mem = KD_NULL;
		*size = 0;
		return;
	    }
	} else {
	    handler = xmlFindCharEncodingHandler(encoding);
	}
    }

    /*
     * Fallback to HTML or ASCII when the encoding is unspecified
     */
    if (handler == KD_NULL)
	handler = xmlFindCharEncodingHandler("HTML");
    if (handler == KD_NULL)
	handler = xmlFindCharEncodingHandler("ascii");

    buf = xmlAllocOutputBufferInternal(handler);
    if (buf == KD_NULL) {
	*mem = KD_NULL;
	*size = 0;
	return;
    }

	htmlDocContentDumpFormatOutput(buf, cur, KD_NULL, format);

    xmlOutputBufferFlush(buf);
    if (buf->conv != KD_NULL) {
	*size = buf->conv->use;
	*mem = xmlStrndup(buf->conv->content, *size);
    } else {
	*size = buf->buffer->use;
	*mem = xmlStrndup(buf->buffer->content, *size);
    }
    (void)xmlOutputBufferClose(buf);
}
void
htmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
	                 const char *encoding) {
    htmlDocContentDumpFormatOutput(buf, cur, encoding, 1);
}