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