Clear the temporary password buffer before returning.
[usmb.git] / xml.c
blob567aeacba4b9fa43f47d14f7dce00c7a79edc718
1 /* usmb - mount SMB shares via FUSE and Samba
2 * Copyright (C) 2006-2008 Geoff Johnstone
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <libxml/xmlreader.h>
18 #include <libxml/xpath.h>
19 #include <libxml/xpathInternals.h>
20 #include <assert.h>
21 #include <stdbool.h>
22 #include <string.h>
23 #include "xml.h"
24 #include "utils.h"
27 bool xml_validate_relaxng (xmlDocPtr doc, const char *schema)
29 xmlRelaxNGParserCtxtPtr rngpcptr = NULL;
30 xmlRelaxNGPtr rngptr = NULL;
31 xmlRelaxNGValidCtxtPtr rngvptr = NULL;
32 bool ret = false;
34 assert (NULL != doc);
38 rngpcptr = xmlRelaxNGNewMemParserCtxt (schema, strlen (schema));
39 if (NULL == rngpcptr)
40 break;
42 rngptr = xmlRelaxNGParse (rngpcptr);
43 if (NULL == rngptr)
44 break;
46 rngvptr = xmlRelaxNGNewValidCtxt (rngptr);
47 if (NULL == rngvptr)
48 break;
50 ret = (0 == xmlRelaxNGValidateDoc (rngvptr, doc));
51 } while (false /*CONSTCOND*/);
53 if (NULL != rngvptr)
54 xmlRelaxNGFreeValidCtxt (rngvptr);
56 if (NULL != rngptr)
57 xmlRelaxNGFree (rngptr);
59 if (NULL != rngpcptr)
60 xmlRelaxNGFreeParserCtxt (rngpcptr);
62 return ret;
66 bool xml_xpath_attr_value (xmlXPathContextPtr ctx,
67 char *xpath,
68 const char *attribute,
69 char **out)
71 xmlXPathObjectPtr obj;
72 xmlChar *tmp;
74 assert (NULL != ctx);
75 assert (NULL != xpath);
76 assert (NULL != out);
78 *out = NULL;
80 obj = xmlXPathEval (BAD_CAST xpath, ctx);
81 if (NULL == obj)
83 DEBUG (fputs ("XPath evaluation error\n", stderr));
84 return false;
89 if (XPATH_NODESET != obj->type)
91 DEBUG (fputs ("XPath evaluation didn't return a nodeset\n", stderr));
92 break;
95 if (NULL == obj->nodesetval)
97 DEBUG (fputs ("nodesetval is NULL\n", stderr));
98 break;
101 if (1 != obj->nodesetval->nodeNr)
103 DEBUG (fprintf (stderr, "Nodeset has %d elements\n",
104 obj->nodesetval->nodeNr));
105 break;
108 tmp = xmlGetProp (obj->nodesetval->nodeTab[0], BAD_CAST attribute);
109 if (NULL == tmp)
110 break;
112 *out = xstrdup ((char *)tmp);
113 if (NULL == *out)
114 break;
116 xmlXPathFreeObject (obj);
117 return true;
118 /*NOTREACHED*/
119 } while (false /*CONSTCOND*/);
121 *out = NULL;
122 xmlXPathFreeObject (obj);
123 return false;
127 bool xml_xpath_text (xmlXPathContextPtr ctx, char *xpath, char **out)
129 xmlXPathObjectPtr obj;
131 assert (NULL != ctx);
132 assert (NULL != xpath);
133 assert (NULL != out);
135 *out = NULL;
137 DEBUG (fprintf (stderr, "xml_xpath_text (%s)\n", xpath));
139 obj = xmlXPathEval (BAD_CAST xpath, ctx);
140 if (NULL == obj)
142 DEBUG (fputs ("XPath evaluation error\n", stderr));
143 return false;
148 if (XPATH_NODESET != obj->type)
150 DEBUG (fputs ("XPath evaluation didn't return a nodeset\n", stderr));
151 break;
154 if (NULL == obj->nodesetval)
156 DEBUG (fputs ("nodesetval is NULL\n", stderr));
157 break;
160 if (1 != obj->nodesetval->nodeNr)
162 DEBUG (fprintf (stderr, "Nodeset has %d elements\n",
163 obj->nodesetval->nodeNr));
164 break;
167 if (NULL == obj->nodesetval->nodeTab[0]->content)
169 DEBUG (fputs ("Node has no text\n", stderr));
170 break;
173 *out = xstrdup ((char *)obj->nodesetval->nodeTab[0]->content);
174 if (NULL == *out)
175 break;
177 xmlXPathFreeObject (obj);
178 return true;
179 /*NOTREACHED*/
180 } while (false /*CONSTCOND*/);
182 *out = NULL;
183 xmlXPathFreeObject (obj);
184 return false;