[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / jabber / libxode.h
blob81841e9a0fca0c3b5bcc786dafd6b018b944a45d
1 #include <string.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <stdio.h>
5 #include <setjmp.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <signal.h>
10 #include <syslog.h>
11 #include <strings.h>
12 #include <unistd.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <netdb.h>
16 #include <arpa/inet.h>
17 #include <arpa/nameser.h>
18 #include <resolv.h>
19 #include <sys/time.h>
20 #include <time.h>
22 #include "xmlparse.h"
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif /* HAVE_CONFIG_H */
28 ** Arrange to use either varargs or stdargs
31 #define MAXSHORTSTR 203 /* max short string length */
32 #define QUAD_T unsigned long long
34 #ifdef __STDC__
36 #include <stdarg.h>
38 # define VA_LOCAL_DECL va_list ap;
39 # define VA_START(f) va_start(ap, f)
40 # define VA_END va_end(ap)
42 #else /* __STDC__ */
44 # include <varargs.h>
46 # define VA_LOCAL_DECL va_list ap;
47 # define VA_START(f) va_start(ap)
48 # define VA_END va_end(ap)
50 #endif /* __STDC__ */
53 #ifndef INCL_LIBXODE_H
54 #define INCL_LIBXODE_H
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
61 #ifndef HAVE_SNPRINTF
62 extern int ap_snprintf(char *, size_t, const char *, ...);
63 #define snprintf ap_snprintf
64 #endif
66 #ifndef HAVE_VSNPRINTF
67 extern int ap_vsnprintf(char *, size_t, const char *, va_list ap);
68 #define vsnprintf ap_vsnprintf
69 #endif
71 #define ZONE zonestr(__FILE__,__LINE__)
72 char *zonestr(char *file, int line);
74 /* --------------------------------------------------------- */
75 /* */
76 /* Pool-based memory management routines */
77 /* */
78 /* --------------------------------------------------------- */
80 #undef POOL_DEBUG
82 flip these, this should be a prime number for top # of pools debugging
83 #define POOL_DEBUG 40009
86 /* pheap - singular allocation of memory */
87 struct pheap
89 void *block;
90 int size, used;
93 /* pool_cleaner - callback type which is associated
94 with a pool entry; invoked when the pool entry is
95 free'd */
96 typedef void (*pool_cleaner)(void *arg);
98 /* pfree - a linked list node which stores an
99 allocation chunk, plus a callback */
100 struct pfree
102 pool_cleaner f;
103 void *arg;
104 struct pheap *heap;
105 struct pfree *next;
108 /* pool - base node for a pool. Maintains a linked list
109 of pool entries (pfree) */
110 typedef struct pool_struct
112 int size;
113 struct pfree *cleanup;
114 struct pheap *heap;
115 #ifdef POOL_DEBUG
116 char name[8], zone[32];
117 int lsize;
118 } _pool, *pool;
119 #define pool_new() _pool_new(ZONE)
120 #define pool_heap(i) _pool_new_heap(i,ZONE)
121 #else
122 } _pool, *pool;
123 #define pool_heap(i) _pool_new_heap(i,NULL)
124 #define pool_new() _pool_new(NULL)
125 #endif
127 pool _pool_new(char *zone); /* new pool :) */
128 pool _pool_new_heap(int size, char *zone); /* creates a new memory pool with an initial heap size */
129 void *pmalloc(pool p, int size); /* wrapper around malloc, takes from the pool, cleaned up automatically */
130 void *pmalloc_x(pool p, int size, char c); /* Wrapper around pmalloc which prefils buffer with c */
131 void *pmalloco(pool p, int size); /* YAPW for zeroing the block */
132 char *pstrdup(pool p, const char *src); /* wrapper around strdup, gains mem from pool */
133 void pool_stat(int full); /* print to stderr the changed pools and reset */
134 char *pstrdupx(pool p, const char *src); /* temp stub */
135 void pool_cleanup(pool p, pool_cleaner f, void *arg); /* calls f(arg) before the pool is freed during cleanup */
136 void pool_free(pool p); /* calls the cleanup functions, frees all the data on the pool, and deletes the pool itself */
137 int pool_size(pool p); /* returns total bytes allocated in this pool */
142 /* --------------------------------------------------------- */
143 /* */
144 /* Socket helper stuff */
145 /* */
146 /* --------------------------------------------------------- */
147 #ifndef MAXHOSTNAMELEN
148 #define MAXHOSTNAMELEN 64
149 #endif
151 #define NETSOCKET_SERVER 0
152 #define NETSOCKET_CLIENT 1
153 #define NETSOCKET_UDP 2
155 #ifndef WIN32
156 int make_netsocket(u_short port, char *host, int type);
157 struct in_addr *make_addr(char *host);
158 int set_fd_close_on_exec(int fd, int flag);
159 #endif
162 /* --------------------------------------------------------- */
163 /* */
164 /* String management routines */
165 /* */
166 /* --------------------------------------------------------- */
167 char *j_strdup(const char *str); /* provides NULL safe strdup wrapper */
168 char *j_strcat(char *dest, char *txt); /* strcpy() clone */
169 int j_strcmp(const char *a, const char *b); /* provides NULL safe strcmp wrapper */
170 int j_strcasecmp(const char *a, const char *b); /* provides NULL safe strcasecmp wrapper */
171 int j_strncmp(const char *a, const char *b, int i); /* provides NULL safe strncmp wrapper */
172 int j_strncasecmp(const char *a, const char *b, int i); /* provides NULL safe strncasecmp wrapper */
173 int j_strlen(const char *a); /* provides NULL safe strlen wrapper */
174 int j_atoi(const char *a, int def); /* checks for NULL and uses default instead, convienence */
175 void str_b64decode(char *str); /* what it says */
178 /* --------------------------------------------------------- */
179 /* */
180 /* SHA calculations */
181 /* */
182 /* --------------------------------------------------------- */
183 #if (SIZEOF_INT == 4)
184 typedef unsigned int uint32;
185 #elif (SIZEOF_SHORT == 4)
186 typedef unsigned short uint32;
187 #else
188 typedef unsigned int uint32;
189 #endif /* HAVEUINT32 */
191 int sha_hash(int *data, int *hash);
192 int sha_init(int *hash);
193 char *shahash(char *str); /* NOT THREAD SAFE */
194 void shahash_r(const char* str, char hashbuf[40]); /* USE ME */
196 int strprintsha(char *dest, int *hashval);
199 /* --------------------------------------------------------- */
200 /* */
201 /* Hashtable functions */
202 /* */
203 /* --------------------------------------------------------- */
204 typedef int (*KEYHASHFUNC)(const void *key);
205 typedef int (*KEYCOMPAREFUNC)(const void *key1, const void *key2);
206 typedef int (*TABLEWALKFUNC)(void *user_data, const void *key, void *data);
208 typedef void *HASHTABLE;
210 HASHTABLE ghash_create(int buckets, KEYHASHFUNC hash, KEYCOMPAREFUNC cmp);
211 void ghash_destroy(HASHTABLE tbl);
212 void *ghash_get(HASHTABLE tbl, const void *key);
213 int ghash_put(HASHTABLE tbl, const void *key, void *value);
214 int ghash_remove(HASHTABLE tbl, const void *key);
215 int ghash_walk(HASHTABLE tbl, TABLEWALKFUNC func, void *user_data);
216 int str_hash_code(const char *s);
219 /* --------------------------------------------------------- */
220 /* */
221 /* XML escaping utils */
222 /* */
223 /* --------------------------------------------------------- */
224 char *strescape(pool p, char *buf); /* Escape <>&'" chars */
225 char *strunescape(pool p, char *buf);
228 /* --------------------------------------------------------- */
229 /* */
230 /* String pools (spool) functions */
231 /* */
232 /* --------------------------------------------------------- */
233 struct spool_node
235 char *c;
236 struct spool_node *next;
239 typedef struct spool_struct
241 pool p;
242 int len;
243 struct spool_node *last;
244 struct spool_node *first;
245 } *spool;
247 spool spool_new(pool p); /* create a string pool */
248 void spooler(spool s, ...); /* append all the char * args to the pool, terminate args with s again */
249 char *spool_print(spool s); /* return a big string */
250 void spool_add(spool s, char *str); /* add a single char to the pool */
251 char *spools(pool p, ...); /* wrap all the spooler stuff in one function, the happy fun ball! */
254 /* --------------------------------------------------------- */
255 /* */
256 /* xmlnodes - Document Object Model */
257 /* */
258 /* --------------------------------------------------------- */
259 #define NTYPE_TAG 0
260 #define NTYPE_ATTRIB 1
261 #define NTYPE_CDATA 2
263 #define NTYPE_LAST 2
264 #define NTYPE_UNDEF -1
266 /* --------------------------------------------------------------------------
267 Node structure. Do not use directly! Always use accessor macros
268 and methods!
269 -------------------------------------------------------------------------- */
270 typedef struct xmlnode_t
272 char* name;
273 unsigned short type;
274 char* data;
275 int data_sz;
276 int complete;
277 pool p;
278 struct xmlnode_t* parent;
279 struct xmlnode_t* firstchild;
280 struct xmlnode_t* lastchild;
281 struct xmlnode_t* prev;
282 struct xmlnode_t* next;
283 struct xmlnode_t* firstattrib;
284 struct xmlnode_t* lastattrib;
285 } _xmlnode, *xmlnode;
287 /* Node creation routines */
288 xmlnode xmlnode_wrap(xmlnode x,const char* wrapper);
289 xmlnode xmlnode_new_tag(const char* name);
290 xmlnode xmlnode_new_tag_pool(pool p, const char* name);
291 xmlnode xmlnode_insert_tag(xmlnode parent, const char* name);
292 xmlnode xmlnode_insert_cdata(xmlnode parent, const char* CDATA, unsigned int size);
293 xmlnode xmlnode_insert_tag_node(xmlnode parent, xmlnode node);
294 void xmlnode_insert_node(xmlnode parent, xmlnode node);
295 xmlnode xmlnode_str(char *str, int len);
296 xmlnode xmlnode_file(char *file);
297 xmlnode xmlnode_dup(xmlnode x); /* duplicate x */
298 xmlnode xmlnode_dup_pool(pool p, xmlnode x);
300 /* Node Memory Pool */
301 pool xmlnode_pool(xmlnode node);
302 xmlnode _xmlnode_new(pool p, const char *name, unsigned int type);
304 /* Node editing */
305 void xmlnode_hide(xmlnode child);
306 void xmlnode_hide_attrib(xmlnode parent, const char *name);
308 /* Node deletion routine, also frees the node pool! */
309 void xmlnode_free(xmlnode node);
311 /* Locates a child tag by name and returns it */
312 xmlnode xmlnode_get_tag(xmlnode parent, const char* name);
313 char* xmlnode_get_tag_data(xmlnode parent, const char* name);
315 /* Attribute accessors */
316 void xmlnode_put_attrib(xmlnode owner, const char* name, const char* value);
317 char* xmlnode_get_attrib(xmlnode owner, const char* name);
318 void xmlnode_put_expat_attribs(xmlnode owner, const char** atts);
320 /* Bastard am I, but these are fun for internal use ;-) */
321 void xmlnode_put_vattrib(xmlnode owner, const char* name, void *value);
322 void* xmlnode_get_vattrib(xmlnode owner, const char* name);
324 /* Node traversal routines */
325 xmlnode xmlnode_get_firstattrib(xmlnode parent);
326 xmlnode xmlnode_get_firstchild(xmlnode parent);
327 xmlnode xmlnode_get_lastchild(xmlnode parent);
328 xmlnode xmlnode_get_nextsibling(xmlnode sibling);
329 xmlnode xmlnode_get_prevsibling(xmlnode sibling);
330 xmlnode xmlnode_get_parent(xmlnode node);
332 /* Node information routines */
333 char* xmlnode_get_name(xmlnode node);
334 char* xmlnode_get_data(xmlnode node);
335 int xmlnode_get_datasz(xmlnode node);
336 int xmlnode_get_type(xmlnode node);
338 int xmlnode_has_children(xmlnode node);
339 int xmlnode_has_attribs(xmlnode node);
341 /* Node-to-string translation */
342 char* xmlnode2str(xmlnode node);
344 /* Node-to-terminated-string translation
345 -- useful for interfacing w/ scripting langs */
346 char* xmlnode2tstr(xmlnode node);
348 int xmlnode_cmp(xmlnode a, xmlnode b); /* compares a and b for equality */
350 int xmlnode2file(char *file, xmlnode node); /* writes node to file */
352 /* Expat callbacks */
353 void expat_startElement(void* userdata, const char* name, const char** atts);
354 void expat_endElement(void* userdata, const char* name);
355 void expat_charData(void* userdata, const char* s, int len);
357 /***********************
358 * XSTREAM Section
359 ***********************/
361 #define XSTREAM_MAXNODE 1000000
362 #define XSTREAM_MAXDEPTH 100
364 #define XSTREAM_ROOT 0 /* root element */
365 #define XSTREAM_NODE 1 /* normal node */
366 #define XSTREAM_CLOSE 2 /* closed </stream:stream> */
367 #define XSTREAM_ERR 4 /* parser error */
369 typedef void (*xstream_onNode)(int type, xmlnode x, void *arg); /* xstream event handler */
371 typedef struct xstream_struct
373 XML_Parser parser;
374 xmlnode node;
375 char *cdata;
376 int cdata_len;
377 pool p;
378 xstream_onNode f;
379 void *arg;
380 int status;
381 int depth;
382 } *xstream, _xstream;
384 xstream xstream_new(pool p, xstream_onNode f, void *arg); /* create a new xstream */
385 int xstream_eat(xstream xs, char *buff, int len); /* parse new data for this xstream, returns last XSTREAM_* status */
387 /* convience functions */
388 xmlnode xstream_header(char *namespace, char *to, char *from);
389 char *xstream_header_char(xmlnode x);
391 /* SHA.H */
393 * The contents of this file are subject to the Mozilla Public
394 * License Version 1.1 (the "License"); you may not use this file
395 * except in compliance with the License. You may obtain a copy of
396 * the License at http://www.mozilla.org/MPL/
398 * Software distributed under the License is distributed on an "AS
399 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
400 * implied. See the License for the specific language governing
401 * rights and limitations under the License.
403 * The Original Code is SHA 180-1 Header File
405 * The Initial Developer of the Original Code is Paul Kocher of
406 * Cryptography Research. Portions created by Paul Kocher are
407 * Copyright (C) 1995-9 by Cryptography Research, Inc. All
408 * Rights Reserved.
410 * Contributor(s):
412 * Paul Kocher
414 * Alternatively, the contents of this file may be used under the
415 * terms of the GNU General Public License Version 2 or later (the
416 * "GPL"), in which case the provisions of the GPL are applicable
417 * instead of those above. If you wish to allow use of your
418 * version of this file only under the terms of the GPL and not to
419 * allow others to use your version of this file under the MPL,
420 * indicate your decision by deleting the provisions above and
421 * replace them with the notice and other provisions required by
422 * the GPL. If you do not delete the provisions above, a recipient
423 * may use your version of this file under either the MPL or the
424 * GPL.
427 typedef struct {
428 unsigned long H[5];
429 unsigned long W[80];
430 int lenW;
431 unsigned long sizeHi,sizeLo;
432 } SHA_CTX;
435 void shaInit(SHA_CTX *ctx);
436 void shaUpdate(SHA_CTX *ctx, unsigned char *dataIn, int len);
437 void shaFinal(SHA_CTX *ctx, unsigned char hashout[20]);
438 void shaBlock(unsigned char *dataIn, int len, unsigned char hashout[20]);
441 /* END SHA.H */
443 #ifdef __cplusplus
445 #endif
447 #endif /* INCL_LIBXODE_H */