Coverage Report

Created: 2025-07-23 08:13

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