source: protocols/jabber/xmltree.c @ 4ecdc69

Last change on this file since 4ecdc69 was 4ecdc69, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-09-24T19:56:44Z

Fixed an off-by-one memory allocation bug in xt_new_node().

  • Property mode set to 100644
File size: 12.1 KB
RevLine 
[8f243ad]1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  Simple XML (stream) parse tree handling code (Jabber/XMPP, mainly)       *
5*                                                                           *
6*  Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net>                   *
7*                                                                           *
8*  This library is free software; you can redistribute it and/or            *
9*  modify it under the terms of the GNU Lesser General Public               *
10*  License as published by the Free Software Foundation, version            *
11*  2.1.                                                                     *
12*                                                                           *
13*  This library is distributed in the hope that it will be useful,          *
14*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
15*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        *
16*  Lesser General Public License for more details.                          *
17*                                                                           *
18*  You should have received a copy of the GNU Lesser General Public License *
19*  along with this library; if not, write to the Free Software Foundation,  *
20*  Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA           *
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
32static void xt_start_element( GMarkupParseContext *ctx, const gchar *element_name, const gchar **attr_names, const gchar **attr_values, gpointer data, GError **error )
33{
34        struct xt_parser *xt = data;
35        struct xt_node *node = g_new0( struct xt_node, 1 ), *nt;
36        int i;
37       
38        node->parent = xt->cur;
39        node->name = g_strdup( element_name );
40       
41        /* First count the number of attributes */
42        for( i = 0; attr_names[i]; i ++ );
43       
44        /* Then allocate a NULL-terminated array. */
45        node->attr = g_new0( struct xt_attr, i + 1 );
46       
47        /* And fill it, saving one variable by starting at the end. */
48        for( i --; i >= 0; i -- )
49        {
50                node->attr[i].key = g_strdup( attr_names[i] );
51                node->attr[i].value = g_strdup( attr_values[i] );
52        }
53       
54        /* Add it to the linked list of children nodes, if we have a current
55           node yet. */
56        if( xt->cur )
57        {
58                if( xt->cur->children )
59                {
60                        for( nt = xt->cur->children; nt->next; nt = nt->next );
61                        nt->next = node;
62                }
63                else
64                {
65                        xt->cur->children = node;
66                }
67        }
68        else if( xt->root )
69        {
70                /* ERROR situation: A second root-element??? */
71        }
72       
73        /* Now this node will be the new current node. */
74        xt->cur = node;
75        /* And maybe this is the root? */
76        if( xt->root == NULL )
77                xt->root = node;
78}
79
80static void xt_text( GMarkupParseContext *ctx, const gchar *text, gsize text_len, gpointer data, GError **error )
81{
82        struct xt_parser *xt = data;
83        struct xt_node *node = xt->cur;
84       
85        if( node == NULL )
86                return;
87       
88        /* FIXME: Does g_renew also OFFICIALLY accept NULL arguments? */
89        node->text = g_renew( char, node->text, node->text_len + text_len + 1 );
90        memcpy( node->text + node->text_len, text, text_len );
91        node->text_len += text_len;
92        /* Zero termination is always nice to have. */
93        node->text[node->text_len] = 0;
94}
95
96static void xt_end_element( GMarkupParseContext *ctx, const gchar *element_name, gpointer data, GError **error )
97{
98        struct xt_parser *xt = data;
99       
100        xt->cur->flags |= XT_COMPLETE;
101        xt->cur = xt->cur->parent;
102}
103
104GMarkupParser xt_parser_funcs =
105{
106        xt_start_element,
107        xt_end_element,
108        xt_text,
109        NULL,
110        NULL
111};
112
113struct xt_parser *xt_new( gpointer data )
114{
115        struct xt_parser *xt = g_new0( struct xt_parser, 1 );
116       
117        xt->data = data;
118        xt_reset( xt );
119       
120        return xt;
121}
122
123/* Reset the parser, flush everything we have so far. For example, we need
124   this for XMPP when doing TLS/SASL to restart the stream. */
125void xt_reset( struct xt_parser *xt )
126{
127        if( xt->parser )
128                g_markup_parse_context_free( xt->parser );
129       
130        xt->parser = g_markup_parse_context_new( &xt_parser_funcs, 0, xt, NULL );
131       
132        if( xt->root )
133        {
134                xt_free_node( xt->root );
135                xt->root = NULL;
136                xt->cur = NULL;
137        }
138}
139
140/* Feed the parser, don't execute any handler. Returns -1 on errors, 0 on
141   end-of-stream and 1 otherwise. */
142int xt_feed( struct xt_parser *xt, char *text, int text_len )
143{
144        if( !g_markup_parse_context_parse( xt->parser, text, text_len, &xt->gerr ) )
145        {
146                return -1;
147        }
148       
149        return !( xt->root && xt->root->flags & XT_COMPLETE );
150}
151
152/* Find completed nodes and see if a handler has to be called. Passing
153   a node isn't necessary if you want to start at the root, just pass
154   NULL. This second argument is needed for recursive calls. FIXME: Retval? */
155int xt_handle( struct xt_parser *xt, struct xt_node *node )
156{
157        struct xt_node *c;
158        xt_status st;
159        int i;
160       
161        /* Let's just hope xt->root isn't NULL! */
162        if( node == NULL )
163                return xt_handle( xt, xt->root );
164       
165        for( c = node->children; c; c = c->next )
166                if( !xt_handle( xt, c ) )
167                        return 0;
168       
169        if( node->flags & XT_COMPLETE && !( node->flags & XT_SEEN ) )
170        {
171                for( i = 0; xt->handlers[i].func; i ++ )
172                {
173                        /* This one is fun! \o/ */
174                       
175                                                /* If handler.name == NULL it means it should always match. */
176                        if( ( xt->handlers[i].name == NULL || 
177                                                /* If it's not, compare. There should always be a name. */
178                              g_strcasecmp( xt->handlers[i].name, node->name ) == 0 ) &&
179                                                /* If handler.parent == NULL, it's a match. */
180                            ( xt->handlers[i].parent == NULL ||
181                                                /* If there's a parent node, see if the name matches. */
182                              ( node->parent ? g_strcasecmp( xt->handlers[i].parent, node->parent->name ) == 0 : 
183                                                /* If there's no parent, the handler should mention <root> as a parent. */
184                                               g_strcasecmp( xt->handlers[i].parent, "<root>" ) == 0 ) ) )
185                        {
186                                st = xt->handlers[i].func( node, xt->data );
187                               
188                                if( st == XT_ABORT )
189                                        return 0;
190                                else if( st != XT_NEXT )
191                                        break;
192                        }
193                }
194               
195                node->flags |= XT_SEEN;
196        }
197       
198        return 1;
199}
200
201/* Garbage collection: Cleans up all nodes that are handled. Useful for
202   streams because there's no reason to keep a complete packet history
203   in memory. */
204void xt_cleanup( struct xt_parser *xt, struct xt_node *node )
205{
206        struct xt_node *c, *prev;
207       
[21167d2]208        if( !xt || !xt->root )
209                return;
210       
[8f243ad]211        if( node == NULL )
212                return xt_cleanup( xt, xt->root );
213       
214        if( node->flags & XT_SEEN && node == xt->root )
215        {
216                xt_free_node( xt->root );
217                xt->root = xt->cur = NULL;
218                /* xt->cur should be NULL already, BTW... */
219               
220                return;
221        }
222       
223        /* c contains the current node, prev the previous node (or NULL).
224           I admit, this one's pretty horrible. */
225        for( c = node->children, prev = NULL; c; prev = c, c = c ? c->next : node->children )
226        {
227                if( c->flags & XT_SEEN )
228                {
229                        /* Remove the node from the linked list. */
230                        if( prev )
231                                prev->next = c->next;
232                        else
233                                node->children = c->next;
234                       
235                        xt_free_node( c );
236                       
237                        /* Since the for loop wants to get c->next, make sure
238                           c points at something that exists (and that c->next
239                           will actually be the next item we should check). c
240                           can be NULL now, if we just removed the first item.
241                           That explains the ? thing in for(). */
242                        c = prev;
243                }
244                else
245                {
246                        /* This node can't be cleaned up yet, but maybe a
247                           subnode can. */
248                        xt_cleanup( xt, c );
249                }
250        }
251}
252
253static void xt_to_string_real( struct xt_node *node, GString *str )
254{
255        char *buf;
256        struct xt_node *c;
257        int i;
258       
259        g_string_append_printf( str, "<%s", node->name );
260       
261        for( i = 0; node->attr[i].key; i ++ )
262        {
263                buf = g_markup_printf_escaped( " %s=\"%s\"", node->attr[i].key, node->attr[i].value );
264                g_string_append( str, buf );
265                g_free( buf );
266        }
267       
268        if( node->text == NULL && node->children == NULL )
269        {
270                g_string_append( str, "/>" );
271                return;
272        }
273       
274        g_string_append( str, ">" );
275        if( node->text_len > 0 )
276        {
277                buf = g_markup_escape_text( node->text, node->text_len );
278                g_string_append( str, buf );
279                g_free( buf );
280        }
281       
282        for( c = node->children; c; c = c->next )
283                xt_to_string_real( c, str );
284       
285        g_string_append_printf( str, "</%s>", node->name );
286}
287
288char *xt_to_string( struct xt_node *node )
289{
290        GString *ret;
291        char *real;
292       
293        ret = g_string_new( "" );
294        xt_to_string_real( node, ret );
295       
296        real = ret->str;
297        g_string_free( ret, FALSE );
298       
299        return real;
300}
301
302void xt_print( struct xt_node *node )
303{
304        int i;
305        struct xt_node *c;
306       
307        /* Indentation */
308        for( c = node; c->parent; c = c->parent )
309                printf( "\t" );
310       
311        /* Start the tag */
312        printf( "<%s", node->name );
313       
314        /* Print the attributes */
315        for( i = 0; node->attr[i].key; i ++ )
316                printf( " %s=\"%s\"", node->attr[i].key, g_markup_escape_text( node->attr[i].value, -1 ) );
317       
318        /* /> in case there's really *nothing* inside this tag, otherwise
319           just >. */
320        /* If this tag doesn't have any content at all... */
321        if( node->text == NULL && node->children == NULL )
322        {
323                printf( "/>\n" );
324                return;
325                /* Then we're finished! */
326        }
327       
328        /* Otherwise... */
329        printf( ">" );
330       
331        /* Only print the text if it contains more than whitespace (TEST). */
332        if( node->text_len > 0 )
333        {
334                for( i = 0; node->text[i] && isspace( node->text[i] ); i ++ );
335                if( node->text[i] )
336                        printf( "%s", g_markup_escape_text( node->text, -1 ) );
337        }
338       
339        if( node->children )
340                printf( "\n" );
341       
342        for( c = node->children; c; c = c->next )
343                xt_print( c );
344       
345        if( node->children )
346                for( c = node; c->parent; c = c->parent )
347                        printf( "\t" );
348       
349        /* Non-empty tag is now finished. */
350        printf( "</%s>\n", node->name );
351}
352
353/* Frees a node. This doesn't clean up references to itself from parents! */
354void xt_free_node( struct xt_node *node )
355{
356        int i;
357       
[21167d2]358        if( !node )
359                return;
360       
[8f243ad]361        g_free( node->name );
362        g_free( node->text );
363       
364        for( i = 0; node->attr[i].key; i ++ )
365        {
366                g_free( node->attr[i].key );
367                g_free( node->attr[i].value );
368        }
369        g_free( node->attr );
370       
371        while( node->children )
372        {
373                struct xt_node *next = node->children->next;
374               
375                xt_free_node( node->children );
376                node->children = next;
377        }
378       
379        g_free( node );
380}
381
382void xt_free( struct xt_parser *xt )
383{
[21167d2]384        if( !xt )
385                return;
386       
[8f243ad]387        if( xt->root )
388                xt_free_node( xt->root );
389       
390        g_markup_parse_context_free( xt->parser );
391       
392        g_free( xt );
393}
394
395/* To find a node's child with a specific name, pass the node's children
396   list, not the node itself! The reason you have to do this by hand: So
397   that you can also use this function as a find-next. */
398struct xt_node *xt_find_node( struct xt_node *node, char *name )
399{
400        while( node )
401        {
402                if( g_strcasecmp( node->name, name ) == 0 )
403                        break;
404               
405                node = node->next;
406        }
407       
408        return node;
409}
410
411char *xt_find_attr( struct xt_node *node, char *key )
412{
413        int i;
414       
[21167d2]415        if( !node )
416                return NULL;
417       
[8f243ad]418        for( i = 0; node->attr[i].key; i ++ )
419                if( g_strcasecmp( node->attr[i].key, key ) == 0 )
420                        break;
421       
422        return node->attr[i].value;
423}
424
425struct xt_node *xt_new_node( char *name, char *text, struct xt_node *children )
426{
427        struct xt_node *node, *c;
428       
429        node = g_new0( struct xt_node, 1 );
430        node->name = g_strdup( name );
431        node->children = children;
432        node->attr = g_new0( struct xt_attr, 1 );
433       
434        if( text )
435        {
436                node->text_len = strlen( text );
[4ecdc69]437                node->text = g_memdup( text, node->text_len + 1 );
[8f243ad]438        }
439       
440        for( c = children; c; c = c->next )
441        {
442                if( c->parent != NULL )
443                {
444                        /* ERROR CONDITION: They seem to have a parent already??? */
445                }
446               
447                c->parent = node;
448        }
449       
450        return node;
451}
452
453void xt_add_child( struct xt_node *parent, struct xt_node *child )
454{
455        struct xt_node *node;
456       
457        /* This function can actually be used to add more than one child, so
458           do handle this properly. */
459        for( node = child; node; node = node->next )
460        {
461                if( node->parent != NULL )
462                {
463                        /* ERROR CONDITION: They seem to have a parent already??? */
464                }
465               
466                node->parent = parent;
467        }
468       
469        if( parent->children == NULL )
470        {
471                parent->children = child;
472        }
473        else
474        {
475                for( node = parent->children; node->next; node = node->next );
476                node->next = child;
477        }
478}
479
480void xt_add_attr( struct xt_node *node, char *key, char *value )
481{
482        int i;
483       
484        for( i = 0; node->attr[i].key; i ++ );
485        node->attr = g_renew( struct xt_attr, node->attr, i + 2 );
486        node->attr[i].key = g_strdup( key );
487        node->attr[i].value = g_strdup( value );
488        node->attr[i+1].key = NULL;
489}
Note: See TracBrowser for help on using the repository browser.