xml.c 21.2 KB
Newer Older
1 2 3
/*
 * xml.c: XML based interfaces for the libvir library
 *
4
 * Copyright (C) 2005, 2007-2011 Red Hat, Inc.
5 6 7 8 9 10
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

11
#include <config.h>
12

13 14 15 16
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
17
#include <limits.h>
18
#include <math.h>               /* for isnan() */
19 20

#include "virterror_internal.h"
21
#include "xml.h"
22
#include "buf.h"
23
#include "util.h"
24
#include "memory.h"
25

26 27
#define VIR_FROM_THIS VIR_FROM_XML

28
#define virGenericReportError(from, code, ...)                          \
29
        virReportErrorHelper(from, code, __FILE__,                      \
30
                             __FUNCTION__, __LINE__, __VA_ARGS__)
31

32 33 34 35 36 37 38 39 40
#define virXMLError(code, ...)                                          \
        virGenericReportError(VIR_FROM_XML, code, __VA_ARGS__)


/* Internal data to be passed to SAX parser and used by error handler. */
struct virParserData {
    int domcode;
};

41 42 43 44 45 46 47

/************************************************************************
 *									*
 * Wrappers around libxml2 XPath specific functions			*
 *									*
 ************************************************************************/

48 49 50 51 52 53 54 55 56 57 58
/**
 * virXPathString:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 *
 * Convenience function to evaluate an XPath string
 *
 * Returns a new string which must be deallocated by the caller or NULL
 *         if the evaluation failed.
 */
char *
59
virXPathString(const char *xpath,
60
               xmlXPathContextPtr ctxt)
61
{
62
    xmlXPathObjectPtr obj;
63
    xmlNodePtr relnode;
64 65 66
    char *ret;

    if ((ctxt == NULL) || (xpath == NULL)) {
67
        virXMLError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
68
                    "%s", _("Invalid parameter to virXPathString()"));
69
        return (NULL);
70
    }
71
    relnode = ctxt->node;
72
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
73
    ctxt->node = relnode;
74
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
D
Daniel P. Berrange 已提交
75
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
76
        xmlXPathFreeObject(obj);
77
        return (NULL);
D
Daniel P. Berrange 已提交
78
    }
79 80 81
    ret = strdup((char *) obj->stringval);
    xmlXPathFreeObject(obj);
    if (ret == NULL) {
82
        virReportOOMError();
83
    }
84
    return (ret);
85 86
}

87 88 89 90 91 92 93 94 95 96 97 98 99
/**
 * virXPathStringLimit:
 * @xpath: the XPath string to evaluate
 * @maxlen: maximum length permittred string
 * @ctxt: an XPath context
 *
 * Wrapper for virXPathString, which validates the length of the returned
 * string.
 *
 * Returns a new string which must be deallocated by the caller or NULL if
 * the evaluation failed.
 */
char *
100
virXPathStringLimit(const char *xpath,
101 102 103
                    size_t maxlen,
                    xmlXPathContextPtr ctxt)
{
104
    char *tmp = virXPathString(xpath, ctxt);
105 106

    if (tmp != NULL && strlen(tmp) >= maxlen) {
107
        virXMLError(VIR_ERR_INTERNAL_ERROR,
P
Phil Petty 已提交
108
                    _("\'%s\' value longer than %zd bytes"),
109
                    xpath, maxlen);
P
Phil Petty 已提交
110 111
        VIR_FREE(tmp);
        return NULL;
112 113 114 115 116
    }

    return tmp;
}

117 118 119 120 121 122 123 124 125 126 127 128
/**
 * virXPathNumber:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @value: the returned double value
 *
 * Convenience function to evaluate an XPath number
 *
 * Returns 0 in case of success in which case @value is set,
 *         or -1 if the evaluation failed.
 */
int
129
virXPathNumber(const char *xpath,
130 131
               xmlXPathContextPtr ctxt,
               double *value)
132
{
133
    xmlXPathObjectPtr obj;
134
    xmlNodePtr relnode;
135 136

    if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
137
        virXMLError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
138
                    "%s", _("Invalid parameter to virXPathNumber()"));
139
        return (-1);
140
    }
141
    relnode = ctxt->node;
142
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
143
    ctxt->node = relnode;
144 145
    if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
        (isnan(obj->floatval))) {
146 147
        xmlXPathFreeObject(obj);
        return (-1);
148
    }
149

150 151
    *value = obj->floatval;
    xmlXPathFreeObject(obj);
152
    return (0);
153 154
}

155
static int
156
virXPathLongBase(const char *xpath,
157 158 159
                 xmlXPathContextPtr ctxt,
                 int base,
                 long *value)
160
{
161
    xmlXPathObjectPtr obj;
162
    xmlNodePtr relnode;
163 164 165
    int ret = 0;

    if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
166
        virXMLError(VIR_ERR_INTERNAL_ERROR,
167
                    "%s", _("Invalid parameter to virXPathLong()"));
168
        return (-1);
169
    }
170
    relnode = ctxt->node;
171
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
172
    ctxt->node = relnode;
173 174 175
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        char *conv = NULL;
176
        long val;
177

178
        val = strtol((const char *) obj->stringval, &conv, base);
179
        if (conv == (const char *) obj->stringval) {
180 181
            ret = -2;
        } else {
182 183
            *value = val;
        }
184 185
    } else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
               (!(isnan(obj->floatval)))) {
186 187 188 189
        *value = (long) obj->floatval;
        if (*value != obj->floatval) {
            ret = -2;
        }
190
    } else {
191
        ret = -1;
192
    }
193

194
    xmlXPathFreeObject(obj);
195
    return (ret);
196 197
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
/**
 * virXPathInt:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @value: the returned int value
 *
 * Convenience function to evaluate an XPath number
 *
 * Returns 0 in case of success in which case @value is set,
 *         or -1 if the XPath evaluation failed or -2 if the
 *         value doesn't have an int format.
 */
int
virXPathInt(const char *xpath,
            xmlXPathContextPtr ctxt,
            int *value)
{
    long tmp;
    int ret;

    ret = virXPathLongBase(xpath, ctxt, 10, &tmp);
    if (ret < 0)
        return ret;
    if ((int) tmp != tmp)
        return -2;
    *value = tmp;
    return 0;
}

227
/**
228
 * virXPathLong:
229 230 231 232 233 234 235 236 237 238 239
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @value: the returned long value
 *
 * Convenience function to evaluate an XPath number
 *
 * Returns 0 in case of success in which case @value is set,
 *         or -1 if the XPath evaluation failed or -2 if the
 *         value doesn't have a long format.
 */
int
240
virXPathLong(const char *xpath,
241 242 243
             xmlXPathContextPtr ctxt,
             long *value)
{
244
    return virXPathLongBase(xpath, ctxt, 10, value);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
}

/**
 * virXPathLongHex:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @value: the returned long value
 *
 * Convenience function to evaluate an XPath number
 * according to a base of 16
 *
 * Returns 0 in case of success in which case @value is set,
 *         or -1 if the XPath evaluation failed or -2 if the
 *         value doesn't have a long format.
 */
int
261
virXPathLongHex(const char *xpath,
262 263 264
                xmlXPathContextPtr ctxt,
                long *value)
{
265
    return virXPathLongBase(xpath, ctxt, 16, value);
266 267 268
}

static int
269
virXPathULongBase(const char *xpath,
270 271 272
                  xmlXPathContextPtr ctxt,
                  int base,
                  unsigned long *value)
273 274 275 276 277 278
{
    xmlXPathObjectPtr obj;
    xmlNodePtr relnode;
    int ret = 0;

    if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
279
        virXMLError(VIR_ERR_INTERNAL_ERROR,
280
                    "%s", _("Invalid parameter to virXPathULong()"));
281 282 283 284
        return (-1);
    }
    relnode = ctxt->node;
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
285
    ctxt->node = relnode;
286 287 288 289 290
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        char *conv = NULL;
        long val;

291
        val = strtoul((const char *) obj->stringval, &conv, base);
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
        if (conv == (const char *) obj->stringval) {
            ret = -2;
        } else {
            *value = val;
        }
    } else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
               (!(isnan(obj->floatval)))) {
        *value = (unsigned long) obj->floatval;
        if (*value != obj->floatval) {
            ret = -2;
        }
    } else {
        ret = -1;
    }

    xmlXPathFreeObject(obj);
    return (ret);
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
/**
 * virXPathUInt:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @value: the returned int value
 *
 * Convenience function to evaluate an XPath number
 *
 * Returns 0 in case of success in which case @value is set,
 *         or -1 if the XPath evaluation failed or -2 if the
 *         value doesn't have an int format.
 */
int
virXPathUInt(const char *xpath,
             xmlXPathContextPtr ctxt,
             unsigned int *value)
{
    unsigned long tmp;
    int ret;

    ret = virXPathULongBase(xpath, ctxt, 10, &tmp);
    if (ret < 0)
        return ret;
    if ((unsigned int) tmp != tmp)
        return -2;
    *value = tmp;
    return 0;
}

340 341 342 343 344 345 346 347 348 349 350 351 352
/**
 * virXPathULong:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @value: the returned long value
 *
 * Convenience function to evaluate an XPath number
 *
 * Returns 0 in case of success in which case @value is set,
 *         or -1 if the XPath evaluation failed or -2 if the
 *         value doesn't have a long format.
 */
int
353
virXPathULong(const char *xpath,
354 355 356
              xmlXPathContextPtr ctxt,
              unsigned long *value)
{
357
    return virXPathULongBase(xpath, ctxt, 10, value);
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
}

/**
 * virXPathUHex:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @value: the returned long value
 *
 * Convenience function to evaluate an XPath number
 * according to base of 16
 *
 * Returns 0 in case of success in which case @value is set,
 *         or -1 if the XPath evaluation failed or -2 if the
 *         value doesn't have a long format.
 */
int
374
virXPathULongHex(const char *xpath,
375 376 377
                 xmlXPathContextPtr ctxt,
                 unsigned long *value)
{
378
    return virXPathULongBase(xpath, ctxt, 16, value);
379 380
}

M
Mark McLoughlin 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393
/**
 * virXPathULongLong:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @value: the returned long long value
 *
 * Convenience function to evaluate an XPath number
 *
 * Returns 0 in case of success in which case @value is set,
 *         or -1 if the XPath evaluation failed or -2 if the
 *         value doesn't have a long format.
 */
int
394
virXPathULongLong(const char *xpath,
M
Mark McLoughlin 已提交
395 396 397 398 399 400 401 402
                  xmlXPathContextPtr ctxt,
                  unsigned long long *value)
{
    xmlXPathObjectPtr obj;
    xmlNodePtr relnode;
    int ret = 0;

    if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
403
        virXMLError(VIR_ERR_INTERNAL_ERROR,
M
Mark McLoughlin 已提交
404 405 406 407 408
                    "%s", _("Invalid parameter to virXPathULong()"));
        return (-1);
    }
    relnode = ctxt->node;
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
409
    ctxt->node = relnode;
M
Mark McLoughlin 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        char *conv = NULL;
        unsigned long long val;

        val = strtoull((const char *) obj->stringval, &conv, 10);
        if (conv == (const char *) obj->stringval) {
            ret = -2;
        } else {
            *value = val;
        }
    } else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
               (!(isnan(obj->floatval)))) {
        *value = (unsigned long long) obj->floatval;
        if (*value != obj->floatval) {
            ret = -2;
        }
    } else {
        ret = -1;
    }

    xmlXPathFreeObject(obj);
    return (ret);
}

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
/**
 * virXPathULongLong:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @value: the returned long long value
 *
 * Convenience function to evaluate an XPath number
 *
 * Returns 0 in case of success in which case @value is set,
 *         or -1 if the XPath evaluation failed or -2 if the
 *         value doesn't have a long format.
 */
int
virXPathLongLong(const char *xpath,
                 xmlXPathContextPtr ctxt,
                 long long *value)
{
    xmlXPathObjectPtr obj;
    xmlNodePtr relnode;
    int ret = 0;

    if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
        virXMLError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("Invalid parameter to virXPathLongLong()"));
        return (-1);
    }
    relnode = ctxt->node;
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
    ctxt->node = relnode;
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        char *conv = NULL;
        unsigned long long val;

        val = strtoll((const char *) obj->stringval, &conv, 10);
        if (conv == (const char *) obj->stringval) {
            ret = -2;
        } else {
            *value = val;
        }
    } else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
               (!(isnan(obj->floatval)))) {
        *value = (long long) obj->floatval;
        if (*value != obj->floatval) {
            ret = -2;
        }
    } else {
        ret = -1;
    }

    xmlXPathFreeObject(obj);
    return (ret);
}

489 490 491 492 493 494 495
char *
virXMLPropString(xmlNodePtr node,
                 const char *name)
{
    return (char *)xmlGetProp(node, BAD_CAST name);
}

496 497 498 499 500 501 502 503 504 505
/**
 * virXPathBoolean:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 *
 * Convenience function to evaluate an XPath boolean
 *
 * Returns 0 if false, 1 if true, or -1 if the evaluation failed.
 */
int
506
virXPathBoolean(const char *xpath,
507
                xmlXPathContextPtr ctxt)
508
{
509
    xmlXPathObjectPtr obj;
510
    xmlNodePtr relnode;
511 512 513
    int ret;

    if ((ctxt == NULL) || (xpath == NULL)) {
514
        virXMLError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
515
                    "%s", _("Invalid parameter to virXPathBoolean()"));
516
        return (-1);
517
    }
518
    relnode = ctxt->node;
519
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
520
    ctxt->node = relnode;
521 522
    if ((obj == NULL) || (obj->type != XPATH_BOOLEAN) ||
        (obj->boolval < 0) || (obj->boolval > 1)) {
523 524
        xmlXPathFreeObject(obj);
        return (-1);
525 526
    }
    ret = obj->boolval;
527

528
    xmlXPathFreeObject(obj);
529
    return (ret);
530 531 532 533 534 535 536 537 538 539 540 541 542
}

/**
 * virXPathNode:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 *
 * Convenience function to evaluate an XPath node set and returning
 * only one node, the first one in the set if any
 *
 * Returns a pointer to the node or NULL if the evaluation failed.
 */
xmlNodePtr
543
virXPathNode(const char *xpath,
544
             xmlXPathContextPtr ctxt)
545
{
546
    xmlXPathObjectPtr obj;
547
    xmlNodePtr relnode;
548 549 550
    xmlNodePtr ret;

    if ((ctxt == NULL) || (xpath == NULL)) {
551
        virXMLError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
552
                    "%s", _("Invalid parameter to virXPathNode()"));
553
        return (NULL);
554
    }
555
    relnode = ctxt->node;
556
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
557
    ctxt->node = relnode;
558 559
    if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
        (obj->nodesetval == NULL) || (obj->nodesetval->nodeNr <= 0) ||
560 561 562
        (obj->nodesetval->nodeTab == NULL)) {
        xmlXPathFreeObject(obj);
        return (NULL);
563
    }
564

565 566
    ret = obj->nodesetval->nodeTab[0];
    xmlXPathFreeObject(obj);
567
    return (ret);
568
}
569

570 571 572 573 574 575 576 577 578 579 580 581
/**
 * virXPathNodeSet:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @list: the returned list of nodes (or NULL if only count matters)
 *
 * Convenience function to evaluate an XPath node set
 *
 * Returns the number of nodes found in which case @list is set (and
 *         must be freed) or -1 if the evaluation failed.
 */
int
582
virXPathNodeSet(const char *xpath,
583 584
                xmlXPathContextPtr ctxt,
                xmlNodePtr **list)
585
{
586
    xmlXPathObjectPtr obj;
587
    xmlNodePtr relnode;
588 589 590
    int ret;

    if ((ctxt == NULL) || (xpath == NULL)) {
591
        virXMLError(VIR_ERR_INTERNAL_ERROR,
J
Jim Meyering 已提交
592
                    "%s", _("Invalid parameter to virXPathNodeSet()"));
593
        return (-1);
594
    }
595 596 597 598

    if (list != NULL)
        *list = NULL;

599
    relnode = ctxt->node;
600
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
601
    ctxt->node = relnode;
D
Daniel Veillard 已提交
602 603
    if (obj == NULL)
        return(0);
604

D
Daniel Veillard 已提交
605
    if (obj->type != XPATH_NODESET) {
606 607
        virXMLError(VIR_ERR_INTERNAL_ERROR,
                    _("Incorrect xpath '%s'"), xpath);
608 609
        xmlXPathFreeObject(obj);
        return (-1);
610
    }
611

D
Daniel Veillard 已提交
612 613 614 615
    if ((obj->nodesetval == NULL)  || (obj->nodesetval->nodeNr < 0)) {
        xmlXPathFreeObject(obj);
        return (0);
    }
616

617
    ret = obj->nodesetval->nodeNr;
618
    if (list != NULL && ret) {
619
        if (VIR_ALLOC_N(*list, ret) < 0) {
620
            virReportOOMError();
621
            ret = -1;
622 623 624 625
        } else {
            memcpy(*list, obj->nodesetval->nodeTab,
                   ret * sizeof(xmlNodePtr));
        }
626 627
    }
    xmlXPathFreeObject(obj);
628
    return (ret);
629
}
630 631 632 633 634 635


/**
 * catchXMLError:
 *
 * Called from SAX on parsing errors in the XML.
636 637
 *
 * This version is heavily based on xmlParserPrintFileContextInternal from libxml2.
638 639 640 641 642 643
 */
static void
catchXMLError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
{
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
    const xmlChar *cur, *base;
    unsigned int n, col;	/* GCC warns if signed, because compared with sizeof() */
    int domcode = VIR_FROM_XML;

    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *contextstr = NULL;
    char *pointerstr = NULL;


    /* conditions for error printing */
    if (!ctxt ||
        (virGetLastError() != NULL) ||
        ctxt->input == NULL ||
        ctxt->lastError.level != XML_ERR_FATAL ||
        ctxt->lastError.message == NULL)
        return;

    if (ctxt->_private)
662 663
            domcode = ((struct virParserData *) ctxt->_private)->domcode;

664 665 666 667 668 669 670

    cur = ctxt->input->cur;
    base = ctxt->input->base;

    /* skip backwards over any end-of-lines */
    while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
        cur--;
671 672
    }

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
    /* search backwards for beginning-of-line (to max buff size) */
    while ((cur > base) && (*(cur) != '\n') && (*(cur) != '\r'))
        cur--;
    if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;

    /* calculate the error position in terms of the current position */
    col = ctxt->input->cur - cur;

    /* search forward for end-of-line (to max buff size) */
    /* copy selected text to our buffer */
    while ((*cur != 0) && (*(cur) != '\n') && (*(cur) != '\r')) {
        virBufferAddChar(&buf, *cur++);
    }

    /* create blank line with problem pointer */
    contextstr = virBufferContentAndReset(&buf);

    /* (leave buffer space for pointer + line terminator) */
    for  (n = 0; (n<col) && (contextstr[n] != 0); n++) {
        if (contextstr[n] == '\t')
            virBufferAddChar(&buf, '\t');
        else
            virBufferAddChar(&buf, '-');
    }

    virBufferAddChar(&buf, '^');

    pointerstr = virBufferContentAndReset(&buf);

    if (ctxt->lastError.file) {
        virGenericReportError(domcode, VIR_ERR_XML_DETAIL,
                              _("%s:%d: %s%s\n%s"),
                              ctxt->lastError.file,
                              ctxt->lastError.line,
                              ctxt->lastError.message,
                              contextstr,
                              pointerstr);
    } else {
         virGenericReportError(domcode, VIR_ERR_XML_DETAIL,
                              _("at line %d: %s%s\n%s"),
                              ctxt->lastError.line,
                              ctxt->lastError.message,
                              contextstr,
                              pointerstr);
    }

    VIR_FREE(contextstr);
    VIR_FREE(pointerstr);
}
722 723 724 725 726 727 728

/**
 * virXMLParseHelper:
 * @domcode: error domain of the caller, usually VIR_FROM_THIS
 * @filename: file to be parsed or NULL if string parsing is requested
 * @xmlStr: XML string to be parsed in case filename is NULL
 * @url: URL of XML document for string parser
729
 * @ctxt: optional pointer to populate with new context pointer
730 731 732 733 734 735 736 737 738 739
 *
 * Parse XML document provided either as a file or a string. The function
 * guarantees that the XML document contains a root element.
 *
 * Returns parsed XML document.
 */
xmlDocPtr
virXMLParseHelper(int domcode,
                  const char *filename,
                  const char *xmlStr,
740 741
                  const char *url,
                  xmlXPathContextPtr *ctxt)
742 743 744 745 746 747 748
{
    struct virParserData private;
    xmlParserCtxtPtr pctxt;
    xmlDocPtr xml = NULL;

    /* Set up a parser context so we can catch the details of XML errors. */
    pctxt = xmlNewParserCtxt();
749 750
    if (!pctxt || !pctxt->sax) {
        virReportOOMError();
751
        goto error;
752
    }
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775

    private.domcode = domcode;
    pctxt->_private = &private;
    pctxt->sax->error = catchXMLError;

    if (filename) {
        xml = xmlCtxtReadFile(pctxt, filename, NULL,
                              XML_PARSE_NOENT | XML_PARSE_NONET |
                              XML_PARSE_NOWARNING);
    } else {
        xml = xmlCtxtReadDoc(pctxt, BAD_CAST xmlStr, url, NULL,
                             XML_PARSE_NOENT | XML_PARSE_NONET |
                             XML_PARSE_NOWARNING);
    }
    if (!xml)
        goto error;

    if (xmlDocGetRootElement(xml) == NULL) {
        virGenericReportError(domcode, VIR_ERR_INTERNAL_ERROR,
                              "%s", _("missing root element"));
        goto error;
    }

776 777 778 779 780 781 782 783 784
    if (ctxt) {
        *ctxt = xmlXPathNewContext(xml);
        if (!*ctxt) {
            virReportOOMError();
            goto error;
        }
        (*ctxt)->node = xmlDocGetRootElement(xml);
    }

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
cleanup:
    xmlFreeParserCtxt(pctxt);

    return xml;

error:
    xmlFreeDoc(xml);
    xml = NULL;

    if (virGetLastError() == NULL) {
        virGenericReportError(domcode, VIR_ERR_XML_ERROR,
                              "%s", _("failed to parse xml document"));
    }
    goto cleanup;
}