123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- /*
- * Copyright (C) 2005 iptelorg GmbH
- *
- * This file is part of ser, a free SIP server.
- *
- * ser 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
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- * [email protected]
- *
- * ser 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 <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <xcap/resource_lists_parser.h>
- #include <xcap/xml_utils.h>
- #include <cds/logger.h>
- #include <libxml/parser.h>
- #include <libxml/tree.h>
- #include <cds/sstr.h>
- static char rl_namespace[] = "urn:ietf:params:xml:ns:resource-lists";
- static int read_entry_ref(xmlNode *entry_node, entry_ref_t **dst)
- {
- xmlAttr *a;
- const char *a_val;
-
- /* allocate memory and prepare empty node */
- if (!dst) return -1;
- *dst = (entry_ref_t*)cds_malloc(sizeof(entry_ref_t));
- if (!(*dst)) return -2;
- memset(*dst, 0, sizeof(entry_ref_t));
- /* get attributes */
- a = find_attr(entry_node->properties, "ref");
- if (a) {
- a_val = get_attr_value(a);
- if (a_val) (*dst)->ref = zt_strdup(a_val);
- }
- return 0;
- }
- static int read_name(xmlNode *name_node, display_name_t **dst)
- {
- xmlAttr *a;
- const char *a_val;
-
- /* allocate memory and prepare empty node */
- if (!dst) return -1;
- *dst = (display_name_t*)cds_malloc(sizeof(display_name_t));
- if (!(*dst)) return -2;
- memset(*dst, 0, sizeof(display_name_t));
- /* get attributes */
- a = find_attr(name_node->properties, "lang");
- if (a) {
- a_val = get_attr_value(a);
- if (a_val) (*dst)->lang = zt_strdup(a_val);
- }
- a_val = get_node_value(name_node);
- if (a_val) (*dst)->name = zt_strdup(a_val);
- return 0;
- }
- static int read_names(xmlNode *entry_node, display_name_t **dst)
- {
- xmlNode *n;
- display_name_t *name, *last;
- int res = 0;
-
- last = NULL;
- *dst = NULL;
- n = entry_node->children;
- while (n) {
- if (n->type == XML_ELEMENT_NODE) {
- if (cmp_node(n, "display-name", rl_namespace) >= 0) {
- res = read_name(n, &name);
- if (res == 0) {
- if (name) {
- SEQUENCE_ADD((*dst), last, name);
- name = NULL;
- }
- }
- else break;
- }
- }
- n = n->next;
- }
- return res;
- }
- static int read_entry(xmlNode *entry_node, entry_t **dst)
- {
- xmlAttr *a;
- const char *a_val;
-
- /* allocate memory and prepare empty node */
- if (!dst) return -1;
- *dst = (entry_t*)cds_malloc(sizeof(entry_t));
- if (!(*dst)) return -2;
- memset(*dst, 0, sizeof(entry_t));
- /* get attributes */
- a = find_attr(entry_node->properties, "uri");
- if (a) {
- a_val = get_attr_value(a);
- if (a_val) (*dst)->uri = zt_strdup(a_val);
- }
- return read_names(entry_node, &((*dst)->display_names));
- }
- static int read_external(xmlNode *entry_node, external_t **dst)
- {
- xmlAttr *a;
- const char *a_val;
-
- /* allocate memory and prepare empty node */
- if (!dst) return -1;
- *dst = (external_t*)cds_malloc(sizeof(external_t));
- if (!(*dst)) return -2;
- memset(*dst, 0, sizeof(external_t));
- /* get attributes */
- a = find_attr(entry_node->properties, "anchor");
- if (a) {
- a_val = get_attr_value(a);
- if (a_val) (*dst)->anchor = zt_strdup(a_val);
- }
- return 0;
- }
- int read_list(xmlNode *list_node, list_t **dst, int read_content_only)
- {
- int res = 0;
- xmlAttr *a;
- const char *a_val;
- xmlNode *n;
- list_content_t *l, *last_l;
-
- /* allocate memory and prepare empty node */
- if (!dst) return -1;
- *dst = (list_t*)cds_malloc(sizeof(list_t));
- if (!(*dst)) return -2;
- memset(*dst, 0, sizeof(list_t));
- /* get attributes */
- if (!read_content_only) {
- a = find_attr(list_node->properties, "name");
- if (a) {
- a_val = get_attr_value(a);
- if (a_val) (*dst)->name = zt_strdup(a_val);
- }
- }
- /* read entries */
- last_l = NULL;
- n = list_node->children;
- while (n) {
- if (n->type == XML_ELEMENT_NODE) {
- l = (list_content_t*) cds_malloc(sizeof(list_content_t));
- if (!l) return -1;
- memset(l, 0, sizeof(*l));
-
- if (cmp_node(n, "list", rl_namespace) >= 0) {
- res = read_list(n, &l->u.list, 0);
- if (res == 0) {
- if (l->u.list) {
- l->type = lct_list;
- SEQUENCE_ADD((*dst)->content, last_l, l);
- l = NULL;
- }
- }
- else break;
- }
-
- if (cmp_node(n, "entry", rl_namespace) >= 0) {
- res = read_entry(n, &l->u.entry);
- if (res == 0) {
- if (l->u.entry) {
- l->type = lct_entry;
- SEQUENCE_ADD((*dst)->content, last_l, l);
- l = NULL;
- }
- }
- else break;
- }
-
- if (cmp_node(n, "entry-ref", rl_namespace) >= 0) {
- res = read_entry_ref(n, &l->u.entry_ref);
- if (res == 0) {
- if (l->u.entry_ref) {
- l->type = lct_entry_ref;
- SEQUENCE_ADD((*dst)->content, last_l, l);
- l = NULL;
- }
- }
- else break;
- }
-
- if (cmp_node(n, "external", rl_namespace) >= 0) {
- res = read_external(n, &l->u.external);
- if (res == 0) {
- if (l->u.external) {
- l->type = lct_external;
- SEQUENCE_ADD((*dst)->content, last_l, l);
- l = NULL;
- }
- }
- else break;
- }
-
- if (l) {
- cds_free(l);
- l = NULL;
- }
-
- }
- n = n->next;
- }
-
- return 0;
- }
- static int read_resource_lists(xmlNode *root, resource_lists_t **dst)
- {
- resource_lists_t *rl;
- /* xmlAttr *a; */
- xmlNode *n;
- list_t *l, *last_l;
- int res = 0;
-
- if (!dst) return -1;
- else *dst = NULL;
- if (!root) return -1;
-
- if (cmp_node(root, "resource-lists", rl_namespace) < 0) {
- ERROR_LOG("document is not a resource-lists\n");
- return -1;
- }
- rl = (resource_lists_t*)cds_malloc(sizeof(resource_lists_t));
- if (!rl) return -2;
- *dst = rl;
- rl->lists = NULL;
-
- last_l = NULL;
- n = root->children;
- while (n) {
- if (n->type == XML_ELEMENT_NODE) {
- if (cmp_node(n, "list", rl_namespace) >= 0) {
- res = read_list(n, &l, 0);
- if (res == 0) {
- if (l) SEQUENCE_ADD(rl->lists, last_l, l);
- }
- else break;
- }
- }
- n = n->next;
- }
- return res;
- }
- int parse_resource_lists_xml(const char *data, int data_len, resource_lists_t **dst)
- {
- int res = 0;
- xmlDocPtr doc; /* the resulting document tree */
- if (dst) *dst = NULL;
- doc = xmlReadMemory(data, data_len, NULL, NULL, xml_parser_flags);
- if (doc == NULL) {
- ERROR_LOG("can't parse document\n");
- return -1;
- }
-
- res = read_resource_lists(xmlDocGetRootElement(doc), dst);
- xmlFreeDoc(doc);
- return res;
- }
- int parse_list_xml(const char *data, int data_len, list_t **dst)
- {
- int res = 0;
- xmlDocPtr doc; /* the resulting document tree */
- if (dst) *dst = NULL;
- doc = xmlReadMemory(data, data_len, NULL, NULL, xml_parser_flags);
- if (doc == NULL) {
- ERROR_LOG("can't parse document\n");
- return -1;
- }
-
- res = read_list(xmlDocGetRootElement(doc), dst, 0);
- xmlFreeDoc(doc);
- return res;
- }
- int parse_as_list_content_xml(const char *data, int data_len, list_t **dst)
- {
- int res = 0;
- xmlDocPtr doc; /* the resulting document tree */
- if (dst) *dst = NULL;
- doc = xmlReadMemory(data, data_len, NULL, NULL, xml_parser_flags);
- if (doc == NULL) {
- ERROR_LOG("can't parse document\n");
- return -1;
- }
-
- res = read_list(xmlDocGetRootElement(doc), dst, 1);
- xmlFreeDoc(doc);
- return res;
- }
- int parse_entry_xml(const char *data, int data_len, entry_t **dst)
- {
- int res = 0;
- xmlDocPtr doc; /* the resulting document tree */
- if (dst) *dst = NULL;
- doc = xmlReadMemory(data, data_len, NULL, NULL, xml_parser_flags);
- if (doc == NULL) {
- ERROR_LOG("can't parse document\n");
- return -1;
- }
-
- res = read_entry(xmlDocGetRootElement(doc), dst);
- xmlFreeDoc(doc);
- return res;
- }
- void free_display_name(display_name_t *n)
- {
- if (!n) return;
- if (n->name) cds_free(n->name);
- if (n->lang) cds_free(n->lang);
- cds_free(n);
- }
- void free_display_names(display_name_t *sequence_first)
- {
- display_name_t *d, *n;
-
- if (!sequence_first) return;
-
- d = SEQUENCE_FIRST(sequence_first);
- while (d) {
- n = SEQUENCE_NEXT(d);
- free_display_name(d);
- d = n;
- }
-
- }
- void free_entry(entry_t *e)
- {
- if (!e) return;
-
- if (e->uri) cds_free(e->uri);
- free_display_names(e->display_names);
-
- cds_free(e);
- }
- void free_entry_ref(entry_ref_t *e)
- {
- if (!e) return;
- if (e->ref) cds_free(e->ref);
- cds_free(e);
- }
- void free_external(external_t *e)
- {
- if (!e) return;
- if (e->anchor) cds_free(e->anchor);
- cds_free(e);
- }
- void free_list(list_t *l)
- {
- list_content_t *e, *f;
- if (!l) return;
-
- if (l->name) cds_free(l->name);
- e = SEQUENCE_FIRST(l->content);
- while (e) {
- switch (e->type) {
- case lct_list: free_list(e->u.list); break;
- case lct_entry: free_entry(e->u.entry); break;
- case lct_entry_ref: free_entry_ref(e->u.entry_ref); break;
- case lct_external: free_external(e->u.external); break;
- }
- f = e;
- e = SEQUENCE_NEXT(e);
- /* TRACE_LOG("freeing %p\n", f); */
- cds_free(f);
- }
- cds_free(l);
- }
- void free_resource_lists(resource_lists_t *rl)
- {
- list_t *e, *f;
- if (!rl) return;
-
- e = SEQUENCE_FIRST(rl->lists);
- while (e) {
- f = SEQUENCE_NEXT(e);
- free_list(e);
- e = f;
- }
- cds_free(rl);
- }
|