1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or
5  *  (at your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful,
8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *  GNU General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
15  *
16  *  Jabber
17  *  Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
18  */
19 
20 /*! \file
21  * \ingroup xmpp
22  */
23 
24 #include <string.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <syslog.h>
30 #include <strings.h>
31 #include <unistd.h>
32 #include <sys/time.h>
33 
34 #include "expat.h"
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif /* HAVE_CONFIG_H */
38 
39 /*
40 **  Arrange to use either varargs or stdargs
41 */
42 
43 #define MAXSHORTSTR	203		/* max short string length */
44 #define QUAD_T	unsigned long long
45 
46 #ifdef __STDC__
47 
48 #include <stdarg.h>
49 
50 # define VA_LOCAL_DECL	va_list ap;
51 # define VA_START(f)	va_start(ap, f)
52 # define VA_END		va_end(ap)
53 
54 #else /* __STDC__ */
55 
56 # include <varargs.h>
57 
58 # define VA_LOCAL_DECL	va_list ap;
59 # define VA_START(f)	va_start(ap)
60 # define VA_END		va_end(ap)
61 
62 #endif /* __STDC__ */
63 
64 
65 #ifndef INCL_LIBXODE_H
66 #define INCL_LIBXODE_H
67 
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71 
72 #ifndef __OS_darwin
73 #ifndef HAVE_SNPRINTF
74 extern int ap_snprintf(char *, size_t, const char *, ...);
75 #define snprintf ap_snprintf
76 #endif
77 
78 #ifndef HAVE_VSNPRINTF
79 extern int ap_vsnprintf(char *, size_t, const char *, va_list ap);
80 #define vsnprintf ap_vsnprintf
81 #endif
82 #endif
83 
84 /* --------------------------------------------------------- */
85 /*                                                           */
86 /* Pool-based memory management routines                     */
87 /*                                                           */
88 /* --------------------------------------------------------- */
89 
90 
91 /* xode_pool_cleaner - callback type which is associated
92    with a pool entry; invoked when the pool entry is
93    free'd */
94 typedef void (*xode_pool_cleaner)(void *arg);
95 
96 
97 /* pheap - singular allocation of memory */
98 struct xode_pool_heap
99 {
100     void *block;
101     int size, used;
102 };
103 
104 /* pool - base node for a pool. Maintains a linked list
105    of pool entries (pool_free) */
106 typedef struct xode_pool_struct
107 {
108     int size;
109     struct xode_pool_free *cleanup;
110     struct xode_pool_heap *heap;
111 } _xode_pool, *xode_pool;
112 
113 /* pool creation routines */
114 xode_pool xode_pool_heap(int bytes);
115 xode_pool xode_pool_new(void);
116 
117 /* pool wrappers for malloc */
118 void *xode_pool_malloc  (xode_pool p, int size);
119 void *xode_pool_mallocx (xode_pool p, int size, char c);
120 void *xode_pool_malloco (xode_pool p, int size);
121 
122 /* wrapper around strdup, gains mem from pool */
123 char *xode_pool_strdup  (xode_pool p, const char *src);
124 
125 /* calls f(arg) before the pool is freed during cleanup */
126 void xode_pool_cleanup  (xode_pool p, xode_pool_cleaner f, void *arg);
127 
128 /* pool wrapper for free, called on a pool */
129 void xode_pool_free     (xode_pool p);
130 
131 /* returns total bytes allocated in this pool */
132 int  xode_pool_size     (xode_pool p);
133 
134 /* --------------------------------------------------------- */
135 /*                                                           */
136 /* XML escaping utils                                        */
137 /*                                                           */
138 /* --------------------------------------------------------- */
139 char *xode_strescape(xode_pool p, char *buf); /* Escape <>&'" chars */
140 char *xode_strunescape(xode_pool p, char *buf);
141 
142 
143 /* --------------------------------------------------------- */
144 /*                                                           */
145 /* String pools (spool) functions                            */
146 /*                                                           */
147 /* --------------------------------------------------------- */
148 struct xode_spool_node
149 {
150     char *c;
151     struct xode_spool_node *next;
152 };
153 
154 typedef struct xode_spool_struct
155 {
156     xode_pool p;
157     int len;
158     struct xode_spool_node *last;
159     struct xode_spool_node *first;
160 } *xode_spool;
161 
162 xode_spool xode_spool_new         ( void                          ); /* create a string pool on a new pool */
163 xode_spool xode_spool_newfrompool ( xode_pool        p            ); /* create a string pool from an existing pool */
164 xode_pool  xode_spool_getpool     ( const xode_spool s            ); /* returns the xode_pool used by this xode_spool */
165 void       xode_spooler           ( xode_spool       s, ...       ); /* append all the char * args to the pool, terminate args with s again */
166 char       *xode_spool_tostr      ( xode_spool       s            ); /* return a big string */
167 void       xode_spool_add         ( xode_spool       s, char *str ); /* add a single char to the pool */
168 char       *xode_spool_str        ( xode_pool        p, ...       ); /* wrap all the spooler stuff in one function, the happy fun ball! */
169 int        xode_spool_getlen      ( const xode_spool s            ); /* returns the total length of the string contained in the pool */
170 void       xode_spool_free        ( xode_spool       s            ); /* Free's the pool associated with the xode_spool */
171 
172 
173 /* --------------------------------------------------------- */
174 /*                                                           */
175 /* xodes - Document Object Model                          */
176 /*                                                           */
177 /* --------------------------------------------------------- */
178 #define XODE_TYPE_TAG    0
179 #define XODE_TYPE_ATTRIB 1
180 #define XODE_TYPE_CDATA  2
181 
182 #define XODE_TYPE_LAST   2
183 #define XODE_TYPE_UNDEF  -1
184 
185 /* --------------------------------------------------------------------------
186    Node structure. Do not use directly! Always use accessors macros
187    and methods!
188    -------------------------------------------------------------------------- */
189 typedef struct xode_struct
190 {
191      char*                name;
192      unsigned short       type;
193      char*                data;
194      int                  data_sz;
195      int                  complete;
196      xode_pool            p;
197      struct xode_struct*  parent;
198      struct xode_struct*  firstchild;
199      struct xode_struct*  lastchild;
200      struct xode_struct*  prev;
201      struct xode_struct*  next;
202      struct xode_struct*  firstattrib;
203      struct xode_struct*  lastattrib;
204 } _xode, *xode;
205 
206 /* Node creation routines */
207 xode  xode_wrap(xode x,const char* wrapper);
208 xode  xode_new(const char* name);
209 xode  xode_new_tag(const char* name);
210 xode  xode_new_frompool(xode_pool p, const char* name);
211 xode  xode_insert_tag(xode parent, const char* name);
212 xode  xode_insert_cdata(xode parent, const char* CDATA, unsigned int size);
213 xode  xode_insert_tagnode(xode parent, xode node);
214 void  xode_insert_node(xode parent, xode node);
215 xode  xode_from_str(char *str, int len);
216 xode  xode_from_strx(char *str, int len, int *err, int *pos);
217 xode  xode_from_file(char *file);
218 xode  xode_dup(xode x); /* duplicate x */
219 xode  xode_dup_frompool(xode_pool p, xode x);
220 
221 /* Node Memory Pool */
222 xode_pool xode_get_pool(xode node);
223 
224 /* Node editing */
225 void xode_hide(xode child);
226 void xode_hide_attrib(xode parent, const char *name);
227 
228 /* Node deletion routine, also frees the node pool! */
229 void xode_free(xode node);
230 
231 /* Locates a child tag by name and returns it */
232 xode  xode_get_tag(xode parent, const char* name);
233 char* xode_get_tagdata(xode parent, const char* name);
234 
235 /* Attribute accessors */
236 void     xode_put_attrib(xode owner, const char* name, const char* value);
237 char*    xode_get_attrib(xode owner, const char* name);
238 
239 /* Bastard am I, but these are fun for internal use ;-) */
240 void     xode_put_vattrib(xode owner, const char* name, void *value);
241 void*    xode_get_vattrib(xode owner, const char* name);
242 
243 /* Node traversal routines */
244 xode  xode_get_firstattrib(xode parent);
245 xode  xode_get_firstchild(xode parent);
246 xode  xode_get_lastchild(xode parent);
247 xode  xode_get_nextsibling(xode sibling);
248 xode  xode_get_prevsibling(xode sibling);
249 xode  xode_get_parent(xode node);
250 
251 /* Node information routines */
252 char*    xode_get_name(xode node);
253 char*    xode_get_data(xode node);
254 int      xode_get_datasz(xode node);
255 int      xode_get_type(xode node);
256 
257 int      xode_has_children(xode node);
258 int      xode_has_attribs(xode node);
259 
260 /* Node-to-string translation */
261 char*    xode_to_str(xode node);
262 char*    xode_to_prettystr(xode node);  /* Puts \t and \n to make a human-easily readable string */
263 
264 int      xode_cmp(xode a, xode b); /* compares a and b for equality */
265 
266 int      xode_to_file(char *file, xode node); /* writes node to file */
267 
268 
269 /***********************
270  * XSTREAM Section
271  ***********************/
272 
273 #define XODE_STREAM_MAXNODE 1000000
274 #define XODE_STREAM_MAXDEPTH 100
275 
276 #define XODE_STREAM_ROOT        0 /* root element */
277 #define XODE_STREAM_NODE        1 /* normal node */
278 #define XODE_STREAM_CLOSE       2 /* closed root node */
279 #define XODE_STREAM_ERROR       4 /* parser error */
280 
281 typedef void (*xode_stream_onNode)(int type, xode x, void *arg); /* xstream event handler */
282 
283 typedef struct xode_stream_struct
284 {
285     XML_Parser parser;
286     xode node;
287     char *cdata;
288     int cdata_len;
289     xode_pool p;
290     xode_stream_onNode f;
291     void *arg;
292     int status;
293     int depth;
294 } *xode_stream, _xode_stream;
295 
296 xode_stream xode_stream_new(xode_pool p, xode_stream_onNode f, void *arg); /* create a new xstream */
297 int xode_stream_eat(xode_stream xs, char *buff, int len); /* parse new data for this xstream, returns last XSTREAM_* status */
298 
299 /* convenience functions */
300 
301 #ifdef __cplusplus
302 }
303 #endif
304 
305 #endif /* INCL_LIBXODE_H */
306