1 /*
2  * Copyright (C) 2005 iptelorg GmbH
3  *
4  * This file is part of ser, a free SIP server.
5  *
6  * ser is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  * For a license to use the ser software under conditions
12  * other than those described here, or to purchase support for this
13  * software, please contact iptel.org by e-mail at the following addresses:
14  *    info@iptel.org
15  *
16  * ser is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24  */
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include <time.h>
29 
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32 #include <cds/logger.h>
33 #include <xcap/xml_utils.h>
34 
35 int xml_parser_flags = XML_PARSE_NOERROR | XML_PARSE_NOWARNING;
36 
str2int(const char * s,int * dst)37 static int str2int(const char *s, int *dst)
38 {
39 	/* if not null sets the given integer to value */
40 	if (!s) {
41 		*dst = 0;
42 		return -1;
43 	}
44 	else *dst = atoi(s);
45 	return 0;
46 }
47 
get_int_attr(xmlNode * n,const char * attr_name,int * dst)48 int get_int_attr(xmlNode *n, const char *attr_name, int *dst)
49 {
50 	const char *s = get_attr_value(find_attr(n->properties, attr_name));
51 	if (!s) return 1;
52 	return str2int(s, dst);
53 }
54 
get_str_attr(xmlNode * n,const char * attr_name,str_t * dst)55 int get_str_attr(xmlNode *n, const char *attr_name, str_t *dst)
56 {
57 	const char *s = get_attr_value(find_attr(n->properties, attr_name));
58 	if (!s) {
59 		str_clear(dst);
60 		return 1;
61 	}
62 	else return str_dup_zt(dst, s);
63 }
64 
xmlstrcmp(const xmlChar * xmls,const char * name)65 int xmlstrcmp(const xmlChar *xmls, const char *name)
66 {
67 	if (!xmls) return -1;
68 	if (!name) return 1;
69 	return strcmp((const char*)xmls, name);
70 }
71 
72 /* xmlNode *find_node(xmlNode *parent, const char *name) */
find_node(xmlNode * parent,const char * name,const char * nspace)73 xmlNode *find_node(xmlNode *parent, const char *name, const char *nspace)
74 {
75 	if (!parent) return NULL;
76 	xmlNode *n = parent->children;
77 	while (n) {
78 		if (cmp_node(n, name, nspace) >= 0) break;
79 		n = n->next;
80 	}
81 	return n;
82 }
83 
find_value(xmlNode * first_child)84 const char *find_value(xmlNode *first_child)
85 {
86 	const char *s = NULL;
87 
88 	xmlNode *c = first_child;
89 	while (c) {
90 		if (c->type == XML_TEXT_NODE) {
91 			if (c->content) s = (const char *)c->content;
92 			break;
93 		}
94 		c = c->next;
95 	}
96 
97 	return s;
98 }
99 
get_node_value(xmlNode * n)100 const char *get_node_value(xmlNode *n)
101 {
102 	if (!n) return NULL;
103 	return find_value(n->children);
104 }
105 
find_attr(xmlAttr * first,const char * name)106 xmlAttr *find_attr(xmlAttr *first, const char *name)
107 {
108 	xmlAttr *a = first;
109 	while (a) {
110 		if (xmlstrcmp(a->name, name) == 0) break;
111 		a = a->next;
112 	}
113 	return a;
114 }
115 
get_attr_value(xmlAttr * a)116 const char *get_attr_value(xmlAttr *a)
117 {
118 	if (!a) return NULL;
119 	return find_value(a->children);
120 }
121 
cmp_node(xmlNode * node,const char * name,const char * nspace)122 int cmp_node(xmlNode *node, const char *name, const char *nspace)
123 {
124 	if (!node) return -1;
125 	if (node->type != XML_ELEMENT_NODE) return -1;
126 
127 	if (xmlstrcmp(node->name, name) != 0) return -1;
128 	if (!nspace) return 0;
129 	if (!node->ns) {
130 		/* DEBUG_LOG("nemam NS!!!!!!!\n"); */
131 		return 1;
132 	}
133 	if (xmlstrcmp(node->ns->href, nspace) == 0) return 0;
134 	return -1;
135 }
136 
xmltime2time(const char * xt)137 time_t xmltime2time(const char *xt)
138 {
139 	/* TODO: translate XML time in input parametr to time_t structure */
140 	ERROR_LOG("can't translate xmltime to time_t: not finished yet!\n");
141 	return 0;
142 }
143 
144