# HG changeset patch
# User Nick Wellnhofer <wellnhofer@aevum.de>
# Date 1661005174 -7200
#      Sat Aug 20 16:19:34 2022 +0200
# Node ID 8e47bfa4203382266b82597804b6396bc0d8e44b
# Parent  cb2671655e2a547dd0dfab8dfa44d34ac5c15528
Fix xmlCtxtReadDoc with encoding

xmlCtxtReadDoc used to create an input stream involving
xmlNewStringInputStream. This would create a stream without an input
buffer, causing problems with encodings (see #34).

After commit aab584dc3, an error was returned even with UTF-8 encodings
which happened to work before.

Make xmlCtxtReadDoc call xmlCtxtReadMemory which doesn't suffer from
these issues. Also fix htmlCtxtReadDoc.

Fixes #397.

diff --git a/HTMLparser.c b/HTMLparser.c
--- a/HTMLparser.c
+++ b/HTMLparser.c
@@ -7116,22 +7116,13 @@
 htmlCtxtReadDoc(htmlParserCtxtPtr ctxt, const xmlChar * cur,
                const char *URL, const char *encoding, int options)
 {
-    xmlParserInputPtr stream;
+    const char *buf;
 
     if (cur == NULL)
         return (NULL);
-    if (ctxt == NULL)
-        return (NULL);
-    xmlInitParser();
-
-    htmlCtxtReset(ctxt);
-
-    stream = xmlNewStringInputStream(ctxt, cur);
-    if (stream == NULL) {
-        return (NULL);
-    }
-    inputPush(ctxt, stream);
-    return (htmlDoRead(ctxt, URL, encoding, options, 1));
+    buf = (const char *) cur;
+    return (htmlCtxtReadMemory(ctxt, buf, strlen(buf), URL, encoding,
+                               options));
 }
 
 /**
diff --git a/parser.c b/parser.c
--- a/parser.c
+++ b/parser.c
@@ -15359,22 +15359,12 @@
 xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur,
                const char *URL, const char *encoding, int options)
 {
-    xmlParserInputPtr stream;
+    const char *buf;
 
     if (cur == NULL)
         return (NULL);
-    if (ctxt == NULL)
-        return (NULL);
-    xmlInitParser();
-
-    xmlCtxtReset(ctxt);
-
-    stream = xmlNewStringInputStream(ctxt, cur);
-    if (stream == NULL) {
-        return (NULL);
-    }
-    inputPush(ctxt, stream);
-    return (xmlDoRead(ctxt, URL, encoding, options, 1));
+    buf = (const char *) cur;
+    return (xmlCtxtReadMemory(ctxt, buf, strlen(buf), URL, encoding, options));
 }
 
 /**