[8f243ad] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Simple XML (stream) parse tree handling code (Jabber/XMPP, mainly) * |
---|
| 5 | * * |
---|
[0e788f5] | 6 | * Copyright 2006-2012 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
[8f243ad] | 7 | * * |
---|
[4f7255d] | 8 | * This program is free software; you can redistribute it and/or modify * |
---|
| 9 | * it under the terms of the GNU General Public License as published by * |
---|
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
| 11 | * (at your option) any later version. * |
---|
[8f243ad] | 12 | * * |
---|
[4f7255d] | 13 | * This program is distributed in the hope that it will be useful, * |
---|
[8f243ad] | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
[4f7255d] | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
| 16 | * GNU General Public License for more details. * |
---|
[8f243ad] | 17 | * * |
---|
[4f7255d] | 18 | * You should have received a copy of the GNU General Public License along * |
---|
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
[8f243ad] | 21 | * * |
---|
| 22 | ****************************************************************************/ |
---|
| 23 | |
---|
| 24 | #include <glib.h> |
---|
| 25 | #include <string.h> |
---|
| 26 | #include <unistd.h> |
---|
| 27 | #include <ctype.h> |
---|
| 28 | #include <stdio.h> |
---|
| 29 | |
---|
| 30 | #include "xmltree.h" |
---|
| 31 | |
---|
[7a2a486] | 32 | #define g_strcasecmp g_ascii_strcasecmp |
---|
| 33 | #define g_strncasecmp g_ascii_strncasecmp |
---|
| 34 | |
---|
[552da22] | 35 | /* g_memdup() deprecated as of glib 2.68.0 */ |
---|
[af09afd] | 36 | #ifndef GLIB_VERSION_2_68 |
---|
[552da22] | 37 | #define g_memdup2 g_memdup |
---|
| 38 | #endif |
---|
| 39 | |
---|
[5ebff60] | 40 | static void xt_start_element(GMarkupParseContext *ctx, const gchar *element_name, const gchar **attr_names, |
---|
| 41 | const gchar **attr_values, gpointer data, GError **error) |
---|
[8f243ad] | 42 | { |
---|
| 43 | struct xt_parser *xt = data; |
---|
[5ebff60] | 44 | struct xt_node *node = g_new0(struct xt_node, 1), *nt; |
---|
[8f243ad] | 45 | int i; |
---|
[5ebff60] | 46 | |
---|
[8f243ad] | 47 | node->parent = xt->cur; |
---|
[5ebff60] | 48 | node->name = g_strdup(element_name); |
---|
| 49 | |
---|
[8f243ad] | 50 | /* First count the number of attributes */ |
---|
[5ebff60] | 51 | for (i = 0; attr_names[i]; i++) { |
---|
| 52 | ; |
---|
| 53 | } |
---|
| 54 | |
---|
[8f243ad] | 55 | /* Then allocate a NULL-terminated array. */ |
---|
[5ebff60] | 56 | node->attr = g_new0(struct xt_attr, i + 1); |
---|
| 57 | |
---|
[8f243ad] | 58 | /* And fill it, saving one variable by starting at the end. */ |
---|
[5ebff60] | 59 | for (i--; i >= 0; i--) { |
---|
| 60 | node->attr[i].key = g_strdup(attr_names[i]); |
---|
| 61 | node->attr[i].value = g_strdup(attr_values[i]); |
---|
[8f243ad] | 62 | } |
---|
[5ebff60] | 63 | |
---|
[8f243ad] | 64 | /* Add it to the linked list of children nodes, if we have a current |
---|
| 65 | node yet. */ |
---|
[5ebff60] | 66 | if (xt->cur) { |
---|
| 67 | if (xt->cur->children) { |
---|
| 68 | for (nt = xt->cur->children; nt->next; nt = nt->next) { |
---|
| 69 | ; |
---|
| 70 | } |
---|
[8f243ad] | 71 | nt->next = node; |
---|
[5ebff60] | 72 | } else { |
---|
[8f243ad] | 73 | xt->cur->children = node; |
---|
| 74 | } |
---|
[5ebff60] | 75 | } else if (xt->root) { |
---|
[8f243ad] | 76 | /* ERROR situation: A second root-element??? */ |
---|
| 77 | } |
---|
[5ebff60] | 78 | |
---|
[8f243ad] | 79 | /* Now this node will be the new current node. */ |
---|
| 80 | xt->cur = node; |
---|
| 81 | /* And maybe this is the root? */ |
---|
[5ebff60] | 82 | if (xt->root == NULL) { |
---|
[8f243ad] | 83 | xt->root = node; |
---|
[5ebff60] | 84 | } |
---|
[8f243ad] | 85 | } |
---|
| 86 | |
---|
[5ebff60] | 87 | static void xt_text(GMarkupParseContext *ctx, const gchar *text, gsize text_len, gpointer data, GError **error) |
---|
[8f243ad] | 88 | { |
---|
| 89 | struct xt_parser *xt = data; |
---|
| 90 | struct xt_node *node = xt->cur; |
---|
[5ebff60] | 91 | |
---|
| 92 | if (node == NULL) { |
---|
[8f243ad] | 93 | return; |
---|
[5ebff60] | 94 | } |
---|
| 95 | |
---|
[8f243ad] | 96 | /* FIXME: Does g_renew also OFFICIALLY accept NULL arguments? */ |
---|
[5ebff60] | 97 | node->text = g_renew(char, node->text, node->text_len + text_len + 1); |
---|
| 98 | memcpy(node->text + node->text_len, text, text_len); |
---|
[8f243ad] | 99 | node->text_len += text_len; |
---|
| 100 | /* Zero termination is always nice to have. */ |
---|
| 101 | node->text[node->text_len] = 0; |
---|
| 102 | } |
---|
| 103 | |
---|
[5ebff60] | 104 | static void xt_end_element(GMarkupParseContext *ctx, const gchar *element_name, gpointer data, GError **error) |
---|
[8f243ad] | 105 | { |
---|
| 106 | struct xt_parser *xt = data; |
---|
[5ebff60] | 107 | |
---|
[8f243ad] | 108 | xt->cur->flags |= XT_COMPLETE; |
---|
| 109 | xt->cur = xt->cur->parent; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | GMarkupParser xt_parser_funcs = |
---|
| 113 | { |
---|
| 114 | xt_start_element, |
---|
| 115 | xt_end_element, |
---|
| 116 | xt_text, |
---|
| 117 | NULL, |
---|
| 118 | NULL |
---|
| 119 | }; |
---|
| 120 | |
---|
[5ebff60] | 121 | struct xt_parser *xt_new(const struct xt_handler_entry *handlers, gpointer data) |
---|
[8f243ad] | 122 | { |
---|
[5ebff60] | 123 | struct xt_parser *xt = g_new0(struct xt_parser, 1); |
---|
| 124 | |
---|
[8f243ad] | 125 | xt->data = data; |
---|
[4bbcba3] | 126 | xt->handlers = handlers; |
---|
[5ebff60] | 127 | xt_reset(xt); |
---|
| 128 | |
---|
[8f243ad] | 129 | return xt; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /* Reset the parser, flush everything we have so far. For example, we need |
---|
| 133 | this for XMPP when doing TLS/SASL to restart the stream. */ |
---|
[5ebff60] | 134 | void xt_reset(struct xt_parser *xt) |
---|
[8f243ad] | 135 | { |
---|
[5ebff60] | 136 | if (xt->parser) { |
---|
| 137 | g_markup_parse_context_free(xt->parser); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | xt->parser = g_markup_parse_context_new(&xt_parser_funcs, 0, xt, NULL); |
---|
| 141 | |
---|
| 142 | if (xt->root) { |
---|
| 143 | xt_free_node(xt->root); |
---|
[8f243ad] | 144 | xt->root = NULL; |
---|
| 145 | xt->cur = NULL; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /* Feed the parser, don't execute any handler. Returns -1 on errors, 0 on |
---|
| 150 | end-of-stream and 1 otherwise. */ |
---|
[5ebff60] | 151 | int xt_feed(struct xt_parser *xt, const char *text, int text_len) |
---|
[8f243ad] | 152 | { |
---|
[5ebff60] | 153 | if (!g_markup_parse_context_parse(xt->parser, text, text_len, &xt->gerr)) { |
---|
[8f243ad] | 154 | return -1; |
---|
| 155 | } |
---|
[5ebff60] | 156 | |
---|
| 157 | return !(xt->root && xt->root->flags & XT_COMPLETE); |
---|
[8f243ad] | 158 | } |
---|
| 159 | |
---|
| 160 | /* Find completed nodes and see if a handler has to be called. Passing |
---|
| 161 | a node isn't necessary if you want to start at the root, just pass |
---|
[101d84f] | 162 | NULL. This second argument is needed for recursive calls. */ |
---|
[5ebff60] | 163 | int xt_handle(struct xt_parser *xt, struct xt_node *node, int depth) |
---|
[8f243ad] | 164 | { |
---|
| 165 | struct xt_node *c; |
---|
| 166 | xt_status st; |
---|
| 167 | int i; |
---|
[5ebff60] | 168 | |
---|
| 169 | if (xt->root == NULL) { |
---|
[b0ee720] | 170 | return 1; |
---|
[5ebff60] | 171 | } |
---|
| 172 | |
---|
| 173 | if (node == NULL) { |
---|
| 174 | return xt_handle(xt, xt->root, depth); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | if (depth != 0) { |
---|
| 178 | for (c = node->children; c; c = c->next) { |
---|
| 179 | if (!xt_handle(xt, c, depth > 0 ? depth - 1 : depth)) { |
---|
[101d84f] | 180 | return 0; |
---|
[8f243ad] | 181 | } |
---|
| 182 | } |
---|
[5ebff60] | 183 | } |
---|
| 184 | |
---|
| 185 | if (node->flags & XT_COMPLETE && !(node->flags & XT_SEEN)) { |
---|
| 186 | if (xt->handlers) { |
---|
| 187 | for (i = 0; xt->handlers[i].func; i++) { |
---|
| 188 | /* This one is fun! \o/ */ |
---|
| 189 | |
---|
| 190 | /* If handler.name == NULL it means it should always match. */ |
---|
| 191 | if ((xt->handlers[i].name == NULL || |
---|
| 192 | /* If it's not, compare. There should always be a name. */ |
---|
| 193 | g_strcasecmp(xt->handlers[i].name, node->name) == 0) && |
---|
| 194 | /* If handler.parent == NULL, it's a match. */ |
---|
| 195 | (xt->handlers[i].parent == NULL || |
---|
| 196 | /* If there's a parent node, see if the name matches. */ |
---|
| 197 | (node->parent ? g_strcasecmp(xt->handlers[i].parent, node->parent->name) == 0 : |
---|
| 198 | /* If there's no parent, the handler should mention <root> as a parent. */ |
---|
| 199 | strcmp(xt->handlers[i].parent, "<root>") == 0))) { |
---|
| 200 | st = xt->handlers[i].func(node, xt->data); |
---|
| 201 | |
---|
| 202 | if (st == XT_ABORT) { |
---|
| 203 | return 0; |
---|
| 204 | } else if (st != XT_NEXT) { |
---|
| 205 | break; |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
[8f243ad] | 211 | node->flags |= XT_SEEN; |
---|
| 212 | } |
---|
[5ebff60] | 213 | |
---|
[8f243ad] | 214 | return 1; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /* Garbage collection: Cleans up all nodes that are handled. Useful for |
---|
| 218 | streams because there's no reason to keep a complete packet history |
---|
| 219 | in memory. */ |
---|
[5ebff60] | 220 | void xt_cleanup(struct xt_parser *xt, struct xt_node *node, int depth) |
---|
[8f243ad] | 221 | { |
---|
| 222 | struct xt_node *c, *prev; |
---|
[5ebff60] | 223 | |
---|
| 224 | if (!xt || !xt->root) { |
---|
[21167d2] | 225 | return; |
---|
[5ebff60] | 226 | } |
---|
| 227 | |
---|
| 228 | if (node == NULL) { |
---|
| 229 | xt_cleanup(xt, xt->root, depth); |
---|
[daae10f] | 230 | return; |
---|
| 231 | } |
---|
[5ebff60] | 232 | |
---|
| 233 | if (node->flags & XT_SEEN && node == xt->root) { |
---|
| 234 | xt_free_node(xt->root); |
---|
[8f243ad] | 235 | xt->root = xt->cur = NULL; |
---|
| 236 | /* xt->cur should be NULL already, BTW... */ |
---|
[5ebff60] | 237 | |
---|
[8f243ad] | 238 | return; |
---|
| 239 | } |
---|
[5ebff60] | 240 | |
---|
[8f243ad] | 241 | /* c contains the current node, prev the previous node (or NULL). |
---|
| 242 | I admit, this one's pretty horrible. */ |
---|
[5ebff60] | 243 | for (c = node->children, prev = NULL; c; prev = c, c = c ? c->next : node->children) { |
---|
| 244 | if (c->flags & XT_SEEN) { |
---|
[8f243ad] | 245 | /* Remove the node from the linked list. */ |
---|
[5ebff60] | 246 | if (prev) { |
---|
[8f243ad] | 247 | prev->next = c->next; |
---|
[5ebff60] | 248 | } else { |
---|
[8f243ad] | 249 | node->children = c->next; |
---|
[5ebff60] | 250 | } |
---|
| 251 | |
---|
| 252 | xt_free_node(c); |
---|
| 253 | |
---|
[8f243ad] | 254 | /* Since the for loop wants to get c->next, make sure |
---|
| 255 | c points at something that exists (and that c->next |
---|
| 256 | will actually be the next item we should check). c |
---|
| 257 | can be NULL now, if we just removed the first item. |
---|
| 258 | That explains the ? thing in for(). */ |
---|
| 259 | c = prev; |
---|
[5ebff60] | 260 | } else { |
---|
[8f243ad] | 261 | /* This node can't be cleaned up yet, but maybe a |
---|
| 262 | subnode can. */ |
---|
[5ebff60] | 263 | if (depth != 0) { |
---|
| 264 | xt_cleanup(xt, c, depth > 0 ? depth - 1 : depth); |
---|
| 265 | } |
---|
[8f243ad] | 266 | } |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
[5ebff60] | 270 | struct xt_node *xt_from_string(const char *in, int len) |
---|
[d93c0eb9] | 271 | { |
---|
| 272 | struct xt_parser *parser; |
---|
[6f55bec] | 273 | struct xt_node *ret = NULL; |
---|
[5ebff60] | 274 | |
---|
| 275 | if (len == 0) { |
---|
| 276 | len = strlen(in); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | parser = xt_new(NULL, NULL); |
---|
| 280 | xt_feed(parser, in, len); |
---|
| 281 | if (parser->cur == NULL) { |
---|
[6f55bec] | 282 | ret = parser->root; |
---|
| 283 | parser->root = NULL; |
---|
| 284 | } |
---|
[5ebff60] | 285 | xt_free(parser); |
---|
| 286 | |
---|
[d93c0eb9] | 287 | return ret; |
---|
| 288 | } |
---|
| 289 | |
---|
[5ebff60] | 290 | static void xt_to_string_real(struct xt_node *node, GString *str, int indent) |
---|
[8f243ad] | 291 | { |
---|
| 292 | char *buf; |
---|
| 293 | struct xt_node *c; |
---|
| 294 | int i; |
---|
[5ebff60] | 295 | |
---|
| 296 | if (indent > 1) { |
---|
| 297 | g_string_append_len(str, "\n\t\t\t\t\t\t\t\t", |
---|
| 298 | indent < 8 ? indent : 8); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | g_string_append_printf(str, "<%s", node->name); |
---|
| 302 | |
---|
| 303 | for (i = 0; node->attr[i].key; i++) { |
---|
| 304 | buf = g_markup_printf_escaped(" %s=\"%s\"", node->attr[i].key, node->attr[i].value); |
---|
| 305 | g_string_append(str, buf); |
---|
| 306 | g_free(buf); |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | if (node->text == NULL && node->children == NULL) { |
---|
| 310 | g_string_append(str, "/>"); |
---|
[8f243ad] | 311 | return; |
---|
| 312 | } |
---|
[5ebff60] | 313 | |
---|
| 314 | g_string_append(str, ">"); |
---|
| 315 | if (node->text_len > 0) { |
---|
| 316 | buf = g_markup_escape_text(node->text, node->text_len); |
---|
| 317 | g_string_append(str, buf); |
---|
| 318 | g_free(buf); |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | for (c = node->children; c; c = c->next) { |
---|
| 322 | xt_to_string_real(c, str, indent ? indent + 1 : 0); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | if (indent > 0 && node->children) { |
---|
| 326 | g_string_append_len(str, "\n\t\t\t\t\t\t\t\t", |
---|
| 327 | indent < 8 ? indent : 8); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | g_string_append_printf(str, "</%s>", node->name); |
---|
[8f243ad] | 331 | } |
---|
| 332 | |
---|
[5ebff60] | 333 | char *xt_to_string(struct xt_node *node) |
---|
[8f243ad] | 334 | { |
---|
| 335 | GString *ret; |
---|
[5ebff60] | 336 | |
---|
| 337 | ret = g_string_new(""); |
---|
| 338 | xt_to_string_real(node, ret, 0); |
---|
| 339 | return g_string_free(ret, FALSE); |
---|
[222b440] | 340 | } |
---|
| 341 | |
---|
| 342 | /* WITH indentation! */ |
---|
[5ebff60] | 343 | char *xt_to_string_i(struct xt_node *node) |
---|
[222b440] | 344 | { |
---|
| 345 | GString *ret; |
---|
[5ebff60] | 346 | |
---|
| 347 | ret = g_string_new(""); |
---|
| 348 | xt_to_string_real(node, ret, 1); |
---|
| 349 | return g_string_free(ret, FALSE); |
---|
[8f243ad] | 350 | } |
---|
| 351 | |
---|
[5ebff60] | 352 | void xt_print(struct xt_node *node) |
---|
[8f243ad] | 353 | { |
---|
[5ebff60] | 354 | char *str = xt_to_string_i(node); |
---|
| 355 | |
---|
| 356 | fprintf(stderr, "%s", str); |
---|
| 357 | g_free(str); |
---|
[8f243ad] | 358 | } |
---|
| 359 | |
---|
[5ebff60] | 360 | struct xt_node *xt_dup(struct xt_node *node) |
---|
[022df46] | 361 | { |
---|
[5ebff60] | 362 | struct xt_node *dup = g_new0(struct xt_node, 1); |
---|
[022df46] | 363 | struct xt_node *c, *dc = NULL; |
---|
| 364 | int i; |
---|
[5ebff60] | 365 | |
---|
[022df46] | 366 | /* Let's NOT copy the parent element here BTW! Only do it for children. */ |
---|
[5ebff60] | 367 | |
---|
| 368 | dup->name = g_strdup(node->name); |
---|
[022df46] | 369 | dup->flags = node->flags; |
---|
[5ebff60] | 370 | if (node->text) { |
---|
[552da22] | 371 | dup->text = g_memdup2(node->text, node->text_len + 1); |
---|
[022df46] | 372 | dup->text_len = node->text_len; |
---|
| 373 | } |
---|
[5ebff60] | 374 | |
---|
[022df46] | 375 | /* Count the number of attributes and allocate the new array. */ |
---|
[5ebff60] | 376 | for (i = 0; node->attr[i].key; i++) { |
---|
| 377 | ; |
---|
| 378 | } |
---|
| 379 | dup->attr = g_new0(struct xt_attr, i + 1); |
---|
| 380 | |
---|
[022df46] | 381 | /* Copy them all! */ |
---|
[5ebff60] | 382 | for (i--; i >= 0; i--) { |
---|
| 383 | dup->attr[i].key = g_strdup(node->attr[i].key); |
---|
| 384 | dup->attr[i].value = g_strdup(node->attr[i].value); |
---|
[022df46] | 385 | } |
---|
[5ebff60] | 386 | |
---|
[022df46] | 387 | /* This nice mysterious loop takes care of the children. */ |
---|
[5ebff60] | 388 | for (c = node->children; c; c = c->next) { |
---|
| 389 | if (dc == NULL) { |
---|
| 390 | dc = dup->children = xt_dup(c); |
---|
| 391 | } else { |
---|
| 392 | dc = (dc->next = xt_dup(c)); |
---|
| 393 | } |
---|
| 394 | |
---|
[022df46] | 395 | dc->parent = dup; |
---|
| 396 | } |
---|
[5ebff60] | 397 | |
---|
[022df46] | 398 | return dup; |
---|
| 399 | } |
---|
| 400 | |
---|
[8f243ad] | 401 | /* Frees a node. This doesn't clean up references to itself from parents! */ |
---|
[5ebff60] | 402 | void xt_free_node(struct xt_node *node) |
---|
[8f243ad] | 403 | { |
---|
| 404 | int i; |
---|
[5ebff60] | 405 | |
---|
| 406 | if (!node) { |
---|
[21167d2] | 407 | return; |
---|
[5ebff60] | 408 | } |
---|
| 409 | |
---|
| 410 | g_free(node->name); |
---|
| 411 | g_free(node->text); |
---|
| 412 | |
---|
| 413 | for (i = 0; node->attr[i].key; i++) { |
---|
| 414 | g_free(node->attr[i].key); |
---|
| 415 | g_free(node->attr[i].value); |
---|
| 416 | } |
---|
| 417 | g_free(node->attr); |
---|
| 418 | |
---|
| 419 | while (node->children) { |
---|
[8f243ad] | 420 | struct xt_node *next = node->children->next; |
---|
[5ebff60] | 421 | |
---|
| 422 | xt_free_node(node->children); |
---|
[8f243ad] | 423 | node->children = next; |
---|
| 424 | } |
---|
[5ebff60] | 425 | |
---|
| 426 | g_free(node); |
---|
[8f243ad] | 427 | } |
---|
| 428 | |
---|
[5ebff60] | 429 | void xt_free(struct xt_parser *xt) |
---|
[8f243ad] | 430 | { |
---|
[5ebff60] | 431 | if (!xt) { |
---|
[21167d2] | 432 | return; |
---|
[5ebff60] | 433 | } |
---|
| 434 | |
---|
| 435 | if (xt->root) { |
---|
| 436 | xt_free_node(xt->root); |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | g_markup_parse_context_free(xt->parser); |
---|
| 440 | |
---|
| 441 | g_free(xt); |
---|
[8f243ad] | 442 | } |
---|
| 443 | |
---|
| 444 | /* To find a node's child with a specific name, pass the node's children |
---|
| 445 | list, not the node itself! The reason you have to do this by hand: So |
---|
| 446 | that you can also use this function as a find-next. */ |
---|
[5ebff60] | 447 | struct xt_node *xt_find_node(struct xt_node *node, const char *name) |
---|
[8f243ad] | 448 | { |
---|
[5ebff60] | 449 | while (node) { |
---|
[3742fb6] | 450 | char *colon; |
---|
[5ebff60] | 451 | |
---|
| 452 | if (g_strcasecmp(node->name, name) == 0 || |
---|
| 453 | ((colon = strchr(node->name, ':')) && |
---|
| 454 | g_strcasecmp(colon + 1, name) == 0)) { |
---|
[8f243ad] | 455 | break; |
---|
[5ebff60] | 456 | } |
---|
| 457 | |
---|
[8f243ad] | 458 | node = node->next; |
---|
| 459 | } |
---|
[5ebff60] | 460 | |
---|
[8f243ad] | 461 | return node; |
---|
| 462 | } |
---|
| 463 | |
---|
[d912fe4] | 464 | /* More advanced than the one above, understands something like |
---|
| 465 | ../foo/bar to find a subnode bar of a node foo which is a child |
---|
| 466 | of node's parent. Pass the node directly, not its list of children. */ |
---|
[5ebff60] | 467 | struct xt_node *xt_find_path(struct xt_node *node, const char *name) |
---|
[d912fe4] | 468 | { |
---|
[5ebff60] | 469 | while (name && *name && node) { |
---|
[d912fe4] | 470 | char *colon, *slash; |
---|
| 471 | int n; |
---|
[5ebff60] | 472 | |
---|
| 473 | if ((slash = strchr(name, '/'))) { |
---|
[d912fe4] | 474 | n = slash - name; |
---|
[5ebff60] | 475 | } else { |
---|
| 476 | n = strlen(name); |
---|
[d912fe4] | 477 | } |
---|
[5ebff60] | 478 | |
---|
| 479 | if (strncmp(name, "..", n) == 0) { |
---|
| 480 | node = node->parent; |
---|
| 481 | } else { |
---|
[d912fe4] | 482 | node = node->children; |
---|
[5ebff60] | 483 | |
---|
| 484 | while (node) { |
---|
| 485 | if (g_strncasecmp(node->name, name, n) == 0 || |
---|
| 486 | ((colon = strchr(node->name, ':')) && |
---|
| 487 | g_strncasecmp(colon + 1, name, n) == 0)) { |
---|
[d912fe4] | 488 | break; |
---|
[5ebff60] | 489 | } |
---|
| 490 | |
---|
[d912fe4] | 491 | node = node->next; |
---|
| 492 | } |
---|
| 493 | } |
---|
[5ebff60] | 494 | |
---|
[d912fe4] | 495 | name = slash ? slash + 1 : NULL; |
---|
| 496 | } |
---|
[5ebff60] | 497 | |
---|
[d912fe4] | 498 | return node; |
---|
| 499 | } |
---|
| 500 | |
---|
[5ebff60] | 501 | char *xt_find_attr(struct xt_node *node, const char *key) |
---|
[8f243ad] | 502 | { |
---|
| 503 | int i; |
---|
[3742fb6] | 504 | char *colon; |
---|
[5ebff60] | 505 | |
---|
| 506 | if (!node) { |
---|
[21167d2] | 507 | return NULL; |
---|
[5ebff60] | 508 | } |
---|
| 509 | |
---|
| 510 | for (i = 0; node->attr[i].key; i++) { |
---|
| 511 | if (g_strcasecmp(node->attr[i].key, key) == 0) { |
---|
[8f243ad] | 512 | break; |
---|
[5ebff60] | 513 | } |
---|
| 514 | } |
---|
| 515 | |
---|
[3742fb6] | 516 | /* This is an awful hack that only takes care of namespace prefixes |
---|
| 517 | inside a tag. Since IMHO excessive namespace usage in XMPP is |
---|
| 518 | massive overkill anyway (this code exists for almost four years |
---|
| 519 | now and never really missed it): Meh. */ |
---|
[5ebff60] | 520 | if (!node->attr[i].key && strcmp(key, "xmlns") == 0 && |
---|
| 521 | (colon = strchr(node->name, ':'))) { |
---|
[3742fb6] | 522 | *colon = '\0'; |
---|
[5ebff60] | 523 | for (i = 0; node->attr[i].key; i++) { |
---|
| 524 | if (strncmp(node->attr[i].key, "xmlns:", 6) == 0 && |
---|
| 525 | strcmp(node->attr[i].key + 6, node->name) == 0) { |
---|
[3742fb6] | 526 | break; |
---|
[5ebff60] | 527 | } |
---|
| 528 | } |
---|
[3742fb6] | 529 | *colon = ':'; |
---|
| 530 | } |
---|
[5ebff60] | 531 | |
---|
[8f243ad] | 532 | return node->attr[i].value; |
---|
| 533 | } |
---|
| 534 | |
---|
[5ebff60] | 535 | struct xt_node *xt_find_node_by_attr(struct xt_node *xt, const char *tag, const char *key, const char *value) |
---|
[9ead105] | 536 | { |
---|
[7b40f17] | 537 | struct xt_node *c; |
---|
| 538 | char *s; |
---|
| 539 | |
---|
[5ebff60] | 540 | for (c = xt; (c = xt_find_node(c, tag)); c = c->next) { |
---|
| 541 | if ((s = xt_find_attr(c, key)) && strcmp(s, value) == 0) { |
---|
[7b40f17] | 542 | return c; |
---|
| 543 | } |
---|
| 544 | } |
---|
| 545 | return NULL; |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | |
---|
[e6b41b1] | 549 | /* Strip a few non-printable characters that aren't allowed in XML streams |
---|
| 550 | (and upset some XMPP servers for example). */ |
---|
[5ebff60] | 551 | void xt_strip_text(char *in) |
---|
[e6b41b1] | 552 | { |
---|
| 553 | char *out = in; |
---|
| 554 | static const char nonprint[32] = { |
---|
| 555 | 0, 0, 0, 0, 0, 0, 0, 0, /* 0..7 */ |
---|
| 556 | 0, 1, 1, 0, 0, 1, 0, 0, /* 9 (tab), 10 (\n), 13 (\r) */ |
---|
| 557 | }; |
---|
[5ebff60] | 558 | |
---|
| 559 | if (!in) { |
---|
[e6b41b1] | 560 | return; |
---|
[5ebff60] | 561 | } |
---|
[e6b41b1] | 562 | |
---|
[5ebff60] | 563 | while (*in) { |
---|
| 564 | if ((unsigned int) *in >= ' ' || nonprint[(unsigned int) *in]) { |
---|
| 565 | *out++ = *in; |
---|
| 566 | } |
---|
| 567 | in++; |
---|
[e6b41b1] | 568 | } |
---|
| 569 | *out = *in; |
---|
| 570 | } |
---|
| 571 | |
---|
[5ebff60] | 572 | struct xt_node *xt_new_node(char *name, const char *text, struct xt_node *children) |
---|
[8f243ad] | 573 | { |
---|
| 574 | struct xt_node *node, *c; |
---|
[5ebff60] | 575 | |
---|
| 576 | node = g_new0(struct xt_node, 1); |
---|
| 577 | node->name = g_strdup(name); |
---|
[8f243ad] | 578 | node->children = children; |
---|
[5ebff60] | 579 | node->attr = g_new0(struct xt_attr, 1); |
---|
| 580 | |
---|
| 581 | if (text) { |
---|
| 582 | node->text = g_strdup(text); |
---|
| 583 | xt_strip_text(node->text); |
---|
| 584 | node->text_len = strlen(node->text); |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | for (c = children; c; c = c->next) { |
---|
| 588 | if (c->parent != NULL) { |
---|
[8f243ad] | 589 | /* ERROR CONDITION: They seem to have a parent already??? */ |
---|
| 590 | } |
---|
[5ebff60] | 591 | |
---|
[8f243ad] | 592 | c->parent = node; |
---|
| 593 | } |
---|
[5ebff60] | 594 | |
---|
[8f243ad] | 595 | return node; |
---|
| 596 | } |
---|
| 597 | |
---|
[5ebff60] | 598 | void xt_add_child(struct xt_node *parent, struct xt_node *child) |
---|
[8f243ad] | 599 | { |
---|
| 600 | struct xt_node *node; |
---|
[5ebff60] | 601 | |
---|
[8f243ad] | 602 | /* This function can actually be used to add more than one child, so |
---|
| 603 | do handle this properly. */ |
---|
[5ebff60] | 604 | for (node = child; node; node = node->next) { |
---|
| 605 | if (node->parent != NULL) { |
---|
[8f243ad] | 606 | /* ERROR CONDITION: They seem to have a parent already??? */ |
---|
| 607 | } |
---|
[5ebff60] | 608 | |
---|
[8f243ad] | 609 | node->parent = parent; |
---|
| 610 | } |
---|
[5ebff60] | 611 | |
---|
| 612 | if (parent->children == NULL) { |
---|
[8f243ad] | 613 | parent->children = child; |
---|
[5ebff60] | 614 | } else { |
---|
| 615 | for (node = parent->children; node->next; node = node->next) { |
---|
| 616 | ; |
---|
| 617 | } |
---|
[8f243ad] | 618 | node->next = child; |
---|
| 619 | } |
---|
| 620 | } |
---|
| 621 | |
---|
[b46769d] | 622 | /* Same, but at the beginning. */ |
---|
[5ebff60] | 623 | void xt_insert_child(struct xt_node *parent, struct xt_node *child) |
---|
[b46769d] | 624 | { |
---|
[afb9ea9] | 625 | struct xt_node *node, *last = NULL; |
---|
[5ebff60] | 626 | |
---|
| 627 | if (child == NULL) { |
---|
[afb9ea9] | 628 | return; /* BUG */ |
---|
[5ebff60] | 629 | |
---|
| 630 | } |
---|
| 631 | for (node = child; node; node = node->next) { |
---|
| 632 | if (node->parent != NULL) { |
---|
[b46769d] | 633 | /* ERROR CONDITION: They seem to have a parent already??? */ |
---|
| 634 | } |
---|
[5ebff60] | 635 | |
---|
[b46769d] | 636 | node->parent = parent; |
---|
| 637 | last = node; |
---|
| 638 | } |
---|
[5ebff60] | 639 | |
---|
[b46769d] | 640 | last->next = parent->children; |
---|
| 641 | parent->children = child; |
---|
| 642 | } |
---|
| 643 | |
---|
[5ebff60] | 644 | void xt_add_attr(struct xt_node *node, const char *key, const char *value) |
---|
[8f243ad] | 645 | { |
---|
| 646 | int i; |
---|
[5ebff60] | 647 | |
---|
[090f1cb] | 648 | /* Now actually it'd be nice if we can also change existing attributes |
---|
| 649 | (which actually means this function doesn't have the right name). |
---|
| 650 | So let's find out if we have this attribute already... */ |
---|
[5ebff60] | 651 | for (i = 0; node->attr[i].key; i++) { |
---|
| 652 | if (strcmp(node->attr[i].key, key) == 0) { |
---|
[090f1cb] | 653 | break; |
---|
[5ebff60] | 654 | } |
---|
[090f1cb] | 655 | } |
---|
[5ebff60] | 656 | |
---|
| 657 | if (node->attr[i].key == NULL) { |
---|
| 658 | /* If not, allocate space for a new attribute. */ |
---|
| 659 | node->attr = g_renew(struct xt_attr, node->attr, i + 2); |
---|
| 660 | node->attr[i].key = g_strdup(key); |
---|
| 661 | node->attr[i + 1].key = NULL; |
---|
| 662 | } else { |
---|
[090f1cb] | 663 | /* Otherwise, free the old value before setting the new one. */ |
---|
[5ebff60] | 664 | g_free(node->attr[i].value); |
---|
[090f1cb] | 665 | } |
---|
[5ebff60] | 666 | |
---|
| 667 | node->attr[i].value = g_strdup(value); |
---|
[8f243ad] | 668 | } |
---|
[259edd4] | 669 | |
---|
[5ebff60] | 670 | int xt_remove_attr(struct xt_node *node, const char *key) |
---|
[259edd4] | 671 | { |
---|
| 672 | int i, last; |
---|
[5ebff60] | 673 | |
---|
| 674 | for (i = 0; node->attr[i].key; i++) { |
---|
| 675 | if (strcmp(node->attr[i].key, key) == 0) { |
---|
[259edd4] | 676 | break; |
---|
[5ebff60] | 677 | } |
---|
| 678 | } |
---|
| 679 | |
---|
[259edd4] | 680 | /* If we didn't find the attribute... */ |
---|
[5ebff60] | 681 | if (node->attr[i].key == NULL) { |
---|
[259edd4] | 682 | return 0; |
---|
[5ebff60] | 683 | } |
---|
| 684 | |
---|
| 685 | g_free(node->attr[i].key); |
---|
| 686 | g_free(node->attr[i].value); |
---|
| 687 | |
---|
[259edd4] | 688 | /* If it's the last, this is easy: */ |
---|
[5ebff60] | 689 | if (node->attr[i + 1].key == NULL) { |
---|
[259edd4] | 690 | node->attr[i].key = node->attr[i].value = NULL; |
---|
[5ebff60] | 691 | } else { /* It's also pretty easy, actually. */ |
---|
[259edd4] | 692 | /* Find the last item. */ |
---|
[5ebff60] | 693 | for (last = i + 1; node->attr[last + 1].key; last++) { |
---|
| 694 | ; |
---|
| 695 | } |
---|
| 696 | |
---|
[259edd4] | 697 | node->attr[i] = node->attr[last]; |
---|
| 698 | node->attr[last].key = NULL; |
---|
| 699 | node->attr[last].value = NULL; |
---|
| 700 | } |
---|
[5ebff60] | 701 | |
---|
[259edd4] | 702 | /* Let's not bother with reallocating memory here. It takes time and |
---|
| 703 | most packets don't stay in memory for long anyway. */ |
---|
[5ebff60] | 704 | |
---|
[259edd4] | 705 | return 1; |
---|
| 706 | } |
---|