Coverage Report

Created: 2022-02-21 09:34

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