source: protocols/jabber/libxode.h @ d3307e2

Last change on this file since d3307e2 was b7d3cc34, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-11-06T18:23:18Z

Initial repository (0.99 release tree)

  • Property mode set to 100644
File size: 12.6 KB
RevLine 
[b7d3cc34]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
18#ifndef __CYGWIN__
19#include <arpa/nameser.h>
20#include <resolv.h>
21#endif
22
23#include <sys/time.h>
24#include <time.h>
25
26#include "xmlparse.h"
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif /* HAVE_CONFIG_H */
30
31/*
32**  Arrange to use either varargs or stdargs
33*/
34
35#define MAXSHORTSTR     203             /* max short string length */
36#define QUAD_T  unsigned long long
37
38#ifdef __STDC__
39
40#include <stdarg.h>
41
42# define VA_LOCAL_DECL  va_list ap;
43# define VA_START(f)    va_start(ap, f)
44# define VA_END         va_end(ap)
45
46#else /* __STDC__ */
47
48# include <varargs.h>
49
50# define VA_LOCAL_DECL  va_list ap;
51# define VA_START(f)    va_start(ap)
52# define VA_END         va_end(ap)
53
54#endif /* __STDC__ */
55
56
57#ifndef INCL_LIBXODE_H
58#define INCL_LIBXODE_H
59
60#ifdef __cplusplus
61extern "C" {
62#endif
63
64
65#ifndef HAVE_SNPRINTF
66extern int ap_snprintf(char *, size_t, const char *, ...);
67#define snprintf ap_snprintf
68#endif
69
70#ifndef HAVE_VSNPRINTF
71extern int ap_vsnprintf(char *, size_t, const char *, va_list ap);
72#define vsnprintf ap_vsnprintf
73#endif
74
75#define ZONE zonestr(__FILE__,__LINE__)
76char *zonestr(char *file, int line);
77
78/* --------------------------------------------------------- */
79/*                                                           */
80/* Pool-based memory management routines                     */
81/*                                                           */
82/* --------------------------------------------------------- */
83
84#undef POOL_DEBUG
85/*
86 flip these, this should be a prime number for top # of pools debugging
87#define POOL_DEBUG 40009
88*/
89
90/* pheap - singular allocation of memory */
91struct pheap
92{
93    void *block;
94    int size, used;
95};
96
97/* pool_cleaner - callback type which is associated
98   with a pool entry; invoked when the pool entry is
99   free'd */
100typedef void (*pool_cleaner)(void *arg);
101
102/* pfree - a linked list node which stores an
103   allocation chunk, plus a callback */
104struct pfree
105{
106    pool_cleaner f;
107    void *arg;
108    struct pheap *heap;
109    struct pfree *next;
110};
111
112/* pool - base node for a pool. Maintains a linked list
113   of pool entries (pfree) */
114typedef struct pool_struct
115{
116    int size;
117    struct pfree *cleanup;
118    struct pheap *heap;
119#ifdef POOL_DEBUG
120    char name[8], zone[32];
121    int lsize;
122} _pool, *pool;
123#define pool_new() _pool_new(ZONE)
124#define pool_heap(i) _pool_new_heap(i,ZONE)
125#else
126} _pool, *pool;
127#define pool_heap(i) _pool_new_heap(i,NULL)
128#define pool_new() _pool_new(NULL)
129#endif
130
131pool _pool_new(char *zone); /* new pool :) */
132pool _pool_new_heap(int size, char *zone); /* creates a new memory pool with an initial heap size */
133void *pmalloc(pool p, int size); /* wrapper around malloc, takes from the pool, cleaned up automatically */
134void *pmalloc_x(pool p, int size, char c); /* Wrapper around pmalloc which prefils buffer with c */
135void *pmalloco(pool p, int size); /* YAPW for zeroing the block */
136char *pstrdup(pool p, const char *src); /* wrapper around strdup, gains mem from pool */
137void pool_stat(int full); /* print to stderr the changed pools and reset */
138void pool_cleanup(pool p, pool_cleaner f, void *arg); /* calls f(arg) before the pool is freed during cleanup */
139void pool_free(pool p); /* calls the cleanup functions, frees all the data on the pool, and deletes the pool itself */
140
141
142
143
144/* --------------------------------------------------------- */
145/*                                                           */
146/* Socket helper stuff                                       */
147/*                                                           */
148/* --------------------------------------------------------- */
149#ifndef MAXHOSTNAMELEN
150#define MAXHOSTNAMELEN 64
151#endif
152
153#define NETSOCKET_SERVER 0
154#define NETSOCKET_CLIENT 1
155#define NETSOCKET_UDP 2
156
157#ifndef WIN32
158int make_netsocket(u_short port, char *host, int type);
159struct in_addr *make_addr(char *host);
160int set_fd_close_on_exec(int fd, int flag);
161#endif
162
163
164/* --------------------------------------------------------- */
165/*                                                           */
166/* SHA calculations                                          */
167/*                                                           */
168/* --------------------------------------------------------- */
169#if (SIZEOF_INT == 4)
170typedef unsigned int uint32;
171#elif (SIZEOF_SHORT == 4)
172typedef unsigned short uint32;
173#else
174typedef unsigned int uint32;
175#endif /* HAVEUINT32 */
176
177int sha_hash(int *data, int *hash);
178int sha_init(int *hash);
179char *shahash(char *str);       /* NOT THREAD SAFE */
180void shahash_r(const char* str, char hashbuf[40]); /* USE ME */
181
182int strprintsha(char *dest, int *hashval);
183
184
185/* --------------------------------------------------------- */
186/*                                                           */
187/* Hashtable functions                                       */
188/*                                                           */
189/* --------------------------------------------------------- */
190typedef int (*KEYHASHFUNC)(const void *key);
191typedef int (*KEYCOMPAREFUNC)(const void *key1, const void *key2);
192typedef int (*TABLEWALKFUNC)(void *user_data, const void *key, void *data);
193
194typedef void *HASHTABLE;
195
196HASHTABLE ghash_create(int buckets, KEYHASHFUNC hash, KEYCOMPAREFUNC cmp);
197void ghash_destroy(HASHTABLE tbl);
198void *ghash_get(HASHTABLE tbl, const void *key);
199int ghash_put(HASHTABLE tbl, const void *key, void *value);
200int ghash_remove(HASHTABLE tbl, const void *key);
201int ghash_walk(HASHTABLE tbl, TABLEWALKFUNC func, void *user_data);
202int str_hash_code(const char *s);
203
204
205/* --------------------------------------------------------- */
206/*                                                           */
207/* XML escaping utils                                        */
208/*                                                           */
209/* --------------------------------------------------------- */
210char *strescape(pool p, char *buf); /* Escape <>&'" chars */
211
212
213/* --------------------------------------------------------- */
214/*                                                           */
215/* String pools (spool) functions                            */
216/*                                                           */
217/* --------------------------------------------------------- */
218struct spool_node
219{
220    char *c;
221    struct spool_node *next;
222};
223
224typedef struct spool_struct
225{
226    pool p;
227    int len;
228    struct spool_node *last;
229    struct spool_node *first;
230} *spool;
231
232spool spool_new(pool p); /* create a string pool */
233void spooler(spool s, ...); /* append all the char * args to the pool, terminate args with s again */
234char *spool_print(spool s); /* return a big string */
235void spool_add(spool s, char *str); /* add a single char to the pool */
236char *spools(pool p, ...); /* wrap all the spooler stuff in one function, the happy fun ball! */
237
238
239/* --------------------------------------------------------- */
240/*                                                           */
241/* xmlnodes - Document Object Model                          */
242/*                                                           */
243/* --------------------------------------------------------- */
244#define NTYPE_TAG    0
245#define NTYPE_ATTRIB 1
246#define NTYPE_CDATA  2
247
248#define NTYPE_LAST   2
249#define NTYPE_UNDEF  -1
250
251/* --------------------------------------------------------------------------
252   Node structure. Do not use directly! Always use accessor macros
253   and methods!
254   -------------------------------------------------------------------------- */
255typedef struct xmlnode_t
256{
257     char*               name;
258     unsigned short      type;
259     char*               data;
260     int                 data_sz;
261     int                 complete;
262     pool               p;
263     struct xmlnode_t*  parent;
264     struct xmlnode_t*  firstchild; 
265     struct xmlnode_t*  lastchild;
266     struct xmlnode_t*  prev; 
267     struct xmlnode_t*  next;
268     struct xmlnode_t*  firstattrib;
269     struct xmlnode_t*  lastattrib;
270} _xmlnode, *xmlnode;
271
272/* Node creation routines */
273xmlnode  xmlnode_wrap(xmlnode x,const char* wrapper);
274xmlnode  xmlnode_new_tag(const char* name);
275xmlnode  xmlnode_new_tag_pool(pool p, const char* name);
276xmlnode  xmlnode_insert_tag(xmlnode parent, const char* name); 
277xmlnode  xmlnode_insert_cdata(xmlnode parent, const char* CDATA, unsigned int size);
278xmlnode  xmlnode_insert_tag_node(xmlnode parent, xmlnode node);
279void     xmlnode_insert_node(xmlnode parent, xmlnode node);
280xmlnode  xmlnode_str(char *str, int len);
281xmlnode  xmlnode_file(char *file);
282xmlnode  xmlnode_dup(xmlnode x); /* duplicate x */
283xmlnode  xmlnode_dup_pool(pool p, xmlnode x);
284
285/* Node Memory Pool */
286pool xmlnode_pool(xmlnode node);
287xmlnode _xmlnode_new(pool p, const char *name, unsigned int type);
288
289/* Node editing */
290void xmlnode_hide(xmlnode child);
291void xmlnode_hide_attrib(xmlnode parent, const char *name);
292
293/* Node deletion routine, also frees the node pool! */
294void xmlnode_free(xmlnode node);
295
296/* Locates a child tag by name and returns it */
297xmlnode  xmlnode_get_tag(xmlnode parent, const char* name);
298char* xmlnode_get_tag_data(xmlnode parent, const char* name);
299
300/* Attribute accessors */
301void     xmlnode_put_attrib(xmlnode owner, const char* name, const char* value);
302char*    xmlnode_get_attrib(xmlnode owner, const char* name);
303void     xmlnode_put_expat_attribs(xmlnode owner, const char** atts);
304
305/* Bastard am I, but these are fun for internal use ;-) */
306void     xmlnode_put_vattrib(xmlnode owner, const char* name, void *value);
307void*    xmlnode_get_vattrib(xmlnode owner, const char* name);
308
309/* Node traversal routines */
310xmlnode  xmlnode_get_firstattrib(xmlnode parent);
311xmlnode  xmlnode_get_firstchild(xmlnode parent);
312xmlnode  xmlnode_get_lastchild(xmlnode parent);
313xmlnode  xmlnode_get_nextsibling(xmlnode sibling);
314xmlnode  xmlnode_get_prevsibling(xmlnode sibling);
315xmlnode  xmlnode_get_parent(xmlnode node);
316
317/* Node information routines */
318char*    xmlnode_get_name(xmlnode node);
319char*    xmlnode_get_data(xmlnode node);
320int      xmlnode_get_datasz(xmlnode node);
321int      xmlnode_get_type(xmlnode node);
322
323int      xmlnode_has_children(xmlnode node);
324int      xmlnode_has_attribs(xmlnode node);
325
326/* Node-to-string translation */
327char*    xmlnode2str(xmlnode node);
328
329/* Node-to-terminated-string translation
330   -- useful for interfacing w/ scripting langs */
331char*    xmlnode2tstr(xmlnode node);
332
333int      xmlnode_cmp(xmlnode a, xmlnode b); /* compares a and b for equality */
334
335int      xmlnode2file(char *file, xmlnode node); /* writes node to file */
336
337/* Expat callbacks */
338void expat_startElement(void* userdata, const char* name, const char** atts);
339void expat_endElement(void* userdata, const char* name);
340void expat_charData(void* userdata, const char* s, int len);
341
342/* SHA.H */
343/*
344 * The contents of this file are subject to the Mozilla Public
345 * License Version 1.1 (the "License"); you may not use this file
346 * except in compliance with the License. You may obtain a copy of
347 * the License at http://www.mozilla.org/MPL/
348 *
349 * Software distributed under the License is distributed on an "AS
350 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
351 * implied. See the License for the specific language governing
352 * rights and limitations under the License.
353 *
354 * The Original Code is SHA 180-1 Header File
355 *
356 * The Initial Developer of the Original Code is Paul Kocher of
357 * Cryptography Research.  Portions created by Paul Kocher are
358 * Copyright (C) 1995-9 by Cryptography Research, Inc.  All
359 * Rights Reserved.
360 *
361 * Contributor(s):
362 *
363 *     Paul Kocher
364 *
365 * Alternatively, the contents of this file may be used under the
366 * terms of the GNU General Public License Version 2 or later (the
367 * "GPL"), in which case the provisions of the GPL are applicable
368 * instead of those above.  If you wish to allow use of your
369 * version of this file only under the terms of the GPL and not to
370 * allow others to use your version of this file under the MPL,
371 * indicate your decision by deleting the provisions above and
372 * replace them with the notice and other provisions required by
373 * the GPL.  If you do not delete the provisions above, a recipient
374 * may use your version of this file under either the MPL or the
375 * GPL.
376 */
377
378typedef struct {
379  unsigned long H[5];
380  unsigned long W[80];
381  int lenW;
382  unsigned long sizeHi,sizeLo;
383} SHA_CTX;
384
385
386void shaInit(SHA_CTX *ctx);
387void shaUpdate(SHA_CTX *ctx, unsigned char *dataIn, int len);
388void shaFinal(SHA_CTX *ctx, unsigned char hashout[20]);
389void shaBlock(unsigned char *dataIn, int len, unsigned char hashout[20]);
390
391
392/* END SHA.H */
393
394#ifdef __cplusplus
395}
396#endif
397
398#endif /* INCL_LIBXODE_H */
Note: See TracBrowser for help on using the repository browser.