Coverage Report

Created: 2026-05-30 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxml2/xmlreader.c
Line
Count
Source
1
/*
2
 * xmlreader.c: implements the xmlTextReader streaming node API
3
 *
4
 * NOTE:
5
 *   XmlTextReader.Normalization Property won't be supported, since
6
 *     it makes the parser non compliant to the XML recommendation
7
 *
8
 * See Copyright for the status of this software.
9
 *
10
 * Author: Daniel Veillard
11
 */
12
13
/*
14
 * TODOs:
15
 *   - XML Schemas validation
16
 */
17
#define IN_LIBXML
18
#include "libxml.h"
19
20
#ifdef LIBXML_READER_ENABLED
21
#include <string.h> /* for memset() only ! */
22
#include <stdarg.h>
23
#include <ctype.h>
24
#include <stdlib.h>
25
26
#include <libxml/xmlmemory.h>
27
#include <libxml/xmlIO.h>
28
#include <libxml/xmlreader.h>
29
#include <libxml/parserInternals.h>
30
#ifdef LIBXML_RELAXNG_ENABLED
31
#include <libxml/relaxng.h>
32
#endif
33
#ifdef LIBXML_SCHEMAS_ENABLED
34
#include <libxml/xmlschemas.h>
35
#endif
36
#include <libxml/uri.h>
37
#ifdef LIBXML_XINCLUDE_ENABLED
38
#include <libxml/xinclude.h>
39
#endif
40
#ifdef LIBXML_PATTERN_ENABLED
41
#include <libxml/pattern.h>
42
#endif
43
44
#include "private/buf.h"
45
#include "private/error.h"
46
#include "private/io.h"
47
#include "private/memory.h"
48
#include "private/parser.h"
49
#include "private/tree.h"
50
#ifdef LIBXML_XINCLUDE_ENABLED
51
#include "private/xinclude.h"
52
#endif
53
54
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
55
/* Keeping free objects can hide memory errors. */
56
0
#define MAX_FREE_NODES 1
57
#else
58
#define MAX_FREE_NODES 100
59
#endif
60
61
#ifndef va_copy
62
  #ifdef __va_copy
63
    #define va_copy(dest, src) __va_copy(dest, src)
64
  #else
65
    #define va_copy(dest, src) memcpy(&(dest), &(src), sizeof(va_list))
66
  #endif
67
#endif
68
69
0
#define CHUNK_SIZE 512
70
/************************************************************************
71
 *                  *
72
 *  The parser: maps the Text Reader API on top of the existing *
73
 *    parsing routines building a tree      *
74
 *                  *
75
 ************************************************************************/
76
77
0
#define XML_TEXTREADER_INPUT  1
78
0
#define XML_TEXTREADER_CTXT 2
79
80
typedef enum {
81
    XML_TEXTREADER_NONE = -1,
82
    XML_TEXTREADER_START= 0,
83
    XML_TEXTREADER_ELEMENT= 1,
84
    XML_TEXTREADER_END= 2,
85
    XML_TEXTREADER_EMPTY= 3,
86
    XML_TEXTREADER_BACKTRACK= 4,
87
    XML_TEXTREADER_DONE= 5,
88
    XML_TEXTREADER_ERROR= 6
89
} xmlTextReaderState;
90
91
typedef enum {
92
    XML_TEXTREADER_NOT_VALIDATE = 0,
93
    XML_TEXTREADER_VALIDATE_DTD = 1,
94
    XML_TEXTREADER_VALIDATE_RNG = 2,
95
    XML_TEXTREADER_VALIDATE_XSD = 4
96
} xmlTextReaderValidate;
97
98
struct _xmlTextReader {
99
    int       mode; /* the parsing mode */
100
    xmlDocPtr     doc;    /* when walking an existing doc */
101
    xmlTextReaderValidate       validate;/* is there any validation */
102
    int       allocs; /* what structure were deallocated */
103
    xmlTextReaderState    state;
104
    xmlParserCtxtPtr    ctxt; /* the parser context */
105
    xmlSAXHandlerPtr    sax;  /* the parser SAX callbacks */
106
    xmlParserInputBufferPtr input;  /* the input */
107
    startElementSAXFunc   startElement;/* initial SAX callbacks */
108
    endElementSAXFunc   endElement;  /* idem */
109
    startElementNsSAX2Func  startElementNs;/* idem */
110
    endElementNsSAX2Func  endElementNs;  /* idem */
111
    charactersSAXFunc   characters;
112
    cdataBlockSAXFunc   cdataBlock;
113
    unsigned int    base; /* base of the segment in the input */
114
    unsigned int    cur;  /* current position in the input */
115
    xmlNodePtr      node; /* current node */
116
    xmlNodePtr      curnode;/* current attribute node */
117
    int       depth;  /* depth of the current node */
118
    xmlNodePtr      faketext;/* fake xmlNs chld */
119
    int       preserve;/* preserve the resulting document */
120
    xmlBufPtr           buffer; /* used to return const xmlChar * */
121
    xmlDictPtr      dict; /* the context dictionary */
122
123
    /* entity stack when traversing entities content */
124
    xmlNodePtr         ent;          /* Current Entity Ref Node */
125
    int                entNr;        /* Depth of the entities stack */
126
    int                entMax;       /* Max depth of the entities stack */
127
    xmlNodePtr        *entTab;       /* array of entities */
128
129
    /* error handling */
130
    xmlTextReaderErrorFunc errorFunc;    /* callback function */
131
    void                  *errorFuncArg; /* callback function user argument */
132
133
#ifdef LIBXML_RELAXNG_ENABLED
134
    /* Handling of RelaxNG validation */
135
    xmlRelaxNGPtr          rngSchemas;  /* The Relax NG schemas */
136
    xmlRelaxNGValidCtxtPtr rngValidCtxt;/* The Relax NG validation context */
137
    int                    rngPreserveCtxt; /* 1 if the context was provided by the user */
138
    int                    rngValidErrors;/* The number of errors detected */
139
    xmlNodePtr             rngFullNode; /* the node if RNG not progressive */
140
#endif
141
#ifdef LIBXML_SCHEMAS_ENABLED
142
    /* Handling of Schemas validation */
143
    xmlSchemaPtr          xsdSchemas; /* The Schemas schemas */
144
    xmlSchemaValidCtxtPtr xsdValidCtxt;/* The Schemas validation context */
145
    int                   xsdPreserveCtxt; /* 1 if the context was provided by the user */
146
    int                   xsdValidErrors;/* The number of errors detected */
147
    xmlSchemaSAXPlugPtr   xsdPlug;  /* the schemas plug in SAX pipeline */
148
#endif
149
#ifdef LIBXML_XINCLUDE_ENABLED
150
    /* Handling of XInclude processing */
151
    int                xinclude;  /* is xinclude asked for */
152
    xmlXIncludeCtxtPtr xincctxt;  /* the xinclude context */
153
    int                in_xinclude; /* counts for xinclude */
154
#endif
155
#ifdef LIBXML_PATTERN_ENABLED
156
    int                patternNr;       /* number of preserve patterns */
157
    int                patternMax;      /* max preserve patterns */
158
    xmlPatternPtr     *patternTab;      /* array of preserve patterns */
159
#endif
160
    int                preserves; /* level of preserves */
161
    int                parserFlags; /* the set of options set */
162
    /* Structured error handling */
163
    xmlStructuredErrorFunc sErrorFunc;  /* callback function */
164
165
    xmlResourceLoader resourceLoader;
166
    void *resourceCtxt;
167
};
168
169
0
#define NODE_IS_EMPTY   0x1
170
0
#define NODE_IS_PRESERVED 0x2
171
0
#define NODE_IS_SPRESERVED  0x4
172
173
static int xmlTextReaderReadTree(xmlTextReaderPtr reader);
174
static int xmlTextReaderNextTree(xmlTextReaderPtr reader);
175
176
/**
177
 * Free a string if it is not owned by the "dict" dictionary in the
178
 * current scope
179
 *
180
 * @param str  a string
181
 */
182
#define DICT_FREE(str)            \
183
0
  if ((str) && ((!dict) ||       \
184
0
      (xmlDictOwns(dict, (const xmlChar *)(str)) == 0)))  \
185
0
      xmlFree((char *)(str));
186
187
static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur);
188
static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur);
189
190
static void
191
0
xmlTextReaderErr(xmlParserErrors code, const char *msg, ...) {
192
0
    va_list ap;
193
0
    int res;
194
195
0
    va_start(ap, msg);
196
0
    res = xmlVRaiseError(NULL, NULL, NULL, NULL, NULL,
197
0
                         XML_FROM_PARSER, code, XML_ERR_FATAL,
198
0
                         NULL, 0, NULL, NULL, NULL, 0, 0,
199
0
                         msg, ap);
200
0
    va_end(ap);
201
0
    if (res < 0)
202
0
        xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_PARSER, NULL);
203
0
}
204
205
static void
206
0
xmlTextReaderErrMemory(xmlTextReaderPtr reader) {
207
0
    if (reader == NULL) {
208
0
        xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_PARSER, NULL);
209
0
        return;
210
0
    }
211
212
0
    if (reader->ctxt != NULL)
213
0
        xmlCtxtErrMemory(reader->ctxt);
214
0
    else
215
0
        xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_PARSER, NULL);
216
217
0
    reader->mode = XML_TEXTREADER_MODE_ERROR;
218
0
    reader->state = XML_TEXTREADER_ERROR;
219
0
}
220
221
static xmlChar *
222
0
readerStrdup(xmlTextReaderPtr reader, const xmlChar *string) {
223
0
    xmlChar *copy;
224
225
0
    if (string == NULL)
226
0
        return(NULL);
227
228
0
    copy = xmlStrdup(string);
229
0
    if (copy == NULL)
230
0
        xmlTextReaderErrMemory(reader);
231
232
0
    return(copy);
233
0
}
234
235
static const xmlChar *
236
0
constString(xmlTextReaderPtr reader, const xmlChar *string) {
237
0
    const xmlChar *dictString;
238
239
0
    if (string == NULL)
240
0
        return(NULL);
241
242
0
    dictString = xmlDictLookup(reader->dict, string, -1);
243
0
    if (dictString == NULL)
244
0
        xmlTextReaderErrMemory(reader);
245
246
0
    return(dictString);
247
0
}
248
249
static const xmlChar *
250
constQString(xmlTextReaderPtr reader, const xmlChar *prefix,
251
0
             const xmlChar *name) {
252
0
    const xmlChar *dictString;
253
254
0
    if (name == NULL)
255
0
        return(NULL);
256
257
0
    dictString = xmlDictQLookup(reader->dict, prefix, name);
258
0
    if (dictString == NULL)
259
0
        xmlTextReaderErrMemory(reader);
260
261
0
    return(dictString);
262
0
}
263
264
/************************************************************************
265
 *                  *
266
 *  Our own version of the freeing routines as we recycle nodes *
267
 *                  *
268
 ************************************************************************/
269
270
/**
271
 * Free a node.
272
 *
273
 * @param reader  the xmlTextReader used
274
 * @param cur  the node
275
 */
276
static void
277
0
xmlTextReaderFreeProp(xmlTextReaderPtr reader, xmlAttrPtr cur) {
278
0
    xmlDictPtr dict;
279
280
0
    if ((reader != NULL) && (reader->ctxt != NULL))
281
0
  dict = reader->ctxt->dict;
282
0
    else
283
0
        dict = NULL;
284
0
    if (cur == NULL) return;
285
286
0
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
287
0
  xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
288
289
0
    if (cur->children != NULL)
290
0
        xmlTextReaderFreeNodeList(reader, cur->children);
291
292
0
    if (cur->id != NULL) {
293
        /*
294
         * Operating in streaming mode, attr is gonna disappear
295
         */
296
0
        cur->id->attr = NULL;
297
0
        if (cur->id->name != NULL)
298
0
            DICT_FREE(cur->id->name);
299
0
        cur->id->name = cur->name;
300
0
        cur->name = NULL;
301
0
    } else {
302
0
        DICT_FREE(cur->name);
303
0
    }
304
305
0
    if ((reader != NULL) && (reader->ctxt != NULL) &&
306
0
        (reader->ctxt->freeAttrsNr < MAX_FREE_NODES)) {
307
0
        cur->next = reader->ctxt->freeAttrs;
308
0
  reader->ctxt->freeAttrs = cur;
309
0
  reader->ctxt->freeAttrsNr++;
310
0
    } else {
311
0
  xmlFree(cur);
312
0
    }
313
0
}
314
315
/**
316
 * Free a property and all its siblings, all the children are freed too.
317
 *
318
 * @param reader  the xmlTextReader used
319
 * @param cur  the first property in the list
320
 */
321
static void
322
0
xmlTextReaderFreePropList(xmlTextReaderPtr reader, xmlAttrPtr cur) {
323
0
    xmlAttrPtr next;
324
325
0
    while (cur != NULL) {
326
0
        next = cur->next;
327
0
        xmlTextReaderFreeProp(reader, cur);
328
0
  cur = next;
329
0
    }
330
0
}
331
332
/**
333
 * Free a node and all its siblings, this is a recursive behaviour, all
334
 * the children are freed too.
335
 *
336
 * @param reader  the xmlTextReader used
337
 * @param cur  the first node in the list
338
 */
339
static void
340
0
xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur) {
341
0
    xmlNodePtr next;
342
0
    xmlNodePtr parent;
343
0
    xmlDictPtr dict;
344
0
    size_t depth = 0;
345
346
0
    if ((reader != NULL) && (reader->ctxt != NULL))
347
0
  dict = reader->ctxt->dict;
348
0
    else
349
0
        dict = NULL;
350
0
    if (cur == NULL) return;
351
0
    if (cur->type == XML_NAMESPACE_DECL) {
352
0
  xmlFreeNsList((xmlNsPtr) cur);
353
0
  return;
354
0
    }
355
0
    if ((cur->type == XML_DOCUMENT_NODE) ||
356
0
  (cur->type == XML_HTML_DOCUMENT_NODE)) {
357
0
  xmlFreeDoc((xmlDocPtr) cur);
358
0
  return;
359
0
    }
360
0
    while (1) {
361
0
        while ((cur->type != XML_DTD_NODE) &&
362
0
               (cur->type != XML_ENTITY_REF_NODE) &&
363
0
               (cur->children != NULL) &&
364
0
               (cur->children->parent == cur)) {
365
0
            cur = cur->children;
366
0
            depth += 1;
367
0
        }
368
369
0
        next = cur->next;
370
0
        parent = cur->parent;
371
372
  /* unroll to speed up freeing the document */
373
0
  if (cur->type != XML_DTD_NODE) {
374
375
0
      if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
376
0
    xmlDeregisterNodeDefaultValue(cur);
377
378
0
      if (((cur->type == XML_ELEMENT_NODE) ||
379
0
     (cur->type == XML_XINCLUDE_START) ||
380
0
     (cur->type == XML_XINCLUDE_END)) &&
381
0
    (cur->properties != NULL))
382
0
    xmlTextReaderFreePropList(reader, cur->properties);
383
0
      if ((cur->content != (xmlChar *) &(cur->properties)) &&
384
0
          (cur->type != XML_ELEMENT_NODE) &&
385
0
    (cur->type != XML_XINCLUDE_START) &&
386
0
    (cur->type != XML_XINCLUDE_END) &&
387
0
    (cur->type != XML_ENTITY_REF_NODE)) {
388
0
    DICT_FREE(cur->content);
389
0
      }
390
0
      if (((cur->type == XML_ELEMENT_NODE) ||
391
0
           (cur->type == XML_XINCLUDE_START) ||
392
0
     (cur->type == XML_XINCLUDE_END)) &&
393
0
    (cur->nsDef != NULL))
394
0
    xmlFreeNsList(cur->nsDef);
395
396
      /*
397
       * we don't free element names here they are interned now
398
       */
399
0
      if ((cur->type != XML_TEXT_NODE) &&
400
0
    (cur->type != XML_COMMENT_NODE))
401
0
    DICT_FREE(cur->name);
402
0
      if (((cur->type == XML_ELEMENT_NODE) ||
403
0
     (cur->type == XML_TEXT_NODE)) &&
404
0
          (reader != NULL) && (reader->ctxt != NULL) &&
405
0
    (reader->ctxt->freeElemsNr < MAX_FREE_NODES)) {
406
0
          cur->next = reader->ctxt->freeElems;
407
0
    reader->ctxt->freeElems = cur;
408
0
    reader->ctxt->freeElemsNr++;
409
0
      } else {
410
0
    xmlFree(cur);
411
0
      }
412
0
  }
413
414
0
        if (next != NULL) {
415
0
      cur = next;
416
0
        } else {
417
0
            if ((depth == 0) || (parent == NULL))
418
0
                break;
419
0
            depth -= 1;
420
0
            cur = parent;
421
0
            cur->children = NULL;
422
0
        }
423
0
    }
424
0
}
425
426
/**
427
 * Free a node, this is a recursive behaviour, all the children are freed too.
428
 * This doesn't unlink the child from the list, use #xmlUnlinkNode first.
429
 *
430
 * @param reader  the xmlTextReader used
431
 * @param cur  the node
432
 */
433
static void
434
0
xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur) {
435
0
    xmlDictPtr dict;
436
437
0
    if ((reader != NULL) && (reader->ctxt != NULL))
438
0
  dict = reader->ctxt->dict;
439
0
    else
440
0
        dict = NULL;
441
0
    if (cur->type == XML_DTD_NODE) {
442
0
  xmlFreeDtd((xmlDtdPtr) cur);
443
0
  return;
444
0
    }
445
0
    if (cur->type == XML_NAMESPACE_DECL) {
446
0
  xmlFreeNs((xmlNsPtr) cur);
447
0
        return;
448
0
    }
449
0
    if (cur->type == XML_ATTRIBUTE_NODE) {
450
0
  xmlTextReaderFreeProp(reader, (xmlAttrPtr) cur);
451
0
  return;
452
0
    }
453
454
0
    if ((cur->children != NULL) &&
455
0
  (cur->type != XML_ENTITY_REF_NODE)) {
456
0
  if (cur->children->parent == cur)
457
0
      xmlTextReaderFreeNodeList(reader, cur->children);
458
0
  cur->children = NULL;
459
0
    }
460
461
0
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
462
0
  xmlDeregisterNodeDefaultValue(cur);
463
464
0
    if (((cur->type == XML_ELEMENT_NODE) ||
465
0
   (cur->type == XML_XINCLUDE_START) ||
466
0
   (cur->type == XML_XINCLUDE_END)) &&
467
0
  (cur->properties != NULL))
468
0
  xmlTextReaderFreePropList(reader, cur->properties);
469
0
    if ((cur->content != (xmlChar *) &(cur->properties)) &&
470
0
        (cur->type != XML_ELEMENT_NODE) &&
471
0
  (cur->type != XML_XINCLUDE_START) &&
472
0
  (cur->type != XML_XINCLUDE_END) &&
473
0
  (cur->type != XML_ENTITY_REF_NODE)) {
474
0
  DICT_FREE(cur->content);
475
0
    }
476
0
    if (((cur->type == XML_ELEMENT_NODE) ||
477
0
   (cur->type == XML_XINCLUDE_START) ||
478
0
   (cur->type == XML_XINCLUDE_END)) &&
479
0
  (cur->nsDef != NULL))
480
0
  xmlFreeNsList(cur->nsDef);
481
482
    /*
483
     * we don't free names here they are interned now
484
     */
485
0
    if ((cur->type != XML_TEXT_NODE) &&
486
0
        (cur->type != XML_COMMENT_NODE))
487
0
  DICT_FREE(cur->name);
488
489
0
    if (((cur->type == XML_ELEMENT_NODE) ||
490
0
   (cur->type == XML_TEXT_NODE)) &&
491
0
  (reader != NULL) && (reader->ctxt != NULL) &&
492
0
  (reader->ctxt->freeElemsNr < MAX_FREE_NODES)) {
493
0
  cur->next = reader->ctxt->freeElems;
494
0
  reader->ctxt->freeElems = cur;
495
0
  reader->ctxt->freeElemsNr++;
496
0
    } else {
497
0
  xmlFree(cur);
498
0
    }
499
0
}
500
501
/**
502
 * Free up all the structures used by a document, tree included.
503
 *
504
 * @param reader  the xmlTextReader used
505
 * @param cur  pointer to the document
506
 */
507
static void
508
0
xmlTextReaderFreeDoc(xmlTextReaderPtr reader, xmlDocPtr cur) {
509
0
    xmlDtdPtr extSubset, intSubset;
510
511
0
    if (cur == NULL) return;
512
513
0
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
514
0
  xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
515
516
    /*
517
     * Do this before freeing the children list to avoid ID lookups
518
     */
519
0
    if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
520
0
    cur->ids = NULL;
521
0
    if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
522
0
    cur->refs = NULL;
523
0
    extSubset = cur->extSubset;
524
0
    intSubset = cur->intSubset;
525
0
    if (intSubset == extSubset)
526
0
  extSubset = NULL;
527
0
    if (extSubset != NULL) {
528
0
  xmlUnlinkNode((xmlNodePtr) cur->extSubset);
529
0
  cur->extSubset = NULL;
530
0
  xmlFreeDtd(extSubset);
531
0
    }
532
0
    if (intSubset != NULL) {
533
0
  xmlUnlinkNode((xmlNodePtr) cur->intSubset);
534
0
  cur->intSubset = NULL;
535
0
  xmlFreeDtd(intSubset);
536
0
    }
537
538
0
    if (cur->children != NULL) xmlTextReaderFreeNodeList(reader, cur->children);
539
540
0
    if (cur->version != NULL) xmlFree(cur->version);
541
0
    if (cur->name != NULL) xmlFree((char *) cur->name);
542
0
    if (cur->encoding != NULL) xmlFree(cur->encoding);
543
0
    if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
544
0
    if (cur->URL != NULL) xmlFree(cur->URL);
545
0
    if (cur->dict != NULL) xmlDictFree(cur->dict);
546
547
0
    xmlFree(cur);
548
0
}
549
550
/************************************************************************
551
 *                  *
552
 *      The reader core parser        *
553
 *                  *
554
 ************************************************************************/
555
556
static void
557
xmlTextReaderStructuredRelay(void *userData, const xmlError *error)
558
0
{
559
0
    xmlTextReaderPtr reader = (xmlTextReaderPtr) userData;
560
561
0
    if (reader->sErrorFunc != NULL) {
562
0
        reader->sErrorFunc(reader->errorFuncArg, error);
563
0
    } else if (reader->errorFunc != NULL) {
564
0
        xmlParserSeverities severity;
565
566
0
        if ((error->domain == XML_FROM_VALID) ||
567
0
            (error->domain == XML_FROM_DTD)) {
568
0
            if (error->level == XML_ERR_WARNING)
569
0
                severity = XML_PARSER_SEVERITY_VALIDITY_WARNING;
570
0
            else
571
0
                severity = XML_PARSER_SEVERITY_VALIDITY_ERROR;
572
0
        } else {
573
0
            if (error->level == XML_ERR_WARNING)
574
0
                severity = XML_PARSER_SEVERITY_WARNING;
575
0
            else
576
0
                severity = XML_PARSER_SEVERITY_ERROR;
577
0
        }
578
579
0
        reader->errorFunc(reader->errorFuncArg, error->message, severity,
580
0
                          reader->ctxt);
581
0
    }
582
0
}
583
584
/**
585
 * Pushes a new entity reference node on top of the entities stack
586
 *
587
 * @param reader  the xmlTextReader used
588
 * @param value  the entity reference node
589
 * @returns -1 in case of error, the index in the stack otherwise
590
 */
591
static int
592
xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value)
593
0
{
594
0
    if (reader->entNr >= reader->entMax) {
595
0
        xmlNodePtr *tmp;
596
0
        int newSize;
597
598
0
        newSize = xmlGrowCapacity(reader->entMax, sizeof(tmp[0]),
599
0
                                  10, XML_MAX_ITEMS);
600
0
        if (newSize < 0) {
601
0
            xmlTextReaderErrMemory(reader);
602
0
            return (-1);
603
0
        }
604
0
        tmp = xmlRealloc(reader->entTab, newSize * sizeof(tmp[0]));
605
0
        if (tmp == NULL) {
606
0
            xmlTextReaderErrMemory(reader);
607
0
            return (-1);
608
0
        }
609
0
        reader->entTab = tmp;
610
0
        reader->entMax = newSize;
611
0
    }
612
0
    reader->entTab[reader->entNr] = value;
613
0
    reader->ent = value;
614
0
    return (reader->entNr++);
615
0
}
616
617
/**
618
 * Pops the top element entity from the entities stack
619
 *
620
 * @param reader  the xmlTextReader used
621
 * @returns the entity just removed
622
 */
623
static xmlNodePtr
624
xmlTextReaderEntPop(xmlTextReaderPtr reader)
625
0
{
626
0
    xmlNodePtr ret;
627
628
0
    if (reader->entNr <= 0)
629
0
        return (NULL);
630
0
    reader->entNr--;
631
0
    if (reader->entNr > 0)
632
0
        reader->ent = reader->entTab[reader->entNr - 1];
633
0
    else
634
0
        reader->ent = NULL;
635
0
    ret = reader->entTab[reader->entNr];
636
0
    reader->entTab[reader->entNr] = NULL;
637
0
    return (ret);
638
0
}
639
640
/**
641
 * called when an opening tag has been processed.
642
 *
643
 * @param ctx  the user data (XML parser context)
644
 * @param fullname  The element name, including namespace prefix
645
 * @param atts  An array of name/value attributes pairs, NULL terminated
646
 */
647
static void
648
xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
649
0
                    const xmlChar **atts) {
650
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
651
0
    xmlTextReaderPtr reader = ctxt->_private;
652
653
0
    if ((reader != NULL) && (reader->startElement != NULL)) {
654
0
  reader->startElement(ctx, fullname, atts);
655
0
  if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
656
0
      (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
657
0
      (ctxt->input->cur[1] == '>'))
658
0
      ctxt->node->extra = NODE_IS_EMPTY;
659
0
    }
660
0
    if (reader != NULL)
661
0
  reader->state = XML_TEXTREADER_ELEMENT;
662
0
}
663
664
/**
665
 * called when an ending tag has been processed.
666
 *
667
 * @param ctx  the user data (XML parser context)
668
 * @param fullname  The element name, including namespace prefix
669
 */
670
static void
671
0
xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
672
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
673
0
    xmlTextReaderPtr reader = ctxt->_private;
674
675
0
    if ((reader != NULL) && (reader->endElement != NULL)) {
676
0
  reader->endElement(ctx, fullname);
677
0
    }
678
0
}
679
680
/**
681
 * called when an opening tag has been processed.
682
 *
683
 * @param ctx  the user data (XML parser context)
684
 * @param localname  the local name of the element
685
 * @param prefix  the element namespace prefix if available
686
 * @param URI  the element namespace name if available
687
 * @param nb_namespaces  number of namespace definitions on that node
688
 * @param namespaces  pointer to the array of prefix/URI pairs namespace definitions
689
 * @param nb_attributes  the number of attributes on that node
690
 * @param nb_defaulted  the number of defaulted attributes.
691
 * @param attributes  pointer to the array of (localname/prefix/URI/value/end)
692
 *               attribute values.
693
 */
694
static void
695
xmlTextReaderStartElementNs(void *ctx,
696
                      const xmlChar *localname,
697
          const xmlChar *prefix,
698
          const xmlChar *URI,
699
          int nb_namespaces,
700
          const xmlChar **namespaces,
701
          int nb_attributes,
702
          int nb_defaulted,
703
          const xmlChar **attributes)
704
0
{
705
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
706
0
    xmlTextReaderPtr reader = ctxt->_private;
707
708
0
    if ((reader != NULL) && (reader->startElementNs != NULL)) {
709
0
  reader->startElementNs(ctx, localname, prefix, URI, nb_namespaces,
710
0
                         namespaces, nb_attributes, nb_defaulted,
711
0
             attributes);
712
0
  if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
713
0
      (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
714
0
      (ctxt->input->cur[1] == '>'))
715
0
      ctxt->node->extra = NODE_IS_EMPTY;
716
0
    }
717
0
    if (reader != NULL)
718
0
  reader->state = XML_TEXTREADER_ELEMENT;
719
0
}
720
721
/**
722
 * called when an ending tag has been processed.
723
 *
724
 * @param ctx  the user data (XML parser context)
725
 * @param localname  the local name of the element
726
 * @param prefix  the element namespace prefix if available
727
 * @param URI  the element namespace name if available
728
 */
729
static void
730
xmlTextReaderEndElementNs(void *ctx,
731
                          const xmlChar * localname,
732
                          const xmlChar * prefix,
733
              const xmlChar * URI)
734
0
{
735
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
736
0
    xmlTextReaderPtr reader = ctxt->_private;
737
738
0
    if ((reader != NULL) && (reader->endElementNs != NULL)) {
739
0
  reader->endElementNs(ctx, localname, prefix, URI);
740
0
    }
741
0
}
742
743
744
/**
745
 * receiving some chars from the parser.
746
 *
747
 * @param ctx  the user data (XML parser context)
748
 * @param ch  a xmlChar string
749
 * @param len  the number of xmlChar
750
 */
751
static void
752
xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
753
0
{
754
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
755
0
    xmlTextReaderPtr reader = ctxt->_private;
756
757
0
    if ((reader != NULL) && (reader->characters != NULL)) {
758
0
  reader->characters(ctx, ch, len);
759
0
    }
760
0
}
761
762
/**
763
 * called when a pcdata block has been parsed
764
 *
765
 * @param ctx  the user data (XML parser context)
766
 * @param ch  The pcdata content
767
 * @param len  the block length
768
 */
769
static void
770
xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
771
0
{
772
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
773
0
    xmlTextReaderPtr reader = ctxt->_private;
774
775
0
    if ((reader != NULL) && (reader->cdataBlock != NULL)) {
776
0
  reader->cdataBlock(ctx, ch, len);
777
0
    }
778
0
}
779
780
/**
781
 * Push data down the progressive parser until a significant callback
782
 * got raised.
783
 *
784
 * @param reader  the xmlTextReader used
785
 * @returns -1 in case of failure, 0 otherwise
786
 */
787
static int
788
0
xmlTextReaderPushData(xmlTextReaderPtr reader) {
789
0
    xmlBufPtr inbuf;
790
0
    int val, s;
791
0
    xmlTextReaderState oldstate;
792
793
0
    if ((reader->input == NULL) || (reader->input->buffer == NULL))
794
0
  return(-1);
795
796
0
    oldstate = reader->state;
797
0
    reader->state = XML_TEXTREADER_NONE;
798
0
    inbuf = reader->input->buffer;
799
800
0
    while (reader->state == XML_TEXTREADER_NONE) {
801
0
  if (xmlBufUse(inbuf) < reader->cur + CHUNK_SIZE) {
802
      /*
803
       * Refill the buffer unless we are at the end of the stream
804
       */
805
0
      if (reader->mode != XML_TEXTREADER_MODE_EOF) {
806
0
    val = xmlParserInputBufferRead(reader->input, 4096);
807
0
    if (val == 0) {
808
0
        if (xmlBufUse(inbuf) == reader->cur) {
809
0
      reader->mode = XML_TEXTREADER_MODE_EOF;
810
0
                        break;
811
0
        }
812
0
    } else if (val < 0) {
813
0
                    xmlCtxtErrIO(reader->ctxt, reader->input->error, NULL);
814
0
                    reader->mode = XML_TEXTREADER_MODE_ERROR;
815
0
                    reader->state = XML_TEXTREADER_ERROR;
816
0
                    return(-1);
817
0
    }
818
819
0
      } else
820
0
    break;
821
0
  }
822
  /*
823
   * parse by block of CHUNK_SIZE bytes, various tests show that
824
   * it's the best tradeoff at least on a 1.2GH Duron
825
   */
826
0
  if (xmlBufUse(inbuf) >= reader->cur + CHUNK_SIZE) {
827
0
      val = xmlParseChunk(reader->ctxt,
828
0
                 (const char *) xmlBufContent(inbuf) + reader->cur,
829
0
                                CHUNK_SIZE, 0);
830
0
      reader->cur += CHUNK_SIZE;
831
0
      if (val != 0)
832
0
    reader->ctxt->wellFormed = 0;
833
0
      if (reader->ctxt->wellFormed == 0)
834
0
    break;
835
0
  } else {
836
0
      s = xmlBufUse(inbuf) - reader->cur;
837
0
      val = xmlParseChunk(reader->ctxt,
838
0
     (const char *) xmlBufContent(inbuf) + reader->cur,
839
0
              s, 0);
840
0
      reader->cur += s;
841
0
      if (val != 0)
842
0
    reader->ctxt->wellFormed = 0;
843
0
      break;
844
0
  }
845
0
    }
846
0
    reader->state = oldstate;
847
848
    /*
849
     * Discard the consumed input when needed and possible
850
     */
851
0
    if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
852
0
        if (reader->cur > 80 /* LINE_LEN */) {
853
0
            val = xmlBufShrink(inbuf, reader->cur - 80);
854
0
            if (val >= 0) {
855
0
                reader->cur -= val;
856
0
            }
857
0
        }
858
0
    }
859
860
    /*
861
     * At the end of the stream signal that the work is done to the Push
862
     * parser.
863
     */
864
0
    else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
865
0
  if (reader->state != XML_TEXTREADER_DONE) {
866
0
      s = xmlBufUse(inbuf) - reader->cur;
867
0
      val = xmlParseChunk(reader->ctxt,
868
0
     (const char *) xmlBufContent(inbuf) + reader->cur,
869
0
              s, 1);
870
0
      reader->cur = xmlBufUse(inbuf);
871
0
      reader->state  = XML_TEXTREADER_DONE;
872
0
      if (val != 0) {
873
0
          if (reader->ctxt->wellFormed)
874
0
        reader->ctxt->wellFormed = 0;
875
0
    else
876
0
        return(-1);
877
0
      }
878
0
  }
879
0
    }
880
0
    if (reader->ctxt->wellFormed == 0) {
881
0
  reader->mode = XML_TEXTREADER_MODE_EOF;
882
0
        return(-1);
883
0
    }
884
885
0
    return(0);
886
0
}
887
888
#ifdef LIBXML_REGEXP_ENABLED
889
/**
890
 * Push the current node for validation
891
 *
892
 * @param reader  the xmlTextReader used
893
 */
894
static int
895
0
xmlTextReaderValidatePush(xmlTextReaderPtr reader) {
896
0
    xmlNodePtr node = reader->node;
897
898
0
#ifdef LIBXML_VALID_ENABLED
899
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
900
0
        (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
901
0
  if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
902
0
      reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
903
0
            reader->ctxt->myDoc, node, node->name);
904
0
  } else {
905
0
            xmlChar buf[50];
906
0
      xmlChar *qname;
907
908
0
      qname = xmlBuildQName(node->name, node->ns->prefix, buf, 50);
909
0
            if (qname == NULL) {
910
0
                xmlTextReaderErrMemory(reader);
911
0
                return(-1);
912
0
            }
913
0
      reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
914
0
            reader->ctxt->myDoc, node, qname);
915
0
            if (qname != buf)
916
0
          xmlFree(qname);
917
0
  }
918
        /*if (reader->ctxt->errNo == XML_ERR_NO_MEMORY) {
919
            reader->mode = XML_TEXTREADER_MODE_ERROR;
920
            reader->state = XML_TEXTREADER_ERROR;
921
            return(-1);
922
        }*/
923
0
    }
924
0
#endif /* LIBXML_VALID_ENABLED */
925
0
#ifdef LIBXML_RELAXNG_ENABLED
926
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
927
0
               (reader->rngValidCtxt != NULL)) {
928
0
  int ret;
929
930
0
  if (reader->rngFullNode != NULL) return(0);
931
0
  ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
932
0
                                      reader->ctxt->myDoc,
933
0
              node);
934
0
  if (ret == 0) {
935
      /*
936
       * this element requires a full tree
937
       */
938
0
      node = xmlTextReaderExpand(reader);
939
0
      if (node == NULL) {
940
0
          ret = -1;
941
0
      } else {
942
0
    ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
943
0
                reader->ctxt->myDoc,
944
0
                node);
945
0
    reader->rngFullNode = node;
946
0
      }
947
0
  }
948
0
  if (ret != 1)
949
0
      reader->rngValidErrors++;
950
0
    }
951
0
#endif
952
953
0
    return(0);
954
0
}
955
956
/**
957
 * Push some CData for validation
958
 *
959
 * @param reader  the xmlTextReader used
960
 * @param data  pointer to the CData
961
 * @param len  length of the CData block in bytes.
962
 */
963
static void
964
xmlTextReaderValidateCData(xmlTextReaderPtr reader,
965
0
                           const xmlChar *data, int len) {
966
0
#ifdef LIBXML_VALID_ENABLED
967
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
968
0
        (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
969
0
  reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
970
0
                                              data, len);
971
0
    }
972
0
#endif /* LIBXML_VALID_ENABLED */
973
0
#ifdef LIBXML_RELAXNG_ENABLED
974
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
975
0
               (reader->rngValidCtxt != NULL)) {
976
0
  int ret;
977
978
0
  if (reader->rngFullNode != NULL) return;
979
0
  ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len);
980
0
  if (ret != 1)
981
0
      reader->rngValidErrors++;
982
0
    }
983
0
#endif
984
0
}
985
986
/**
987
 * Pop the current node from validation
988
 *
989
 * @param reader  the xmlTextReader used
990
 */
991
static int
992
0
xmlTextReaderValidatePop(xmlTextReaderPtr reader) {
993
0
    xmlNodePtr node = reader->node;
994
995
0
#ifdef LIBXML_VALID_ENABLED
996
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
997
0
        (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
998
0
  if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
999
0
      reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
1000
0
            reader->ctxt->myDoc, node, node->name);
1001
0
  } else {
1002
0
            xmlChar buf[50];
1003
0
      xmlChar *qname;
1004
1005
0
      qname = xmlBuildQName(node->name, node->ns->prefix, buf, 50);
1006
0
            if (qname == NULL) {
1007
0
                xmlTextReaderErrMemory(reader);
1008
0
                return(-1);
1009
0
            }
1010
0
      reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
1011
0
            reader->ctxt->myDoc, node, qname);
1012
0
            if (qname != buf)
1013
0
          xmlFree(qname);
1014
0
  }
1015
        /*if (reader->ctxt->errNo == XML_ERR_NO_MEMORY) {
1016
            reader->mode = XML_TEXTREADER_MODE_ERROR;
1017
            reader->state = XML_TEXTREADER_ERROR;
1018
            return(-1);
1019
        }*/
1020
0
    }
1021
0
#endif /* LIBXML_VALID_ENABLED */
1022
0
#ifdef LIBXML_RELAXNG_ENABLED
1023
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
1024
0
               (reader->rngValidCtxt != NULL)) {
1025
0
  int ret;
1026
1027
0
  if (reader->rngFullNode != NULL) {
1028
0
      if (node == reader->rngFullNode)
1029
0
          reader->rngFullNode = NULL;
1030
0
      return(0);
1031
0
  }
1032
0
  ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt,
1033
0
                                     reader->ctxt->myDoc,
1034
0
             node);
1035
0
  if (ret != 1)
1036
0
      reader->rngValidErrors++;
1037
0
    }
1038
0
#endif
1039
1040
0
    return(0);
1041
0
}
1042
1043
/**
1044
 * Handle the validation when an entity reference is encountered and
1045
 * entity substitution is not activated. As a result the parser interface
1046
 * must walk through the entity and do the validation calls
1047
 *
1048
 * @param reader  the xmlTextReader used
1049
 */
1050
static int
1051
0
xmlTextReaderValidateEntity(xmlTextReaderPtr reader) {
1052
0
    xmlNodePtr oldnode = reader->node;
1053
0
    xmlNodePtr node = reader->node;
1054
1055
0
    do {
1056
0
  if (node->type == XML_ENTITY_REF_NODE) {
1057
0
      if ((node->children != NULL) &&
1058
0
    (node->children->type == XML_ENTITY_DECL) &&
1059
0
    (node->children->children != NULL)) {
1060
0
    if (xmlTextReaderEntPush(reader, node) < 0) {
1061
0
                    if (node == oldnode)
1062
0
                        break;
1063
0
                    goto skip_children;
1064
0
                }
1065
0
    node = node->children->children;
1066
0
    continue;
1067
0
      } else {
1068
    /*
1069
     * The error has probably been raised already.
1070
     */
1071
0
    if (node == oldnode)
1072
0
        break;
1073
0
                goto skip_children;
1074
0
      }
1075
0
#ifdef LIBXML_REGEXP_ENABLED
1076
0
  } else if (node->type == XML_ELEMENT_NODE) {
1077
0
      reader->node = node;
1078
0
      if (xmlTextReaderValidatePush(reader) < 0)
1079
0
                return(-1);
1080
0
  } else if ((node->type == XML_TEXT_NODE) ||
1081
0
       (node->type == XML_CDATA_SECTION_NODE)) {
1082
0
            xmlTextReaderValidateCData(reader, node->content,
1083
0
                                 xmlStrlen(node->content));
1084
0
#endif
1085
0
  }
1086
1087
  /*
1088
   * go to next node
1089
   */
1090
0
  if (node->children != NULL) {
1091
0
      node = node->children;
1092
0
      continue;
1093
0
  } else if (node->type == XML_ELEMENT_NODE) {
1094
0
      if (xmlTextReaderValidatePop(reader) < 0)
1095
0
                return(-1);
1096
0
  }
1097
0
skip_children:
1098
0
  if (node->next != NULL) {
1099
0
      node = node->next;
1100
0
      continue;
1101
0
  }
1102
0
  do {
1103
0
      node = node->parent;
1104
0
      if (node->type == XML_ELEMENT_NODE) {
1105
0
          xmlNodePtr tmp;
1106
0
    if (reader->entNr == 0) {
1107
0
        while ((tmp = node->last) != NULL) {
1108
0
      if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
1109
0
          xmlUnlinkNode(tmp);
1110
0
          xmlTextReaderFreeNode(reader, tmp);
1111
0
      } else
1112
0
          break;
1113
0
        }
1114
0
    }
1115
0
    reader->node = node;
1116
0
    if (xmlTextReaderValidatePop(reader) < 0)
1117
0
                    return(-1);
1118
0
      }
1119
0
      if ((node->type == XML_ENTITY_DECL) &&
1120
0
    (reader->ent != NULL) && (reader->ent->children == node)) {
1121
0
    node = xmlTextReaderEntPop(reader);
1122
0
      }
1123
0
      if (node == oldnode)
1124
0
    break;
1125
0
      if (node->next != NULL) {
1126
0
    node = node->next;
1127
0
    break;
1128
0
      }
1129
0
  } while ((node != NULL) && (node != oldnode));
1130
0
    } while ((node != NULL) && (node != oldnode));
1131
0
    reader->node = oldnode;
1132
1133
0
    return(0);
1134
0
}
1135
#endif /* LIBXML_REGEXP_ENABLED */
1136
1137
1138
/**
1139
 * Get the successor of a node if available.
1140
 *
1141
 * @param cur  the current node
1142
 * @returns the successor node or NULL
1143
 */
1144
static xmlNodePtr
1145
0
xmlTextReaderGetSuccessor(xmlNodePtr cur) {
1146
0
    if (cur == NULL) return(NULL) ; /* ERROR */
1147
0
    if (cur->next != NULL) return(cur->next) ;
1148
0
    do {
1149
0
        cur = cur->parent;
1150
0
        if (cur == NULL) break;
1151
0
        if (cur->next != NULL) return(cur->next);
1152
0
    } while (cur != NULL);
1153
0
    return(cur);
1154
0
}
1155
1156
/**
1157
 * Makes sure that the current node is fully read as well as all its
1158
 * descendant. It means the full DOM subtree must be available at the
1159
 * end of the call.
1160
 *
1161
 * @param reader  the xmlTextReader used
1162
 * @returns 1 if the node was expanded successfully, 0 if there is no more
1163
 *          nodes to read, or -1 in case of error
1164
 */
1165
static int
1166
0
xmlTextReaderDoExpand(xmlTextReaderPtr reader) {
1167
0
    int val;
1168
1169
0
    if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
1170
0
        return(-1);
1171
0
    do {
1172
0
  if (PARSER_STOPPED(reader->ctxt))
1173
0
            return(1);
1174
1175
0
        if (xmlTextReaderGetSuccessor(reader->node) != NULL)
1176
0
      return(1);
1177
0
  if (reader->ctxt->nodeNr < reader->depth)
1178
0
      return(1);
1179
0
  if (reader->mode == XML_TEXTREADER_MODE_EOF)
1180
0
      return(1);
1181
0
  val = xmlTextReaderPushData(reader);
1182
0
  if (val < 0){
1183
0
      reader->mode = XML_TEXTREADER_MODE_ERROR;
1184
0
            reader->state = XML_TEXTREADER_ERROR;
1185
0
      return(-1);
1186
0
  }
1187
0
    } while(reader->mode != XML_TEXTREADER_MODE_EOF);
1188
0
    return(1);
1189
0
}
1190
1191
/**
1192
 *  Moves the position of the current instance to the next node in
1193
 *  the stream, exposing its properties.
1194
 *
1195
 * @param reader  the xmlTextReader used
1196
 * @returns 1 if the node was read successfully, 0 if there is no more
1197
 *          nodes to read, or -1 in case of error
1198
 */
1199
int
1200
0
xmlTextReaderRead(xmlTextReader *reader) {
1201
0
    int val, olddepth = 0;
1202
0
    xmlTextReaderState oldstate = XML_TEXTREADER_START;
1203
0
    xmlNodePtr oldnode = NULL;
1204
1205
0
    if (reader == NULL)
1206
0
  return(-1);
1207
0
    if (reader->state == XML_TEXTREADER_ERROR)
1208
0
        return(-1);
1209
1210
0
    reader->curnode = NULL;
1211
0
    if (reader->doc != NULL)
1212
0
        return(xmlTextReaderReadTree(reader));
1213
0
    if (reader->ctxt == NULL)
1214
0
  return(-1);
1215
1216
0
    if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
1217
0
  reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
1218
  /*
1219
   * Initial state
1220
   */
1221
0
  do {
1222
0
      val = xmlTextReaderPushData(reader);
1223
0
            if (val < 0) {
1224
0
                reader->mode = XML_TEXTREADER_MODE_ERROR;
1225
0
                reader->state = XML_TEXTREADER_ERROR;
1226
0
                return(-1);
1227
0
            }
1228
0
  } while ((reader->ctxt->node == NULL) &&
1229
0
     ((reader->mode != XML_TEXTREADER_MODE_EOF) &&
1230
0
      (reader->state != XML_TEXTREADER_DONE)));
1231
0
  if (reader->ctxt->node == NULL) {
1232
0
      if (reader->ctxt->myDoc != NULL) {
1233
0
    reader->node = reader->ctxt->myDoc->children;
1234
0
      }
1235
0
      if (reader->node == NULL) {
1236
0
                reader->mode = XML_TEXTREADER_MODE_ERROR;
1237
0
                reader->state = XML_TEXTREADER_ERROR;
1238
0
    return(-1);
1239
0
      }
1240
0
      reader->state = XML_TEXTREADER_ELEMENT;
1241
0
  } else {
1242
0
      if (reader->ctxt->myDoc != NULL) {
1243
0
    reader->node = reader->ctxt->myDoc->children;
1244
0
      }
1245
0
      if (reader->node == NULL)
1246
0
    reader->node = reader->ctxt->nodeTab[0];
1247
0
      reader->state = XML_TEXTREADER_ELEMENT;
1248
0
  }
1249
0
  reader->depth = 0;
1250
0
  reader->ctxt->parseMode = XML_PARSE_READER;
1251
0
  goto node_found;
1252
0
    }
1253
0
    oldstate = reader->state;
1254
0
    olddepth = reader->ctxt->nodeNr;
1255
0
    oldnode = reader->node;
1256
1257
0
get_next_node:
1258
0
    if (reader->node == NULL) {
1259
0
  if (reader->mode == XML_TEXTREADER_MODE_EOF) {
1260
0
      return(0);
1261
0
        } else {
1262
0
            reader->mode = XML_TEXTREADER_MODE_ERROR;
1263
0
            reader->state = XML_TEXTREADER_ERROR;
1264
0
      return(-1);
1265
0
        }
1266
0
    }
1267
1268
    /*
1269
     * If we are not backtracking on ancestors or examined nodes,
1270
     * that the parser didn't finished or that we aren't at the end
1271
     * of stream, continue processing.
1272
     */
1273
0
    while ((reader->node != NULL) && (reader->node->next == NULL) &&
1274
0
     (reader->ctxt->nodeNr == olddepth) &&
1275
0
           ((oldstate == XML_TEXTREADER_BACKTRACK) ||
1276
0
            (reader->node->children == NULL) ||
1277
0
      (reader->node->type == XML_ENTITY_REF_NODE) ||
1278
0
      ((reader->node->children != NULL) &&
1279
0
       (reader->node->children->type == XML_TEXT_NODE) &&
1280
0
       (reader->node->children->next == NULL)) ||
1281
0
      (reader->node->type == XML_DTD_NODE) ||
1282
0
      (reader->node->type == XML_DOCUMENT_NODE) ||
1283
0
      (reader->node->type == XML_HTML_DOCUMENT_NODE)) &&
1284
0
     ((reader->ctxt->node == NULL) ||
1285
0
      (reader->ctxt->node == reader->node) ||
1286
0
      (reader->ctxt->node == reader->node->parent)) &&
1287
0
     (reader->ctxt->instate != XML_PARSER_EOF) &&
1288
0
     (PARSER_STOPPED(reader->ctxt) == 0)) {
1289
0
  val = xmlTextReaderPushData(reader);
1290
0
  if (val < 0) {
1291
0
            reader->mode = XML_TEXTREADER_MODE_ERROR;
1292
0
            reader->state = XML_TEXTREADER_ERROR;
1293
0
      return(-1);
1294
0
        }
1295
0
  if (reader->node == NULL)
1296
0
      goto node_end;
1297
0
    }
1298
0
    if (oldstate != XML_TEXTREADER_BACKTRACK) {
1299
0
  if ((reader->node->children != NULL) &&
1300
0
      (reader->node->type != XML_ENTITY_REF_NODE) &&
1301
0
      (reader->node->type != XML_XINCLUDE_START) &&
1302
0
      (reader->node->type != XML_DTD_NODE)) {
1303
0
      reader->node = reader->node->children;
1304
0
      reader->depth++;
1305
0
      reader->state = XML_TEXTREADER_ELEMENT;
1306
0
      goto node_found;
1307
0
  }
1308
0
    }
1309
0
    if (reader->node->next != NULL) {
1310
0
  if ((oldstate == XML_TEXTREADER_ELEMENT) &&
1311
0
            (reader->node->type == XML_ELEMENT_NODE) &&
1312
0
      (reader->node->children == NULL) &&
1313
0
      ((reader->node->extra & NODE_IS_EMPTY) == 0)
1314
0
#ifdef LIBXML_XINCLUDE_ENABLED
1315
0
      && (reader->in_xinclude <= 0)
1316
0
#endif
1317
0
      ) {
1318
0
      reader->state = XML_TEXTREADER_END;
1319
0
      goto node_found;
1320
0
  }
1321
0
#ifdef LIBXML_REGEXP_ENABLED
1322
0
  if ((reader->validate) &&
1323
0
      (reader->node->type == XML_ELEMENT_NODE))
1324
0
      if (xmlTextReaderValidatePop(reader) < 0)
1325
0
                return(-1);
1326
0
#endif /* LIBXML_REGEXP_ENABLED */
1327
0
        if ((reader->preserves > 0) &&
1328
0
      (reader->node->extra & NODE_IS_SPRESERVED))
1329
0
      reader->preserves--;
1330
0
  reader->node = reader->node->next;
1331
0
  reader->state = XML_TEXTREADER_ELEMENT;
1332
1333
  /*
1334
   * Cleanup of the old node
1335
   */
1336
0
  if ((reader->preserves == 0) &&
1337
0
#ifdef LIBXML_XINCLUDE_ENABLED
1338
0
      (reader->in_xinclude == 0) &&
1339
0
#endif
1340
0
      (reader->entNr == 0) &&
1341
0
      (reader->node->prev != NULL) &&
1342
0
            (reader->node->prev->type != XML_DTD_NODE)) {
1343
0
      xmlNodePtr tmp = reader->node->prev;
1344
0
      if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
1345
0
                if (oldnode == tmp)
1346
0
                    oldnode = NULL;
1347
0
    xmlUnlinkNode(tmp);
1348
0
    xmlTextReaderFreeNode(reader, tmp);
1349
0
      }
1350
0
  }
1351
1352
0
  goto node_found;
1353
0
    }
1354
0
    if ((oldstate == XML_TEXTREADER_ELEMENT) &&
1355
0
  (reader->node->type == XML_ELEMENT_NODE) &&
1356
0
  (reader->node->children == NULL) &&
1357
0
  ((reader->node->extra & NODE_IS_EMPTY) == 0)) {;
1358
0
  reader->state = XML_TEXTREADER_END;
1359
0
  goto node_found;
1360
0
    }
1361
0
#ifdef LIBXML_REGEXP_ENABLED
1362
0
    if ((reader->validate != XML_TEXTREADER_NOT_VALIDATE) &&
1363
0
        (reader->node->type == XML_ELEMENT_NODE)) {
1364
0
        if (xmlTextReaderValidatePop(reader) < 0)
1365
0
            return(-1);
1366
0
    }
1367
0
#endif /* LIBXML_REGEXP_ENABLED */
1368
0
    if ((reader->preserves > 0) &&
1369
0
  (reader->node->extra & NODE_IS_SPRESERVED))
1370
0
  reader->preserves--;
1371
0
    reader->node = reader->node->parent;
1372
0
    if ((reader->node == NULL) ||
1373
0
  (reader->node->type == XML_DOCUMENT_NODE) ||
1374
0
  (reader->node->type == XML_HTML_DOCUMENT_NODE)) {
1375
0
  if (reader->mode != XML_TEXTREADER_MODE_EOF) {
1376
0
      val = xmlParseChunk(reader->ctxt, "", 0, 1);
1377
0
      reader->state = XML_TEXTREADER_DONE;
1378
0
      if (val != 0) {
1379
0
                reader->mode = XML_TEXTREADER_MODE_ERROR;
1380
0
                reader->state = XML_TEXTREADER_ERROR;
1381
0
          return(-1);
1382
0
            }
1383
0
  }
1384
0
  reader->node = NULL;
1385
0
  reader->depth = -1;
1386
1387
  /*
1388
   * Cleanup of the old node
1389
   */
1390
0
  if ((oldnode != NULL) && (reader->preserves == 0) &&
1391
0
#ifdef LIBXML_XINCLUDE_ENABLED
1392
0
      (reader->in_xinclude == 0) &&
1393
0
#endif
1394
0
      (reader->entNr == 0) &&
1395
0
      (oldnode->type != XML_DTD_NODE) &&
1396
0
      ((oldnode->extra & NODE_IS_PRESERVED) == 0)) {
1397
0
      xmlUnlinkNode(oldnode);
1398
0
      xmlTextReaderFreeNode(reader, oldnode);
1399
0
  }
1400
1401
0
  goto node_end;
1402
0
    }
1403
0
    if ((reader->preserves == 0) &&
1404
0
#ifdef LIBXML_XINCLUDE_ENABLED
1405
0
        (reader->in_xinclude == 0) &&
1406
0
#endif
1407
0
  (reader->entNr == 0) &&
1408
0
        (reader->node->last != NULL) &&
1409
0
        ((reader->node->last->extra & NODE_IS_PRESERVED) == 0)) {
1410
0
  xmlNodePtr tmp = reader->node->last;
1411
0
  xmlUnlinkNode(tmp);
1412
0
  xmlTextReaderFreeNode(reader, tmp);
1413
0
    }
1414
0
    reader->depth--;
1415
0
    reader->state = XML_TEXTREADER_BACKTRACK;
1416
1417
0
node_found:
1418
    /*
1419
     * If we are in the middle of a piece of CDATA make sure it's finished
1420
     */
1421
0
    if ((reader->node != NULL) &&
1422
0
        (reader->node->next == NULL) &&
1423
0
        ((reader->node->type == XML_TEXT_NODE) ||
1424
0
   (reader->node->type == XML_CDATA_SECTION_NODE))) {
1425
0
            if (xmlTextReaderExpand(reader) == NULL)
1426
0
          return -1;
1427
0
    }
1428
1429
0
#ifdef LIBXML_XINCLUDE_ENABLED
1430
    /*
1431
     * Handle XInclude if asked for
1432
     */
1433
0
    if ((reader->xinclude) && (reader->in_xinclude == 0) &&
1434
0
        (reader->state != XML_TEXTREADER_BACKTRACK) &&
1435
0
        (reader->node != NULL) &&
1436
0
  (reader->node->type == XML_ELEMENT_NODE) &&
1437
0
  (reader->node->ns != NULL) &&
1438
0
  ((xmlStrEqual(reader->node->ns->href, XINCLUDE_NS)) ||
1439
0
   (xmlStrEqual(reader->node->ns->href, XINCLUDE_OLD_NS)))) {
1440
0
  if (reader->xincctxt == NULL) {
1441
0
      reader->xincctxt = xmlXIncludeNewContext(reader->ctxt->myDoc);
1442
0
            if (reader->xincctxt == NULL) {
1443
0
                xmlTextReaderErrMemory(reader);
1444
0
                return(-1);
1445
0
            }
1446
0
      xmlXIncludeSetFlags(reader->xincctxt,
1447
0
                          reader->parserFlags & (~XML_PARSE_NOXINCNODE));
1448
0
            xmlXIncludeSetStreamingMode(reader->xincctxt, 1);
1449
0
            if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
1450
0
                xmlXIncludeSetErrorHandler(reader->xincctxt,
1451
0
                        xmlTextReaderStructuredRelay, reader);
1452
0
            if (reader->resourceLoader != NULL)
1453
0
                xmlXIncludeSetResourceLoader(reader->xincctxt,
1454
0
                        reader->resourceLoader, reader->resourceCtxt);
1455
0
  }
1456
  /*
1457
   * expand that node and process it
1458
   */
1459
0
  if (xmlTextReaderExpand(reader) == NULL)
1460
0
      return(-1);
1461
0
        if (xmlXIncludeProcessNode(reader->xincctxt, reader->node) < 0) {
1462
0
            int err = xmlXIncludeGetLastError(reader->xincctxt);
1463
1464
0
            if (xmlIsCatastrophicError(XML_ERR_FATAL, err)) {
1465
0
                xmlFatalErr(reader->ctxt, err, NULL);
1466
0
                reader->mode = XML_TEXTREADER_MODE_ERROR;
1467
0
                reader->state = XML_TEXTREADER_ERROR;
1468
0
            }
1469
0
            return(-1);
1470
0
        }
1471
0
    }
1472
0
    if ((reader->node != NULL) && (reader->node->type == XML_XINCLUDE_START)) {
1473
0
        reader->in_xinclude++;
1474
0
  goto get_next_node;
1475
0
    }
1476
0
    if ((reader->node != NULL) && (reader->node->type == XML_XINCLUDE_END)) {
1477
0
        reader->in_xinclude--;
1478
0
  goto get_next_node;
1479
0
    }
1480
0
#endif
1481
    /*
1482
     * Handle entities enter and exit when in entity replacement mode
1483
     */
1484
0
    if ((reader->node != NULL) &&
1485
0
  (reader->node->type == XML_ENTITY_REF_NODE) &&
1486
0
  (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) {
1487
0
  if ((reader->node->children != NULL) &&
1488
0
      (reader->node->children->type == XML_ENTITY_DECL) &&
1489
0
      (reader->node->children->children != NULL)) {
1490
0
      if (xmlTextReaderEntPush(reader, reader->node) < 0)
1491
0
                goto get_next_node;
1492
0
      reader->node = reader->node->children->children;
1493
0
  }
1494
0
#ifdef LIBXML_REGEXP_ENABLED
1495
0
    } else if ((reader->node != NULL) &&
1496
0
         (reader->node->type == XML_ENTITY_REF_NODE) &&
1497
0
         (reader->ctxt != NULL) && (reader->validate)) {
1498
0
  if (xmlTextReaderValidateEntity(reader) < 0)
1499
0
            return(-1);
1500
0
#endif /* LIBXML_REGEXP_ENABLED */
1501
0
    }
1502
0
    if ((reader->node != NULL) &&
1503
0
  (reader->node->type == XML_ENTITY_DECL) &&
1504
0
  (reader->ent != NULL) && (reader->ent->children == reader->node)) {
1505
0
  reader->node = xmlTextReaderEntPop(reader);
1506
0
  reader->depth++;
1507
0
        goto get_next_node;
1508
0
    }
1509
0
#ifdef LIBXML_REGEXP_ENABLED
1510
0
    if ((reader->validate != XML_TEXTREADER_NOT_VALIDATE) && (reader->node != NULL)) {
1511
0
  xmlNodePtr node = reader->node;
1512
1513
0
  if ((node->type == XML_ELEMENT_NODE) &&
1514
0
            ((reader->state != XML_TEXTREADER_END) &&
1515
0
       (reader->state != XML_TEXTREADER_BACKTRACK))) {
1516
0
      if (xmlTextReaderValidatePush(reader) < 0)
1517
0
                return(-1);
1518
0
  } else if ((node->type == XML_TEXT_NODE) ||
1519
0
       (node->type == XML_CDATA_SECTION_NODE)) {
1520
0
            xmlTextReaderValidateCData(reader, node->content,
1521
0
                                 xmlStrlen(node->content));
1522
0
  }
1523
0
    }
1524
0
#endif /* LIBXML_REGEXP_ENABLED */
1525
0
#ifdef LIBXML_PATTERN_ENABLED
1526
0
    if ((reader->patternNr > 0) && (reader->state != XML_TEXTREADER_END) &&
1527
0
        (reader->state != XML_TEXTREADER_BACKTRACK)) {
1528
0
        int i;
1529
0
  for (i = 0;i < reader->patternNr;i++) {
1530
0
       if (xmlPatternMatch(reader->patternTab[i], reader->node) == 1) {
1531
0
           xmlTextReaderPreserve(reader);
1532
0
     break;
1533
0
             }
1534
0
  }
1535
0
    }
1536
0
#endif /* LIBXML_PATTERN_ENABLED */
1537
0
#ifdef LIBXML_SCHEMAS_ENABLED
1538
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_XSD) &&
1539
0
        (reader->xsdValidErrors == 0) &&
1540
0
  (reader->xsdValidCtxt != NULL)) {
1541
0
  reader->xsdValidErrors = !xmlSchemaIsValid(reader->xsdValidCtxt);
1542
0
    }
1543
0
#endif /* LIBXML_PATTERN_ENABLED */
1544
0
    return(1);
1545
0
node_end:
1546
0
    reader->state = XML_TEXTREADER_DONE;
1547
0
    return(0);
1548
0
}
1549
1550
/**
1551
 * Gets the read state of the reader.
1552
 *
1553
 * @param reader  the xmlTextReader used
1554
 * @returns the state value, or -1 in case of error
1555
 */
1556
int
1557
0
xmlTextReaderReadState(xmlTextReader *reader) {
1558
0
    if (reader == NULL)
1559
0
  return(-1);
1560
0
    return(reader->mode);
1561
0
}
1562
1563
/**
1564
 * Reads the contents of the current node and the full subtree. It then makes
1565
 * the subtree available until the next #xmlTextReaderRead call
1566
 *
1567
 * @param reader  the xmlTextReader used
1568
 * @returns a node pointer valid until the next #xmlTextReaderRead call
1569
 *         or NULL in case of error.
1570
 */
1571
xmlNode *
1572
0
xmlTextReaderExpand(xmlTextReader *reader) {
1573
0
    if ((reader == NULL) || (reader->node == NULL))
1574
0
        return(NULL);
1575
0
    if (reader->doc != NULL)
1576
0
        return(reader->node);
1577
0
    if (reader->ctxt == NULL)
1578
0
        return(NULL);
1579
0
    if (xmlTextReaderDoExpand(reader) < 0)
1580
0
        return(NULL);
1581
0
    return(reader->node);
1582
0
}
1583
1584
/**
1585
 * Skip to the node following the current one in document order while
1586
 * avoiding the subtree if any.
1587
 *
1588
 * @param reader  the xmlTextReader used
1589
 * @returns 1 if the node was read successfully, 0 if there is no more
1590
 *          nodes to read, or -1 in case of error
1591
 */
1592
int
1593
0
xmlTextReaderNext(xmlTextReader *reader) {
1594
0
    int ret;
1595
0
    xmlNodePtr cur;
1596
1597
0
    if (reader == NULL)
1598
0
  return(-1);
1599
0
    if (reader->doc != NULL)
1600
0
        return(xmlTextReaderNextTree(reader));
1601
0
    cur = reader->node;
1602
0
    if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
1603
0
        return(xmlTextReaderRead(reader));
1604
0
    if (reader->state == XML_TEXTREADER_END || reader->state == XML_TEXTREADER_BACKTRACK)
1605
0
        return(xmlTextReaderRead(reader));
1606
0
    if (cur->extra & NODE_IS_EMPTY)
1607
0
        return(xmlTextReaderRead(reader));
1608
0
    do {
1609
0
        ret = xmlTextReaderRead(reader);
1610
0
  if (ret != 1)
1611
0
      return(ret);
1612
0
    } while (reader->node != cur);
1613
0
    return(xmlTextReaderRead(reader));
1614
0
}
1615
1616
#ifdef LIBXML_WRITER_ENABLED
1617
static void
1618
xmlTextReaderDumpCopy(xmlTextReaderPtr reader, xmlOutputBufferPtr output,
1619
0
                      xmlNodePtr node) {
1620
0
    if ((node->type == XML_ELEMENT_DECL) ||
1621
0
        (node->type == XML_ATTRIBUTE_DECL) ||
1622
0
        (node->type == XML_ENTITY_DECL))
1623
0
        return;
1624
1625
0
    if ((node->type == XML_DOCUMENT_NODE) ||
1626
0
        (node->type == XML_HTML_DOCUMENT_NODE)) {
1627
0
        xmlNodeDumpOutput(output, node->doc, node, 0, 0, NULL);
1628
0
    } else {
1629
0
        xmlNodePtr copy;
1630
1631
        /*
1632
         * Create a copy to make sure that namespace declarations from
1633
         * ancestors are added.
1634
         */
1635
0
        copy = xmlDocCopyNode(node, node->doc, 1);
1636
0
        if (copy == NULL) {
1637
0
            xmlTextReaderErrMemory(reader);
1638
0
            return;
1639
0
        }
1640
1641
0
        xmlNodeDumpOutput(output, copy->doc, copy, 0, 0, NULL);
1642
1643
0
        xmlFreeNode(copy);
1644
0
    }
1645
0
}
1646
1647
/**
1648
 * Reads the contents of the current node, including child nodes and markup.
1649
 *
1650
 * @param reader  the xmlTextReader used
1651
 * @returns a string containing the XML content, or NULL if the current node
1652
 *         is neither an element nor attribute, or has no child nodes. The
1653
 *         string must be deallocated by the caller.
1654
 */
1655
xmlChar *
1656
xmlTextReaderReadInnerXml(xmlTextReader *reader)
1657
0
{
1658
0
    xmlOutputBufferPtr output;
1659
0
    xmlNodePtr cur;
1660
0
    xmlChar *ret;
1661
1662
0
    if (xmlTextReaderExpand(reader) == NULL)
1663
0
        return(NULL);
1664
1665
0
    if (reader->node == NULL)
1666
0
        return(NULL);
1667
1668
0
    output = xmlAllocOutputBuffer(NULL);
1669
0
    if (output == NULL) {
1670
0
        xmlTextReaderErrMemory(reader);
1671
0
        return(NULL);
1672
0
    }
1673
1674
0
    for (cur = reader->node->children; cur != NULL; cur = cur->next)
1675
0
        xmlTextReaderDumpCopy(reader, output, cur);
1676
1677
0
    if (output->error)
1678
0
        xmlCtxtErrIO(reader->ctxt, output->error, NULL);
1679
1680
0
    ret = xmlBufDetach(output->buffer);
1681
0
    xmlOutputBufferClose(output);
1682
1683
0
    return(ret);
1684
0
}
1685
1686
/**
1687
 * Reads the contents of the current node, including child nodes and markup.
1688
 *
1689
 * @param reader  the xmlTextReader used
1690
 * @returns a string containing the node and any XML content, or NULL if the
1691
 *         current node cannot be serialized. The string must be deallocated
1692
 *         by the caller.
1693
 */
1694
xmlChar *
1695
xmlTextReaderReadOuterXml(xmlTextReader *reader)
1696
0
{
1697
0
    xmlOutputBufferPtr output;
1698
0
    xmlNodePtr node;
1699
0
    xmlChar *ret;
1700
1701
0
    if (xmlTextReaderExpand(reader) == NULL)
1702
0
        return(NULL);
1703
1704
0
    node = reader->node;
1705
0
    if (node == NULL)
1706
0
        return(NULL);
1707
1708
0
    output = xmlAllocOutputBuffer(NULL);
1709
0
    if (output == NULL) {
1710
0
        xmlTextReaderErrMemory(reader);
1711
0
        return(NULL);
1712
0
    }
1713
1714
0
    xmlTextReaderDumpCopy(reader, output, node);
1715
0
    if (output->error)
1716
0
        xmlCtxtErrIO(reader->ctxt, output->error, NULL);
1717
1718
0
    ret = xmlBufDetach(output->buffer);
1719
0
    xmlOutputBufferClose(output);
1720
1721
0
    return(ret);
1722
0
}
1723
#endif
1724
1725
/**
1726
 * Reads the contents of an element or a text node as a string.
1727
 *
1728
 * @param reader  the xmlTextReader used
1729
 * @returns a string containing the contents of the non-empty Element or
1730
 *         Text node (including CDATA sections), or NULL if the reader
1731
 *         is positioned on any other type of node.
1732
 *         The string must be deallocated by the caller.
1733
 */
1734
xmlChar *
1735
xmlTextReaderReadString(xmlTextReader *reader)
1736
0
{
1737
0
    xmlNodePtr node, cur;
1738
0
    xmlBufPtr buf;
1739
0
    xmlChar *ret;
1740
1741
0
    if ((reader == NULL) || (reader->node == NULL))
1742
0
       return(NULL);
1743
1744
0
    node = (reader->curnode != NULL) ? reader->curnode : reader->node;
1745
0
    switch (node->type) {
1746
0
        case XML_TEXT_NODE:
1747
0
        case XML_CDATA_SECTION_NODE:
1748
0
            break;
1749
0
        case XML_ELEMENT_NODE:
1750
0
            if ((xmlTextReaderDoExpand(reader) == -1) ||
1751
0
                (node->children == NULL))
1752
0
                return(NULL);
1753
0
            break;
1754
0
        default:
1755
0
            return(NULL);
1756
0
    }
1757
1758
0
    buf = xmlBufCreate(50);
1759
0
    if (buf == NULL) {
1760
0
        xmlTextReaderErrMemory(reader);
1761
0
        return(NULL);
1762
0
    }
1763
1764
0
    cur = node;
1765
0
    while (cur != NULL) {
1766
0
        switch (cur->type) {
1767
0
            case XML_TEXT_NODE:
1768
0
            case XML_CDATA_SECTION_NODE:
1769
0
                xmlBufCat(buf, cur->content);
1770
0
                break;
1771
1772
0
            case XML_ELEMENT_NODE:
1773
0
                if (cur->children != NULL) {
1774
0
                    cur = cur->children;
1775
0
                    continue;
1776
0
                }
1777
0
                break;
1778
1779
0
            default:
1780
0
                break;
1781
0
        }
1782
1783
0
        if (cur == node)
1784
0
            goto done;
1785
1786
0
        while (cur->next == NULL) {
1787
0
            cur = cur->parent;
1788
0
            if (cur == node)
1789
0
                goto done;
1790
0
        }
1791
0
        cur = cur->next;
1792
0
    }
1793
1794
0
done:
1795
0
    ret = xmlBufDetach(buf);
1796
0
    if (ret == NULL)
1797
0
        xmlTextReaderErrMemory(reader);
1798
1799
0
    xmlBufFree(buf);
1800
0
    return(ret);
1801
0
}
1802
1803
/************************************************************************
1804
 *                  *
1805
 *      Operating on a preparsed tree     *
1806
 *                  *
1807
 ************************************************************************/
1808
static int
1809
xmlTextReaderNextTree(xmlTextReaderPtr reader)
1810
0
{
1811
0
    if (reader == NULL)
1812
0
        return(-1);
1813
1814
0
    if (reader->state == XML_TEXTREADER_END)
1815
0
        return(0);
1816
1817
0
    if (reader->node == NULL) {
1818
0
        if (reader->doc->children == NULL) {
1819
0
            reader->state = XML_TEXTREADER_END;
1820
0
            return(0);
1821
0
        }
1822
1823
0
        reader->node = reader->doc->children;
1824
0
        reader->state = XML_TEXTREADER_START;
1825
0
        return(1);
1826
0
    }
1827
1828
0
    if (reader->state != XML_TEXTREADER_BACKTRACK) {
1829
  /* Here removed traversal to child, because we want to skip the subtree,
1830
  replace with traversal to sibling to skip subtree */
1831
0
        if (reader->node->next != 0) {
1832
      /* Move to sibling if present,skipping sub-tree */
1833
0
            reader->node = reader->node->next;
1834
0
            reader->state = XML_TEXTREADER_START;
1835
0
            return(1);
1836
0
        }
1837
1838
  /* if reader->node->next is NULL mean no subtree for current node,
1839
  so need to move to sibling of parent node if present */
1840
0
  reader->state = XML_TEXTREADER_BACKTRACK;
1841
  /* This will move to parent if present */
1842
0
  xmlTextReaderRead(reader);
1843
0
    }
1844
1845
0
    if (reader->node->next != 0) {
1846
0
        reader->node = reader->node->next;
1847
0
        reader->state = XML_TEXTREADER_START;
1848
0
        return(1);
1849
0
    }
1850
1851
0
    if (reader->node->parent != 0) {
1852
0
        if (reader->node->parent->type == XML_DOCUMENT_NODE) {
1853
0
            reader->state = XML_TEXTREADER_END;
1854
0
            return(0);
1855
0
        }
1856
1857
0
        reader->node = reader->node->parent;
1858
0
        reader->depth--;
1859
0
        reader->state = XML_TEXTREADER_BACKTRACK;
1860
  /* Repeat process to move to sibling of parent node if present */
1861
0
        xmlTextReaderNextTree(reader);
1862
0
    }
1863
1864
0
    reader->state = XML_TEXTREADER_END;
1865
1866
0
    return(1);
1867
0
}
1868
1869
/**
1870
 *  Moves the position of the current instance to the next node in
1871
 *  the stream, exposing its properties.
1872
 *
1873
 * @param reader  the xmlTextReader used
1874
 * @returns 1 if the node was read successfully, 0 if there is no more
1875
 *          nodes to read, or -1 in case of error
1876
 */
1877
static int
1878
0
xmlTextReaderReadTree(xmlTextReaderPtr reader) {
1879
0
    if (reader->state == XML_TEXTREADER_END)
1880
0
        return(0);
1881
1882
0
next_node:
1883
0
    if (reader->node == NULL) {
1884
0
        if (reader->doc->children == NULL) {
1885
0
            reader->state = XML_TEXTREADER_END;
1886
0
            return(0);
1887
0
        }
1888
1889
0
        reader->node = reader->doc->children;
1890
0
        reader->state = XML_TEXTREADER_START;
1891
0
        goto found_node;
1892
0
    }
1893
1894
0
    if ((reader->state != XML_TEXTREADER_BACKTRACK) &&
1895
0
        (reader->node->type != XML_DTD_NODE) &&
1896
0
        (reader->node->type != XML_XINCLUDE_START) &&
1897
0
  (reader->node->type != XML_ENTITY_REF_NODE)) {
1898
0
        if (reader->node->children != NULL) {
1899
0
            reader->node = reader->node->children;
1900
0
            reader->depth++;
1901
0
            reader->state = XML_TEXTREADER_START;
1902
0
            goto found_node;
1903
0
        }
1904
1905
0
        if (reader->node->type == XML_ATTRIBUTE_NODE) {
1906
0
            reader->state = XML_TEXTREADER_BACKTRACK;
1907
0
            goto found_node;
1908
0
        }
1909
0
    }
1910
1911
0
    if (reader->node->next != NULL) {
1912
0
        reader->node = reader->node->next;
1913
0
        reader->state = XML_TEXTREADER_START;
1914
0
        goto found_node;
1915
0
    }
1916
1917
0
    if (reader->node->parent != NULL) {
1918
0
        if ((reader->node->parent->type == XML_DOCUMENT_NODE) ||
1919
0
      (reader->node->parent->type == XML_HTML_DOCUMENT_NODE)) {
1920
0
            reader->state = XML_TEXTREADER_END;
1921
0
            return(0);
1922
0
        }
1923
1924
0
        reader->node = reader->node->parent;
1925
0
        reader->depth--;
1926
0
        reader->state = XML_TEXTREADER_BACKTRACK;
1927
0
        goto found_node;
1928
0
    }
1929
1930
0
    reader->state = XML_TEXTREADER_END;
1931
1932
0
found_node:
1933
0
    if ((reader->node->type == XML_XINCLUDE_START) ||
1934
0
        (reader->node->type == XML_XINCLUDE_END))
1935
0
  goto next_node;
1936
1937
0
    return(1);
1938
0
}
1939
1940
/**
1941
 * Skip to the node following the current one in document order while
1942
 * avoiding the subtree if any.
1943
 * Currently implemented only for Readers built on a document
1944
 *
1945
 * @param reader  the xmlTextReader used
1946
 * @returns 1 if the node was read successfully, 0 if there is no more
1947
 *          nodes to read, or -1 in case of error
1948
 */
1949
int
1950
0
xmlTextReaderNextSibling(xmlTextReader *reader) {
1951
0
    if (reader == NULL)
1952
0
        return(-1);
1953
0
    if (reader->doc == NULL) {
1954
        /* TODO */
1955
0
  return(-1);
1956
0
    }
1957
1958
0
    if (reader->state == XML_TEXTREADER_END)
1959
0
        return(0);
1960
1961
0
    if (reader->node == NULL)
1962
0
        return(xmlTextReaderNextTree(reader));
1963
1964
0
    if (reader->node->next != NULL) {
1965
0
        reader->node = reader->node->next;
1966
0
        reader->state = XML_TEXTREADER_START;
1967
0
        return(1);
1968
0
    }
1969
1970
0
    return(0);
1971
0
}
1972
1973
/************************************************************************
1974
 *                  *
1975
 *      Constructor and destructors     *
1976
 *                  *
1977
 ************************************************************************/
1978
/**
1979
 * Create an xmlTextReader structure fed with `input`
1980
 *
1981
 * @param input  the xmlParserInputBuffer used to read data
1982
 * @param URI  the URI information for the source if available
1983
 * @returns the new xmlTextReader or NULL in case of error
1984
 */
1985
xmlTextReader *
1986
0
xmlNewTextReader(xmlParserInputBuffer *input, const char *URI) {
1987
0
    xmlTextReaderPtr ret;
1988
1989
0
    if (input == NULL)
1990
0
  return(NULL);
1991
0
    ret = xmlMalloc(sizeof(xmlTextReader));
1992
0
    if (ret == NULL)
1993
0
  return(NULL);
1994
0
    memset(ret, 0, sizeof(xmlTextReader));
1995
0
    ret->doc = NULL;
1996
0
    ret->entTab = NULL;
1997
0
    ret->entMax = 0;
1998
0
    ret->entNr = 0;
1999
0
    ret->input = input;
2000
0
    ret->buffer = xmlBufCreate(50);
2001
0
    if (ret->buffer == NULL) {
2002
0
        xmlFree(ret);
2003
0
  return(NULL);
2004
0
    }
2005
0
    ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
2006
0
    if (ret->sax == NULL) {
2007
0
  xmlBufFree(ret->buffer);
2008
0
  xmlFree(ret);
2009
0
  return(NULL);
2010
0
    }
2011
0
    xmlSAXVersion(ret->sax, 2);
2012
0
    ret->startElement = ret->sax->startElement;
2013
0
    ret->sax->startElement = xmlTextReaderStartElement;
2014
0
    ret->endElement = ret->sax->endElement;
2015
0
    ret->sax->endElement = xmlTextReaderEndElement;
2016
0
#ifdef LIBXML_SAX1_ENABLED
2017
0
    if (ret->sax->initialized == XML_SAX2_MAGIC) {
2018
0
#endif /* LIBXML_SAX1_ENABLED */
2019
0
  ret->startElementNs = ret->sax->startElementNs;
2020
0
  ret->sax->startElementNs = xmlTextReaderStartElementNs;
2021
0
  ret->endElementNs = ret->sax->endElementNs;
2022
0
  ret->sax->endElementNs = xmlTextReaderEndElementNs;
2023
0
#ifdef LIBXML_SAX1_ENABLED
2024
0
    } else {
2025
0
  ret->startElementNs = NULL;
2026
0
  ret->endElementNs = NULL;
2027
0
    }
2028
0
#endif /* LIBXML_SAX1_ENABLED */
2029
0
    ret->characters = ret->sax->characters;
2030
0
    ret->sax->characters = xmlTextReaderCharacters;
2031
0
    ret->sax->ignorableWhitespace = xmlTextReaderCharacters;
2032
0
    ret->cdataBlock = ret->sax->cdataBlock;
2033
0
    ret->sax->cdataBlock = xmlTextReaderCDataBlock;
2034
2035
0
    ret->mode = XML_TEXTREADER_MODE_INITIAL;
2036
0
    ret->node = NULL;
2037
0
    ret->curnode = NULL;
2038
0
    if (xmlBufUse(ret->input->buffer) < 4) {
2039
0
  xmlParserInputBufferRead(input, 4);
2040
0
    }
2041
0
    if (xmlBufUse(ret->input->buffer) >= 4) {
2042
0
  ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
2043
0
           (const char *) xmlBufContent(ret->input->buffer),
2044
0
                                            4, URI);
2045
0
  ret->base = 0;
2046
0
  ret->cur = 4;
2047
0
    } else {
2048
0
  ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
2049
0
  ret->base = 0;
2050
0
  ret->cur = 0;
2051
0
    }
2052
2053
0
    if (ret->ctxt == NULL) {
2054
0
  xmlBufFree(ret->buffer);
2055
0
  xmlFree(ret->sax);
2056
0
  xmlFree(ret);
2057
0
  return(NULL);
2058
0
    }
2059
0
    ret->ctxt->parseMode = XML_PARSE_READER;
2060
0
    ret->ctxt->_private = ret;
2061
0
    ret->ctxt->dictNames = 1;
2062
0
    ret->allocs = XML_TEXTREADER_CTXT;
2063
    /*
2064
     * use the parser dictionary to allocate all elements and attributes names
2065
     */
2066
0
    ret->dict = ret->ctxt->dict;
2067
0
#ifdef LIBXML_XINCLUDE_ENABLED
2068
0
    ret->xinclude = 0;
2069
0
#endif
2070
0
#ifdef LIBXML_PATTERN_ENABLED
2071
0
    ret->patternMax = 0;
2072
0
    ret->patternTab = NULL;
2073
0
#endif
2074
0
    return(ret);
2075
0
}
2076
2077
/**
2078
 * Create an xmlTextReader structure fed with the resource at `URI`
2079
 *
2080
 * @param URI  the URI of the resource to process
2081
 * @returns the new xmlTextReader or NULL in case of error
2082
 */
2083
xmlTextReader *
2084
0
xmlNewTextReaderFilename(const char *URI) {
2085
0
    xmlParserInputBufferPtr input;
2086
0
    xmlTextReaderPtr ret;
2087
2088
0
    if (xmlParserInputBufferCreateFilenameValue != NULL) {
2089
0
        input = xmlParserInputBufferCreateFilenameValue(URI,
2090
0
                XML_CHAR_ENCODING_NONE);
2091
0
        if (input == NULL) {
2092
0
            xmlTextReaderErr(XML_IO_ENOENT, "filaed to open %s", URI);
2093
0
            return(NULL);
2094
0
        }
2095
0
    } else {
2096
0
        xmlParserErrors code;
2097
2098
        /*
2099
         * TODO: Remove XML_INPUT_UNZIP
2100
         */
2101
0
        code = xmlParserInputBufferCreateUrl(URI, XML_CHAR_ENCODING_NONE,
2102
0
                                             XML_INPUT_UNZIP, &input);
2103
0
        if (code != XML_ERR_OK) {
2104
0
            xmlTextReaderErr(code, "failed to open %s", URI);
2105
0
            return(NULL);
2106
0
        }
2107
0
    }
2108
2109
0
    ret = xmlNewTextReader(input, URI);
2110
0
    if (ret == NULL) {
2111
0
        xmlTextReaderErrMemory(NULL);
2112
0
  xmlFreeParserInputBuffer(input);
2113
0
  return(NULL);
2114
0
    }
2115
0
    ret->allocs |= XML_TEXTREADER_INPUT;
2116
0
    return(ret);
2117
0
}
2118
2119
/**
2120
 * Deallocate all the resources associated to the reader
2121
 *
2122
 * @param reader  the xmlTextReader
2123
 */
2124
void
2125
0
xmlFreeTextReader(xmlTextReader *reader) {
2126
0
    if (reader == NULL)
2127
0
  return;
2128
0
#ifdef LIBXML_RELAXNG_ENABLED
2129
0
    if (reader->rngSchemas != NULL) {
2130
0
  xmlRelaxNGFree(reader->rngSchemas);
2131
0
  reader->rngSchemas = NULL;
2132
0
    }
2133
0
    if (reader->rngValidCtxt != NULL) {
2134
0
  if (! reader->rngPreserveCtxt)
2135
0
      xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2136
0
  reader->rngValidCtxt = NULL;
2137
0
    }
2138
0
#endif
2139
0
#ifdef LIBXML_SCHEMAS_ENABLED
2140
0
    if (reader->xsdPlug != NULL) {
2141
0
  xmlSchemaSAXUnplug(reader->xsdPlug);
2142
0
  reader->xsdPlug = NULL;
2143
0
    }
2144
0
    if (reader->xsdValidCtxt != NULL) {
2145
0
  if (! reader->xsdPreserveCtxt)
2146
0
      xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
2147
0
  reader->xsdValidCtxt = NULL;
2148
0
    }
2149
0
    if (reader->xsdSchemas != NULL) {
2150
0
  xmlSchemaFree(reader->xsdSchemas);
2151
0
  reader->xsdSchemas = NULL;
2152
0
    }
2153
0
#endif
2154
0
#ifdef LIBXML_XINCLUDE_ENABLED
2155
0
    if (reader->xincctxt != NULL)
2156
0
  xmlXIncludeFreeContext(reader->xincctxt);
2157
0
#endif
2158
0
#ifdef LIBXML_PATTERN_ENABLED
2159
0
    if (reader->patternTab != NULL) {
2160
0
        int i;
2161
0
  for (i = 0;i < reader->patternNr;i++) {
2162
0
      if (reader->patternTab[i] != NULL)
2163
0
          xmlFreePattern(reader->patternTab[i]);
2164
0
  }
2165
0
  xmlFree(reader->patternTab);
2166
0
    }
2167
0
#endif
2168
0
    if (reader->mode != XML_TEXTREADER_MODE_CLOSED)
2169
0
        xmlTextReaderClose(reader);
2170
0
    if (reader->ctxt != NULL) {
2171
0
        if (reader->dict == reader->ctxt->dict)
2172
0
      reader->dict = NULL;
2173
0
  if (reader->allocs & XML_TEXTREADER_CTXT)
2174
0
      xmlFreeParserCtxt(reader->ctxt);
2175
0
    }
2176
0
    if (reader->sax != NULL)
2177
0
  xmlFree(reader->sax);
2178
0
    if (reader->buffer != NULL)
2179
0
        xmlBufFree(reader->buffer);
2180
0
    if (reader->entTab != NULL)
2181
0
  xmlFree(reader->entTab);
2182
0
    if (reader->dict != NULL)
2183
0
        xmlDictFree(reader->dict);
2184
0
    xmlFree(reader);
2185
0
}
2186
2187
/************************************************************************
2188
 *                  *
2189
 *      Methods for XmlTextReader     *
2190
 *                  *
2191
 ************************************************************************/
2192
2193
/**
2194
 * This method releases any resources allocated by the current instance
2195
 * changes the state to Closed and close any underlying input.
2196
 *
2197
 * @param reader  the xmlTextReader used
2198
 * @returns 0 or -1 in case of error
2199
 */
2200
int
2201
0
xmlTextReaderClose(xmlTextReader *reader) {
2202
0
    if (reader == NULL)
2203
0
  return(-1);
2204
0
    reader->node = NULL;
2205
0
    reader->curnode = NULL;
2206
0
    reader->mode = XML_TEXTREADER_MODE_CLOSED;
2207
0
    if (reader->faketext != NULL) {
2208
0
        xmlFreeNode(reader->faketext);
2209
0
        reader->faketext = NULL;
2210
0
    }
2211
0
    if (reader->ctxt != NULL) {
2212
0
#ifdef LIBXML_VALID_ENABLED
2213
0
  if ((reader->ctxt->vctxt.vstateTab != NULL) &&
2214
0
      (reader->ctxt->vctxt.vstateMax > 0)){
2215
0
#ifdef LIBXML_REGEXP_ENABLED
2216
0
            while (reader->ctxt->vctxt.vstateNr > 0)
2217
0
                xmlValidatePopElement(&reader->ctxt->vctxt, NULL, NULL, NULL);
2218
0
#endif /* LIBXML_REGEXP_ENABLED */
2219
0
      xmlFree(reader->ctxt->vctxt.vstateTab);
2220
0
      reader->ctxt->vctxt.vstateTab = NULL;
2221
0
      reader->ctxt->vctxt.vstateMax = 0;
2222
0
  }
2223
0
#endif /* LIBXML_VALID_ENABLED */
2224
0
  xmlStopParser(reader->ctxt);
2225
0
  if (reader->ctxt->myDoc != NULL) {
2226
0
      if (reader->preserve == 0)
2227
0
    xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
2228
0
      reader->ctxt->myDoc = NULL;
2229
0
  }
2230
0
    }
2231
0
    if ((reader->input != NULL)  && (reader->allocs & XML_TEXTREADER_INPUT)) {
2232
0
  xmlFreeParserInputBuffer(reader->input);
2233
0
  reader->allocs -= XML_TEXTREADER_INPUT;
2234
0
    }
2235
0
    return(0);
2236
0
}
2237
2238
/**
2239
 * Provides the value of the attribute with the specified index relative
2240
 * to the containing element.
2241
 *
2242
 * @param reader  the xmlTextReader used
2243
 * @param no  the zero-based index of the attribute relative to the containing element
2244
 * @returns a string containing the value of the specified attribute, or NULL
2245
 *    in case of error. The string must be deallocated by the caller.
2246
 */
2247
xmlChar *
2248
0
xmlTextReaderGetAttributeNo(xmlTextReader *reader, int no) {
2249
0
    xmlChar *ret;
2250
0
    int i;
2251
0
    xmlAttrPtr cur;
2252
0
    xmlNsPtr ns;
2253
2254
0
    if (reader == NULL)
2255
0
  return(NULL);
2256
0
    if (reader->node == NULL)
2257
0
  return(NULL);
2258
0
    if (reader->curnode != NULL)
2259
0
  return(NULL);
2260
    /* TODO: handle the xmlDecl */
2261
0
    if (reader->node->type != XML_ELEMENT_NODE)
2262
0
  return(NULL);
2263
2264
0
    ns = reader->node->nsDef;
2265
0
    for (i = 0;(i < no) && (ns != NULL);i++) {
2266
0
  ns = ns->next;
2267
0
    }
2268
0
    if (ns != NULL)
2269
0
  return(readerStrdup(reader, ns->href));
2270
2271
0
    cur = reader->node->properties;
2272
0
    if (cur == NULL)
2273
0
  return(NULL);
2274
0
    for (;i < no;i++) {
2275
0
  cur = cur->next;
2276
0
  if (cur == NULL)
2277
0
      return(NULL);
2278
0
    }
2279
    /* TODO walk the DTD if present */
2280
2281
0
    if (cur->children == NULL)
2282
0
        return(NULL);
2283
0
    ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
2284
0
    if (ret == NULL)
2285
0
        xmlTextReaderErrMemory(reader);
2286
0
    return(ret);
2287
0
}
2288
2289
/**
2290
 * Provides the value of the attribute with the specified qualified name.
2291
 *
2292
 * @param reader  the xmlTextReader used
2293
 * @param name  the qualified name of the attribute.
2294
 * @returns a string containing the value of the specified attribute, or NULL
2295
 *    in case of error. The string must be deallocated by the caller.
2296
 */
2297
xmlChar *
2298
0
xmlTextReaderGetAttribute(xmlTextReader *reader, const xmlChar *name) {
2299
0
    xmlChar *prefix = NULL;
2300
0
    const xmlChar *localname;
2301
0
    xmlNsPtr ns;
2302
0
    xmlChar *ret = NULL;
2303
0
    int result;
2304
2305
0
    if ((reader == NULL) || (name == NULL))
2306
0
  return(NULL);
2307
0
    if (reader->node == NULL)
2308
0
  return(NULL);
2309
0
    if (reader->curnode != NULL)
2310
0
  return(NULL);
2311
2312
    /* TODO: handle the xmlDecl */
2313
0
    if (reader->node->type != XML_ELEMENT_NODE)
2314
0
  return(NULL);
2315
2316
0
    localname = xmlSplitQName4(name, &prefix);
2317
0
    if (localname == NULL) {
2318
0
        xmlTextReaderErrMemory(reader);
2319
0
        return(NULL);
2320
0
    }
2321
0
    if (prefix == NULL) {
2322
        /*
2323
         * Namespace default decl
2324
         */
2325
0
        if (xmlStrEqual(name, BAD_CAST "xmlns")) {
2326
0
            ns = reader->node->nsDef;
2327
0
            while (ns != NULL) {
2328
0
                if (ns->prefix == NULL) {
2329
0
                    return(readerStrdup(reader, ns->href));
2330
0
                }
2331
0
                ns = ns->next;
2332
0
            }
2333
0
            return NULL;
2334
0
        }
2335
2336
0
        result = xmlNodeGetAttrValue(reader->node, name, NULL, &ret);
2337
0
        if (result < 0)
2338
0
            xmlTextReaderErrMemory(reader);
2339
0
        return(ret);
2340
0
    }
2341
2342
    /*
2343
     * Namespace default decl
2344
     */
2345
0
    if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
2346
0
        ns = reader->node->nsDef;
2347
0
        while (ns != NULL) {
2348
0
            if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
2349
0
                ret = readerStrdup(reader, ns->href);
2350
0
                break;
2351
0
            }
2352
0
            ns = ns->next;
2353
0
        }
2354
0
    } else {
2355
0
        result = xmlSearchNsSafe(reader->node, prefix, &ns);
2356
0
        if (result < 0)
2357
0
            xmlTextReaderErrMemory(reader);
2358
0
        if (ns != NULL) {
2359
0
            result = xmlNodeGetAttrValue(reader->node, localname, ns->href,
2360
0
                                         &ret);
2361
0
            if (result < 0)
2362
0
                xmlTextReaderErrMemory(reader);
2363
0
        }
2364
0
    }
2365
2366
0
    if (prefix != NULL)
2367
0
        xmlFree(prefix);
2368
0
    return(ret);
2369
0
}
2370
2371
2372
/**
2373
 * Provides the value of the specified attribute
2374
 *
2375
 * @param reader  the xmlTextReader used
2376
 * @param localName  the local name of the attribute.
2377
 * @param namespaceURI  the namespace URI of the attribute.
2378
 * @returns a string containing the value of the specified attribute, or NULL
2379
 *    in case of error. The string must be deallocated by the caller.
2380
 */
2381
xmlChar *
2382
xmlTextReaderGetAttributeNs(xmlTextReader *reader, const xmlChar *localName,
2383
0
          const xmlChar *namespaceURI) {
2384
0
    xmlChar *ret = NULL;
2385
0
    xmlChar *prefix = NULL;
2386
0
    xmlNsPtr ns;
2387
0
    int result;
2388
2389
0
    if ((reader == NULL) || (localName == NULL))
2390
0
  return(NULL);
2391
0
    if (reader->node == NULL)
2392
0
  return(NULL);
2393
0
    if (reader->curnode != NULL)
2394
0
  return(NULL);
2395
2396
    /* TODO: handle the xmlDecl */
2397
0
    if (reader->node->type != XML_ELEMENT_NODE)
2398
0
  return(NULL);
2399
2400
0
    if (xmlStrEqual(namespaceURI, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
2401
0
        if (! xmlStrEqual(localName, BAD_CAST "xmlns")) {
2402
0
            prefix = BAD_CAST localName;
2403
0
        }
2404
0
        ns = reader->node->nsDef;
2405
0
        while (ns != NULL) {
2406
0
            if ((prefix == NULL && ns->prefix == NULL) ||
2407
0
                ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localName)))) {
2408
0
                return readerStrdup(reader, ns->href);
2409
0
            }
2410
0
            ns = ns->next;
2411
0
        }
2412
0
        return NULL;
2413
0
    }
2414
2415
0
    result = xmlNodeGetAttrValue(reader->node, localName, namespaceURI, &ret);
2416
0
    if (result < 0)
2417
0
        xmlTextReaderErrMemory(reader);
2418
2419
0
    return(ret);
2420
0
}
2421
2422
/**
2423
 * Method to get the remainder of the buffered XML. this method stops the
2424
 * parser, set its state to End Of File and return the input stream with
2425
 * what is left that the parser did not use.
2426
 *
2427
 * The implementation is not good, the parser certainly progressed past
2428
 * what's left in reader->input, and there is an allocation problem. Best
2429
 * would be to rewrite it differently.
2430
 *
2431
 * @param reader  the xmlTextReader used
2432
 * @returns the xmlParserInputBuffer attached to the XML or NULL
2433
 *    in case of error.
2434
 */
2435
xmlParserInputBuffer *
2436
0
xmlTextReaderGetRemainder(xmlTextReader *reader) {
2437
0
    xmlParserInputBufferPtr ret = NULL;
2438
2439
0
    if (reader == NULL)
2440
0
  return(NULL);
2441
0
    if (reader->node == NULL)
2442
0
  return(NULL);
2443
2444
0
    reader->node = NULL;
2445
0
    reader->curnode = NULL;
2446
0
    reader->mode = XML_TEXTREADER_MODE_EOF;
2447
0
    if (reader->ctxt != NULL) {
2448
0
  xmlStopParser(reader->ctxt);
2449
0
  if (reader->ctxt->myDoc != NULL) {
2450
0
      if (reader->preserve == 0)
2451
0
    xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
2452
0
      reader->ctxt->myDoc = NULL;
2453
0
  }
2454
0
    }
2455
0
    if (reader->allocs & XML_TEXTREADER_INPUT) {
2456
0
  ret = reader->input;
2457
0
  reader->input = NULL;
2458
0
  reader->allocs -= XML_TEXTREADER_INPUT;
2459
0
    } else {
2460
  /*
2461
   * Hum, one may need to duplicate the data structure because
2462
   * without reference counting the input may be freed twice:
2463
   *   - by the layer which allocated it.
2464
   *   - by the layer to which would have been returned to.
2465
   */
2466
0
  return(NULL);
2467
0
    }
2468
0
    return(ret);
2469
0
}
2470
2471
/**
2472
 * Resolves a namespace prefix in the scope of the current element.
2473
 *
2474
 * @param reader  the xmlTextReader used
2475
 * @param prefix  the prefix whose namespace URI is to be resolved. To return
2476
 *          the default namespace, specify NULL
2477
 * @returns a string containing the namespace URI to which the prefix maps
2478
 *    or NULL in case of error. The string must be deallocated by the caller.
2479
 */
2480
xmlChar *
2481
0
xmlTextReaderLookupNamespace(xmlTextReader *reader, const xmlChar *prefix) {
2482
0
    xmlNsPtr ns;
2483
0
    int result;
2484
2485
0
    if (reader == NULL)
2486
0
  return(NULL);
2487
0
    if (reader->node == NULL)
2488
0
  return(NULL);
2489
2490
0
    result = xmlSearchNsSafe(reader->node, prefix, &ns);
2491
0
    if (result < 0) {
2492
0
        xmlTextReaderErrMemory(reader);
2493
0
        return(NULL);
2494
0
    }
2495
0
    if (ns == NULL)
2496
0
  return(NULL);
2497
0
    return(readerStrdup(reader, ns->href));
2498
0
}
2499
2500
/**
2501
 * Moves the position of the current instance to the attribute with
2502
 * the specified index relative to the containing element.
2503
 *
2504
 * @param reader  the xmlTextReader used
2505
 * @param no  the zero-based index of the attribute relative to the containing
2506
 *      element.
2507
 * @returns 1 in case of success, -1 in case of error, 0 if not found
2508
 */
2509
int
2510
0
xmlTextReaderMoveToAttributeNo(xmlTextReader *reader, int no) {
2511
0
    int i;
2512
0
    xmlAttrPtr cur;
2513
0
    xmlNsPtr ns;
2514
2515
0
    if (reader == NULL)
2516
0
  return(-1);
2517
0
    if (reader->node == NULL)
2518
0
  return(-1);
2519
    /* TODO: handle the xmlDecl */
2520
0
    if (reader->node->type != XML_ELEMENT_NODE)
2521
0
  return(-1);
2522
2523
0
    reader->curnode = NULL;
2524
2525
0
    ns = reader->node->nsDef;
2526
0
    for (i = 0;(i < no) && (ns != NULL);i++) {
2527
0
  ns = ns->next;
2528
0
    }
2529
0
    if (ns != NULL) {
2530
0
  reader->curnode = (xmlNodePtr) ns;
2531
0
  return(1);
2532
0
    }
2533
2534
0
    cur = reader->node->properties;
2535
0
    if (cur == NULL)
2536
0
  return(0);
2537
0
    for (;i < no;i++) {
2538
0
  cur = cur->next;
2539
0
  if (cur == NULL)
2540
0
      return(0);
2541
0
    }
2542
    /* TODO walk the DTD if present */
2543
2544
0
    reader->curnode = (xmlNodePtr) cur;
2545
0
    return(1);
2546
0
}
2547
2548
/**
2549
 * Moves the position of the current instance to the attribute with
2550
 * the specified qualified name.
2551
 *
2552
 * @param reader  the xmlTextReader used
2553
 * @param name  the qualified name of the attribute.
2554
 * @returns 1 in case of success, -1 in case of error, 0 if not found
2555
 */
2556
int
2557
0
xmlTextReaderMoveToAttribute(xmlTextReader *reader, const xmlChar *name) {
2558
0
    xmlChar *prefix = NULL;
2559
0
    const xmlChar *localname;
2560
0
    xmlNsPtr ns;
2561
0
    xmlAttrPtr prop;
2562
2563
0
    if ((reader == NULL) || (name == NULL))
2564
0
  return(-1);
2565
0
    if (reader->node == NULL)
2566
0
  return(-1);
2567
2568
    /* TODO: handle the xmlDecl */
2569
0
    if (reader->node->type != XML_ELEMENT_NODE)
2570
0
  return(0);
2571
2572
0
    localname = xmlSplitQName4(name, &prefix);
2573
0
    if (localname == NULL) {
2574
0
        xmlTextReaderErrMemory(reader);
2575
0
        return(-1);
2576
0
    }
2577
0
    if (prefix == NULL) {
2578
  /*
2579
   * Namespace default decl
2580
   */
2581
0
  if (xmlStrEqual(name, BAD_CAST "xmlns")) {
2582
0
      ns = reader->node->nsDef;
2583
0
      while (ns != NULL) {
2584
0
    if (ns->prefix == NULL) {
2585
0
        reader->curnode = (xmlNodePtr) ns;
2586
0
        return(1);
2587
0
    }
2588
0
    ns = ns->next;
2589
0
      }
2590
0
      return(0);
2591
0
  }
2592
2593
0
  prop = reader->node->properties;
2594
0
  while (prop != NULL) {
2595
      /*
2596
       * One need to have
2597
       *   - same attribute names
2598
       *   - and the attribute carrying that namespace
2599
       */
2600
0
      if ((xmlStrEqual(prop->name, name)) &&
2601
0
    ((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
2602
0
    reader->curnode = (xmlNodePtr) prop;
2603
0
    return(1);
2604
0
      }
2605
0
      prop = prop->next;
2606
0
  }
2607
0
  return(0);
2608
0
    }
2609
2610
    /*
2611
     * Namespace default decl
2612
     */
2613
0
    if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
2614
0
  ns = reader->node->nsDef;
2615
0
  while (ns != NULL) {
2616
0
      if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
2617
0
    reader->curnode = (xmlNodePtr) ns;
2618
0
    goto found;
2619
0
      }
2620
0
      ns = ns->next;
2621
0
  }
2622
0
  goto not_found;
2623
0
    }
2624
0
    prop = reader->node->properties;
2625
0
    while (prop != NULL) {
2626
  /*
2627
   * One need to have
2628
   *   - same attribute names
2629
   *   - and the attribute carrying that namespace
2630
   */
2631
0
  if ((xmlStrEqual(prop->name, localname)) &&
2632
0
      (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
2633
0
      reader->curnode = (xmlNodePtr) prop;
2634
0
      goto found;
2635
0
  }
2636
0
  prop = prop->next;
2637
0
    }
2638
0
not_found:
2639
0
    if (prefix != NULL)
2640
0
        xmlFree(prefix);
2641
0
    return(0);
2642
2643
0
found:
2644
0
    if (prefix != NULL)
2645
0
        xmlFree(prefix);
2646
0
    return(1);
2647
0
}
2648
2649
/**
2650
 * Moves the position of the current instance to the attribute with the
2651
 * specified local name and namespace URI.
2652
 *
2653
 * @param reader  the xmlTextReader used
2654
 * @param localName  the local name of the attribute.
2655
 * @param namespaceURI  the namespace URI of the attribute.
2656
 * @returns 1 in case of success, -1 in case of error, 0 if not found
2657
 */
2658
int
2659
xmlTextReaderMoveToAttributeNs(xmlTextReader *reader,
2660
0
  const xmlChar *localName, const xmlChar *namespaceURI) {
2661
0
    xmlAttrPtr prop;
2662
0
    xmlNodePtr node;
2663
0
    xmlNsPtr ns;
2664
0
    xmlChar *prefix = NULL;
2665
2666
0
    if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
2667
0
  return(-1);
2668
0
    if (reader->node == NULL)
2669
0
  return(-1);
2670
0
    if (reader->node->type != XML_ELEMENT_NODE)
2671
0
  return(0);
2672
0
    node = reader->node;
2673
2674
0
    if (xmlStrEqual(namespaceURI, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
2675
0
    if (! xmlStrEqual(localName, BAD_CAST "xmlns")) {
2676
0
      prefix = BAD_CAST localName;
2677
0
    }
2678
0
    ns = reader->node->nsDef;
2679
0
    while (ns != NULL) {
2680
0
      if ((prefix == NULL && ns->prefix == NULL) ||
2681
0
        ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localName)))) {
2682
0
        reader->curnode = (xmlNodePtr) ns;
2683
0
        return(1);
2684
0
      }
2685
0
      ns = ns->next;
2686
0
    }
2687
0
    return(0);
2688
0
    }
2689
2690
0
    prop = node->properties;
2691
0
    while (prop != NULL) {
2692
  /*
2693
   * One need to have
2694
   *   - same attribute names
2695
   *   - and the attribute carrying that namespace
2696
   */
2697
0
        if (xmlStrEqual(prop->name, localName) &&
2698
0
      ((prop->ns != NULL) &&
2699
0
       (xmlStrEqual(prop->ns->href, namespaceURI)))) {
2700
0
      reader->curnode = (xmlNodePtr) prop;
2701
0
      return(1);
2702
0
        }
2703
0
  prop = prop->next;
2704
0
    }
2705
0
    return(0);
2706
0
}
2707
2708
/**
2709
 * Moves the position of the current instance to the first attribute
2710
 * associated with the current node.
2711
 *
2712
 * @param reader  the xmlTextReader used
2713
 * @returns 1 in case of success, -1 in case of error, 0 if not found
2714
 */
2715
int
2716
0
xmlTextReaderMoveToFirstAttribute(xmlTextReader *reader) {
2717
0
    if (reader == NULL)
2718
0
  return(-1);
2719
0
    if (reader->node == NULL)
2720
0
  return(-1);
2721
0
    if (reader->node->type != XML_ELEMENT_NODE)
2722
0
  return(0);
2723
2724
0
    if (reader->node->nsDef != NULL) {
2725
0
  reader->curnode = (xmlNodePtr) reader->node->nsDef;
2726
0
  return(1);
2727
0
    }
2728
0
    if (reader->node->properties != NULL) {
2729
0
  reader->curnode = (xmlNodePtr) reader->node->properties;
2730
0
  return(1);
2731
0
    }
2732
0
    return(0);
2733
0
}
2734
2735
/**
2736
 * Moves the position of the current instance to the next attribute
2737
 * associated with the current node.
2738
 *
2739
 * @param reader  the xmlTextReader used
2740
 * @returns 1 in case of success, -1 in case of error, 0 if not found
2741
 */
2742
int
2743
0
xmlTextReaderMoveToNextAttribute(xmlTextReader *reader) {
2744
0
    if (reader == NULL)
2745
0
  return(-1);
2746
0
    if (reader->node == NULL)
2747
0
  return(-1);
2748
0
    if (reader->node->type != XML_ELEMENT_NODE)
2749
0
  return(0);
2750
0
    if (reader->curnode == NULL)
2751
0
  return(xmlTextReaderMoveToFirstAttribute(reader));
2752
2753
0
    if (reader->curnode->type == XML_NAMESPACE_DECL) {
2754
0
  xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2755
0
  if (ns->next != NULL) {
2756
0
      reader->curnode = (xmlNodePtr) ns->next;
2757
0
      return(1);
2758
0
  }
2759
0
  if (reader->node->properties != NULL) {
2760
0
      reader->curnode = (xmlNodePtr) reader->node->properties;
2761
0
      return(1);
2762
0
  }
2763
0
  return(0);
2764
0
    } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
2765
0
         (reader->curnode->next != NULL)) {
2766
0
  reader->curnode = reader->curnode->next;
2767
0
  return(1);
2768
0
    }
2769
0
    return(0);
2770
0
}
2771
2772
/**
2773
 * Moves the position of the current instance to the node that
2774
 * contains the current Attribute  node.
2775
 *
2776
 * @param reader  the xmlTextReader used
2777
 * @returns 1 in case of success, -1 in case of error, 0 if not moved
2778
 */
2779
int
2780
0
xmlTextReaderMoveToElement(xmlTextReader *reader) {
2781
0
    if (reader == NULL)
2782
0
  return(-1);
2783
0
    if (reader->node == NULL)
2784
0
  return(-1);
2785
0
    if (reader->node->type != XML_ELEMENT_NODE)
2786
0
  return(0);
2787
0
    if (reader->curnode != NULL) {
2788
0
  reader->curnode = NULL;
2789
0
  return(1);
2790
0
    }
2791
0
    return(0);
2792
0
}
2793
2794
/**
2795
 * Parses an attribute value into one or more Text and EntityReference nodes.
2796
 *
2797
 * @param reader  the xmlTextReader used
2798
 * @returns 1 in case of success, 0 if the reader was not positioned on an
2799
 *         attribute node or all the attribute values have been read, or -1
2800
 *         in case of error.
2801
 */
2802
int
2803
0
xmlTextReaderReadAttributeValue(xmlTextReader *reader) {
2804
0
    if (reader == NULL)
2805
0
  return(-1);
2806
0
    if (reader->node == NULL)
2807
0
  return(-1);
2808
0
    if (reader->curnode == NULL)
2809
0
  return(0);
2810
0
    if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
2811
0
  if (reader->curnode->children == NULL)
2812
0
      return(0);
2813
0
  reader->curnode = reader->curnode->children;
2814
0
    } else if (reader->curnode->type == XML_NAMESPACE_DECL) {
2815
0
  xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2816
2817
0
  if (reader->faketext == NULL) {
2818
0
      reader->faketext = xmlNewDocText(reader->node->doc,
2819
0
                                 ns->href);
2820
0
            if (reader->faketext == NULL) {
2821
0
                xmlTextReaderErrMemory(reader);
2822
0
                return(-1);
2823
0
            }
2824
0
  } else {
2825
0
            if ((reader->faketext->content != NULL) &&
2826
0
          (reader->faketext->content !=
2827
0
     (xmlChar *) &(reader->faketext->properties)))
2828
0
    xmlFree(reader->faketext->content);
2829
0
            if (ns->href == NULL) {
2830
0
                reader->faketext->content = NULL;
2831
0
            } else {
2832
0
                reader->faketext->content = xmlStrdup(ns->href);
2833
0
                if (reader->faketext->content == NULL) {
2834
0
                    xmlTextReaderErrMemory(reader);
2835
0
                    return(-1);
2836
0
                }
2837
0
            }
2838
0
  }
2839
0
  reader->curnode = reader->faketext;
2840
0
    } else {
2841
0
  if (reader->curnode->next == NULL)
2842
0
      return(0);
2843
0
  reader->curnode = reader->curnode->next;
2844
0
    }
2845
0
    return(1);
2846
0
}
2847
2848
/**
2849
 * Determine the encoding of the document being read.
2850
 *
2851
 * @param reader  the xmlTextReader used
2852
 * @returns a string containing the encoding of the document or NULL in
2853
 * case of error.  The string is deallocated with the reader.
2854
 */
2855
const xmlChar *
2856
0
xmlTextReaderConstEncoding(xmlTextReader *reader) {
2857
0
    const xmlChar *encoding = NULL;
2858
2859
0
    if (reader == NULL)
2860
0
        return(NULL);
2861
2862
0
    if (reader->ctxt != NULL)
2863
0
        encoding = xmlGetActualEncoding(reader->ctxt);
2864
0
    else if (reader->doc != NULL)
2865
0
        encoding = reader->doc->encoding;
2866
2867
0
    return(constString(reader, encoding));
2868
0
}
2869
2870
2871
/************************************************************************
2872
 *                  *
2873
 *      Access API to the current node      *
2874
 *                  *
2875
 ************************************************************************/
2876
/**
2877
 * Provides the number of attributes of the current node
2878
 *
2879
 * @param reader  the xmlTextReader used
2880
 * @returns 0 i no attributes, -1 in case of error or the attribute count
2881
 */
2882
int
2883
0
xmlTextReaderAttributeCount(xmlTextReader *reader) {
2884
0
    int ret;
2885
0
    xmlAttrPtr attr;
2886
0
    xmlNsPtr ns;
2887
0
    xmlNodePtr node;
2888
2889
0
    if (reader == NULL)
2890
0
  return(-1);
2891
0
    if (reader->node == NULL)
2892
0
  return(0);
2893
2894
0
    if (reader->curnode != NULL)
2895
0
  node = reader->curnode;
2896
0
    else
2897
0
  node = reader->node;
2898
2899
0
    if (node->type != XML_ELEMENT_NODE)
2900
0
  return(0);
2901
0
    if ((reader->state == XML_TEXTREADER_END) ||
2902
0
  (reader->state == XML_TEXTREADER_BACKTRACK))
2903
0
  return(0);
2904
0
    ret = 0;
2905
0
    attr = node->properties;
2906
0
    while (attr != NULL) {
2907
0
  ret++;
2908
0
  attr = attr->next;
2909
0
    }
2910
0
    ns = node->nsDef;
2911
0
    while (ns != NULL) {
2912
0
  ret++;
2913
0
  ns = ns->next;
2914
0
    }
2915
0
    return(ret);
2916
0
}
2917
2918
/**
2919
 * Get the node type of the current node
2920
 * Reference:
2921
 * http://www.gnu.org/software/dotgnu/pnetlib-doc/System/Xml/XmlNodeType.html
2922
 *
2923
 * @param reader  the xmlTextReader used
2924
 * @returns the xmlReaderTypes of the current node or -1 in case of error
2925
 */
2926
int
2927
0
xmlTextReaderNodeType(xmlTextReader *reader) {
2928
0
    xmlNodePtr node;
2929
2930
0
    if (reader == NULL)
2931
0
  return(-1);
2932
0
    if (reader->node == NULL)
2933
0
  return(XML_READER_TYPE_NONE);
2934
0
    if (reader->curnode != NULL)
2935
0
  node = reader->curnode;
2936
0
    else
2937
0
  node = reader->node;
2938
0
    switch (node->type) {
2939
0
        case XML_ELEMENT_NODE:
2940
0
      if ((reader->state == XML_TEXTREADER_END) ||
2941
0
    (reader->state == XML_TEXTREADER_BACKTRACK))
2942
0
    return(XML_READER_TYPE_END_ELEMENT);
2943
0
      return(XML_READER_TYPE_ELEMENT);
2944
0
        case XML_NAMESPACE_DECL:
2945
0
        case XML_ATTRIBUTE_NODE:
2946
0
      return(XML_READER_TYPE_ATTRIBUTE);
2947
0
        case XML_TEXT_NODE:
2948
0
      if (xmlIsBlankNode(reader->node)) {
2949
0
    if (xmlNodeGetSpacePreserve(reader->node))
2950
0
        return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE);
2951
0
    else
2952
0
        return(XML_READER_TYPE_WHITESPACE);
2953
0
      } else {
2954
0
    return(XML_READER_TYPE_TEXT);
2955
0
      }
2956
0
        case XML_CDATA_SECTION_NODE:
2957
0
      return(XML_READER_TYPE_CDATA);
2958
0
        case XML_ENTITY_REF_NODE:
2959
0
      return(XML_READER_TYPE_ENTITY_REFERENCE);
2960
0
        case XML_ENTITY_NODE:
2961
0
      return(XML_READER_TYPE_ENTITY);
2962
0
        case XML_PI_NODE:
2963
0
      return(XML_READER_TYPE_PROCESSING_INSTRUCTION);
2964
0
        case XML_COMMENT_NODE:
2965
0
      return(XML_READER_TYPE_COMMENT);
2966
0
        case XML_DOCUMENT_NODE:
2967
0
        case XML_HTML_DOCUMENT_NODE:
2968
0
      return(XML_READER_TYPE_DOCUMENT);
2969
0
        case XML_DOCUMENT_FRAG_NODE:
2970
0
      return(XML_READER_TYPE_DOCUMENT_FRAGMENT);
2971
0
        case XML_NOTATION_NODE:
2972
0
      return(XML_READER_TYPE_NOTATION);
2973
0
        case XML_DOCUMENT_TYPE_NODE:
2974
0
        case XML_DTD_NODE:
2975
0
      return(XML_READER_TYPE_DOCUMENT_TYPE);
2976
2977
0
        case XML_ELEMENT_DECL:
2978
0
        case XML_ATTRIBUTE_DECL:
2979
0
        case XML_ENTITY_DECL:
2980
0
        case XML_XINCLUDE_START:
2981
0
        case XML_XINCLUDE_END:
2982
0
      return(XML_READER_TYPE_NONE);
2983
0
    }
2984
0
    return(-1);
2985
0
}
2986
2987
/**
2988
 * Check if the current node is empty
2989
 *
2990
 * @param reader  the xmlTextReader used
2991
 * @returns 1 if empty, 0 if not and -1 in case of error
2992
 */
2993
int
2994
0
xmlTextReaderIsEmptyElement(xmlTextReader *reader) {
2995
0
    if ((reader == NULL) || (reader->node == NULL))
2996
0
  return(-1);
2997
0
    if (reader->node->type != XML_ELEMENT_NODE)
2998
0
  return(0);
2999
0
    if (reader->curnode != NULL)
3000
0
  return(0);
3001
0
    if (reader->node->children != NULL)
3002
0
  return(0);
3003
0
    if (reader->state == XML_TEXTREADER_END)
3004
0
  return(0);
3005
0
    if (reader->doc != NULL)
3006
0
        return(1);
3007
0
#ifdef LIBXML_XINCLUDE_ENABLED
3008
0
    if (reader->in_xinclude > 0)
3009
0
        return(1);
3010
0
#endif
3011
0
    return((reader->node->extra & NODE_IS_EMPTY) != 0);
3012
0
}
3013
3014
/**
3015
 * The local name of the node.
3016
 *
3017
 * @param reader  the xmlTextReader used
3018
 * @returns the local name or NULL if not available,
3019
 *   if non NULL it need to be freed by the caller.
3020
 */
3021
xmlChar *
3022
0
xmlTextReaderLocalName(xmlTextReader *reader) {
3023
0
    xmlNodePtr node;
3024
0
    if ((reader == NULL) || (reader->node == NULL))
3025
0
  return(NULL);
3026
0
    if (reader->curnode != NULL)
3027
0
  node = reader->curnode;
3028
0
    else
3029
0
  node = reader->node;
3030
0
    if (node->type == XML_NAMESPACE_DECL) {
3031
0
  xmlNsPtr ns = (xmlNsPtr) node;
3032
0
  if (ns->prefix == NULL)
3033
0
      return(readerStrdup(reader, BAD_CAST "xmlns"));
3034
0
  else
3035
0
      return(readerStrdup(reader, ns->prefix));
3036
0
    }
3037
0
    if ((node->type != XML_ELEMENT_NODE) &&
3038
0
  (node->type != XML_ATTRIBUTE_NODE))
3039
0
  return(xmlTextReaderName(reader));
3040
0
    return(readerStrdup(reader, node->name));
3041
0
}
3042
3043
/**
3044
 * The local name of the node.
3045
 *
3046
 * @param reader  the xmlTextReader used
3047
 * @returns the local name or NULL if not available, the
3048
 *         string will be deallocated with the reader.
3049
 */
3050
const xmlChar *
3051
0
xmlTextReaderConstLocalName(xmlTextReader *reader) {
3052
0
    xmlNodePtr node;
3053
0
    if ((reader == NULL) || (reader->node == NULL))
3054
0
  return(NULL);
3055
0
    if (reader->curnode != NULL)
3056
0
  node = reader->curnode;
3057
0
    else
3058
0
  node = reader->node;
3059
0
    if (node->type == XML_NAMESPACE_DECL) {
3060
0
  xmlNsPtr ns = (xmlNsPtr) node;
3061
0
  if (ns->prefix == NULL)
3062
0
      return(constString(reader, BAD_CAST "xmlns"));
3063
0
  else
3064
0
      return(ns->prefix);
3065
0
    }
3066
0
    if ((node->type != XML_ELEMENT_NODE) &&
3067
0
  (node->type != XML_ATTRIBUTE_NODE))
3068
0
  return(xmlTextReaderConstName(reader));
3069
0
    return(node->name);
3070
0
}
3071
3072
/**
3073
 * The qualified name of the node, equal to Prefix :LocalName.
3074
 *
3075
 * @param reader  the xmlTextReader used
3076
 * @returns the local name or NULL if not available,
3077
 *   if non NULL it need to be freed by the caller.
3078
 */
3079
xmlChar *
3080
0
xmlTextReaderName(xmlTextReader *reader) {
3081
0
    xmlNodePtr node;
3082
0
    xmlChar *ret;
3083
3084
0
    if ((reader == NULL) || (reader->node == NULL))
3085
0
  return(NULL);
3086
0
    if (reader->curnode != NULL)
3087
0
  node = reader->curnode;
3088
0
    else
3089
0
  node = reader->node;
3090
0
    switch (node->type) {
3091
0
        case XML_ELEMENT_NODE:
3092
0
        case XML_ATTRIBUTE_NODE:
3093
0
      if ((node->ns == NULL) ||
3094
0
    (node->ns->prefix == NULL))
3095
0
    return(readerStrdup(reader, node->name));
3096
3097
0
            ret = xmlBuildQName(node->name, node->ns->prefix, NULL, 0);
3098
0
            if (ret == NULL)
3099
0
                xmlTextReaderErrMemory(reader);
3100
0
      return(ret);
3101
0
        case XML_TEXT_NODE:
3102
0
      return(readerStrdup(reader, BAD_CAST "#text"));
3103
0
        case XML_CDATA_SECTION_NODE:
3104
0
      return(readerStrdup(reader, BAD_CAST "#cdata-section"));
3105
0
        case XML_ENTITY_NODE:
3106
0
        case XML_ENTITY_REF_NODE:
3107
0
      return(readerStrdup(reader, node->name));
3108
0
        case XML_PI_NODE:
3109
0
      return(readerStrdup(reader, node->name));
3110
0
        case XML_COMMENT_NODE:
3111
0
      return(readerStrdup(reader, BAD_CAST "#comment"));
3112
0
        case XML_DOCUMENT_NODE:
3113
0
        case XML_HTML_DOCUMENT_NODE:
3114
0
      return(readerStrdup(reader, BAD_CAST "#document"));
3115
0
        case XML_DOCUMENT_FRAG_NODE:
3116
0
      return(readerStrdup(reader, BAD_CAST "#document-fragment"));
3117
0
        case XML_NOTATION_NODE:
3118
0
      return(readerStrdup(reader, node->name));
3119
0
        case XML_DOCUMENT_TYPE_NODE:
3120
0
        case XML_DTD_NODE:
3121
0
      return(readerStrdup(reader, node->name));
3122
0
        case XML_NAMESPACE_DECL: {
3123
0
      xmlNsPtr ns = (xmlNsPtr) node;
3124
3125
0
      if (ns->prefix == NULL)
3126
0
    return(readerStrdup(reader, BAD_CAST "xmlns"));
3127
0
            ret = xmlBuildQName(ns->prefix, BAD_CAST "xmlns", NULL, 0);
3128
0
            if (ret == NULL)
3129
0
                xmlTextReaderErrMemory(reader);
3130
0
      return(ret);
3131
0
  }
3132
3133
0
        case XML_ELEMENT_DECL:
3134
0
        case XML_ATTRIBUTE_DECL:
3135
0
        case XML_ENTITY_DECL:
3136
0
        case XML_XINCLUDE_START:
3137
0
        case XML_XINCLUDE_END:
3138
0
      return(NULL);
3139
0
    }
3140
0
    return(NULL);
3141
0
}
3142
3143
/**
3144
 * The qualified name of the node, equal to Prefix :LocalName.
3145
 *
3146
 * @param reader  the xmlTextReader used
3147
 * @returns the local name or NULL if not available, the string is
3148
 *         deallocated with the reader.
3149
 */
3150
const xmlChar *
3151
0
xmlTextReaderConstName(xmlTextReader *reader) {
3152
0
    xmlNodePtr node;
3153
3154
0
    if ((reader == NULL) || (reader->node == NULL))
3155
0
  return(NULL);
3156
0
    if (reader->curnode != NULL)
3157
0
  node = reader->curnode;
3158
0
    else
3159
0
  node = reader->node;
3160
0
    switch (node->type) {
3161
0
        case XML_ELEMENT_NODE:
3162
0
        case XML_ATTRIBUTE_NODE:
3163
0
      if ((node->ns == NULL) ||
3164
0
    (node->ns->prefix == NULL))
3165
0
    return(node->name);
3166
0
      return(constQString(reader, node->ns->prefix, node->name));
3167
0
        case XML_TEXT_NODE:
3168
0
      return(constString(reader, BAD_CAST "#text"));
3169
0
        case XML_CDATA_SECTION_NODE:
3170
0
      return(constString(reader, BAD_CAST "#cdata-section"));
3171
0
        case XML_ENTITY_NODE:
3172
0
        case XML_ENTITY_REF_NODE:
3173
0
      return(constString(reader, node->name));
3174
0
        case XML_PI_NODE:
3175
0
      return(constString(reader, node->name));
3176
0
        case XML_COMMENT_NODE:
3177
0
      return(constString(reader, BAD_CAST "#comment"));
3178
0
        case XML_DOCUMENT_NODE:
3179
0
        case XML_HTML_DOCUMENT_NODE:
3180
0
      return(constString(reader, BAD_CAST "#document"));
3181
0
        case XML_DOCUMENT_FRAG_NODE:
3182
0
      return(constString(reader, BAD_CAST "#document-fragment"));
3183
0
        case XML_NOTATION_NODE:
3184
0
      return(constString(reader, node->name));
3185
0
        case XML_DOCUMENT_TYPE_NODE:
3186
0
        case XML_DTD_NODE:
3187
0
      return(constString(reader, node->name));
3188
0
        case XML_NAMESPACE_DECL: {
3189
0
      xmlNsPtr ns = (xmlNsPtr) node;
3190
3191
0
      if (ns->prefix == NULL)
3192
0
    return(constString(reader, BAD_CAST "xmlns"));
3193
0
      return(constQString(reader, BAD_CAST "xmlns", ns->prefix));
3194
0
  }
3195
3196
0
        case XML_ELEMENT_DECL:
3197
0
        case XML_ATTRIBUTE_DECL:
3198
0
        case XML_ENTITY_DECL:
3199
0
        case XML_XINCLUDE_START:
3200
0
        case XML_XINCLUDE_END:
3201
0
      return(NULL);
3202
0
    }
3203
0
    return(NULL);
3204
0
}
3205
3206
/**
3207
 * A shorthand reference to the namespace associated with the node.
3208
 *
3209
 * @param reader  the xmlTextReader used
3210
 * @returns the prefix or NULL if not available,
3211
 *    if non NULL it need to be freed by the caller.
3212
 */
3213
xmlChar *
3214
0
xmlTextReaderPrefix(xmlTextReader *reader) {
3215
0
    xmlNodePtr node;
3216
0
    if ((reader == NULL) || (reader->node == NULL))
3217
0
  return(NULL);
3218
0
    if (reader->curnode != NULL)
3219
0
  node = reader->curnode;
3220
0
    else
3221
0
  node = reader->node;
3222
0
    if (node->type == XML_NAMESPACE_DECL) {
3223
0
  xmlNsPtr ns = (xmlNsPtr) node;
3224
0
  if (ns->prefix == NULL)
3225
0
      return(NULL);
3226
0
  return(readerStrdup(reader, BAD_CAST "xmlns"));
3227
0
    }
3228
0
    if ((node->type != XML_ELEMENT_NODE) &&
3229
0
  (node->type != XML_ATTRIBUTE_NODE))
3230
0
  return(NULL);
3231
0
    if ((node->ns != NULL) && (node->ns->prefix != NULL))
3232
0
  return(readerStrdup(reader, node->ns->prefix));
3233
0
    return(NULL);
3234
0
}
3235
3236
/**
3237
 * A shorthand reference to the namespace associated with the node.
3238
 *
3239
 * @param reader  the xmlTextReader used
3240
 * @returns the prefix or NULL if not available, the string is deallocated
3241
 *         with the reader.
3242
 */
3243
const xmlChar *
3244
0
xmlTextReaderConstPrefix(xmlTextReader *reader) {
3245
0
    xmlNodePtr node;
3246
0
    if ((reader == NULL) || (reader->node == NULL))
3247
0
  return(NULL);
3248
0
    if (reader->curnode != NULL)
3249
0
  node = reader->curnode;
3250
0
    else
3251
0
  node = reader->node;
3252
0
    if (node->type == XML_NAMESPACE_DECL) {
3253
0
  xmlNsPtr ns = (xmlNsPtr) node;
3254
0
  if (ns->prefix == NULL)
3255
0
      return(NULL);
3256
0
  return(constString(reader, BAD_CAST "xmlns"));
3257
0
    }
3258
0
    if ((node->type != XML_ELEMENT_NODE) &&
3259
0
  (node->type != XML_ATTRIBUTE_NODE))
3260
0
  return(NULL);
3261
0
    if ((node->ns != NULL) && (node->ns->prefix != NULL))
3262
0
  return(constString(reader, node->ns->prefix));
3263
0
    return(NULL);
3264
0
}
3265
3266
/**
3267
 * The URI defining the namespace associated with the node.
3268
 *
3269
 * @param reader  the xmlTextReader used
3270
 * @returns the namespace URI or NULL if not available,
3271
 *    if non NULL it need to be freed by the caller.
3272
 */
3273
xmlChar *
3274
0
xmlTextReaderNamespaceUri(xmlTextReader *reader) {
3275
0
    xmlNodePtr node;
3276
0
    if ((reader == NULL) || (reader->node == NULL))
3277
0
  return(NULL);
3278
0
    if (reader->curnode != NULL)
3279
0
  node = reader->curnode;
3280
0
    else
3281
0
  node = reader->node;
3282
0
    if (node->type == XML_NAMESPACE_DECL)
3283
0
  return(readerStrdup(reader, BAD_CAST "http://www.w3.org/2000/xmlns/"));
3284
0
    if ((node->type != XML_ELEMENT_NODE) &&
3285
0
  (node->type != XML_ATTRIBUTE_NODE))
3286
0
  return(NULL);
3287
0
    if (node->ns != NULL)
3288
0
  return(readerStrdup(reader, node->ns->href));
3289
0
    return(NULL);
3290
0
}
3291
3292
/**
3293
 * The URI defining the namespace associated with the node.
3294
 *
3295
 * @param reader  the xmlTextReader used
3296
 * @returns the namespace URI or NULL if not available, the string
3297
 *         will be deallocated with the reader
3298
 */
3299
const xmlChar *
3300
0
xmlTextReaderConstNamespaceUri(xmlTextReader *reader) {
3301
0
    xmlNodePtr node;
3302
0
    if ((reader == NULL) || (reader->node == NULL))
3303
0
  return(NULL);
3304
0
    if (reader->curnode != NULL)
3305
0
  node = reader->curnode;
3306
0
    else
3307
0
  node = reader->node;
3308
0
    if (node->type == XML_NAMESPACE_DECL)
3309
0
  return(constString(reader, BAD_CAST "http://www.w3.org/2000/xmlns/"));
3310
0
    if ((node->type != XML_ELEMENT_NODE) &&
3311
0
  (node->type != XML_ATTRIBUTE_NODE))
3312
0
  return(NULL);
3313
0
    if (node->ns != NULL)
3314
0
  return(constString(reader, node->ns->href));
3315
0
    return(NULL);
3316
0
}
3317
3318
/**
3319
 * The base URI of the node.
3320
 *
3321
 * @param reader  the xmlTextReader used
3322
 * @returns the base URI or NULL if not available,
3323
 *    if non NULL it need to be freed by the caller.
3324
 */
3325
xmlChar *
3326
0
xmlTextReaderBaseUri(xmlTextReader *reader) {
3327
0
    xmlChar *ret = NULL;
3328
0
    int result;
3329
3330
0
    if ((reader == NULL) || (reader->node == NULL))
3331
0
  return(NULL);
3332
0
    result = xmlNodeGetBaseSafe(NULL, reader->node, &ret);
3333
0
    if (result < 0)
3334
0
        xmlTextReaderErrMemory(reader);
3335
3336
0
    return(ret);
3337
0
}
3338
3339
/**
3340
 * The base URI of the node.
3341
 *
3342
 * @param reader  the xmlTextReader used
3343
 * @returns the base URI or NULL if not available, the string
3344
 *         will be deallocated with the reader
3345
 */
3346
const xmlChar *
3347
0
xmlTextReaderConstBaseUri(xmlTextReader *reader) {
3348
0
    xmlChar *tmp;
3349
0
    const xmlChar *ret;
3350
0
    int result;
3351
3352
0
    if ((reader == NULL) || (reader->node == NULL))
3353
0
  return(NULL);
3354
0
    result = xmlNodeGetBaseSafe(NULL, reader->node, &tmp);
3355
0
    if (result < 0)
3356
0
        xmlTextReaderErrMemory(reader);
3357
0
    if (tmp == NULL)
3358
0
        return(NULL);
3359
0
    ret = constString(reader, tmp);
3360
0
    xmlFree(tmp);
3361
0
    return(ret);
3362
0
}
3363
3364
/**
3365
 * The depth of the node in the tree.
3366
 *
3367
 * @param reader  the xmlTextReader used
3368
 * @returns the depth or -1 in case of error
3369
 */
3370
int
3371
0
xmlTextReaderDepth(xmlTextReader *reader) {
3372
0
    if (reader == NULL)
3373
0
  return(-1);
3374
0
    if (reader->node == NULL)
3375
0
  return(0);
3376
3377
0
    if (reader->curnode != NULL) {
3378
0
  if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
3379
0
      (reader->curnode->type == XML_NAMESPACE_DECL))
3380
0
      return(reader->depth + 1);
3381
0
  return(reader->depth + 2);
3382
0
    }
3383
0
    return(reader->depth);
3384
0
}
3385
3386
/**
3387
 * Whether the node has attributes.
3388
 *
3389
 * @param reader  the xmlTextReader used
3390
 * @returns 1 if true, 0 if false, and -1 in case or error
3391
 */
3392
int
3393
0
xmlTextReaderHasAttributes(xmlTextReader *reader) {
3394
0
    xmlNodePtr node;
3395
0
    if (reader == NULL)
3396
0
  return(-1);
3397
0
    if (reader->node == NULL)
3398
0
  return(0);
3399
0
    if (reader->curnode != NULL)
3400
0
  node = reader->curnode;
3401
0
    else
3402
0
  node = reader->node;
3403
3404
0
    if ((node->type == XML_ELEMENT_NODE) &&
3405
0
  ((node->properties != NULL) || (node->nsDef != NULL)))
3406
0
  return(1);
3407
    /* TODO: handle the xmlDecl */
3408
0
    return(0);
3409
0
}
3410
3411
/**
3412
 * Whether the node can have a text value.
3413
 *
3414
 * @param reader  the xmlTextReader used
3415
 * @returns 1 if true, 0 if false, and -1 in case or error
3416
 */
3417
int
3418
0
xmlTextReaderHasValue(xmlTextReader *reader) {
3419
0
    xmlNodePtr node;
3420
0
    if (reader == NULL)
3421
0
  return(-1);
3422
0
    if (reader->node == NULL)
3423
0
  return(0);
3424
0
    if (reader->curnode != NULL)
3425
0
  node = reader->curnode;
3426
0
    else
3427
0
  node = reader->node;
3428
3429
0
    switch (node->type) {
3430
0
        case XML_ATTRIBUTE_NODE:
3431
0
        case XML_TEXT_NODE:
3432
0
        case XML_CDATA_SECTION_NODE:
3433
0
        case XML_PI_NODE:
3434
0
        case XML_COMMENT_NODE:
3435
0
        case XML_NAMESPACE_DECL:
3436
0
      return(1);
3437
0
  default:
3438
0
      break;
3439
0
    }
3440
0
    return(0);
3441
0
}
3442
3443
/**
3444
 * Provides the text value of the node if present
3445
 *
3446
 * @param reader  the xmlTextReader used
3447
 * @returns the string or NULL if not available. The result must be deallocated
3448
 *     with #xmlFree
3449
 */
3450
xmlChar *
3451
0
xmlTextReaderValue(xmlTextReader *reader) {
3452
0
    xmlNodePtr node;
3453
0
    if (reader == NULL)
3454
0
  return(NULL);
3455
0
    if (reader->node == NULL)
3456
0
  return(NULL);
3457
0
    if (reader->curnode != NULL)
3458
0
  node = reader->curnode;
3459
0
    else
3460
0
  node = reader->node;
3461
3462
0
    switch (node->type) {
3463
0
        case XML_NAMESPACE_DECL:
3464
0
      return(readerStrdup(reader, ((xmlNsPtr) node)->href));
3465
0
        case XML_ATTRIBUTE_NODE:{
3466
0
      xmlAttrPtr attr = (xmlAttrPtr) node;
3467
0
            xmlDocPtr doc = NULL;
3468
0
            xmlChar *ret;
3469
3470
0
            if (attr->children == NULL)
3471
0
                return(NULL);
3472
0
      if (attr->parent != NULL)
3473
0
                doc = attr->parent->doc;
3474
0
      ret = xmlNodeListGetString(doc, attr->children, 1);
3475
0
            if (ret == NULL)
3476
0
                xmlTextReaderErrMemory(reader);
3477
0
      return(ret);
3478
0
  }
3479
0
        case XML_TEXT_NODE:
3480
0
        case XML_CDATA_SECTION_NODE:
3481
0
        case XML_PI_NODE:
3482
0
        case XML_COMMENT_NODE:
3483
0
            return(readerStrdup(reader, node->content));
3484
0
  default:
3485
0
      break;
3486
0
    }
3487
0
    return(NULL);
3488
0
}
3489
3490
/**
3491
 * Provides the text value of the node if present
3492
 *
3493
 * @param reader  the xmlTextReader used
3494
 * @returns the string or NULL if not available. The result will be
3495
 *     deallocated on the next Read() operation.
3496
 */
3497
const xmlChar *
3498
0
xmlTextReaderConstValue(xmlTextReader *reader) {
3499
0
    xmlNodePtr node;
3500
0
    if (reader == NULL)
3501
0
  return(NULL);
3502
0
    if (reader->node == NULL)
3503
0
  return(NULL);
3504
0
    if (reader->curnode != NULL)
3505
0
  node = reader->curnode;
3506
0
    else
3507
0
  node = reader->node;
3508
3509
0
    switch (node->type) {
3510
0
        case XML_NAMESPACE_DECL:
3511
0
      return(((xmlNsPtr) node)->href);
3512
0
        case XML_ATTRIBUTE_NODE:{
3513
0
      xmlAttrPtr attr = (xmlAttrPtr) node;
3514
0
      const xmlChar *ret;
3515
3516
0
      if ((attr->children != NULL) &&
3517
0
          (attr->children->type == XML_TEXT_NODE) &&
3518
0
    (attr->children->next == NULL))
3519
0
    return(attr->children->content);
3520
0
      else {
3521
0
    if (reader->buffer == NULL) {
3522
0
        reader->buffer = xmlBufCreate(50);
3523
0
                    if (reader->buffer == NULL)
3524
0
                        return (NULL);
3525
0
                } else
3526
0
                    xmlBufEmpty(reader->buffer);
3527
0
          xmlBufGetNodeContent(reader->buffer, node);
3528
0
    ret = xmlBufContent(reader->buffer);
3529
0
    if (ret == NULL) {
3530
0
                    xmlTextReaderErrMemory(reader);
3531
        /* error on the buffer best to reallocate */
3532
0
        xmlBufFree(reader->buffer);
3533
0
        reader->buffer = xmlBufCreate(50);
3534
0
    }
3535
0
    return(ret);
3536
0
      }
3537
0
      break;
3538
0
  }
3539
0
        case XML_TEXT_NODE:
3540
0
        case XML_CDATA_SECTION_NODE:
3541
0
        case XML_PI_NODE:
3542
0
        case XML_COMMENT_NODE:
3543
0
      return(node->content);
3544
0
  default:
3545
0
      break;
3546
0
    }
3547
0
    return(NULL);
3548
0
}
3549
3550
/**
3551
 * Whether an Attribute  node was generated from the default value
3552
 * defined in the DTD or schema.
3553
 *
3554
 * @param reader  the xmlTextReader used
3555
 * @returns 0 if not defaulted, 1 if defaulted, and -1 in case of error
3556
 */
3557
int
3558
0
xmlTextReaderIsDefault(xmlTextReader *reader) {
3559
0
    if (reader == NULL)
3560
0
  return(-1);
3561
0
    return(0);
3562
0
}
3563
3564
/**
3565
 * The quotation mark character used to enclose the value of an attribute.
3566
 *
3567
 * @param reader  the xmlTextReader used
3568
 * @returns " or ' and -1 in case of error
3569
 */
3570
int
3571
0
xmlTextReaderQuoteChar(xmlTextReader *reader) {
3572
0
    if (reader == NULL)
3573
0
  return(-1);
3574
    /* TODO maybe lookup the attribute value for " first */
3575
0
    return('"');
3576
0
}
3577
3578
/**
3579
 * The xml:lang scope within which the node resides.
3580
 *
3581
 * @param reader  the xmlTextReader used
3582
 * @returns the xml:lang value or NULL if none exists.,
3583
 *    if non NULL it need to be freed by the caller.
3584
 */
3585
xmlChar *
3586
0
xmlTextReaderXmlLang(xmlTextReader *reader) {
3587
0
    if (reader == NULL)
3588
0
  return(NULL);
3589
0
    if (reader->node == NULL)
3590
0
  return(NULL);
3591
0
    return(xmlNodeGetLang(reader->node));
3592
0
}
3593
3594
/**
3595
 * The xml:lang scope within which the node resides.
3596
 *
3597
 * @param reader  the xmlTextReader used
3598
 * @returns the xml:lang value or NULL if none exists.
3599
 */
3600
const xmlChar *
3601
0
xmlTextReaderConstXmlLang(xmlTextReader *reader) {
3602
0
    xmlChar *tmp;
3603
0
    const xmlChar *ret;
3604
3605
0
    if (reader == NULL)
3606
0
  return(NULL);
3607
0
    if (reader->node == NULL)
3608
0
  return(NULL);
3609
0
    tmp = xmlNodeGetLang(reader->node);
3610
0
    if (tmp == NULL)
3611
0
        return(NULL);
3612
0
    ret = constString(reader, tmp);
3613
0
    xmlFree(tmp);
3614
0
    return(ret);
3615
0
}
3616
3617
/**
3618
 * Get an interned string from the reader, allows for example to
3619
 * speedup string name comparisons
3620
 *
3621
 * @param reader  the xmlTextReader used
3622
 * @param str  the string to intern.
3623
 * @returns an interned copy of the string or NULL in case of error. The
3624
 *         string will be deallocated with the reader.
3625
 */
3626
const xmlChar *
3627
0
xmlTextReaderConstString(xmlTextReader *reader, const xmlChar *str) {
3628
0
    if (reader == NULL)
3629
0
  return(NULL);
3630
0
    return(constString(reader, str));
3631
0
}
3632
3633
/**
3634
 * The value indicating whether to normalize white space and attribute values.
3635
 * Since attribute value and end of line normalizations are a MUST in the XML
3636
 * specification only the value true is accepted. The broken behaviour of
3637
 * accepting out of range character entities like &\#0; is of course not
3638
 * supported either.
3639
 *
3640
 * @param reader  the xmlTextReader used
3641
 * @returns 1 or -1 in case of error.
3642
 */
3643
int
3644
0
xmlTextReaderNormalization(xmlTextReader *reader) {
3645
0
    if (reader == NULL)
3646
0
  return(-1);
3647
0
    return(1);
3648
0
}
3649
3650
/************************************************************************
3651
 *                  *
3652
 *      Extensions to the base APIs     *
3653
 *                  *
3654
 ************************************************************************/
3655
3656
/**
3657
 * Change the parser processing behaviour by changing some of its internal
3658
 * properties. Note that some properties can only be changed before any
3659
 * read has been done.
3660
 *
3661
 * @param reader  the xmlTextReader used
3662
 * @param prop  the xmlParserProperties to set
3663
 * @param value  usually 0 or 1 to (de)activate it
3664
 * @returns 0 if the call was successful, or -1 in case of error
3665
 */
3666
int
3667
0
xmlTextReaderSetParserProp(xmlTextReader *reader, int prop, int value) {
3668
0
    xmlParserProperties p = (xmlParserProperties) prop;
3669
0
    xmlParserCtxtPtr ctxt;
3670
3671
0
    if ((reader == NULL) || (reader->ctxt == NULL))
3672
0
  return(-1);
3673
0
    ctxt = reader->ctxt;
3674
3675
0
    switch (p) {
3676
0
        case XML_PARSER_LOADDTD:
3677
0
      if (value != 0) {
3678
0
    if (ctxt->loadsubset == 0) {
3679
0
        if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3680
0
      return(-1);
3681
0
                    ctxt->options |= XML_PARSE_DTDLOAD;
3682
0
        ctxt->loadsubset |= XML_DETECT_IDS;
3683
0
    }
3684
0
      } else {
3685
0
                ctxt->options &= ~XML_PARSE_DTDLOAD;
3686
0
    ctxt->loadsubset &= ~XML_DETECT_IDS;
3687
0
      }
3688
0
      return(0);
3689
0
        case XML_PARSER_DEFAULTATTRS:
3690
0
      if (value != 0) {
3691
0
                ctxt->options |= XML_PARSE_DTDATTR;
3692
0
    ctxt->loadsubset |= XML_COMPLETE_ATTRS;
3693
0
      } else {
3694
0
                ctxt->options &= ~XML_PARSE_DTDATTR;
3695
0
    ctxt->loadsubset &= ~XML_COMPLETE_ATTRS;
3696
0
      }
3697
0
      return(0);
3698
0
        case XML_PARSER_VALIDATE:
3699
0
      if (value != 0) {
3700
0
                ctxt->options |= XML_PARSE_DTDVALID;
3701
0
    ctxt->validate = 1;
3702
0
    reader->validate = XML_TEXTREADER_VALIDATE_DTD;
3703
0
      } else {
3704
0
                ctxt->options &= ~XML_PARSE_DTDVALID;
3705
0
    ctxt->validate = 0;
3706
0
      }
3707
0
      return(0);
3708
0
        case XML_PARSER_SUBST_ENTITIES:
3709
0
      if (value != 0) {
3710
0
                ctxt->options |= XML_PARSE_NOENT;
3711
0
    ctxt->replaceEntities = 1;
3712
0
      } else {
3713
0
                ctxt->options &= ~XML_PARSE_NOENT;
3714
0
    ctxt->replaceEntities = 0;
3715
0
      }
3716
0
      return(0);
3717
0
    }
3718
0
    return(-1);
3719
0
}
3720
3721
/**
3722
 * Read the parser internal property.
3723
 *
3724
 * @param reader  the xmlTextReader used
3725
 * @param prop  the xmlParserProperties to get
3726
 * @returns the value, usually 0 or 1, or -1 in case of error.
3727
 */
3728
int
3729
0
xmlTextReaderGetParserProp(xmlTextReader *reader, int prop) {
3730
0
    xmlParserProperties p = (xmlParserProperties) prop;
3731
0
    xmlParserCtxtPtr ctxt;
3732
3733
0
    if ((reader == NULL) || (reader->ctxt == NULL))
3734
0
  return(-1);
3735
0
    ctxt = reader->ctxt;
3736
3737
0
    switch (p) {
3738
0
        case XML_PARSER_LOADDTD:
3739
0
      if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
3740
0
    return(1);
3741
0
      return(0);
3742
0
        case XML_PARSER_DEFAULTATTRS:
3743
0
      if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
3744
0
    return(1);
3745
0
      return(0);
3746
0
        case XML_PARSER_VALIDATE:
3747
0
      return(reader->validate);
3748
0
  case XML_PARSER_SUBST_ENTITIES:
3749
0
      return(ctxt->replaceEntities);
3750
0
    }
3751
0
    return(-1);
3752
0
}
3753
3754
3755
/**
3756
 * Provide the line number of the current parsing point.
3757
 *
3758
 * @param reader  the user data (XML reader context)
3759
 * @returns an int or 0 if not available
3760
 */
3761
int
3762
xmlTextReaderGetParserLineNumber(xmlTextReader *reader)
3763
0
{
3764
0
    if ((reader == NULL) || (reader->ctxt == NULL) ||
3765
0
        (reader->ctxt->input == NULL)) {
3766
0
        return (0);
3767
0
    }
3768
0
    return (reader->ctxt->input->line);
3769
0
}
3770
3771
/**
3772
 * Provide the column number of the current parsing point.
3773
 *
3774
 * @param reader  the user data (XML reader context)
3775
 * @returns an int or 0 if not available
3776
 */
3777
int
3778
xmlTextReaderGetParserColumnNumber(xmlTextReader *reader)
3779
0
{
3780
0
    if ((reader == NULL) || (reader->ctxt == NULL) ||
3781
0
        (reader->ctxt->input == NULL)) {
3782
0
        return (0);
3783
0
    }
3784
0
    return (reader->ctxt->input->col);
3785
0
}
3786
3787
/**
3788
 * Hacking interface allowing to get the xmlNode corresponding to the
3789
 * current node being accessed by the xmlTextReader. This is dangerous
3790
 * because the underlying node may be destroyed on the next Reads.
3791
 *
3792
 * @param reader  the xmlTextReader used
3793
 * @returns the xmlNode or NULL in case of error.
3794
 */
3795
xmlNode *
3796
0
xmlTextReaderCurrentNode(xmlTextReader *reader) {
3797
0
    if (reader == NULL)
3798
0
  return(NULL);
3799
3800
0
    if (reader->curnode != NULL)
3801
0
  return(reader->curnode);
3802
0
    return(reader->node);
3803
0
}
3804
3805
/**
3806
 * This tells the XML Reader to preserve the current node.
3807
 * The caller must also use #xmlTextReaderCurrentDoc to
3808
 * keep an handle on the resulting document once parsing has finished
3809
 *
3810
 * @param reader  the xmlTextReader used
3811
 * @returns the xmlNode or NULL in case of error.
3812
 */
3813
xmlNode *
3814
0
xmlTextReaderPreserve(xmlTextReader *reader) {
3815
0
    xmlNodePtr cur, parent;
3816
3817
0
    if (reader == NULL)
3818
0
  return(NULL);
3819
3820
0
    cur = reader->node;
3821
0
    if (cur == NULL)
3822
0
        return(NULL);
3823
3824
0
    if ((cur->type != XML_DOCUMENT_NODE) && (cur->type != XML_DTD_NODE)) {
3825
0
  cur->extra |= NODE_IS_PRESERVED;
3826
0
  cur->extra |= NODE_IS_SPRESERVED;
3827
0
    }
3828
0
    reader->preserves++;
3829
3830
0
    parent = cur->parent;;
3831
0
    while (parent != NULL) {
3832
0
        if (parent->type == XML_ELEMENT_NODE)
3833
0
      parent->extra |= NODE_IS_PRESERVED;
3834
0
  parent = parent->parent;
3835
0
    }
3836
0
    return(cur);
3837
0
}
3838
3839
#ifdef LIBXML_PATTERN_ENABLED
3840
/**
3841
 * This tells the XML Reader to preserve all nodes matched by the
3842
 * pattern. The caller must also use #xmlTextReaderCurrentDoc to
3843
 * keep an handle on the resulting document once parsing has finished
3844
 *
3845
 * @param reader  the xmlTextReader used
3846
 * @param pattern  an XPath subset pattern
3847
 * @param namespaces  the prefix definitions, array of [URI, prefix] or NULL
3848
 * @returns a non-negative number in case of success and -1 in case of error
3849
 */
3850
int
3851
xmlTextReaderPreservePattern(xmlTextReader *reader, const xmlChar *pattern,
3852
                             const xmlChar **namespaces)
3853
0
{
3854
0
    xmlPatternPtr comp;
3855
3856
0
    if ((reader == NULL) || (pattern == NULL))
3857
0
  return(-1);
3858
3859
0
    comp = xmlPatterncompile(pattern, reader->dict, 0, namespaces);
3860
0
    if (comp == NULL)
3861
0
        return(-1);
3862
3863
0
    if (reader->patternNr >= reader->patternMax) {
3864
0
        xmlPatternPtr *tmp;
3865
0
        int newSize;
3866
3867
0
        newSize = xmlGrowCapacity(reader->patternMax, sizeof(tmp[0]),
3868
0
                                  4, XML_MAX_ITEMS);
3869
0
        if (newSize < 0) {
3870
0
            xmlTextReaderErrMemory(reader);
3871
0
            return(-1);
3872
0
        }
3873
0
  tmp = xmlRealloc(reader->patternTab, newSize * sizeof(tmp[0]));
3874
0
        if (tmp == NULL) {
3875
0
            xmlTextReaderErrMemory(reader);
3876
0
            return(-1);
3877
0
        }
3878
0
  reader->patternTab = tmp;
3879
0
        reader->patternMax = newSize;
3880
0
    }
3881
0
    reader->patternTab[reader->patternNr] = comp;
3882
0
    return(reader->patternNr++);
3883
0
}
3884
#endif
3885
3886
/**
3887
 * Hacking interface allowing to get the xmlDoc corresponding to the
3888
 * current document being accessed by the xmlTextReader.
3889
 * NOTE: as a result of this call, the reader will not destroy the
3890
 *       associated XML document and calling #xmlFreeDoc on the result
3891
 *       is needed once the reader parsing has finished.
3892
 *
3893
 * @param reader  the xmlTextReader used
3894
 * @returns the xmlDoc or NULL in case of error.
3895
 */
3896
xmlDoc *
3897
0
xmlTextReaderCurrentDoc(xmlTextReader *reader) {
3898
0
    if (reader == NULL)
3899
0
  return(NULL);
3900
0
    if (reader->doc != NULL)
3901
0
        return(reader->doc);
3902
0
    if ((reader->ctxt == NULL) || (reader->ctxt->myDoc == NULL))
3903
0
  return(NULL);
3904
3905
0
    reader->preserve = 1;
3906
0
    return(reader->ctxt->myDoc);
3907
0
}
3908
3909
#ifdef LIBXML_RELAXNG_ENABLED
3910
/**
3911
 * Use RelaxNG to validate the document as it is processed.
3912
 * Activation is only possible before the first Read().
3913
 * if `schema` is NULL, then RelaxNG validation is deactivated.
3914
 * The `schema` should not be freed until the reader is deallocated
3915
 * or its use has been deactivated.
3916
 *
3917
 * @param reader  the xmlTextReader used
3918
 * @param schema  a precompiled RelaxNG schema
3919
 * @returns 0 in case the RelaxNG validation could be (de)activated and
3920
 *         -1 in case of error.
3921
 */
3922
int
3923
0
xmlTextReaderRelaxNGSetSchema(xmlTextReader *reader, xmlRelaxNG *schema) {
3924
0
    if (reader == NULL)
3925
0
        return(-1);
3926
0
    if (schema == NULL) {
3927
0
        if (reader->rngSchemas != NULL) {
3928
0
      xmlRelaxNGFree(reader->rngSchemas);
3929
0
      reader->rngSchemas = NULL;
3930
0
  }
3931
0
        if (reader->rngValidCtxt != NULL) {
3932
0
      if (! reader->rngPreserveCtxt)
3933
0
    xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3934
0
      reader->rngValidCtxt = NULL;
3935
0
        }
3936
0
  reader->rngPreserveCtxt = 0;
3937
0
  return(0);
3938
0
    }
3939
0
    if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3940
0
  return(-1);
3941
0
    if (reader->rngSchemas != NULL) {
3942
0
  xmlRelaxNGFree(reader->rngSchemas);
3943
0
  reader->rngSchemas = NULL;
3944
0
    }
3945
0
    if (reader->rngValidCtxt != NULL) {
3946
0
  if (! reader->rngPreserveCtxt)
3947
0
      xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
3948
0
  reader->rngValidCtxt = NULL;
3949
0
    }
3950
0
    reader->rngPreserveCtxt = 0;
3951
0
    reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema);
3952
0
    if (reader->rngValidCtxt == NULL)
3953
0
        return(-1);
3954
0
    if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
3955
0
  xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
3956
0
      xmlTextReaderStructuredRelay, reader);
3957
0
    reader->rngValidErrors = 0;
3958
0
    reader->rngFullNode = NULL;
3959
0
    reader->validate = XML_TEXTREADER_VALIDATE_RNG;
3960
0
    return(0);
3961
0
}
3962
#endif /* LIBXML_RELAXNG_ENABLED */
3963
3964
#ifdef LIBXML_SCHEMAS_ENABLED
3965
/**
3966
 * Internal locator function for the readers
3967
 *
3968
 * @param ctx  the xmlTextReader used
3969
 * @param file  returned file information
3970
 * @param line  returned line information
3971
 * @returns 0 in case the Schema validation could be (de)activated and
3972
 *         -1 in case of error.
3973
 */
3974
static int
3975
0
xmlTextReaderLocator(void *ctx, const char **file, unsigned long *line) {
3976
0
    xmlTextReaderPtr reader;
3977
3978
0
    if ((ctx == NULL) || ((file == NULL) && (line == NULL)))
3979
0
        return(-1);
3980
3981
0
    if (file != NULL)
3982
0
        *file = NULL;
3983
0
    if (line != NULL)
3984
0
        *line = 0;
3985
3986
0
    reader = (xmlTextReaderPtr) ctx;
3987
0
    if ((reader->ctxt != NULL) && (reader->ctxt->input != NULL)) {
3988
0
  if (file != NULL)
3989
0
      *file = reader->ctxt->input->filename;
3990
0
  if (line != NULL)
3991
0
      *line = reader->ctxt->input->line;
3992
0
  return(0);
3993
0
    }
3994
0
    if (reader->node != NULL) {
3995
0
        long res;
3996
0
  int ret = 0;
3997
3998
0
  if (line != NULL) {
3999
0
      res = xmlGetLineNo(reader->node);
4000
0
      if (res > 0)
4001
0
          *line = (unsigned long) res;
4002
0
      else
4003
0
                ret = -1;
4004
0
  }
4005
0
        if (file != NULL) {
4006
0
      xmlDocPtr doc = reader->node->doc;
4007
0
      if ((doc != NULL) && (doc->URL != NULL))
4008
0
          *file = (const char *) doc->URL;
4009
0
      else
4010
0
                ret = -1;
4011
0
  }
4012
0
  return(ret);
4013
0
    }
4014
0
    return(-1);
4015
0
}
4016
4017
/**
4018
 * Use XSD Schema to validate the document as it is processed.
4019
 * Activation is only possible before the first Read().
4020
 * if `schema` is NULL, then Schema validation is deactivated.
4021
 * The `schema` should not be freed until the reader is deallocated
4022
 * or its use has been deactivated.
4023
 *
4024
 * @param reader  the xmlTextReader used
4025
 * @param schema  a precompiled Schema schema
4026
 * @returns 0 in case the Schema validation could be (de)activated and
4027
 *         -1 in case of error.
4028
 */
4029
int
4030
0
xmlTextReaderSetSchema(xmlTextReader *reader, xmlSchema *schema) {
4031
0
    if (reader == NULL)
4032
0
        return(-1);
4033
0
    if (schema == NULL) {
4034
0
  if (reader->xsdPlug != NULL) {
4035
0
      xmlSchemaSAXUnplug(reader->xsdPlug);
4036
0
      reader->xsdPlug = NULL;
4037
0
  }
4038
0
        if (reader->xsdValidCtxt != NULL) {
4039
0
      if (! reader->xsdPreserveCtxt)
4040
0
    xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
4041
0
      reader->xsdValidCtxt = NULL;
4042
0
        }
4043
0
  reader->xsdPreserveCtxt = 0;
4044
0
        if (reader->xsdSchemas != NULL) {
4045
0
      xmlSchemaFree(reader->xsdSchemas);
4046
0
      reader->xsdSchemas = NULL;
4047
0
  }
4048
0
  return(0);
4049
0
    }
4050
0
    if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
4051
0
  return(-1);
4052
0
    if (reader->xsdPlug != NULL) {
4053
0
  xmlSchemaSAXUnplug(reader->xsdPlug);
4054
0
  reader->xsdPlug = NULL;
4055
0
    }
4056
0
    if (reader->xsdValidCtxt != NULL) {
4057
0
  if (! reader->xsdPreserveCtxt)
4058
0
      xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
4059
0
  reader->xsdValidCtxt = NULL;
4060
0
    }
4061
0
    reader->xsdPreserveCtxt = 0;
4062
0
    if (reader->xsdSchemas != NULL) {
4063
0
  xmlSchemaFree(reader->xsdSchemas);
4064
0
  reader->xsdSchemas = NULL;
4065
0
    }
4066
0
    reader->xsdValidCtxt = xmlSchemaNewValidCtxt(schema);
4067
0
    if (reader->xsdValidCtxt == NULL) {
4068
0
  xmlSchemaFree(reader->xsdSchemas);
4069
0
  reader->xsdSchemas = NULL;
4070
0
        return(-1);
4071
0
    }
4072
0
    reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
4073
0
                                       &(reader->ctxt->sax),
4074
0
               &(reader->ctxt->userData));
4075
0
    if (reader->xsdPlug == NULL) {
4076
0
  xmlSchemaFree(reader->xsdSchemas);
4077
0
  reader->xsdSchemas = NULL;
4078
0
  xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
4079
0
  reader->xsdValidCtxt = NULL;
4080
0
  return(-1);
4081
0
    }
4082
0
    xmlSchemaValidateSetLocator(reader->xsdValidCtxt,
4083
0
                                xmlTextReaderLocator,
4084
0
        (void *) reader);
4085
4086
0
    if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
4087
0
  xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
4088
0
      xmlTextReaderStructuredRelay, reader);
4089
0
    reader->xsdValidErrors = 0;
4090
0
    reader->validate = XML_TEXTREADER_VALIDATE_XSD;
4091
0
    return(0);
4092
0
}
4093
#endif /* LIBXML_SCHEMAS_ENABLED */
4094
4095
#ifdef LIBXML_RELAXNG_ENABLED
4096
/**
4097
 * Use RelaxNG to validate the document as it is processed.
4098
 * Activation is only possible before the first Read().
4099
 * If both `rng` and `ctxt` are NULL, then RelaxNG validation is deactivated.
4100
 *
4101
 * @param reader  the xmlTextReader used
4102
 * @param rng  the path to a RelaxNG schema or NULL
4103
 * @param ctxt  the RelaxNG schema validation context or NULL
4104
 * @param options  options (not yet used)
4105
 * @returns 0 in case the RelaxNG validation could be (de)activated and
4106
 *     -1 in case of error.
4107
 */
4108
static int
4109
xmlTextReaderRelaxNGValidateInternal(xmlTextReaderPtr reader,
4110
             const char *rng,
4111
             xmlRelaxNGValidCtxtPtr ctxt,
4112
             int options ATTRIBUTE_UNUSED)
4113
0
{
4114
0
    if (reader == NULL)
4115
0
  return(-1);
4116
4117
0
    if ((rng != NULL) && (ctxt != NULL))
4118
0
  return (-1);
4119
4120
0
    if (((rng != NULL) || (ctxt != NULL)) &&
4121
0
  ((reader->mode != XML_TEXTREADER_MODE_INITIAL) ||
4122
0
   (reader->ctxt == NULL)))
4123
0
  return(-1);
4124
4125
    /* Cleanup previous validation stuff. */
4126
0
    if (reader->rngValidCtxt != NULL) {
4127
0
  if ( !reader->rngPreserveCtxt)
4128
0
      xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
4129
0
  reader->rngValidCtxt = NULL;
4130
0
    }
4131
0
    reader->rngPreserveCtxt = 0;
4132
0
    if (reader->rngSchemas != NULL) {
4133
0
  xmlRelaxNGFree(reader->rngSchemas);
4134
0
  reader->rngSchemas = NULL;
4135
0
    }
4136
4137
0
    if ((rng == NULL) && (ctxt == NULL)) {
4138
  /* We just want to deactivate the validation, so get out. */
4139
0
  return(0);
4140
0
    }
4141
4142
4143
0
    if (rng != NULL) {
4144
0
  xmlRelaxNGParserCtxtPtr pctxt;
4145
  /* Parse the schema and create validation environment. */
4146
4147
0
  pctxt = xmlRelaxNGNewParserCtxt(rng);
4148
0
  if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
4149
0
      xmlRelaxNGSetParserStructuredErrors(pctxt,
4150
0
                    xmlTextReaderStructuredRelay, reader);
4151
0
        if (reader->resourceLoader != NULL)
4152
0
            xmlRelaxNGSetResourceLoader(pctxt, reader->resourceLoader,
4153
0
                                        reader->resourceCtxt);
4154
0
  reader->rngSchemas = xmlRelaxNGParse(pctxt);
4155
0
  xmlRelaxNGFreeParserCtxt(pctxt);
4156
0
  if (reader->rngSchemas == NULL)
4157
0
      return(-1);
4158
4159
0
  reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas);
4160
0
  if (reader->rngValidCtxt == NULL) {
4161
0
      xmlRelaxNGFree(reader->rngSchemas);
4162
0
      reader->rngSchemas = NULL;
4163
0
      return(-1);
4164
0
  }
4165
0
    } else {
4166
  /* Use the given validation context. */
4167
0
  reader->rngValidCtxt = ctxt;
4168
0
  reader->rngPreserveCtxt = 1;
4169
0
    }
4170
    /*
4171
    * Redirect the validation context's error channels to use
4172
    * the reader channels.
4173
    * TODO: In case the user provides the validation context we
4174
    * could make this redirection optional.
4175
    */
4176
0
    if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
4177
0
        xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
4178
0
                xmlTextReaderStructuredRelay, reader);
4179
0
    reader->rngValidErrors = 0;
4180
0
    reader->rngFullNode = NULL;
4181
0
    reader->validate = XML_TEXTREADER_VALIDATE_RNG;
4182
0
    return(0);
4183
0
}
4184
#endif /* LIBXML_RELAXNG_ENABLED */
4185
4186
#ifdef LIBXML_SCHEMAS_ENABLED
4187
/**
4188
 * Validate the document as it is processed using XML Schema.
4189
 * Activation is only possible before the first Read().
4190
 * If both `xsd` and `ctxt` are NULL then XML Schema validation is deactivated.
4191
 *
4192
 * @param reader  the xmlTextReader used
4193
 * @param xsd  the path to a W3C XSD schema or NULL
4194
 * @param ctxt  the XML Schema validation context or NULL
4195
 * @param options  options (not used yet)
4196
 * @returns 0 in case the schemas validation could be (de)activated and
4197
 *         -1 in case of error.
4198
 */
4199
static int
4200
xmlTextReaderSchemaValidateInternal(xmlTextReaderPtr reader,
4201
            const char *xsd,
4202
            xmlSchemaValidCtxtPtr ctxt,
4203
            int options ATTRIBUTE_UNUSED)
4204
0
{
4205
0
    if (reader == NULL)
4206
0
        return(-1);
4207
4208
0
    if ((xsd != NULL) && (ctxt != NULL))
4209
0
  return(-1);
4210
4211
0
    if (((xsd != NULL) || (ctxt != NULL)) &&
4212
0
  ((reader->mode != XML_TEXTREADER_MODE_INITIAL) ||
4213
0
        (reader->ctxt == NULL)))
4214
0
  return(-1);
4215
4216
    /* Cleanup previous validation stuff. */
4217
0
    if (reader->xsdPlug != NULL) {
4218
0
  xmlSchemaSAXUnplug(reader->xsdPlug);
4219
0
  reader->xsdPlug = NULL;
4220
0
    }
4221
0
    if (reader->xsdValidCtxt != NULL) {
4222
0
  if (! reader->xsdPreserveCtxt)
4223
0
      xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
4224
0
  reader->xsdValidCtxt = NULL;
4225
0
    }
4226
0
    reader->xsdPreserveCtxt = 0;
4227
0
    if (reader->xsdSchemas != NULL) {
4228
0
  xmlSchemaFree(reader->xsdSchemas);
4229
0
  reader->xsdSchemas = NULL;
4230
0
    }
4231
4232
0
    if ((xsd == NULL) && (ctxt == NULL)) {
4233
  /* We just want to deactivate the validation, so get out. */
4234
0
  return(0);
4235
0
    }
4236
4237
0
    if (xsd != NULL) {
4238
0
  xmlSchemaParserCtxtPtr pctxt;
4239
  /* Parse the schema and create validation environment. */
4240
0
  pctxt = xmlSchemaNewParserCtxt(xsd);
4241
0
  if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
4242
0
      xmlSchemaSetParserStructuredErrors(pctxt,
4243
0
                    xmlTextReaderStructuredRelay, reader);
4244
0
        if (reader->resourceLoader != NULL)
4245
0
            xmlSchemaSetResourceLoader(pctxt, reader->resourceLoader,
4246
0
                                       reader->resourceCtxt);
4247
0
  reader->xsdSchemas = xmlSchemaParse(pctxt);
4248
0
  xmlSchemaFreeParserCtxt(pctxt);
4249
0
  if (reader->xsdSchemas == NULL)
4250
0
      return(-1);
4251
0
  reader->xsdValidCtxt = xmlSchemaNewValidCtxt(reader->xsdSchemas);
4252
0
  if (reader->xsdValidCtxt == NULL) {
4253
0
      xmlSchemaFree(reader->xsdSchemas);
4254
0
      reader->xsdSchemas = NULL;
4255
0
      return(-1);
4256
0
  }
4257
0
  reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
4258
0
      &(reader->ctxt->sax),
4259
0
      &(reader->ctxt->userData));
4260
0
  if (reader->xsdPlug == NULL) {
4261
0
      xmlSchemaFree(reader->xsdSchemas);
4262
0
      reader->xsdSchemas = NULL;
4263
0
      xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
4264
0
      reader->xsdValidCtxt = NULL;
4265
0
      return(-1);
4266
0
  }
4267
0
    } else {
4268
  /* Use the given validation context. */
4269
0
  reader->xsdValidCtxt = ctxt;
4270
0
  reader->xsdPreserveCtxt = 1;
4271
0
  reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
4272
0
      &(reader->ctxt->sax),
4273
0
      &(reader->ctxt->userData));
4274
0
  if (reader->xsdPlug == NULL) {
4275
0
      reader->xsdValidCtxt = NULL;
4276
0
      reader->xsdPreserveCtxt = 0;
4277
0
      return(-1);
4278
0
  }
4279
0
    }
4280
0
    xmlSchemaValidateSetLocator(reader->xsdValidCtxt,
4281
0
                                xmlTextReaderLocator,
4282
0
        (void *) reader);
4283
    /*
4284
    * Redirect the validation context's error channels to use
4285
    * the reader channels.
4286
    * TODO: In case the user provides the validation context we
4287
    *   could make this redirection optional.
4288
    */
4289
0
    if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
4290
0
  xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
4291
0
      xmlTextReaderStructuredRelay, reader);
4292
0
    reader->xsdValidErrors = 0;
4293
0
    reader->validate = XML_TEXTREADER_VALIDATE_XSD;
4294
0
    return(0);
4295
0
}
4296
4297
/**
4298
 * Use W3C XSD schema context to validate the document as it is processed.
4299
 * Activation is only possible before the first Read().
4300
 * If `ctxt` is NULL, then XML Schema validation is deactivated.
4301
 *
4302
 * @param reader  the xmlTextReader used
4303
 * @param ctxt  the XML Schema validation context or NULL
4304
 * @param options  options (not used yet)
4305
 * @returns 0 in case the schemas validation could be (de)activated and
4306
 *         -1 in case of error.
4307
 */
4308
int
4309
xmlTextReaderSchemaValidateCtxt(xmlTextReader *reader,
4310
            xmlSchemaValidCtxt *ctxt,
4311
            int options)
4312
0
{
4313
0
    return(xmlTextReaderSchemaValidateInternal(reader, NULL, ctxt, options));
4314
0
}
4315
4316
/**
4317
 * Use W3C XSD schema to validate the document as it is processed.
4318
 * Activation is only possible before the first Read().
4319
 * If `xsd` is NULL, then XML Schema validation is deactivated.
4320
 *
4321
 * @param reader  the xmlTextReader used
4322
 * @param xsd  the path to a W3C XSD schema or NULL
4323
 * @returns 0 in case the schemas validation could be (de)activated and
4324
 *         -1 in case of error.
4325
 */
4326
int
4327
xmlTextReaderSchemaValidate(xmlTextReader *reader, const char *xsd)
4328
0
{
4329
0
    return(xmlTextReaderSchemaValidateInternal(reader, xsd, NULL, 0));
4330
0
}
4331
#endif /* LIBXML_SCHEMAS_ENABLED */
4332
4333
#ifdef LIBXML_RELAXNG_ENABLED
4334
/**
4335
 * Use RelaxNG schema context to validate the document as it is processed.
4336
 * Activation is only possible before the first Read().
4337
 * If `ctxt` is NULL, then RelaxNG schema validation is deactivated.
4338
 *
4339
 * @param reader  the xmlTextReader used
4340
 * @param ctxt  the RelaxNG schema validation context or NULL
4341
 * @param options  options (not used yet)
4342
 * @returns 0 in case the schemas validation could be (de)activated and
4343
 *         -1 in case of error.
4344
 */
4345
int
4346
xmlTextReaderRelaxNGValidateCtxt(xmlTextReader *reader,
4347
         xmlRelaxNGValidCtxt *ctxt,
4348
         int options)
4349
0
{
4350
0
    return(xmlTextReaderRelaxNGValidateInternal(reader, NULL, ctxt, options));
4351
0
}
4352
4353
/**
4354
 * Use RelaxNG schema to validate the document as it is processed.
4355
 * Activation is only possible before the first Read().
4356
 * If `rng` is NULL, then RelaxNG schema validation is deactivated.
4357
 *
4358
 * @param reader  the xmlTextReader used
4359
 * @param rng  the path to a RelaxNG schema or NULL
4360
 * @returns 0 in case the schemas validation could be (de)activated and
4361
 *         -1 in case of error.
4362
 */
4363
int
4364
xmlTextReaderRelaxNGValidate(xmlTextReader *reader, const char *rng)
4365
0
{
4366
0
    return(xmlTextReaderRelaxNGValidateInternal(reader, rng, NULL, 0));
4367
0
}
4368
#endif /* LIBXML_RELAXNG_ENABLED */
4369
4370
/**
4371
 * Determine whether the current node is a namespace declaration
4372
 * rather than a regular attribute.
4373
 *
4374
 * @param reader  the xmlTextReader used
4375
 * @returns 1 if the current node is a namespace declaration, 0 if it
4376
 * is a regular attribute or other type of node, or -1 in case of
4377
 * error.
4378
 */
4379
int
4380
0
xmlTextReaderIsNamespaceDecl(xmlTextReader *reader) {
4381
0
    xmlNodePtr node;
4382
0
    if (reader == NULL)
4383
0
  return(-1);
4384
0
    if (reader->node == NULL)
4385
0
  return(-1);
4386
0
    if (reader->curnode != NULL)
4387
0
  node = reader->curnode;
4388
0
    else
4389
0
  node = reader->node;
4390
4391
0
    if (XML_NAMESPACE_DECL == node->type)
4392
0
  return(1);
4393
0
    else
4394
0
  return(0);
4395
0
}
4396
4397
/**
4398
 * Determine the XML version of the document being read.
4399
 *
4400
 * @param reader  the xmlTextReader used
4401
 * @returns a string containing the XML version of the document or NULL
4402
 * in case of error.  The string is deallocated with the reader.
4403
 */
4404
const xmlChar *
4405
0
xmlTextReaderConstXmlVersion(xmlTextReader *reader) {
4406
0
    xmlDocPtr doc = NULL;
4407
0
    if (reader == NULL)
4408
0
  return(NULL);
4409
0
    if (reader->doc != NULL)
4410
0
        doc = reader->doc;
4411
0
    else if (reader->ctxt != NULL)
4412
0
  doc = reader->ctxt->myDoc;
4413
0
    if (doc == NULL)
4414
0
  return(NULL);
4415
4416
0
    if (doc->version == NULL)
4417
0
  return(NULL);
4418
0
    else
4419
0
      return(constString(reader, doc->version));
4420
0
}
4421
4422
/**
4423
 * Determine the standalone status of the document being read.
4424
 *
4425
 * @param reader  the xmlTextReader used
4426
 * @returns 1 if the document was declared to be standalone, 0 if it
4427
 * was declared to be not standalone, or -1 if the document did not
4428
 * specify its standalone status or in case of error.
4429
 */
4430
int
4431
0
xmlTextReaderStandalone(xmlTextReader *reader) {
4432
0
    xmlDocPtr doc = NULL;
4433
0
    if (reader == NULL)
4434
0
  return(-1);
4435
0
    if (reader->doc != NULL)
4436
0
        doc = reader->doc;
4437
0
    else if (reader->ctxt != NULL)
4438
0
  doc = reader->ctxt->myDoc;
4439
0
    if (doc == NULL)
4440
0
  return(-1);
4441
4442
0
    return(doc->standalone);
4443
0
}
4444
4445
/************************************************************************
4446
 *                  *
4447
 *      Error Handling Extensions                       *
4448
 *                  *
4449
 ************************************************************************/
4450
4451
/**
4452
 * Obtain the line number for the given locator.
4453
 *
4454
 * @param locator  the void used
4455
 * @returns the line number or -1 in case of error.
4456
 */
4457
int
4458
0
xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) {
4459
    /* we know that locator is a xmlParserCtxtPtr */
4460
0
    xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
4461
0
    int ret = -1;
4462
4463
0
    if (locator == NULL)
4464
0
        return(-1);
4465
0
    if (ctx->node != NULL) {
4466
0
  ret = xmlGetLineNo(ctx->node);
4467
0
    }
4468
0
    else {
4469
  /* inspired from error.c */
4470
0
  xmlParserInputPtr input;
4471
0
  input = ctx->input;
4472
0
  if ((input->filename == NULL) && (ctx->inputNr > 1))
4473
0
      input = ctx->inputTab[ctx->inputNr - 2];
4474
0
  if (input != NULL) {
4475
0
      ret = input->line;
4476
0
  }
4477
0
  else {
4478
0
      ret = -1;
4479
0
  }
4480
0
    }
4481
4482
0
    return ret;
4483
0
}
4484
4485
/**
4486
 * Obtain the base URI for the given locator.
4487
 *
4488
 * @param locator  the void used
4489
 * @returns the base URI or NULL in case of error,
4490
 *    if non NULL it need to be freed by the caller.
4491
 */
4492
xmlChar *
4493
0
xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) {
4494
    /* we know that locator is a xmlParserCtxtPtr */
4495
0
    xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
4496
0
    xmlChar *ret = NULL;
4497
4498
0
    if (locator == NULL)
4499
0
        return(NULL);
4500
0
    if (ctx->node != NULL) {
4501
0
  ret = xmlNodeGetBase(NULL,ctx->node);
4502
0
    }
4503
0
    else {
4504
  /* inspired from error.c */
4505
0
  xmlParserInputPtr input;
4506
0
  input = ctx->input;
4507
0
  if ((input->filename == NULL) && (ctx->inputNr > 1))
4508
0
      input = ctx->inputTab[ctx->inputNr - 2];
4509
0
  if (input != NULL) {
4510
0
      ret = xmlStrdup(BAD_CAST input->filename);
4511
0
  }
4512
0
  else {
4513
0
      ret = NULL;
4514
0
  }
4515
0
    }
4516
4517
0
    return ret;
4518
0
}
4519
4520
/**
4521
 * Register a callback function that will be called on error and warnings.
4522
 *
4523
 * @deprecated Use #xmlTextReaderSetStructuredErrorHandler.
4524
 *
4525
 * If `f` is NULL, the default error and warning handlers are restored.
4526
 *
4527
 * @param reader  the xmlTextReader used
4528
 * @param f   the callback function to call on error and warnings
4529
 * @param arg  a user argument to pass to the callback function
4530
 */
4531
void
4532
xmlTextReaderSetErrorHandler(xmlTextReader *reader,
4533
                             xmlTextReaderErrorFunc f, void *arg)
4534
0
{
4535
0
    if (reader == NULL)
4536
0
        return;
4537
4538
0
    if (f != NULL) {
4539
0
        reader->errorFunc = f;
4540
0
        reader->sErrorFunc = NULL;
4541
0
        reader->errorFuncArg = arg;
4542
0
        xmlCtxtSetErrorHandler(reader->ctxt,
4543
0
                xmlTextReaderStructuredRelay, reader);
4544
0
#ifdef LIBXML_RELAXNG_ENABLED
4545
0
        if (reader->rngValidCtxt) {
4546
0
            xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
4547
0
                    xmlTextReaderStructuredRelay, reader);
4548
0
        }
4549
0
#endif
4550
0
#ifdef LIBXML_SCHEMAS_ENABLED
4551
0
        if (reader->xsdValidCtxt) {
4552
0
            xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
4553
0
                    xmlTextReaderStructuredRelay, reader);
4554
0
        }
4555
0
#endif
4556
0
    } else {
4557
        /* restore defaults */
4558
0
        reader->errorFunc = NULL;
4559
0
        reader->sErrorFunc = NULL;
4560
0
        reader->errorFuncArg = NULL;
4561
0
        xmlCtxtSetErrorHandler(reader->ctxt, NULL, NULL);
4562
0
#ifdef LIBXML_RELAXNG_ENABLED
4563
0
        if (reader->rngValidCtxt) {
4564
0
            xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL,
4565
0
                                               NULL);
4566
0
        }
4567
0
#endif
4568
0
#ifdef LIBXML_SCHEMAS_ENABLED
4569
0
        if (reader->xsdValidCtxt) {
4570
0
            xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL,
4571
0
                                              NULL);
4572
0
        }
4573
0
#endif
4574
0
    }
4575
0
}
4576
4577
/**
4578
* xmlTextReaderSetStructuredErrorHandler:
4579
 *
4580
 * Register a callback function that will be called on error and warnings.
4581
 *
4582
 * If `f` is NULL, the default error and warning handlers are restored.
4583
 *
4584
 * @param reader  the xmlTextReader used
4585
 * @param f   the callback function to call on error and warnings
4586
 * @param arg  a user argument to pass to the callback function
4587
 */
4588
void
4589
xmlTextReaderSetStructuredErrorHandler(xmlTextReader *reader,
4590
                                       xmlStructuredErrorFunc f, void *arg)
4591
0
{
4592
0
    if (reader == NULL)
4593
0
        return;
4594
4595
0
    if (f != NULL) {
4596
0
        reader->sErrorFunc = f;
4597
0
        reader->errorFunc = NULL;
4598
0
        reader->errorFuncArg = arg;
4599
0
        xmlCtxtSetErrorHandler(reader->ctxt,
4600
0
                xmlTextReaderStructuredRelay, reader);
4601
0
#ifdef LIBXML_RELAXNG_ENABLED
4602
0
        if (reader->rngValidCtxt) {
4603
0
            xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
4604
0
                    xmlTextReaderStructuredRelay, reader);
4605
0
        }
4606
0
#endif
4607
0
#ifdef LIBXML_SCHEMAS_ENABLED
4608
0
        if (reader->xsdValidCtxt) {
4609
0
            xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
4610
0
                    xmlTextReaderStructuredRelay, reader);
4611
0
        }
4612
0
#endif
4613
0
    } else {
4614
        /* restore defaults */
4615
0
        reader->errorFunc = NULL;
4616
0
        reader->sErrorFunc = NULL;
4617
0
        reader->errorFuncArg = NULL;
4618
0
        xmlCtxtSetErrorHandler(reader->ctxt, NULL, NULL);
4619
0
#ifdef LIBXML_RELAXNG_ENABLED
4620
0
        if (reader->rngValidCtxt) {
4621
0
            xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL,
4622
0
                                               NULL);
4623
0
        }
4624
0
#endif
4625
0
#ifdef LIBXML_SCHEMAS_ENABLED
4626
0
        if (reader->xsdValidCtxt) {
4627
0
            xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL,
4628
0
                                              NULL);
4629
0
        }
4630
0
#endif
4631
0
    }
4632
0
}
4633
4634
/**
4635
 * Retrieve the error callback function and user argument.
4636
 *
4637
 * @param reader  the xmlTextReader used
4638
 * @param f   the callback function or NULL is no callback has been registered
4639
 * @param arg  a user argument
4640
 */
4641
void
4642
xmlTextReaderGetErrorHandler(xmlTextReader *reader,
4643
                             xmlTextReaderErrorFunc * f, void **arg)
4644
0
{
4645
0
    if (f != NULL)
4646
0
        *f = reader->errorFunc;
4647
0
    if (arg != NULL)
4648
0
        *arg = reader->errorFuncArg;
4649
0
}
4650
4651
/**
4652
 * Register a callback function that will be called to load external
4653
 * resources like entities.
4654
 *
4655
 * @since 2.14.0
4656
 * @param reader  thr reader
4657
 * @param loader  resource loader
4658
 * @param data  user data which will be passed to the loader
4659
 */
4660
void
4661
xmlTextReaderSetResourceLoader(xmlTextReader *reader,
4662
0
                               xmlResourceLoader loader, void *data) {
4663
0
    if ((reader == NULL) || (reader->ctxt == NULL))
4664
0
        return;
4665
4666
0
    reader->resourceLoader = loader;
4667
0
    reader->resourceCtxt = data;
4668
4669
0
    xmlCtxtSetResourceLoader(reader->ctxt, loader, data);
4670
0
}
4671
4672
/**
4673
 * Retrieve the validity status from the parser context
4674
 *
4675
 * @param reader  the xmlTextReader used
4676
 * @returns the flag value 1 if valid, 0 if no, and -1 in case of error
4677
 */
4678
int
4679
xmlTextReaderIsValid(xmlTextReader *reader)
4680
0
{
4681
0
    if (reader == NULL)
4682
0
        return (-1);
4683
0
#ifdef LIBXML_RELAXNG_ENABLED
4684
0
    if (reader->validate == XML_TEXTREADER_VALIDATE_RNG)
4685
0
        return (reader->rngValidErrors == 0);
4686
0
#endif
4687
0
#ifdef LIBXML_SCHEMAS_ENABLED
4688
0
    if (reader->validate == XML_TEXTREADER_VALIDATE_XSD)
4689
0
        return (reader->xsdValidErrors == 0);
4690
0
#endif
4691
0
    if ((reader->ctxt != NULL) && (reader->ctxt->validate == 1))
4692
0
        return (reader->ctxt->valid);
4693
0
    return (0);
4694
0
}
4695
4696
/************************************************************************
4697
 *                  *
4698
 *  New set (2.6.0) of simpler and more flexible APIs   *
4699
 *                  *
4700
 ************************************************************************/
4701
4702
/**
4703
 * Setup an XML reader with new options
4704
 *
4705
 * @param reader  an XML reader
4706
 * @param input  xmlParserInputBuffer used to feed the reader, will
4707
 *         be destroyed with it.
4708
 * @param URL  the base URL to use for the document
4709
 * @param encoding  the document encoding, or NULL
4710
 * @param options  a combination of xmlParserOption
4711
 * @returns 0 in case of success and -1 in case of error.
4712
 */
4713
int
4714
xmlTextReaderSetup(xmlTextReader *reader,
4715
                   xmlParserInputBuffer *input, const char *URL,
4716
                   const char *encoding, int options)
4717
0
{
4718
0
    if (reader == NULL) {
4719
0
        if (input != NULL)
4720
0
      xmlFreeParserInputBuffer(input);
4721
0
        return (-1);
4722
0
    }
4723
4724
    /*
4725
     * we force the generation of compact text nodes on the reader
4726
     * since usr applications should never modify the tree
4727
     */
4728
0
    options |= XML_PARSE_COMPACT;
4729
4730
0
    reader->doc = NULL;
4731
0
    reader->entNr = 0;
4732
0
    reader->parserFlags = options;
4733
0
    reader->validate = XML_TEXTREADER_NOT_VALIDATE;
4734
0
    if ((input != NULL) && (reader->input != NULL) &&
4735
0
        (reader->allocs & XML_TEXTREADER_INPUT)) {
4736
0
  xmlFreeParserInputBuffer(reader->input);
4737
0
  reader->input = NULL;
4738
0
  reader->allocs -= XML_TEXTREADER_INPUT;
4739
0
    }
4740
0
    if (input != NULL) {
4741
0
  reader->input = input;
4742
0
  reader->allocs |= XML_TEXTREADER_INPUT;
4743
0
    }
4744
0
    if (reader->buffer == NULL)
4745
0
        reader->buffer = xmlBufCreate(50);
4746
0
    if (reader->buffer == NULL) {
4747
0
        return (-1);
4748
0
    }
4749
0
    if (reader->sax == NULL)
4750
0
  reader->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
4751
0
    if (reader->sax == NULL) {
4752
0
        return (-1);
4753
0
    }
4754
0
    xmlSAXVersion(reader->sax, 2);
4755
0
    reader->startElement = reader->sax->startElement;
4756
0
    reader->sax->startElement = xmlTextReaderStartElement;
4757
0
    reader->endElement = reader->sax->endElement;
4758
0
    reader->sax->endElement = xmlTextReaderEndElement;
4759
0
#ifdef LIBXML_SAX1_ENABLED
4760
0
    if (reader->sax->initialized == XML_SAX2_MAGIC) {
4761
0
#endif /* LIBXML_SAX1_ENABLED */
4762
0
        reader->startElementNs = reader->sax->startElementNs;
4763
0
        reader->sax->startElementNs = xmlTextReaderStartElementNs;
4764
0
        reader->endElementNs = reader->sax->endElementNs;
4765
0
        reader->sax->endElementNs = xmlTextReaderEndElementNs;
4766
0
#ifdef LIBXML_SAX1_ENABLED
4767
0
    } else {
4768
0
        reader->startElementNs = NULL;
4769
0
        reader->endElementNs = NULL;
4770
0
    }
4771
0
#endif /* LIBXML_SAX1_ENABLED */
4772
0
    reader->characters = reader->sax->characters;
4773
0
    reader->sax->characters = xmlTextReaderCharacters;
4774
0
    reader->sax->ignorableWhitespace = xmlTextReaderCharacters;
4775
0
    reader->cdataBlock = reader->sax->cdataBlock;
4776
0
    reader->sax->cdataBlock = xmlTextReaderCDataBlock;
4777
4778
0
    reader->mode = XML_TEXTREADER_MODE_INITIAL;
4779
0
    reader->node = NULL;
4780
0
    reader->curnode = NULL;
4781
0
    if (input != NULL) {
4782
0
        if (xmlBufUse(reader->input->buffer) < 4) {
4783
0
            xmlParserInputBufferRead(input, 4);
4784
0
        }
4785
0
        if (reader->ctxt == NULL) {
4786
0
            if (xmlBufUse(reader->input->buffer) >= 4) {
4787
0
                reader->ctxt = xmlCreatePushParserCtxt(reader->sax, NULL,
4788
0
           (const char *) xmlBufContent(reader->input->buffer),
4789
0
                                      4, URL);
4790
0
                reader->base = 0;
4791
0
                reader->cur = 4;
4792
0
            } else {
4793
0
                reader->ctxt =
4794
0
                    xmlCreatePushParserCtxt(reader->sax, NULL, NULL, 0, URL);
4795
0
                reader->base = 0;
4796
0
                reader->cur = 0;
4797
0
            }
4798
0
            if (reader->ctxt == NULL) {
4799
0
                return (-1);
4800
0
            }
4801
0
        } else {
4802
0
      xmlParserInputPtr inputStream;
4803
0
      xmlParserInputBufferPtr buf;
4804
4805
0
      xmlCtxtReset(reader->ctxt);
4806
0
      buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
4807
0
      if (buf == NULL) return(-1);
4808
0
      inputStream = xmlNewInputStream(reader->ctxt);
4809
0
      if (inputStream == NULL) {
4810
0
    xmlFreeParserInputBuffer(buf);
4811
0
    return(-1);
4812
0
      }
4813
4814
0
      if (URL == NULL)
4815
0
    inputStream->filename = NULL;
4816
0
      else
4817
0
    inputStream->filename = (char *)
4818
0
        xmlCanonicPath((const xmlChar *) URL);
4819
0
      inputStream->buf = buf;
4820
0
            xmlBufResetInput(buf->buffer, inputStream);
4821
4822
0
            if (xmlCtxtPushInput(reader->ctxt, inputStream) < 0) {
4823
0
                xmlFreeInputStream(inputStream);
4824
0
                return(-1);
4825
0
            }
4826
0
      reader->cur = 0;
4827
0
  }
4828
0
    }
4829
0
    if (reader->dict != NULL) {
4830
0
        if (reader->ctxt->dict != NULL) {
4831
0
      if (reader->dict != reader->ctxt->dict) {
4832
0
    xmlDictFree(reader->dict);
4833
0
    reader->dict = reader->ctxt->dict;
4834
0
      }
4835
0
  } else {
4836
0
      reader->ctxt->dict = reader->dict;
4837
0
  }
4838
0
    } else {
4839
0
  if (reader->ctxt->dict == NULL)
4840
0
      reader->ctxt->dict = xmlDictCreate();
4841
0
        reader->dict = reader->ctxt->dict;
4842
0
    }
4843
0
    reader->ctxt->_private = reader;
4844
0
    reader->ctxt->dictNames = 1;
4845
    /*
4846
     * use the parser dictionary to allocate all elements and attributes names
4847
     */
4848
0
    reader->ctxt->parseMode = XML_PARSE_READER;
4849
4850
0
#ifdef LIBXML_XINCLUDE_ENABLED
4851
0
    if (reader->xincctxt != NULL) {
4852
0
  xmlXIncludeFreeContext(reader->xincctxt);
4853
0
  reader->xincctxt = NULL;
4854
0
    }
4855
0
    if (options & XML_PARSE_XINCLUDE) {
4856
0
        reader->xinclude = 1;
4857
0
  options -= XML_PARSE_XINCLUDE;
4858
0
    } else
4859
0
        reader->xinclude = 0;
4860
0
    reader->in_xinclude = 0;
4861
0
#endif
4862
0
#ifdef LIBXML_PATTERN_ENABLED
4863
0
    if (reader->patternTab == NULL) {
4864
0
        reader->patternNr = 0;
4865
0
  reader->patternMax = 0;
4866
0
    }
4867
0
    while (reader->patternNr > 0) {
4868
0
        reader->patternNr--;
4869
0
  if (reader->patternTab[reader->patternNr] != NULL) {
4870
0
      xmlFreePattern(reader->patternTab[reader->patternNr]);
4871
0
            reader->patternTab[reader->patternNr] = NULL;
4872
0
  }
4873
0
    }
4874
0
#endif
4875
4876
0
    if (options & XML_PARSE_DTDVALID)
4877
0
        reader->validate = XML_TEXTREADER_VALIDATE_DTD;
4878
4879
0
    xmlCtxtUseOptions(reader->ctxt, options);
4880
0
    if (encoding != NULL)
4881
0
        xmlSwitchEncodingName(reader->ctxt, encoding);
4882
0
    if ((URL != NULL) && (reader->ctxt->input != NULL) &&
4883
0
        (reader->ctxt->input->filename == NULL)) {
4884
0
        reader->ctxt->input->filename = (char *)
4885
0
            xmlStrdup((const xmlChar *) URL);
4886
0
        if (reader->ctxt->input->filename == NULL)
4887
0
            return(-1);
4888
0
    }
4889
4890
0
    reader->doc = NULL;
4891
4892
0
    return (0);
4893
0
}
4894
4895
/**
4896
 * Set the maximum amplification factor. See #xmlCtxtSetMaxAmplification.
4897
 *
4898
 * @param reader  an XML reader
4899
 * @param maxAmpl  maximum amplification factor
4900
 */
4901
void
4902
xmlTextReaderSetMaxAmplification(xmlTextReader *reader, unsigned maxAmpl)
4903
0
{
4904
0
    if (reader == NULL)
4905
0
        return;
4906
0
    xmlCtxtSetMaxAmplification(reader->ctxt, maxAmpl);
4907
0
}
4908
4909
/**
4910
 * @since 2.13.0
4911
 *
4912
 * @param reader  an XML reader
4913
 * @returns the last error.
4914
 */
4915
const xmlError *
4916
xmlTextReaderGetLastError(xmlTextReader *reader)
4917
0
{
4918
0
    if ((reader == NULL) || (reader->ctxt == NULL))
4919
0
        return(NULL);
4920
0
    return(&reader->ctxt->lastError);
4921
0
}
4922
4923
/**
4924
 * This function provides the current index of the parser used
4925
 * by the reader, relative to the start of the current entity.
4926
 * This function actually just wraps a call to #xmlByteConsumed
4927
 * for the parser context associated with the reader.
4928
 * See #xmlByteConsumed for more information.
4929
 *
4930
 * @deprecated The returned value is mostly random and useless.
4931
 * It reflects the parser reading ahead and is in no way related to
4932
 * the current node.
4933
 *
4934
 * @param reader  an XML reader
4935
 * @returns the index in bytes from the beginning of the entity or -1
4936
 *         in case the index could not be computed.
4937
 */
4938
long
4939
0
xmlTextReaderByteConsumed(xmlTextReader *reader) {
4940
0
    xmlParserInputPtr in;
4941
4942
0
    if ((reader == NULL) || (reader->ctxt == NULL))
4943
0
        return(-1);
4944
0
    in = reader->ctxt->input;
4945
0
    if (in == NULL)
4946
0
        return(-1);
4947
0
    return(in->consumed + (in->cur - in->base));
4948
0
}
4949
4950
4951
/**
4952
 * Create an xmltextReader for a preparsed document.
4953
 *
4954
 * @param doc  a preparsed document
4955
 * @returns the new reader or NULL in case of error.
4956
 */
4957
xmlTextReader *
4958
xmlReaderWalker(xmlDoc *doc)
4959
0
{
4960
0
    xmlTextReaderPtr ret;
4961
4962
0
    if (doc == NULL)
4963
0
        return(NULL);
4964
4965
0
    ret = xmlMalloc(sizeof(xmlTextReader));
4966
0
    if (ret == NULL) {
4967
0
  return(NULL);
4968
0
    }
4969
0
    memset(ret, 0, sizeof(xmlTextReader));
4970
0
    ret->entNr = 0;
4971
0
    ret->input = NULL;
4972
0
    ret->mode = XML_TEXTREADER_MODE_INITIAL;
4973
0
    ret->node = NULL;
4974
0
    ret->curnode = NULL;
4975
0
    ret->base = 0;
4976
0
    ret->cur = 0;
4977
0
    ret->allocs = XML_TEXTREADER_CTXT;
4978
0
    ret->doc = doc;
4979
0
    ret->state = XML_TEXTREADER_START;
4980
0
    ret->dict = xmlDictCreate();
4981
0
    return(ret);
4982
0
}
4983
4984
/**
4985
 * Create an xmltextReader for an XML in-memory document.
4986
 * The parsing flags `options` are a combination of xmlParserOption.
4987
 *
4988
 * @param cur  a pointer to a zero terminated string
4989
 * @param URL  the base URL to use for the document
4990
 * @param encoding  the document encoding, or NULL
4991
 * @param options  a combination of xmlParserOption
4992
 * @returns the new reader or NULL in case of error.
4993
 */
4994
xmlTextReader *
4995
xmlReaderForDoc(const xmlChar * cur, const char *URL, const char *encoding,
4996
                int options)
4997
0
{
4998
0
    int len;
4999
5000
0
    if (cur == NULL)
5001
0
        return (NULL);
5002
0
    len = xmlStrlen(cur);
5003
5004
0
    return (xmlReaderForMemory
5005
0
            ((const char *) cur, len, URL, encoding, options));
5006
0
}
5007
5008
/**
5009
 * parse an XML file from the filesystem or the network.
5010
 * The parsing flags `options` are a combination of xmlParserOption.
5011
 *
5012
 * @param filename  a file or URL
5013
 * @param encoding  the document encoding, or NULL
5014
 * @param options  a combination of xmlParserOption
5015
 * @returns the new reader or NULL in case of error.
5016
 */
5017
xmlTextReader *
5018
xmlReaderForFile(const char *filename, const char *encoding, int options)
5019
0
{
5020
0
    xmlTextReaderPtr reader;
5021
5022
0
    reader = xmlNewTextReaderFilename(filename);
5023
0
    if (reader == NULL)
5024
0
        return (NULL);
5025
0
    if (xmlTextReaderSetup(reader, NULL, NULL, encoding, options) < 0) {
5026
0
        xmlFreeTextReader(reader);
5027
0
        return (NULL);
5028
0
    }
5029
0
    return (reader);
5030
0
}
5031
5032
/**
5033
 * Create an xmltextReader for an XML in-memory document.
5034
 * The parsing flags `options` are a combination of xmlParserOption.
5035
 *
5036
 * @param buffer  a pointer to a char array
5037
 * @param size  the size of the array
5038
 * @param URL  the base URL to use for the document
5039
 * @param encoding  the document encoding, or NULL
5040
 * @param options  a combination of xmlParserOption
5041
 * @returns the new reader or NULL in case of error.
5042
 */
5043
xmlTextReader *
5044
xmlReaderForMemory(const char *buffer, int size, const char *URL,
5045
                   const char *encoding, int options)
5046
0
{
5047
0
    xmlTextReaderPtr reader;
5048
0
    xmlParserInputBufferPtr buf;
5049
5050
0
    buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
5051
0
    if (buf == NULL) {
5052
0
        return (NULL);
5053
0
    }
5054
0
    reader = xmlNewTextReader(buf, URL);
5055
0
    if (reader == NULL) {
5056
0
        xmlFreeParserInputBuffer(buf);
5057
0
        return (NULL);
5058
0
    }
5059
0
    reader->allocs |= XML_TEXTREADER_INPUT;
5060
0
    if (xmlTextReaderSetup(reader, NULL, URL, encoding, options) < 0) {
5061
0
        xmlFreeTextReader(reader);
5062
0
        return (NULL);
5063
0
    }
5064
0
    return (reader);
5065
0
}
5066
5067
/**
5068
 * Create an xmltextReader for an XML from a file descriptor.
5069
 * The parsing flags `options` are a combination of xmlParserOption.
5070
 * NOTE that the file descriptor will not be closed when the
5071
 *      reader is closed or reset.
5072
 *
5073
 * @param fd  an open file descriptor
5074
 * @param URL  the base URL to use for the document
5075
 * @param encoding  the document encoding, or NULL
5076
 * @param options  a combination of xmlParserOption
5077
 * @returns the new reader or NULL in case of error.
5078
 */
5079
xmlTextReader *
5080
xmlReaderForFd(int fd, const char *URL, const char *encoding, int options)
5081
0
{
5082
0
    xmlTextReaderPtr reader;
5083
0
    xmlParserInputBufferPtr input;
5084
0
    xmlParserErrors code;
5085
5086
0
    if (fd < 0) {
5087
0
        xmlTextReaderErr(XML_ERR_ARGUMENT, "invalid argument");
5088
0
        return(NULL);
5089
0
    }
5090
5091
0
    input = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
5092
0
    if (input == NULL) {
5093
0
        xmlTextReaderErrMemory(NULL);
5094
0
        return(NULL);
5095
0
    }
5096
    /*
5097
     * TODO: Remove XML_INPUT_UNZIP
5098
     */
5099
0
    code = xmlInputFromFd(input, fd, XML_INPUT_UNZIP);
5100
0
    if (code != XML_ERR_OK) {
5101
0
        xmlTextReaderErr(code, "failed to open fd");
5102
0
        return(NULL);
5103
0
    }
5104
0
    reader = xmlNewTextReader(input, URL);
5105
0
    if (reader == NULL) {
5106
0
        xmlTextReaderErrMemory(NULL);
5107
0
        xmlFreeParserInputBuffer(input);
5108
0
        return(NULL);
5109
0
    }
5110
0
    reader->allocs |= XML_TEXTREADER_INPUT;
5111
0
    if (xmlTextReaderSetup(reader, NULL, URL, encoding, options) < 0) {
5112
0
        xmlTextReaderErrMemory(NULL);
5113
0
        xmlFreeTextReader(reader);
5114
0
        return(NULL);
5115
0
    }
5116
0
    return (reader);
5117
0
}
5118
5119
/**
5120
 * Create an xmltextReader for an XML document from I/O functions and source.
5121
 * The parsing flags `options` are a combination of xmlParserOption.
5122
 *
5123
 * @param ioread  an I/O read function
5124
 * @param ioclose  an I/O close function
5125
 * @param ioctx  an I/O handler
5126
 * @param URL  the base URL to use for the document
5127
 * @param encoding  the document encoding, or NULL
5128
 * @param options  a combination of xmlParserOption
5129
 * @returns the new reader or NULL in case of error.
5130
 */
5131
xmlTextReader *
5132
xmlReaderForIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
5133
               void *ioctx, const char *URL, const char *encoding,
5134
               int options)
5135
0
{
5136
0
    xmlTextReaderPtr reader;
5137
0
    xmlParserInputBufferPtr input;
5138
5139
0
    if (ioread == NULL)
5140
0
        return (NULL);
5141
5142
0
    input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
5143
0
                                         XML_CHAR_ENCODING_NONE);
5144
0
    if (input == NULL) {
5145
0
        if (ioclose != NULL)
5146
0
            ioclose(ioctx);
5147
0
        return (NULL);
5148
0
    }
5149
0
    reader = xmlNewTextReader(input, URL);
5150
0
    if (reader == NULL) {
5151
0
        xmlFreeParserInputBuffer(input);
5152
0
        return (NULL);
5153
0
    }
5154
0
    reader->allocs |= XML_TEXTREADER_INPUT;
5155
0
    if (xmlTextReaderSetup(reader, NULL, URL, encoding, options) < 0) {
5156
0
        xmlFreeTextReader(reader);
5157
0
        return (NULL);
5158
0
    }
5159
0
    return (reader);
5160
0
}
5161
5162
/**
5163
 * Setup an xmltextReader to parse a preparsed XML document.
5164
 * This reuses the existing `reader` xmlTextReader.
5165
 *
5166
 * @param reader  an XML reader
5167
 * @param doc  a preparsed document
5168
 * @returns 0 in case of success and -1 in case of error
5169
 */
5170
int
5171
xmlReaderNewWalker(xmlTextReader *reader, xmlDoc *doc)
5172
0
{
5173
0
    if (doc == NULL)
5174
0
        return (-1);
5175
0
    if (reader == NULL)
5176
0
        return (-1);
5177
5178
0
    if (reader->input != NULL) {
5179
0
        xmlFreeParserInputBuffer(reader->input);
5180
0
    }
5181
0
    if (reader->ctxt != NULL) {
5182
0
  xmlCtxtReset(reader->ctxt);
5183
0
    }
5184
5185
0
    reader->entNr = 0;
5186
0
    reader->input = NULL;
5187
0
    reader->mode = XML_TEXTREADER_MODE_INITIAL;
5188
0
    reader->node = NULL;
5189
0
    reader->curnode = NULL;
5190
0
    reader->base = 0;
5191
0
    reader->cur = 0;
5192
0
    reader->allocs = XML_TEXTREADER_CTXT;
5193
0
    reader->doc = doc;
5194
0
    reader->state = XML_TEXTREADER_START;
5195
0
    if (reader->dict == NULL) {
5196
0
        if ((reader->ctxt != NULL) && (reader->ctxt->dict != NULL))
5197
0
      reader->dict = reader->ctxt->dict;
5198
0
  else
5199
0
      reader->dict = xmlDictCreate();
5200
0
    }
5201
0
    return(0);
5202
0
}
5203
5204
/**
5205
 * Setup an xmltextReader to parse an XML in-memory document.
5206
 * The parsing flags `options` are a combination of xmlParserOption.
5207
 * This reuses the existing `reader` xmlTextReader.
5208
 *
5209
 * @param reader  an XML reader
5210
 * @param cur  a pointer to a zero terminated string
5211
 * @param URL  the base URL to use for the document
5212
 * @param encoding  the document encoding, or NULL
5213
 * @param options  a combination of xmlParserOption
5214
 * @returns 0 in case of success and -1 in case of error
5215
 */
5216
int
5217
xmlReaderNewDoc(xmlTextReader *reader, const xmlChar * cur,
5218
                const char *URL, const char *encoding, int options)
5219
0
{
5220
5221
0
    int len;
5222
5223
0
    if (cur == NULL)
5224
0
        return (-1);
5225
0
    if (reader == NULL)
5226
0
        return (-1);
5227
5228
0
    len = xmlStrlen(cur);
5229
0
    return (xmlReaderNewMemory(reader, (const char *)cur, len,
5230
0
                               URL, encoding, options));
5231
0
}
5232
5233
/**
5234
 * parse an XML file from the filesystem or the network.
5235
 * The parsing flags `options` are a combination of xmlParserOption.
5236
 * This reuses the existing `reader` xmlTextReader.
5237
 *
5238
 * @param reader  an XML reader
5239
 * @param filename  a file or URL
5240
 * @param encoding  the document encoding, or NULL
5241
 * @param options  a combination of xmlParserOption
5242
 * @returns 0 in case of success and -1 in case of error
5243
 */
5244
int
5245
xmlReaderNewFile(xmlTextReader *reader, const char *filename,
5246
                 const char *encoding, int options)
5247
0
{
5248
0
    xmlParserInputBufferPtr input;
5249
5250
0
    if ((filename == NULL) || (reader == NULL)) {
5251
0
        xmlTextReaderErr(XML_ERR_ARGUMENT, "invalid argument");
5252
0
        return(-1);
5253
0
    }
5254
5255
0
    if (xmlParserInputBufferCreateFilenameValue != NULL) {
5256
0
        input = xmlParserInputBufferCreateFilenameValue(filename,
5257
0
                XML_CHAR_ENCODING_NONE);
5258
0
        if (input == NULL) {
5259
0
            xmlTextReaderErr(XML_IO_ENOENT, "failed to open %s", filename);
5260
0
            return(-1);
5261
0
        }
5262
0
    } else {
5263
        /*
5264
         * TODO: Remove XML_INPUT_UNZIP
5265
         */
5266
0
        xmlParserInputFlags flags = XML_INPUT_UNZIP;
5267
0
        xmlParserErrors code;
5268
5269
0
        if ((options & XML_PARSE_NONET) == 0)
5270
0
            flags |= XML_INPUT_NETWORK;
5271
5272
0
        code = xmlParserInputBufferCreateUrl(filename, XML_CHAR_ENCODING_NONE,
5273
0
                                             flags, &input);
5274
0
        if (code != XML_ERR_OK) {
5275
0
            xmlTextReaderErr(code, "failed to open %s", filename);
5276
0
            return(-1);
5277
0
        }
5278
0
    }
5279
5280
0
    if (xmlTextReaderSetup(reader, input, filename, encoding, options) < 0) {
5281
0
        xmlTextReaderErrMemory(NULL);
5282
0
        return(-1);
5283
0
    }
5284
5285
0
    return(0);
5286
0
}
5287
5288
/**
5289
 * Setup an xmltextReader to parse an XML in-memory document.
5290
 * The parsing flags `options` are a combination of xmlParserOption.
5291
 * This reuses the existing `reader` xmlTextReader.
5292
 *
5293
 * @param reader  an XML reader
5294
 * @param buffer  a pointer to a char array
5295
 * @param size  the size of the array
5296
 * @param URL  the base URL to use for the document
5297
 * @param encoding  the document encoding, or NULL
5298
 * @param options  a combination of xmlParserOption
5299
 * @returns 0 in case of success and -1 in case of error
5300
 */
5301
int
5302
xmlReaderNewMemory(xmlTextReader *reader, const char *buffer, int size,
5303
                   const char *URL, const char *encoding, int options)
5304
0
{
5305
0
    xmlParserInputBufferPtr input;
5306
5307
0
    if (reader == NULL)
5308
0
        return (-1);
5309
0
    if (buffer == NULL)
5310
0
        return (-1);
5311
5312
0
    input = xmlParserInputBufferCreateMem(buffer, size,
5313
0
                                      XML_CHAR_ENCODING_NONE);
5314
0
    if (input == NULL) {
5315
0
        return (-1);
5316
0
    }
5317
0
    return (xmlTextReaderSetup(reader, input, URL, encoding, options));
5318
0
}
5319
5320
/**
5321
 * Setup an xmltextReader to parse an XML from a file descriptor.
5322
 * NOTE that the file descriptor will not be closed when the
5323
 *      reader is closed or reset.
5324
 * The parsing flags `options` are a combination of xmlParserOption.
5325
 * This reuses the existing `reader` xmlTextReader.
5326
 *
5327
 * @param reader  an XML reader
5328
 * @param fd  an open file descriptor
5329
 * @param URL  the base URL to use for the document
5330
 * @param encoding  the document encoding, or NULL
5331
 * @param options  a combination of xmlParserOption
5332
 * @returns 0 in case of success and -1 in case of error
5333
 */
5334
int
5335
xmlReaderNewFd(xmlTextReader *reader, int fd,
5336
               const char *URL, const char *encoding, int options)
5337
0
{
5338
0
    xmlParserInputBufferPtr input;
5339
0
    xmlParserErrors code;
5340
5341
0
    if ((fd < 0) || (reader == NULL)) {
5342
0
        xmlTextReaderErr(XML_ERR_ARGUMENT, "invalid argument");
5343
0
        return(-1);
5344
0
    }
5345
5346
0
    input = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
5347
0
    if (input == NULL) {
5348
0
        xmlTextReaderErrMemory(NULL);
5349
0
        return(-1);
5350
0
    }
5351
    /*
5352
     * TODO: Remove XML_INPUT_UNZIP
5353
     */
5354
0
    code = xmlInputFromFd(input, fd, XML_INPUT_UNZIP);
5355
0
    if (code != XML_ERR_OK) {
5356
0
        xmlTextReaderErr(code, "failed to open fd");
5357
0
        return(-1);
5358
0
    }
5359
0
    if (xmlTextReaderSetup(reader, input, URL, encoding, options) < 0) {
5360
0
        xmlTextReaderErrMemory(NULL);
5361
0
        return(-1);
5362
0
    }
5363
5364
0
    return(0);
5365
0
}
5366
5367
/**
5368
 * Setup an xmltextReader to parse an XML document from I/O functions
5369
 * and source.
5370
 * The parsing flags `options` are a combination of xmlParserOption.
5371
 * This reuses the existing `reader` xmlTextReader.
5372
 *
5373
 * @param reader  an XML reader
5374
 * @param ioread  an I/O read function
5375
 * @param ioclose  an I/O close function
5376
 * @param ioctx  an I/O handler
5377
 * @param URL  the base URL to use for the document
5378
 * @param encoding  the document encoding, or NULL
5379
 * @param options  a combination of xmlParserOption
5380
 * @returns 0 in case of success and -1 in case of error
5381
 */
5382
int
5383
xmlReaderNewIO(xmlTextReader *reader, xmlInputReadCallback ioread,
5384
               xmlInputCloseCallback ioclose, void *ioctx,
5385
               const char *URL, const char *encoding, int options)
5386
0
{
5387
0
    xmlParserInputBufferPtr input;
5388
5389
0
    if (ioread == NULL)
5390
0
        return (-1);
5391
0
    if (reader == NULL)
5392
0
        return (-1);
5393
5394
0
    input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
5395
0
                                         XML_CHAR_ENCODING_NONE);
5396
0
    if (input == NULL) {
5397
0
        if (ioclose != NULL)
5398
0
            ioclose(ioctx);
5399
0
        return (-1);
5400
0
    }
5401
0
    return (xmlTextReaderSetup(reader, input, URL, encoding, options));
5402
0
}
5403
5404
#endif /* LIBXML_READER_ENABLED */