Coverage Report

Created: 2025-08-28 06:44

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