-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-template.c
758 lines (650 loc) · 22.6 KB
/
parse-template.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
* This file is part of FAST Wireshark.
*
* FAST Wireshark is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FAST Wireshark 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
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with FAST Wireshark. If not, see
* <http://www.gnu.org/licenses/lgpl.txt>.
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib/gprintf.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include "wmem_aux.h"
#include "debug.h"
#include "template.h"
#include "parse-template.h"
#include "dictionaries.h"
#include "error_log.h"
/* Private (static) headers. */
static GNode* parse_template (xmlNodePtr cur);
static void set_field_attributes (xmlNodePtr node,
FieldType* tfield);
static gboolean ignore_xml_node (xmlNodePtr cur);
static void parser_walk_children (xmlNodePtr cur,
GNode* parent,
GNode* tnode_prev,
char * dictionary);
static GNode* new_parsed_field (xmlNodePtr xmlnode,
char * dictionary);
static gboolean field_type_match (xmlNodePtr node,
FieldTypeIdentifier type);
static gboolean parse_field_operator (xmlNodePtr xmlnode,
FieldType * tfield);
static gboolean parse_operator (xmlNodePtr xmlnode,
FieldType * tfield);
static gboolean prepend_length (xmlNodePtr xmlnode,
const FieldType* parent,
GNode* parent_node,
char * dictionary);
static gboolean parse_decimal (xmlNodePtr xmlnode,
FieldType * tfield,
GNode * tnode,
char * dictionary);
static gboolean parse_sequence (xmlNodePtr xmlnode,
FieldType* tfield,
GNode* tnode,
char * dictionary);
static gboolean operator_type_match (xmlNodePtr node,
FieldOperatorIdentifier type);
static gboolean check_valid_operator(xmlNodePtr xmlnode,
FieldType * tfield);
static gboolean is_integer(FieldTypeIdentifier type);
static gint templateID = -1; /* Stores tid while parsing */
GNode* parse_templates_xml(const char* filename)
{
xmlDocPtr doc; /* pointer to XML document */
xmlNodePtr cur; /* pointer to current node within document */
GNode* templates; /* Return value, all templates stored as children here. */
GNode* tnode_prev = 0;
/* attempt to parse xml file and get pointer to parsed document */
doc = xmlParseFile(filename);
if (doc == NULL) {
fast_log_static_error(1,
-1,
" Invalid XML syntax\nRun wireshark \
from console for more details.");
return 0;
}
/* Start at the root of the XML document. */
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return 0;
}
templates = wmem_node_new (wmem_epan_scope(), 0);
if (!templates) BAILOUT(0, "Error creating root of templates tree.");
/* Check if root is of type "templates". */
if (xmlStrcmp(cur->name, (xmlChar*) "templates")) {
fast_log_static_error(1,
cur->line,
" FAST syntax error: root node != templates");
xmlFreeDoc(doc);
return 0;
}
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if (!ignore_xml_node(cur)) {
if ((!xmlStrcasecmp(cur->name, (xmlChar*) "template"))) {
GNode* tnode = parse_template(cur);
/* Parse template, and check if parse failed. */
if (tnode) {
g_node_insert_after (templates, tnode_prev, tnode);
tnode_prev = tnode;
}
else {
DBG0("Parsing a template failed.");
xmlFreeDoc(doc);
return 0;
}
}
else {
char* extra_info = g_strdup_printf("Warning: Unkown templates child: %s\n", cur->name);
fast_log_static_error(1, cur->line, extra_info);
g_free(extra_info);
}
}
cur = cur->next;
}
xmlFreeDoc(doc);
return templates;
}
/*! \brief Parse a template section of the XML document
* \param cur Current position in the XML structure.
* \return Tree containing the internal template definition.
*/
GNode* parse_template (xmlNodePtr cur)
{
GNode* tnode;
FieldType* tfield;
tnode = create_field(FieldTypeUInt32, FieldOperatorCopy);
if (!tnode) BAILOUT(0, "Error allocating memory.");
tfield = (FieldType*) tnode->data;
/* Get name, id, etc. */
set_field_attributes(cur, tfield);
if(!tfield->dictionary){
tfield->dictionary = GLOBAL_DICTIONARY;
}
/* Store templateID */
tfield->tid = tfield->id;
templateID = tfield->tid;
cur = cur->xmlChildrenNode;
parser_walk_children (cur, tnode, 0, tfield->dictionary);
return tnode;
}
/*! \brief Build up the parse tree from XML data,
* starting at an arbitrary node.
*
* \param cur Current node in an xml tree.
* \param parent This parse tree level's parent.
* \param tnode_prev Previous node in parse tree at this level.
*/
void parser_walk_children (xmlNodePtr cur,
GNode* parent, GNode* tnode_prev, char* dictionary)
{
while (cur != NULL) {
if (!ignore_xml_node(cur)) {
GNode* tnode;
tnode = new_parsed_field (cur, dictionary);
if (tnode) {
g_node_insert_after (parent, tnode_prev, tnode);
tnode_prev = tnode;
}
}
cur = cur->next;
}
}
/*! \brief Create a new field in the parse tree based on an XML node.
* \param xmlnode The XML node which /should/ be a field.
* \return The new GNode* containing a FieldType.
* NULL if something went wrong.
*/
GNode* new_parsed_field (xmlNodePtr xmlnode, char* dictionary)
{
gboolean found = FALSE; /* Field type found. */
gboolean valid = FALSE; /* Field type valid. */
GNode* tnode;
FieldType* tfield;
tnode = create_field (FieldTypeInvalid, FieldOperatorNone);
if (!tnode) {
return 0;
}
tfield = (FieldType*) tnode->data;
set_field_attributes(xmlnode, tfield);
if(!tfield->dictionary){
tfield->dictionary = dictionary;
}
/* Try the integers. */
if (!found) {
found = TRUE;
if (field_type_match (xmlnode, FieldTypeUInt32)) {
tfield->type = FieldTypeUInt32;
}
else if (field_type_match (xmlnode, FieldTypeUInt64)) {
tfield->type = FieldTypeUInt64;
}
else if (field_type_match (xmlnode, FieldTypeInt32)) {
tfield->type = FieldTypeInt32;
}
else if (field_type_match (xmlnode, FieldTypeInt64)) {
tfield->type = FieldTypeInt64;
}
else {
found = FALSE;
}
if (found) {
/* check if field has valid operators */
valid = parse_field_operator(xmlnode, tfield);
}
}
/* Try decimal. */
if (!found && field_type_match (xmlnode, FieldTypeDecimal)) {
found = TRUE;
valid = parse_decimal(xmlnode, tfield, tnode, tfield->dictionary);
}
/* Try string */
if(!found && field_type_match (xmlnode, FieldTypeAsciiString)){
tfield->type = FieldTypeAsciiString;
found = TRUE;
valid = parse_field_operator(xmlnode, tfield);
} else if(!found && field_type_match (xmlnode, FieldTypeUnicodeString)){
tfield->type = FieldTypeUnicodeString;
found = TRUE;
valid = parse_field_operator(xmlnode, tfield);
if (valid) {
valid = prepend_length(xmlnode, tfield, tnode, tfield->dictionary);
}
}
/* Try bytevector */
if(!found && field_type_match (xmlnode, FieldTypeByteVector)){
tfield->type = FieldTypeByteVector;
found = TRUE;
valid = parse_field_operator(xmlnode, tfield);
if (valid) {
valid = prepend_length(xmlnode, tfield, tnode, tfield->dictionary);
}
}
/* Try group */
if(!found && field_type_match(xmlnode, FieldTypeGroup)){
tfield->type = FieldTypeGroup;
found = TRUE;
valid = TRUE;
parser_walk_children (xmlnode->xmlChildrenNode,
tnode,
0,
tfield->dictionary);
}
/* Try sequence */
if(!found && field_type_match(xmlnode, FieldTypeSequence)){
found = TRUE;
valid = parse_sequence (xmlnode, tfield, tnode, tfield->dictionary);
if (valid) {
valid = prepend_length(xmlnode, tfield, tnode,tfield->dictionary);
}
}
/*
* If field type is not recognized or if the field was not correctly parsed
*/
if (!found || !valid) {
/* TODO: Free parse tree. */
tnode = 0;
char* extra_info = 0;
if (!valid) {
extra_info = g_strdup_printf("FAST syntax error: Field %s could not be parsed.", xmlnode->name);
} else {
extra_info = g_strdup_printf("FAST syntax error: Unknown field type %s.", xmlnode->name);
}
fast_log_static_error(1, xmlnode->line, extra_info);
g_free(extra_info);
}
return tnode;
}
/*! \brief Add a length field as a child.
* \param xmlnode The XML node whose children we will search for a length.
* \param parent Parent field type;
* \param parent_node Tree node to build a length child.
* \return TRUE iff the a child was added.
*/
gboolean prepend_length (xmlNodePtr xmlnode,
const FieldType* parent,
GNode* parent_node,
char * dictionary)
{
GNode* tnode;
FieldType* ftype;
tnode = create_field (FieldTypeUInt32, FieldOperatorNone);
if (!tnode) {
BAILOUT(0, "Error creating exponent field.");
}
g_node_insert_before (parent_node, parent_node->children, tnode);
ftype = (FieldType*) tnode->data;
/* search through children of xmlnode */
for (xmlnode = xmlnode->xmlChildrenNode;
xmlnode;
xmlnode = xmlnode->next) {
/* if this child node's name is "length" */
if (0 == xmlStrcasecmp(xmlnode->name, (xmlChar*)"length")) {
set_field_attributes(xmlnode, ftype);
if (!parse_field_operator(xmlnode, ftype)) {
char* extra_info = g_strdup_printf("FAST syntax error: no length field for %s.", parent->name);
fast_log_static_error(1, xmlnode->line, extra_info);
g_free(extra_info);
BAILOUT(FALSE, "TemplateParser: Failed to get length.");
}
}
}
/* set the key */
if(!ftype->key)
ftype->key = (char*)wmem_strdup_printf(wmem_epan_scope(), "%s-length", parent->name);
/* set the dictionary */
if(!ftype->dictionary){
ftype->dictionary = dictionary;
}
ftype->tid = templateID;
ftype->mandatory = parent->mandatory;
return TRUE;
}
/*! \brief Fill in a decimal field in the parse tree.
* \param The XML node which /should/ be a decimal field.
* \param A pointer to the template within the parse tree.
* \return True if sucessfully parsed
*/
gboolean parse_decimal (xmlNodePtr xmlnode,
FieldType * tfield,
GNode * tnode,
char * dictionary)
{
GNode* exptNode;
GNode* mantNode;
FieldType * expt;
FieldType * mant;
tfield->type = FieldTypeDecimal;
/* Add exponent and mantissa. */
exptNode = create_field (FieldTypeInt32, FieldOperatorNone);
if (!exptNode) BAILOUT(0, "Error creating exponent field.");
g_node_insert_after (tnode, 0, exptNode);
expt = (FieldType*) exptNode->data;
mantNode = create_field (FieldTypeInt64, FieldOperatorNone);
if (!mantNode) BAILOUT(0, "Error creating mantissa field.");
g_node_insert_after (tnode, exptNode, mantNode);
mant = (FieldType*) mantNode->data;
/* Get exponent and mantissa operators and values (if given) */
xmlnode = xmlnode->xmlChildrenNode;
while (xmlnode != NULL) {
if (!ignore_xml_node(xmlnode)){
if(0 == xmlStrcasecmp(xmlnode->name, (xmlChar*)"exponent")){
set_field_attributes(xmlnode, expt);
if(!parse_field_operator(xmlnode, expt)) {
char* extra_info = g_strdup_printf("FAST syntax error: failed to parse operator for field %s.", tfield->name);
fast_log_static_error(1, xmlnode->line, extra_info);
g_free(extra_info);
BAILOUT(FALSE, "Failed to get exponent.");
}
} else if( 0 == xmlStrcasecmp(xmlnode->name, (xmlChar*)"mantissa")){
set_field_attributes(xmlnode, mant);
if(!parse_field_operator(xmlnode, mant)) {
char* extra_info = g_strdup_printf("FAST syntax error: failed to parse operator for field %s.", tfield->name);
fast_log_static_error(1, xmlnode->line, extra_info);
g_free(extra_info);
BAILOUT(FALSE, "Failed to get mantissa.");
}
} else {
/* assume this node is an operator */
parse_operator (xmlnode, tfield);
}
}
xmlnode = xmlnode->next;
}
/* set dictionaries */
if(!expt->dictionary){
expt->dictionary = dictionary;
}
if(!mant->dictionary){
mant->dictionary = dictionary;
}
expt->tid = templateID;
mant->tid = templateID;
expt->mandatory = tfield->mandatory;
/* set key */
if (tfield->key) {
if (!mant->key) {
mant->key = wmem_strdup_printf (wmem_epan_scope(), "%s-mantissa", tfield->key);
}
if (!expt->key) {
expt->key = wmem_strdup_printf (wmem_epan_scope(), "%s-exponent", tfield->key);
}
}
return TRUE;
}
/*! \brief Fill in a sequence field in the parse tree.
* \param xmlnode The XML node which /should/ be a sequence field.
* \param tfield Return value. An initialized field type.
* \param tnode A pointer to the template within the parse tree.
* \return True if sucessfully parsed
*/
gboolean parse_sequence (xmlNodePtr xmlnode,
FieldType* tfield,
GNode* tnode,
char * dictionary)
{
GNode* group_tnode;
tfield->type = FieldTypeSequence;
group_tnode = create_field (FieldTypeGroup, FieldOperatorNone);
if (!group_tnode) {
return FALSE;
}
g_node_insert_after (tnode, 0, group_tnode);
/* Descend the tree on the group. */
parser_walk_children (xmlnode->xmlChildrenNode, group_tnode, 0, dictionary);
return TRUE;
}
/*! \brief Fill in a field in the parse tree with operator info.
* \param xmlnode The XML node which /should/ be a field.
* \param tfield A pointer to the field within the parse tree.
* \return True if sucessfully parsed
*/
gboolean parse_field_operator(xmlNodePtr xmlnode, FieldType * tfield)
{
/* loop through field to find operators */
xmlnode = xmlnode->xmlChildrenNode;
while (xmlnode != NULL) {
if (!ignore_xml_node(xmlnode)){
return parse_operator(xmlnode, tfield);
}
xmlnode = xmlnode->next;
}
return TRUE;
}
/*! \brief Fill in a field in the parse tree with operator info.
* \param xmlnode The XML node which /should/ be a operator.
* \param tfield A pointer to the operator within the parse tree.
* \return True if sucessfully parsed
*/
gboolean parse_operator (xmlNodePtr xmlnode, FieldType * tfield){
xmlChar *prop;
if (xmlnode==NULL){
return FALSE;
}
/* set the operator type */
if (operator_type_match (xmlnode, FieldOperatorConstant)) {
tfield->op = FieldOperatorConstant;
} else if (operator_type_match (xmlnode, FieldOperatorDefault)) {
tfield->op = FieldOperatorDefault;
} else if (operator_type_match (xmlnode, FieldOperatorCopy)) {
tfield->op = FieldOperatorCopy;
} else if (operator_type_match (xmlnode, FieldOperatorIncrement)) {
tfield->op = FieldOperatorIncrement;
} else if (operator_type_match (xmlnode, FieldOperatorDelta)) {
tfield->op = FieldOperatorDelta;
} else if (operator_type_match (xmlnode, FieldOperatorTail)) {
tfield->op = FieldOperatorTail;
} else {
char* extra_info = g_strdup_printf(
"FAST syntax error: Invalid operator (%s) for field %s",
xmlnode->name,
tfield->name);
fast_log_static_error(1, xmlnode->line, extra_info);
g_free(extra_info);
return FALSE;
}
if(!check_valid_operator(xmlnode, tfield)){
return FALSE;
}
/* get value of operator if given */
prop = xmlGetProp(xmlnode, (xmlChar*)"value");
if (prop!=NULL) {
tfield->hasDefault = TRUE;
if(!string_to_field_value((char*)prop, tfield->type, &tfield->value)){
char* extra_info = g_strdup_printf("Unable to parse value(%s) for field %s",
prop,
tfield->name);
fast_log_static_error(3, xmlnode->line, extra_info);
g_free(extra_info);
}
xmlFree(prop);
} else if(tfield->op == FieldOperatorConstant){
fast_log_static_error(4,
xmlnode->line,
"No value specified with a constant operator.");
} else if(tfield->op == FieldOperatorDefault && tfield->mandatory){
fast_log_static_error(5,
xmlnode->line,
"No value specified with a default operator on a mandatory field.");
} else {
tfield->hasDefault = FALSE;
}
return TRUE;
}
/*! \brief Check if a field's operator is valid
*
* \param tfield Field to check
*/
static gboolean check_valid_operator(xmlNodePtr xmlnode, FieldType * tfield){
if(tfield->op == FieldOperatorIncrement && !is_integer(tfield->type)){
char* extra_info = g_strdup_printf(
"Field %s cannot have an operator of type %s\nIncrement can only be used on integers.",
tfield->name,
xmlnode->name);
fast_log_static_error(2, xmlnode->line, extra_info);
g_free(extra_info);
return FALSE;
}
if(tfield->op == FieldOperatorTail){
if(is_integer(tfield->type) || tfield->type == FieldTypeDecimal){
char* extra_info = g_strdup_printf(
"Field %s cannot have an operator of type %s\nTail can only be used on strings and bytevectors.",
tfield->name,
xmlnode->name);
fast_log_static_error(2, xmlnode->line, extra_info);
g_free(extra_info);
return FALSE;
}
}
return TRUE;
}
/*! \brief check to see if this field type is an integer type
* \return true iff field type is int32, int64, uint32, or uint64
*/
static gboolean is_integer(FieldTypeIdentifier type){
return type == FieldTypeInt32 ||
type == FieldTypeInt64 ||
type == FieldTypeUInt32 ||
type == FieldTypeUInt64;
}
/*! \brief Set standard attributes of a field.
*
* Specifically, set 'id', 'name', and 'presence'.
* \param xmlnode XML node to query for attributes.
* \param tfield Return value. Must already be allocated.
*/
void set_field_attributes (xmlNodePtr xmlnode, FieldType* tfield)
{
const xmlChar* str;
/* Name. */
str = xmlGetProp(xmlnode, (xmlChar*) "name");
if (str)
tfield->name = wmem_strdup (wmem_epan_scope(), (char*)str);
xmlFree((void*)str);
/* set tid to parent template id */
tfield->tid = templateID;
/* Identifier number. */
str = xmlGetProp(xmlnode, (xmlChar*) "id");
if (str) {
tfield->id = atoi((char*)str);
}
xmlFree((void*)str);
/* Key. */
str = xmlGetProp(xmlnode, (xmlChar*) "key");
if (str) {
tfield->key = wmem_strdup (wmem_epan_scope(), (char*)str);
} else if (tfield->name){
tfield->key = wmem_strdup (wmem_epan_scope(), tfield->name);
}
xmlFree((void*)str);
/* Presence. */
str = xmlGetProp(xmlnode, (xmlChar*) "presence");
if (str) {
if (0 == xmlStrcasecmp(str, (xmlChar*) "optional")) {
tfield->mandatory = FALSE;
}
else if (0 == xmlStrcasecmp(str, (xmlChar*) "mandatory")) {
tfield->mandatory = TRUE;
}
else {
char* extra_info = g_strdup_printf(
"FAST syntax error: Error, bad presence option (%s) for field %s",
(char*) str,
tfield->name);
fast_log_static_error(1, xmlnode->line, extra_info);
g_free(extra_info);
}
}
xmlFree((void*)str);
str = xmlGetProp(xmlnode, (xmlChar*) "dictionary");
if (str) {
tfield->dictionary = wmem_strdup(wmem_epan_scope(), (char*) str);
xmlFree((void*)str);
}
}
/*! \brief Check if this XML node should be ignored.
*
* Ignored nodes include comments and text between XML tags.
*
* \param xmlnode Current XML node.
* \return TRUE iff the node is something we ignore.
*/
gboolean ignore_xml_node (xmlNodePtr xmlnode)
{
return (0 == xmlStrcasecmp(xmlnode->name, (xmlChar*) "text") ||
0 == xmlStrcasecmp(xmlnode->name, (xmlChar*) "comment") ||
0 == xmlStrcasecmp(xmlnode->name, (xmlChar*) "typeRef") ||
0 == xmlStrcasecmp(xmlnode->name, (xmlChar*) "templateRef") ||
0 == xmlStrcasecmp(xmlnode->name, (xmlChar*) "length"));
}
/*! \brief Check if a string from XML.
* \param node Node in the XML tree. Its name is to be used in comparison.
* \param type Type whose name you want to check against.
* \return TRUE iff the node name matches.
*/
gboolean field_type_match (xmlNodePtr node, FieldTypeIdentifier type)
{
const xmlChar* str1 = node->name;
const char* str2 = field_typename (type);
if (type == FieldTypeAsciiString) {
if (0 == xmlStrcasecmp(str1, (xmlChar*) "string")) {
/* check if charset is ascii or unicode */
gboolean result = TRUE;
xmlChar * prop = xmlGetProp(node, (xmlChar*) "charset");
if(prop==NULL){
/* Assume ascii if not given */
return TRUE;
}
result = (0 == xmlStrcasecmp(prop, (xmlChar*) "ascii"));
xmlFree(prop);
return result;
}
return FALSE;
}
else if (type == FieldTypeUnicodeString) {
if (0 == xmlStrcasecmp(str1, (xmlChar*) "string")) {
/* check if charset is ascii or unicode */
gboolean result = FALSE;
xmlChar * prop = xmlGetProp(node, (xmlChar*)"charset");
if(prop==NULL){
/* Assume ascii if not given */
return FALSE;
}
result = (0 == xmlStrcasecmp(prop, (xmlChar*) "unicode"));
xmlFree(prop);
return result;
}
return FALSE;
}
else {
return (0 == xmlStrcasecmp(str1, (xmlChar*) str2));
}
}
/*! \brief Check if a string from XML matches the given Operator's type string
* \param node Node in the XML tree. Its name is to be used in comparison.
* \param type Type whose name you want to check against.
* \return TRUE iff the node name matches.
*/
gboolean operator_type_match (xmlNodePtr node, FieldOperatorIdentifier type)
{
const xmlChar* str1 = node->name;
const char* str2 = operator_typename(type);
return (0 == xmlStrcasecmp(str1, (xmlChar*) str2));
}