-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdd.c
378 lines (315 loc) · 9.24 KB
/
xdd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* xdd.c
* Routines for reading in Ethernet POWERLINK XDD profiles
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "xdd.h"
#include "packet-epl.h"
#include <glib.h>
#include <string.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <errno.h>
#include "wmem_iarray.h"
static guint16
epl_strtou16(const char * str, char ** endptr, int base)
{
unsigned long val = strtoul(str, endptr, base);
if (val > G_MAXUINT16)
{
val = G_MAXUINT16;
errno = ERANGE;
}
return val;
}
void
xdd_init(void)
{
xmlInitParser();
LIBXML_TEST_VERSION;
}
void
xdd_free(void)
{
xmlCleanupParser();
}
#if !defined(LIBXML_XPATH_ENABLED) \
|| !defined(LIBXML_SAX1_ENABLED) \
|| !defined(LIBXML_TREE_ENABLED)
#error "packet-epl.c needs XPATH, SAX1 and TREE support compiled in!"
#endif
typedef int xpath_handler(xmlNodeSetPtr, void*);
static xpath_handler populate_objectList, populate_dataTypeList, populate_profileName;
static struct xpath_namespace {
const xmlChar *prefix, *href;
} namespaces[] = {
{ BAD_CAST "x", BAD_CAST "http://www.ethernet-powerlink.org" },
{ BAD_CAST "xsi", BAD_CAST "http://www.w3.org/2001/XMLSchema-instance" },
{ NULL, NULL }
};
static struct xpath {
const xmlChar *expr;
xpath_handler *handler;
} xpaths[] = {
{
BAD_CAST "//x:ISO15745Profile[x:ProfileHeader/x:ProfileIdentification='Powerlink_Communication_Profile']/x:ProfileHeader/x:ProfileName",
populate_profileName
},
{
BAD_CAST "//x:ProfileBody[@xsi:type='ProfileBody_CommunicationNetwork_Powerlink']/x:ApplicationLayers/x:DataTypeList/x:defType",
populate_dataTypeList
},
{
BAD_CAST "//x:ProfileBody[@xsi:type='ProfileBody_CommunicationNetwork_Powerlink']/x:ApplicationLayers/x:ObjectList/x:Object",
populate_objectList
},
{ NULL, NULL }
};
struct profile *
xdd_load(struct profile *profile, const char *xml_file)
{
xmlXPathContextPtr xpathCtx = NULL;
xmlDoc *doc = NULL;
struct xpath_namespace *ns = NULL;
struct xpath *xpath = NULL;
GHashTable *typemap = NULL;
/* Load XML document */
doc = xmlParseFile(xml_file);
if (!doc)
{
g_warning("Error: unable to parse file \"%s\"\n", xml_file);
profile = NULL;
goto cleanup;
}
/* Create xpath evaluation context */
xpathCtx = xmlXPathNewContext(doc);
if(!xpathCtx)
{
g_warning("Error: unable to create new XPath context\n");
profile = NULL;
goto cleanup;
}
/* Register namespaces from list */
for (ns = namespaces; ns->href; ns++)
{
if(xmlXPathRegisterNs(xpathCtx, ns->prefix, ns->href) != 0)
{
g_warning("Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", ns->prefix, ns->href);
profile = NULL;
goto cleanup;
}
}
profile->path = wmem_strdup(profile->scope, xml_file);
/* mapping type ids to &hf_s */
profile->data = typemap = (GHashTable*)g_hash_table_new_full(epl_g_int16_hash, epl_g_int16_equal, NULL, g_free);
/* Evaluate xpath expressions */
for (xpath = xpaths; xpath->expr; xpath++)
{
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(xpath->expr, xpathCtx);
if (!xpathObj || !xpathObj->nodesetval)
{
g_warning("Error: unable to evaluate xpath expression \"%s\"\n", xpath->expr);
xmlXPathFreeObject(xpathObj);
profile = NULL;
goto cleanup;
}
/* run handler */
if (xpath->handler && xpathObj->nodesetval->nodeNr)
xpath->handler(xpathObj->nodesetval, profile);
xmlXPathFreeObject(xpathObj);
}
/* We create ObjectMappings while reading the XML, this is makes it likely,
* that we won't be able to reference a mapped object in the ObjectMapping
* as we didn't reach its XML tag yet. Therefore, after reading the XDD
* completely, we update mappings in the profile
*/
profile_object_mappings_update(profile);
cleanup:
if (typemap)
g_hash_table_destroy(typemap);
if (xpathCtx)
xmlXPathFreeContext(xpathCtx);
if (doc)
xmlFreeDoc(doc);
return profile;
}
void
xdd_unload()
{
}
static int
populate_profileName(xmlNodeSetPtr nodes, void *_profile)
{
struct profile *profile = (struct profile*)_profile;
if (nodes->nodeNr == 1
&& nodes->nodeTab[0]->type == XML_ELEMENT_NODE
&& nodes->nodeTab[0]->children)
{
profile->name = wmem_strdup(profile->scope, (char*)nodes->nodeTab[0]->children->content);
return 0;
}
return -1;
}
struct dataType {
guint16 id;
const struct epl_datatype *ptr;
};
static int
populate_dataTypeList(xmlNodeSetPtr nodes, void *_profile)
{
xmlNodePtr cur;
int i;
struct profile *profile = (struct profile*)_profile;
for(i = 0; i < nodes->nodeNr; ++i)
{
xmlAttrPtr attr;
if(!nodes->nodeTab[i] || nodes->nodeTab[i]->type != XML_ELEMENT_NODE)
return -1;
cur = nodes->nodeTab[i];
for(attr = cur->properties; attr; attr = attr->next)
{
char *endptr;
const char *key = (const char*)attr->name;
const char *val = (const char*)attr->children->content;
if (g_str_equal("dataType", key))
{
xmlNode *subnode;
guint16 idx = epl_strtou16(val, &endptr, 16);
if (endptr == val) continue;
for (subnode = cur->children; subnode; subnode = subnode->next)
{
if (subnode->type == XML_ELEMENT_NODE)
{
struct dataType *type;
const struct epl_datatype *ptr = epl_type_to_hf((char*)subnode->name);
if (!ptr)
{
EPL_INFO("Skipping unknown type '%s'\n", subnode->name);
continue;
}
type = g_new(struct dataType, 1);
type->id = idx;
type->ptr = ptr;
g_hash_table_insert((GHashTable*)profile->data, &type->id, type);
continue;
}
}
}
}
}
return 0;
}
static gboolean
parse_obj_tag(xmlNode *cur, struct od_entry *out, struct profile *profile) {
xmlAttrPtr attr;
const char *defaultValue = NULL, *actualValue = NULL, *value;
char *endptr;
for(attr = cur->properties; attr; attr = attr->next)
{
const char *key = (char*)attr->name,
*val = (char*)attr->children->content;
if (g_str_equal("index", key))
{
out->idx = epl_strtou16(val, &endptr, 16);
if (val == endptr) return FALSE;
} else if (g_str_equal("subIndex", key)) {
out->idx = epl_strtou16(val, &endptr, 16);
if (val == endptr) return FALSE;
} else if (g_str_equal("name", key)) {
g_strlcpy(out->name, val, sizeof out->name);
} else if (g_str_equal("objectType", key)) {
out->type_class = epl_strtou16(val, &endptr, 16);
} else if (g_str_equal("dataType", key)) {
guint16 id = epl_strtou16(val, &endptr, 16);
if (endptr != val)
{
struct dataType *type = (struct dataType*)g_hash_table_lookup((GHashTable*)profile->data, &id);
if (type) out->type = type->ptr;
}
} else if (g_str_equal("defaultValue", key)) {
defaultValue = val;
} else if (g_str_equal("actualValue", key)) {
actualValue = val;
}
/*else if (g_str_equal("PDOmapping", key)) {
obj.PDOmapping = get_index(ObjectPDOmapping_tostr, val);
assert(obj.PDOmapping >= 0);
}*/
}
value = actualValue ? actualValue
: defaultValue ? defaultValue
: NULL;
out->value = value ? g_ascii_strtoull(value, &endptr, 0) : 0;
return TRUE;
}
static int
populate_objectList(xmlNodeSetPtr nodes, void *_profile)
{
int i;
struct profile *profile = (struct profile*)_profile;
for(i = 0; i < nodes->nodeNr; ++i)
{
xmlNodePtr cur = nodes->nodeTab[i];
struct od_entry tmpobj = {0};
if (!nodes->nodeTab[i] || nodes->nodeTab[i]->type != XML_ELEMENT_NODE)
continue;
parse_obj_tag(cur, &tmpobj, profile);
if (tmpobj.idx)
{
struct object *obj = profile_object_add(profile, tmpobj.idx);
obj->info = tmpobj;
if (tmpobj.type_class == 8 || tmpobj.type_class == 9)
{
xmlNode *subcur;
struct subobject subobj = {0};
obj->subindices = epl_wmem_iarray_new(profile->scope, sizeof (struct subobject), subobject_equal);
for (subcur = cur->children; subcur; subcur = subcur->next)
{
if (subcur->type != XML_ELEMENT_NODE)
continue;
if (parse_obj_tag(subcur, &subobj.info, profile))
{
epl_wmem_iarray_insert(obj->subindices,
subobj.info.idx, &subobj.range);
}
if (subobj.info.value && profile_object_mapping_add(profile, obj->info.idx, subobj.info.idx, subobj.info.value))
{
EPL_INFO("Loaded mapping from XDC %s:%s", obj->info.name, subobj.info.name);
}
}
epl_wmem_iarray_sort(obj->subindices);
}
}
}
return 0;
}
/*
* Editor modelines - http://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 8
* tab-width: 8
* indent-tabs-mode: t
* End:
*
* vi: set shiftwidth=8 tabstop=8 noexpandtab:
* :indentSize=8:tabSize=8:noTabs=false:
*/