1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 **/
19
20 #include "common.h"
21
22 static char data_static[ZBX_MAX_B64_LEN];
23
24 /******************************************************************************
25 * *
26 * Purpose: get DATA from <tag>DATA</tag> *
27 * *
28 ******************************************************************************/
xml_get_data_dyn(const char * xml,const char * tag,char ** data)29 int xml_get_data_dyn(const char *xml, const char *tag, char **data)
30 {
31 size_t len, sz;
32 char *start, *end;
33
34 sz = sizeof(data_static);
35
36 len = zbx_snprintf(data_static, sz, "<%s>", tag);
37 if (NULL == (start = strstr(xml, data_static)))
38 return FAIL;
39
40 zbx_snprintf(data_static, sz, "</%s>", tag);
41 if (NULL == (end = strstr(xml, data_static)))
42 return FAIL;
43
44 if (end < start)
45 return FAIL;
46
47 start += len;
48 len = end - start;
49
50 if (len > sz - 1)
51 *data = zbx_malloc(*data, len + 1);
52 else
53 *data = data_static;
54
55 zbx_strlcpy(*data, start, len + 1);
56
57 return SUCCEED;
58 }
59
xml_free_data_dyn(char ** data)60 void xml_free_data_dyn(char **data)
61 {
62 if (*data == data_static)
63 *data = NULL;
64 else
65 zbx_free(*data);
66 }
67
68 /******************************************************************************
69 * *
70 * Function: xml_escape_dyn *
71 * *
72 * Purpose: replace <> symbols in string with <> so the resulting *
73 * string can be written into xml field *
74 * *
75 * Parameters: data - [IN] the input string *
76 * *
77 * Return value: an allocated string containing escaped input string *
78 * *
79 * Comments: The caller must free the returned string after it has been used. *
80 * *
81 ******************************************************************************/
xml_escape_dyn(const char * data)82 char *xml_escape_dyn(const char *data)
83 {
84 char *out, *ptr_out;
85 const char *ptr_in;
86 int size = 0;
87
88 if (NULL == data)
89 return zbx_strdup(NULL, "");
90
91 for (ptr_in = data; '\0' != *ptr_in; ptr_in++)
92 {
93 switch (*ptr_in)
94 {
95 case '<':
96 case '>':
97 size += 4;
98 break;
99 case '&':
100 size += 5;
101 break;
102 case '"':
103 case '\'':
104 size += 6;
105 break;
106 default:
107 size++;
108 }
109 }
110 size++;
111
112 out = zbx_malloc(NULL, size);
113
114 for (ptr_out = out, ptr_in = data; '\0' != *ptr_in; ptr_in++)
115 {
116 switch (*ptr_in)
117 {
118 case '<':
119 *ptr_out++ = '&';
120 *ptr_out++ = 'l';
121 *ptr_out++ = 't';
122 *ptr_out++ = ';';
123 break;
124 case '>':
125 *ptr_out++ = '&';
126 *ptr_out++ = 'g';
127 *ptr_out++ = 't';
128 *ptr_out++ = ';';
129 break;
130 case '&':
131 *ptr_out++ = '&';
132 *ptr_out++ = 'a';
133 *ptr_out++ = 'm';
134 *ptr_out++ = 'p';
135 *ptr_out++ = ';';
136 break;
137 case '"':
138 *ptr_out++ = '&';
139 *ptr_out++ = 'q';
140 *ptr_out++ = 'u';
141 *ptr_out++ = 'o';
142 *ptr_out++ = 't';
143 *ptr_out++ = ';';
144 break;
145 case '\'':
146 *ptr_out++ = '&';
147 *ptr_out++ = 'a';
148 *ptr_out++ = 'p';
149 *ptr_out++ = 'o';
150 *ptr_out++ = 's';
151 *ptr_out++ = ';';
152 break;
153 default:
154 *ptr_out++ = *ptr_in;
155 }
156
157 }
158 *ptr_out = '\0';
159
160 return out;
161 }
162