Implement DisableOwnDataBox and EnableOwnDataBox
[libisds.git] / src / physxml.c
blobc382a3a9367e281efa2a247c756f7273080f1ef6
1 #define _POSIX_SOURCE /* For strtok_r */
2 #include "isds_priv.h"
3 #include "physxml.h"
4 #include "utils.h"
6 #include <string.h>
7 #include <expat.h>
8 #include <inttypes.h>
10 #define NS_CHAR_SEPARATOR '>'
12 struct expat_data {
13 XML_Parser parser;
14 const XML_Char **elements; /* NULL terminated array of elements */
15 _Bool found;
16 size_t *start;
17 size_t *end;
18 int depth; /* Current parser depth, root element is 0 */
19 int element_depth; /* elements[element_depth] we are in,
20 -1 if we are not in any (root mismatch)*/
24 /* Check for expat compile-time configuration */
25 _hidden isds_error init_expat(void) {
26 XML_Expat_Version current;
27 const int min_major = 1;
28 const int min_minor = 95;
29 const int min_micro = 8;
30 const XML_Feature *features; /* Static array stored in expat BSS */
31 _Bool ns_supported = 0;
34 * Max(XML_Size) <= Max(size_t)
35 * XML_Char is char, not a wchar_t
36 * XML_UNICODE is undefined (i.e. strings in UTF-8)
37 * */
39 /* Check minimal expat version */
40 current = XML_ExpatVersionInfo();
41 if ( (current.major < min_major) ||
42 (current.major == min_major && current.minor < min_minor) ||
43 (current.major == min_major && current.minor == min_minor &&
44 current.micro < min_micro) ) {
45 isds_log(ILF_ISDS, ILL_CRIT,
46 _("Minimal %d.%d.%d Expat version required. "
47 "Current version is %d.%d.%d\n"),
48 min_major, min_minor, min_micro,
49 current.major, current.minor, current.micro);
50 return IE_ERROR;
53 /* XML_Char must be char, not a wchar_t */
54 features = XML_GetFeatureList();
55 while (features->feature != XML_FEATURE_END) {
56 switch (features->feature) {
57 case XML_FEATURE_UNICODE_WCHAR_T:
58 case XML_FEATURE_UNICODE:
59 isds_log(ILF_ISDS, ILL_CRIT,
60 _("Expat compiled with UTF-16 (wide) characters\n"));
61 return IE_ERROR;
62 break;
63 case XML_FEATURE_SIZEOF_XML_CHAR:
64 if (features->value != sizeof(char)) {
65 isds_log(ILF_ISDS, ILL_CRIT,
66 "Expat compiled with XML_Chars incompatible "
67 "with chars\n");
68 return IE_ERROR;
70 break;
71 case XML_FEATURE_NS:
72 ns_supported = 1;
73 default:
74 break;
76 features++;
79 if (!ns_supported) {
80 isds_log(ILF_ISDS, ILL_CRIT,
81 _("Expat not compiled with name space support\n"));
82 return IE_ERROR;
85 return IE_SUCCESS;
89 /* Breaks element path address into NULL terminated array of elements in
90 * preserved order. Zeroth array element will be first path element.
91 * @path element address, content will be damaged
92 * @return array of elements, NULL in case of error */
93 static const XML_Char **path2elements(XML_Char *path) {
94 const XML_Char **elements = NULL;
95 XML_Char *tmp_path;
96 char *saveptr = NULL;
97 XML_Char *element;
98 unsigned int depth = 0;
100 if (!path) return NULL;
102 elements = malloc(sizeof(elements[0]) * (strlen(path) + 1));
103 if (!elements) return NULL;
105 elements[0] = NULL;
107 tmp_path = path;
108 while ((element = (XML_Char *) strtok_r(tmp_path,
109 PHYSXML_ELEMENT_SEPARATOR, &saveptr))) {
110 tmp_path = NULL;
111 elements[depth++] = element;
114 elements[depth] = NULL;
115 return elements;
119 /* Examine start and empty element tag.
120 * @name is expanded name */
121 static void XMLCALL element_start(void *userData, const XML_Char *name,
122 const XML_Char **atts) {
123 struct expat_data *data = (struct expat_data *) userData;
124 data->depth++;
126 const XML_Index index = XML_GetCurrentByteIndex(data->parser);
127 /* XXX: Because document length is stored as size_t, index always fits
128 * size_t. */
129 const size_t boundary = index;
131 isds_log(ILF_XML, ILL_DEBUG, _("Start: name=%s, depth=%zd, offset=%#jx "
132 "=> boundary=%#zx\n"),
133 name, data->depth, (uintmax_t)index, boundary);
135 if ((!data->found) &&
136 (data->depth == data->element_depth + 1) &&
137 (!strcmp(data->elements[data->element_depth + 1], name))) {
138 data->element_depth++;
140 isds_log(ILF_XML, ILL_DEBUG,
141 _("\tStart tag for element `%s' found\n"),
142 data->elements[data->element_depth]);
144 if (!data->elements[data->element_depth + 1]) {
145 data->found = 1;
146 *data->start = boundary;
152 /* Examine end and empty element tag.
153 * @name is expanded name */
154 static void XMLCALL element_end(void *userData, const XML_Char *name) {
156 struct expat_data *data = (struct expat_data *) userData;
157 enum XML_Status xerr;
159 const XML_Index index = (uintmax_t) XML_GetCurrentByteIndex(data->parser);
160 const int count = XML_GetCurrentByteCount(data->parser);
161 /* XXX: Because document length is stored as size_t, index + count always
162 * fits size_t. */
163 const size_t boundary = index + count - 1;
165 isds_log(ILF_XML, ILL_DEBUG, _("End: name=%s, depth=%zd, offset=%#jx "
166 "count=%u => boundary=%#zx\n"),
167 name, data->depth, (uintmax_t)index, count, boundary);
169 if (data->element_depth == data->depth) {
170 if (data->found) {
171 isds_log(ILF_XML, ILL_DEBUG,
172 _("\tEnd tag for element `%s' found\n"),
173 data->elements[data->element_depth]);
174 *data->end = boundary;
176 /* Here we can stop parser
177 * XXX: requires Expat 1.95.8 */
178 xerr = XML_StopParser(data->parser, XML_FALSE);
179 if (xerr != XML_STATUS_OK) {
180 PANIC("Error while stopping parser");
184 data->element_depth--;
187 data->depth--;
191 /* Locate element specified by element path in XML stream.
192 * TODO: Support other encodings than UTF-8
193 * @document is XML documuent as bitstream
194 * @length is size of @docuement in bytes. Zero length is forbidden.
195 * @path is special path (e.g. "|html|head|title",
196 * quallified element names are specified as
197 * NSURI '>' LOCALNAME, ommit NSURI and '>' separator if no namespace
198 * should be addressed (i.e. use only locale name)
199 * You can use PHYSXML_ELEMENT_SEPARATOR and PHYSXML_NS_SEPARATOR string
200 * macros.
201 * @start outputs start of the element location in @document (inclusive,
202 * counts from 0)
203 * @end outputs end of element (inclusive, counts from 0)
204 * @return 0 if element found */
205 _hidden isds_error find_element_boundary(void *document, size_t length,
206 char *path, size_t *start, size_t *end) {
208 XML_Parser parser;
209 enum XML_Status xerr;
210 struct expat_data user_data;
212 if (!document || !path || !start || !end || length <= 0)
213 return IE_INVAL;
215 isds_log(ILF_XML, ILL_DEBUG, _("Searching boundary of element: %s\n"),
216 path);
218 /* Parse XPath */
219 user_data.elements = path2elements(path);
220 if (!user_data.elements) return IE_NOMEM;
222 /* No element means whole document */
223 if (!user_data.elements[0]) {
224 free(user_data.elements);
225 *start = 0;
226 *end = length - 1;
227 return IE_SUCCESS;
230 /* Create parser*/
231 parser = XML_ParserCreateNS(NULL, NS_CHAR_SEPARATOR);
233 XML_SetStartElementHandler(parser, element_start);
234 XML_SetEndElementHandler(parser, element_end);
236 user_data.parser = parser;
237 user_data.found = 0;
238 user_data.start = start;
239 user_data.end = end;
240 user_data.depth = -1;
241 user_data.element_depth = -1;
242 XML_SetUserData(parser, &user_data);
244 /* Parse it */
245 xerr = XML_Parse(parser, (const char *) document, length, 1);
246 if (xerr != XML_STATUS_OK &&
247 !( (xerr == XML_STATUS_ERROR &&
248 XML_GetErrorCode(parser) == XML_ERROR_ABORTED))) {
249 free(user_data.elements);
250 isds_log(ILF_ISDS, ILL_CRIT, _("XML_Parse failed\n"));
251 return IE_ERROR;
253 free(user_data.elements);
255 XML_ParserFree(parser);
256 if (user_data.found) return IE_SUCCESS;
257 else return IE_NOEXIST;