Coverage Report

Created: 2023-06-07 06:06

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