Coverage Report

Created: 2025-07-07 10:01

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