xml_parse.c 29.6 KB
Newer Older
1 2 3 4 5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

22 23 24
#include <string.h>
#include <unistd.h>

25 26
#define G_LOG_DOMAIN ("PARSER")

27 28
#include <gtk/gtk.h>

29 30 31
#include <libxml/parser.h>
#include <libxml/tree.h>

32
#include "logs.h"
33 34 35 36 37
#include "types.h"
#include "xml_parse.h"
#include "union_type.h"

#include "ui_interface.h"
38
#include "ui_main_screen.h"
Cedric Roux's avatar
Cedric Roux committed
39
#include "ui_notif_dlg.h"
winckel's avatar
winckel committed
40
#include "ui_filters.h"
41 42 43 44 45 46 47 48 49 50 51 52

#include "../libresolver/locate_root.h"
#include "../libresolver/resolvers.h"

extern int debug_parser;

#if (ENABLE_DISPLAY_PARSE_INFO != 0)
# define INDENT_START 30
#else
# define INDENT_START 0
#endif

53 54 55 56 57
void       *xml_raw_data;
uint32_t    xml_raw_data_size;

types_t    *xml_head;
types_t    *root;
58 59 60 61 62 63 64

static int xml_parse_doc(xmlDocPtr doc);

static int parse_attribute_name(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "name")) == NULL) {
        if (mandatory) {
65 66
            g_error("cannot retrieve name attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
67 68 69
            return -1;
        }
        else {
winckel's avatar
winckel committed
70 71
            g_debug("cannot retrieve name attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
72 73 74 75 76 77 78 79 80 81
            return 0;
        }
    }
    type->name = strdup ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_id(xmlNode *node, types_t *type) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "id")) == NULL) {
82 83
        g_error("cannot retrieve id attribute in node %s, %s:%ld",
                (char *)node->name, node->doc->URL, XML_GET_LINE(node));
84 85 86 87 88 89 90 91 92 93
        return -1;
    }
    type->id = atoi ((char *) &node_attribute->children->content[1]);
    return 0;
}

static int parse_attribute_size(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "size")) == NULL) {
        if (mandatory) {
94 95
            g_error("cannot retrieve size attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
96 97 98 99
            type->size = -1;
            return -1;
        }
        else {
winckel's avatar
winckel committed
100 101
            g_debug("cannot retrieve size attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
102 103 104 105 106 107 108 109 110 111 112 113
            type->size = -1;
            return 0;
        }
    }
    type->size = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_align(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "align")) == NULL) {
        if (mandatory) {
114 115
            g_error("cannot retrieve align attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
116 117 118 119
            type->align = -1;
            return -1;
        }
        else {
winckel's avatar
winckel committed
120 121
            g_debug("cannot retrieve align attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
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
            type->align = -1;
            return 0;
        }
    }
    type->align = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_line(xmlNode *node, types_t *type) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "line")) == NULL) {
        type->line = -1;
        return 0;
    }
    type->line = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_file(xmlNode *node, types_t *type) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "file")) == NULL) {
        type->file = NULL;
        return 0;
    }
    type->file = strdup ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_context(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "context")) == NULL) {
        if (mandatory) {
154 155
            g_error("cannot retrieve context attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
            type->context = -1;
            return -1;
        }
        else {
            type->context = -1;
            return 0;
        }
    }
    type->context = atoi ((char *) &node_attribute->children->content[1]);
    return 0;
}

static int parse_attribute_artificial(xmlNode *node, types_t *type) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "artificial")) == NULL) {
winckel's avatar
winckel committed
171 172
        g_debug("cannot retrieve artificial attribute in node %s, %s:%ld",
                (char *)node->name, node->doc->URL, XML_GET_LINE(node));
173 174 175 176 177 178 179 180 181 182
        type->artificial = -1;
        return 0;
    }
    type->artificial = atoi ((char *) &node_attribute->children->content[1]);
    return 0;
}

static int parse_attribute_init(xmlNode *node, types_t *type) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "init")) == NULL) {
183 184
        g_error("cannot retrieve init attribute in node %s, %s:%ld",
                (char *)node->name, node->doc->URL, XML_GET_LINE(node));
185 186 187 188 189 190 191 192 193 194 195
        type->init_value = -1;
        return 0;
    }
    type->init_value = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_members(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "members")) == NULL) {
        if (mandatory) {
196 197
            g_error("cannot retrieve members attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
198 199 200 201
            type->members = 0;
            return -1;
        }
        else {
winckel's avatar
winckel committed
202 203
            g_debug("cannot retrieve members attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
204 205 206 207 208 209 210 211 212 213 214 215
            type->members = 0;
            return 0;
        }
    }
    type->members = strdup ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_mangled(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "mangled")) == NULL) {
        if (mandatory) {
216 217
            g_error("cannot retrieve mangled attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
218 219 220 221
            type->mangled = 0;
            return -1;
        }
        else {
winckel's avatar
winckel committed
222 223
            g_debug("cannot retrieve mangled attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
224 225 226 227 228 229 230 231 232 233 234 235
            type->mangled = 0;
            return 0;
        }
    }
    type->mangled = strdup ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_demangled(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "mangled")) == NULL) {
        if (mandatory) {
236 237
            g_error("cannot retrieve demangled attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
238 239 240 241
            type->demangled = 0;
            return -1;
        }
        else {
winckel's avatar
winckel committed
242 243
            g_debug("cannot retrieve demangled attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
244 245 246 247 248 249 250 251 252 253 254 255
            type->demangled = 0;
            return 0;
        }
    }
    type->demangled = strdup ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_offset(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "offset")) == NULL) {
        if (mandatory) {
256 257
            g_error("cannot retrieve offset attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
258 259 260 261
            type->offset = -1;
            return -1;
        }
        else {
winckel's avatar
winckel committed
262 263
            g_debug("cannot retrieve offset attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
264 265 266 267 268 269 270 271 272 273 274 275
            type->offset = -1;
            return 0;
        }
    }
    type->offset = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_bits(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "bits")) == NULL) {
        if (mandatory) {
276 277
            g_error("cannot retrieve bits attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
278 279 280 281
            type->bits = -1;
            return -1;
        }
        else {
winckel's avatar
winckel committed
282 283
            g_debug("cannot retrieve bits attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
284 285 286 287 288 289 290 291 292 293 294 295
            type->bits = -1;
            return 0;
        }
    }
    type->bits = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_type(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "type")) == NULL) {
        if (mandatory) {
296 297
            g_error("cannot retrieve type attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
298 299 300 301
            type->type_xml = 0;
            return -1;
        }
        else {
winckel's avatar
winckel committed
302 303
            g_debug("cannot retrieve type attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
304 305 306 307 308 309 310 311 312 313 314 315
            type->type_xml = 0;
            return 0;
        }
    }
    type->type_xml = atoi ((char *) &node_attribute->children->content[1]);
    return 0;
}

static int parse_attribute_min(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "min")) == NULL) {
        if (mandatory) {
316 317
            g_error("cannot retrieve min attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
318 319 320 321
            type->min = 0;
            return -1;
        }
        else {
winckel's avatar
winckel committed
322 323
            g_debug("cannot retrieve min attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
324 325 326 327 328 329 330 331 332 333 334 335
            type->min = 0;
            return 0;
        }
    }
    type->min = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_max(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "max")) == NULL) {
        if (mandatory) {
336 337
            g_error("cannot retrieve max attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
338 339 340 341
            type->max = 0;
            return -1;
        }
        else {
winckel's avatar
winckel committed
342 343
            g_debug("cannot retrieve max attribute in node %s, %s:%ld",
                    (char *)node->name, node->doc->URL, XML_GET_LINE(node));
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
            type->max = 0;
            return 0;
        }
    }
    type->max = atoi ((char *) &node_attribute->children->content[1]);
    return 0;
}

static int parse_enum_values(xmlNode *node, types_t *parent) {
    types_t *new;

    if (node == NULL || parent == NULL)
        return -1;

    new = type_new (TYPE_ENUMERATION_VALUE);

    CHECK_FCT(parse_attribute_name(node, new, 1));
    CHECK_FCT(parse_attribute_init(node, new));

    CHECK_FCT(types_insert_tail(&parent->child, new));

    return 0;
}

static int parse_enumeration(xmlNode *node, types_t **head) {
    types_t *new;
    xmlNode *enum_value_node;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_ENUMERATION);

    CHECK_FCT(parse_attribute_name(node, new, 1));
    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_size(node, new, 1));
    CHECK_FCT(parse_attribute_align(node, new, 1));
    CHECK_FCT(parse_attribute_context(node, new, 0));
    CHECK_FCT(parse_attribute_line(node, new));
    CHECK_FCT(parse_attribute_file(node, new));
    CHECK_FCT(parse_attribute_artificial(node, new));

    CHECK_FCT(types_insert_tail(head, new));

    /* Parse enum values */
    for (enum_value_node = node->children; enum_value_node; enum_value_node = enum_value_node->next) {
        if (strcmp ((char *) enum_value_node->name, "EnumValue") == 0)
            CHECK_FCT(parse_enum_values(enum_value_node, new));
    }

    return 0;
}

static int parse_union(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_UNION);

    CHECK_FCT(parse_attribute_name(node, new, 0));
    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_size(node, new, 1));
    CHECK_FCT(parse_attribute_align(node, new, 1));
    CHECK_FCT(parse_attribute_context(node, new, 0));
    CHECK_FCT(parse_attribute_members(node, new, 0));
    CHECK_FCT(parse_attribute_line(node, new));
    CHECK_FCT(parse_attribute_file(node, new));
    CHECK_FCT(parse_attribute_artificial(node, new));
    CHECK_FCT(parse_attribute_mangled(node, new, 0));
    CHECK_FCT(parse_attribute_demangled(node, new, 0));

    if (new->name)
        if (strcmp (new->name, "msg_s") == 0)
            new->type_dissect_from_buffer = union_msg_dissect_from_buffer;

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_fundamental(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_FUNDAMENTAL);

    CHECK_FCT(parse_attribute_name(node, new, 1));
    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_size(node, new, 0));
    CHECK_FCT(parse_attribute_align(node, new, 0));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_struct(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_STRUCT);

    CHECK_FCT(parse_attribute_name(node, new, 0));
    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_size(node, new, 0));
    CHECK_FCT(parse_attribute_align(node, new, 0));
    CHECK_FCT(parse_attribute_file(node, new));
    CHECK_FCT(parse_attribute_line(node, new));
    CHECK_FCT(parse_attribute_context(node, new, 0));
    CHECK_FCT(parse_attribute_artificial(node, new));
    CHECK_FCT(parse_attribute_members(node, new, 0));
    CHECK_FCT(parse_attribute_mangled(node, new, 0));
    CHECK_FCT(parse_attribute_demangled(node, new, 0));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_typedef(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_TYPEDEF);

    CHECK_FCT(parse_attribute_name(node, new, 1));
    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_file(node, new));
    CHECK_FCT(parse_attribute_line(node, new));
    CHECK_FCT(parse_attribute_type(node, new, 1));
    CHECK_FCT(parse_attribute_size(node, new, 0));
    CHECK_FCT(parse_attribute_align(node, new, 0));
    CHECK_FCT(parse_attribute_context(node, new, 0));
    CHECK_FCT(parse_attribute_artificial(node, new));
    CHECK_FCT(parse_attribute_members(node, new, 0));
    CHECK_FCT(parse_attribute_mangled(node, new, 0));
    CHECK_FCT(parse_attribute_demangled(node, new, 0));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_field(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_FIELD);

    CHECK_FCT(parse_attribute_name(node, new, 1));
    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_type(node, new, 1));
    CHECK_FCT(parse_attribute_size(node, new, 0));
    CHECK_FCT(parse_attribute_bits(node, new, 0));
    CHECK_FCT(parse_attribute_offset(node, new, 0));
    CHECK_FCT(parse_attribute_align(node, new, 0));
    CHECK_FCT(parse_attribute_context(node, new, 0));
    CHECK_FCT(parse_attribute_artificial(node, new));
    CHECK_FCT(parse_attribute_members(node, new, 0));
    CHECK_FCT(parse_attribute_mangled(node, new, 0));
    CHECK_FCT(parse_attribute_demangled(node, new, 0));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_file(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_FILE);

    CHECK_FCT(parse_attribute_name(node, new, 1));
    CHECK_FCT(parse_attribute_id(node, new));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_reference_type(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_REFERENCE);

    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_type(node, new, 1));
    CHECK_FCT(parse_attribute_size(node, new, 1));
    CHECK_FCT(parse_attribute_align(node, new, 1));
    CHECK_FCT(parse_attribute_context(node, new, 0));
    CHECK_FCT(parse_attribute_offset(node, new, 0));
    CHECK_FCT(parse_attribute_file(node, new));
    CHECK_FCT(parse_attribute_line(node, new));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_array_type(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_ARRAY);

    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_type(node, new, 1));
    CHECK_FCT(parse_attribute_size(node, new, 0));
    CHECK_FCT(parse_attribute_align(node, new, 1));
    CHECK_FCT(parse_attribute_min(node, new, 1));
    CHECK_FCT(parse_attribute_max(node, new, 1));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_pointer_type(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_POINTER);

    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_type(node, new, 1));
    CHECK_FCT(parse_attribute_size(node, new, 1));
    CHECK_FCT(parse_attribute_align(node, new, 1));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
static int parse_function_type(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_FUNCTION);

    CHECK_FCT(parse_attribute_id(node, new));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
/**
 * print_element_names:
 * @a_node: the initial xml node to consider.
 *
 * Prints the names of the all the xml elements
 * that are siblings or children of a given xml node.
 */
static int parse_elements(xmlNode * a_node, types_t **head) {
    xmlNode *cur_node = NULL;
    xmlNode *child_node = NULL;

    for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
        for (child_node = cur_node->children; child_node; child_node = child_node->next) {
            if (child_node->type == XML_ELEMENT_NODE) {
                if (strcmp ((char *) child_node->name, "Enumeration") == 0) {
Cedric Roux's avatar
Cedric Roux committed
627
                    CHECK_FCT_DO(parse_enumeration(child_node, head), return RC_FAIL);
628 629 630
                }
                else
                    if (strcmp ((char *) child_node->name, "FundamentalType") == 0) {
Cedric Roux's avatar
Cedric Roux committed
631
                        CHECK_FCT_DO(parse_fundamental(child_node, head), return RC_FAIL);
632 633 634
                    }
                    else
                        if (strcmp ((char *) child_node->name, "Struct") == 0) {
Cedric Roux's avatar
Cedric Roux committed
635
                            CHECK_FCT_DO(parse_struct(child_node, head), return RC_FAIL);
636 637 638
                        }
                        else
                            if (strcmp ((char *) child_node->name, "Union") == 0) {
Cedric Roux's avatar
Cedric Roux committed
639
                                CHECK_FCT_DO(parse_union(child_node, head), return RC_FAIL);
640 641 642
                            }
                            else
                                if (strcmp ((char *) child_node->name, "Typedef") == 0) {
Cedric Roux's avatar
Cedric Roux committed
643
                                    CHECK_FCT_DO(parse_typedef(child_node, head), return RC_FAIL);
644 645 646
                                }
                                else
                                    if (strcmp ((char *) child_node->name, "File") == 0) {
Cedric Roux's avatar
Cedric Roux committed
647
                                        CHECK_FCT_DO(parse_file(child_node, head), return RC_FAIL);
648 649 650
                                    }
                                    else
                                        if (strcmp ((char *) child_node->name, "Field") == 0) {
Cedric Roux's avatar
Cedric Roux committed
651
                                            CHECK_FCT_DO(parse_field(child_node, head), return RC_FAIL);
652 653 654
                                        }
                                        else
                                            if (strcmp ((char *) child_node->name, "ReferenceType") == 0) {
Cedric Roux's avatar
Cedric Roux committed
655
                                                CHECK_FCT_DO(parse_reference_type(child_node, head), return RC_FAIL);
656 657 658
                                            }
                                            else
                                                if (strcmp ((char *) child_node->name, "ArrayType") == 0) {
659
                                                    CHECK_FCT_DO(parse_array_type(child_node, head), return RC_FAIL);
660 661 662
                                                }
                                                else
                                                    if (strcmp ((char *) child_node->name, "PointerType") == 0) {
663
                                                        CHECK_FCT_DO(parse_pointer_type(child_node, head), return RC_FAIL);
664
                                                    }
665 666 667 668
                                                    else
                                                        if (strcmp ((char *) child_node->name, "FunctionType") == 0) {
                                                            CHECK_FCT_DO(parse_function_type(child_node, head), return RC_FAIL);
                                                        }
669 670 671 672 673 674 675
            }
        }
    }

    return RC_OK;
}

676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
static int free_elements(types_t *parent, int indent) {
    types_t *cur_node = parent;
    types_t *child_node;

    g_debug("%*s%p %s", indent, "", cur_node, cur_node->name != NULL ? cur_node->name : "");

    for (; cur_node != NULL; cur_node = cur_node->next) {
        if (cur_node->child != NULL) {
            child_node = cur_node->child;
            cur_node->child = NULL; /* Clear the child pointer to avoid re-processing it, we are handling a graph with loops */
            CHECK_FCT_DO(free_elements((child_node), indent + 2), return RC_FAIL);
        }
    }
    free (cur_node);

    return RC_OK;
}

694
int xml_parse_buffer(char *xml_buffer, const int size) {
695 696 697
    xmlDocPtr doc; /* the resulting document tree */

    if (xml_buffer == NULL) {
698
        return RC_NULL_POINTER;
699 700
    }

701 702 703 704 705 706 707 708
    if (xml_raw_data != NULL)
    {
        /* Free previous raw data */
        free (xml_raw_data);
    }
    xml_raw_data = xml_buffer;
    xml_raw_data_size = size;

709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
    if (xml_head != NULL)
    {
        /* Free previous definitions */
        free_elements(xml_head, 0);

        g_info("xml_parse_buffer freed previous definitions");
    }

    xml_head = NULL;
    root = NULL;
    messages_id_enum = NULL;
    lte_time_type = NULL;
    lte_time_frame_type = NULL;
    lte_time_slot_type = NULL;
    origin_task_id_type = NULL;
    destination_task_id_type = NULL;
    instance_type = NULL;
    message_header_type = NULL;
    message_type = NULL;
    message_size_type = NULL;

    g_info("Parsing XML definition from buffer ...");
731 732 733 734

    doc = xmlReadMemory(xml_buffer, size, NULL, NULL, 0);

    if (doc == NULL) {
735
        g_warning("Failed to parse XML buffer: %s", xml_buffer);
winckel's avatar
winckel committed
736
        ui_notification_dialog(GTK_MESSAGE_ERROR, FALSE, "parse messages format definition", "Fail to parse XML buffer");
737 738 739 740 741 742
        return RC_FAIL;
    }

    return xml_parse_doc(doc);
}

winckel's avatar
winckel committed
743 744 745 746 747 748 749 750 751 752
static int update_filters() {
    types_t *types;

    ui_init_filters(FALSE, TRUE);

    types = messages_id_enum;
    if (types != NULL)
    {
        types = types->child;

753 754 755 756
        while (types != NULL)
        {
            if (strcmp (types->name, "MESSAGES_ID_MAX") != 0)
            {
757
                ui_filters_add (FILTER_MESSAGES, types->init_value, types->name, ENTRY_ENABLED_UNDEFINED, NULL, NULL);
758
            }
winckel's avatar
winckel committed
759 760 761 762 763 764 765 766 767 768
            types = types->next;
        }
    }

    types = origin_task_id_type;
    if (types != NULL)
    {
        types = types->child->child;

        while (types != NULL) {
769 770
            if ((strcmp (types->name, "TASK_FIRST") != 0) && (strcmp (types->name, "TASK_MAX") != 0))
            {
771
            	ui_filters_add(FILTER_ORIGIN_TASKS, types->init_value, types->name, ENTRY_ENABLED_UNDEFINED, NULL, NULL);
772
            }
winckel's avatar
winckel committed
773 774 775 776 777 778 779 780 781 782
            types = types->next;
        }
    }

    types = destination_task_id_type;
    if (types != NULL)
    {
        types = types->child->child;

        while (types != NULL) {
783 784
            if ((strcmp (types->name, "TASK_FIRST") != 0) && (strcmp (types->name, "TASK_MAX") != 0))
            {
785
            	ui_filters_add(FILTER_DESTINATION_TASKS, types->init_value, types->name, ENTRY_ENABLED_UNDEFINED, NULL, NULL);
786
            }
winckel's avatar
winckel committed
787 788 789 790 791 792 793
            types = types->next;
        }
    }

    return RC_OK;
}

794 795 796
static int xml_parse_doc(xmlDocPtr doc) {
    xmlNode *root_element = NULL;
    int ret = 0;
797
    FILE *dissect_file = NULL;
798

799 800 801
    if (ui_main_data.dissect_file_name != NULL) {
        dissect_file = fopen (ui_main_data.dissect_file_name, "w");
    }
802 803

    /* Get the root element node */
Cedric Roux's avatar
Cedric Roux committed
804
    root_element = xmlDocGetRootElement(doc);
805

806
    ret = parse_elements(root_element, &xml_head);
807 808

    /* Free the document */
Cedric Roux's avatar
Cedric Roux committed
809
    xmlFreeDoc(doc);
810 811

    if (ret == RC_OK) {
812 813 814 815 816 817 818 819 820
        resolve_typedefs (&xml_head);
        resolve_pointer_type (&xml_head);
        resolve_field (&xml_head);
        resolve_array (&xml_head);
        resolve_reference (&xml_head);
        resolve_struct (&xml_head);
        resolve_file (&xml_head);
        resolve_union (&xml_head);
        resolve_function (&xml_head);
821

Cedric Roux's avatar
Cedric Roux committed
822
        /* Locate the root element which corresponds to the MessageDef struct */
823
        CHECK_FCT(locate_root("MessageDef", xml_head, &root));
824 825

        /* Locate the LTE time fields */
826
        if (locate_type("lte_time", xml_head, &lte_time_type) == RC_OK)
827 828 829 830 831 832
        {
            CHECK_FCT(locate_type("frame", lte_time_type->child->child, &lte_time_frame_type));
            CHECK_FCT(locate_type("slot", lte_time_type->child->child, &lte_time_slot_type));
        }

        /* Locate the message id field */
833
        CHECK_FCT(locate_type("MessagesIds", xml_head, &messages_id_enum));
834 835

        /* Locate the header part of a message */
836
        CHECK_FCT(locate_type("ittiMsgHeader", xml_head, &message_header_type));
837
        /* Locate the main message part */
838
        CHECK_FCT(locate_type("ittiMsg", xml_head, &message_type));
839

840
        /* Locate the origin task id field */
841
        CHECK_FCT(locate_type_children("originTaskId", message_header_type->child->child, &origin_task_id_type));
842
        /* Locate the destination task id field */
843
        CHECK_FCT(locate_type_children("destinationTaskId", message_header_type->child->child, &destination_task_id_type));
844
        /* Locate the instance field */
845
        CHECK_FCT(locate_type_children("instance", message_header_type->child->child, &instance_type));
846
        /* Locate the message size field */
847
        CHECK_FCT(locate_type_children("ittiMsgSize", message_header_type->child->child, &message_size_type));
848

849
        // root->type_hr_display(root, 0);
850

winckel's avatar
winckel committed
851
        update_filters();
852
        if (dissect_file != NULL) {
853
            g_debug("generating dissected types file \"%s\" ...", ui_main_data.dissect_file_name);
854 855 856 857
            root->type_file_print (root, 0, dissect_file);
        }
    }

858 859 860
    if (dissect_file != NULL) {
        fclose (dissect_file);
    }
861 862 863

    g_message("Parsed XML definition");

864 865 866
    return ret;
}

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
int dissect_signal_header(buffer_t *buffer, ui_set_signal_text_cb_t ui_set_signal_text_cb,
                          gpointer cb_user_data)
{
    if (message_header_type == NULL) {
        g_error("No messages format definition provided");
        return RC_FAIL;
    }

    if (buffer == NULL) {
        g_error("Failed buffer is NULL");
        return RC_FAIL;
    }

    message_header_type->type_dissect_from_buffer(
        message_header_type, ui_set_signal_text_cb, cb_user_data,
882
        buffer, 0, 0, INDENT_START, TRUE);
883 884 885 886

    return RC_OK;
}

887
int dissect_signal(buffer_t *buffer, ui_set_signal_text_cb_t ui_set_signal_text_cb,
888
                   gpointer cb_user_data)
889
{
890
    if (message_type == NULL) {
891
        g_error("No messages format definition provided");
892 893 894 895
        return RC_FAIL;
    }

    if (buffer == NULL) {
896
        g_error("Failed buffer is NULL");
897 898 899
        return RC_FAIL;
    }

900 901
    message_type->type_dissect_from_buffer(message_type, ui_set_signal_text_cb, cb_user_data,
                                           buffer, 0, 0, INDENT_START, TRUE);
902 903 904

    return RC_OK;
}