rolled back to 2.9.1 because 2.9.2 doesn't work with connecting net
[platform/upstream/libxml2.git] / xmlreader.c
index 77ac71b..00083d0 100644 (file)
@@ -44,6 +44,8 @@
 #include <libxml/pattern.h>
 #endif
 
+#include "buf.h"
+
 #define MAX_ERR_MSG_SIZE 64000
 
 /*
@@ -135,7 +137,7 @@ struct _xmlTextReader {
     int                                depth;  /* depth of the current node */
     xmlNodePtr                 faketext;/* fake xmlNs chld */
     int                                preserve;/* preserve the resulting document */
-    xmlBufferPtr               buffer; /* used to return const xmlChar * */
+    xmlBufPtr                  buffer; /* used to return const xmlChar * */
     xmlDictPtr                 dict;   /* the context dictionnary */
 
     /* entity stack when traversing entities content */
@@ -807,9 +809,10 @@ xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
  */
 static int
 xmlTextReaderPushData(xmlTextReaderPtr reader) {
-    xmlBufferPtr inbuf;
+    xmlBufPtr inbuf;
     int val, s;
     xmlTextReaderState oldstate;
+    int alloc;
 
     if ((reader->input == NULL) || (reader->input->buffer == NULL))
        return(-1);
@@ -817,17 +820,18 @@ xmlTextReaderPushData(xmlTextReaderPtr reader) {
     oldstate = reader->state;
     reader->state = XML_TEXTREADER_NONE;
     inbuf = reader->input->buffer;
+    alloc = xmlBufGetAllocationScheme(inbuf);
 
     while (reader->state == XML_TEXTREADER_NONE) {
-       if (inbuf->use < reader->cur + CHUNK_SIZE) {
+       if (xmlBufUse(inbuf) < reader->cur + CHUNK_SIZE) {
            /*
             * Refill the buffer unless we are at the end of the stream
             */
            if (reader->mode != XML_TEXTREADER_MODE_EOF) {
                val = xmlParserInputBufferRead(reader->input, 4096);
                if ((val == 0) &&
-                   (inbuf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) {
-                   if (inbuf->use == reader->cur) {
+                   (alloc == XML_BUFFER_ALLOC_IMMUTABLE)) {
+                   if (xmlBufUse(inbuf) == reader->cur) {
                        reader->mode = XML_TEXTREADER_MODE_EOF;
                        reader->state = oldstate;
                    }
@@ -850,20 +854,20 @@ xmlTextReaderPushData(xmlTextReaderPtr reader) {
         * parse by block of CHUNK_SIZE bytes, various tests show that
         * it's the best tradeoff at least on a 1.2GH Duron
         */
-       if (inbuf->use >= reader->cur + CHUNK_SIZE) {
+       if (xmlBufUse(inbuf) >= reader->cur + CHUNK_SIZE) {
            val = xmlParseChunk(reader->ctxt,
-                         (const char *) &inbuf->content[reader->cur],
-                         CHUNK_SIZE, 0);
+                 (const char *) xmlBufContent(inbuf) + reader->cur,
+                                CHUNK_SIZE, 0);
            reader->cur += CHUNK_SIZE;
            if (val != 0)
                reader->ctxt->wellFormed = 0;
            if (reader->ctxt->wellFormed == 0)
                break;
        } else {
-           s = inbuf->use - reader->cur;
+           s = xmlBufUse(inbuf) - reader->cur;
            val = xmlParseChunk(reader->ctxt,
-                         (const char *) &inbuf->content[reader->cur],
-                         s, 0);
+                (const char *) xmlBufContent(inbuf) + reader->cur,
+                               s, 0);
            reader->cur += s;
            if (val != 0)
                reader->ctxt->wellFormed = 0;
@@ -875,10 +879,10 @@ xmlTextReaderPushData(xmlTextReaderPtr reader) {
      * Discard the consumed input when needed and possible
      */
     if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
-        if (inbuf->alloc != XML_BUFFER_ALLOC_IMMUTABLE) {
+        if (alloc != XML_BUFFER_ALLOC_IMMUTABLE) {
            if ((reader->cur >= 4096) &&
-               (inbuf->use - reader->cur <= CHUNK_SIZE)) {
-               val = xmlBufferShrink(inbuf, reader->cur);
+               (xmlBufUse(inbuf) - reader->cur <= CHUNK_SIZE)) {
+               val = xmlBufShrink(inbuf, reader->cur);
                if (val >= 0) {
                    reader->cur -= val;
                }
@@ -892,11 +896,11 @@ xmlTextReaderPushData(xmlTextReaderPtr reader) {
      */
     else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
        if (reader->state != XML_TEXTREADER_DONE) {
-           s = inbuf->use - reader->cur;
+           s = xmlBufUse(inbuf) - reader->cur;
            val = xmlParseChunk(reader->ctxt,
-                   (const char *) &inbuf->content[reader->cur],
-                   s, 1);
-           reader->cur = inbuf->use;
+                (const char *) xmlBufContent(inbuf) + reader->cur,
+                               s, 1);
+           reader->cur = xmlBufUse(inbuf);
            reader->state  = XML_TEXTREADER_DONE;
            if (val != 0) {
                if (reader->ctxt->wellFormed)
@@ -907,8 +911,11 @@ xmlTextReaderPushData(xmlTextReaderPtr reader) {
        }
     }
     reader->state = oldstate;
-    if (reader->ctxt->wellFormed == 0)
+    if (reader->ctxt->wellFormed == 0) {
        reader->mode = XML_TEXTREADER_MODE_EOF;
+        return(-1);
+    }
+
     return(0);
 }
 
@@ -977,7 +984,7 @@ printf("Expand failed !\n");
  * xmlTextReaderValidateCData:
  * @reader:  the xmlTextReaderPtr used
  * @data:  pointer to the CData
- * @len:  lenght of the CData block in bytes.
+ * @len:  length of the CData block in bytes.
  *
  * Push some CData for validation
  */
@@ -1221,6 +1228,9 @@ xmlTextReaderCollectSiblings(xmlNodePtr node)
     xmlBufferPtr buffer;
     xmlChar *ret;
 
+    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
+        return(NULL);
+
     buffer = xmlBufferCreate();
     if (buffer == NULL)
        return NULL;
@@ -1399,8 +1409,7 @@ get_next_node:
 #endif
            (reader->entNr == 0) &&
            (reader->node->prev != NULL) &&
-            (reader->node->prev->type != XML_DTD_NODE) &&
-           (reader->entNr == 0)) {
+            (reader->node->prev->type != XML_DTD_NODE)) {
            xmlNodePtr tmp = reader->node->prev;
            if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
                xmlUnlinkNode(tmp);
@@ -1449,8 +1458,7 @@ get_next_node:
 #endif
            (reader->entNr == 0) &&
            (oldnode->type != XML_DTD_NODE) &&
-           ((oldnode->extra & NODE_IS_PRESERVED) == 0) &&
-           (reader->entNr == 0)) {
+           ((oldnode->extra & NODE_IS_PRESERVED) == 0)) {
            xmlUnlinkNode(oldnode);
            xmlTextReaderFreeNode(reader, oldnode);
        }
@@ -1716,8 +1724,8 @@ xmlTextReaderReadInnerXml(xmlTextReaderPtr reader ATTRIBUTE_UNUSED)
  *
  * Reads the contents of the current node, including child nodes and markup.
  *
- * Returns a string containing the node and any XML content, or NULL if the 
- *         current node cannot be serialized. The string must be deallocated 
+ * Returns a string containing the node and any XML content, or NULL if the
+ *         current node cannot be serialized. The string must be deallocated
  *         by the caller.
  */
 xmlChar *
@@ -2062,7 +2070,7 @@ xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
     ret->entMax = 0;
     ret->entNr = 0;
     ret->input = input;
-    ret->buffer = xmlBufferCreateSize(100);
+    ret->buffer = xmlBufCreateSize(100);
     if (ret->buffer == NULL) {
         xmlFree(ret);
         xmlGenericError(xmlGenericErrorContext,
@@ -2071,7 +2079,7 @@ xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
     }
     ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
     if (ret->sax == NULL) {
-       xmlBufferFree(ret->buffer);
+       xmlBufFree(ret->buffer);
        xmlFree(ret);
         xmlGenericError(xmlGenericErrorContext,
                "xmlNewTextReader : malloc failed\n");
@@ -2104,12 +2112,13 @@ xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
     ret->mode = XML_TEXTREADER_MODE_INITIAL;
     ret->node = NULL;
     ret->curnode = NULL;
-    if (ret->input->buffer->use < 4) {
+    if (xmlBufUse(ret->input->buffer) < 4) {
        xmlParserInputBufferRead(input, 4);
     }
-    if (ret->input->buffer->use >= 4) {
+    if (xmlBufUse(ret->input->buffer) >= 4) {
        ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
-                       (const char *) ret->input->buffer->content, 4, URI);
+                            (const char *) xmlBufContent(ret->input->buffer),
+                                            4, URI);
        ret->base = 0;
        ret->cur = 4;
     } else {
@@ -2121,7 +2130,7 @@ xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
     if (ret->ctxt == NULL) {
         xmlGenericError(xmlGenericErrorContext,
                "xmlNewTextReader : malloc failed\n");
-       xmlBufferFree(ret->buffer);
+       xmlBufFree(ret->buffer);
        xmlFree(ret->sax);
        xmlFree(ret);
        return(NULL);
@@ -2251,7 +2260,7 @@ xmlFreeTextReader(xmlTextReaderPtr reader) {
     if ((reader->input != NULL)  && (reader->allocs & XML_TEXTREADER_INPUT))
        xmlFreeParserInputBuffer(reader->input);
     if (reader->buffer != NULL)
-        xmlBufferFree(reader->buffer);
+        xmlBufFree(reader->buffer);
     if (reader->entTab != NULL)
        xmlFree(reader->entTab);
     if (reader->dict != NULL)
@@ -3599,16 +3608,17 @@ xmlTextReaderConstValue(xmlTextReaderPtr reader) {
                (attr->children->next == NULL))
                return(attr->children->content);
            else {
-               if (reader->buffer == NULL)
-                   reader->buffer = xmlBufferCreateSize(100);
                if (reader->buffer == NULL) {
-                   xmlGenericError(xmlGenericErrorContext,
-                                   "xmlTextReaderSetup : malloc failed\n");
-                   return (NULL);
-               }
-               reader->buffer->use = 0;
-               xmlNodeBufGetContent(reader->buffer, node);
-               return(reader->buffer->content);
+                   reader->buffer = xmlBufCreateSize(100);
+                    if (reader->buffer == NULL) {
+                        xmlGenericError(xmlGenericErrorContext,
+                                        "xmlTextReaderSetup : malloc failed\n");
+                        return (NULL);
+                    }
+                } else
+                    xmlBufEmpty(reader->buffer);
+               xmlBufGetNodeContent(reader->buffer, node);
+               return(xmlBufContent(reader->buffer));
            }
            break;
        }
@@ -4143,6 +4153,60 @@ xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) {
 }
 
 /**
+ * xmlTextReaderLocator:
+ * @ctx: the xmlTextReaderPtr used
+ * @file: returned file information
+ * @line: returned line information
+ *
+ * Internal locator function for the readers
+ *
+ * Returns 0 in case the Schema validation could be (des)activated and
+ *         -1 in case of error.
+ */
+static int
+xmlTextReaderLocator(void *ctx, const char **file, unsigned long *line) {
+    xmlTextReaderPtr reader;
+
+    if ((ctx == NULL) || ((file == NULL) && (line == NULL)))
+        return(-1);
+
+    if (file != NULL)
+        *file = NULL;
+    if (line != NULL)
+        *line = 0;
+
+    reader = (xmlTextReaderPtr) ctx;
+    if ((reader->ctxt != NULL) && (reader->ctxt->input != NULL)) {
+       if (file != NULL)
+           *file = reader->ctxt->input->filename;
+       if (line != NULL)
+           *line = reader->ctxt->input->line;
+       return(0);
+    }
+    if (reader->node != NULL) {
+        long res;
+       int ret = 0;
+
+       if (line != NULL) {
+           res = xmlGetLineNo(reader->node);
+           if (res > 0)
+               *line = (unsigned long) res;
+           else
+                ret = -1;
+       }
+        if (file != NULL) {
+           xmlDocPtr doc = reader->node->doc;
+           if ((doc != NULL) && (doc->URL != NULL))
+               *file = (const char *) doc->URL;
+           else
+                ret = -1;
+       }
+       return(ret);
+    }
+    return(-1);
+}
+
+/**
  * xmlTextReaderSetSchema:
  * @reader:  the xmlTextReaderPtr used
  * @schema:  a precompiled Schema schema
@@ -4209,6 +4273,10 @@ xmlTextReaderSetSchema(xmlTextReaderPtr reader, xmlSchemaPtr schema) {
        reader->xsdValidCtxt = NULL;
        return(-1);
     }
+    xmlSchemaValidateSetLocator(reader->xsdValidCtxt,
+                                xmlTextReaderLocator,
+                               (void *) reader);
+
     if (reader->errorFunc != NULL) {
        xmlSchemaSetValidErrors(reader->xsdValidCtxt,
                        xmlTextReaderValidityErrorRelay,
@@ -4423,6 +4491,9 @@ xmlTextReaderSchemaValidateInternal(xmlTextReaderPtr reader,
            return(-1);
        }
     }
+    xmlSchemaValidateSetLocator(reader->xsdValidCtxt,
+                                xmlTextReaderLocator,
+                               (void *) reader);
     /*
     * Redirect the validation context's error channels to use
     * the reader channels.
@@ -5040,7 +5111,7 @@ xmlTextReaderSetup(xmlTextReaderPtr reader,
        reader->allocs |= XML_TEXTREADER_INPUT;
     }
     if (reader->buffer == NULL)
-        reader->buffer = xmlBufferCreateSize(100);
+        reader->buffer = xmlBufCreateSize(100);
     if (reader->buffer == NULL) {
         xmlGenericError(xmlGenericErrorContext,
                         "xmlTextReaderSetup : malloc failed\n");
@@ -5081,13 +5152,14 @@ xmlTextReaderSetup(xmlTextReaderPtr reader,
     reader->node = NULL;
     reader->curnode = NULL;
     if (input != NULL) {
-        if (reader->input->buffer->use < 4) {
+        if (xmlBufUse(reader->input->buffer) < 4) {
             xmlParserInputBufferRead(input, 4);
         }
         if (reader->ctxt == NULL) {
-            if (reader->input->buffer->use >= 4) {
+            if (xmlBufUse(reader->input->buffer) >= 4) {
                 reader->ctxt = xmlCreatePushParserCtxt(reader->sax, NULL,
-                      (const char *) reader->input->buffer->content, 4, URL);
+                      (const char *) xmlBufContent(reader->input->buffer),
+                                      4, URL);
                 reader->base = 0;
                 reader->cur = 4;
             } else {
@@ -5116,10 +5188,7 @@ xmlTextReaderSetup(xmlTextReaderPtr reader,
                inputStream->filename = (char *)
                    xmlCanonicPath((const xmlChar *) URL);
            inputStream->buf = buf;
-           inputStream->base = inputStream->buf->buffer->content;
-           inputStream->cur = inputStream->buf->buffer->content;
-           inputStream->end =
-            &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
+            xmlBufResetInput(buf->buffer, inputStream);
 
            inputPush(reader->ctxt, inputStream);
            reader->cur = 0;