Coverage Report

Created: 2023-06-07 06:54

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